summaryrefslogtreecommitdiffstats
path: root/meta/recipes-core/initrdscripts/files/init-live.sh
diff options
context:
space:
mode:
Diffstat (limited to 'meta/recipes-core/initrdscripts/files/init-live.sh')
-rw-r--r--meta/recipes-core/initrdscripts/files/init-live.sh218
1 files changed, 218 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..3f5fde83bf
--- /dev/null
+++ b/meta/recipes-core/initrdscripts/files/init-live.sh
@@ -0,0 +1,218 @@
1#!/bin/sh
2
3PATH=/sbin:/bin:/usr/sbin:/usr/bin
4
5ROOT_MOUNT="/rootfs/"
6ROOT_IMAGE="rootfs.img"
7MOUNT="/bin/mount"
8UMOUNT="/bin/umount"
9ISOLINUX=""
10
11# Copied from initramfs-framework. The core of this script probably should be
12# turned into initramfs-framework modules to reduce duplication.
13udev_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
28early_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
45read_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 rootfstype=*)
53 modprobe $optarg 2> /dev/null ;;
54 LABEL=*)
55 label=$optarg ;;
56 video=*)
57 video_mode=$arg ;;
58 vga=*)
59 vga_mode=$arg ;;
60 console=*)
61 if [ -z "${console_params}" ]; then
62 console_params=$arg
63 else
64 console_params="$console_params $arg"
65 fi ;;
66 debugshell*)
67 if [ -z "$optarg" ]; then
68 shelltimeout=30
69 else
70 shelltimeout=$optarg
71 fi
72 esac
73 done
74}
75
76boot_live_root() {
77 # Watches the udev event queue, and exits if all current events are handled
78 udevadm settle --timeout=3 --quiet
79 killall "${_UDEV_DAEMON##*/}" 2>/dev/null
80
81 # Move the mount points of some filesystems over to
82 # the corresponding directories under the real root filesystem.
83 for dir in `awk '/\/dev.* \/media/{print $2}' /proc/mounts`; do
84 mkdir -p ${ROOT_MOUNT}/$dir
85 mount -n --move $dir ${ROOT_MOUNT}/$dir
86 done
87 mount -n --move /proc ${ROOT_MOUNT}/proc
88 mount -n --move /sys ${ROOT_MOUNT}/sys
89 mount -n --move /dev ${ROOT_MOUNT}/dev
90
91 cd $ROOT_MOUNT
92 exec switch_root -c /dev/console $ROOT_MOUNT /sbin/init
93}
94
95fatal() {
96 echo $1 >$CONSOLE
97 echo >$CONSOLE
98 exec sh
99}
100
101early_setup
102
103[ -z "$CONSOLE" ] && CONSOLE="/dev/console"
104
105read_args
106
107echo "Waiting for removable media..."
108C=0
109while true
110do
111 for i in `ls /media 2>/dev/null`; do
112 if [ -f /media/$i/$ROOT_IMAGE ] ; then
113 found="yes"
114 break
115 elif [ -f /media/$i/isolinux/$ROOT_IMAGE ]; then
116 found="yes"
117 ISOLINUX="isolinux"
118 break
119 fi
120 done
121 if [ "$found" = "yes" ]; then
122 break;
123 fi
124 # don't wait for more than $shelltimeout seconds, if it's set
125 if [ -n "$shelltimeout" ]; then
126 echo -n " " $(( $shelltimeout - $C ))
127 if [ $C -ge $shelltimeout ]; then
128 echo "..."
129 echo "Mounted filesystems"
130 mount | grep media
131 echo "Available block devices"
132 ls /dev/sd*
133 fatal "Cannot find rootfs.img file in /media/* , dropping to a shell "
134 fi
135 C=$(( C + 1 ))
136 fi
137 sleep 1
138done
139
140# Try to mount the root image read-write and then boot it up.
141# This function distinguishes between a read-only image and a read-write image.
142# In the former case (typically an iso), it tries to make a union mount if possible.
143# In the latter case, the root image could be mounted and then directly booted up.
144mount_and_boot() {
145 mkdir $ROOT_MOUNT
146 mknod /dev/loop0 b 7 0 2>/dev/null
147
148 if ! mount -o rw,loop,noatime,nodiratime /media/$i/$ISOLINUX/$ROOT_IMAGE $ROOT_MOUNT ; then
149 fatal "Could not mount rootfs image"
150 fi
151
152 if touch $ROOT_MOUNT/bin 2>/dev/null; then
153 # The root image is read-write, directly boot it up.
154 boot_live_root
155 fi
156
157 # determine which unification filesystem to use
158 union_fs_type=""
159 if grep -q -w "overlayfs" /proc/filesystems; then
160 union_fs_type="overlayfs"
161 elif grep -q -w "aufs" /proc/filesystems; then
162 union_fs_type="aufs"
163 else
164 union_fs_type=""
165 fi
166
167 # make a union mount if possible
168 case $union_fs_type in
169 "overlayfs")
170 mkdir -p /rootfs.ro /rootfs.rw
171 if ! mount -n --move $ROOT_MOUNT /rootfs.ro; then
172 rm -rf /rootfs.ro /rootfs.rw
173 fatal "Could not move rootfs mount point"
174 else
175 mount -t tmpfs -o rw,noatime,mode=755 tmpfs /rootfs.rw
176 mount -t overlayfs -o "lowerdir=/rootfs.ro,upperdir=/rootfs.rw" overlayfs $ROOT_MOUNT
177 mkdir -p $ROOT_MOUNT/rootfs.ro $ROOT_MOUNT/rootfs.rw
178 mount --move /rootfs.ro $ROOT_MOUNT/rootfs.ro
179 mount --move /rootfs.rw $ROOT_MOUNT/rootfs.rw
180 fi
181 ;;
182 "aufs")
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 aufs -o "dirs=/rootfs.rw=rw:/rootfs.ro=ro" aufs $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 "")
196 mount -t tmpfs -o rw,noatime,mode=755 tmpfs $ROOT_MOUNT/media
197 ;;
198 esac
199
200 # boot the image
201 boot_live_root
202}
203
204case $label in
205 boot)
206 mount_and_boot
207 ;;
208 install|install-efi)
209 if [ -f /media/$i/$ISOLINUX/$ROOT_IMAGE ] ; then
210 ./$label.sh $i/$ISOLINUX $ROOT_IMAGE $video_mode $vga_mode $console_params
211 else
212 fatal "Could not find $label script"
213 fi
214
215 # If we're getting here, we failed...
216 fatal "Installation image failed"
217 ;;
218esac