Skip to content

Instantly share code, notes, and snippets.

@hard-geha
Created January 14, 2014 11:11
Show Gist options
  • Save hard-geha/8416723 to your computer and use it in GitHub Desktop.
Save hard-geha/8416723 to your computer and use it in GitHub Desktop.
Simple init.d script to save all running virtualboxes on host shutdown or reboot
#!/bin/bash
### BEGIN INIT INFO
# Provides: save-running-virtualboxes
# Required-Start:
# Required-Stop: $network
# Default-Start:
# Default-Stop: 0 1 6
# Short-Description: Stops all running virtualboxes
# Description: Stops all running virtualboxes of all users
### END INIT INFO
case "$1" in
stop)
echo "Saving all running virtualboxes ..."
for user in $(ps -ouser= -C VBoxHeadless,VirtualBox | sort -u); do
for vm_name in $(su -c '/usr/bin/VBoxManage list runningvms | cut -d " " -f 1' $user); do
echo "Saving box '$vm_name' of user '$user'"
su -c "/usr/bin/VBoxManage controlvm $vm_name savestate" $user
done
done
echo "Done."
;;
*)
echo "Usage: /etc/init.d/save-running-virtualboxes stop"
exit 1
;;
esac
exit 0
@hard-geha
Copy link
Author

INSTALL

Download file as /etc/init.d/save-running-virtualboxes
chmod a+x /etc/init.d/save-running-virtualboxes
update-rc.d save-running-virtualboxes stop 19 0 1 6 . # for example - feel free to use insserv or chkconfig too

WHY

  • Just to have a nice saved virtualbox instead of an aborted one. You can use 'vagrant resume' for example.
  • Avoid all kinds of hanging shutdowns with messages like 'waiting for vboxnet0 to become free'.

@StefanNeuser
Copy link

Have to same issue und same idea but doesnt work for me. When i will reboot my System it idles still on 'waiting for vboxnet0 to become free'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment