207404
From 8aa62b8cb28a41d4739633aee9b02e40dc4a75d0 Mon Sep 17 00:00:00 2001
207404
From: Matt Coleman <matt@datto.com>
207404
Date: Fri, 23 Sep 2022 19:28:25 -0400
207404
Subject: [PATCH] feat(dmsquash-live): add new dmsquash-live-autooverlay module
207404
207404
Adapt to test properly with the CentOS Stream 9 container too.
207404
207404
(cherry picked from commit a3c67d27e75223bb45df19f850d246ced9a09938)
207404
207404
Co-authored-by: Neal Gompa <ngompa@datto.com>
207404
207404
Resolves: #2135060
207404
---
207404
 .github/workflows/integration.yml                  |   1 +
207404
 man/dracut.cmdline.7.asc                           |   4 +
207404
 .../create-overlay-genrules.sh                     |  10 ++
207404
 .../90dmsquash-live-autooverlay/create-overlay.sh  | 119 +++++++++++++++++++++
207404
 .../90dmsquash-live-autooverlay/module-setup.sh    |  25 +++++
207404
 pkgbuild/dracut.spec                               |   3 +-
207404
 test/TEST-16-DMSQUASH/create-root.sh               |  12 ++-
207404
 test/TEST-16-DMSQUASH/test-init.sh                 |   6 ++
207404
 test/TEST-16-DMSQUASH/test.sh                      |  38 ++++++-
207404
 test/container/Dockerfile-Arch                     |   2 +-
207404
 test/container/Dockerfile-CentOS-9-Stream          |   2 +
207404
 test/container/Dockerfile-Debian                   |   1 +
207404
 test/container/Dockerfile-Fedora-latest            |   1 +
207404
 test/container/Dockerfile-OpenSuse-latest          |   2 +-
207404
 14 files changed, 218 insertions(+), 8 deletions(-)
207404
207404
diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml
207404
index 40da300c..c22bf916 100644
207404
--- a/.github/workflows/integration.yml
207404
+++ b/.github/workflows/integration.yml
207404
@@ -98,6 +98,7 @@ jobs:
207404
                         "11",
207404
                         "12",
207404
                         "13",
207404
+                        "16",
207404
                         "17",
207404
                         "20",
207404
                         "21",
207404
diff --git a/man/dracut.cmdline.7.asc b/man/dracut.cmdline.7.asc
207404
index fda62fd1..40d13d83 100644
207404
--- a/man/dracut.cmdline.7.asc
207404
+++ b/man/dracut.cmdline.7.asc
207404
@@ -1161,6 +1161,10 @@ rd.live.overlay=/dev/sdb1:persistent-overlay.img
207404
 rd.live.overlay=UUID=99440c1f-8daa-41bf-b965-b7240a8996f4
207404
 --
207404
 
207404
+**rd.live.overlay.cowfs=**__[btrfs|ext4|xfs]__::
207404
+Specifies the filesystem to use when formatting the overlay partition.
207404
+The default is ext4.
207404
+
207404
 **rd.live.overlay.size=**__<size_MiB>__::
207404
 Specifies a non-persistent Device-mapper overlay size in MiB.  The default is
207404
 _32768_.
207404
diff --git a/modules.d/90dmsquash-live-autooverlay/create-overlay-genrules.sh b/modules.d/90dmsquash-live-autooverlay/create-overlay-genrules.sh
207404
new file mode 100755
207404
index 00000000..ed168d9f
207404
--- /dev/null
207404
+++ b/modules.d/90dmsquash-live-autooverlay/create-overlay-genrules.sh
207404
@@ -0,0 +1,10 @@
207404
+#!/bin/sh
207404
+
207404
+# shellcheck disable=SC2154
207404
+case "$root" in
207404
+    live:/dev/*)
207404
+        printf 'SYMLINK=="%s", RUN+="/sbin/initqueue --settled --onetime --unique /sbin/create-overlay %s"\n' \
207404
+            "${root#live:/dev/}" "${root#live:}" >> /etc/udev/rules.d/95-create-overlay.rules
207404
+        wait_for_dev -n "${root#live:}"
207404
+        ;;
207404
+esac
207404
diff --git a/modules.d/90dmsquash-live-autooverlay/create-overlay.sh b/modules.d/90dmsquash-live-autooverlay/create-overlay.sh
207404
new file mode 100755
207404
index 00000000..c89bda2b
207404
--- /dev/null
207404
+++ b/modules.d/90dmsquash-live-autooverlay/create-overlay.sh
207404
@@ -0,0 +1,119 @@
207404
+#!/bin/sh
207404
+
207404
+type getarg > /dev/null 2>&1 || . /lib/dracut-lib.sh
207404
+
207404
+if getargbool 0 rd.live.debug -n -y rdlivedebug; then
207404
+    exec > /tmp/create-overlay.$$.out
207404
+    exec 2>> /tmp/create-overlay.$$.out
207404
+    set -x
207404
+fi
207404
+
207404
+gatherData() {
207404
+    overlay=$(getarg rd.live.overlay)
207404
+    if [ -z "$overlay" ]; then
207404
+        info "Skipping overlay creation: kernel command line parameter 'rd.live.overlay' is not set"
207404
+        exit 0
207404
+    fi
207404
+    # shellcheck disable=SC2086
207404
+    if ! str_starts ${overlay} LABEL=; then
207404
+        die "Overlay creation failed: the partition must be set by LABEL in the 'rd.live.overlay' kernel parameter"
207404
+    fi
207404
+
207404
+    overlayLabel=${overlay#LABEL=}
207404
+    # shellcheck disable=SC2086
207404
+    if [ -b /dev/disk/by-label/${overlayLabel} ]; then
207404
+        info "Skipping overlay creation: overlay already exists"
207404
+        exit 0
207404
+    fi
207404
+
207404
+    filesystem=$(getarg rd.live.overlay.cowfs)
207404
+    [ -z "$filesystem" ] && filesystem="ext4"
207404
+    if [ "$filesystem" != "ext4" ] && [ "$filesystem" != "xfs" ] && [ "$filesystem" != "btrfs" ]; then
207404
+        die "Overlay creation failed: only ext4, xfs, and btrfs are supported in the 'rd.live.overlay.cowfs' kernel parameter"
207404
+    fi
207404
+
207404
+    live_dir=$(getarg rd.live.dir)
207404
+    [ -z "$live_dir" ] && live_dir="LiveOS"
207404
+
207404
+    [ -z "$1" ] && exit 1
207404
+    rootDevice=$1
207404
+
207404
+    # The kernel command line's 'root=' parameter was parsed into the $root variable by the dmsquash-live module.
207404
+    # $root contains the path to a symlink within /dev/disk/by-label, which points to a partition.
207404
+    # This script needs that partition's parent block device.
207404
+    # shellcheck disable=SC2046
207404
+    # shellcheck disable=SC2086
207404
+    rootDeviceAbsolutePath=$(readlink -f ${rootDevice})
207404
+    rootDeviceSysfsPath=/sys/class/block/${rootDeviceAbsolutePath##*/}
207404
+    if [ -f "${rootDeviceSysfsPath}/partition" ]; then
207404
+        # shellcheck disable=SC2086
207404
+        partition=$(cat ${rootDeviceSysfsPath}/partition)
207404
+    else
207404
+        partition=0
207404
+    fi
207404
+    # shellcheck disable=SC2086
207404
+    readonly=$(cat ${rootDeviceSysfsPath}/ro)
207404
+    # shellcheck disable=SC2086
207404
+    if [ "$partition" != "1" ] || [ "$readonly" != "0" ]; then
207404
+        info "Skipping overlay creation: unpartitioned or read-only media detected"
207404
+        exit 0
207404
+    fi
207404
+    # shellcheck disable=SC2046
207404
+    # shellcheck disable=SC2086
207404
+    fullDriveSysfsPath=$(readlink -f ${rootDeviceSysfsPath}/..)
207404
+    blockDevice=/dev/${fullDriveSysfsPath##*/}
207404
+    currentPartitionCount=$(grep --count -E "${blockDevice#/dev/}[0-9]+" /proc/partitions)
207404
+
207404
+    # shellcheck disable=SC2086
207404
+    freeSpaceStart=$(parted --script ${blockDevice} unit % print free \
207404
+        | awk -v x=${currentPartitionCount} '$1 == x {getline; print $1}')
207404
+    if [ -z "$freeSpaceStart" ]; then
207404
+        info "Skipping overlay creation: there is no free space after the last partition"
207404
+        exit 0
207404
+    fi
207404
+    partitionStart=$((${freeSpaceStart%.*} + 1))
207404
+    if [ $partitionStart -eq 100 ]; then
207404
+        info "Skipping overlay creation: there is not enough free space after the last partition"
207404
+        exit 0
207404
+    fi
207404
+
207404
+    overlayPartition=${blockDevice}$((currentPartitionCount + 1))
207404
+
207404
+    label=$(blkid --match-tag LABEL --output value "$rootDevice")
207404
+    uuid=$(blkid --match-tag UUID --output value "$rootDevice")
207404
+    if [ -z "$label" ] || [ -z "$uuid" ]; then
207404
+        die "Overlay creation failed: failed to look up root device label and UUID"
207404
+    fi
207404
+}
207404
+
207404
+createPartition() {
207404
+    # shellcheck disable=SC2086
207404
+    parted --script --align optimal ${blockDevice} mkpart primary ${partitionStart}% 100%
207404
+}
207404
+
207404
+createFilesystem() {
207404
+    # shellcheck disable=SC2086
207404
+    mkfs.${filesystem} -L ${overlayLabel} ${overlayPartition}
207404
+
207404
+    baseDir=/run/initramfs/create-overlayfs
207404
+    mkdir -p ${baseDir}
207404
+    # shellcheck disable=SC2086
207404
+    mount -t auto ${overlayPartition} ${baseDir}
207404
+
207404
+    mkdir -p ${baseDir}/${live_dir}/ovlwork
207404
+    # shellcheck disable=SC2086
207404
+    mkdir ${baseDir}/${live_dir}/overlay-${label}-${uuid}
207404
+
207404
+    umount ${baseDir}
207404
+    rm -r ${baseDir}
207404
+}
207404
+
207404
+main() {
207404
+    gatherData "$1"
207404
+    createPartition
207404
+    udevsettle
207404
+    createFilesystem
207404
+    udevsettle
207404
+}
207404
+
207404
+main "$1"
207404
diff --git a/modules.d/90dmsquash-live-autooverlay/module-setup.sh b/modules.d/90dmsquash-live-autooverlay/module-setup.sh
207404
new file mode 100755
207404
index 00000000..c3712eba
207404
--- /dev/null
207404
+++ b/modules.d/90dmsquash-live-autooverlay/module-setup.sh
207404
@@ -0,0 +1,25 @@
207404
+#!/bin/bash
207404
+
207404
+check() {
207404
+    # including a module dedicated to live environments in a host-only initrd doesn't make sense
207404
+    [[ $hostonly ]] && return 1
207404
+    return 255
207404
+}
207404
+
207404
+depends() {
207404
+    echo dmsquash-live
207404
+    return 0
207404
+}
207404
+
207404
+installkernel() {
207404
+    instmods btrfs ext4 xfs
207404
+}
207404
+
207404
+install() {
207404
+    inst_multiple awk blkid cat grep mkdir mount parted readlink rmdir tr umount
207404
+    inst_multiple -o mkfs.btrfs mkfs.ext4 mkfs.xfs
207404
+    # shellcheck disable=SC2154
207404
+    inst_hook pre-udev 25 "$moddir/create-overlay-genrules.sh"
207404
+    inst_script "$moddir/create-overlay.sh" "/sbin/create-overlay"
207404
+    dracut_need_initqueue
207404
+}
207404
diff --git a/pkgbuild/dracut.spec b/pkgbuild/dracut.spec
207404
index 38de47b4..e148bbf7 100644
207404
--- a/pkgbuild/dracut.spec
207404
+++ b/pkgbuild/dracut.spec
207404
@@ -138,7 +138,7 @@ Requires: %{name} >= %{version}-%{dist_free_release}
207404
 Requires: %{name} = %{version}-%{release}
207404
 %endif
207404
 Requires: %{name}-network = %{version}-%{release}
207404
-Requires: tar gzip coreutils bash device-mapper curl
207404
+Requires: tar gzip coreutils bash device-mapper curl parted
207404
 %if 0%{?fedora}
207404
 Requires: fuse ntfs-3g
207404
 %endif
207404
@@ -461,6 +461,7 @@ echo 'dracut_rescue_image="yes"' > $RPM_BUILD_ROOT%{dracutlibdir}/dracut.conf.d/
207404
 %files live
207404
 %{dracutlibdir}/modules.d/99img-lib
207404
 %{dracutlibdir}/modules.d/90dmsquash-live
207404
+%{dracutlibdir}/modules.d/90dmsquash-live-autooverlay
207404
 %{dracutlibdir}/modules.d/90dmsquash-live-ntfs
207404
 %{dracutlibdir}/modules.d/90livenet
207404
 
207404
diff --git a/test/TEST-16-DMSQUASH/create-root.sh b/test/TEST-16-DMSQUASH/create-root.sh
207404
index 9bc1aa5d..c11e17e0 100755
207404
--- a/test/TEST-16-DMSQUASH/create-root.sh
207404
+++ b/test/TEST-16-DMSQUASH/create-root.sh
207404
@@ -11,9 +11,17 @@ udevadm control --reload
207404
 set -e
207404
 
207404
 udevadm settle
207404
-mkfs.ext4 -q -L dracut /dev/disk/by-id/ata-disk_root
207404
+
207404
+# create a single partition using 50% of the capacity of the image file created by test_setup() in test.sh
207404
+sfdisk /dev/disk/by-id/ata-disk_root << EOF
207404
+2048,161792
207404
+EOF
207404
+
207404
+udevadm settle
207404
+
207404
+mkfs.ext4 -q -L dracut /dev/disk/by-id/ata-disk_root-part1
207404
 mkdir -p /root
207404
-mount /dev/disk/by-id/ata-disk_root /root
207404
+mount /dev/disk/by-id/ata-disk_root-part1 /root
207404
 mkdir -p /root/run /root/testdir
207404
 echo "Creating squashfs"
207404
 mksquashfs /source /root/testdir/rootfs.img -quiet
207404
diff --git a/test/TEST-16-DMSQUASH/test-init.sh b/test/TEST-16-DMSQUASH/test-init.sh
207404
index 068e8f38..959fa25f 100755
207404
--- a/test/TEST-16-DMSQUASH/test-init.sh
207404
+++ b/test/TEST-16-DMSQUASH/test-init.sh
207404
@@ -9,6 +9,12 @@ exec > /dev/console 2>&1
207404
 
207404
 echo "dracut-root-block-success" | dd oflag=direct,dsync of=/dev/disk/by-id/ata-disk_marker
207404
 
207404
+if grep -qF ' rd.live.overlay=LABEL=persist ' /proc/cmdline; then
207404
+    # Writing to a file in the root filesystem lets test_run() verify that the autooverlay module successfully created
207404
+    # and formatted the overlay partition and that the dmsquash-live module used it when setting up the rootfs overlay.
207404
+    echo "dracut-autooverlay-success" > /overlay-marker
207404
+fi
207404
+
207404
 export TERM=linux
207404
 export PS1='initramfs-test:\w\$ '
207404
 [ -f /etc/mtab ] || ln -sfn /proc/mounts /etc/mtab
207404
diff --git a/test/TEST-16-DMSQUASH/test.sh b/test/TEST-16-DMSQUASH/test.sh
207404
index 52029375..58d73b0d 100755
207404
--- a/test/TEST-16-DMSQUASH/test.sh
207404
+++ b/test/TEST-16-DMSQUASH/test.sh
207404
@@ -5,8 +5,9 @@ TEST_DESCRIPTION="live root on a squash filesystem"
207404
 
207404
 KVERSION="${KVERSION-$(uname -r)}"
207404
 
207404
-# Uncomment this to debug failures
207404
-#DEBUGFAIL="rd.shell rd.debug loglevel=7"
207404
+# Uncomment these to debug failures
207404
+#DEBUGFAIL="rd.shell rd.debug rd.live.debug loglevel=7"
207404
+#DEBUGTOOLS="setsid ls cat sfdisk"
207404
 
207404
 test_run() {
207404
     dd if=/dev/zero of="$TESTDIR"/marker.img bs=1MiB count=1
207404
@@ -23,6 +24,27 @@ test_run() {
207404
         -initrd "$TESTDIR"/initramfs.testing
207404
 
207404
     grep -U --binary-files=binary -F -m 1 -q dracut-root-block-success -- "$TESTDIR"/marker.img || return 1
207404
+
207404
+    rootPartitions=$(sfdisk -d "$TESTDIR"/root.img | grep -c 'root\.img[0-9]')
207404
+    [ "$rootPartitions" -eq 1 ] || return 1
207404
+
207404
+    "$testdir"/run-qemu \
207404
+        "${disk_args[@]}" \
207404
+        -boot order=d \
207404
+        -append "rd.live.image rd.live.overlay.overlayfs=1 rd.live.overlay=LABEL=persist rd.live.dir=testdir root=LABEL=dracut console=ttyS0,115200n81 quiet selinux=0 rd.info rd.shell=0 panic=1 oops=panic softlockup_panic=1 $DEBUGFAIL" \
207404
+        -initrd "$TESTDIR"/initramfs.testing-autooverlay
207404
+
207404
+    rootPartitions=$(sfdisk -d "$TESTDIR"/root.img | grep -c 'root\.img[0-9]')
207404
+    [ "$rootPartitions" -eq 2 ] || return 1
207404
+
207404
+    (
207404
+        # Ensure that this test works when run with the `V=1` parameter, which runs the script with `set -o pipefail`.
207404
+        set +o pipefail
207404
+
207404
+        # Verify that the string "dracut-autooverlay-success" occurs in the second partition in the image file.
207404
+        dd if="$TESTDIR"/root.img bs=1MiB skip=80 status=none \
207404
+            | grep -U --binary-files=binary -F -m 1 -q dracut-autooverlay-success
207404
+    ) || return 1
207404
 }
207404
 
207404
 test_setup() {
207404
@@ -49,7 +71,7 @@ test_setup() {
207404
         ln -s dracut-util "${initdir}/usr/bin/dracut-getarg"
207404
         ln -s dracut-util "${initdir}/usr/bin/dracut-getargs"
207404
 
207404
-        inst_multiple mkdir ln dd stty mount poweroff
207404
+        inst_multiple mkdir ln dd stty mount poweroff grep "$DEBUGTOOLS"
207404
 
207404
         cp -a -- /etc/ld.so.conf* "$initdir"/etc
207404
         ldconfig -r "$initdir"
207404
@@ -115,6 +137,16 @@ test_setup() {
207404
         --force "$TESTDIR"/initramfs.testing "$KVERSION" || return 1
207404
 
207404
     ls -sh "$TESTDIR"/initramfs.testing
207404
+
207404
+    "$basedir"/dracut.sh -l -i "$TESTDIR"/overlay / \
207404
+        --modules "dmsquash-live-autooverlay qemu" \
207404
+        --omit "rngd" \
207404
+        --drivers "ext4 sd_mod" \
207404
+        --no-hostonly --no-hostonly-cmdline \
207404
+        --force "$TESTDIR"/initramfs.testing-autooverlay "$KVERSION" || return 1
207404
+
207404
+    ls -sh "$TESTDIR"/initramfs.testing-autooverlay
207404
+
207404
     rm -rf -- "$TESTDIR"/overlay
207404
 }
207404
 
207404
diff --git a/test/container/Dockerfile-Arch b/test/container/Dockerfile-Arch
207404
index ac1513a1..4112cc5b 100644
207404
--- a/test/container/Dockerfile-Arch
207404
+++ b/test/container/Dockerfile-Arch
207404
@@ -12,7 +12,7 @@ RUN pacman --noconfirm -Sy \
207404
     linux dash strace dhclient asciidoc cpio pigz squashfs-tools \
207404
     qemu btrfs-progs mdadm dmraid nfs-utils nfsidmap lvm2 nbd \
207404
     dhcp networkmanager multipath-tools vi tcpdump open-iscsi \
207404
-    git shfmt shellcheck astyle which base-devel && yes | pacman  -Scc
207404
+    git shfmt shellcheck astyle which base-devel glibc parted && yes | pacman -Scc
207404
 
207404
 RUN useradd -m build
207404
 RUN su build -c 'cd && git clone https://aur.archlinux.org/perl-config-general.git && cd perl-config-general && makepkg -s --noconfirm'
207404
diff --git a/test/container/Dockerfile-CentOS-9-Stream b/test/container/Dockerfile-CentOS-9-Stream
207404
index c9a96020..abcc067e 100644
207404
--- a/test/container/Dockerfile-CentOS-9-Stream
207404
+++ b/test/container/Dockerfile-CentOS-9-Stream
207404
@@ -33,8 +33,10 @@ RUN dnf -y install --enablerepo crb --setopt=install_weak_deps=False \
207404
     make \
207404
     mdadm \
207404
     nfs-utils \
207404
+    parted \
207404
     pigz \
207404
     rpm-build \
207404
+    squashfs-tools \
207404
     strace \
207404
     sudo \
207404
     tar \
207404
diff --git a/test/container/Dockerfile-Debian b/test/container/Dockerfile-Debian
207404
index b4c1704e..15eb9958 100644
207404
--- a/test/container/Dockerfile-Debian
207404
+++ b/test/container/Dockerfile-Debian
207404
@@ -42,6 +42,7 @@ RUN apt-get update -y -qq && apt-get upgrade -y -qq && DEBIAN_FRONTEND=nonintera
207404
     network-manager \
207404
     nfs-common \
207404
     open-iscsi \
207404
+    parted \
207404
     pigz \
207404
     pkg-config \
207404
     procps \
207404
diff --git a/test/container/Dockerfile-Fedora-latest b/test/container/Dockerfile-Fedora-latest
207404
index 87c749f9..a38a72ef 100644
207404
--- a/test/container/Dockerfile-Fedora-latest
207404
+++ b/test/container/Dockerfile-Fedora-latest
207404
@@ -49,6 +49,7 @@ RUN dnf -y install --setopt=install_weak_deps=False \
207404
     which \
207404
     ShellCheck \
207404
     shfmt \
207404
+    parted \
207404
     && dnf -y update && dnf clean all
207404
 
207404
 # Set default command
207404
diff --git a/test/container/Dockerfile-OpenSuse-latest b/test/container/Dockerfile-OpenSuse-latest
207404
index 637d50c6..9aaf07b1 100644
207404
--- a/test/container/Dockerfile-OpenSuse-latest
207404
+++ b/test/container/Dockerfile-OpenSuse-latest
207404
@@ -13,7 +13,7 @@ RUN dnf -y install --setopt=install_weak_deps=False \
207404
     strace libkmod-devel gcc bzip2 xz tar wget rpm-build make git bash-completion \
207404
     sudo kernel dhcp-client qemu-kvm /usr/bin/qemu-system-$(uname -m) e2fsprogs \
207404
     tcpdump iproute iputils kbd NetworkManager btrfsprogs tgt dbus-broker \
207404
-    iscsiuio open-iscsi which ShellCheck procps pigz \
207404
+    iscsiuio open-iscsi which ShellCheck procps pigz parted squashfs \
207404
     && dnf -y update && dnf clean all
207404
 
207404
 RUN shfmt_version=3.2.4; wget "https://github.com/mvdan/sh/releases/download/v${shfmt_version}/shfmt_v${shfmt_version}_linux_amd64" -O /usr/local/bin/shfmt \