Virtualbox management commands
<meta name="keywords" content="Virtual Box, VirtualBox, virtualbox, virtual box, command line, build vm, vuild virtual machine, remove hdd, unregester hdd, unregister harddisk, unregister virtual machine, headless mode, build script, virtualbox commands Virtualbox management commands"></meta> <meta name="description" content="Full Control of VirtualBox with the command line"></meta>
Assume the vm guest name is CentOS
Contents |
[edit] General Commands
[edit] Find System ID
(you can also use system name for the commands)
VBoxManage list vms
[edit] Show details about vm
VBoxManage showvminfo "CentOS"
[edit] Start VM
VBoxManage startvm "CentOS"
[edit] Start in Headless Mode
This automatically enables the vrdp mode (rdesktop)
VBoxHeadless --startvm "CentOS"
[edit] You can also set a specif port
VBoxHeadless -s "CentOS" -p 3399
To use this mode connect the rdp client to the ip address of the HOST server and specify the proper port
[edit] Start without vrdp (ssh only access)
VBoxHeadless --startvm CentOS --vrdp=off
[edit] Stop VM
VBoxManage controlvm "CentOS" poweroff
[edit] Take Snapshot
- Note: the snap1-stable-system is a cosmetic name
take <name> [--description <desc>]
VBoxManage snapshot "CentOS" take snap1-stable-system
[edit] Revert Snapshot
- Note: Remember to stop the virtual machine for best practice.
To Discard current changes
VBoxManage snapshot "CentOS" discardcurrent -state
Revert to last snapshot:
VBoxManage snapshot "CentOS" discardcurrent -all
[edit] Discard a specific Snapshot or system state
*NOTE: Use Extreme caution this logic here is weird
Use showvminfo command to see Snapshot information
VBoxManage snapshot "CentOS" discard <SnapName>
or
VBoxManage snapshot "CentOS" discard <UID>
[edit] Global Settings
[edit] Change HardDisks Storage Location
Command is:
VBoxManage setproperty hdfolder </file/location>
IE:
VBoxManage setproperty hdfolder /data/vbox/HardDisks
To revert to the default location use:
VBoxManage setproperty hdfolder default
[edit] Change Machine Storage Location
Command is:
VBoxManage setproperty machinefolder </file/location>
IE:
VBoxManage setproperty machinefolder /data/vbox/Machines/
To revert to the default location use:
VBoxManage setproperty machinefolder default
[edit] Move VM to new host server
Verify settings and get information:
VBoxManage showvminfo w2k3 |grep -e 'DVD:' -e 'Floppy:' -e 'SATA ' -e 'UUID' -e 'Name:' -e 'Config file:' -e 'State:' -e 'USB:'
- Note: Make sure all secondary devices are detached (USB, Floppy, DVD etc)
- Note: You may want to output this to a file for reference by adding a > tomove.txt at the end.
Detach the primary drives
VBoxManage modifyvm "w2k3" --sataport1 none
- Note: Increment the port number as needed
Verify with:
VBoxManage showvminfo w2k3 |grep -e 'SATA:'
Should be blank
Unregister the Harddisks
VBoxManage unregisterimage disk <UUID>
- Note: Get the UUID from the output of first command
Unregister the Virtual Machine
VBoxManage unregistervm w2k3
Handle any Snapshots. CAUTION Snaps will be lost with the following command.
VBoxManage snapshot w2k3 discard <SNAP UUID>
Unregister VM
VBoxManage unregistervm w2k3
Copy Machine and HardDisks
rsync -avP /current/location/.VirtualBox/Machines/w2k3 root@192.168.0.150:/data/vbox/Machines/ rsync -avP /current/location/.VirtualBox/HardDisks/w2k3_system.vdi root@<IP ADDRESS>:/new/location/HardDisks/
ON NEW MACHINE
Register the VM
VBoxManage registervm /data/vbox/Machines/w2k3/w2k3.xml
Register the Harddisks
VBoxManage openmedium disk --type normal w2k3_system.vdi
Attach Hardisks
- NOTE: Run for all HardDisks, Remember to invrement the port number as needed (use source output file to match if needed)
VBoxManage modifyvm "w2k3" --sataportcount 8 --sataport1 "w2k3_system.vdi"
Find the rdesktop port binding (VRDBPort)
cat /data/vbox/Machines/w2k3/w2k3.xml |grep VRDPBindPort | cut -d"=" -f 3 |cut -b 2-5
Start VM
VBoxHeadless --startvm "w2k3"
[edit] Duplicate (Clone) Drive
To duplicate an existing virtualbox machine use the VBoxManage clonehd command
VBoxManage clonehd /source/harddrive/location/CentOS.vdi /target/location/Clonediskname.vdi --format VDI --variant Standard --type normal
You can also use the disk UID but that will name the targe copy the UID.vdi file name
Here is a list of options:
Usage:
VBoxManage clonehd <uuid>|<filename> <outputfile>
[--format VDI|VMDK|VHD|RAW|<other>]
[--variant Standard,Fixed,Split2G,Stream,ESX]
[--type normal|writethrough|immutable]
[--remember] [--existing]
This will run for a little while be patent there will be status info along the way 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
After duplication simply create a new machine and attach then new disk (verify permissions)
[edit] Create Virtual Machine Guest
This example is for Debian, with SATA drives and Bridged network
Create new machine
VBoxManage createvm -name "Debian" -ostype "Debian" --register
Set Machine settings
VBoxManage modifyvm "Debian" --memory "512 MB" --acpi on --boot1 dvd --vrdpport 3399
Set Network Settings (bridged)
VBoxManage modifyvm "Debian" --cableconnected1 on --nic1 bridged --nictype1 82543GC -hostifdev1 eth0
Create Virtual HardDisk (10 GB raw)
VBoxManage createhd --filename "deb_main.vdi" --size 10000 --format VDI --variant Standard --remember
Create others as needed. Make sure to give them new names
Attach HardDisk (SATA)
First
VBoxManage modifyvm "Debian" --sata on --sataportcount 8 --sataport1 "deb_main.vdi"
Additional
- Note remember to increment the --sataport number 2.3.4...
VBoxManage modifyvm "Debian" --sataportcount 8 --sataport2 "deb_apache.vdi"
Register the iso with the OS
VBoxManage openmedium dvd /full/path/to/debian.iso
Attach ISO to Virtual Machine
VBoxManage modifyvm "Debian" --dvd /full/path/to/debian.iso
Start the Vm (headless)
VBoxHeadless --startvm "Debian"
[edit] Post Install Steps
Remove DVD from Virtual Machine
VBoxManage modifyvm "Debian" --dvd none
[edit] Install Client Tools
While guest system is running run this command on the host terminal
Ensure dvd drive is empty
VBoxManage controlvm "Debian" dvdattach none
Mount Client Tools DVD
- Note: The path may vary just search for the ISO
VBoxManage controlvm "Debian" dvdattach /usr/share/virtualbox/VBoxGuestAdditions.iso
Run the setup within the guest IE: for Linus
/media/cdrom/autorun.sh
Unmount the Client Tools DVD
VBoxManage controlvm "Debian" dvdattach none
[edit] Speed Builds
[edit] Command Line
[edit] Debian iso boot disk
Change the following parameters on each command
- <name>
- <harddisk>
- <memory>
- <port>
- </full/path/to/disk>
- Verify the eth1 port
VBoxManage createvm -name "<name>" -ostype "Linux" --register VBoxManage createhd --filename "<harddisk>.vdi" --size 10000 --format VDI --variant Standard --remember VBoxManage modifyvm "<name>" --sata on --sataportcount 8 --sataport1 "<harddisk>.vdi" --memory "<memory> MB" --acpi on --boot1 dvd --vrdpport <port> --cableconnected1 on --nic1 bridged --nictype1 82543GC -hostifdev1 eth0 --dvd </full/path/to/disk>.iso
[edit] CentOS PXE Boot
Change the following parameters
- <name>
- <harddisk>
- <memory>
- <port>
- Verify the eth1 port
VBoxManage createvm -name "<name>" -ostype "Linux" --register VBoxManage createhd --filename "<harddisk>.vdi" --size 10000 --format VDI --variant Standard --remember VBoxManage modifyvm "<name>" --sata on --sataportcount 8 --sataport1 "<harddisk>.vdi" --memory "<memory> MB" --acpi on --boot1 net --vrdpport <port> --cableconnected1 on --nic1 bridged --nictype1 82543GC -hostifdev1 eth0
[edit] CentOS iso boot disk
Change the following parameters
- <name>
- <harddisk>
- <memory>
- <port>
- </full/path/to/disk>
- Verify the eth1 port
VBoxManage createvm -name "<name>" -ostype "Linux" --register VBoxManage createhd --filename "<harddisk>.vdi" --size 10000 --format VDI --variant Standard --remember VBoxManage modifyvm "<name>" --sata on --sataportcount 8 --sataport1 "<harddisk>.vdi" --memory "<memory> MB" --acpi on --boot1 dvd --vrdpport <port> --cableconnected1 on --nic1 bridged --nictype1 82543GC -hostifdev1 eth0 --dvd </full/path/to/disk>.iso
[edit] Scripts
[edit] With iso boot
vi /scripts/vboxisobuild.sh chmod 777 /scripts/vboxisobuild.sh
#! /bin/bash # This will build a simple single hardrive server for iso install. # # Example Settings # me=Debian # harddisk=Debian # memory=256 # port=3390 # isopath=/tmp/debian-503-i386-netinst.iso ################################ name= harddisk= memory= port= isopath= #### Caution with Edits Below ### VBoxManage createvm -name "$name" -ostype "Debian" --register VBoxManage createhd --filename "$harddisk.vdi" --size 10000 --format VDI --variant Standard --remember VBoxManage modifyvm "$name" --sata on --sataportcount 8 --sataport1 "$harddisk.vdi" --memory "$memory MB" --acpi on --boot1 dvd --vrdpport $port --cableconnected1 on --nic1 bridged --nictype1 82543GC -hostifdev1 eth0 --dvd $isopath
[edit] Network (PXE) boot
vi /scripts/vboxnetbuild.sh chmod 777 /scripts/vboxnetbuild.sh
#! /bin/bash # This will build a simple single hardrive server for iso install. # # Example Settings # name=CentOS # harddisk=centos # memory=256 # port=3390 ################################ name= harddisk= memory= port= #### Caution with Edits Below ### VBoxManage createvm -name "$name" -ostype "Redhat" --register VBoxManage createhd --filename "$harddisk.vdi" --size 10000 --format VDI --variant Standard --remember VBoxManage modifyvm "$name" --sata on --sataportcount 8 --sataport1 "$harddisk.vdi" --memory "$memory MB" --acpi on --boot1 net --vrdpport $port --cableconnected1 on --nic1 bridged --nictype1 82543GC -hostifdev1 eth0
[edit] Remove Virtual Machines
The are a few steps here and the order of operations is critical
[edit] Remove all harddisks attached
Generally find teh UUID from the disk list
VBoxManage list hdds
[edit] For Sata
First find the UUID for the harddisks
VBoxManage showvminfo "debian" |grep SATA*
- Note: Adjust port number as needed to remove all harddisks
VBoxManage modifyvm "Debian" --sataport1 none
[edit] For hdd
First find the UUID for the harddisks
VBoxManage showvminfo "debian" |grep hd*
- Note: adjust hd(a) as needed to remove all harddisks
VBoxManage modifyvm "debian" --hda none
[edit] Unregister the harddisk
Need to use UUID here name may not always work
VBoxManage unregisterimage disk 38fe3756-0ec9-4f78-bca2-120ccab02d6d
[edit] Delete physical harddisk
rm -rf /location/of/HardDisk/disk.vdi
[edit] Unregister the Virtual Machine
VBoxManage unregistervm Debian
[edit] Sata on Windows
Reference Site This is for Windows XP/Server 2003 and earlier
You need the Intel driver from here or locally from here.
For Windows XP/2003, you need to extract the f6flpy32.zip Zip file. Inside it is an EXE; all it does is write an image to a real floppy; if you're already using Windows and want to make a real floppy disk, go ahead and use it, otherwise just use an UnZip utility to extract the image (named F32.IMA).
[edit] Attach a floppy Drive
[edit] Pre Boot
VBoxManage modifyvm w2k3_s01 --floppy /data/ISO/F32.IMA
[edit] Post Boot
VBoxManage controlvm w2k3_s01 --floppyattach /data/ISO/F32.IMA
[edit] Remove Floppy
VBoxManage modifyvm w2k3_s01 --floppy none
Just create a virtual machine now for Windows, and change the virtual hard disk so it uses SATA instead of the default IDE. Mount the floppy in VBox (either real or the disk image, the latter is faster), and make sure that the floppy drive is not set as one of the boot devices (or press F12 when you start the VM and boot the CD-ROM manually).
Press F6 when Windows Setup prompts you if you want to load additional drivers. It doesn't seem to take effect immediately, but just wait a few seconds. Once that is done, this screen will come up:
Press S, and select the second option "Intel(R) 82801HEM/HBM SATA AHCI Controller (Mobile ICH8M-E/M)" as the driver to load:
Then pres Enter to continue
[edit] SCSI Drives
Create normal Harddisk
VBoxManage createhd --filename "SCSI2.vdi" --size "4000" --format VDI --variant Standard --remember
[edit] Connect First SCSI Drive
VBoxManage modifyvm SCSITest --scsi on --scsitype LsiLogic --scsiport2 "SCSI2.vdi"
[edit] Connect additional Drives
- Note: Increment port as needed.
VBoxManage modifyvm SCSITest --scsiport2 "SCSI2.vdi"
[edit] Troubleshoot
[edit] Spanning Error
Can come after a system update
KillVirtual Box if needed
killall VirtualBox
Reconfigure
/etc/init.d/vboxdrv setup
Restart VirtualBox
[edit] VBox commands not working
Remember to run as a user that is in the vboxusers user group ie: probably not root
[edit] References:
http://www.virtualbox.org/manual/UserManual.html#vboxmanage