Debian Codegraph @

dracut/modules.d/55kiwi-live/kiwi-live-lib.sh

raw
1 #!/bin/bash
2 type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh
3
4 function getOverlayBaseDirectory {
5 # initialize and print overlay base directory below which
6 # the iso and overlayfs based mountpoints are managed
7 local overlay_base=/run/overlay
8 local overlay_size
9 mkdir -p "${overlay_base}"
10 if ! mountpoint -q "${overlay_base}";then
11 overlay_size=$(getarg rd.live.overlay.size)
12 [ -z "${overlay_size}" ] && overlay_size="50%"
13 mount -t tmpfs -o "size=${overlay_size}" tmpfs "${overlay_base}"
14 fi
15 echo "${overlay_base}"
16 }
17
18 function ProvideIso {
19 local root=$1
20 local iso_url=${root#live:net:}
21 local system
22 local rd_size
23 local custom_rd_size
24 local modfile=/etc/modprobe.d/99-brd.conf
25 system=$(getarg rd.kiwi.live.system=)
26 if [ ! -b "${system}" ];then
27 system=/dev/ram0
28 fi
29 ptable=$(blkid -s PTTYPE -o value "${system}" 2>/dev/null)
30 case "${iso_url}" in
31 ftp:*|http:*|https:*|dolly:*) \
32 if [ -z "${ptable}" ] || getargbool 0 rd.kiwi.live.reset; then
33 custom_rd_size=$(getarg ramdisk_size=)
34 if [ -n "${custom_rd_size}" ];then
35 rd_size="${custom_rd_size}"
36 else
37 # set minimum default ramdisk size: 2G
38 rd_size="2097152"
39 fi
40 mkdir -p /etc/modprobe.d
41 if [ -n "${rd_size}" ];then
42 echo "options brd rd_size=${rd_size}" > ${modfile}
43 fi
44 modprobe --remove brd
45 modprobe brd
46 udev_pending
47 fetch_file "${iso_url}" | dd bs=32k of="${system}" &>/dev/null
48 fi
49 if [[ ${system} =~ ^/dev/ram ]];then
50 echo "${system}"
51 else
52 udev_pending
53 # This is not 100% safe as in theory the volume ID
54 # of the live ISO could be configured in the kiwi image
55 # description and doesn't have to be set to: CDROM.
56 # The value could be obtained as follows:
57 # label=$(
58 # isoinfo -j utf-8 -d -i ${system} |\
59 # grep "Volume id:" | cut -f2 -d:
60 # )
61 # but this would require an additional tool as part of the
62 # initrd. Thus this is subject to a followup pull request
63 echo "/dev/disk/by-label/CDROM"
64 fi
65 ;;
66 *) \
67 echo "${root}"
68 ;;
69 esac
70 }
71
72 function lookupIsoDiskDevice {
73 local root=$1
74 local iso_label=${root#/dev/disk/by-label/}
75 local disk
76 local mount_point
77 local iso_volid
78 for disk in $(lsblk -p -n -r -o NAME,TYPE | grep disk | cut -f1 -d' ');do
79 if [[ ${disk} =~ ^/dev/fd ]];then
80 # ignore floppy disk devices
81 continue
82 fi
83 # 1. Mapping:
84 # If the ISO is bootet as disk in hybrid mode the exposed LABEL
85 # of the disk device matches the ISO label
86 iso_volid=$(blkid -s LABEL -o value "${disk}")
87 if [ "${iso_volid}" = "${iso_label}" ];then
88 echo "${disk}%disk_boot"
89 return
90 fi
91 # 2. Mapping:
92 # If the ISO is booted via grub loopback the partition of the
93 # disk device which contains the ISO file and was loopbacked
94 # by grub is mounted to /run/initramfs/isoscan
95 for mount_point in $(lsblk -p -n -r -o MOUNTPOINT "${disk}");do
96 if [ "${mount_point}" = "/run/initramfs/isoscan" ];then
97 echo "${disk}%loop_boot"
98 return
99 fi
100 done
101 done
102 }
103
104 function setupDebugMode {
105 if getargbool 0 rd.kiwi.debug; then
106 local log=/run/initramfs/log
107 mkdir -p ${log}
108 exec > ${log}/boot.kiwi
109 exec 2>> ${log}/boot.kiwi
110 set -x
111 fi
112 }
113
114 function initialize {
115 local profile=/.profile
116 test -f ${profile} && cat ${profile}
117 }
118
119 function loadKernelModules {
120 modprobe squashfs
121 }
122
123 function udev_pending {
124 declare DEVICE_TIMEOUT=${DEVICE_TIMEOUT}
125 local limit=120
126 if [[ "${DEVICE_TIMEOUT}" =~ ^[0-9]+$ ]]; then
127 limit="${DEVICE_TIMEOUT}"
128 fi
129 udevadm settle --timeout="${limit}"
130 }
131
132 function set_device_lock {
133 # """
134 # Set device lock, preferrable via udevadm or via
135 # flock as fallback if systemd/udev does not provide
136 # the command
137 # """
138 if udevadm lock --help &>/dev/null;then
139 udevadm lock --device "$@"
140 else
141 flock -x "$@"
142 fi
143 }
144
145 function initGlobalDevices {
146 if [ -z "$1" ]; then
147 die "No root device for operation given"
148 fi
149 isodev="$1"
150 local isodisk
151 isodisk=$(lookupIsoDiskDevice "${isodev}")
152 isodiskdev=$(echo "${isodisk}" | cut -f1 -d%)
153 isodiskmode=$(echo "${isodisk}" | cut -f2 -d%)
154 isofs_type=$(blkid -s TYPE -o value "${isodev}")
155 media_check_device=${isodev}
156 if [ -n "${isodiskdev}" ]; then
157 media_check_device=${isodiskdev}
158 fi
159 export media_check_device
160 export isodev
161 export isodiskdev
162 export isodiskmode
163 export isofs_type
164 }
165
166 function initGlobalOptions {
167 live_dir=$(getarg rd.live.dir -d live_dir)
168 [ -z "${live_dir}" ] && live_dir="LiveOS"
169
170 squash_image=$(getarg rd.live.squashimg)
171 [ -z "${squash_image}" ] && squash_image="squashfs.img"
172
173 cow_filesystem=$(getarg rd.live.overlay.cowfs)
174 [ -z "${cow_filesystem}" ] && cow_filesystem="ext4"
175
176 cow_file_mbsize=$(getarg rd.live.cowfile.mbsize)
177 [ -z "${cow_file_mbsize}" ] && cow_file_mbsize="500"
178
179 cow_file_path=$(getarg rd.live.cowfile.path)
180 [ -z "${cow_file_path}" ] && cow_file_path="live_system.cow"
181 }
182
183 function mountIso {
184 local overlay_base
185 overlay_base=$(getOverlayBaseDirectory)
186 ln -s "${isodev}" "${overlay_base}/isodev"
187 local iso_mount_point="${overlay_base}/live"
188 mkdir -m 0755 -p "${iso_mount_point}"
189 if ! mount -o ro -n -t "${isofs_type}" "${isodev}" "${iso_mount_point}"; then
190 die "Failed to mount live ISO device"
191 fi
192 echo "${iso_mount_point}"
193 }
194
195 function mountCompressedContainerFromIso {
196 local iso_mount_point=$1
197 local overlay_base
198 local squashfs_container
199 overlay_base=$(getOverlayBaseDirectory)
200 local container_mount_point="${overlay_base}/squashfs_container"
201 squashfs_container="${iso_mount_point}/${live_dir}/${squash_image}"
202 mkdir -m 0755 -p "${container_mount_point}"
203
204 if activate_verity "${squashfs_container}";then
205 squashfs_container=/dev/mapper/verityroot
206 fi
207
208 if activate_luks "${squashfs_container}" luks; then
209 squashfs_container=/dev/mapper/luks
210 fi
211
212 if ! mount -n "${squashfs_container}" "${container_mount_point}";then
213 die "Failed to mount live ISO compressed read-only container"
214 fi
215 echo "${container_mount_point}"
216 }
217
218 function activate_verity {
219 # The method loopback mounts the given compressed root image
220 # and calls veritysetup to activate the verity device. The
221 # activatrion of the verity device can also be done with help
222 # from systemd-veritysetup which would automatically be called
223 # after losetup if the dracut module systemd-veritysetup
224 # would be included to the initrd. Unfortunately no proper
225 # veritytab file can be provided if the root image is based on
226 # squashfs because there is no support for UUIDs in squashfs
227 # which means the veritytab would have to be modified with the
228 # plain loopback device after the losetup call. Such a modification
229 # then requires to call "systemctl daemon-reload" followed by
230 # "systemctl restart systemd-veritysetup@verityroot" to make
231 # systemd-veritysetup aware of the changes in the veritytab.
232 # To avoid this complexity the verity device is activated via
233 # veritysetup directly.
234 local compressed_root=$1
235 local verity_loop
236 local verity_name
237 local data_device
238 local hash_device
239 local root_hash
240 local verity_options
241 local kiwi_verity_options
242 local veritysetup
243 local option
244 if [ -f /etc/veritytab ];then
245 read -r \
246 verity_name data_device hash_device root_hash verity_options \
247 < /etc/veritytab
248 verity_loop=$(losetup --show --find "${compressed_root}")
249 udevadm wait "${verity_loop}"
250 if [ "${data_device}" = "PLACEHOLDER" ];then
251 # squashfs does not support UUIDs, therefore the initial
252 # veritytab only contains a placeholder for the device name
253 sed -ie "s|PLACEHOLDER|${verity_loop}|g" /etc/veritytab
254 fi
255 read -r \
256 verity_name data_device hash_device root_hash verity_options \
257 < /etc/veritytab
258 if [ "$(echo "${data_device}" | cut -f1 -d=)" = "UUID" ];then
259 data_device=/dev/disk/by-uuid/$(echo "${data_device}" | cut -f2 -d=)
260 udevadm wait "${data_device}"
261 fi
262 if [ "$(echo "${hash_device}" | cut -f1 -d=)" = "UUID" ];then
263 hash_device=/dev/disk/by-uuid/$(echo "${hash_device}" | cut -f2 -d=)
264 udevadm wait "${hash_device}"
265 fi
266 # Read kernel command line verity options and merge the options
267 kiwi_verity_options=$(getarg rd.kiwi.verity_options=)
268 if [ -n "${kiwi_verity_options}" ]; then
269 if [ -n "${verity_options}" ]; then
270 verity_options="${verity_options},${kiwi_verity_options}"
271 else
272 verity_options="${kiwi_verity_options}"
273 fi
274 fi
275 veritysetup="veritysetup open "
276 veritysetup="${veritysetup} ${data_device} ${verity_name} "
277 veritysetup="${veritysetup} ${hash_device} ${root_hash}"
278 for option in $(echo "${verity_options}" | tr , " ");do
279 veritysetup="${veritysetup} --${option}"
280 done
281 eval "${veritysetup}"
282 set_device_lock "${verity_loop}" \
283 udevadm wait "/dev/mapper/verityroot"
284 return 0
285 else
286 return 1
287 fi
288 }
289
290 function activate_luks {
291 local rootfs_image=$1
292 local mapname=$2
293 declare kiwi_luks_empty_passphrase=${kiwi_luks_empty_passphrase}
294 if ! cryptsetup isLuks "${rootfs_image}" &>/dev/null;then
295 return 1
296 fi
297
298 if [ "${kiwi_luks_empty_passphrase}" = "true" ];then
299 cryptsetup \
300 --key-file /dev/zero \
301 --keyfile-size 32 \
302 luksOpen "${rootfs_image}" "${mapname}"
303 else
304 systemd-cryptsetup attach "${mapname}" "${rootfs_image}"
305 fi
306 }
307
308 function mountReadOnlyRootImageFromContainer {
309 local container_mount_point=$1
310 local overlay_base
311 overlay_base=$(getOverlayBaseDirectory)
312 local rootfs_image="${container_mount_point}/${live_dir}/rootfs.img"
313 local root_mount_point="${overlay_base}/rootfsbase"
314 mkdir -m 0755 -p "${root_mount_point}"
315
316 if activate_luks "${rootfs_image}" luks; then
317 rootfs_image=/dev/mapper/luks
318 fi
319
320 if ! [ -e "${rootfs_image}" ] && [ -d "${container_mount_point}/proc" ]; then
321 # It's the root filesystem directly, just do a bind mount
322 mount -n --bind "${container_mount_point}" "${root_mount_point}"
323 echo "${root_mount_point}"
324 return
325 fi
326
327 if ! mount -n "${rootfs_image}" "${root_mount_point}"; then
328 die "Failed to mount live ISO root filesystem"
329 fi
330 echo "${root_mount_point}"
331 }
332
333 function prepareTemporaryOverlay {
334 local overlay_base
335 overlay_base=$(getOverlayBaseDirectory)
336 mkdir -m 0755 -p "${overlay_base}/overlayfs/rw"
337 mkdir -m 0755 -p "${overlay_base}/overlayfs/work"
338 }
339
340 function preparePersistentOverlay {
341 if [ -z "${isodiskmode}" ] || [ -z "${isodiskdev}" ]; then
342 return 1
343 fi
344 local overlay_base
345 overlay_base=$(getOverlayBaseDirectory)
346 local overlay_mount_point="${overlay_base}/overlayfs"
347 mkdir -m 0755 -p "${overlay_mount_point}"
348 if [ "${isodiskmode}" = "disk_boot" ];then
349 if ! preparePersistentOverlayDiskBoot "${overlay_mount_point}"; then
350 return 1
351 fi
352 else
353 if ! preparePersistentOverlayLoopBoot "${overlay_mount_point}"; then
354 return 1
355 fi
356 fi
357 mkdir -m 0755 -p "${overlay_mount_point}/rw"
358 mkdir -m 0755 -p "${overlay_mount_point}/work"
359 }
360
361 function preparePersistentOverlayLoopBoot {
362 # Create or re-use a cow file on the filesystem the iso
363 # was loopback booted from. If the file already exists
364 # it will be used as presented. If not it will be created
365 # with the custom size configured in rd.live.cowfile.mbsize
366 # or the default size of 500MB
367 local overlay_mount_point=$1
368 local isoscan_loop_mount=/run/initramfs/isoscan
369 local cow_file_name="${isoscan_loop_mount}/${cow_file_path}"
370 mkdir -m 0755 -p "${overlay_mount_point}"
371 if ! mount -o "remount,rw" "${isoscan_loop_mount}"; then
372 return 1
373 fi
374 if activate_luks "${cow_file_name}" lukscow; then
375 cow_file_name=/dev/mapper/lukscow
376 fi
377 if ! mount "${cow_file_name}" "${overlay_mount_point}"; then
378 if ! dd if=/dev/zero of="${cow_file_name}" \
379 count=0 bs=1M seek="${cow_file_mbsize}"; then
380 return 1
381 fi
382 if getargbool 0 rd.live.encrypt; then
383 if prepareEncryptedOverlay "${cow_file_name}" lukscow; then
384 cow_file_name=/dev/mapper/lukscow
385 fi
386 fi
387 if ! mkfs."${cow_filesystem}" "${cow_file_name}"; then
388 return 1
389 fi
390 if ! mount "${cow_file_name}" "${overlay_mount_point}"; then
391 return 1
392 fi
393 fi
394 }
395
396 function preparePersistentOverlayDiskBoot {
397 # Create a write partition and filesystem on the iso
398 # disk device with the cow label and mount it. If the
399 # disk already populates a cow labeled partition it is
400 # used as presented
401 local overlay_mount_point=$1
402 local partitions_before_cow_part
403 mkdir -m 0755 -p "${overlay_mount_point}"
404 activate_luks /dev/disk/by-label/lukscow lukscow
405 if ! mount -L cow "${overlay_mount_point}"; then
406 partitions_before_cow_part=$(_partition_count)
407 echo -e "n\np\n\n\n\nw\nq" | fdisk "${isodiskdev}"
408 if ! set_device_lock "${isodiskdev}" partx -u "${isodiskdev}"; then
409 return 1
410 fi
411 udev_pending
412 if [ "$(_partition_count)" -le "${partitions_before_cow_part}" ];then
413 return 1
414 fi
415 local write_partition
416 write_partition=$(lsblk "${isodiskdev}" -p -l -n -o NAME | tail -n1)
417 if getargbool 0 rd.live.encrypt; then
418 if prepareEncryptedOverlay "${write_partition}" lukscow; then
419 write_partition=/dev/mapper/lukscow
420 fi
421 fi
422 if ! mkfs."${cow_filesystem}" -L cow "${write_partition}"; then
423 return 1
424 fi
425 if ! mount -L cow "${overlay_mount_point}"; then
426 return 1
427 fi
428 fi
429 }
430
431 function prepareEncryptedOverlay {
432 # Create LUKS on provided read-write overlay
433 local device=$1
434 local label=$2
435 cat >/run/dracut-interactive <<-EOF
436 cryptsetup -q luksFormat \\
437 --force-password --type luks2 --label "${label}" "${device}"
438 EOF
439 _run_interactive
440 systemd-cryptsetup attach "${label}" "${device}"
441 }
442
443 function runMediaCheck {
444 # messages are redirected to stderr because this code is called
445 # as part of the pre-mount hook via the dracut-pre-mount.service
446 # which only shows messages on stderr to the console and we want
447 # to see the check results during boot
448 if ! command -v checkmedia &>/dev/null; then
449 echo "No mediacheck program installed, mediacheck skipped" 1>&2
450 return
451 fi
452 local timeout=20
453 local check_result
454 check_result=/run/initramfs/checkmedia.result
455 checkmedia "${media_check_device}" &>${check_result}
456 local check_status=$?
457 if [ ${check_status} != 0 ];then
458 echo "ISO check failed" >> ${check_result}
459 else
460 echo "ISO check passed" >> ${check_result}
461 fi
462 echo "Press key to continue (waiting ${timeout}sec...)" >> ${check_result}
463 _run_dialog --timeout ${timeout} --textbox ${check_result} 20 70
464 if [ ${check_status} != 0 ];then
465 die "Failed to verify system integrity"
466 fi
467 }
468
469 function fetch_file {
470 # Fetch file from remote location
471 local source_url=$1
472 local source_uncompressed_bytes=$2
473 local fetch_info=/tmp/fetch.info
474 local fetch
475 local curl_options
476 local dolly_options
477 curl_options=$(getarg rd.kiwi.live.curl_options=)
478 dolly_options=$(getarg rd.kiwi.live.dolly_options=)
479 if [ -n "${curl_options}" ]; then
480 curl_options=$(str_replace "${curl_options}" "," " ")
481 fetch="curl ${curl_options} -f ${source_url}"
482 else
483 fetch="curl -f ${source_url}"
484 fi
485 if [ -n "${dolly_options}" ]; then
486 dolly_options=$(str_replace "${dolly_options}" "," " ")
487 fetch="dolly ${dolly_options} -f ${source_url}"
488 fi
489 if _is_dolly "${source_url}";then
490 fetch="dolly"
491 fi
492 if _is_xz_compressed "${source_url}";then
493 fetch="${fetch} | xz -d"
494 fi
495 if [ -z "${source_uncompressed_bytes}" ];then
496 eval "${fetch}" 2>${fetch_info}
497 else
498 eval "${fetch}" 2>${fetch_info} |\
499 pv --size "${source_uncompressed_bytes}" --stop-at-size -n
500 fi
501 return "${PIPESTATUS[0]}"
502 }
503
504 function show_log_and_quit {
505 local text_message="$1"
506 local log_file="$2"
507 local timeout
508 timeout=$(_dialog_timeout)
509 if [ "${timeout}" = "off" ];then
510 _run_dialog --backtitle "\"${text_message}\"" \
511 --textbox "${log_file}" 15 80
512 else
513 _run_dialog --timeout "${timeout}" --backtitle "\"${text_message}\"" \
514 --textbox "${log_file}" 15 80
515 fi
516 if getargbool 0 rd.debug; then
517 die "${text_message}"
518 else
519 reboot -f
520 fi
521 }
522
523 #=========================================
524 # Methods considered private
525 #-----------------------------------------
526 function _is_xz_compressed {
527 local source_url=$1
528 [[ ${source_url} =~ .xz$ ]]
529 }
530
531 function _is_dolly {
532 local source_url=$1
533 [[ ${source_url} =~ ^dolly ]]
534 }
535
536 function _setup_interactive_service {
537 local service=/run/systemd/system/dracut-run-interactive.service
538 mkdir -p /run/systemd/system
539 [ -e ${service} ] && return
540 {
541 echo "[Unit]"
542 echo "Description=Dracut Run Interactive"
543 echo "DefaultDependencies=no"
544 echo "After=systemd-vconsole-setup.service"
545 echo "Wants=systemd-vconsole-setup.service"
546 echo "Conflicts=emergency.service emergency.target"
547 echo "[Service]"
548 echo "Environment=HOME=/"
549 echo "Environment=DRACUT_SYSTEMD=1"
550 echo "Environment=NEWROOT=/sysroot"
551 echo "WorkingDirectory=/"
552 echo "ExecStart=/bin/bash /run/dracut-interactive"
553 echo "Type=oneshot"
554 echo "StandardInput=tty-force"
555 echo "StandardOutput=inherit"
556 echo "StandardError=inherit"
557 echo "KillMode=process"
558 echo "IgnoreSIGPIPE=no"
559 echo "TaskMax=infinity"
560 echo "KillSignal=SIGHUP"
561 } > ${service}
562 }
563
564 function _run_interactive {
565 _setup_interactive_service
566 systemctl start dracut-run-interactive.service
567 }
568
569 function _dialog_timeout {
570 local timeout=60
571 custom_timeout=$(getarg "rd.kiwi.dialog.timeout=")
572 [ -n "${custom_timeout}" ] && timeout="${custom_timeout}"
573 echo "${timeout}"
574 }
575
576 function _run_dialog {
577 # """
578 # Run the dialog program via the systemd service either in a
579 # framebuffer terminal or on the console. The return code of
580 # the function is the return code of the dialog call. The
581 # output of the dialog call is stored in a file and can be
582 # one time read via the get_dialog_result function
583 # """
584 local dialog_result=/tmp/dialog_result
585 local dialog_exit_code=/tmp/dialog_code
586 {
587 echo "dialog $* 2>$dialog_result"
588 echo "echo -n \$? >$dialog_exit_code"
589 } >/run/dracut-interactive
590 _run_interactive
591 return "$(cat $dialog_exit_code)"
592 }
593
594 function _partition_count {
595 if [ -z "${isodiskdev}" ]; then
596 echo 0
597 else
598 lsblk "${isodiskdev}" -p -r -n -o TYPE | grep -c part
599 fi
600 }