From 89b9bbdc96d48b9e1d4d6b6036dd504f42409561 Mon Sep 17 00:00:00 2001
From: "A. Wilcox" <AWilcox@Wilcox-Tech.com>
Date: Sat, 29 Aug 2020 07:22:38 -0500
Subject: image: Convert custom cdinit to Dracut

---
 image/backends/initrd.sh.cpp          |  83 ----------------------------------
 image/backends/iso.cc                 |  33 +++++++++-----
 image/iso-share/cdinits/cdinit-pmmx   | Bin 19260 -> 0 bytes
 image/iso-share/cdinits/cdinit-ppc    | Bin 74268 -> 0 bytes
 image/iso-share/cdinits/cdinit-ppc64  | Bin 75608 -> 0 bytes
 image/iso-share/cdinits/cdinit-x86    | Bin 11892 -> 0 bytes
 image/iso-share/cdinits/cdinit-x86_64 | Bin 20792 -> 0 bytes
 image/iso-share/post-pmmx.sh          |   6 +--
 image/iso-share/post-ppc.sh           |   6 +--
 image/iso-share/post-ppc64.sh         |   6 +--
 image/iso-share/post-x86_64.sh        |   6 +--
 11 files changed, 34 insertions(+), 106 deletions(-)
 delete mode 100644 image/backends/initrd.sh.cpp
 delete mode 100755 image/iso-share/cdinits/cdinit-pmmx
 delete mode 100755 image/iso-share/cdinits/cdinit-ppc
 delete mode 100755 image/iso-share/cdinits/cdinit-ppc64
 delete mode 100755 image/iso-share/cdinits/cdinit-x86
 delete mode 100755 image/iso-share/cdinits/cdinit-x86_64

diff --git a/image/backends/initrd.sh.cpp b/image/backends/initrd.sh.cpp
deleted file mode 100644
index 0f4fee7..0000000
--- a/image/backends/initrd.sh.cpp
+++ /dev/null
@@ -1,83 +0,0 @@
-const char *initrd = "#!/bin/sh -e\n\
-        # \n\
-        # This file is part of the Horizon image creation system.\n\
-        # SPDX-License-Identifier: AGPL-3.0-only\n\
-        # \n\
-        # Portions of this file are derived from adelie-build-cd.\n\
-        # \n\
-        \n\
-        log() {\n\
-            # $1 - Type of log message (info, warning, error)\n\
-            # $2 - The message.\n\
-            printf \"%s\tlog\tinitrd: %s: %s\n\" `date -u +%Y-%m-%dT%H:%M:%S.000` \"$1\" \"$2\"\n\
-        }\n\
-        \n\
-        log info 'Creating initrd structure...'\n\
-        \n\
-        ARCH=$1\n\
-	LDARCH=$1\n\
-	case $ARCH in\n\
-	ppc) LDARCH=powerpc;;\n\
-	ppc64) LDARCH=powerpc64;;\n\
-	pmmx) LDARCH=i386;;\n\
-	esac\n\
-        TARGET_DIR=$2\n\
-        CDROOT_DIR=$3\n\
-        CDINIT_DIR=$4\n\
-        INITRD_DIR=`mktemp -d -t 'hinitrd-XXXXXX'`\n\
-        if [ $? -ne 0 ]; then\n\
-            log error 'cannot create temporary directory'\n\
-            exit 1\n\
-        fi\n\
-        \n\
-        # mount points\n\
-        mkdir ${INITRD_DIR}/dev\n\
-        mkdir ${INITRD_DIR}/media\n\
-        for _rootdir in newroot lowerroot upperroot; do\n\
-            mkdir ${INITRD_DIR}/$_rootdir\n\
-            chmod 755 ${INITRD_DIR}/$_rootdir\n\
-        done\n\
-        mkdir ${INITRD_DIR}/proc\n\
-        mkdir ${INITRD_DIR}/sys\n\
-        \n\
-        # manual /dev nodes for initial udev startup\n\
-        mknod -m 600 ${INITRD_DIR}/dev/console c 5 1\n\
-        mknod -m 666 ${INITRD_DIR}/dev/null c 1 3\n\
-        mknod -m 666 ${INITRD_DIR}/dev/ptmx c 5 2\n\
-        mknod -m 666 ${INITRD_DIR}/dev/random c 1 8\n\
-        mknod -m 666 ${INITRD_DIR}/dev/tty c 5 0\n\
-        mknod -m 620 ${INITRD_DIR}/dev/tty1 c 4 1\n\
-        mknod -m 666 ${INITRD_DIR}/dev/urandom c 1 9\n\
-        mknod -m 666 ${INITRD_DIR}/dev/zero c 1 5\n\
-        \n\
-        # base\n\
-        mkdir ${INITRD_DIR}/lib\n\
-        cp ${TARGET_DIR}/lib/ld-musl-$LDARCH.so.1 ${INITRD_DIR}/lib/\n\
-        cp ${TARGET_DIR}/lib/libblkid.so.1 ${INITRD_DIR}/lib/\n\
-        cp ${TARGET_DIR}/lib/libuuid.so.1 ${INITRD_DIR}/lib/\n\
-        \n\
-        # udev\n\
-        mkdir -p ${INITRD_DIR}/etc/udev\n\
-        mkdir ${INITRD_DIR}/run\n\
-        mkdir ${INITRD_DIR}/sbin\n\
-        cp ${TARGET_DIR}/bin/udevadm ${INITRD_DIR}/sbin/\n\
-        cp ${TARGET_DIR}/sbin/udevd ${INITRD_DIR}/sbin/\n\
-        cp ${TARGET_DIR}/lib/libkmod.so.2 ${INITRD_DIR}/lib/\n\
-	cp ${TARGET_DIR}/lib/libcrypto.so.1.1 ${INITRD_DIR}/lib/\n\
-        cp ${TARGET_DIR}/lib/liblzma.so.5 ${INITRD_DIR}/lib/\n\
-        cp ${TARGET_DIR}/lib/libudev.so.1 ${INITRD_DIR}/lib/\n\
-        cp ${TARGET_DIR}/lib/libz.so.1 ${INITRD_DIR}/lib/\n\
-        \n\
-        if [ -n ${CDINIT_DIR} ]; then\n\
-                cp ${CDINIT_DIR}/cdinit-$ARCH ${INITRD_DIR}/init\n\
-                chmod 755 ${INITRD_DIR}/init\n\
-        else\n\
-                log error 'No cdinit binary found, and compilation is not yet supported'\n\
-                exit 2\n\
-        fi\n\
-        \n\
-        log info 'Compressing initrd...'\n\
-        \n\
-        (cd ${INITRD_DIR}; find . | cpio -H newc -o) > ${CDROOT_DIR}/initrd-$ARCH\n\
-        gzip -9 ${CDROOT_DIR}/initrd-$ARCH\n\
-        mv ${CDROOT_DIR}/initrd-$ARCH.gz ${CDROOT_DIR}/initrd-$ARCH";
diff --git a/image/backends/iso.cc b/image/backends/iso.cc
index b0936c3..89d1e84 100644
--- a/image/backends/iso.cc
+++ b/image/backends/iso.cc
@@ -350,18 +350,29 @@ public:
 
         /* REQ: ISO.23 */
         output_info("CD backend", "creating initrd");
-        std::string cdinit_path{""};
-        for(const auto &path : data_dirs()) {
-            fs::path candidate{fs::path{path}.append("horizon").append("iso")
-                               .append("cdinits")};
-            if(fs::exists(candidate, ec)) {
-                cdinit_path = candidate;
+        std::string kver, kverpath;
+        for(const auto &dent :
+            fs::recursive_directory_iterator(target + "/usr/share/kernel", ec))
+        {
+            if(dent.is_regular_file() &&
+               dent.path().filename().string() == "kernel.release") {
+                kverpath = dent.path().string();
+                break;
             }
         }
-#include "initrd.sh.cpp"
-        if(run_command("/bin/sh", {"-ec", initrd, "mkinitrd", my_arch, target,
-                                   cdpath, cdinit_path}) != 0) {
-            output_error("CD backend", "failed to create initrd");
+        if(kverpath.length() == 0) {
+            output_error("CD backend", "cannot find kernel.release");
+            return FS_ERROR;
+        }
+        std::ifstream kverstream(kverpath);
+        kverstream >> kver;
+
+        /* dracut with -r can't autodetect udev directory without udev.pc */
+        ::setenv("udevdir", "/lib/udev", 0);
+        if(run_command("dracut", {"--kver", kver, "-r", target+"/", "-N",
+                                  "--force", "-a", "dmsquash-live",
+                                  cdpath + "/initrd-" + my_arch}) != 0) {
+            output_error("CD backend", "dracut failed to create initramfs");
             return COMMAND_ERROR;
         }
 
@@ -409,7 +420,7 @@ public:
         output_info("CD backend", "creating ISO");
         std::vector<std::string> iso_args = {"-as", "mkisofs", "-o", out_path,
                                              "-joliet", "-rational-rock", "-V",
-                                             "Adelie "+my_arch};
+                                             "Adelie-"+my_arch};
         std::vector<std::string> arch_args;
         std::string raw_arch;
         {
diff --git a/image/iso-share/cdinits/cdinit-pmmx b/image/iso-share/cdinits/cdinit-pmmx
deleted file mode 100755
index c59bcfb..0000000
Binary files a/image/iso-share/cdinits/cdinit-pmmx and /dev/null differ
diff --git a/image/iso-share/cdinits/cdinit-ppc b/image/iso-share/cdinits/cdinit-ppc
deleted file mode 100755
index f98d2c4..0000000
Binary files a/image/iso-share/cdinits/cdinit-ppc and /dev/null differ
diff --git a/image/iso-share/cdinits/cdinit-ppc64 b/image/iso-share/cdinits/cdinit-ppc64
deleted file mode 100755
index 1e5dcdb..0000000
Binary files a/image/iso-share/cdinits/cdinit-ppc64 and /dev/null differ
diff --git a/image/iso-share/cdinits/cdinit-x86 b/image/iso-share/cdinits/cdinit-x86
deleted file mode 100755
index e4df83e..0000000
Binary files a/image/iso-share/cdinits/cdinit-x86 and /dev/null differ
diff --git a/image/iso-share/cdinits/cdinit-x86_64 b/image/iso-share/cdinits/cdinit-x86_64
deleted file mode 100755
index 366dc48..0000000
Binary files a/image/iso-share/cdinits/cdinit-x86_64 and /dev/null differ
diff --git a/image/iso-share/post-pmmx.sh b/image/iso-share/post-pmmx.sh
index 09aeea5..3b8d518 100755
--- a/image/iso-share/post-pmmx.sh
+++ b/image/iso-share/post-pmmx.sh
@@ -3,7 +3,7 @@
 mkdir -p cdroot/boot
 
 cat >early.cfg <<'EARLYCFG'
-search.fs_label "Adelie pmmx" root
+search.fs_label "Adelie-pmmx" root
 set prefix=($root)/boot
 EARLYCFG
 
@@ -11,8 +11,8 @@ cat >cdroot/boot/grub.cfg <<'GRUBCFG'
 menuentry "Adelie Linux Live (Intel 32-bit)" --class linux --id adelie-live-cd {
         insmod iso9660
         insmod linux
-        search --label "Adelie pmmx" --no-floppy --set
-        linux ($root)/kernel-pmmx squashroot=pmmx.squashfs
+        search --label "Adelie-pmmx" --no-floppy --set
+        linux ($root)/kernel-pmmx root=live:LABEL=Adelie-pmmx rd.live.dir=/ rd.live.squashimg=pmmx.squashfs
         initrd ($root)/initrd-pmmx
 }
 
diff --git a/image/iso-share/post-ppc.sh b/image/iso-share/post-ppc.sh
index 9623a50..e834d7b 100644
--- a/image/iso-share/post-ppc.sh
+++ b/image/iso-share/post-ppc.sh
@@ -3,7 +3,7 @@
 mkdir -p cdroot/boot
 
 cat >early.cfg <<'EARLYCFG'
-search.fs_label "Adelie ppc" root
+search.fs_label "Adelie-ppc" root
 set prefix=($root)/boot
 EARLYCFG
 
@@ -23,8 +23,8 @@ menuentry "Adelie Linux Live (PowerPC 32-bit)" --class linux --id adelie-live-cd
         insmod part_apple
         insmod iso9660
         insmod linux
-        search --label "Adelie ppc" --hint cd,apple2 --no-floppy --set
-        linux ($root)/kernel-ppc squashroot=ppc.squashfs
+        search --label "Adelie-ppc" --hint cd,apple2 --no-floppy --set
+        linux ($root)/kernel-ppc root=live:LABEL=Adelie-ppc rd.live.dir=/ rd.live.squashimg=ppc.squashfs
         initrd ($root)/initrd-ppc
 }
 
diff --git a/image/iso-share/post-ppc64.sh b/image/iso-share/post-ppc64.sh
index e5b5c65..7c6e76f 100755
--- a/image/iso-share/post-ppc64.sh
+++ b/image/iso-share/post-ppc64.sh
@@ -4,7 +4,7 @@ mkdir -p cdroot/boot
 
 # /boot/grub instead of /boot for Petitboot compatibility.
 cat >early.cfg <<'EARLYCFG'
-search.fs_label "Adelie ppc64" root
+search.fs_label "Adelie-ppc64" root
 set prefix=($root)/boot/grub
 EARLYCFG
 
@@ -25,8 +25,8 @@ menuentry "Adelie Linux Live (PowerPC 64-bit)" --class linux --id adelie-live-cd
         insmod part_apple
         insmod iso9660
         insmod linux
-        search --label "Adelie ppc64" --hint cd,apple2 --no-floppy --set
-        linux ($root)/kernel-ppc64 squashroot=ppc64.squashfs
+        search --label "Adelie-ppc64" --hint cd,apple2 --no-floppy --set
+        linux ($root)/kernel-ppc64 root=live:LABEL=Adelie-ppc64 rd.live.dir=/ rd.live.squashimg=ppc64.squashfs
         initrd ($root)/initrd-ppc64
 }
 
diff --git a/image/iso-share/post-x86_64.sh b/image/iso-share/post-x86_64.sh
index 5594a13..e8b904d 100755
--- a/image/iso-share/post-x86_64.sh
+++ b/image/iso-share/post-x86_64.sh
@@ -3,7 +3,7 @@
 mkdir -p cdroot/boot
 
 cat >early.cfg <<'EARLYCFG'
-search.fs_label "Adelie x86_64" root
+search.fs_label "Adelie-x86_64" root
 set prefix=($root)/boot
 EARLYCFG
 
@@ -11,8 +11,8 @@ cat >cdroot/boot/grub.cfg <<'GRUBCFG'
 menuentry "Adelie Linux Live (Intel 64-bit)" --class linux --id adelie-live-cd {
         insmod iso9660
         insmod linux
-        search --label "Adelie x86_64" --no-floppy --set
-        linux ($root)/kernel-x86_64 squashroot=x86_64.squashfs
+        search --label "Adelie-x86_64" --no-floppy --set
+        linux ($root)/kernel-x86_64 root=live:LABEL=Adelie-x86_64 rd.live.dir=/ rd.live.squashimg=x86_64.squashfs
         initrd ($root)/initrd-x86_64
 }
 
-- 
cgit v1.2.3-70-g09d2