From be09b89856110382edf1326a65904d93ebd646cb Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 29 Dec 2025 19:08:11 +0000 Subject: [PATCH] Extract Docker RUN commands into shell scripts for better maintainability Co-authored-by: johndoe6345789 <224850594+johndoe6345789@users.noreply.github.com> --- Dockerfile | 31 ++---- Dockerfile.image | 138 ++---------------------- scripts/docker-image-create-uefi.sh | 119 ++++++++++++++++++++ scripts/docker-image-download-kernel.sh | 10 ++ scripts/docker-image-install-tools.sh | 20 ++++ scripts/docker-install-packages.sh | 6 ++ scripts/docker-setup-config.sh | 19 ++++ scripts/docker-setup-rootfs.sh | 25 +++++ 8 files changed, 211 insertions(+), 157 deletions(-) create mode 100755 scripts/docker-image-create-uefi.sh create mode 100755 scripts/docker-image-download-kernel.sh create mode 100755 scripts/docker-image-install-tools.sh create mode 100755 scripts/docker-install-packages.sh create mode 100755 scripts/docker-setup-config.sh create mode 100755 scripts/docker-setup-rootfs.sh diff --git a/Dockerfile b/Dockerfile index 4fdbc13..18848aa 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,40 +20,21 @@ FROM alpine:3.19 # Install file command for testing init binary # file package provides the file(1) command to determine file type -RUN apk add --no-cache file +COPY scripts/docker-install-packages.sh /tmp/ +RUN /tmp/docker-install-packages.sh # Note: Alpine includes busybox by default # Create minimal rootfs structure -RUN mkdir -p /sparkos/rootfs/bin \ - /sparkos/rootfs/sbin \ - /sparkos/rootfs/etc \ - /sparkos/rootfs/proc \ - /sparkos/rootfs/sys \ - /sparkos/rootfs/dev \ - /sparkos/rootfs/tmp \ - /sparkos/rootfs/usr/bin \ - /sparkos/rootfs/usr/sbin \ - /sparkos/rootfs/usr/lib \ - /sparkos/rootfs/var/log \ - /sparkos/rootfs/var/run \ - /sparkos/rootfs/root \ - /sparkos/rootfs/home/spark && \ - chmod 1777 /sparkos/rootfs/tmp && \ - chmod 700 /sparkos/rootfs/root && \ - chmod 755 /sparkos/rootfs/home/spark +COPY scripts/docker-setup-rootfs.sh /tmp/ +RUN /tmp/docker-setup-rootfs.sh # Copy built init binary from builder COPY --from=builder /build/init /sparkos/rootfs/sbin/init # Set up basic configuration files -RUN echo "sparkos" > /sparkos/rootfs/etc/hostname && \ - echo "127.0.0.1 localhost" > /sparkos/rootfs/etc/hosts && \ - echo "127.0.1.1 sparkos" >> /sparkos/rootfs/etc/hosts && \ - echo "root:x:0:0:root:/root:/bin/sh" > /sparkos/rootfs/etc/passwd && \ - echo "spark:x:1000:1000:SparkOS User:/home/spark:/bin/sh" >> /sparkos/rootfs/etc/passwd && \ - echo "root:x:0:" > /sparkos/rootfs/etc/group && \ - echo "spark:x:1000:" >> /sparkos/rootfs/etc/group +COPY scripts/docker-setup-config.sh /tmp/ +RUN /tmp/docker-setup-config.sh # Create a test entrypoint COPY scripts/test.sh /sparkos/test.sh diff --git a/Dockerfile.image b/Dockerfile.image index c36446b..1f73314 100644 --- a/Dockerfile.image +++ b/Dockerfile.image @@ -4,21 +4,8 @@ FROM ubuntu:22.04 AS image-builder # Install required tools -RUN apt-get update && \ - apt-get install -y \ - gcc \ - make \ - dosfstools \ - mtools \ - e2fsprogs \ - parted \ - gdisk \ - grub-efi-amd64-bin \ - grub-common \ - wget \ - busybox-static \ - kmod \ - && rm -rf /var/lib/apt/lists/* +COPY scripts/docker-image-install-tools.sh /tmp/ +RUN /tmp/docker-image-install-tools.sh WORKDIR /build @@ -33,125 +20,12 @@ COPY rootfs/ ./rootfs/ RUN make init # Download a minimal Linux kernel (using Ubuntu's kernel for now) -RUN mkdir -p /kernel && \ - apt-get update && \ - apt-get download linux-image-generic && \ - dpkg -x linux-image-*.deb /kernel && \ - rm -rf /var/lib/apt/lists/* linux-image-*.deb +COPY scripts/docker-image-download-kernel.sh /tmp/ +RUN /tmp/docker-image-download-kernel.sh # Create UEFI-bootable image with GPT partition table -RUN mkdir -p /output /mnt/esp /mnt/root && \ - echo "=== Creating UEFI-bootable SparkOS image with GRUB ===" && \ - # Create 1GB disk image (larger for kernel + bootloader) - dd if=/dev/zero of=/output/sparkos.img bs=1M count=1024 && \ - \ - # Create GPT partition table - echo "Creating GPT partition table..." && \ - parted -s /output/sparkos.img mklabel gpt && \ - \ - # Create EFI System Partition (ESP) - 200MB, FAT32 - echo "Creating EFI System Partition..." && \ - parted -s /output/sparkos.img mkpart ESP fat32 1MiB 201MiB && \ - parted -s /output/sparkos.img set 1 esp on && \ - \ - # Create root partition - remaining space, ext4 - echo "Creating root partition..." && \ - parted -s /output/sparkos.img mkpart primary ext4 201MiB 100% && \ - \ - # Set up loop device for the image - LOOP_DEV=$(losetup -f) && \ - losetup -P $LOOP_DEV /output/sparkos.img && \ - \ - # Wait for partition devices - sleep 1 && \ - \ - # Format partitions - echo "Formatting EFI System Partition (FAT32)..." && \ - mkfs.vfat -F 32 -n "SPARKOSEFI" ${LOOP_DEV}p1 && \ - \ - echo "Formatting root partition (ext4)..." && \ - mkfs.ext4 -L "SparkOS" ${LOOP_DEV}p2 && \ - \ - # Mount ESP - echo "Mounting partitions..." && \ - mount ${LOOP_DEV}p1 /mnt/esp && \ - mount ${LOOP_DEV}p2 /mnt/root && \ - \ - # Install GRUB to ESP - echo "Installing GRUB bootloader..." && \ - mkdir -p /mnt/esp/EFI/BOOT && \ - \ - # Create GRUB EFI binary using grub-mkstandalone - grub-mkstandalone \ - --format=x86_64-efi \ - --output=/mnt/esp/EFI/BOOT/BOOTX64.EFI \ - --locales="" \ - --fonts="" \ - "boot/grub/grub.cfg=/dev/null" && \ - \ - # Find the kernel - KERNEL_PATH=$(find /kernel/boot -name "vmlinuz-*" | head -1) && \ - KERNEL_VERSION=$(basename $KERNEL_PATH | sed 's/vmlinuz-//') && \ - INITRD_PATH=$(find /kernel/boot -name "initrd.img-*" | head -1) && \ - \ - # Copy kernel and initrd to ESP - echo "Installing kernel..." && \ - mkdir -p /mnt/esp/boot && \ - cp $KERNEL_PATH /mnt/esp/boot/vmlinuz && \ - if [ -f "$INITRD_PATH" ]; then cp $INITRD_PATH /mnt/esp/boot/initrd.img; fi && \ - \ - # Create GRUB configuration - mkdir -p /mnt/esp/boot/grub && \ - printf '%s\n' \ - 'set timeout=3' \ - 'set default=0' \ - '' \ - 'menuentry "SparkOS" {' \ - ' linux /boot/vmlinuz root=LABEL=SparkOS rw init=/sbin/init console=tty1 quiet' \ - '}' \ - > /mnt/esp/boot/grub/grub.cfg && \ - \ - # Set up root filesystem - echo "Setting up root filesystem..." && \ - mkdir -p /mnt/root/{bin,sbin,etc,proc,sys,dev,tmp,usr/{bin,sbin,lib,lib64},var/{log,run},root,home/spark,boot} && \ - \ - # Install SparkOS init - cp /build/init /mnt/root/sbin/init && \ - chmod 755 /mnt/root/sbin/init && \ - \ - # Install busybox - echo "Installing busybox..." && \ - cp /bin/busybox /mnt/root/bin/busybox && \ - chmod 755 /mnt/root/bin/busybox && \ - \ - # Create busybox symlinks for essential commands - for cmd in sh ls cat echo mount umount mkdir rm cp mv chmod chown ln ps kill; do \ - ln -sf busybox /mnt/root/bin/$cmd; \ - done && \ - \ - # Create system configuration files - echo "sparkos" > /mnt/root/etc/hostname && \ - echo "127.0.0.1 localhost" > /mnt/root/etc/hosts && \ - echo "127.0.1.1 sparkos" >> /mnt/root/etc/hosts && \ - echo "root:x:0:0:root:/root:/bin/sh" > /mnt/root/etc/passwd && \ - echo "spark:x:1000:1000:SparkOS User:/home/spark:/bin/sh" >> /mnt/root/etc/passwd && \ - echo "root:x:0:" > /mnt/root/etc/group && \ - echo "spark:x:1000:" >> /mnt/root/etc/group && \ - \ - # Copy README to root partition - cp /build/config/image-readme.txt /mnt/root/README.txt && \ - \ - # Sync and unmount - echo "Finalizing image..." && \ - sync && \ - umount /mnt/esp && \ - umount /mnt/root && \ - losetup -d $LOOP_DEV && \ - \ - # Compress the image - echo "Compressing image..." && \ - gzip -9 /output/sparkos.img && \ - echo "UEFI-bootable image created: /output/sparkos.img.gz" +COPY scripts/docker-image-create-uefi.sh /tmp/ +RUN /tmp/docker-image-create-uefi.sh # Final stage - export the image FROM scratch AS export diff --git a/scripts/docker-image-create-uefi.sh b/scripts/docker-image-create-uefi.sh new file mode 100755 index 0000000..b690b77 --- /dev/null +++ b/scripts/docker-image-create-uefi.sh @@ -0,0 +1,119 @@ +#!/bin/bash +# Create UEFI-bootable SparkOS image with GPT partition table + +set -e + +mkdir -p /output /mnt/esp /mnt/root + +echo "=== Creating UEFI-bootable SparkOS image with GRUB ===" + +# Create 1GB disk image (larger for kernel + bootloader) +dd if=/dev/zero of=/output/sparkos.img bs=1M count=1024 + +# Create GPT partition table +echo "Creating GPT partition table..." +parted -s /output/sparkos.img mklabel gpt + +# Create EFI System Partition (ESP) - 200MB, FAT32 +echo "Creating EFI System Partition..." +parted -s /output/sparkos.img mkpart ESP fat32 1MiB 201MiB +parted -s /output/sparkos.img set 1 esp on + +# Create root partition - remaining space, ext4 +echo "Creating root partition..." +parted -s /output/sparkos.img mkpart primary ext4 201MiB 100% + +# Set up loop device for the image +LOOP_DEV=$(losetup -f) +losetup -P $LOOP_DEV /output/sparkos.img + +# Wait for partition devices +sleep 1 + +# Format partitions +echo "Formatting EFI System Partition (FAT32)..." +mkfs.vfat -F 32 -n "SPARKOSEFI" ${LOOP_DEV}p1 + +echo "Formatting root partition (ext4)..." +mkfs.ext4 -L "SparkOS" ${LOOP_DEV}p2 + +# Mount ESP +echo "Mounting partitions..." +mount ${LOOP_DEV}p1 /mnt/esp +mount ${LOOP_DEV}p2 /mnt/root + +# Install GRUB to ESP +echo "Installing GRUB bootloader..." +mkdir -p /mnt/esp/EFI/BOOT + +# Create GRUB EFI binary using grub-mkstandalone +grub-mkstandalone \ + --format=x86_64-efi \ + --output=/mnt/esp/EFI/BOOT/BOOTX64.EFI \ + --locales="" \ + --fonts="" \ + "boot/grub/grub.cfg=/dev/null" + +# Find the kernel +KERNEL_PATH=$(find /kernel/boot -name "vmlinuz-*" | head -1) +KERNEL_VERSION=$(basename $KERNEL_PATH | sed 's/vmlinuz-//') +INITRD_PATH=$(find /kernel/boot -name "initrd.img-*" | head -1) + +# Copy kernel and initrd to ESP +echo "Installing kernel..." +mkdir -p /mnt/esp/boot +cp $KERNEL_PATH /mnt/esp/boot/vmlinuz +if [ -f "$INITRD_PATH" ]; then cp $INITRD_PATH /mnt/esp/boot/initrd.img; fi + +# Create GRUB configuration +mkdir -p /mnt/esp/boot/grub +printf '%s\n' \ + 'set timeout=3' \ + 'set default=0' \ + '' \ + 'menuentry "SparkOS" {' \ + ' linux /boot/vmlinuz root=LABEL=SparkOS rw init=/sbin/init console=tty1 quiet' \ + '}' \ + > /mnt/esp/boot/grub/grub.cfg + +# Set up root filesystem +echo "Setting up root filesystem..." +mkdir -p /mnt/root/{bin,sbin,etc,proc,sys,dev,tmp,usr/{bin,sbin,lib,lib64},var/{log,run},root,home/spark,boot} + +# Install SparkOS init +cp /build/init /mnt/root/sbin/init +chmod 755 /mnt/root/sbin/init + +# Install busybox +echo "Installing busybox..." +cp /bin/busybox /mnt/root/bin/busybox +chmod 755 /mnt/root/bin/busybox + +# Create busybox symlinks for essential commands +for cmd in sh ls cat echo mount umount mkdir rm cp mv chmod chown ln ps kill; do + ln -sf busybox /mnt/root/bin/$cmd +done + +# Create system configuration files +echo "sparkos" > /mnt/root/etc/hostname +echo "127.0.0.1 localhost" > /mnt/root/etc/hosts +echo "127.0.1.1 sparkos" >> /mnt/root/etc/hosts +echo "root:x:0:0:root:/root:/bin/sh" > /mnt/root/etc/passwd +echo "spark:x:1000:1000:SparkOS User:/home/spark:/bin/sh" >> /mnt/root/etc/passwd +echo "root:x:0:" > /mnt/root/etc/group +echo "spark:x:1000:" >> /mnt/root/etc/group + +# Copy README to root partition +cp /build/config/image-readme.txt /mnt/root/README.txt + +# Sync and unmount +echo "Finalizing image..." +sync +umount /mnt/esp +umount /mnt/root +losetup -d $LOOP_DEV + +# Compress the image +echo "Compressing image..." +gzip -9 /output/sparkos.img +echo "UEFI-bootable image created: /output/sparkos.img.gz" diff --git a/scripts/docker-image-download-kernel.sh b/scripts/docker-image-download-kernel.sh new file mode 100755 index 0000000..c1a0310 --- /dev/null +++ b/scripts/docker-image-download-kernel.sh @@ -0,0 +1,10 @@ +#!/bin/bash +# Download a minimal Linux kernel for UEFI image + +set -e + +mkdir -p /kernel +apt-get update +apt-get download linux-image-generic +dpkg -x linux-image-*.deb /kernel +rm -rf /var/lib/apt/lists/* linux-image-*.deb diff --git a/scripts/docker-image-install-tools.sh b/scripts/docker-image-install-tools.sh new file mode 100755 index 0000000..f58c18a --- /dev/null +++ b/scripts/docker-image-install-tools.sh @@ -0,0 +1,20 @@ +#!/bin/bash +# Install required tools for building UEFI-bootable SparkOS image + +set -e + +apt-get update +apt-get install -y \ + gcc \ + make \ + dosfstools \ + mtools \ + e2fsprogs \ + parted \ + gdisk \ + grub-efi-amd64-bin \ + grub-common \ + wget \ + busybox-static \ + kmod +rm -rf /var/lib/apt/lists/* diff --git a/scripts/docker-install-packages.sh b/scripts/docker-install-packages.sh new file mode 100755 index 0000000..2bc58b1 --- /dev/null +++ b/scripts/docker-install-packages.sh @@ -0,0 +1,6 @@ +#!/bin/sh +# Install runtime packages for Alpine-based Docker image + +set -e + +apk add --no-cache file diff --git a/scripts/docker-setup-config.sh b/scripts/docker-setup-config.sh new file mode 100755 index 0000000..d3a1336 --- /dev/null +++ b/scripts/docker-setup-config.sh @@ -0,0 +1,19 @@ +#!/bin/sh +# Set up basic configuration files for SparkOS + +set -e + +# Create hostname +echo "sparkos" > /sparkos/rootfs/etc/hostname + +# Create hosts file +echo "127.0.0.1 localhost" > /sparkos/rootfs/etc/hosts +echo "127.0.1.1 sparkos" >> /sparkos/rootfs/etc/hosts + +# Create passwd file +echo "root:x:0:0:root:/root:/bin/sh" > /sparkos/rootfs/etc/passwd +echo "spark:x:1000:1000:SparkOS User:/home/spark:/bin/sh" >> /sparkos/rootfs/etc/passwd + +# Create group file +echo "root:x:0:" > /sparkos/rootfs/etc/group +echo "spark:x:1000:" >> /sparkos/rootfs/etc/group diff --git a/scripts/docker-setup-rootfs.sh b/scripts/docker-setup-rootfs.sh new file mode 100755 index 0000000..f86bffe --- /dev/null +++ b/scripts/docker-setup-rootfs.sh @@ -0,0 +1,25 @@ +#!/bin/sh +# Create minimal rootfs structure for SparkOS + +set -e + +# Create minimal rootfs structure +mkdir -p /sparkos/rootfs/bin \ + /sparkos/rootfs/sbin \ + /sparkos/rootfs/etc \ + /sparkos/rootfs/proc \ + /sparkos/rootfs/sys \ + /sparkos/rootfs/dev \ + /sparkos/rootfs/tmp \ + /sparkos/rootfs/usr/bin \ + /sparkos/rootfs/usr/sbin \ + /sparkos/rootfs/usr/lib \ + /sparkos/rootfs/var/log \ + /sparkos/rootfs/var/run \ + /sparkos/rootfs/root \ + /sparkos/rootfs/home/spark + +# Set proper permissions +chmod 1777 /sparkos/rootfs/tmp +chmod 700 /sparkos/rootfs/root +chmod 755 /sparkos/rootfs/home/spark