]> git.ipfire.org Git - thirdparty/dracut.git/blob - modules.d/99base/init
Merge commit 'dillow/master'
[thirdparty/dracut.git] / modules.d / 99base / init
1 #!/bin/sh
2 #
3 # Licensed under the GPLv2
4 #
5 # Copyright 2008, Red Hat, Inc.
6 # Jeremy Katz <katzj@redhat.com>
7
8 emergency_shell()
9 {
10 source_all emergency
11 echo ; echo
12 echo "Bug in initramfs /init detected. Dropping to a shell. Good luck!"
13 echo
14 sh -i
15 }
16
17 export PATH=/sbin:/bin:/usr/sbin:/usr/bin
18 export TERM=linux
19 NEWROOT="/sysroot"
20
21 trap "emergency_shell" 0
22
23 . /lib/dracut-lib
24
25 mknod /dev/null c 1 3
26
27 # mount some important things
28 mount -t proc /proc /proc >/dev/null 2>&1
29 mount -t sysfs /sys /sys >/dev/null 2>&1
30 mount -t tmpfs -omode=0755 udev /dev >/dev/null 2>&1
31
32 getarg rdinitdebug && set -x
33 # Make some basic devices first, let udev handle the rest
34 mknod /dev/null c 1 3
35 mknod /dev/ptmx c 5 2
36 mknod /dev/console c 5 1
37 mkdir /dev/pts
38 mount -t devpts -o gid=5,mode=620 /dev/pts /dev/pts >/dev/null 2>&1
39
40 # run scriptlets to parse the command line
41 getarg 'rdbreak=cmdline' && emergency_shell
42 source_all cmdline
43
44 if [ -z "$rootok" -a -n "${root%%error:*}" ]; then
45 root="error: No handler for root=${root}"
46 fi
47
48 if [ -z "${root%%error:*}" ]; then
49 case "${root%%:*}" in
50 '') echo "FATAL: no root= option specified, and no network support" ;;
51 error) echo "FATAL: ${root#error:}" ;;
52 esac
53 emergency_shell
54 fi
55
56 # Network root scripts may need updated root= options,
57 # so deposit them where they can see them (udev purges the env)
58 {
59 echo "root='$root'"
60 echo "rflags='$rflags'"
61 echo "fstype='$fstype'"
62 echo "NEWROOT='$NEWROOT'"
63 } > /tmp/root.info
64
65 # pre-udev scripts run before udev starts, and are run only once.
66 getarg 'rdbreak=pre-udev' && emergency_shell
67 source_all pre-udev
68
69 # start up udev and trigger cold plugs
70 udevd --daemon
71 getarg rdudevinfo && udevadm control --log_priority=info
72 getarg rdudevdebug && udevadm control --log_priority=debug
73
74 source_all pre-trigger
75
76 # then the rest
77 [ -f /tmp/udevtriggeropts ] && udevtriggeropts=$(while read line; do echo $line;done < /tmp/udevtriggeropts)
78 udevadm trigger $udevtriggeropts >/dev/null 2>&1
79 udevadm settle --timeout=30 >/dev/null 2>&1
80
81 # pre-mount happens before we try to mount the root filesystem,
82 # and happens once.
83 getarg 'rdbreak=pre-mount' && emergency_shell
84 source_all pre-mount
85 getarg 'rdbreak=mount' && emergency_shell
86
87 # mount scripts actually try to mount the root filesystem, and may
88 # be sourced any number of times. As soon as one suceeds, no more are sourced.
89 i=0
90 while :; do
91 [ -d "$NEWROOT/proc" ] && break;
92
93 for f in /mount/*.sh; do
94 [ -x "$f" ] && . "$f";
95 [ "$ROOTFS_MOUNTED" ] && break;
96 done
97
98 sleep 0.5
99 i=$(($i+1))
100 { flock -s 9 ; [ $i -gt 20 ] && emergency_shell; } 9>/.console_lock
101 done
102
103 # pre pivot scripts are sourced just before we switch over to the new root.
104 getarg 'rdbreak=pre-pivot' && emergency_shell
105 source_all pre-pivot
106
107 # by the time we get here, the root filesystem should be mounted.
108 # Try to find init.
109 for i in "$(getarg init=)" /sbin/init /etc/init /init /bin/sh; do
110 [ -f "$NEWROOT$i" -a -x "$NEWROOT$i" ] && { INIT="$i"; break; }
111 done
112 [ "$INIT" ] || {
113 echo "Cannot find init! Please check to make sure you passed"
114 echo "a valid root filesystem! Dropping to a shell."
115 emergency_shell
116 }
117
118 getarg rdbreak && emergency_shell
119 kill $(pidof udevd)
120
121 # Clean up the environment
122 for i in $(export -p); do
123 i=${i#declare -x}
124 i=${i#export}
125 i=${i%%=*}
126 [ "$i" = "root" -o "$i" = "PATH" -o "$i" = "HOME" -o "$i" = "TERM" ] || unset $i
127 done
128
129 initargs=""
130 for x in "$@"; do
131 [ "${x%%=*}" = "console" ] && continue
132 [ "${x%%=*}" = "BOOT_IMAGE" ] && continue
133 [ "${x%%=*}" = "rdbreak" ] && continue
134 [ "${x%%=*}" = "rdinitdebug" ] && continue
135 [ "${x%%=*}" = "rdudevinfo" ] && continue
136 [ "${x%%=*}" = "rdudevdebug" ] && continue
137 initargs="$initargs $x"
138 done
139 exec switch_root "$NEWROOT" "$INIT" $initargs || {
140 # davej doesn't like initrd bugs
141 echo "Something went very badly wrong in the initrd. Please "
142 echo "file a bug against mkinitrd."
143 emergency_shell
144 }