Jaime Gago Condensing Information Systems From the Vapor Of Data


Dynamic Guest(RHEL6 and cousins)/Host(Os X) Time Zone Sync in Vagrant (VirtualBox Provider)

If like me you're in charge of the vagrant base boxes supporting a team with folks in different time zones you might find this hack useful. I use it to bring the time zone of a Screen Shot 2013-03-30 at 6.07.36 PMRHEL 6 or cousins (tested on CentOS 6.4) guest VM to the same as an Os X host (tested on 10.7.5) in Vagrant, VirtualBox provider environment. I'm leveraging an Os X CLI utility  with a shell provisioner but it shouldn't be too much of a deal to refactor for Ansible/Puppet/Chef provisioner. I'm pretty sure there are better ways of doing this, so if you have a more elegant solution please describe it in the comments!

1) Find the host time zone

Add this to your VagrantFile

TimeZone = `systemsetup -gettimezone|cut -d':' -f2`

2) Pass it as an argument to a shell script provisioner

Following Vagrant official doc example it will look like this in your VagrantFile

config.vm.provision "shell", path: "guest-host-timezone-map.sh", args:TimeZone

3) Shell script to set time zone based on this tip from Red Hat

Place this shell script next to your VagrantFile (if you follow my example it'd have to be named guest-host-timezone-map.sh)

CurrentTimeZone=$(cat /etc/sysconfig/clock |cut -d'=' -f'2')
if [ ! $CurrentTimeZone == $1 ];then
cat << EOF > /etc/sysconfig/clock
ln -sf /usr/share/zoneinfo/$1 /etc/localtime
Comments (0) Trackbacks (0)

No comments yet.

Leave a comment

No trackbacks yet.