virtual-machinevirtualboxsnapshot

VirtualBox revert to snapshot from inside guest


Is there any way to restore a snapshot from inside a VBox guest machine?

I have a Windows machine that hosts numerous machines. Currently we are working with something using a Ubuntu guest and it is really painful to have to keep switching machines just to revert a snapshot.

What I had in mind is setting the machine to a "base" state and every time I want to go to that I just type some command like:

revertbase

Than the machine would restart in the previous snapshot and I would just need to restart ssh to continue.


Solution

  • You cannot snapshot a running machine, you have to freeze it before, so my guess is that the host itself cannot do that.

    In the host machine, from command line you can do this using VBoxManage.

    The file is located in

    Program Files/Oracle/VirtualBox/VBoxManage.exe

    and is used as a command-line interface with VirtualBox.

    Using the command:

    VBoxManage snapshot "MachineName" take SnapShotName
    

    Them after that:

    VBoxManage snapshot "MachineName" discardcurrent -state
    

    To return to the last state, for more read the text bellow, to have easy acess to VBoxManage add it to your path:

    PATH=%PATH%;c:\Program Files\Oracle\VirtualBox
    

    Taken from: https://www.linux.com/news/secrets-controlling-virtualbox-command-line/

    Managing snapshots

    One of the most useful feature of virtualization software is its ability to take snapshots of VMs. It's always a good idea to take a snapshot of a VM before making changes to it. Snapshots help on the hardware level to recover a system that has been rendered unusable due to changes to the hardware configuration, and on the software level they protect against data loss due to accidental deletion or virus.

    Taking a snapshot from the VirtualBox CLI is child's play. VBoxManage snapshot "Fedora" take snap1-stable-system takes the snapshot of a stable Fedora VM when everything is working perfectly. Saving a snapshot might take some time, depending on the VM and the resources on the host. To make sure you don't make changes to a system while a snapshot is being taken, VirtualBox grays out the whole VM interface, and you cannot use it until the snapshot has been saved.

    With a stable snapshot in hand, go ahead and play with the system. If you get in trouble and your machine won't boot or starts behaving abnormally, you can revert to the snapshot of the stable machine. To do this, first power off the VM with VBoxManage controlvm "Fedora" poweroff, then revert to last snapshot with VBoxManage snapshot "Fedora" discardcurrent -state. If you have multiple snapshots, you can revert to the last but one snapshot with the -all switch instead of -state.

    Of course when you revert to an older state, all the changes you made since that snapshot was taken are lost, including all configuration changes and changes to old and new files. You can work around this by specifying that your data should be stored on a "writethrough" disk, which behaves like a normal disk but isn't affected by snapshots. Put another way, when you take a snapshot, VirtualBox ignores the writethrough disk. You can store all your important data and files or your complete /home directory on that disk.

     

    To add a writethrough disk, use the -type writethough option when creating a new disk with createvdi. You can also change a disk you created earlier and make it writethrough. To do so, first unattach it from the VM with VBoxManage modifyvm "Fedora" -hdb none, and then unregister it with VBoxManage unregisterimage disk fourgig (using the name of the disk on your system in place of fourgig). Now register it back again but as a writethrough disk with VBoxManage registerimage disk "fourgig" -type writethrough. Finally, attach it back to the VM using VBoxManage modifyvm "Fedora" -hdb fourgig.

    Now you can safely save data on this disk, and no matter what state the VM is in, the data will always be safe. But remember not to revert back to a state that was saved before this disk was created; if you do, VirtualBox will simply delete the disk, becase it didn't exist in that state. Also, VirtualBox doesn't currently let you take a snapshot of a VM that has a writethrough disk attached, so you have to unattach a writethough disk before saving the state of the VM and then reattach it. I hope in upcoming VirtualBox versions the presence of a writethrough disk will have no influence on the snapshot process.