Skip to content

Commit

Permalink
refact(builder): refactor macos(arm64 & intel) builder (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
ihexon authored Dec 24, 2024
1 parent 1fdefae commit 80b0ded
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 93 deletions.
140 changes: 66 additions & 74 deletions target_builder/macos_amd64
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 \
Expand All @@ -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 \
Expand All @@ -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 \
Expand All @@ -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 !
}

Expand Down Expand Up @@ -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" [email protected] -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

Expand All @@ -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 [email protected] -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
53 changes: 34 additions & 19 deletions target_builder/macos_arm64
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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 \
Expand All @@ -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 \
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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" [email protected] -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

Expand All @@ -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 [email protected] -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

0 comments on commit 80b0ded

Please sign in to comment.