Page 1 of 1

Custom Batch file to launch VM

Posted: 22. May 2014, 22:57
by Noremac430
***UPDATE***
SOLVED!! See my last post for details.


Hey all,

Forgive me if I'm posting in the wrong area.

I am attempting to write a batch script that does the following:

1. Check for Vbox installation
2. Check if appliance has already been installed - Import if not found (appliance stored remotely)
3. Check for the vmdk's - if not found, attach them (stored remotely) otherwise goto Step 4.
4. Start the VM

Now, I have a working script that I wrote today but it does not execute Step 3 from the above list.
For further background: The idea here is to have an immutable vmdk as this will be an on-demand development machine for a large team of devs. Simply a place for them to quickly bring up an environment and check their code. Hence the immutable vmdk.

What my current script does is when it finds an already installed instance of the Guest VM, it still attempts to attach the storage, so my Virtual Media Manager is getting filled up with tons of parent and child snapshots (I'm using the --setuuid "" command in my script to generate a new random uuid on each launch).
I understand that the setuuid parameter is my main problem and I'd rather not use that. but I don't know how to check to see if the users local install of VBox has the correct storage attached already.

Here is the first revision of the script I wrote today:

Code: Select all

@echo off
Title Configure VBox and launch VM
REM Script to configure and launch a VM with remote storage.

:START
echo Checking for virtual Box...
IF EXIST C:\progra~1\Oracle\VirtualBox\VirtualBox.exe goto STARTCONFIG
goto VBOXERROR

:STARTCONFIG
echo Starting vbox Import...
CD C:\progra~1\Oracle\VirtualBox\

IF EXIST C:\Users\%USERNAME%\Virtua~2\JavaTesting\JavaTesting.vbox goto SKIPCONFIG
echo.
echo.
echo VM Not Found. Beginning import...
echo.
echo.
pause
REM Import the appliance and attach the storage to it.
vboxmanage import \\w7-2ua22514sf\VMTesting\JavaTest1.ova --vsys 0 --vmname JavaTesting --memory 2048
vboxmanage storageattach JavaTesting --storagectl "SATA Controller" --port 0 --type hdd --medium \\w7-2ua22514sf\VMTesting\JavaTestingDisk1.vmdk --mtype immutable --setuuid ""
vboxmanage storageattach JavaTesting --storagectl "SATA Controller" --port 1 --type hdd --medium \\w7-2ua22514sf\VMTesting\JavaTestingDisk2.vmdk --mtype immutable --setuuid ""
vboxmanage startvm JavaTesting

goto END

:SKIPCONFIG
cls
REM Found the VM.  Attach the storage.  (I need to check for storage first..How to?)
echo VM already exists. Starting Storage Configuration...
vboxmanage storageattach JavaTesting --storagectl "SATA Controller" --port 0 --type hdd --medium \\w7-2ua22514sf\VMTesting\JavaTestingDisk1.vmdk --mtype immutable --setuuid ""
vboxmanage storageattach JavaTesting --storagectl "SATA Controller" --port 1 --type hdd --medium \\w7-2ua22514sf\VMTesting\JavaTestingDisk2.vmdk --mtype immutable --setuuid ""
vboxmanage startvm JavaTesting
goto END

:VBOXERROR
cls
echo Virtual Box is not installed. You cannot continue.
goto END

:END
REM pause
Again, when there is no storage attached and no VM already created, this script works like a dream. It's launching it a 2nd, 3rd, 4th, etc. time that issues arise.

I hope I've explained this well enough.

Any help is greatly appreciated!

Re: Custom Batch file to launch VM

Posted: 23. May 2014, 14:53
by noteirak
Noremac430 wrote:I don't know how to check to see if the users local install of VBox has the correct storage attached already.
You have two options :
1. Retrieve the VM info using vboxmanage showvminfo and then check for the disk UUID in the output. if you get a return, it is attached.
2. Retrieve the info of the disk using vboxmanage list hdds and check if the disk is registered, and to which VM the disk is attached to (if it is).

You can then choose what to do.

Re: Custom Batch file to launch VM

Posted: 23. May 2014, 15:20
by Noremac430
That makes sense, I guess the problem I'm running into is that I'm not sure how to check those things within the batch file. I'm no expert coder. Are there any snippits around that would be useful?

Re: Custom Batch file to launch VM

Posted: 23. May 2014, 17:38
by Noremac430
I think I have found the answer to my question. I'm working on it now. I'll post the results when it's complete.

Re: Custom Batch file to launch VM

Posted: 23. May 2014, 19:27
by Noremac430
Success!!
So while I know there is still some cleanup to do in this file, the functionality I wanted is now there and working beautifully on multiple host machines.

I hope this will be a good starting point for anyone else that might need this type of help.
Thank you all for your help!

Code: Select all

@echo off
Title Configure VBox and launch VM
REM Script to configure and launch a VM with remote storage.

:BEGIN
REM ========== Variables ===========
SET VBoxDisk1="21af0464-ac03-4a23-beaf-82461bef042a"
SET VBoxDisk2="4fc82a11-4074-4df9-aa8f-473bb38aee74"
SET disk1found=""
SET disk2found=""
SET vboxpath="C:\progra~1\Oracle\VirtualBox"
SET VBoxManage="%vboxpath%\VBoxManage.exe"
SET VBREmoteDir="\\w7-2ua22514sf\VMTesting"
REM ======== End Variables =========

:START
echo.
echo.
echo Checking for virtual Box...
echo.
echo.
IF EXIST %vboxpath%\VirtualBox.exe echo VirtualBox Found! & goto STARTIMPORT
goto VBOXERROR

:STARTIMPORT
CD C:\progra~1\Oracle\VirtualBox
echo.
echo.
echo Checking for VM...
echo.
echo.
REM TODO: Make this next line more robust with the FOR method.
IF EXIST C:\Users\%USERNAME%\Virtua~2\JavaTesting\JavaTesting.vbox echo VM Found! & goto DISK1SEARCH
echo.
echo.
echo VM Not Found. Beginning import...
echo.
echo.
REM Import the appliance and attach the storage to it.
vboxmanage import %VBREmoteDir%\JavaTest1.ova --vsys 0 --vmname JavaTesting --memory 2048

:DISK1SEARCH
FOR /F "usebackq tokens=2"  %%i IN (`vboxmanage list hdds`) DO (
	IF "%%i"==%VBoxDisk1% (
		SET disk1found="1"
		goto DISK2SEARCH
	)
)
vboxmanage storageattach JavaTesting --storagectl "SATA Controller" --port 0 --type hdd --medium \\w7-2ua22514sf\VMTesting\JavaTestingDisk1.vmdk --mtype immutable

:DISK2SEARCH
FOR /F "usebackq tokens=2"  %%i IN (`vboxmanage list hdds`) DO (
	IF "%%i"==%VBoxDisk2% (
		SET disk2found="1"
		goto SKIPIMPORT
	)
)
vboxmanage storageattach JavaTesting --storagectl "SATA Controller" --port 1 --type hdd --medium \\w7-2ua22514sf\VMTesting\JavaTestingDisk2.vmdk --mtype immutable

:SKIPIMPORT
cls
REM Found the VM.  Attach the storage.
echo.
echo.
echo VM already exists. Checking for storage...
echo.
echo.
IF %disk1found%=="1" echo Disk 1 Found!
IF %disk2found%=="1" echo Disk 2 Found! & GOTO DISKFOUND
GOTO DISK1SEARCH

:DISKFOUND
Echo Storage found! Starting VM...
GOTO STARTVM

:STARTVM
vboxmanage startvm "JavaTesting"
goto ENDSUCCESS

:VBOXERROR
cls
echo -----------------------------------------------------
echo.
echo Virtual Box is not installed. You cannot continue.
echo.
echo -----------------------------------------------------
goto ENDERROR

:ENDERROR
pause
GOTO :EOF

:ENDSUCCESS