]> git.ipfire.org Git - thirdparty/dracut-ng.git/commitdiff
refactor(test): introduce build_ext4_image function
authorBenjamin Drung <benjamin.drung@canonical.com>
Thu, 30 Oct 2025 11:14:06 +0000 (12:14 +0100)
committerNeal Gompa (ニール・ゴンパ) <ngompa13@gmail.com>
Thu, 30 Oct 2025 11:43:16 +0000 (07:43 -0400)
Building an ext4 rootfs image from a directory is done in multiple test
cases. Move those code into a single `build_ext4_image` helper function
to reduce code duplication.

test/TEST-10-BASIC/test.sh
test/TEST-13-SYSROOT/test.sh
test/TEST-43-KERNEL-INSTALL/test.sh
test/TEST-50-NETWORK/test.sh
test/test-functions

index 9d47873455a86206626cf5838c4c72af13935660..a5d2a0a641e72ae947d991a3b3f5a5ab2ccf100a 100755 (executable)
@@ -26,10 +26,7 @@ test_setup() {
         --add-confdir test-root \
         -f "$TESTDIR"/initramfs.root
 
-    dd if=/dev/zero of="$TESTDIR"/root.img bs=200MiB count=1 status=none
-    sync "$TESTDIR"/root.img
-    mkfs.ext4 -q -L dracut -d "$TESTDIR"/dracut.*/initramfs/ "$TESTDIR"/root.img
-    sync "$TESTDIR"/root.img
+    build_ext4_image "$TESTDIR"/dracut.*/initramfs/ "$TESTDIR"/root.img dracut
 
     test_dracut
 }
index 69d816d53573ce4173b4d999c22b490efad0c062..3a069fc834b64752cfe0ecc18fb6f6ac4b0afa8e 100755 (executable)
@@ -26,10 +26,7 @@ test_setup() {
         --add-confdir test-root \
         -f "$TESTDIR"/initramfs.root
 
-    dd if=/dev/zero of="$TESTDIR"/root.img bs=200MiB count=1 status=none
-    sync "$TESTDIR"/root.img
-    mkfs.ext4 -q -L dracut -d "$TESTDIR"/dracut.*/initramfs/ "$TESTDIR"/root.img
-    sync "$TESTDIR"/root.img
+    build_ext4_image "$TESTDIR"/dracut.*/initramfs/ "$TESTDIR"/root.img dracut
 
     ln -s / "$TESTDIR"/sysroot
     test_dracut --hostonly --sysroot "$TESTDIR"/sysroot
index 5536542ed5a83544feec96fdf71195fc65ef0734..345e3367a0b6014454be6eb48147b7e6cc3b45e1 100755 (executable)
@@ -47,10 +47,7 @@ test_setup() {
     KVERSION=$(determine_kernel_version "$TESTDIR"/initramfs.root)
     KIMAGE=$(determine_kernel_image "$KVERSION")
 
-    dd if=/dev/zero of="$TESTDIR"/root.img bs=200MiB count=1 status=none
-    sync "$TESTDIR"/root.img
-    mkfs.ext4 -q -L dracut -d "$TESTDIR"/dracut.*/initramfs/ "$TESTDIR"/root.img
-    sync "$TESTDIR"/root.img
+    build_ext4_image "$TESTDIR"/dracut.*/initramfs/ "$TESTDIR"/root.img dracut
 
     mkdir -p /run/kernel /run/initramfs/dracut.conf.d
     echo 'initrd_generator=dracut' >> /run/kernel/install.conf
index 3f338e6d7ce3e9f55647a7579868ada9ccd2412b..765aeaa012b1404e18561e8e0e530be02b18d41f 100755 (executable)
@@ -32,10 +32,7 @@ test_setup() {
         --add-confdir test-root \
         -f "$TESTDIR"/initramfs.root "$KVERSION"
 
-    dd if=/dev/zero of="$TESTDIR"/root.img bs=200MiB count=1 status=none
-    sync "$TESTDIR"/root.img
-    mkfs.ext4 -q -L dracut -d "$TESTDIR"/dracut.*/initramfs/ "$TESTDIR"/root.img
-    sync "$TESTDIR"/root.img
+    build_ext4_image "$TESTDIR"/dracut.*/initramfs/ "$TESTDIR"/root.img dracut
 
     test_dracut --add-drivers "virtio_net" --add "qemu-net $USE_NETWORK"
 }
index 951a7fe42cc06db24d6568042a4cbdeea35c86ca..8ad6f7d373b30ac2fb69b8755213384626e9508a 100644 (file)
@@ -151,6 +151,18 @@ COLOR_FAILURE='\033[0;31m'
 COLOR_WARNING='\033[0;33m'
 COLOR_NORMAL='\033[0;39m'
 
+# build an ext4 image from the given directory
+build_ext4_image() {
+    local source_dir="$1"
+    local image="$2"
+    local label="$3"
+
+    dd if=/dev/zero of="$image" bs=200MiB count=1 status=none
+    sync "$image"
+    mkfs.ext4 -q -L "$label" -d "$source_dir" "$image"
+    sync "$image"
+}
+
 # override the init script from the test-root dracut module (see module-setup.sh)
 inst_init() {
     local init_script="$1"