diff options
Diffstat (limited to 'meta/recipes-core/initrdscripts/files/init-live.sh')
-rw-r--r-- | meta/recipes-core/initrdscripts/files/init-live.sh | 235 |
1 files changed, 235 insertions, 0 deletions
diff --git a/meta/recipes-core/initrdscripts/files/init-live.sh b/meta/recipes-core/initrdscripts/files/init-live.sh new file mode 100644 index 0000000000..d852c5737f --- /dev/null +++ b/meta/recipes-core/initrdscripts/files/init-live.sh | |||
@@ -0,0 +1,235 @@ | |||
1 | #!/bin/sh | ||
2 | |||
3 | PATH=/sbin:/bin:/usr/sbin:/usr/bin | ||
4 | |||
5 | ROOT_MOUNT="/rootfs/" | ||
6 | ROOT_IMAGE="rootfs.img" | ||
7 | MOUNT="/bin/mount" | ||
8 | UMOUNT="/bin/umount" | ||
9 | ISOLINUX="" | ||
10 | |||
11 | ROOT_DISK="" | ||
12 | |||
13 | # Copied from initramfs-framework. The core of this script probably should be | ||
14 | # turned into initramfs-framework modules to reduce duplication. | ||
15 | udev_daemon() { | ||
16 | OPTIONS="/sbin/udev/udevd /sbin/udevd /lib/udev/udevd /lib/systemd/systemd-udevd" | ||
17 | |||
18 | for o in $OPTIONS; do | ||
19 | if [ -x "$o" ]; then | ||
20 | echo $o | ||
21 | return 0 | ||
22 | fi | ||
23 | done | ||
24 | |||
25 | return 1 | ||
26 | } | ||
27 | |||
28 | _UDEV_DAEMON=`udev_daemon` | ||
29 | |||
30 | early_setup() { | ||
31 | mkdir -p /proc | ||
32 | mkdir -p /sys | ||
33 | mount -t proc proc /proc | ||
34 | mount -t sysfs sysfs /sys | ||
35 | mount -t devtmpfs none /dev | ||
36 | |||
37 | # support modular kernel | ||
38 | modprobe isofs 2> /dev/null | ||
39 | |||
40 | mkdir -p /run | ||
41 | mkdir -p /var/run | ||
42 | |||
43 | $_UDEV_DAEMON --daemon | ||
44 | udevadm trigger --action=add | ||
45 | } | ||
46 | |||
47 | read_args() { | ||
48 | [ -z "$CMDLINE" ] && CMDLINE=`cat /proc/cmdline` | ||
49 | for arg in $CMDLINE; do | ||
50 | optarg=`expr "x$arg" : 'x[^=]*=\(.*\)'` | ||
51 | case $arg in | ||
52 | root=*) | ||
53 | ROOT_DEVICE=$optarg ;; | ||
54 | rootimage=*) | ||
55 | ROOT_IMAGE=$optarg ;; | ||
56 | rootfstype=*) | ||
57 | modprobe $optarg 2> /dev/null ;; | ||
58 | LABEL=*) | ||
59 | label=$optarg ;; | ||
60 | video=*) | ||
61 | video_mode=$arg ;; | ||
62 | vga=*) | ||
63 | vga_mode=$arg ;; | ||
64 | console=*) | ||
65 | if [ -z "${console_params}" ]; then | ||
66 | console_params=$arg | ||
67 | else | ||
68 | console_params="$console_params $arg" | ||
69 | fi ;; | ||
70 | debugshell*) | ||
71 | if [ -z "$optarg" ]; then | ||
72 | shelltimeout=30 | ||
73 | else | ||
74 | shelltimeout=$optarg | ||
75 | fi | ||
76 | esac | ||
77 | done | ||
78 | } | ||
79 | |||
80 | boot_live_root() { | ||
81 | # Watches the udev event queue, and exits if all current events are handled | ||
82 | udevadm settle --timeout=3 --quiet | ||
83 | killall "${_UDEV_DAEMON##*/}" 2>/dev/null | ||
84 | |||
85 | # Allow for identification of the real root even after boot | ||
86 | mkdir -p ${ROOT_MOUNT}/media/realroot | ||
87 | mount -n --move "/run/media/${ROOT_DISK}" ${ROOT_MOUNT}/media/realroot | ||
88 | |||
89 | # Move the mount points of some filesystems over to | ||
90 | # the corresponding directories under the real root filesystem. | ||
91 | for dir in `awk '/\/dev.* \/run\/media/{print $2}' /proc/mounts`; do | ||
92 | mkdir -p ${ROOT_MOUNT}/media/${dir##*/} | ||
93 | mount -n --move $dir ${ROOT_MOUNT}/media/${dir##*/} | ||
94 | done | ||
95 | mount -n --move /proc ${ROOT_MOUNT}/proc | ||
96 | mount -n --move /sys ${ROOT_MOUNT}/sys | ||
97 | mount -n --move /dev ${ROOT_MOUNT}/dev | ||
98 | |||
99 | cd $ROOT_MOUNT | ||
100 | |||
101 | # busybox switch_root supports -c option | ||
102 | exec switch_root -c /dev/console $ROOT_MOUNT /sbin/init $CMDLINE || | ||
103 | fatal "Couldn't switch_root, dropping to shell" | ||
104 | } | ||
105 | |||
106 | fatal() { | ||
107 | echo $1 >$CONSOLE | ||
108 | echo >$CONSOLE | ||
109 | exec sh | ||
110 | } | ||
111 | |||
112 | early_setup | ||
113 | |||
114 | [ -z "$CONSOLE" ] && CONSOLE="/dev/console" | ||
115 | |||
116 | read_args | ||
117 | |||
118 | echo "Waiting for removable media..." | ||
119 | C=0 | ||
120 | while true | ||
121 | do | ||
122 | for i in `ls /run/media 2>/dev/null`; do | ||
123 | if [ -f /run/media/$i/$ROOT_IMAGE ] ; then | ||
124 | found="yes" | ||
125 | ROOT_DISK="$i" | ||
126 | break | ||
127 | elif [ -f /run/media/$i/isolinux/$ROOT_IMAGE ]; then | ||
128 | found="yes" | ||
129 | ISOLINUX="isolinux" | ||
130 | ROOT_DISK="$i" | ||
131 | break | ||
132 | fi | ||
133 | done | ||
134 | if [ "$found" = "yes" ]; then | ||
135 | break; | ||
136 | fi | ||
137 | # don't wait for more than $shelltimeout seconds, if it's set | ||
138 | if [ -n "$shelltimeout" ]; then | ||
139 | echo -n " " $(( $shelltimeout - $C )) | ||
140 | if [ $C -ge $shelltimeout ]; then | ||
141 | echo "..." | ||
142 | echo "Mounted filesystems" | ||
143 | mount | grep media | ||
144 | echo "Available block devices" | ||
145 | cat /proc/partitions | ||
146 | fatal "Cannot find $ROOT_IMAGE file in /run/media/* , dropping to a shell " | ||
147 | fi | ||
148 | C=$(( C + 1 )) | ||
149 | fi | ||
150 | sleep 1 | ||
151 | done | ||
152 | |||
153 | # Try to mount the root image read-write and then boot it up. | ||
154 | # This function distinguishes between a read-only image and a read-write image. | ||
155 | # In the former case (typically an iso), it tries to make a union mount if possible. | ||
156 | # In the latter case, the root image could be mounted and then directly booted up. | ||
157 | mount_and_boot() { | ||
158 | mkdir $ROOT_MOUNT | ||
159 | mknod /dev/loop0 b 7 0 2>/dev/null | ||
160 | |||
161 | if ! mount -o rw,loop,noatime,nodiratime /run/media/$ROOT_DISK/$ISOLINUX/$ROOT_IMAGE $ROOT_MOUNT ; then | ||
162 | fatal "Could not mount rootfs image" | ||
163 | fi | ||
164 | |||
165 | if touch $ROOT_MOUNT/bin 2>/dev/null; then | ||
166 | # The root image is read-write, directly boot it up. | ||
167 | boot_live_root | ||
168 | fi | ||
169 | |||
170 | # determine which unification filesystem to use | ||
171 | union_fs_type="" | ||
172 | if grep -q -w "overlayfs" /proc/filesystems; then | ||
173 | union_fs_type="overlayfs" | ||
174 | elif grep -q -w "aufs" /proc/filesystems; then | ||
175 | union_fs_type="aufs" | ||
176 | else | ||
177 | union_fs_type="" | ||
178 | fi | ||
179 | |||
180 | # make a union mount if possible | ||
181 | case $union_fs_type in | ||
182 | "overlayfs") | ||
183 | mkdir -p /rootfs.ro /rootfs.rw | ||
184 | if ! mount -n --move $ROOT_MOUNT /rootfs.ro; then | ||
185 | rm -rf /rootfs.ro /rootfs.rw | ||
186 | fatal "Could not move rootfs mount point" | ||
187 | else | ||
188 | mount -t tmpfs -o rw,noatime,mode=755 tmpfs /rootfs.rw | ||
189 | mount -t overlayfs -o "lowerdir=/rootfs.ro,upperdir=/rootfs.rw" overlayfs $ROOT_MOUNT | ||
190 | mkdir -p $ROOT_MOUNT/rootfs.ro $ROOT_MOUNT/rootfs.rw | ||
191 | mount --move /rootfs.ro $ROOT_MOUNT/rootfs.ro | ||
192 | mount --move /rootfs.rw $ROOT_MOUNT/rootfs.rw | ||
193 | fi | ||
194 | ;; | ||
195 | "aufs") | ||
196 | mkdir -p /rootfs.ro /rootfs.rw | ||
197 | if ! mount -n --move $ROOT_MOUNT /rootfs.ro; then | ||
198 | rm -rf /rootfs.ro /rootfs.rw | ||
199 | fatal "Could not move rootfs mount point" | ||
200 | else | ||
201 | mount -t tmpfs -o rw,noatime,mode=755 tmpfs /rootfs.rw | ||
202 | mount -t aufs -o "dirs=/rootfs.rw=rw:/rootfs.ro=ro" aufs $ROOT_MOUNT | ||
203 | mkdir -p $ROOT_MOUNT/rootfs.ro $ROOT_MOUNT/rootfs.rw | ||
204 | mount --move /rootfs.ro $ROOT_MOUNT/rootfs.ro | ||
205 | mount --move /rootfs.rw $ROOT_MOUNT/rootfs.rw | ||
206 | fi | ||
207 | ;; | ||
208 | "") | ||
209 | mount -t tmpfs -o rw,noatime,mode=755 tmpfs $ROOT_MOUNT/media | ||
210 | ;; | ||
211 | esac | ||
212 | |||
213 | # boot the image | ||
214 | boot_live_root | ||
215 | } | ||
216 | |||
217 | case $label in | ||
218 | boot) | ||
219 | mount_and_boot | ||
220 | ;; | ||
221 | install|install-efi) | ||
222 | if [ -f /run/media/$i/$ISOLINUX/$ROOT_IMAGE ] ; then | ||
223 | ./$label.sh $i/$ISOLINUX $ROOT_IMAGE $video_mode $vga_mode $console_params | ||
224 | else | ||
225 | fatal "Could not find $label script" | ||
226 | fi | ||
227 | |||
228 | # If we're getting here, we failed... | ||
229 | fatal "Installation image failed" | ||
230 | ;; | ||
231 | *) | ||
232 | # Not sure what boot label is provided. Try to boot to avoid locking up. | ||
233 | mount_and_boot | ||
234 | ;; | ||
235 | esac | ||