]> git.ipfire.org Git - thirdparty/dracut.git/blob - modules.d/90dmsquash-live/dmsquash-live-root.sh
Renamed all shell scripts to *.sh
[thirdparty/dracut.git] / modules.d / 90dmsquash-live / dmsquash-live-root.sh
1 #!/bin/sh
2 # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3 # ex: ts=8 sw=4 sts=4 et filetype=sh
4
5 type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh
6 type det_fs >/dev/null 2>&1 || . /lib/fs-lib.sh
7
8 [ -f /tmp/root.info ] && . /tmp/root.info
9
10 PATH=/usr/sbin:/usr/bin:/sbin:/bin
11
12 if getargbool 0 rd.live.debug -y rdlivedebug; then
13 exec > /tmp/liveroot.$$.out
14 exec 2>> /tmp/liveroot.$$.out
15 set -x
16 fi
17
18 [ -z "$1" ] && exit 1
19 livedev="$1"
20
21 # parse various live image specific options that make sense to be
22 # specified as their own things
23 live_dir=$(getarg rd.live.dir live_dir)
24 [ -z "$live_dir" ] && live_dir="LiveOS"
25 getargbool 0 rd.live.ram -y live_ram && live_ram="yes"
26 getargbool 0 rd.live.overlay.reset -y reset_overlay && reset_overlay="yes"
27 getargbool 0 rd.live.overlay.readonly -y readonly_overlay && readonly_overlay="--readonly" || readonly_overlay=""
28 overlay=$(getarg rd.live.overlay overlay)
29
30 # CD/DVD media check
31 [ -b $livedev ] && fs=$(blkid -s TYPE -o value $livedev)
32 if [ "$fs" = "iso9660" -o "$fs" = "udf" ]; then
33 check="yes"
34 fi
35 getarg rd.live.check check || check=""
36 if [ -n "$check" ]; then
37 [ -x /bin/plymouth ] && /bin/plymouth --hide-splash
38 checkisomd5 --verbose $livedev || :
39 if [ $? -ne 0 ]; then
40 die "CD check failed!"
41 exit 1
42 fi
43 [ -x /bin/plymouth ] && /bin/plymouth --show-splash
44 fi
45
46 # determine filesystem type for a filesystem image
47 det_img_fs() {
48 blkid -s TYPE -u noraid -o value "$1"
49 }
50
51 for arg in $CMDLINE; do case $arg in ro|rw) liverw=$arg ;; esac; done
52 # mount the backing of the live image first
53 mkdir -m 0755 -p /run/initramfs/live
54 if [ -f $livedev ]; then
55 # no mount needed - we've already got the LiveOS image in initramfs
56 # check filesystem type and handle accordingly
57 fstype=$(det_img_fs $livedev)
58 case $fstype in
59 squashfs) SQUASHED=$livedev;;
60 auto) die "cannot mount live image (unknown filesystem type)" ;;
61 *) FSIMG=$livedev ;;
62 esac
63 [ -e /sys/fs/$fstype ] || modprobe $fstype
64 else
65 mount -n -t $fstype -o ${liverw:-ro} $livedev /run/initramfs/live
66 if [ "$?" != "0" ]; then
67 die "Failed to mount block device of live image"
68 exit 1
69 fi
70 fi
71
72 # overlay setup helper function
73 do_live_overlay() {
74 # create a sparse file for the overlay
75 # overlay: if non-ram overlay searching is desired, do it,
76 # otherwise, create traditional overlay in ram
77 OVERLAY_LOOPDEV=$( losetup -f )
78
79 l=$(blkid -s LABEL -o value $livedev) || l=""
80 u=$(blkid -s UUID -o value $livedev) || u=""
81
82 if [ -z "$overlay" ]; then
83 pathspec="/${live_dir}/overlay-$l-$u"
84 elif ( echo $overlay | grep -q ":" ); then
85 # pathspec specified, extract
86 pathspec=$( echo $overlay | sed -e 's/^.*://' )
87 fi
88
89 if [ -z "$pathspec" -o "$pathspec" = "auto" ]; then
90 pathspec="/${live_dir}/overlay-$l-$u"
91 fi
92 devspec=$( echo $overlay | sed -e 's/:.*$//' )
93
94 # need to know where to look for the overlay
95 setup=""
96 if [ -n "$devspec" -a -n "$pathspec" -a -n "$overlay" ]; then
97 mkdir -m 0755 /run/initramfs/overlayfs
98 mount -n -t auto $devspec /run/initramfs/overlayfs || :
99 if [ -f /run/initramfs/overlayfs$pathspec -a -w /run/initramfs/overlayfs$pathspec ]; then
100 losetup $OVERLAY_LOOPDEV /run/initramfs/overlayfs$pathspec
101 if [ -n "$reset_overlay" ]; then
102 dd if=/dev/zero of=$OVERLAY_LOOPDEV bs=64k count=1 2>/dev/null
103 fi
104 setup="yes"
105 fi
106 umount -l /run/initramfs/overlayfs || :
107 fi
108
109 if [ -z "$setup" ]; then
110 if [ -n "$devspec" -a -n "$pathspec" ]; then
111 warn "Unable to find persistent overlay; using temporary"
112 sleep 5
113 fi
114
115 dd if=/dev/null of=/overlay bs=1024 count=1 seek=$((512*1024)) 2> /dev/null
116 losetup $OVERLAY_LOOPDEV /overlay
117 fi
118
119 # set up the snapshot
120 echo 0 `blockdev --getsz $BASE_LOOPDEV` snapshot $BASE_LOOPDEV $OVERLAY_LOOPDEV p 8 | dmsetup create $readonly_overlay live-rw
121 }
122
123 # live cd helper function
124 do_live_from_base_loop() {
125 do_live_overlay
126 }
127
128 # we might have a genMinInstDelta delta file for anaconda to take advantage of
129 if [ -e /run/initramfs/live/${live_dir}/osmin.img ]; then
130 OSMINSQFS=/run/initramfs/live/${live_dir}/osmin.img
131 fi
132
133 if [ -n "$OSMINSQFS" ]; then
134 # decompress the delta data
135 dd if=$OSMINSQFS of=/osmin.img 2> /dev/null
136 OSMIN_SQUASHED_LOOPDEV=$( losetup -f )
137 losetup -r $OSMIN_SQUASHED_LOOPDEV /osmin.img
138 mkdir -m 0755 -p /run/initramfs/squashfs.osmin
139 mount -n -t squashfs -o ro $OSMIN_SQUASHED_LOOPDEV /run/initramfs/squashfs.osmin
140 OSMIN_LOOPDEV=$( losetup -f )
141 losetup -r $OSMIN_LOOPDEV /run/initramfs/squashfs.osmin/osmin
142 umount -l /run/initramfs/squashfs.osmin
143 fi
144
145 # we might have an embedded fs image to use as rootfs (uncompressed live)
146 if [ -e /run/initramfs/live/${live_dir}/ext3fs.img ]; then
147 FSIMG="/run/initramfs/live/${live_dir}/ext3fs.img"
148 elif [ -e /run/initramfs/live/${live_dir}/rootfs.img ]; then
149 FSIMG="/run/initramfs/live/${live_dir}/rootfs.img"
150 fi
151
152 if [ -n "$FSIMG" ] ; then
153 BASE_LOOPDEV=$( losetup -f )
154 losetup -r $BASE_LOOPDEV $FSIMG
155
156 do_live_from_base_loop
157 fi
158
159 # we might have an embedded fs image on squashfs (compressed live)
160 if [ -e /run/initramfs/live/${live_dir}/squashfs.img ]; then
161 SQUASHED="/run/initramfs/live/${live_dir}/squashfs.img"
162 fi
163
164 if [ -e "$SQUASHED" ] ; then
165 if [ -n "$live_ram" ] ; then
166 echo "Copying live image to RAM..."
167 echo "(this may take a few minutes)"
168 dd if=$SQUASHED of=/squashed.img bs=512 2> /dev/null
169 umount -n /run/initramfs/live
170 echo "Done copying live image to RAM."
171 eject -p $livedev || :
172 SQUASHED="/squashed.img"
173 fi
174
175 SQUASHED_LOOPDEV=$( losetup -f )
176 losetup -r $SQUASHED_LOOPDEV $SQUASHED
177 mkdir -m 0755 -p /run/initramfs/squashfs
178 mount -n -t squashfs -o ro $SQUASHED_LOOPDEV /run/initramfs/squashfs
179
180 BASE_LOOPDEV=$( losetup -f )
181 if [ -f /run/initramfs/squashfs/LiveOS/ext3fs.img ]; then
182 losetup -r $BASE_LOOPDEV /run/initramfs/squashfs/LiveOS/ext3fs.img
183 elif [ -f /run/initramfs/squashfs/LiveOS/rootfs.img ]; then
184 losetup -r $BASE_LOOPDEV /run/initramfs/squashfs/LiveOS/rootfs.img
185 fi
186
187 umount -l /run/initramfs/squashfs
188
189 do_live_from_base_loop
190 fi
191
192 if [ -b "$OSMIN_LOOPDEV" ]; then
193 # set up the devicemapper snapshot device, which will merge
194 # the normal live fs image, and the delta, into a minimzied fs image
195 echo "0 $( blockdev --getsz $BASE_LOOPDEV ) snapshot $BASE_LOOPDEV $OSMIN_LOOPDEV p 8" | dmsetup create --readonly live-osimg-min
196 fi
197
198 ROOTFLAGS="$(getarg rootflags)"
199 if [ -n "$ROOTFLAGS" ]; then
200 ROOTFLAGS="-o $ROOTFLAGS"
201 fi
202
203 if [ -b "$BASE_LOOPDEV" ]; then
204 ln -s $BASE_LOOPDEV /run/initramfs/live-baseloop
205 fi
206 ln -s /dev/mapper/live-rw /dev/root
207 printf '/bin/mount %s /dev/mapper/live-rw %s\n' "$ROOTFLAGS" "$NEWROOT" > $hookdir/mount/01-$$-live.sh
208
209 need_shutdown
210
211 exit 0