Best way to force VirtualBox to execute a batch script before shutting down a Windows Guest OS?

Discussions related to using VirtualBox on Windows hosts.
Post Reply
woodrock
Posts: 26
Joined: 9. Jun 2017, 08:17

Best way to force VirtualBox to execute a batch script before shutting down a Windows Guest OS?

Post by woodrock »

What's the best way to have VirtualBox always run a batch command before before shutting down a Windows Guest OS inside VirtualBox?
socratis
Site Moderator
Posts: 27329
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: Best way to force VirtualBox to execute a batch script before shutting down a Windows Guest OS?

Post by socratis »

I'm not sure I follow. You want to run a script: where, hosts or guest? Before shutdown: what shutdown, scheduled or not, from within the the guest of from the host?
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.
scottgus1
Site Moderator
Posts: 20945
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Windows, Linux

Re: Best way to force VirtualBox to execute a batch script before shutting down a Windows Guest OS?

Post by scottgus1 »

Edit: Wish I'd read more carefully, I would have not typed all the below stuff. You said "before" the guest OS shuts down. (Memo: Administer personal dope slap to self...)

There is no built-in Virtualbox method to "run this script before Guest X shuts down". Virtualbox doesn't really know what is going on inside the guest OS. You can use a network to establish a connection between a guest process run during shut down (use Group Policy Editor in the guest if you have access to it to look for Log-Off and Shut-Down Scripts, or research the appropriate registry locations to set up such scripts) and a host process polling the guest process through a network connection (maybe Host-Only).

Treat the problem as if you have two networked PCs and you want one to know that the other is going to shut down.

The below is left for posterity, in case it may be helpful in different situations:
********************************************************
There is no built-in Virtualbox method to "run this script when Guest X shuts down". But if you make a Windows batch file which is properly programmed, you can have the batch file start the guest then wait until the guest window disappears to continue running the script. Here's a quick .cmd to get you started:

Code: Select all

start "running my VB Guest" /wait {target from guest desktop shortcut}
echo proceeding
pause
To get the info for {target from guest desktop shortcut}, right-click your guest in the main Virtualbox window and choose "Create Shortcut on Desktop". Right-click the new shortcut and choose Properties. The "Target" line contains the "Virtualbox.exe" command-line data you need to put in place of "{target from guest desktop shortcut}" in the batch file.

When you try this you will see a command window open, show the "Start /wait" command, then your guest will start, then the command window will do nothing until the guest is turned off and the Virtualbox window disappears. Then the command window will print "Proceeding press any key to continue".

Note that starting the guest with Vboxmanage.exe will not allow the sample batch file to pause, since Vboxmanage finishes running when it is finished causing whatever it caused (in other words it closes soon as the guest is started), whereas Virtualbox.exe remains running for the whole time the guest is running.

If you really need to use Vboxmanage to start your guest, so you can start it headless, for example, or some other such, you will need to script a loop in the batch file to look for and monitor the presence of the process that runs the guest and only continue when that process is gone. (Note that for this monitoring system you will need the UUID of the guest, which you can get using the Desktop shortcut target line above.) You can use Process Explorer to see what happens when you run the guest without using the batch file and tweak the monitor loop appropriately. (How to do this is, as the old math books say, "left as an exercise for the student" :)

One other situation: If your batch file needs to know even when the guest OS reboots, the above system will not help in that case, because the batch file is waiting for the absence of the Virtualbox window containing the guest, not the guest processes themselves. You would need a communication system from guest OS to host OS to trigger when the guest reboots. Figure this one out as if you had two PCs side by side and networked.
woodrock
Posts: 26
Joined: 9. Jun 2017, 08:17

Re: Best way to force VirtualBox to execute a batch script before shutting down a Windows Guest OS?

Post by woodrock »

socratis wrote:You want to run a script: where, hosts or guest? Before shutdown: what shutdown, scheduled or not, from within the the guest of from the host?
The plan is to put a set of initially empty 4.5MB-size-limited "folders" permanently on the WIndows 10 Guest OS desktop.
The only "problem" the shutdown script has to prevent is this corruption issue stated by Microsoft over here:
Microsoft wrote:You should always detach VHDs prior to restarting the operating system to prevent data corruption. To do this, first select the file that you want to detach (select vdisk file="c:\vhd\test.vhd"), and then type detach vdisk.
So, the problem is how to run this set of commands (as admin) suggested by Microsoft BEFORE the Guest Windows OS is restarted?
C:\> diskpart
DISKPART> select vdisk file="c:\Users\x\Documents\vdisk01.vhd"
DISKPART> detach vdisk
DISKPART> exit
scottgus1 wrote:There is no built-in Virtualbox method to "run this script before Guest X shuts down".
Darn. I was hoping to be able to tie a script to the guest-os shutdown process.

The goal is to have a set of size-limited folders permanently on the Windows 10 Guest OS desktop but the problem is the Microsoft statement above that these size-limited "folders" must have their virtual disk volumes detached prior to restarting the Windows 10 Guest OS.

The plan is to create each of these permanently size-limited desktop folders on the Guest OS using the Microsoft method described here.

STEP 1: Onetime only ... As admin, create any number of permanent virtual disks in the Windows 10 Guest OS:
C:\> diskpart
DISKPART> create vdisk file=c:\Users\x\Documents\vdisk01.vhd maximum=4500
DISKPART> list vdisk (should report that the virtual disk file vdisk01.vhd was added but the type is unknown)
DISKPART> detail vdisk (should report that the virtual disk is added)
DISKPART> attach vdisk
DISKPART> list vdisk (should report that the virtual disk file vdisk01.vhd was added and the type is fixed)
DISKPART> detail vdisk (should report that the virtual disk is added and attached but not open)
DISKPART> create partition primary
DISKPART> format fs=ntfs label="vhd vdisk01" quick
DISKPART> assign letter=z
DISKPART> list vdisk (should report that the virtual disk file vdisk01.vhd is added and the type is fixed)
DISKPART> detail vdisk (should report that the virtual disk is created, attached, and not open)
DISKPART> exit

STEP 2: Onetime only ... Create any number of permanent size-limited desktop folders tied to that virtual disk:
C:\> mklink /d C:\Users\x\Desktop\folder01 Z:

The hope was that I could just tie a guest-os script to the process of shutting down the virtual machine that detaches the Microsoft virtual disks.
socratis
Site Moderator
Posts: 27329
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: Best way to force VirtualBox to execute a batch script before shutting down a Windows Guest OS?

Post by socratis »

woodrock wrote:The plan is to put a set of initially empty 4.5MB-size-limited "folders" permanently on the WIndows 10 Guest OS desktop.
Why????? Why do you want to have a virtual hard disk inside your virtual VM? What do you have in mind? What's the end goal here?
woodrock wrote:Darn. I was hoping to be able to tie a script to the guest-os shutdown process.
Well, write a script that performs whatever you want and then shuts down the VM, what's the difference?
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.
woodrock
Posts: 26
Joined: 9. Jun 2017, 08:17

Re: Best way to force VirtualBox to execute a batch script before shutting down a Windows Guest OS?

Post by woodrock »

socratis wrote:Why do you want to have a virtual hard disk inside your virtual VM? What do you have in mind? What's the end goal here?
As you seem to have correctly surmised, the end goal has nothing to do (per se) with virtual hard disk volumes.

The end goal is simply to have a number of size-limited folders on the Guest OS desktop.
However ...
There is no known way to limit the size of Windows folders without creating a "partition" of some sort (and then symlinking that size-limited "volume" to what becomes, in effect, size-limited "folders").

How does one limit the folder size in WIndows?
SOLUTION 1: Logically, one would first consider size "quotas" but quotas just don't work on folders in Windows.
SOLUTION 2: Next one would consider hard disk partitions, which would work, but who wants dozens of hard disk partitions?
SOLUTION 3: One might use 3rd-party software, such as VeraCrypt, to create "volumes" which can be mounted as logical drives and then symlinked to desktop folders, but that carries the overhead of encryption.
SOLUTION 4: The most logical answer is Microsoft Virtual Hard Disks, but they carry the risk of corruption when the Windows Guest OS is "restarted".

BTW, I don't know how prevalent this corruption problem is, and I'm not sure why Microsoft says the problem happens when the Guest OS is "restarted" and not just "rebooted" (I presume that means the same thing but I could be wrong).
socratis wrote:Well, write a script that performs whatever you want and then shuts down the VM, what's the difference?
That's probably a good idea.

STEP 1: Write a batch file that first detaches all existing virtual disks, and then it shuts down the Guest OS inside the VirtualBox VM.
STEP 2: Tie that batch script to the existing menu item for "VirtualBox: File > Close > (o)Send the shutdown SIgnal"

Is that "Send the shutdown signal" menu entry easily extensible to run a batch file?
socratis
Site Moderator
Posts: 27329
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: Best way to force VirtualBox to execute a batch script before shutting down a Windows Guest OS?

Post by socratis »

woodrock wrote:The end goal is simply to have a number of size-limited folders on the Guest OS desktop.
Again, why? That's not your end goal, that's a means to the goal. Having fixed size folders on its own doesn't make too much sense.

If you want something size limited, you could very well create additional virtual hard drives from VirtualBox, with the required size, and attach them to your VM. They will appear as additional hard drives in your guest. E:\, F:\, etc.
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.
woodrock
Posts: 26
Joined: 9. Jun 2017, 08:17

Re: Best way to force VirtualBox to execute a batch script before shutting down a Windows Guest OS?

Post by woodrock »

socratis wrote:That's not your end goal, that's a means to the goal. Having fixed size folders on its own doesn't make too much sense.

What I'm trying to do is limit the desktop "folders" to DVD size
, so that I can manually fill them, over time, with a large variety of files which I need to go through manually, where I want the size-limited folder to never exceed DVD size (nominally let's just use 4.5GB as a pragmatic value but where the actual size isn't the issue here).

Suffice to say there will be multiple folders, some for photos of x, others for photos of y, others for videos of x, and others for videos of y, others for files related to x, and another for files related to y, etc., where I will be moving myriad files into these folders over a period of days or weeks as an organizational task, and then I will be burning those data files, each to separate DVDs for storage and specific distribution purposes.

However, the moment I mention the dirty word "DVD", some people go bonkers because some people are dead set against the entire concept of DVD media (despite the fact that almost every desktop on the planet has the hardware, and so do most laptops except the newest thinnest ones, and despite the fact that they store nicely in stacks with well-written large labels, which USB sticks can't compete with in organization and where USB sticks can't compare in terms of 5GB quantum cost when distributed to others in the snail mail system).

If I only needed a single DVD-size-limited folder, then I could just format an open DVD and slide the files on ad-hoc, or I could even use the Windows "recycle bin" which happens to be able to be size limited. And, if I wanted to, I could buy a dozen DVD burners, and hook them up on a USB hub to accomplish the simultaneous task of filling each "folder" with data over the coming weeks. But that's slow, and crazy (and can't be undone when inevitable mistakes are made).

Could I just use a non-size-limited folder? Yes. But that's so much of a pain that anyone who suggests that hasn't actually ever done it, at least not with large files and many folders. Right clicking and checking the size is a PITA in many ways. Better to have the folder itself limited in size.

Could I just use a series of DVD-size-limited flash drives? Yes. But adding superfluous hardware is not the goal here since the goal is to use the existing terabyte of disk space and not purchasing a handful of slow flash cards that have their own cost and labeling and file-corruption problems.

Could I just use third-party archival software and/or third-party DVD burner software that makes a series of discs, e.g., disc 1 of 20, disc 2 of 20, disc 3 of 20, etc.? Sure, but each disc has to stand on its own while the third party tools adds its own level of complexity to what should be a simple task - which is to limit the size of folders to less than DVD size and let the Guest OS tell me when I'm getting full.
socratis wrote:If you want something size limited, you could very well create additional virtual hard drives from VirtualBox, with the required size, and attach them to your VM. They will appear as additional hard drives in your guest. E:\, F:\, etc.
Ah. This might be the best idea yet. I will look up how to create "virtual hard drives from VirtualBox".
Hopefully, they will not have the corruption problem that Microsoft states is the problem with the Microsoft Virtual Hard Drive mechanism.
Last edited by woodrock on 30. Jun 2017, 07:48, edited 4 times in total.
socratis
Site Moderator
Posts: 27329
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: Best way to force VirtualBox to execute a batch script before shutting down a Windows Guest OS?

Post by socratis »

woodrock wrote:I will look up how to create "virtual hard drives from VirtualBox".
You just add a new disk to the VM. Up to 4 if I'm not mistaken for an IDE controller and up to 30 for SATA.

One question that just hit me: did you EVER have a corruption problem or is this a completely theoretical scenario that you're trying to avoid? My gut feeling is, never.
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.
woodrock
Posts: 26
Joined: 9. Jun 2017, 08:17

Re: Best way to force VirtualBox to execute a batch script before shutting down a Windows Guest OS?

Post by woodrock »

socratis wrote:You just add a new disk to the VM. Up to 4 if I'm not mistaken for an IDE controller and up to 30 for SATA.
The controller is in a desktop with a single 3.5-inch 1TB disk whose controller is an "AMD AHCI Compatible RAID Controller".
socratis wrote:One question that just hit me: did you EVER have a corruption problem or is this a completely theoretical scenario that you're trying to avoid? My gut feeling is, never.
Over the years, I've personally witnessed far too many corruptions from pulling out USB-mounted external hard drives without "safely removing hardware", so I don't doubt Microsoft when they say that "restarting" the Guest OS will cause corruptions in data stored within Virtual Hard Disk mounted volumes.

I have gotten the setup to work, but who wants to test corruption of valuable data when Microsoft clearly says "You should always detach VHDs prior to restarting the operating system to prevent data corruption"?

I have scoured the Internet to figure out WHAT would cause that data corruption; but I don't know.
Microsoft knows. But I don't know.
Martin
Volunteer
Posts: 2562
Joined: 30. May 2007, 18:05
Primary OS: Fedora other
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: XP, Win7, Win10, Linux, OS/2

Re: Best way to force VirtualBox to execute a batch script before shutting down a Windows Guest OS?

Post by Martin »

woodrock wrote:
socratis wrote:You just add a new disk to the VM. Up to 4 if I'm not mistaken for an IDE controller and up to 30 for SATA.
The controller is in a desktop with a single 3.5-inch 1TB disk whose controller is an "AMD AHCI Compatible RAID Controller".
The controller of your hardware is not relevant here. ;)
We are talking about the possible virtual connectors on the virtual disk controller in the guest VM.
The additional "disks" will just be additional vdi files in the guest folder on your host system.
woodrock
Posts: 26
Joined: 9. Jun 2017, 08:17

Re: Best way to force VirtualBox to execute a batch script before shutting down a Windows Guest OS?

Post by woodrock »

Martin wrote:The controller of your hardware is not relevant here.
Thanks for the clarification, as I'm thoroughly confused about what VirtualBox "thinks" is the controller since I get different answers out of VirtualBox which just confuse me about which is the right answer for the question asked.
Image
Image
Martin wrote:We are talking about the possible virtual connectors on the virtual disk controller in the guest VM.
Thanks. While the instructions are clear (i.e., "You just add a new disk to the VM"), it's not obvious how to do that inside of VirtualBox.
Image
Martin wrote:The additional "disks" will just be additional vdi files in the guest folder on your host system.
Thanks. Looking up "how to add a new disk to virtualbox", I find this documentation and the following tutorials - but they're all dated:
1. How-to: Create New Virtual Disks in VirtualBox, 2012
2. How to add a second hard disk to a virtual machine in Virtualbox, 2012
3. Understanding and Configuring VirtualBox Virtual Hard Disks, 2009
4. How to add hard disks in VirtualBox - Tutorial, 2009
5. How to Add SATA Hard Disk to Virtual Machine in VirtualBox, 2009
6. Add Disk Storage to Oracle Virtual Box, undated but it looks old

Do you know of a tutoral that isn't half a decade old for doing what is suggested?
mpack
Site Moderator
Posts: 39134
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Mostly XP

Re: Best way to force VirtualBox to execute a batch script before shutting down a Windows Guest OS?

Post by mpack »

The date of a tutorial is not relevant. The provenance is.

If a tutorial is present on this site (see Howtos and Tutorials section) then you can assume it's current, unless you care to point out any errors. We don't repeat them every year just to stay fashionable, but we do edit them when needed.

Most of us don't pay any attention to tutorials not on this site.

And that's assuming that the user manual is not enough. Frankly, for the simple task of adding disks to a VM, the user manual should be ample guidance, and is of course always up to date.
scottgus1
Site Moderator
Posts: 20945
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Windows, Linux

Re: Best way to force VirtualBox to execute a batch script before shutting down a Windows Guest OS?

Post by scottgus1 »

Woodrock, always remember that the hardware presented to the guest OS is, with exception of the CPU, completely virtual. All of the hardware is emulated in the Virtualbox software. So you hard disk controller for the guest can be something completely different from your host PC's hardware. In fact you can put things in the guest you may have never even heard of, much less actually own.

When you make a new guest match the guest type to your guest OS, Virtualbox matches the virtual hardware to the types of hardware the guest OS would have drivers for. So in your screenshots above, the Windows 10 home guest has both the virtual drive file and the virtual CD drive on ports 0 and 1 of the virtual SATA controller presented to your guest. Your host can have only IDE drives or even only SCSI drives, and your 10 guest will happily run on the virtual SATA controller presented to it. Same for the Ubuntu guest. It has a virtual SATA and virtual IDE. The actual hardware your host uses has nothing to do with what your guest uses.

For your project, add more 4.5GB-sized drives to the SATA ports on your guest. You can have up to 32 virtual drives on the virtual SATA controller, numbered 0 to 31. You can also have 4 drives on the IDE controller if you find you need more. Your guest OS may even allow more IDE and SATA controllers. Be sure to NOT move your guest's boot drive from the controller slot it is on now.

Note that if you go over 26 drives total, including the boot and CD drives, you will run out of letters in the alphabet. You can mount drives as folders appearing on another drive, then you can mount as many as you have.

A point on drive size: When I had to back up lots of music once I chose DVDs. I had failed files at the very end of the burn process where the laser got to the end of the burnable media, and I had to start dropping a couple hundred MB off each DVD to get consistent good burns. be aware of this when you size your virtual drives.
woodrock
Posts: 26
Joined: 9. Jun 2017, 08:17

Re: Best way to force VirtualBox to execute a batch script before shutting down a Windows Guest OS?

Post by woodrock »

scottgus1 wrote:the hardware presented to the guest OS is, with exception of the CPU, completely virtual
Thanks for that clarification as the results, taken on face value alone, were confusing.
scottgus1 wrote:You can have up to 32 virtual drives on the virtual SATA controller, numbered 0 to 31.
Thanks for confirming that the virtual SATA controller can handle 32 virtual drives (of which I'll need only about a score).
scottgus1 wrote:Note that if you go over 26 drives total, including the boot and CD drives, you will run out of letters in the alphabet.
I don't envision more than about a score of dvd-size-limited "folders" on the desktop.
scottgus1 wrote:I had to start dropping a couple hundred MB off each DVD to get consistent good burns. be aware of this when you size your virtual drives.
This is great advice. Since archival reliability is a factor, I'll aim for about 4300 MB for my size limitation on the desktop "folders".
I'll have to experiment with how large that means the virtual drive will need to be, since I expect "overhead" to consume space, so that probably means something like 4400MB or 4500MB-sized virtual drives.
Post Reply