Quellcode durchsuchen

script: refactor the build image script

Rearrange code to pass args instead of by envs, no functionality
changes.

Signed-off-by: greatbridf <greatbridf@icloud.com>
greatbridf vor 1 Woche
Ursprung
Commit
3842078d05
3 geänderte Dateien mit 127 neuen und 60 gelöschten Zeilen
  1. 2 2
      Makefile.src
  2. 0 58
      script/build-img.sh
  3. 125 0
      script/build-img/build-img

+ 2 - 2
Makefile.src

@@ -173,8 +173,8 @@ $(BINARY_DIR)/eonix_kernel: $(KERNEL_DEPS)
 build/kernel.sym: $(BINARY_DIR)/eonix_kernel
 	CARGO_TARGET_DIR=build cargo objcopy -q $(CARGO_FLAGS) -- --only-keep-debug build/kernel.sym
 
-build/fs-%.img: user-programs/init_script_%.sh script/build-img.sh $(USER_PROGRAMS)
-	ARCH=$* OUTPUT=$@ sh script/build-img.sh
+build/fs-%.img: user-programs/init_script_%.sh script/build-img/build-img $(USER_PROGRAMS)
+	script/build-img/build-img -a $* -o $@
 
 build/mbr.bin: $(BINARY_DIR)/eonix_kernel
 	CARGO_TARGET_DIR=build cargo objcopy -q $(CARGO_FLAGS) -- -O binary -j .mbr build/mbr.bin

+ 0 - 58
script/build-img.sh

@@ -1,58 +0,0 @@
-#!/bin/sh
-
-OS=`uname -s`
-
-if sudo --version > /dev/null 2>&1; then
-    SUDO=sudo
-fi
-
-if [ "$OUTPUT" = "" ]; then
-    OUTPUT="build/fs-$ARCH.img"
-fi
-
-if [ "$ARCH" = "" ]; then
-    echo "ARCH is not set, exiting..." >&2
-    exit 1
-fi
-
-dd if=/dev/zero of="$OUTPUT" bs=`expr 1024 \* 1024` count=1020
-mkfs.fat -n SYSTEM "$OUTPUT"
-
-if [ "$OS" = "Darwin" ]; then
-    SUDO=''
-    hdiutil detach build/mnt > /dev/null 2>&1 || true
-    hdiutil attach "$OUTPUT" -mountpoint build/mnt
-else
-    mkdir -p build/mnt
-    $SUDO losetup -P /dev/loop2 "$OUTPUT"
-    $SUDO mount /dev/loop2 build/mnt
-fi
-
-if [ "$ARCH" = "x86_64" ]; then
-    $SUDO cp ./user-programs/init.out build/mnt/init
-    $SUDO cp ./user-programs/int.out build/mnt/int
-    $SUDO cp ./user-programs/dynamic_test build/mnt/dynamic_test
-    $SUDO cp ./user-programs/busybox build/mnt/busybox
-    $SUDO cp ./user-programs/busybox-minimal build/mnt/busybox_
-    $SUDO cp ./user-programs/ld-musl-i386.so.1 build/mnt/ld-musl-i386.so.1
-    $SUDO cp ./user-programs/pthread_test build/mnt/pthread_test
-    $SUDO cp ./user-programs/init_script_x86_64.sh build/mnt/initsh
-elif [ "$ARCH" = "riscv64" ]; then
-    $SUDO cp ./user-programs/busybox.static build/mnt/busybox
-    $SUDO cp ./user-programs/init_script_riscv64.sh build/mnt/initsh
-elif [ "$ARCH" = "loongarch64" ]; then
-    $SUDO cp ./user-programs/busybox.la64 build/mnt/busybox
-    $SUDO cp ./user-programs/init_script_loongarch64.sh build/mnt/initsh
-fi
-
-# Add your custom files here
-
-
-# End of custom files
-
-if [ "$OS" = "Darwin" ]; then
-    hdiutil detach build/mnt
-else
-    $SUDO losetup -d /dev/loop2
-    $SUDO umount build/mnt
-fi

+ 125 - 0
script/build-img/build-img

@@ -0,0 +1,125 @@
+#!/bin/sh
+
+KB=1024
+MB=$((1024 * KB))
+
+OS=$(uname -s)
+SUDO=$(which sudo || :)
+
+ARCH=
+IMAGE=
+MOUNTPOINT="$(mktemp -d)"
+
+die() {
+    echo "error: $1" >&2
+    exit 1
+}
+
+usage() {
+    cat >&2 <<EOF
+Usage: $0 -a <ARCH> -o <OUTPUT> [-h]
+
+Options
+    -a          The arch building image for [available: riscv64 x86 loongarch64]
+    -o          Output image path
+    -h          Show help message
+EOF
+
+    exit "$1"
+}
+
+sudo() {
+    "$SUDO" "$@"
+}
+
+ensure_mount_macos() {
+    hdiutil detach "$MOUNTPOINT" > /dev/null 2>&1 || :
+    hdiutil attach "$IMAGE" -mountpoint "$MOUNTPOINT"
+}
+
+ensure_mount_linux() {
+    DEV=$(sudo losetup -f "$IMAGE" --show)
+    sudo mount "$DEV" "$MOUNTPOINT"
+}
+
+ensure_mount() {
+    if [ "$OS" = Darwin ]; then
+        ensure_mount_macos
+        return
+    fi
+
+    ensure_mount_linux
+}
+
+copy_to_image() {
+    _prefix=sudo
+    [ "$OS" = Darwin ] && _prefix=
+
+    $_prefix cp "$1" "$MOUNTPOINT/$2"
+}
+
+unmount_macos() {
+    hdiutil detach "$MOUNTPOINT"
+}
+
+unmount_linux() {
+    sudo umount "$MOUNTPOINT"
+    sudo losetup -d "$DEV"
+}
+
+cleanup() {
+    case "$OS" in
+        Darwin)
+            unmount_macos
+            ;;
+        *)
+            unmount_linux
+            ;;
+    esac
+}
+
+set -eu
+
+while getopts "a:o:h" opt; do
+    case "$opt" in
+        a) ARCH="$OPTARG";;
+        o) IMAGE="$OPTARG";;
+        h) usage 0;;
+        ?) usage 1;;
+    esac
+done
+
+shift $((OPTIND - 1))
+
+[ -z "$ARCH" ] && die "ARCH is not set"
+[ -z "$IMAGE" ] && die "output image path is not set"
+
+echo "Build image with ARCH=$ARCH OUTPUT=$IMAGE MOUNTPOINT=$MOUNTPOINT"
+
+dd if=/dev/zero of="$IMAGE" bs=$((1 * MB)) count=$((1024 - 4))
+mkfs.fat -n SYSTEM "$IMAGE"
+
+ensure_mount
+trap cleanup EXIT
+
+if [ "$ARCH" = "x86_64" ]; then
+    copy_to_image ./user-programs/init.out init
+    copy_to_image ./user-programs/int.out int
+    copy_to_image ./user-programs/dynamic_test dynamic_test
+    copy_to_image ./user-programs/busybox busybox
+    copy_to_image ./user-programs/busybox-minimal busybox_
+    copy_to_image ./user-programs/ld-musl-i386.so.1 ld-musl-i386.so.1
+    copy_to_image ./user-programs/pthread_test pthread_test
+    copy_to_image ./user-programs/init_script_x86_64.sh initsh
+elif [ "$ARCH" = "riscv64" ]; then
+    copy_to_image ./user-programs/busybox.static busybox
+    copy_to_image ./user-programs/init_script_riscv64.sh initsh
+elif [ "$ARCH" = "loongarch64" ]; then
+    copy_to_image ./user-programs/busybox.la64 busybox
+    copy_to_image ./user-programs/init_script_loongarch64.sh initsh
+fi
+
+# Add your custom files here
+
+
+# End of custom files