-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
Showing
4 changed files
with
82 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,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 \ | ||
|
@@ -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,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 | ||
} || { | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,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 \ | ||
|
@@ -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,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 | ||
} || { | ||
|
@@ -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 | ||
|