Skip to content

Commit

Permalink
chore(timesync): sched time sync every 15 min (#50)
Browse files Browse the repository at this point in the history
- chore(timesync): sched time sync every 15 min
- chore(refact): shellcheck lint

---------

Signed-off-by: ihexon <[email protected]>
  • Loading branch information
ihexon authored Jan 6, 2025
1 parent 47eeeb2 commit d31aa8e
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 71 deletions.
22 changes: 22 additions & 0 deletions layers/macos_amd64/etc/periodic/15min/timeschedsync
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions layers/macos_arm64/etc/periodic/15min/timeschedsync
Original file line number Diff line number Diff line change
@@ -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
58 changes: 21 additions & 37 deletions target_builder/macos_amd64
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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 \
Expand All @@ -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 \
Expand All @@ -86,40 +86,41 @@ 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
}
} || {
echo 'Error: change dir to ./layers/$target_profile failed'
exit 100
}

cd $output && {
cd "$output" && {
sudo -E proot $qemu_flag --rootfs=./target_rootfs \
-b /dev:/dev \
-b /sys:/sys \
Expand All @@ -136,7 +137,7 @@ copy_layer() {
}
}

cd $output && {
cd "$output" && {
sudo -E proot $qemu_flag --rootfs=./target_rootfs \
-b /dev:/dev \
-b /sys:/sys \
Expand All @@ -149,7 +150,7 @@ copy_layer() {
}
}

cd $output && {
cd "$output" && {
sudo -E proot $qemu_flag --rootfs=./target_rootfs \
-b /dev:/dev \
-b /sys:/sys \
Expand All @@ -163,28 +164,28 @@ copy_layer() {
}

set +x
cd $workspace || {
cd "$workspace" || {
echo "Error: failed to change dir to workspace"
exit 100
}
sync # must do sync !
}

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
} || {
Expand All @@ -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" [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
if [[ -z "$workspace" ]] && [[ -z "$output" ]]; then
echo 'Error: env workspace or output empty'
exit 100
fi
Expand Down
51 changes: 17 additions & 34 deletions target_builder/macos_arm64
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 \
Expand All @@ -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 \
Expand All @@ -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
}
Expand All @@ -99,27 +99,27 @@ 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
}
} || {
echo 'Error: change dir to ./layers/$target_profile failed'
exit 100
}

cd $output && {
cd "$output" && {
sudo -E proot $qemu_flag --rootfs=./target_rootfs \
-b /dev:/dev \
-b /sys:/sys \
Expand All @@ -136,7 +136,7 @@ copy_layer() {
}
}

cd $output && {
cd "$output" && {
sudo -E proot $qemu_flag --rootfs=./target_rootfs \
-b /dev:/dev \
-b /sys:/sys \
Expand All @@ -149,7 +149,7 @@ copy_layer() {
}
}

cd $output && {
cd "$output" && {
sudo -E proot $qemu_flag --rootfs=./target_rootfs \
-b /dev:/dev \
-b /sys:/sys \
Expand All @@ -163,28 +163,28 @@ copy_layer() {
}

set +x
cd $workspace || {
cd "$workspace" || {
echo "Error: failed to change dir to workspace"
exit 100
}
sync # must do sync !
}

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
} || {
Expand All @@ -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" [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
if [[ -z "$workspace" ]] && [[ -z "$output" ]]; then
echo 'Error: env workspace or output empty'
exit 100
fi
Expand Down

0 comments on commit d31aa8e

Please sign in to comment.