blob: 65183d7eec35f53193ae27beeb6782abefdc14e1 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/bin/sh
2
3PATH=/sbin:/bin:/usr/sbin:/usr/bin
4
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05005ROOT_MOUNT="/rootfs"
Patrick Williamsc124f4f2015-09-15 14:41:29 -05006ROOT_IMAGE="rootfs.img"
7MOUNT="/bin/mount"
8UMOUNT="/bin/umount"
9ISOLINUX=""
10
11ROOT_DISK=""
12
13# Copied from initramfs-framework. The core of this script probably should be
14# turned into initramfs-framework modules to reduce duplication.
15udev_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
30early_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
47read_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
80boot_live_root() {
81 # Watches the udev event queue, and exits if all current events are handled
82 udevadm settle --timeout=3 --quiet
Patrick Williamsc0f7c042017-02-23 20:41:17 -060083 # Kills the current udev running processes, which survived after
84 # device node creation events were handled, to avoid unexpected behavior
85 killall -9 "${_UDEV_DAEMON##*/}" 2>/dev/null
Patrick Williamsc124f4f2015-09-15 14:41:29 -050086
Brad Bishopd7bf8c12018-02-25 22:55:05 -050087 # Don't run systemd-update-done on systemd-based live systems
88 # because it triggers a slow rebuild of ldconfig caches.
89 touch ${ROOT_MOUNT}/etc/.updated ${ROOT_MOUNT}/var/.updated
90
Patrick Williamsc124f4f2015-09-15 14:41:29 -050091 # Allow for identification of the real root even after boot
92 mkdir -p ${ROOT_MOUNT}/media/realroot
93 mount -n --move "/run/media/${ROOT_DISK}" ${ROOT_MOUNT}/media/realroot
94
95 # Move the mount points of some filesystems over to
96 # the corresponding directories under the real root filesystem.
97 for dir in `awk '/\/dev.* \/run\/media/{print $2}' /proc/mounts`; do
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080098 # Parse any OCT or HEX encoded chars such as spaces
99 # in the mount points to actual ASCII chars
100 dir=`printf $dir`
101 mkdir -p "${ROOT_MOUNT}/media/${dir##*/}"
102 mount -n --move "$dir" "${ROOT_MOUNT}/media/${dir##*/}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500103 done
104 mount -n --move /proc ${ROOT_MOUNT}/proc
105 mount -n --move /sys ${ROOT_MOUNT}/sys
106 mount -n --move /dev ${ROOT_MOUNT}/dev
107
108 cd $ROOT_MOUNT
109
110 # busybox switch_root supports -c option
111 exec switch_root -c /dev/console $ROOT_MOUNT /sbin/init $CMDLINE ||
112 fatal "Couldn't switch_root, dropping to shell"
113}
114
115fatal() {
116 echo $1 >$CONSOLE
117 echo >$CONSOLE
118 exec sh
119}
120
121early_setup
122
123[ -z "$CONSOLE" ] && CONSOLE="/dev/console"
124
125read_args
126
127echo "Waiting for removable media..."
128C=0
129while true
130do
131 for i in `ls /run/media 2>/dev/null`; do
132 if [ -f /run/media/$i/$ROOT_IMAGE ] ; then
133 found="yes"
134 ROOT_DISK="$i"
135 break
136 elif [ -f /run/media/$i/isolinux/$ROOT_IMAGE ]; then
137 found="yes"
138 ISOLINUX="isolinux"
139 ROOT_DISK="$i"
140 break
141 fi
142 done
143 if [ "$found" = "yes" ]; then
144 break;
145 fi
146 # don't wait for more than $shelltimeout seconds, if it's set
147 if [ -n "$shelltimeout" ]; then
148 echo -n " " $(( $shelltimeout - $C ))
149 if [ $C -ge $shelltimeout ]; then
150 echo "..."
151 echo "Mounted filesystems"
152 mount | grep media
153 echo "Available block devices"
154 cat /proc/partitions
155 fatal "Cannot find $ROOT_IMAGE file in /run/media/* , dropping to a shell "
156 fi
157 C=$(( C + 1 ))
158 fi
159 sleep 1
160done
161
162# Try to mount the root image read-write and then boot it up.
163# This function distinguishes between a read-only image and a read-write image.
164# In the former case (typically an iso), it tries to make a union mount if possible.
165# In the latter case, the root image could be mounted and then directly booted up.
166mount_and_boot() {
167 mkdir $ROOT_MOUNT
168 mknod /dev/loop0 b 7 0 2>/dev/null
169
170 if ! mount -o rw,loop,noatime,nodiratime /run/media/$ROOT_DISK/$ISOLINUX/$ROOT_IMAGE $ROOT_MOUNT ; then
171 fatal "Could not mount rootfs image"
172 fi
173
174 if touch $ROOT_MOUNT/bin 2>/dev/null; then
175 # The root image is read-write, directly boot it up.
176 boot_live_root
177 fi
178
179 # determine which unification filesystem to use
180 union_fs_type=""
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500181 if grep -q -w "overlay" /proc/filesystems; then
182 union_fs_type="overlay"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500183 elif grep -q -w "aufs" /proc/filesystems; then
184 union_fs_type="aufs"
185 else
186 union_fs_type=""
187 fi
188
189 # make a union mount if possible
190 case $union_fs_type in
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500191 "overlay")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500192 mkdir -p /rootfs.ro /rootfs.rw
193 if ! mount -n --move $ROOT_MOUNT /rootfs.ro; then
194 rm -rf /rootfs.ro /rootfs.rw
195 fatal "Could not move rootfs mount point"
196 else
197 mount -t tmpfs -o rw,noatime,mode=755 tmpfs /rootfs.rw
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500198 mkdir -p /rootfs.rw/upperdir /rootfs.rw/work
199 mount -t overlay overlay -o "lowerdir=/rootfs.ro,upperdir=/rootfs.rw/upperdir,workdir=/rootfs.rw/work" $ROOT_MOUNT
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500200 mkdir -p $ROOT_MOUNT/rootfs.ro $ROOT_MOUNT/rootfs.rw
201 mount --move /rootfs.ro $ROOT_MOUNT/rootfs.ro
202 mount --move /rootfs.rw $ROOT_MOUNT/rootfs.rw
203 fi
204 ;;
205 "aufs")
206 mkdir -p /rootfs.ro /rootfs.rw
207 if ! mount -n --move $ROOT_MOUNT /rootfs.ro; then
208 rm -rf /rootfs.ro /rootfs.rw
209 fatal "Could not move rootfs mount point"
210 else
211 mount -t tmpfs -o rw,noatime,mode=755 tmpfs /rootfs.rw
212 mount -t aufs -o "dirs=/rootfs.rw=rw:/rootfs.ro=ro" aufs $ROOT_MOUNT
213 mkdir -p $ROOT_MOUNT/rootfs.ro $ROOT_MOUNT/rootfs.rw
214 mount --move /rootfs.ro $ROOT_MOUNT/rootfs.ro
215 mount --move /rootfs.rw $ROOT_MOUNT/rootfs.rw
216 fi
217 ;;
218 "")
219 mount -t tmpfs -o rw,noatime,mode=755 tmpfs $ROOT_MOUNT/media
220 ;;
221 esac
222
223 # boot the image
224 boot_live_root
225}
226
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500227if [ "$label" != "boot" -a -f $label.sh ] ; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500228 if [ -f /run/media/$i/$ISOLINUX/$ROOT_IMAGE ] ; then
229 ./$label.sh $i/$ISOLINUX $ROOT_IMAGE $video_mode $vga_mode $console_params
230 else
231 fatal "Could not find $label script"
232 fi
233
234 # If we're getting here, we failed...
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500235 fatal "Target $label failed"
236fi
237
238mount_and_boot
239