]> git.ipfire.org Git - thirdparty/dracut.git/blame - modules.d/90dmsquash-live/dmsquash-live-root.sh
Fix boot with `fips` without a value
[thirdparty/dracut.git] / modules.d / 90dmsquash-live / dmsquash-live-root.sh
CommitLineData
2e44f115
JK
1#!/bin/sh
2
c9f1e3d1 3type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh
566dab2a 4
504c0a8f
MH
5command -v unpack_archive >/dev/null || . /lib/img-lib.sh
6
fb59f4c9 7PATH=/usr/sbin:/usr/bin:/sbin:/bin
2e44f115 8
68e7661c 9if getargbool 0 rd.live.debug -n -y rdlivedebug; then
2e44f115
JK
10 exec > /tmp/liveroot.$$.out
11 exec 2>> /tmp/liveroot.$$.out
12 set -x
13fi
14
15[ -z "$1" ] && exit 1
16livedev="$1"
17
cc0e7a36
JK
18# parse various live image specific options that make sense to be
19# specified as their own things
68e7661c 20live_dir=$(getarg rd.live.dir -d live_dir)
cc0e7a36 21[ -z "$live_dir" ] && live_dir="LiveOS"
32214acb 22squash_image=$(getarg rd.live.squashimg)
d18fd023 23[ -z "$squash_image" ] && squash_image="squashfs.img"
32214acb 24
68e7661c
HH
25getargbool 0 rd.live.ram -d -y live_ram && live_ram="yes"
26getargbool 0 rd.live.overlay.reset -d -y reset_overlay && reset_overlay="yes"
27getargbool 0 rd.live.overlay.readonly -d -y readonly_overlay && readonly_overlay="--readonly" || readonly_overlay=""
28overlay=$(getarg rd.live.overlay -d overlay)
504c0a8f 29getargbool 0 rd.writable.fsimg -d -y writable_fsimg && writable_fsimg="yes"
18423f79 30overlay_size=$(getarg rd.live.overlay.size=)
ea288240 31[ -z "$overlay_size" ] && overlay_size=32768
cc0e7a36 32
d6e34d36 33getargbool 0 rd.live.overlay.thin && thin_snapshot="yes"
ea288240 34getargbool 0 rd.live.overlay.overlayfs && overlayfs="yes"
d6e34d36 35
48205bb0
WW
36# CD/DVD media check
37[ -b $livedev ] && fs=$(blkid -s TYPE -o value $livedev)
a607b7d4 38if [ "$fs" = "iso9660" -o "$fs" = "udf" ]; then
c56b63d1
JK
39 check="yes"
40fi
68e7661c 41getarg rd.live.check -d check || check=""
c56b63d1 42if [ -n "$check" ]; then
ca2c6936 43 type plymouth >/dev/null 2>&1 && plymouth --hide-splash
5204d1d3 44 if [ -n "$DRACUT_SYSTEMD" ]; then
3d579309 45 p=$(dev_unit_name "$livedev")
040f3883
HH
46 systemctl start checkisomd5@${p}.service
47 else
48 checkisomd5 --verbose $livedev
49 fi
370035d5 50 if [ $? -eq 1 ]; then
cc02093d
HH
51 die "CD check failed!"
52 exit 1
c56b63d1 53 fi
ca2c6936 54 type plymouth >/dev/null 2>&1 && plymouth --show-splash
c56b63d1 55fi
2e44f115 56
b2de89c5
HH
57ln -s $livedev /run/initramfs/livedev
58
a12db35e
WW
59# determine filesystem type for a filesystem image
60det_img_fs() {
de7ab164 61 udevadm settle >&2
380b8b51 62 blkid -s TYPE -u noraid -o value "$1"
a12db35e
WW
63}
64
85804a91 65modprobe squashfs
8d3d72a6 66CMDLINE=$(getcmdline)
48205bb0 67for arg in $CMDLINE; do case $arg in ro|rw) liverw=$arg ;; esac; done
2e44f115 68# mount the backing of the live image first
d125a470 69mkdir -m 0755 -p /run/initramfs/live
0ddd68f7
WW
70if [ -f $livedev ]; then
71 # no mount needed - we've already got the LiveOS image in initramfs
a12db35e 72 # check filesystem type and handle accordingly
baa5c113
HH
73 fstype=$(det_img_fs $livedev)
74 case $fstype in
75 squashfs) SQUASHED=$livedev;;
a12db35e
WW
76 auto) die "cannot mount live image (unknown filesystem type)" ;;
77 *) FSIMG=$livedev ;;
0ddd68f7 78 esac
baa5c113 79 [ -e /sys/fs/$fstype ] || modprobe $fstype
0ddd68f7 80else
37437cac
RS
81 if [ "$(blkid -o value -s TYPE $livedev)" != "ntfs" ]; then
82 mount -n -t $fstype -o ${liverw:-ro} $livedev /run/initramfs/live
83 else
84 # Symlinking /usr/bin/ntfs-3g as /sbin/mount.ntfs seems to boot
85 # at the first glance, but ends with lots and lots of squashfs
86 # errors, because systemd attempts to kill the ntfs-3g process?!
c42c387c 87 if [ -x "/usr/bin/ntfs-3g" ]; then
37437cac
RS
88 ( exec -a @ntfs-3g ntfs-3g -o ${liverw:-ro} $livedev /run/initramfs/live ) | vwarn
89 else
90 die "Failed to mount block device of live image: Missing NTFS support"
91 exit 1
92 fi
93 fi
94
48205bb0 95 if [ "$?" != "0" ]; then
0ddd68f7
WW
96 die "Failed to mount block device of live image"
97 exit 1
98 fi
2e44f115
JK
99fi
100
2e44f115
JK
101# overlay setup helper function
102do_live_overlay() {
103 # create a sparse file for the overlay
104 # overlay: if non-ram overlay searching is desired, do it,
105 # otherwise, create traditional overlay in ram
2e44f115
JK
106
107 l=$(blkid -s LABEL -o value $livedev) || l=""
108 u=$(blkid -s UUID -o value $livedev) || u=""
109
dfba82b3
JK
110 if [ -z "$overlay" ]; then
111 pathspec="/${live_dir}/overlay-$l-$u"
a7d8fc28 112 elif strstr $overlay ":"; then
2e44f115 113 # pathspec specified, extract
a7d8fc28 114 pathspec=${overlay##*:}
2e44f115
JK
115 fi
116
a607b7d4 117 if [ -z "$pathspec" -o "$pathspec" = "auto" ]; then
2e44f115
JK
118 pathspec="/${live_dir}/overlay-$l-$u"
119 fi
a7d8fc28 120 devspec=${overlay%%:*}
2e44f115
JK
121
122 # need to know where to look for the overlay
3c6337f6 123 if [ -z "$setup" -a -n "$devspec" -a -n "$pathspec" -a -n "$overlay" ]; then
d125a470 124 mkdir -m 0755 /run/initramfs/overlayfs
ea288240
FG
125 opt=''
126 [ -n "$readonly_overlay" ] && opt=-r
3da78360
HH
127 mount -n -t auto $devspec /run/initramfs/overlayfs || :
128 if [ -f /run/initramfs/overlayfs$pathspec -a -w /run/initramfs/overlayfs$pathspec ]; then
3c6337f6
FG
129 OVERLAY_LOOPDEV=$(losetup -f --show $opt /run/initramfs/overlayfs$pathspec)
130 over=$OVERLAY_LOOPDEV
ea288240
FG
131 umount -l /run/initramfs/overlayfs || :
132 oltype=$(det_img_fs $OVERLAY_LOOPDEV)
133 if [ -z "$oltype" ] || [ "$oltype" = DM_snapshot_cow ]; then
134 if [ -n "$reset_overlay" ]; then
135 info "Resetting the Device-mapper overlay."
136 dd if=/dev/zero of=$OVERLAY_LOOPDEV bs=64k count=1 conv=fsync 2>/dev/null
137 fi
138 if [ -n "$overlayfs" ]; then
139 unset -v overlayfs
eb18a1fe 140 [ -n "$DRACUT_SYSTEMD" ] && reloadsysrootmountunit=":>/xor_overlayfs;"
ea288240
FG
141 fi
142 setup="yes"
143 else
144 mount -n -t $oltype $opt $OVERLAY_LOOPDEV /run/initramfs/overlayfs
145 if [ -d /run/initramfs/overlayfs/overlayfs ] &&
146 [ -d /run/initramfs/overlayfs/ovlwork ]; then
147 ln -s /run/initramfs/overlayfs/overlayfs /run/overlayfs$opt
148 ln -s /run/initramfs/overlayfs/ovlwork /run/ovlwork$opt
44d16881
FG
149 if [ -z "$overlayfs" ] && [ -n "$DRACUT_SYSTEMD" ]; then
150 reloadsysrootmountunit=":>/xor_overlayfs;"
ea288240 151 fi
44d16881 152 overlayfs="required"
ea288240
FG
153 setup="yes"
154 fi
155 fi
156 elif [ -d /run/initramfs/overlayfs$pathspec ] &&
157 [ -d /run/initramfs/overlayfs$pathspec/../ovlwork ]; then
158 ln -s /run/initramfs/overlayfs$pathspec /run/overlayfs$opt
159 ln -s /run/initramfs/overlayfs$pathspec/../ovlwork /run/ovlwork$opt
44d16881
FG
160 if [ -z "$overlayfs" ] && [ -n "$DRACUT_SYSTEMD" ]; then
161 reloadsysrootmountunit=":>/xor_overlayfs;"
2e44f115 162 fi
44d16881 163 overlayfs="required"
2e44f115
JK
164 setup="yes"
165 fi
ea288240
FG
166 fi
167 if [ -n "$overlayfs" ]; then
168 modprobe overlay
169 if [ $? != 0 ]; then
44d16881
FG
170 if [ "$overlayfs" = required ]; then
171 die "OverlayFS is required but not available."
172 exit 1
173 fi
174 [ -n "$DRACUT_SYSTEMD" ] && reloadsysrootmountunit=":>/xor_overlayfs;"
f1b65e92 175 m='OverlayFS is not available; using temporary Device-mapper overlay.'
44d16881
FG
176 info $m
177 unset -v overlayfs setup
ea288240 178 fi
2e44f115
JK
179 fi
180
579f3853
FG
181 if [ -z "$setup" -o -n "$readonly_overlay" ]; then
182 if [ -n "$setup" ]; then
183 warn "Using temporary overlay."
184 elif [ -n "$devspec" -a -n "$pathspec" ]; then
f1b65e92 185 [ -z "$m" ] &&
b1e1b6cb
FG
186 m=' Unable to find a persistent overlay; using a temporary one.'
187 m="$m"$'\n All root filesystem changes will be lost on shutdown.'
188 m="$m"$'\n Press [Enter] to continue.'
eb18a1fe 189 printf "\n\n\n\n${m}\n\n\n" > /dev/kmsg
f1b65e92 190 if [ -n "$DRACUT_SYSTEMD" ]; then
b1e1b6cb 191 if type plymouth >/dev/null 2>&1 && plymouth --ping ; then
f1b65e92 192 if getargbool 0 rhgb || getargbool 0 splash ; then
b1e1b6cb
FG
193 m='>>>'$'\n''>>>'$'\n''>>>'$'\n\n\n'"$m"
194 m="${m%n.*}"$'n.\n\n\n''<<<'$'\n''<<<'$'\n''<<<'
195 plymouth display-message --text="${m}"
f1b65e92 196 else
b1e1b6cb 197 plymouth ask-question --prompt="${m}" --command=true
f1b65e92
FG
198 fi
199 else
b1e1b6cb
FG
200 m=">>>${m//.[[:space:]]/.} <<<"
201 systemd-ask-password --timeout=0 "${m}"
f1b65e92
FG
202 fi
203 else
b1e1b6cb
FG
204 type plymouth >/dev/null 2>&1 && plymouth --ping && plymouth --quit
205 read -s -r -p $'\n\n'"${m}" -n 1 reply
f1b65e92 206 fi
2e44f115 207 fi
ea288240
FG
208 if [ -n "$overlayfs" ]; then
209 mkdir -m 0755 /run/overlayfs
210 mkdir -m 0755 /run/ovlwork
eb18a1fe
FG
211 if [ -n "$readonly_overlay" ] && ! [ -h /run/overlayfs-r ]; then
212 info "No persistent overlay found."
213 unset -v readonly_overlay
214 [ -n "$DRACUT_SYSTEMD" ] && reloadsysrootmountunit="${reloadsysrootmountunit}:>/xor_readonly;"
215 fi
579f3853 216 else
ea288240
FG
217 dd if=/dev/null of=/overlay bs=1024 count=1 seek=$((overlay_size*1024)) 2> /dev/null
218 if [ -n "$setup" -a -n "$readonly_overlay" ]; then
3c6337f6
FG
219 RO_OVERLAY_LOOPDEV=$(losetup -f --show /overlay)
220 over=$RO_OVERLAY_LOOPDEV
ea288240 221 else
3c6337f6
FG
222 OVERLAY_LOOPDEV=$(losetup -f --show /overlay)
223 over=$OVERLAY_LOOPDEV
ea288240 224 fi
579f3853 225 fi
2e44f115
JK
226 fi
227
228 # set up the snapshot
ea288240 229 if [ -z "$overlayfs" ]; then
3c6337f6 230 if [ -n "$readonly_overlay" ] && [ -n "$OVERLAY_LOOPDEV" ]; then
ea288240
FG
231 echo 0 $sz snapshot $BASE_LOOPDEV $OVERLAY_LOOPDEV P 8 | dmsetup create --readonly live-ro
232 base="/dev/mapper/live-ro"
ea288240
FG
233 else
234 base=$BASE_LOOPDEV
ea288240 235 fi
579f3853 236 fi
b0472eac 237
d6e34d36
FD
238 if [ -n "$thin_snapshot" ]; then
239 modprobe dm_thin_pool
ea288240 240 mkdir -m 0755 /run/initramfs/thin-overlay
d6e34d36
FD
241
242 # In block units (512b)
243 thin_data_sz=$(( $overlay_size * 1024 * 1024 / 512 ))
244 thin_meta_sz=$(( $thin_data_sz / 10 ))
245
246 # It is important to have the backing file on a tmpfs
247 # this is needed to let the loopdevice support TRIM
248 dd if=/dev/null of=/run/initramfs/thin-overlay/meta bs=1b count=1 seek=$((thin_meta_sz)) 2> /dev/null
249 dd if=/dev/null of=/run/initramfs/thin-overlay/data bs=1b count=1 seek=$((thin_data_sz)) 2> /dev/null
250
251 THIN_META_LOOPDEV=$( losetup --show -f /run/initramfs/thin-overlay/meta )
252 THIN_DATA_LOOPDEV=$( losetup --show -f /run/initramfs/thin-overlay/data )
253
254 echo 0 $thin_data_sz thin-pool $THIN_META_LOOPDEV $THIN_DATA_LOOPDEV 1024 1024 | dmsetup create live-overlay-pool
255 dmsetup message /dev/mapper/live-overlay-pool 0 "create_thin 0"
256
257 # Create a snapshot of the base image
258 echo 0 $sz thin /dev/mapper/live-overlay-pool 0 $base | dmsetup create live-rw
ea288240 259 elif [ -z "$overlayfs" ]; then
97be9335 260 echo 0 $sz snapshot $base $over PO 8 | dmsetup create live-rw
d6e34d36 261 fi
3622d6d7 262
789668de
FG
263 # Create a device for the ro base of overlayed file systems.
264 if [ -z "$overlayfs" ]; then
ea765c0e
FG
265 echo 0 $sz linear $BASE_LOOPDEV 0 | dmsetup create --readonly live-base
266 fi
789668de 267 ln -s $BASE_LOOPDEV /dev/live-base
2e44f115 268}
eb18a1fe 269# end do_live_overlay()
2e44f115 270
2e44f115 271# we might have a genMinInstDelta delta file for anaconda to take advantage of
19f3a804
HH
272if [ -e /run/initramfs/live/${live_dir}/osmin.img ]; then
273 OSMINSQFS=/run/initramfs/live/${live_dir}/osmin.img
2e44f115 274 # decompress the delta data
1f8abe81 275 dd if=$OSMINSQFS of=/run/initramfs/osmin.img 2> /dev/null
2e44f115 276 OSMIN_SQUASHED_LOOPDEV=$( losetup -f )
1f8abe81 277 losetup -r $OSMIN_SQUASHED_LOOPDEV /run/initramfs/osmin.img
d125a470
HH
278 mkdir -m 0755 -p /run/initramfs/squashfs.osmin
279 mount -n -t squashfs -o ro $OSMIN_SQUASHED_LOOPDEV /run/initramfs/squashfs.osmin
2e44f115 280 OSMIN_LOOPDEV=$( losetup -f )
d125a470
HH
281 losetup -r $OSMIN_LOOPDEV /run/initramfs/squashfs.osmin/osmin
282 umount -l /run/initramfs/squashfs.osmin
2e44f115
JK
283fi
284
dfec8467 285# we might have an embedded fs image on squashfs (compressed live)
32214acb
HH
286if [ -e /run/initramfs/live/${live_dir}/${squash_image} ]; then
287 SQUASHED="/run/initramfs/live/${live_dir}/${squash_image}"
ce9a3987 288fi
ea288240 289if [ -e "$SQUASHED" ]; then
8b5ee88f 290 if [ -n "$live_ram" ]; then
ea288240
FG
291 echo 'Copying live image to RAM...' > /dev/kmsg
292 echo ' (this may take a minute)' > /dev/kmsg
1f8abe81 293 dd if=$SQUASHED of=/run/initramfs/squashed.img bs=512 2> /dev/null
ea288240 294 echo 'Done copying live image to RAM.' > /dev/kmsg
1f8abe81 295 SQUASHED="/run/initramfs/squashed.img"
2e44f115
JK
296 fi
297
298 SQUASHED_LOOPDEV=$( losetup -f )
299 losetup -r $SQUASHED_LOOPDEV $SQUASHED
d125a470
HH
300 mkdir -m 0755 -p /run/initramfs/squashfs
301 mount -n -t squashfs -o ro $SQUASHED_LOOPDEV /run/initramfs/squashfs
2e44f115 302
789668de
FG
303 if [ -d /run/initramfs/squashfs/LiveOS ]; then
304 if [ -f /run/initramfs/squashfs/LiveOS/rootfs.img ]; then
305 FSIMG="/run/initramfs/squashfs/LiveOS/rootfs.img"
306 elif [ -f /run/initramfs/squashfs/LiveOS/ext3fs.img ]; then
307 FSIMG="/run/initramfs/squashfs/LiveOS/ext3fs.img"
308 fi
309 elif [ -d /run/initramfs/squashfs/proc ]; then
310 FSIMG=$SQUASHED
44d16881
FG
311 if [ -z "$overlayfs" ] && [ -n "$DRACUT_SYSTEMD" ]; then
312 reloadsysrootmountunit=":>/xor_overlayfs;"
789668de 313 fi
44d16881 314 overlayfs="required"
789668de
FG
315 else
316 die "Failed to find a root filesystem in $SQUASHED."
317 exit 1
8b5ee88f
FG
318 fi
319else
320 # we might have an embedded fs image to use as rootfs (uncompressed live)
ea288240 321 if [ -e /run/initramfs/live/${live_dir}/rootfs.img ]; then
8b5ee88f 322 FSIMG="/run/initramfs/live/${live_dir}/rootfs.img"
ea288240
FG
323 elif [ -e /run/initramfs/live/${live_dir}/ext3fs.img ]; then
324 FSIMG="/run/initramfs/live/${live_dir}/ext3fs.img"
8b5ee88f
FG
325 fi
326 if [ -n "$live_ram" ]; then
ea288240
FG
327 echo 'Copying live image to RAM...' > /dev/kmsg
328 echo ' (this may take a minute or so)' > /dev/kmsg
8b5ee88f 329 dd if=$FSIMG of=/run/initramfs/rootfs.img bs=512 2> /dev/null
ea288240 330 echo 'Done copying live image to RAM.' > /dev/kmsg
8b5ee88f
FG
331 FSIMG='/run/initramfs/rootfs.img'
332 fi
b0472eac 333fi
2e44f115 334
789668de
FG
335if [ -n "$FSIMG" ]; then
336 if [ -n "$writable_fsimg" ]; then
8b5ee88f 337 # mount the provided filesystem read/write
ea288240
FG
338 echo "Unpacking live filesystem (may take some time)" > /dev/kmsg
339 mkdir -m 0755 /run/initramfs/fsimg/
b0472eac
FD
340 if [ -n "$SQUASHED" ]; then
341 cp -v $FSIMG /run/initramfs/fsimg/rootfs.img
342 else
343 unpack_archive $FSIMG /run/initramfs/fsimg/
344 fi
6ed1d9fd 345 FSIMG=/run/initramfs/fsimg/rootfs.img
8b5ee88f 346 fi
3c6337f6
FG
347 opt=-r
348 # For writable DM images...
349 if [ -z "$SQUASHED" -a -n "$live_ram" -a -z "$overlayfs" ] ||
350 [ -n "$writable_fsimg" ] ||
8b5ee88f 351 [ "$overlay" = none -o "$overlay" = None -o "$overlay" = NONE ]; then
3c6337f6
FG
352 if [ -z "$readonly_overlay" ]; then
353 opt=''
354 setup=rw
355 else
356 setup=yes
357 fi
358 fi
789668de
FG
359 if [ "$FSIMG" = "$SQUASHED" ]; then
360 BASE_LOOPDEV=$SQUASHED_LOOPDEV
361 else
362 BASE_LOOPDEV=$(losetup -f --show $opt $FSIMG)
363 sz=$(blockdev --getsz $BASE_LOOPDEV)
364 fi
365 if [ "$setup" = rw ]; then
8b5ee88f 366 echo 0 $sz linear $BASE_LOOPDEV 0 | dmsetup create live-rw
b0472eac 367 else
3c6337f6 368 # Add a DM snapshot or OverlayFS for writes.
ea288240 369 do_live_overlay
b0472eac 370 fi
2e44f115
JK
371fi
372
373if [ -b "$OSMIN_LOOPDEV" ]; then
374 # set up the devicemapper snapshot device, which will merge
375 # the normal live fs image, and the delta, into a minimzied fs image
ea288240 376 echo "0 $sz snapshot $BASE_LOOPDEV $OSMIN_LOOPDEV P 8" | dmsetup create --readonly live-osimg-min
2e44f115
JK
377fi
378
ea288240 379if [ -n "$reloadsysrootmountunit" ]; then
eb18a1fe 380 eval "$reloadsysrootmountunit"
ea288240 381 systemctl daemon-reload
fbf1b5b1
JB
382fi
383
ea288240 384ROOTFLAGS="$(getarg rootflags)"
8ff624df 385
ea288240
FG
386if [ -n "$overlayfs" ]; then
387 mkdir -m 0755 /run/rootfsbase
eb18a1fe 388 if [ -n "$reset_overlay" ] && [ -h /run/overlayfs ]; then
ea288240
FG
389 ovlfs=$(readlink /run/overlayfs)
390 info "Resetting the OverlayFS overlay directory."
391 rm -r -- ${ovlfs}/* ${ovlfs}/.* >/dev/null 2>&1
392 fi
eb18a1fe
FG
393 if [ -n "$readonly_overlay" ] && [ -h /run/overlayfs-r ]; then
394 ovlfs=lowerdir=/run/overlayfs-r:/run/rootfsbase
ea288240 395 else
eb18a1fe 396 ovlfs=lowerdir=/run/rootfsbase
ea288240 397 fi
eb18a1fe 398 mount -r $FSIMG /run/rootfsbase
ea288240 399 if [ -z "$DRACUT_SYSTEMD" ]; then
ea288240 400 printf 'mount -t overlay LiveOS_rootfs -o%s,%s %s\n' "$ROOTFLAGS" \
eb18a1fe 401 "$ovlfs",upperdir=/run/overlayfs,workdir=/run/ovlwork \
ea288240
FG
402 "$NEWROOT" > $hookdir/mount/01-$$-live.sh
403 fi
404else
ea288240
FG
405 if [ -z "$DRACUT_SYSTEMD" ]; then
406 [ -n "$ROOTFLAGS" ] && ROOTFLAGS="-o $ROOTFLAGS"
407 printf 'mount %s /dev/mapper/live-rw %s\n' "$ROOTFLAGS" "$NEWROOT" > $hookdir/mount/01-$$-live.sh
408 fi
8ff624df 409fi
789668de
FG
410[ -e "$SQUASHED" ] && umount -l /run/initramfs/squashfs
411
412ln -s null /dev/root
2e44f115 413
fb67e4aa
HH
414need_shutdown
415
2e44f115 416exit 0
789668de 417