Page 1 of 1

Controlling a VM with a BATCH Script

Posted: 13. Nov 2013, 19:09
by chrislynch8
Hi,

I have a VM that I have install DOS6.22 on to run a real old Business Application that is required. I am running the VM on a Windows 7 Host. At the moment I am performing the following steps to transfer Data to/from my new VM

1: Mount my VHD and copy files to it
2: Unmount my VHD
3: Start my VM in Virtual Box
4: Do the work that is required
5: Closed the VM
6: Mount the VHD and copy new files off
7: Unmount the VHD

I want to create a Simple BATCH Script that will manage all of the above for me. Mounting/Unmounting the VHD and copying files to/from the VHD is OK and working. My Problem is that when I issue the VBOXMANAGE.EXE command to start my VM, once the VM is starting it will give control back to the BAT File. Is there an option when starting the VM to not give control back to the BAT file until the opened VM is closed again, so when the BAT sees the User has closed the VM it will continue working.

Rgds
Chris

Re: Controlling a VM with a BATCH Script

Posted: 13. Nov 2013, 20:20
by mpack
The way to get a batch command to wait for an asynch process to terminate is to precede the normal console command with "start /wait", e.g. "start /wait VBoxManage ...etc".

One complication is that when VBoxManage starts a VM, that VM runs as a separate process from VBoxManage, so VBoxManage terminating doesn't mean that the VM has. Offhand I don't know of any way to get VBoxManage to wait for a VM to close. The only way I can think to do it is to have your own host tool that looks for the "VBoxSVC" process in the host task list.

Re: Controlling a VM with a BATCH Script

Posted: 13. Nov 2013, 20:53
by noteirak
Directly call the front-end that you require, which won't return until the VM process is finished.

Re: Controlling a VM with a BATCH Script

Posted: 14. Nov 2013, 02:14
by mpack
noteirak wrote:Directly call the front-end that you require, which won't return until the VM process is finished.
Sorry, but I'm pretty sure that isn't true. For example you can launch a VM from the GUI front end, then terminate the GUI app - the VM continues running. I don't see why the same would be untrue of VBoxManage.

On Windows hosts the VBoxSVC process would be spawned (if not already running), and a VirtualBox.exe process for the VM itself. The front end (whatever it is) is then free to terminate.

Re: Controlling a VM with a BATCH Script

Posted: 14. Nov 2013, 11:33
by noteirak
VBoxSVC will run whenever anything that requires the Virtualbox API is running. So if more than one VM is running, or that the Web-services is running, or anything of the like, it will NOT terminate.
So there is no way to wait for a specific VM using that way.
About the front-end, you are right about the Virtualbox one - it returns directly and spawn another process. On the other hand, vboxheadless does not return until the VM is Powered Off.

I guess it depends which front-end the OP wants to use now.

Re: Controlling a VM with a BATCH Script

Posted: 14. Nov 2013, 13:38
by chrislynch8
Hi all,

Thanks for the updates. I am directly calling the Front End I require with the following command

VBoxManage.exe "Dos" --type gui

This executes, starts the VM which is another process which is why the Script will continue. As far as it is concerned the VBoxMange.exe is finished.

So vboxmanage.exe starts the VBoxSVC.exe which spawns VirtualBox.exe

I'll need to think about this some more so.

Re: Controlling a VM with a BATCH Script

Posted: 14. Nov 2013, 13:43
by mpack
chrislynch8 wrote:So vboxmanage.exe starts the VBoxSVC.exe which spawns VirtualBox.exe.
Not quite. VBoxManage starts VirtualBox.exe, which is the VM. The VM relies on certain common services (e.g. virtual networking) which require VBoxSVC to be running, hence if it was not already running VBoxSVC will start. VBoxSVC terminates itself a few seconds after the last VM closes.