diff --git a/build-image.sh b/build-image.sh index dcb6811..363ec3f 100755 --- a/build-image.sh +++ b/build-image.sh @@ -199,26 +199,21 @@ umount -l $BUILD_DIR/rootfs/proc rm $BUILD_DIR/rootfs/setup.sh -# Build initramfs with dracut using the rootfs modules -echo "Building initramfs with dracut using rootfs kernel and modules..." +# Build initramfs with custom netboot hooks/scripts +echo "Building custom netboot initramfs..." KERNEL_VERSION=$(ls -1 $BUILD_DIR/rootfs/boot/vmlinuz-* | sed 's|.*/vmlinuz-||' | head -1) -# Mount proc/sys temporarily for dracut if needed -mount -t proc proc $BUILD_DIR/rootfs/proc 2>/dev/null || true - -dracut -f \ - --add "network" \ - --hostonly \ - --hostonly-cmdline \ - --include "/usr/share/initramfs-tools/hooks/rooturl" "/usr/share/initramfs-tools/hooks/" \ - --include "/usr/share/initramfs-tools/scripts/local-top/rooturl" "/usr/share/initramfs-tools/scripts/local-top/" \ - -k $KERNEL_VERSION \ - -r $BUILD_DIR/rootfs \ - $BUILD_DIR/rootfs/boot/initrd-netboot.img - -umount -l $BUILD_DIR/rootfs/proc 2>/dev/null || true - -echo "Initramfs build complete. Size: $(du -h $BUILD_DIR/rootfs/boot/initrd-netboot.img | cut -f1)" +# Use mkinitramfs with custom initramfs directory +INITRAMFS_CONFIG="/srv/netboot/initramfs" +if [ -d "$INITRAMFS_CONFIG" ]; then + mkinitramfs -d "$INITRAMFS_CONFIG" \ + -k "$KERNEL_VERSION" \ + -o $BUILD_DIR/rootfs/boot/initrd-netboot.img + echo "Initramfs build complete. Size: $(du -h $BUILD_DIR/rootfs/boot/initrd-netboot.img | cut -f1)" +else + echo "ERROR: Custom initramfs config not found at $INITRAMFS_CONFIG" + exit 1 +fi # Copy kernel and netboot initramfs mkdir -p $IMAGE_DIR/$VERSION diff --git a/dracut-module/90netboot/module-setup.sh b/dracut-module/90netboot/module-setup.sh new file mode 100755 index 0000000..416829b --- /dev/null +++ b/dracut-module/90netboot/module-setup.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +check() { + return 0 +} + +depends() { + echo network url-lib + return 0 +} + +install() { + inst_hook cmdline 90 "$moddir/parse-netboot.sh" + inst_hook pre-mount 90 "$moddir/mount-netboot.sh" + inst_multiple wget curl +} diff --git a/dracut-module/90netboot/mount-netboot.sh b/dracut-module/90netboot/mount-netboot.sh new file mode 100755 index 0000000..f31fef2 --- /dev/null +++ b/dracut-module/90netboot/mount-netboot.sh @@ -0,0 +1,32 @@ +#!/bin/sh +# Download and mount squashfs root with overlay + +. /tmp/netboot.conf + +info "Downloading root filesystem from http://${BOOT_SERVER}/${IMAGE_VERSION}/filesystem.squashfs" + +mkdir -p /run/netboot +wget -O /run/netboot/root.squashfs \ + "http://${BOOT_SERVER}/${IMAGE_VERSION}/filesystem.squashfs" || \ + die "Failed to download root filesystem from $BOOT_SERVER" + +info "Download complete, mounting filesystems..." + +# Mount squashfs (read-only base) +mkdir -p /run/rootfs/lower +mount -t squashfs /run/netboot/root.squashfs /run/rootfs/lower || \ + die "Failed to mount squashfs" + +# Create tmpfs for overlay (writable layer) +mkdir -p /run/rootfs/upper /run/rootfs/work +mount -t tmpfs -o size=4G tmpfs /run/rootfs/upper || \ + die "Failed to create tmpfs overlay" +mkdir -p /run/rootfs/upper/upper /run/rootfs/upper/work + +# Mount overlay (combines read-only base + writable tmpfs) +mount -t overlay overlay \ + -o lowerdir=/run/rootfs/lower,upperdir=/run/rootfs/upper/upper,workdir=/run/rootfs/upper/work \ + /sysroot || \ + die "Failed to mount overlay" + +info "Root filesystem mounted successfully" diff --git a/dracut-module/90netboot/parse-netboot.sh b/dracut-module/90netboot/parse-netboot.sh new file mode 100755 index 0000000..a109433 --- /dev/null +++ b/dracut-module/90netboot/parse-netboot.sh @@ -0,0 +1,13 @@ +#!/bin/sh +# Parse netboot parameters from kernel command line + +BOOT_SERVER=$(getarg boot_server=) +IMAGE_VERSION=$(getarg image_version=) + +[ -z "$BOOT_SERVER" ] && BOOT_SERVER="192.168.100.1" +[ -z "$IMAGE_VERSION" ] && IMAGE_VERSION="latest" + +echo "BOOT_SERVER=$BOOT_SERVER" > /tmp/netboot.conf +echo "IMAGE_VERSION=$IMAGE_VERSION" >> /tmp/netboot.conf + +info "Netboot: server=$BOOT_SERVER version=$IMAGE_VERSION"