Jeg har over en lengre periode brukt følgende script for å automagisk starte VM's ved boot av Ubuntu serveren min. Det stopper også VM's på en grei måte noe jeg gjerne vil beholde.
Men så har det seg slik at dette scriptet starter alle VM's jeg har. Dette synes jeg etterhvert er svært lite intressant da det i utgangspunktet er bare 1 VM jeg må starte ved boot.
Spørsmålet er da, hva skal jeg endre på i scriptet for å oppnå dette?
OBS: Jeg har kopiert og limt inn scriptet fra putty, så ikke sikkert det er 100% komplett, men jeg håper dere ser nok til å kunne hjelpe meg.
Vil det være nok å endre
til
Men så har det seg slik at dette scriptet starter alle VM's jeg har. Dette synes jeg etterhvert er svært lite intressant da det i utgangspunktet er bare 1 VM jeg må starte ved boot.
Spørsmålet er da, hva skal jeg endre på i scriptet for å oppnå dette?
OBS: Jeg har kopiert og limt inn scriptet fra putty, så ikke sikkert det er 100% komplett, men jeg håper dere ser nok til å kunne hjelpe meg.
Vil det være nok å endre
Kode
function startService(){ for v in $ALLVMS; do if [[ -n $($SU $VBOXMANAGE showvminfo $v) ]]; then $echocmd "Waiting for VM \"$($SU $VBOXMANAGE list vms | grep $v | awk -F\" '{print $(NF -1)}')\" to power on..." && $SU $VBOXMANAGE startvm $v --type headless 1> /dev/null$ $echocmd "No Saved VMs to start!"
Kode
function startService(){ for v in $ALLVMS; do if [[ -n $($SU $VBOXMANAGE showvminfo $v) ]]; then $echocmd "Waiting for VM \"$($SU $VBOXMANAGE list vms | grep $v | awk -F\" '{print $(NF -1)}')\" to power on..." && $SU $VBOXMANAGE startvm VMNAME --type headless 1> /dev/null$ $echocmd "No Saved VMs to start!"
SPOILER ALERT! Vis spoiler
#!/bin/bash
### BEGIN INIT INFO
# Provides: vmboot
# Required-Start: vboxdrv $local_fs $syslog
# Required-Stop: vboxdrv $local_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Stop/Start VMs before/after System shutdown
### END INIT INFO
# Add this script to /etc/init.d/
# Run update-rc.d vmboot defaults as root
# Written to work with VirtualBox 4.x
# User who is running the VMs
VBOXUSER=vboxuser
SU="sudo -H -u $VBOXUSER"
#en/disable syslogging 1|0
EnableLogging="1"
echocmd="echo -e"
loggercmd="logger -s -f /var/log/syslog"
logdate=$(echo -e $(date +"%b %d %H:%M:%S"))
loggermsg="$(echo -e $logdate `hostname` vmboot: %s)\n"
# Path to VBoxManage
VBOXMANAGE="/usr/bin/vboxmanage --nologo"
# Get UUID of All Running VMs
# UUID looks like this: a02b5b54-a84d-48fa-8dac-2a3fad55717e
RUNNINGVMS=$($SU $VBOXMANAGE list runningvms | sed -e 's/^".*".*{\(.*\)}/\1/')
# Get UUID of All VMs
ALLVMS=$($SU $VBOXMANAGE list vms | sed -e 's/^".*".*{\(.*\)}/\1/')
#set logging
if [[ "$EnableLogging" -eq "1" ]]; then
echocmd=$loggercmd
fi
# Check to insure $ALLVMS is not null and exit if it is.
if [[ $ALLVMS = "" ]]; then
$echocmd "No VMs are detected on this host! Did you configure the VBOXUSER variable?"; exit
fi
function logMsg(){
if [[ "$EnableLogging" -eq "1" ]]; then
printf "$loggermsg" "$1" >> /var/log/syslog >> /dev/stdout;
fi
}
function stopService(){
if [[ -n $RUNNINGVMS ]]; then
for v in $RUNNINGVMS; do
$echocmd "Saving state of $($SU $VBOXMANAGE list vms | grep $v | awk -F\" '{print $(NF-1)}')..." && $SU $VBOXMANAGE controlvm $v savestate
done; else
$echocmd "No running VMs to save!"
fi
# If the previous loop has an error, the loops exits and returns an error.
if [[ $? -ne 0 ]]; then
$echocmd "There was an error stopping your VMs!"; else
echo -e "$(touch /var/log/vmboot-shutdown-`date +%Y%m%d%H%M%S`)";
logMsg "VMBoot has saved all VMs";
fi
}
function startService(){
for v in $ALLVMS; do
if [[ -n $($SU $VBOXMANAGE showvminfo $v) ]]; then
$echocmd "Waiting for VM \"$($SU $VBOXMANAGE list vms | grep $v | awk -F\" '{print $(NF -1)}')\" to power on..." && $SU $VBOXMANAGE startvm $v --type headless 1> /dev/null$
$echocmd "No Saved VMs to start!"
fi
# If the previous loop has an error, the loops exits and returns an error.
if [[ $? -ne 0 ]]; then
$echocmd "There was an error starting your VMs! Try starting them manually to see what the problem is."; break
fi
done
}
function restartService(){
stopService
startService
}
function serviceStatus(){
if [[ -n $RUNNINGVMS ]]; then
$echocmd "List of Running VMs:" && $SU $VBOXMANAGE list runningvms; else
$echocmd "No VMs Currently Running!"
fi
}
case $1 in
stop)
stopService
;;
start)
startService
;;
restart)
restartService
;;
status)
serviceStatus
;;
force-reload)
#not really sure what to do here other than restart
restartService
;;
*)
echo "Usage: /etc/init.d/vmboot start | stop | status | restart | force-reload"; exit 1
;;
esac
exit 0
# eof
### BEGIN INIT INFO
# Provides: vmboot
# Required-Start: vboxdrv $local_fs $syslog
# Required-Stop: vboxdrv $local_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Stop/Start VMs before/after System shutdown
### END INIT INFO
# Add this script to /etc/init.d/
# Run update-rc.d vmboot defaults as root
# Written to work with VirtualBox 4.x
# User who is running the VMs
VBOXUSER=vboxuser
SU="sudo -H -u $VBOXUSER"
#en/disable syslogging 1|0
EnableLogging="1"
echocmd="echo -e"
loggercmd="logger -s -f /var/log/syslog"
logdate=$(echo -e $(date +"%b %d %H:%M:%S"))
loggermsg="$(echo -e $logdate `hostname` vmboot: %s)\n"
# Path to VBoxManage
VBOXMANAGE="/usr/bin/vboxmanage --nologo"
# Get UUID of All Running VMs
# UUID looks like this: a02b5b54-a84d-48fa-8dac-2a3fad55717e
RUNNINGVMS=$($SU $VBOXMANAGE list runningvms | sed -e 's/^".*".*{\(.*\)}/\1/')
# Get UUID of All VMs
ALLVMS=$($SU $VBOXMANAGE list vms | sed -e 's/^".*".*{\(.*\)}/\1/')
#set logging
if [[ "$EnableLogging" -eq "1" ]]; then
echocmd=$loggercmd
fi
# Check to insure $ALLVMS is not null and exit if it is.
if [[ $ALLVMS = "" ]]; then
$echocmd "No VMs are detected on this host! Did you configure the VBOXUSER variable?"; exit
fi
function logMsg(){
if [[ "$EnableLogging" -eq "1" ]]; then
printf "$loggermsg" "$1" >> /var/log/syslog >> /dev/stdout;
fi
}
function stopService(){
if [[ -n $RUNNINGVMS ]]; then
for v in $RUNNINGVMS; do
$echocmd "Saving state of $($SU $VBOXMANAGE list vms | grep $v | awk -F\" '{print $(NF-1)}')..." && $SU $VBOXMANAGE controlvm $v savestate
done; else
$echocmd "No running VMs to save!"
fi
# If the previous loop has an error, the loops exits and returns an error.
if [[ $? -ne 0 ]]; then
$echocmd "There was an error stopping your VMs!"; else
echo -e "$(touch /var/log/vmboot-shutdown-`date +%Y%m%d%H%M%S`)";
logMsg "VMBoot has saved all VMs";
fi
}
function startService(){
for v in $ALLVMS; do
if [[ -n $($SU $VBOXMANAGE showvminfo $v) ]]; then
$echocmd "Waiting for VM \"$($SU $VBOXMANAGE list vms | grep $v | awk -F\" '{print $(NF -1)}')\" to power on..." && $SU $VBOXMANAGE startvm $v --type headless 1> /dev/null$
$echocmd "No Saved VMs to start!"
fi
# If the previous loop has an error, the loops exits and returns an error.
if [[ $? -ne 0 ]]; then
$echocmd "There was an error starting your VMs! Try starting them manually to see what the problem is."; break
fi
done
}
function restartService(){
stopService
startService
}
function serviceStatus(){
if [[ -n $RUNNINGVMS ]]; then
$echocmd "List of Running VMs:" && $SU $VBOXMANAGE list runningvms; else
$echocmd "No VMs Currently Running!"
fi
}
case $1 in
stop)
stopService
;;
start)
startService
;;
restart)
restartService
;;
status)
serviceStatus
;;
force-reload)
#not really sure what to do here other than restart
restartService
;;
*)
echo "Usage: /etc/init.d/vmboot start | stop | status | restart | force-reload"; exit 1
;;
esac
exit 0
# eof