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