Virtualbox Command Line Utility

From Joe's Boredom
Revision as of 17:59, 6 December 2020 by Joe (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The following Bash script was created to make controlling Virtualbox from the command line a little easier.

This script was created while using Virtualbox 3.1.6
Tested on:

  • Virtualbox 3.1.6


Configure

The script itself has one config setting: 'viewYNinMenu'
After any/all option menus are displayed, you can have a second question asked: "You choose '$this' (y/n)?"
Setting 'viewYNinMenu' to 1 will enable this second question, setting 'viewYNinMenu' to 0 the second question will not be asked.


Main Menu Options

File:Vbox utily mainmenu.jpg

Create New vm Guest

Function called createNewVM()

You will be asked a few questions

  • New guest name
  • Menu: What OS (choices from VBoxManage list ostypes)

File:Vbox utily select os.jpg

  • Path to .iso
  • Memory Size
  • Harddrive Size
  • .vdi file name (or file name and path if you want to store the .vdi file else where) Default: ~/.VirtualBox/HardDisks/
  • The variables at the top of the createNewVM() functon will be used for the rest of the options
    boot1, boot2, clipboard, acpi, ioapic, pae, nestedpaging, cpus, synthcpu, hwvirtex, vtxvpid, snapshotfolder, vram, accelerate3d, accelerate2dvideo, vrdp, vrdpport, vrdpauthtype, vrdpmulticon, storagectl, storagect2, floppy, usb, usbehci, audio, audiocontroller, nic1, nictype1, cableconnected1

After the questions are asked, the script will display your answers with a final prompt to create. Then you will be asked if you want to Start the guest (in "Headless" mode) VBoxHeadless -startvm [name]


DELETE Guest

............


Power Management

Uses selectPowerManagement()

Questions asked:

  • Menu: What guest to work with
  • Menu: Power Options

File:Vbox utily powermangmant.jpg


Add DVD iso

..............


Add shared folder

Calls workWithShareFolders()
Lets you add a new Shared Folder to a guest.

Questions asked:

  • Menu: What guest to work with
  • Host path to Shared folder
  • Share folder name
  • readonly
  • transient
  • Then the final "do you want to do this (y/n)"


New shared folders can only be added to guests which are not running.
If the guest is running you will be prompted:
File:Vbox utily sharedfolder.jpg


Add storage (harddrive)

...........


Remove DVD iso

...............


Remove shared folder

Calls workWithShareFolders()
Lets you remove a Shared Folder from a guest.

Questions asked:

  • Menu: What guest to work with
  • Menu: Listing the names of the current shared folders
  • transient
  • Then the final "do you want to do this (y/n)"

Just like adding a new Shared Folder, guest must be off.


Remove storage (harddrive)

...........


Select Guest and showvminfo

Calls askWhatVM_thenDisplayInfo() <source lang="bash">askWhatVM_thenDisplayInfo "Select guest to view" 1</source>


Display "VBoxManage list" options

Displays menu for VBoxManage list
This can be edited in the displayVBoxManageList()
File:Vbox utily vboxmanage list.jpg


Command line

Lets you work with the terminal command line with-out exiting the script


Functions

startUpQuestions()

askWhatVM_thenDisplayInfo()

This function will:

  1. Create a Menu parsing VBoxManage list vms out-put and displaying the names of the Guests installed on the host.

File:VboxUtily select guest.jpg

  1. "Returns" some information
  2. Display VBoxManage showvminfo [vmaname] if argument is passed


Arguments passed into function:

  • Question (string)
    This function can be called for different reasons so we pass the "question" to it
  • Display VBoxManage showvminfo [vmaname] (int)
    Thought an option to display the Guest info would be nice.


Returns an "Array"

  • arr[0] = Name
  • arr[1] = UUID
  • arr[2] = 1st Controller
  • arr[3] = 2nd Controller
  • arr[4] = Shared folders
  • arr[5] = State (running, saved, off, etc.)

Usage: <source lang="bash">

  1. mock-up
  2. Just ask question and get results with out displaying 'VBoxManage showvminfo'

tmpArr=( $(askWhatVM_thenDisplayInfo "Add iso to DVD for ?") ) vmGuestName="${tmpArr[0]}" vmGuestUUID="${tmpArr[1]}" ...... ...... VBoxManage storageattach "$vmGuestUUID".... </source> OR <source lang="bash">

  1. Display info from 'VBoxManage showvminfo'

askWhatVM_thenDisplayInfo "Display info for guest ?" 1 ; </source>


displayVBoxManageList()

workWithShareFolders()

This function will first display a menu to choose what guest to work with via the askWhatVM_thenDisplayInfo() function. After it gets the results from askWhatVM_thenDisplayInfo it will do a check to see what 'state' the guest is in, if guest is not in the 'powered off' state, another menu will appear where you can turn off the guest with one of the VBoxManage controlvm options (pause|resume|reset|poweroff|savestate|acpipowerbutton|acpisleepbutton).

After machine is turned off a list of current shared folders will be displayed and depending on why you are calling this function (adding a new Share or removing a Share) you will be asked acouple questions.

Adding Shared Folder:

  • Host path to Shared folder
  • Share folder name
  • readonly
  • transient
  • Then the final "do you want to do this (y/n)"

Removing Shared Folder:

  • A menu will appear listing the names of the current shared folders
  • transient
  • Then the final "do you want to do this (y/n)"

If you select "no" to the "do you want to do this (y/n)" questions, you will be brought back to the main menu.


Arguments passed into function:

  • "todo" (string) - adding or removing


workWithDVD()

Calls

Arguments passed into function:

  • "Question" (string)
  • "todo" (string)


createNewVM()

At the top of this function you ca set a bunch of variables which will be used during the creation of a new vm guest.


choose_OSfromList()

askQuestion()

displayOptions()

selectPowerManagement()

Calls askWhatVM_thenDisplayInfo() and powerOffSaveState()


powerOffSaveState()

Calls displayOptions() and loopPowerState()


loopPowerState()

Arguments passed into function:

  • vmID (string)
  • stateOrg (string)

When this function is called it will loop VBoxManage showvminfo [vmID], grab the current 'state' of the guest and compare that to the passed 'stateOrg'. When the current state is different then the passed 'stateOrg' it will break the loop.

This is used mainly for shutting down a machine with VBoxManage controlvm [name] acpipowerbutton during changes to a guest which require the guest to be turned off. Since it takes alittle while for a guest to shut down, this loop let you know when the guest is off.



Thanks to

geirha
Roughly a year ago i made the switch to Ubuntu (from Windows). Through that time I've been learning Bash when time permits and for this "project", geirha has been a great help and has taught me so much. Thank you.


The script

<source lang="bash">

  1. !/bin/bash

</source>