|
| 1 | +@echo off |
| 2 | + |
| 3 | +rem Usage: VMSnapStartHost <vm uuid> |
| 4 | + |
| 5 | +rem - Takes a snapshot of the powered off VM to mitigate crashes. |
| 6 | +rem - Unplugs the network cable on adapter 1 for security. |
| 7 | +rem - Starts the VM. |
| 8 | +rem - Sets the transient guest property SnapStart=1. |
| 9 | + |
| 10 | +rem VirtualBox v6.1.16 |
| 11 | +rem An autostart guest script (see VMSnapStartGuest) can query the property |
| 12 | +rem via VBoxControl and alert the user if it's missing. |
| 13 | +rem *BUG* Transient properties should be automatically deleted when the VM |
| 14 | +rem shuts down, but somehow that doesn't happen. Therefore the guest script |
| 15 | +rem should explicitly unset it (Only VBoxControl...unset seems to work, even |
| 16 | +rem VBoxControl...delete has the variable somehow comming back). |
| 17 | + |
| 18 | +rem VirtualBox v6.1.18 |
| 19 | +rem It seems, the property now survives a restart of the guest OS. |
| 20 | +rem The property is removed on VM shutdown. Still using "unset". Not yet tested |
| 21 | +rem if "delete" would work now. |
| 22 | +rem Seems to have been a glitch, gone again after restart. |
| 23 | + |
| 24 | + |
| 25 | +rem Unlike other path vars, this one already has trailing backslash. |
| 26 | +set vbm="%VBOX_MSI_INSTALL_PATH%vboxmanage.exe" --nologo |
| 27 | + |
| 28 | +rem Preset error msg for easier if/goto handling |
| 29 | +set emsg=Missing VM uuid parameter. |
| 30 | +if "%1" == "" goto failed |
| 31 | + |
| 32 | +set emsg=The VM is not powered off. |
| 33 | +%vbm% showvminfo %1 | find "State:" | find "powered off" |
| 34 | +if errorlevel 1 goto failed |
| 35 | + |
| 36 | +set emsg=snapshot command failed. |
| 37 | +%vbm% snapshot %1 take "Starting VM" |
| 38 | +if errorlevel 1 goto failed |
| 39 | + |
| 40 | +set emsg=modifyvm command failed. |
| 41 | +%vbm% modifyvm %1 --cableconnected1 off |
| 42 | +if errorlevel 1 goto failed |
| 43 | + |
| 44 | +set emsg=startvm command failed. |
| 45 | +%vbm% startvm %1 |
| 46 | +if errorlevel 1 goto failed |
| 47 | + |
| 48 | +set emsg=guestproperty command failed. |
| 49 | +%vbm% guestproperty set %1 SnapStart 1 --flags TRANSIENT |
| 50 | +if errorlevel 1 goto failed |
| 51 | +goto fini |
| 52 | + |
| 53 | +:failed |
| 54 | +echo. |
| 55 | +echo ***ERROR*** while starting the VM! |
| 56 | +echo. |
| 57 | +echo %emsg% |
| 58 | +echo. |
| 59 | +pause |
| 60 | +goto fini |
| 61 | + |
| 62 | +:fini |
0 commit comments