how to auto-shutdown Windows VM

Discussions related to using VirtualBox on Linux hosts.
Post Reply
MarkFoley
Posts: 150
Joined: 19. Aug 2016, 03:20

how to auto-shutdown Windows VM

Post by MarkFoley »

I have installed a Window 7 virtual machine on Ubuntu 16.04 and VirtualBox 5.1.2. I've found out the hard way that if the user running a VM logs out, or the computer is shutdown or rebooted, or 'Close/Power Off' is selected on the the VM VirtualBox Manager, that it is as if a "real" Windows computer is powered off. The next time the Windows VM is started it asks about starting Windows normally, in Safe Mode, etc. I imagine there is also the possibility of file corruption.

So, is there a way to tell the VM to shut down gracefully from the Linux command line? I could put those into my rc.local_shutdown or ~/.bash_logout.

Sorry if this question has already been asked a million times, but I couldn't find the answer in the Search.
socratis
Site Moderator
Posts: 27330
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: how to auto-shutdown Windows VM

Post by socratis »

You could use the ACPI feature to shut down the guest, if your guest supports and is configured for it, or you can save the VM state. I'd go for option #1. Use "VBoxManage controlvm":
  • VBoxManage controlvm <uuid|vmname> poweroff|savestate|acpipowerbutton|acpisleepbutton
I trimmed the options available. There are also other ones that you can explore. For more details, see User Manual, Chapter 8.13 VBoxManage controlvm. Do let us know what you picked.
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
MarkFoley
Posts: 150
Joined: 19. Aug 2016, 03:20

Re: how to auto-shutdown Windows VM

Post by MarkFoley »

I'm going to try `VBoxManage controlvm <vm> acpipowerbutton` since that one is described as "will send an ACPI shutdown signal to the VM, as if the power button on a real computer had been pressed. So long as the VM is running a fairly modern guest operating system providing ACPI support, this should trigger a proper shutdown mechanism from within the VM." A proper shutdown is what I want.

2 questions:

1) the VM I will have running is ...mark/labratw//labratw.vdi. What do I put in the <vm> arg? labratw?

2) is there a way to first test and see if this VM is actually running? Does it matter? I.e. if I to the `controlvm` command, and the vm is not running, will that matter? Note that `VBoxManage list runningvms` shows nothing, even with the VM running.
socratis
Site Moderator
Posts: 27330
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: how to auto-shutdown Windows VM

Post by socratis »

MarkFoley wrote:1) the VM I will have running is ...mark/labratw//labratw.vdi. What do I put in the <vm> arg? labratw?
The VM name is the name that you gave it when you created it, or when you renamed it. I don't know if you followed the defaults or not, but "...mark" doesn't seem like a normal path in any OS that I know of. You can get the names and the UUIDs with:
  • VBoxManage list vms
MarkFoley wrote:2) is there a way to first test and see if this VM is actually running? Does it matter? I.e. if I to the `controlvm` command, and the vm is not running, will that matter?
If I issue the command to a VM that is not running, I get:
  • $ VBoxManage controlvm "Mac" acpipowerbutton
    VBoxManage: error: Machine 'Mac' is not currently running
In all honesty, all of the controlvm options apply only to running VMs.
MarkFoley wrote:Note that `VBoxManage list runningvms` shows nothing, even with the VM running.
That's not good. Not good at all. Are you running the VM(s) as a different user? You shouldn't. You won't be able to get information or act on a VM that is not running in your user context.
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
MarkFoley
Posts: 150
Joined: 19. Aug 2016, 03:20

Re: how to auto-shutdown Windows VM

Post by MarkFoley »

socratis wrote:
MarkFoley wrote:1) the VM I will have running is ...mark/labratw//labratw.vdi. What do I put in the <vm> arg? labratw?
The VM name is the name that you gave it when you created it, or when you renamed it. I don't know if you followed the defaults or not, but "...mark" doesn't seem like a normal path in any OS that I know of. You can get the names and the UUIDs with:
  • VBoxManage list vms
I'm not using the default paths. My user's home directory is on a domain server, but I want the actual VM locally. The full path is /home/VirtualMachines/user/vmname. See my previous post viewtopic.php?f=7&t=79355&sid=9719623e3 ... 93a9ea7a52. The name I gave the vm itself was labratw.
MarkFoley wrote:2) is there a way to first test and see if this VM is actually running? Does it matter? I.e. if I to the `controlvm` command, and the vm is not running, will that matter?
If I issue the command to a VM that is not running, I get:
  • $ VBoxManage controlvm "Mac" acpipowerbutton
    VBoxManage: error: Machine 'Mac' is not currently running
In all honesty, all of the controlvm options apply only to running VMs.
`VBoxManage controlvm "labratw" acpipowerbutton` failed:

Code: Select all

# VBoxManage controlvm "labratw" acpipowerbutton
VBoxManage: error: Could not find a registered machine named 'labratw'
VBoxManage: error: Details: code VBOX_E_OBJECT_NOT_FOUND (0x80bb0001), component VirtualBoxWrap, interface IVirtualBox, callee nsISupports
VBoxManage: error: Context: "FindMachine(Bstr(a->argv[0]).raw(), machine.asOutParam())" at line 96 of file VBoxManageControlVM.cpp
`VBoxManage controlvm labratw acpipowerbutton` failed:

Code: Select all

# VBoxManage controlvm labratw acpipowerbutton
VBoxManage: error: Could not find a registered machine named 'labratw'
VBoxManage: error: Details: code VBOX_E_OBJECT_NOT_FOUND (0x80bb0001), component VirtualBoxWrap, interface IVirtualBox, callee nsISupports
VBoxManage: error: Context: "FindMachine(Bstr(a->argv[0]).raw(), machine.asOutParam())" at line 96 of file VBoxManageControlVM.cpp
MarkFoley wrote:Note that `VBoxManage list runningvms` shows nothing, even with the VM running.
That's not good. Not good at all. Are you running the VM(s) as a different user? You shouldn't. You won't be able to get information or act on a VM that is not running in your user context.
The VM is running as user mark. I'm running the VBoxManage command as root. Do I need to provide some additional parameter to specify user or non-default path? I can see the VM running in `ps ax`:

Code: Select all

mark     12387  9.7 56.7 4821160 2235672 ?     Sl   08:05   2:17 /usr/lib/virtualbox/VirtualBox --comment labratw --startvm 8e8edb13-abe3-4cc1-bda4-d
MarkFoley
Posts: 150
Joined: 19. Aug 2016, 03:20

Re: how to auto-shutdown Windows VM

Post by MarkFoley »

More info ...

Running `VBoxManage list runningvms` as user mark (VM owner) did work. Doing the `controlvm acpipowerbutton` did nothing. `controlvm poweroff` did work, but I got the "Start Windows Normally" screen when I turned the VM back on -- which is exactly what I'm trying to prevent.

Code: Select all

$ VBoxManage list runningvms
"labratw" {8e8edb13-abe3-4cc1-bda4-dd000b2cdcb6}

$ VBoxManage controlvm labratw acpipowerbutton

$ VBoxManage controlvm labratw poweroff
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
So, some questions ... can root list/control the VMs of other users somehow? I'm trying to create a shutdown script that will end the VM session gracefully.

I have Windows itself configured in Power Options > System Settings to do a shutdown when the power button is pressed, but that setting is apparently not connected to the acpipowerbutton command. Is there a setting I can do in either the Windows VM or VirtualBox to enable acpipowerbutton ? Note that apcishutdown does work from the VirtualBox Manager > right-click on VM > Close > ACPI Shutdown. I wonder why it doesn't work with `VBoxManage controlvm labratw acpipowerbutton`?
Perryg
Site Moderator
Posts: 34369
Joined: 6. Sep 2008, 22:55
Primary OS: Linux other
VBox Version: OSE self-compiled
Guest OSses: *NIX

Re: how to auto-shutdown Windows VM

Post by Perryg »

VBoxManage controlvm <vm> savestate will save the current state of the VM to disk
and then stop the VM. (This is equivalent to selecting the “Close” item in the “Machine”
menu of the GUI or pressing the window’s close button, and then selecting “Save the ma-
chine state” in the dialog.)
All of this is included in the manual section 8, http://www.virtualbox.org/manual/ch08.html But if you want to control it your script must be run as the user due to Linux permissions. You can setup a shared user if that makes it easier but this is not really a VBox issue at this point, but rather a Linux question. I would suggest that you create a systemd or initd script and let the OS handle the start/shut down. There is also a section on autostarting a guest in the manual.
socratis
Site Moderator
Posts: 27330
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: how to auto-shutdown Windows VM

Post by socratis »

@MarkFoley
You have to read the whole post before replying, it would have saved you several steps ;) Like:
  • Your VMs are yours and yours alone. Not system's, not root's, not anyone else's but yours. Whoever "you" might be.
  • "acpipowerbutton" won't work if your guest is not configured for it. Same for "acpisleepbutton". Check your guest's configuration.
  • "poweroff" was one option, it wasn't the suggested one.
@Perryg
It's not just Linux that does this, it's on every platform that VirtualBox runs. It's not a Linux permission issue, it's a user-context security feature. To which I must ask; don't systemd and/or initd scripts run under the root context? They wouldn't be able to control a VM from another account. But, I may be wrong here.
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
Perryg
Site Moderator
Posts: 34369
Joined: 6. Sep 2008, 22:55
Primary OS: Linux other
VBox Version: OSE self-compiled
Guest OSses: *NIX

Re: how to auto-shutdown Windows VM

Post by Perryg »

Since the OP is posting in the Linux Hosts section I refereed to that as the OS. As for starting and stopping with systemd or initd, yes it can be manipulated to user space given the proper setup. Do I use it? No I don't have that need but at one point years ago I did in fact setup this very thing as a test. Trick was to use a bash to call and use the proper flags to retain user, "-u". I would think that would still be available today though, but only testing will tell.
MarkFoley
Posts: 150
Joined: 19. Aug 2016, 03:20

Re: how to auto-shutdown Windows VM

Post by MarkFoley »

Progress. I get it that only the VM owner can use the VBoxManage commands on his own VMs. Seems a little odd to me that the root user can't do anything though. My intention is this -- my user(s) are normal office staff, not techie. They have used Windows forever, but have not used VMs and are not really used to the idea that they have shut down the VM window gracefully. Nor do they generally close windows at the end of the day. So, I want a mechanism that will automatically close the VM when the machine is shutdown (e.g. user, powerfailure, etc.), rebooted (user, udates, etc.) or the user logs off if the user forgets to shutdown the VM, which they will. The following will work as root (tested):

Code: Select all

# su mark --command "/usr/bin/VBoxManage list runningvms"
"labratw" {8e8edb13-abe3-4cc1-bda4-dd000b2cdcb6}

# su mark --command "/usr/bin/VBoxManage controlvm labratw acpipowerbutton"
So, I can put something like the following into a systemd shutdown/reboot script:

Code: Select all

# For all logged in users ...
who | awk '{print $1}' | \
while read u
do
    # See if they have a virtual machine
    if [ -d /home/VirtualMachines/$u ]  # this is where I'm keeping all virtual machines, non-standard
    then
        # if so, see if their VM is running
        vm=`su $u --command "/usr/bin/VBoxManage list runningvms" | awk '{print $1} | tr -d '"'`

        # if so, shutdown gracefully
        if [ -n "$vm" ]; then su $u --command "/usr/bin/VBoxManage controlvm $VM acpipowerbutton"
    fi
done
I'll also create a user logout script to 'savestate', though I don't know where that would go in Ubuntu/Cinnamon yet.

BTW `VBoxManage controlvm mark acpipowerbutton` did work on subsequent tries. I don't know why it didn't work the very first time I tried it.
Perryg
Site Moderator
Posts: 34369
Joined: 6. Sep 2008, 22:55
Primary OS: Linux other
VBox Version: OSE self-compiled
Guest OSses: *NIX

Re: how to auto-shutdown Windows VM

Post by Perryg »

There you go, now you see. I do need to correct one thing though. Root can in fact be made to run the user land access but that will change the setuid and then the user will not be able to access their guests. This has to do with the VBOX_USER_HOME environment and you would need to reset that each time for any user that you try to run as root. Much better to just use a script and be done with it. http://www.virtualbox.org/manual/ch10.html#idm9567
JSA
Posts: 26
Joined: 25. Aug 2016, 03:24
Primary OS: openSUSE
VBox Version: OSE other
Guest OSses: Windows Linux
Location: USA

Re: how to auto-shutdown Windows VM

Post by JSA »

The link you posted seems a little vague about what exactly VBOX_USER_HOME is to be set to
you can supply an alternate configuration directory by setting the VBOX_USER_HOME environment variable,
The users home dir,
the /home/USER/.config/VirtualBox/
The directory where the VMs are stored?

Last time I played with this nothing would boot till I cleared that, and went back to what ever defaults were set to.
Post Reply