From d31aa8ec6803e3360db30a661f23a333cab9f22a Mon Sep 17 00:00:00 2001 From: CAT <14349453+ihexon@users.noreply.github.com> Date: Sun, 5 Jan 2025 22:11:28 -0800 Subject: [PATCH] chore(timesync): sched time sync every 15 min (#50) - chore(timesync): sched time sync every 15 min - chore(refact): shellcheck lint --------- Signed-off-by: ihexon <14349453+ihexon@users.noreply.github.com> --- .../etc/periodic/15min/timeschedsync | 22 +++++++ .../etc/periodic/15min/timeschedsync | 22 +++++++ target_builder/macos_amd64 | 58 +++++++------------ target_builder/macos_arm64 | 51 ++++++---------- 4 files changed, 82 insertions(+), 71 deletions(-) create mode 100644 layers/macos_amd64/etc/periodic/15min/timeschedsync create mode 100644 layers/macos_arm64/etc/periodic/15min/timeschedsync diff --git a/layers/macos_amd64/etc/periodic/15min/timeschedsync b/layers/macos_amd64/etc/periodic/15min/timeschedsync new file mode 100644 index 0000000..0e25dc8 --- /dev/null +++ b/layers/macos_amd64/etc/periodic/15min/timeschedsync @@ -0,0 +1,22 @@ +#! /usr/bin/env bash +log(){ + msg="$*" + echo "$msg" >> /dev/shm/timesync.log +} + +restart_ntp_client() { + ntp_client_service="/etc/init.d/ntpd" + if [[ -f "$ntp_client_service" ]]; then + $ntp_client_service restart + else + log "/etc/init.d/ntpd not exist, skip" + exit 127 + fi +} + +main() { + log "sync time now" + restart_ntp_client +} + +main diff --git a/layers/macos_arm64/etc/periodic/15min/timeschedsync b/layers/macos_arm64/etc/periodic/15min/timeschedsync new file mode 100644 index 0000000..0e25dc8 --- /dev/null +++ b/layers/macos_arm64/etc/periodic/15min/timeschedsync @@ -0,0 +1,22 @@ +#! /usr/bin/env bash +log(){ + msg="$*" + echo "$msg" >> /dev/shm/timesync.log +} + +restart_ntp_client() { + ntp_client_service="/etc/init.d/ntpd" + if [[ -f "$ntp_client_service" ]]; then + $ntp_client_service restart + else + log "/etc/init.d/ntpd not exist, skip" + exit 127 + fi +} + +main() { + log "sync time now" + restart_ntp_client +} + +main diff --git a/target_builder/macos_amd64 b/target_builder/macos_amd64 index bfdaf03..a514730 100644 --- a/target_builder/macos_amd64 +++ b/target_builder/macos_amd64 @@ -28,7 +28,7 @@ efi_part_uuid="F41A-23A6" bootable_file_zst="alpine_uefi_bootable.img.zst" clean_mapping() { - umount -R $output/target_rootfs >/dev/null 2>&1 + umount -R "$output/target_rootfs" >/dev/null 2>&1 # Clean kpartx mapping devices sudo -E losetup -a | grep alpine_uefi_bootable.img | cut -d ':' -f1 | while read -r item; do sudo -E kpartx -dv $item; done && { sudo -E losetup -D @@ -45,10 +45,10 @@ bootstrap_alpine() { 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 + cd "$output" zstd -d -f "${bootable_file_zst}" # Mapping the part into /dev/mapper/loopNpN - kpartx -av ${bootable_file} + kpartx -av "${bootable_file}" mount -m -U $rootfs_part_uuid ./target_rootfs mount -m -U $efi_part_uuid ./target_rootfs/boot/efi @@ -59,13 +59,13 @@ bootstrap_alpine() { if [[ $CROSS_BUILD == true ]]; then qemu_flag="--qemu=$output/qemu_bins/static_qemu/bin/qemu-x86_64" else - qemu_flag='' + qemu_flag= fi # Install required package into rootfs - pkgs=$(echo $preinstalled_packages | xargs) + pkgs=$(echo "$preinstalled_packages" | xargs) set -xe - cd $output + cd "$output" sudo -E proot $qemu_flag --rootfs=./target_rootfs \ -b /dev:/dev \ -b /sys:/sys \ @@ -77,7 +77,7 @@ bootstrap_alpine() { echo "Generat ssh keys and copy into rootfs" set -ex - cd $output + cd "$output" sudo -E proot $qemu_flag --rootfs=./target_rootfs \ -b /dev:/dev \ -b /sys:/sys \ @@ -86,32 +86,33 @@ bootstrap_alpine() { -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 + sudo -E cp -rf "$output/target_rootfs/root/.ssh" /tmp/ovm_ssh set +xe sync } copy_layer() { - if [[ -z ${target_profile} ]];then + 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 || { + cd "$workspace" || { echo 'Error: change dir to $workspace failed' exit 100 } - cd ./layers/$target_profile && { + 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 else find ./ -type f -exec sed -i 's/VM_CONSOLE_DEVICE/hvc0/g' {} + # Replace VM_CONSOLE_DEVICE to hvc0 fi sudo -E cp -rf ./* "$output/target_rootfs" || { - echo "Copy layer into $output/$target_rootfs failed" + echo "Copy layer into $output/target_rootfs failed" exit 100 } } || { @@ -119,7 +120,7 @@ copy_layer() { exit 100 } - cd $output && { + cd "$output" && { sudo -E proot $qemu_flag --rootfs=./target_rootfs \ -b /dev:/dev \ -b /sys:/sys \ @@ -136,7 +137,7 @@ copy_layer() { } } - cd $output && { + cd "$output" && { sudo -E proot $qemu_flag --rootfs=./target_rootfs \ -b /dev:/dev \ -b /sys:/sys \ @@ -149,7 +150,7 @@ copy_layer() { } } - cd $output && { + cd "$output" && { sudo -E proot $qemu_flag --rootfs=./target_rootfs \ -b /dev:/dev \ -b /sys:/sys \ @@ -163,7 +164,7 @@ copy_layer() { } set +x - cd $workspace || { + cd "$workspace" || { echo "Error: failed to change dir to workspace" exit 100 } @@ -171,20 +172,20 @@ copy_layer() { } umount_rootfs() { - cd $output && { + 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 && { + cd "$output" && { zstd --force alpine_uefi_bootable.img && { sha1sum alpine_uefi_bootable.img.zst >alpine_uefi_bootable.img.zst.sha1sum } || { @@ -199,25 +200,8 @@ pack_rootfs() { } } - -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 +if [[ -z "$workspace" ]] && [[ -z "$output" ]]; then echo 'Error: env workspace or output empty' exit 100 fi diff --git a/target_builder/macos_arm64 b/target_builder/macos_arm64 index 4caa07e..b9a3633 100644 --- a/target_builder/macos_arm64 +++ b/target_builder/macos_arm64 @@ -45,10 +45,10 @@ bootstrap_alpine() { 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 + cd "$output" zstd -d -f "${bootable_file_zst}" # Mapping the part into /dev/mapper/loopNpN - kpartx -av ${bootable_file} + kpartx -av "${bootable_file}" mount -m -U $rootfs_part_uuid ./target_rootfs mount -m -U $efi_part_uuid ./target_rootfs/boot/efi @@ -59,13 +59,13 @@ bootstrap_alpine() { if [[ $CROSS_BUILD == true ]]; then qemu_flag="--qemu=$output/qemu_bins/static_qemu/bin/qemu-aarch64" else - qemu_flag='' + qemu_flag= fi # Install required package into rootfs pkgs=$(echo $preinstalled_packages | xargs) set -xe - cd $output + cd "$output" sudo -E proot $qemu_flag --rootfs=./target_rootfs \ -b /dev:/dev \ -b /sys:/sys \ @@ -77,7 +77,7 @@ bootstrap_alpine() { echo "Generat ssh keys and copy into rootfs" set -ex - cd $output + cd "$output" sudo -E proot $qemu_flag --rootfs=./target_rootfs \ -b /dev:/dev \ -b /sys:/sys \ @@ -86,7 +86,7 @@ bootstrap_alpine() { -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 + sudo -E cp -rf "$output/target_rootfs/root/.ssh" /tmp/ovm_ssh set +xe sync } @@ -99,19 +99,19 @@ copy_layer() { export VM_CONSOLE_DEVICE=hvc0 set -x - cd $workspace || { + cd "$workspace" || { echo 'Error: change dir to $workspace failed' exit 100 } - cd ./layers/$target_profile && { + 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 else find ./ -type f -exec sed -i 's/VM_CONSOLE_DEVICE/hvc0/g' {} + # Replace VM_CONSOLE_DEVICE to hvc0 fi sudo -E cp -rf ./* "$output/target_rootfs" || { - echo "Copy layer into $output/$target_rootfs failed" + echo "Copy layer into $output/target_rootfs failed" exit 100 } } || { @@ -119,7 +119,7 @@ copy_layer() { exit 100 } - cd $output && { + cd "$output" && { sudo -E proot $qemu_flag --rootfs=./target_rootfs \ -b /dev:/dev \ -b /sys:/sys \ @@ -136,7 +136,7 @@ copy_layer() { } } - cd $output && { + cd "$output" && { sudo -E proot $qemu_flag --rootfs=./target_rootfs \ -b /dev:/dev \ -b /sys:/sys \ @@ -149,7 +149,7 @@ copy_layer() { } } - cd $output && { + cd "$output" && { sudo -E proot $qemu_flag --rootfs=./target_rootfs \ -b /dev:/dev \ -b /sys:/sys \ @@ -163,7 +163,7 @@ copy_layer() { } set +x - cd $workspace || { + cd "$workspace" || { echo "Error: failed to change dir to workspace" exit 100 } @@ -171,20 +171,20 @@ copy_layer() { } umount_rootfs() { - cd $output && { + 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 && { + cd "$output" && { zstd --force alpine_uefi_bootable.img && { sha1sum alpine_uefi_bootable.img.zst >alpine_uefi_bootable.img.zst.sha1sum } || { @@ -199,25 +199,8 @@ pack_rootfs() { } } - -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 +if [[ -z "$workspace" ]] && [[ -z "$output" ]]; then echo 'Error: env workspace or output empty' exit 100 fi