Something like that:
vbox_start $machine
sleep 3
#!/bin/bash
. /etc/rc.conf
. /etc/rc.d/functions
[ -f /etc/conf.d/virtualbox ] && . /etc/conf.d/virtualbox
SU="su $VBOX_USER -c"
VBOXMANAGE="VBoxManage --nologo"
VBOXHEADLESS="VBoxHeadless"
vbox_stop() {
$SU "$VBOXMANAGE controlvm \"$1\" acpipowerbutton"
}
vbox_start() {
$SU "$VBOXHEADLESS -s \"$1\" -v config > /dev/null &"
}
# List all running VMs
vbox_list() {
$SU "$VBOXMANAGE list runningvms"
}
case "$1" in
start)
stat_busy "Starting VirtualBox machines"
for machine in "${VBOX_MACHINES[@]}"; do
vbox_start $machine
done
add_daemon virtualbox
stat_done
;;
stop)
stat_busy "Stopping VirtualBox machines"
for machine in `vbox_list`; do
vbox_stop $machine
done;
sleep 30
rm_daemon virtualbox
stat_done
;;
restart)
$0 stop
$0 start
;;
*)
echo "usage: $0 {start|stop|restart}"
esac
exit 0
When my arch-box boots, only the first of the two virtual machines start. The other one stays powered off.
When i run the script from terminal as root, all virtual machines star normally.
Same thing happens when i add these lines to /etc/rc.local (as suggested in arch wiki):
exec /bin/su -c "VBoxHeadless -s openser -v off" homer >/dev/null 2>&1
exec /bin/su -c "VBoxHeadless -s winserver" homer >/dev/null 2>&1
Only one machine starts (the first).
Any ideas?
]]>If you modify this section of RedShift's script:
# List all running VMs
vbox_list() {
$SU "$VBOXMANAGE list runningvms"
}
to look like this:
# List all running VMs
vbox_list() {
$SU "$VBOXMANAGE list runningvms | sed -e 's/^\".*\".*{\(.*\)}/\1/'"
}
You can eliminate errors like this:
ERROR: Could not find a registered machine named '{26f8caca-ddc5-40c0-94d3-2ee29c9e26bb}'
Details: code VBOX_E_OBJECT_NOT_FOUND (0x80bb0001), component VirtualBox, interface IVirtualBox, callee nsISupports
Context: "FindMachine (Bstr(a->argv[0]), machine.asOutParam())" at line 570 of file VBoxManage.cpp
ERROR: Could not find a registered machine named '{093d1dcb-e581-403d-a00d-234972fb5ca5}'
Details: code VBOX_E_OBJECT_NOT_FOUND (0x80bb0001), component VirtualBox, interface IVirtualBox, callee nsISupports
Context: "FindMachine (Bstr(a->argv[0]), machine.asOutParam())" at line 570 of file VBoxManage.cpp
See here for more details:
http://virtual-box.de/ticket/4388
Thanks again, RedShift!
]]># The user which the virtual machines are running
VBOX_USER=glenn
# Machines to start at boot
VBOX_MACHINES=(CentOS)
# Worst case scenario, shutting down will take VBOX_WAIT * VBOX_RETRY seconds.
# At first we send the ACPI powerbutton event, after this we do a poweroff
# Seconds to wait while shutting down
VBOX_WAIT=30
# Number of times to retry sending the ACPI powerbutton event
VBOX_RETRY=5
rc.virtualbox:
#!/bin/bash
. /etc/rc.conf
. /etc/rc.d/functions
[ -f /etc/conf.d/virtualbox ] && . /etc/conf.d/virtualbox
SU="su $VBOX_USER -c"
VBOXMANAGE="VBoxManage -nologo"
VBOXHEADLESS="VBoxHeadless"
# Send the ACPI powerbutton event to the virtual machine
vbox_powerbutton() {
$SU "$VBOXMANAGE controlvm \"$1\" acpipowerbutton"
}
# Poweroff the machine
vbox_poweroff() {
$SU "$VBOXMANAGE controlvm \"$1\" poweroff"
}
# Start the virtual machine
vbox_start() {
$SU "$VBOXHEADLESS -s \"$1\" > /dev/null &"
}
# List all running VMs
vbox_list() {
$SU "$VBOXMANAGE list runningvms"
}
case "$1" in
start)
stat_busy "Starting VirtualBox machines"
for machine in "${VBOX_MACHINES[@]}"; do
vbox_start $machine
if [ $? -gt 0 ]; then
echo "WARNING: FAILED STARTING $machine"
fi
done
add_daemon virtualbox
stat_done
;;
stop)
stat_busy "Stopping VirtualBox machines"
tries=0
while [ $tries -lt $VBOX_RETRY ]; do
for machine in `vbox_list`; do
vbox_powerbutton $machine
sleep $VBOX_WAIT
done;
tries=$((tries + 1))
done
for machine in `vbox_list`; do
echo "WARNING: UNCLEAN SHUTDOWN $machine"
vbox_poweroff $machine
done
rm_daemon virtualbox
stat_done
;;
restart)
$0 stop
$0 start
;;
*)
echo "usage: $0 {start|stop|restart}"
esac
exit 0
If you don't want to use it you can watch at the script on the author's site and use it as a template or inspiration for your solution.
bye
]]>I run an Arch linux server and i use Virtualbox-bin in order to host some windows services.
Until now, i have been starting the vm using /etc/rc.local:
/bin/su skysurfer -c "/usr/bin/VBoxHeadless -s winxp -v off"
The last days this has stopped working. Is there a way to make a custom daemon in /etc/rc.d so that i can start the vm as a system service?
I want to do the same for opendchub and other services... I guess the method would be the same.
Thanks in advance
]]>