From 80b0ded7faeeb48a81f7a8f15b4797047028d50c Mon Sep 17 00:00:00 2001 From: CAT <14349453+ihexon@users.noreply.github.com> Date: Mon, 23 Dec 2024 18:43:02 -0800 Subject: [PATCH] refact(builder): refactor macos(arm64 & intel) builder (#48) --- target_builder/macos_amd64 | 140 +++++++++++++++++-------------------- target_builder/macos_arm64 | 53 +++++++++----- 2 files changed, 100 insertions(+), 93 deletions(-) diff --git a/target_builder/macos_amd64 b/target_builder/macos_amd64 index 5811e4f..09f81df 100644 --- a/target_builder/macos_amd64 +++ b/target_builder/macos_amd64 @@ -21,7 +21,6 @@ preinstalled_packages=" zstd tzdata " -rootfs_url="" bootable_url="https://github.com/oomol/builded/releases/download/v1.5/alpine_uefi_bootable-x86_64.img.zst" sha1sum="fc62a59289a3f4721bb7d44730ca5a631c592531" # the alpine_uefi_bootable.img.zst has ROOTFS and EFI partition @@ -42,38 +41,20 @@ clean_mapping() { bootstrap_alpine() { bootable_file="${bootable_file_zst%.*}" - sha1sum_file="${bootable_file_zst}.sha1sum" - set -x + set -ex sudo -E rm -rf "$output/${bootable_file_zst}" wget -c "$bootable_url" --output-document="$output/${bootable_file_zst}" --output-file=/tmp/log_download_alpine_uefi_bootable.img.txt - set +x - - echo "$sha1sum ${bootable_file_zst}" >"$output/${sha1sum_file}" - cd $output && sha1sum -c "${sha1sum_file}" || { - echo "Error: checksha1sum failed" - exit 100 - } && cd $workspace - cd $output && { - # Decompress alpine_uefi_bootable.img.zst - zstd -d -f "${bootable_file_zst}" || { - echo "Decompress ${bootable_file_zst} failed" - exit 100 - } - # Mapping the part into /dev/mapper/loopNpN - kpartx -av ${bootable_file} && - mount -m -U $rootfs_part_uuid ./target_rootfs && - mount -m -U $efi_part_uuid ./target_rootfs/boot || { - echo "Error: Mount alpine_uefi_bootable.img failed" - exit 100 - } - } && cd $workspace + cd $output + zstd -d -f "${bootable_file_zst}" + # Mapping the part into /dev/mapper/loopNpN + kpartx -av ${bootable_file} + mount -m -U $rootfs_part_uuid ./target_rootfs + mount -m -U $efi_part_uuid ./target_rootfs/boot - cd $output && { - # Podman live in http://dl-cdn.alpinelinux.org/alpine/v3.20/community - echo http://dl-cdn.alpinelinux.org/alpine/v3.20/community >>target_rootfs/etc/apk/repositories - } && cd $workspace + echo http://dl-cdn.alpinelinux.org/alpine/v3.20/community >>"$output/target_rootfs/etc/apk/repositories" + set +ex # set qemu_flag if CROSS_BUILD if [[ $CROSS_BUILD == true ]]; then @@ -84,50 +65,46 @@ bootstrap_alpine() { # Install required package into rootfs pkgs=$(echo $preinstalled_packages | xargs) - set -x - cd $output && { - sudo -E proot $qemu_flag --rootfs=./target_rootfs \ - -b /dev:/dev \ - -b /sys:/sys \ - -b /proc:/proc \ - -b /etc/resolv.conf:/etc/resolv.conf \ - -w /root \ - -0 /bin/su -c "apk update;apk add $pkgs" || { - echo "Error: proot into rootfs failed" - exit 100 - } - } && cd $workspace - set +x + set -xe + cd $output + sudo -E proot $qemu_flag --rootfs=./target_rootfs \ + -b /dev:/dev \ + -b /sys:/sys \ + -b /proc:/proc \ + -b /etc/resolv.conf:/etc/resolv.conf \ + -w /root \ + -0 /bin/su -c "apk update;apk add $pkgs" + set +xe echo "Generat ssh keys and copy into rootfs" - set -x - cd $output && { - sudo -E proot $qemu_flag --rootfs=./target_rootfs \ - -b /dev:/dev \ - -b /sys:/sys \ - -b /proc:/proc \ - -b /etc/resolv.conf:/etc/resolv.conf \ - -w /root \ - -0 /bin/su -c 'rm -rf /root/.ssh/ovm_id && ssh-keygen -f /root/.ssh/ovm_id -N "" && cat /root/.ssh/ovm_id.pub >> /root/.ssh/authorized_keys' || { - echo "Error: proot into rootfs failed" - exit 100 - } - } && cd $workspace - + set -ex + cd $output + sudo -E proot $qemu_flag --rootfs=./target_rootfs \ + -b /dev:/dev \ + -b /sys:/sys \ + -b /proc:/proc \ + -b /etc/resolv.conf:/etc/resolv.conf \ + -w /root \ + -0 /bin/su -c 'rm -rf /root/.ssh/ovm_id && ssh-keygen -f /root/.ssh/ovm_id -N "" && cat /root/.ssh/ovm_id.pub >> /root/.ssh/authorized_keys' sudo -E rm -rf /tmp/ovm_ssh sudo -E cp -rf $output/target_rootfs/root/.ssh /tmp/ovm_ssh - set +x + set +xe sync } copy_layer() { + if [[ -z ${target_profile} ]];then + echo "Error: env target_profile empty" + exit 100 + fi echo "INFO: Copy $target_profile layers files" export VM_CONSOLE_DEVICE=hvc0 + + set -x cd $workspace || { echo 'Error: change dir to $workspace failed' exit 100 } - set -x cd ./layers/$target_profile && { if [[ ${VM_PROVIDER} == "qemu" ]]; then find ./ -type f -exec sed -i 's/VM_CONSOLE_DEVICE/ttyAMA0/g' {} + # Replace VM_CONSOLE_DEVICE to ttyAMA0 @@ -141,7 +118,7 @@ copy_layer() { } || { echo 'Error: change dir to ./layers/$target_profile failed' exit 100 - } && cd $workspace + } cd $output && { sudo -E proot $qemu_flag --rootfs=./target_rootfs \ @@ -158,7 +135,7 @@ copy_layer() { echo "Error: add ovmservice into default stage failed" exit 100 } - } && cd $workspace + } cd $output && { sudo -E proot $qemu_flag --rootfs=./target_rootfs \ @@ -171,7 +148,7 @@ copy_layer() { echo "Error: enable rc_parallel failed" exit 100 } - } && cd $workspace + } cd $output && { sudo -E proot $qemu_flag --rootfs=./target_rootfs \ @@ -184,9 +161,13 @@ copy_layer() { echo "Error: enable rc_parallel failed" exit 100 } - } && cd $workspace + } set +x + cd $workspace || { + echo "Error: failed to change dir to workspace" + exit 100 + } sync # must do sync ! } @@ -224,15 +205,32 @@ bootimage() { echo "Failed to change dir to workspace" exit 100 } - bash +x ./subfunc/boot_raw_disk.sh "$output/$bootable_file" x86_64 || { + + output=$output workspace=$workspace bash +x ./subfunc/boot_raw_disk.sh "$output/$bootable_file" x86_64 || { echo "Error: boot $output/$bootable_file failed" exit 100 } } +try_ssh_into_vm() { + while true; do + ssh -o ConnectTimeout=1 \ + -o StrictHostKeyChecking=no \ + -i "/tmp/ovm_ssh/ovm_id" root@127.0.0.1 -p 10025 "update-grub && sync && halt" && { + echo "Update-grub successful" + break + } || { + echo "Try ssh into vm again..." + sleep 2 + continue + } + done + +} + echo "=== Bootstrap Alpine ===" if [[ -z $workspace ]] && [[ -z $output ]]; then - echo 'Error: $workspace or $output empty' + echo 'Error: env workspace or output empty' exit 100 fi @@ -243,16 +241,10 @@ bootstrap_alpine #copy_layer umount_rootfs clean_mapping + +# Final stage: boot raw disk and do update-grub bootimage >/tmp/vm_boot_log 2>&1 & tail -f /tmp/vm_boot_log & -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 +try_ssh_into_vm pack_rootfs +exit 0 diff --git a/target_builder/macos_arm64 b/target_builder/macos_arm64 index 37dca5c..eb87358 100644 --- a/target_builder/macos_arm64 +++ b/target_builder/macos_arm64 @@ -25,9 +25,7 @@ bootable_url="https://github.com/oomol/builded/releases/download/v1.1/alpine_uef # the alpine_uefi_bootable.img.zst has ROOTFS and EFI partition rootfs_part_uuid="d1e46927-eb5d-4fbf-986a-7e5581f37a16" efi_part_uuid="6729-82AB" - -# file_name:sha1sum -sha1sum="cceb5153fa0bc45dc921494af35e4d61543dfbe5 alpine_uefi_bootable.img.zst" +bootable_file_zst="alpine_uefi_bootable.img.zst" clean_mapping() { umount -R $output/target_rootfs >/dev/null 2>&1 @@ -41,16 +39,19 @@ clean_mapping() { } bootstrap_alpine() { + bootable_file="${bootable_file_zst%.*}" + set -ex - sudo -E rm -rf "$output/alpine_uefi_bootable.img.zst" - wget -c "$bootable_url" --output-document="$output/alpine_uefi_bootable.img.zst" --output-file=/tmp/log_download_alpine_uefi_bootable.img.txt + sudo -E rm -rf "$output/${bootable_file_zst}" + wget -c "$bootable_url" --output-document="$output/${bootable_file_zst}" --output-file=/tmp/log_download_alpine_uefi_bootable.img.txt cd $output - zstd -d -f alpine_uefi_bootable.img.zst + zstd -d -f "${bootable_file_zst}" # Mapping the part into /dev/mapper/loopNpN - kpartx -av alpine_uefi_bootable.img + kpartx -av ${bootable_file} mount -m -U $rootfs_part_uuid ./target_rootfs mount -m -U $efi_part_uuid ./target_rootfs/boot/efi + echo http://dl-cdn.alpinelinux.org/alpine/v3.20/community >>"$output/target_rootfs/etc/apk/repositories" set +ex @@ -64,6 +65,7 @@ bootstrap_alpine() { # Install required package into rootfs pkgs=$(echo $preinstalled_packages | xargs) set -xe + cd $output sudo -E proot $qemu_flag --rootfs=./target_rootfs \ -b /dev:/dev \ -b /sys:/sys \ @@ -75,6 +77,7 @@ bootstrap_alpine() { echo "Generat ssh keys and copy into rootfs" set -ex + cd $output sudo -E proot $qemu_flag --rootfs=./target_rootfs \ -b /dev:/dev \ -b /sys:/sys \ @@ -88,6 +91,10 @@ bootstrap_alpine() { sync } copy_layer() { + if [[ -z ${target_profile} ]];then + echo "Error: env target_profile empty" + exit 100 + fi echo "INFO: Copy $target_profile layers files" export VM_CONSOLE_DEVICE=hvc0 @@ -96,6 +103,7 @@ copy_layer() { echo 'Error: change dir to $workspace failed' exit 100 } + cd ./layers/$target_profile && { if [[ ${VM_PROVIDER} == "qemu" ]]; then find ./ -type f -exec sed -i 's/VM_CONSOLE_DEVICE/ttyAMA0/g' {} + # Replace VM_CONSOLE_DEVICE to ttyAMA0 @@ -197,16 +205,31 @@ bootimage() { 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 } } +try_ssh_into_vm() { + while true; do + ssh -o ConnectTimeout=1 \ + -o StrictHostKeyChecking=no \ + -i "/tmp/ovm_ssh/ovm_id" root@127.0.0.1 -p 10025 "update-grub && sync && halt" && { + echo "Update-grub successful" + break + } || { + echo "Try ssh into vm again..." + sleep 2 + continue + } + done + +} + echo "=== Bootstrap Alpine ===" if [[ -z $workspace ]] && [[ -z $output ]]; then - echo 'Error: $workspace or $output empty' + echo 'Error: env workspace or output empty' exit 100 fi @@ -221,14 +244,6 @@ clean_mapping # Final stage: boot raw disk and do update-grub bootimage >/tmp/vm_boot_log 2>&1 & tail -f /tmp/vm_boot_log & -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 +try_ssh_into_vm pack_rootfs +exit 0