From 7801838648c44e309044b83b041f5becf2a19186 Mon Sep 17 00:00:00 2001 From: ihexon <14349453+ihexon@users.noreply.github.com> Date: Mon, 23 Dec 2024 09:09:31 +0000 Subject: [PATCH] fix(builder): fix boot failed in CI env --- layers/macos_arm64/etc/default/grub | 2 +- layers/macos_arm64/etc/inittab | 2 +- make | 2 +- subfunc/boot_raw_disk.sh | 25 +++++++++--- subfunc/install_qemu.sh | 2 +- subfunc/mkext4disk.sh | 59 ----------------------------- target_builder/macos_amd64 | 45 +++++++++++++++------- target_builder/macos_arm64 | 46 ++++++++++++++++++---- 8 files changed, 94 insertions(+), 89 deletions(-) delete mode 100755 subfunc/mkext4disk.sh diff --git a/layers/macos_arm64/etc/default/grub b/layers/macos_arm64/etc/default/grub index e23845b..3b62fa3 100644 --- a/layers/macos_arm64/etc/default/grub +++ b/layers/macos_arm64/etc/default/grub @@ -2,4 +2,4 @@ GRUB_TIMEOUT=0 GRUB_DISABLE_SUBMENU=y GRUB_DISABLE_RECOVERY=true GRUB_TERMINAL=console -GRUB_CMDLINE_LINUX_DEFAULT="console=VM_CONSOLE_DEVICE modules=sd-mod,usb-storage,ext4 quiet rootfstype=ext4" +GRUB_CMDLINE_LINUX_DEFAULT="console=ttyAMA0 modules=sd-mod,usb-storage,ext4 quiet rootfstype=ext4" diff --git a/layers/macos_arm64/etc/inittab b/layers/macos_arm64/etc/inittab index c3edf29..c91764a 100644 --- a/layers/macos_arm64/etc/inittab +++ b/layers/macos_arm64/etc/inittab @@ -22,4 +22,4 @@ tty6::respawn:/sbin/getty 38400 tty6 ::shutdown:/sbin/openrc shutdown # enable login on alternative console -VM_CONSOLE_DEVICE::respawn:/sbin/getty -L 0 VM_CONSOLE_DEVICE vt100 +ttyAMA0::respawn:/sbin/getty -L 0 ttyAMA0 vt100 diff --git a/make b/make index 25cceff..64732dc 100755 --- a/make +++ b/make @@ -50,7 +50,7 @@ parse_profile() { if [[ $SKIP_INSTALL_QEMU == "true" ]]; then echo "SKIP_INSTALL_QEMU set true, skip install qemu" else - bash +x ${workspace}/subfunc/install_qemu.sh || { + output=$output workspace=$workspace bash +x ${workspace}/subfunc/install_qemu.sh || { echo "Error: Install qemu failed" exit 100 } diff --git a/subfunc/boot_raw_disk.sh b/subfunc/boot_raw_disk.sh index dc8d013..7635964 100644 --- a/subfunc/boot_raw_disk.sh +++ b/subfunc/boot_raw_disk.sh @@ -42,29 +42,44 @@ check_envs() { boot_raw_arm64() { check_envs echo "INFO:Boot arm64 alpine disk" + set -x + "$output/qemu_bins/lib/$ld_loader" \ + --library-path "$output/qemu_bins/lib" \ + "$output/qemu_bins/bin/qemu-system-aarch64" -machine virt -cpu cortex-a72 -m 2048 \ + -nographic \ + -drive file="$raw_disk,format=raw,if=virtio" \ + -bios "$output/qemu_bins/share/qemu/edk2-aarch64-code.fd" \ + -netdev user,id=net0,restrict=n,hostfwd=tcp:127.0.0.1:10025-:22 \ + -device e1000,netdev=net0 -device virtio-balloon-pci,id=balloon0 + set +x } # Args1: bootable.img path boot_raw_x86_64() { check_envs echo "INFO:Boot $raw_disk" - mycmd="./qemu_bins/lib/$ld_loader --library-path ./qemu_bins/lib \ - ./qemu_bins/bin/qemu-system-x86_64 \ + set -x + "$output/qemu_bins/lib/$ld_loader" \ + --library-path "$output/qemu_bins/lib" \ + "$output/qemu_bins/bin/qemu-system-x86_64" \ -nographic -cpu max -smp 4 -m 2G \ -netdev user,id=net0,restrict=n,hostfwd=tcp:127.0.0.1:10025-:22 \ -device e1000,netdev=net0 \ -device virtio-balloon-pci,id=balloon0 \ - -drive file=$raw_disk,format=raw,if=virtio - " - echo "Run: $mycmd" + -drive file="$raw_disk,format=raw,if=virtio" + set -x } boot_raw_disk() { check_envs if [[ "$target_arch" == arm64 ]] || [[ "$target_arch" == aarch64 ]]; then + set -x boot_raw_arm64 "$raw_disk" + set +x elif [[ "$target_arch" == x86_64 ]] || [[ "$target_arch" == amd64 ]]; then + set -x boot_raw_x86_64 "$raw_disk" + set +x fi } diff --git a/subfunc/install_qemu.sh b/subfunc/install_qemu.sh index 3712d37..fff039f 100644 --- a/subfunc/install_qemu.sh +++ b/subfunc/install_qemu.sh @@ -47,8 +47,8 @@ download_qemu() { } test_qemu_bin() { - cd "$output" set -xe + cd "$output" ./qemu_bins/lib/$ld_loader --library-path ./qemu_bins/lib ./qemu_bins/bin/qemu-system-x86_64 --version >/dev/null ./qemu_bins/lib/$ld_loader --library-path ./qemu_bins/lib ./qemu_bins/bin/qemu-system-aarch64 --version >/dev/null ./qemu_bins/static_qemu/bin/qemu-x86_64 --version >/dev/null diff --git a/subfunc/mkext4disk.sh b/subfunc/mkext4disk.sh deleted file mode 100755 index ecaad0d..0000000 --- a/subfunc/mkext4disk.sh +++ /dev/null @@ -1,59 +0,0 @@ -#! /usr/bin/env bash - -make_exit_disk() { - if [[ -n $disk_size ]]; then - # Do nothing - echo -n "" - else - echo 'Error: env $disk_size empty' - exit 100 - fi - if [[ -n $disk_format ]]; then - echo -n "" - else - echo 'Error: env $disk_format empty' - exit 100 - fi - if [[ -n $disk_uuid ]]; then - echo -n "" - else - echo 'Error: env $disk_uuid empty' - exit 100 - fi - if [[ -n $disk_name ]]; then - echo -n "" - else - echo 'Error: env $disk_name empty' - exit 100 - fi - disk_name="ovm_disk.raw" - echo "Make a $output/$disk_name,size:$disk_size,format:$disk_format,uuid:$disk_uuid" - set -x - truncate -s 2048M "$output/$disk_name" - sudo mkfs.ext4 -U 8a3219d0-4002-4cd9-8cb1-f3ffe52451f1 -F ovm_disk.raw - set +x -} - -copy_rootfs_into_disk(){ - ovm_disk_mount_point=/tmp/ovm_disk_mount_point - set -x - mkdir -p $ovm_disk_mount_point - sudo -E mount $output/$disk_name $ovm_disk_mount_point - sudo -E tar -xvf $rootfs_archive -C $ovm_disk_mount_point - sync - umount $ovm_disk_mount_point - set +x -} - -main() { - if [[ -n $output ]]; then - echo -n "" - else - echo 'Error: env $output empty' - exit 100 - fi - make_exit_disk - copy_rootfs_into_disk -} - -main diff --git a/target_builder/macos_amd64 b/target_builder/macos_amd64 index a59aa47..8871e86 100644 --- a/target_builder/macos_amd64 +++ b/target_builder/macos_amd64 @@ -190,27 +190,33 @@ copy_layer() { sync # must do sync ! } -pack_rootfs() { - bootable_file="${bootable_file_zst%.*}" - sha1sum_file="${bootable_file_zst}.sha1sum" - set -x - +umount_rootfs() { cd $output && { umount -R ./target_rootfs } || { echo "Error: failed to umount $output/target_rootfs" exit 100 - } && cd $workspace + } + cd $workspace || { + echo "Change dir to workspace failed" + exit 100 + } +} +pack_rootfs() { cd $output && { - zstd --force $bootable_file && { - sha1sum "${bootable_file_zst}" >"${sha1sum_file}" + zstd --force alpine_uefi_bootable.img && { + sha1sum alpine_uefi_bootable.img.zst >alpine_uefi_bootable.img.zst.sha1sum } || { echo "Error: Compress alpine_uefi_bootable.img failed" exit 100 } - } && cd $workspace - set +x + } + + cd "$workspace" || { + echo "Change dir to workspace failed" + exit 100 + } } bootimage() { @@ -230,11 +236,22 @@ if [[ -z $workspace ]] && [[ -z $output ]]; then exit 100 fi -echo workspace: $workspace -echo output: $output +echo "workspace: $workspace" +echo "output: $output" clean_mapping bootstrap_alpine #copy_layer -pack_rootfs +umount_rootfs clean_mapping -bootimage +bootimage >/tmp/vm_boot_log 2>&1 & +while true; do + ssh -o ConnectTimeout=1 -o StrictHostKeyChecking=no -i /tmp/ovm_ssh/ovm_id root@127.0.0.1 -p10025 "update-grub && sync && halt" && { + echo Update-grub successful + break + } || { + echo "Try ssh into vm again..." + sleep 2 + continue + } +done +pack_rootfs diff --git a/target_builder/macos_arm64 b/target_builder/macos_arm64 index 700cfc4..0635dc7 100644 --- a/target_builder/macos_arm64 +++ b/target_builder/macos_arm64 @@ -162,15 +162,20 @@ copy_layer() { sync # must do sync ! } -pack_rootfs() { - set -x +umount_rootfs() { cd $output && { umount -R ./target_rootfs } || { echo "Error: failed to umount $output/target_rootfs" exit 100 } + cd $workspace || { + echo "Change dir to workspace failed" + exit 100 + } +} +pack_rootfs() { cd $output && { zstd --force alpine_uefi_bootable.img && { sha1sum alpine_uefi_bootable.img.zst >alpine_uefi_bootable.img.zst.sha1sum @@ -179,23 +184,50 @@ pack_rootfs() { exit 100 } } - set +x - cd $workspace || { + + cd "$workspace" || { echo "Change dir to workspace failed" exit 100 } } +bootimage() { + cd "$workspace" || { + echo "Failed to change dir to workspace" + exit 100 + } + + bootable_file=alpine_uefi_bootable.img + output=$output workspace=$workspace bash +x ./subfunc/boot_raw_disk.sh "$output/$bootable_file" arm64 || { + echo "Error: boot $output/$bootable_file failed" + exit 100 + } +} + echo "=== Bootstrap Alpine ===" if [[ -z $workspace ]] && [[ -z $output ]]; then echo 'Error: $workspace or $output empty' exit 100 fi -echo workspace: $workspace -echo output: $output +echo "workspace: $workspace" +echo "output: $output" clean_mapping bootstrap_alpine copy_layer -pack_rootfs +umount_rootfs clean_mapping + +# Final stage: boot raw disk and do update-grub +bootimage >/tmp/vm_boot_log 2>&1 & +while true; do + ssh -o ConnectTimeout=1 -o StrictHostKeyChecking=no -i /tmp/ovm_ssh/ovm_id root@127.0.0.1 -p10025 "update-grub && sync && halt" && { + echo Update-grub successful + break + } || { + echo "Try ssh into vm again..." + sleep 2 + continue + } +done +pack_rootfs