Skip to content

Instantly share code, notes, and snippets.

@shalomb
Last active January 3, 2022 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shalomb/6041782dc8c16a698ca45b09c7b3429b to your computer and use it in GitHub Desktop.
Save shalomb/6041782dc8c16a698ca45b09c7b3429b to your computer and use it in GitHub Desktop.
Virtualbox guest time workaround
#!/bin/bash
# /etc/cron.hourly/0refresh-ntp
# Workaround for guest clock going out of skew regularly
# Essentially synchronizes systemd-timesyncd with NTP
# via timedatectl but using a reference of the datetime
# returned by http://worldtimeapi.org/api/ip
set -eu -o pipefail
function systemctl {
command systemctl "$@" > /dev/null
}
function restart-guest-services {
if ! systemctl status vboxadd.service; then
systemctl restart vboxadd.service
fi
systemctl status vboxadd.service
if ! systemctl status vboxadd-service.service; then
systemctl restart vboxadd-service.service
fi
systemctl status vboxadd-service.service
}
if [[ $1 == 'full' ]]; then
restart-guest-services || true
fi
timedatectl set-ntp false
wtapi=$(curl -fsSL "http://worldtimeapi.org/api/ip")
timezone=$(jq -Scer .timezone <<< "$wtapi")
echo "$timezone" > /etc/timezone
timedatectl set-timezone "$TZ"
datetime=$(jq -Scer .datetime <<< "$wtapi")
date -s "$datetime" > /dev/null
TZ=$(< /etc/timezone) date
timedatectl set-local-rtc false
timedatectl set-ntp true
sleep 2
timedatectl --monitor
# function to launch our VM
freya() {
set -xv
while ! VBoxManage showvminfo freya --machinereadable | awk -F'[\"=]' '$1 == "VMState" && $3 == "running" { ec=0 } BEGIN { ec=1 } END { exit ec }'; do
VBoxManage modifyvm freya --rtcuseutc on
VBoxManage setextradata freya 'VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled' 1
VBoxManage startvm freya --type headless;
done
ssh -tt -o ConnectTimeout=1 -o ConnectionAttempts=60 localhost -p 2022 'FORCE=1 ~/.bin/tmuxie default'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment