howto save guest state each hour to start VM after poweroffs

Discussions related to using VirtualBox on Windows hosts.
Post Reply
abu
Posts: 17
Joined: 13. Aug 2009, 11:33
Primary OS: MS Windows XP
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Linux, Windows
Location: Galiza
Contact:

howto save guest state each hour to start VM after poweroffs

Post by abu »

Hi there
I am running a LAMP (ubuntu) guest machine on a WinXP host.
I have set up a scheduled task executing the following command, so the guest machine starts up after each host boot.

VBoxManage startvm "Ubuntu_server" --type headless

But I have not an UPS so, when there is a power off both (host and guest) are badly shut down. Unfortunately, this happens frequently (at least 2 or 3 times a week). So, I have 2 questions:

1. I wonder how to periodically save the current status of the guest machine running a .BAT script (let's say, each hour). I am only interested in the last status (I mean, the "2 hours ago status" could be overwritten by the "1 hour ago status").

2. And then, I would like to modify the startvm command (at host boot) so the VM starts from that saved status

For question 1, I guessed snapshots should be the solution (they are supposed to let you save the status of a running machine, true?), but the command below is giving me an error. Anyway, I wouldn't know how to start from that saved status (question 2).

VBoxManage snapshot "Ubuntu_server" take "Ubuntu_server_status"

this was my result (also when using the GUI to take the snapshot):

Code: Select all

VirtualBox Command Line Management Interface Version 3.0.4
(C) 2005-2009 Sun Microsystems, Inc.
All rights reserved.

0%...10%...20%...30%...FAILED
Error: failed to take snapshot. 
Error message: Hard disk 'E:\Documents and Settings\abu\.VirtualBox\HardDisks\VM-VB-ubuntu.vdi' is attached to a virtual machine with UUID {c4655d83-a6ab-4432-a86f-ac9895df0b51}. 
No differencing hard disks based on it may be created until it is detached
Any clues for any of the tasks?
Sasquatch
Volunteer
Posts: 17798
Joined: 17. Mar 2008, 13:41
Primary OS: Debian other
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Windows XP, Windows 7, Linux
Location: /dev/random

Re: howto save guest state each hour to start VM after poweroffs

Post by Sasquatch »

When you turn off the VM with saved state, or create a snapshot, you start the VM with the same command as if you never had either of those. With the snapshots, you have to keep in mind that you clean up on snapshots every once in a while, else the list will get very big and could cause problems. Nobody has tried running a VM with, let's say, 24 snapshots before, let alone 72 (3 days non-stop run).
Read the Forum Posting Guide before opening a topic.
VirtualBox FAQ: Check this before asking questions.
Online User Manual: A must read if you want to know what we're talking about.
Howto: Install Linux Guest Additions
Howto: Use Shared Folders on Linux Guest
See the Tutorials and FAQ section at the top of the Forum for more guides.
Try searching the forums first with Google and add the site filter for this forum.
E.g. install guest additions site:forums.virtualbox.org

Retired from this Forum since OSSO introduction.
abu
Posts: 17
Joined: 13. Aug 2009, 11:33
Primary OS: MS Windows XP
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Linux, Windows
Location: Galiza
Contact:

Re: howto save guest state each hour to start VM after poweroffs

Post by abu »

Thank you Sasquatch.
So I should understand VM will always automatically start from the last snapshot? Even if the VM was shutted by a host power-off? This is an example:
1. 10:50 AM: I am running a text editor on the guest. I write 2 lines and I go out for a coffee
2. 11:00 AM: the scheduled host script takes a snapshot (which should overwrite the previous one, see below)
3. 11:05 AM: power failure (so the host machine running VB deads suddenly)
4. 11:10 AM: power is back, host restarts and the "startvm" script boots guest.
From your answer, I understand VM auto-detects the snapshot and starts from it.
5. 11:15 AM: I am back. Should I expect the guest is now showing the editor with my 2 lines?
Sasquatch wrote:keep in mind that you clean up on snapshots every once in a while
Yes. I said I would like my snapshot to overwrite the previous one. But I don't know how to do it with the command line
Perhaps running a "discard" command before taking the snapshot?

VBoxManage "Ubuntu_server" snapshot discard "Ubuntu_server_status"
VBoxManage "Ubuntu_server" snapshot take "Ubuntu_server_status"

Anyway, as I said, I am getting an error when running the "take" command. Dont know why
Sasquatch
Volunteer
Posts: 17798
Joined: 17. Mar 2008, 13:41
Primary OS: Debian other
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Windows XP, Windows 7, Linux
Location: /dev/random

Re: howto save guest state each hour to start VM after poweroffs

Post by Sasquatch »

You're asking one tough question. Let's rephrase that: "You have a document on your computer and write a few lines. You go out for a cup of coffee and when you return, the power went out. When you boot your PC again, do you think you still have that document with the added lines?" The answer is simple: Only if you saved the document to disk. Everything stored in memory is lost. The same goes with the VM and snapshots. They are a shot of the current hard disk, and all new data will be put in the new state.
Now I haven't really tested it, but it is possible to revert to a running state of the OS, instead of a clean boot. You have to test that yourself by opening a program, make some changes (enter some text in notepad for example), create a snapshot and then shut down the VM clean. Now revert to the state and hope that you see your text in notepad. In theory, it should work.

For the commands, you need to test that too. Try various things and parameters, one should give you what you want. Reverting to a snapshot state after the power down, would be the adoptstate parameter. Discarding the state can be tricky. You have to be careful that you don't loose things.

Good luck.

Btw, I tested your command, VBoxManage snapshot "Win XP" take "test 1", and it succeeded without issues.
Read the Forum Posting Guide before opening a topic.
VirtualBox FAQ: Check this before asking questions.
Online User Manual: A must read if you want to know what we're talking about.
Howto: Install Linux Guest Additions
Howto: Use Shared Folders on Linux Guest
See the Tutorials and FAQ section at the top of the Forum for more guides.
Try searching the forums first with Google and add the site filter for this forum.
E.g. install guest additions site:forums.virtualbox.org

Retired from this Forum since OSSO introduction.
abu
Posts: 17
Joined: 13. Aug 2009, 11:33
Primary OS: MS Windows XP
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Linux, Windows
Location: Galiza
Contact:

Re: howto save guest state each hour to start VM after poweroffs

Post by abu »

Sasquatch wrote:Everything stored in memory is lost. The same goes with the VM and snapshots. They are a shot of the current hard disk, and all new data will be put in the new state.
Thanks a lot for that point, I had completely misunderstood what the snapshots are. Then I can't do what I expected.
Reverting to a snapshot state after the power down, would be the adoptstate parameter.
Where did you find that info? The VB user manual only says (page 108):

VBoxManage adoptstate <uuid>|<name> <state_file>

The manual does not further explain command usage. Do you have other documentation?
Btw, I tested your command, VBoxManage snapshot "Win XP" take "test 1", and it succeeded without issues.
Your machine is running or not? I can't understand what's happening. Your command is exactly like mine. No idea about what causes the error message I got?

Error message: Hard disk 'E:\...\VM-VB-ubuntu.vdi' is attached to a virtual machine with UUID {c4655d83-a6ab-4432-a86f-ac9895df0b51}. No differencing hard disks based on it may be created until it is detached

If a machine is running, isn't the hard disk supposed to be attached to it? Otherwise, the machine is not running.

Thanks a lot for your help
Sasquatch
Volunteer
Posts: 17798
Joined: 17. Mar 2008, 13:41
Primary OS: Debian other
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Windows XP, Windows 7, Linux
Location: /dev/random

Re: howto save guest state each hour to start VM after poweroffs

Post by Sasquatch »

I made a small mistake with the snapshot part. I tried it with my own machine, opened notepad and typed some text. Didn't save or whatever. Created the snapshot with the command and it made it just fine. Closing it went just like a normal machine without snapshots or whatever. In the GUI, I reverted to the previous state, in other words, when I created the snapshot. That cause the VM to start when I created the snapshot, with Notepad running and the text in it. So you can do what you want with snapshots.

The Adopt-state isn't something you can use, I tried it, didn't work. I did test a few other things, but not thoroughly, that's up to you. It is somewhere with the snapshot parameters.

The error you get is probably because you have one VDI attached to several VMs. Compare the UUID you get from the error with the one of your VM.
Read the Forum Posting Guide before opening a topic.
VirtualBox FAQ: Check this before asking questions.
Online User Manual: A must read if you want to know what we're talking about.
Howto: Install Linux Guest Additions
Howto: Use Shared Folders on Linux Guest
See the Tutorials and FAQ section at the top of the Forum for more guides.
Try searching the forums first with Google and add the site filter for this forum.
E.g. install guest additions site:forums.virtualbox.org

Retired from this Forum since OSSO introduction.
Perryg
Site Moderator
Posts: 34369
Joined: 6. Sep 2008, 22:55
Primary OS: Linux other
VBox Version: OSE self-compiled
Guest OSses: *NIX

Re: howto save guest state each hour to start VM after poweroffs

Post by Perryg »

One other thing to keep in mind here. If you intend to use snapshots you really should have a second VDI attached to store you data. Second drives are not included in snapshot reverts, so you do not loose data when you need to revert in time on the VM.
abu
Posts: 17
Joined: 13. Aug 2009, 11:33
Primary OS: MS Windows XP
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Linux, Windows
Location: Galiza
Contact:

Re: howto save guest state each hour to start VM after poweroffs

Post by abu »

Sasquatch wrote:The error you get is probably because you have one VDI attached to several VMs. Compare the UUID you get from the error with the one of your VM.
You were absolutely right!! Now it works perfectly, thanks a lot for your help.

Now I see many nested snapshots in GUI as I keep on running the following command (lets say, hourly):

Code: Select all

VBoxManage snapshot "MachineName" take "SnapshotName_datetime"
When the host is booting, I run the following .BAT as a scheduled task:

Code: Select all

REM ... current state of "MachineName" is stopped (safely) or aborted (power off)
REM ... so I restore it from the most recent snapshot, using this command:
   VBoxManage snapshot "MachineName" discardcurrent --state

REM ... then, I should delete the N old snapshots one by one by their names, like this:
   VBoxManage snapshot "MachineName" discard "SnapshotName_1"
   VBoxManage snapshot "MachineName" discard "SnapshotName_2"
   ... VBoxManage snapshot "MachineName" discard "SnapshotName_N-1"

REM ... I keep only the most recent snapshot 
REM     (just in case the "discardcurrent --state" command failed above).
REM ... Finally, I restart my machine like this:
   VBoxManage startvm "MachineName"
I am stil not running .BAT script, since I need to find out the actual names of the snapshots (1 to N) and pass them to this script. But this is something related to my poor batch scripting.
The important thing is that I tried the commands by hand, and they worked OK (my machine shows up with the text editor I was using, and showing the not-saved lines I wrote before the power went off).

Regarding the topic of my post, I expected that lots of people should already have had similar needs, so somebody would redirect me to a different post.

Well, I started this topic, but actually I am not quite sure of all this being important in order to run a guest web server (on VirtualBox hosts which are permanently powered on, but not plugged to an UPS).

What do you think?
vbox4me2
Volunteer
Posts: 5218
Joined: 21. Nov 2008, 20:27
Location: Rotterdam
Contact:

Re: howto save guest state each hour to start VM after poweroffs

Post by vbox4me2 »

abu wrote:Well, I started this topic, but actually I am not quite sure of all this being important in order to run a guest web server (on VirtualBox hosts which are permanently powered on, but not plugged to an UPS).

What do you think?
Ahum... maybe spend 45 bucks on a ups and use a shutdown script run by the ups software?
abu
Posts: 17
Joined: 13. Aug 2009, 11:33
Primary OS: MS Windows XP
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Linux, Windows
Location: Galiza
Contact:

Re: howto save guest state each hour to start VM after poweroffs

Post by abu »

vbox4me2 wrote:maybe spend 45 bucks on a ups
That was of great help, you saved my life :?

What I am trying to do is find the best way to run a LAMP server on machines which I don't even know (some might have UPS, some might not): I plan to distribute an VDI together with some instructions on how to run it ("download VirtualBox, put these files within this place, run this BAT as scheduled boot task, ...")

So I'm trying to find out the best way to make it survive after power outages
vbox4me2
Volunteer
Posts: 5218
Joined: 21. Nov 2008, 20:27
Location: Rotterdam
Contact:

Re: howto save guest state each hour to start VM after poweroffs

Post by vbox4me2 »

Yes I do get your intensions, but you can not ever anticipate what happens at poweroff time, corruption for one thing which can not easely be detected by a Host os or a Guest. Despite all kinds of error/md5(alike) checking if one bit somewhere gets flipped due to a poweroutage without detection the OS can no longer be relied upon. I've seen advanced systems with very advanced journalling go bust due to a single bit flip, albeit a rollback got it back online but they lost 3 minutes of transactions due to caching getting lost. At least a battery backedup controller should be considered.
Sasquatch
Volunteer
Posts: 17798
Joined: 17. Mar 2008, 13:41
Primary OS: Debian other
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Windows XP, Windows 7, Linux
Location: /dev/random

Re: howto save guest state each hour to start VM after poweroffs

Post by Sasquatch »

In addition to vbox4me2, you could end up with a broken VM if your script is creating a snapshot, but the power is lost right in the middle of it. Have you tried that yourself, create a snapshot and power down the computer forcefully? It might register the snapshot, create a file but is essentially empty and unusable. Thought about solving that problem?
Read the Forum Posting Guide before opening a topic.
VirtualBox FAQ: Check this before asking questions.
Online User Manual: A must read if you want to know what we're talking about.
Howto: Install Linux Guest Additions
Howto: Use Shared Folders on Linux Guest
See the Tutorials and FAQ section at the top of the Forum for more guides.
Try searching the forums first with Google and add the site filter for this forum.
E.g. install guest additions site:forums.virtualbox.org

Retired from this Forum since OSSO introduction.
Post Reply