121 lines
3.2 KiB
Bash
Executable File
121 lines
3.2 KiB
Bash
Executable File
#!/bin/sh
|
|
#
|
|
# Licensed under the GPLv2
|
|
#
|
|
# Copyright 2008, Red Hat, Inc.
|
|
# Jeremy Katz <katzj@redhat.com>
|
|
|
|
emergency_shell()
|
|
{
|
|
source_all emergency
|
|
echo ; echo
|
|
echo "Bug in initramfs /init detected. Dropping to a shell. Good luck!"
|
|
echo
|
|
sh -i
|
|
}
|
|
|
|
source_all() {
|
|
local f
|
|
[ "$1" ] && [ -d "/$1" ] || return
|
|
for f in "/$1"/*.sh; do [ -f "$f" ] && . "$f"; done
|
|
}
|
|
|
|
export PATH=/sbin:/bin:/usr/sbin:/usr/bin
|
|
export TERM=linux
|
|
NEWROOT="/sysroot"
|
|
|
|
trap "emergency_shell" 0
|
|
|
|
. /lib/dracut-lib
|
|
|
|
mknod /dev/null c 1 3
|
|
|
|
# mount some important things
|
|
mount -t proc /proc /proc >/dev/null 2>&1
|
|
mount -t sysfs /sys /sys >/dev/null 2>&1
|
|
mount -t tmpfs -omode=0755 udev /dev >/dev/null 2>&1
|
|
|
|
getarg rdinitdebug && set -x
|
|
# Make some basic devices first, let udev handle the rest
|
|
mknod /dev/null c 1 3
|
|
mknod /dev/ptmx c 5 2
|
|
mknod /dev/console c 5 1
|
|
mkdir /dev/pts
|
|
mount -t devpts -o gid=5,mode=620 /dev/pts /dev/pts >/dev/null 2>&1
|
|
|
|
# pre-udev scripts run before udev starts, and are run only once.
|
|
getarg 'rdbreak=pre-udev' && emergency_shell
|
|
source_all pre-udev
|
|
|
|
# start up udev and trigger cold plugs
|
|
udevd --daemon
|
|
getarg rdudevinfo && udevadm control --log_priority=info
|
|
getarg rdudevdebug && udevadm control --log_priority=debug
|
|
udevadm trigger >/dev/null 2>&1
|
|
udevadm settle --timeout=30 >/dev/null 2>&1
|
|
|
|
# pre-mount happens before we try to mount the root filesystem,
|
|
# and happens once.
|
|
getarg 'rdbreak=pre-mount' && emergency_shell
|
|
source_all pre-mount
|
|
getarg 'rdbreak=mount' && emergency_shell
|
|
|
|
# mount scripts actually try to mount the root filesystem, and may
|
|
# be sourced any number of times. As soon as one suceeds, no more are sourced.
|
|
i=0
|
|
while :; do
|
|
[ -d "$NEWROOT/proc" ] && break;
|
|
|
|
for f in /mount/*.sh; do
|
|
[ -x "$f" ] && . "$f";
|
|
[ "$ROOTFS_MOUNTED" ] && break;
|
|
done
|
|
|
|
sleep 0.5
|
|
i=$(($i+1))
|
|
{ flock -s 9 ; [ $i -gt 20 ] && emergency_shell; } 9>/.console_lock
|
|
done
|
|
|
|
# pre pivot scripts are sourced just before we switch over to the new root.
|
|
getarg 'rdbreak=pre-pivot' && emergency_shell
|
|
source_all pre-pivot
|
|
|
|
# by the time we get here, the root filesystem should be mounted.
|
|
# Try to find init.
|
|
for i in "$(getarg init=)" /sbin/init /etc/init /init /bin/sh; do
|
|
[ -f "$NEWROOT$i" -a -x "$NEWROOT$i" ] && { INIT="$i"; break; }
|
|
done
|
|
[ "$INIT" ] || {
|
|
echo "Cannot find init! Please check to make sure you passed"
|
|
echo "a valid root filesystem! Dropping to a shell."
|
|
emergency_shell
|
|
}
|
|
|
|
getarg rdbreak && emergency_shell
|
|
kill $(pidof udevd)
|
|
|
|
# Clean up the environment
|
|
for i in $(export -p); do
|
|
i=${i#declare -x}
|
|
i=${i#export}
|
|
i=${i%%=*}
|
|
[ "$i" = "root" -o "$i" = "PATH" -o "$i" = "HOME" -o "$i" = "TERM" ] || unset $i
|
|
done
|
|
|
|
initargs=""
|
|
for x in "$@"; do
|
|
[ "${x%%=*}" = "console" ] && continue
|
|
[ "${x%%=*}" = "BOOT_IMAGE" ] && continue
|
|
[ "${x%%=*}" = "rdbreak" ] && continue
|
|
[ "${x%%=*}" = "rdinitdebug" ] && continue
|
|
[ "${x%%=*}" = "rdudevinfo" ] && continue
|
|
[ "${x%%=*}" = "rdudevdebug" ] && continue
|
|
initargs="$initargs $x"
|
|
done
|
|
exec switch_root "$NEWROOT" "$INIT" $initargs || {
|
|
# davej doesn't like initrd bugs
|
|
echo "Something went very badly wrong in the initrd. Please "
|
|
echo "file a bug against mkinitrd."
|
|
emergency_shell
|
|
}
|