12 September 2012

Virtual Box Console Commands

These are some notes about commands I had to dig out to operate my VirtualBox image through the command line (since the GUI for it segfaults on start).
For the purpose of this article, I am using [machine_name] as the name you have given during the creation of the virtual machine. Also, this article does not describe the actual creation of the virtualbox machine and image for attached storage, as those can vary quite a bit, depending on your needs and situation. This just gives some tips about how to operate your machines when you need to work with them.

Before we start, all the virtualbox modules should be loaded in. The following command makes sure that is done:
for m in vbox{drv,netadp,netflt}; do modprobe $m; done

Create Virtual Machine

VBoxManage createvm -name "[machine_name]" -register --ostype Windows7 --basefolder /mnt/disks

Adjust settings

VBoxManage modifyvm [machine_name] --memory 2048

Set network in bridged mode

VBoxManage modifyvm [machine_name] --nic1 bridged --bridgeadapter1 eth0
or for running OS:
VBoxManage controlvm [machine_name] nic1 bridged eth0

Attach Storage controller

VBoxManage storagectl [machine_name] --name "SATA Controller" --add sata --controller IntelAHCI --bootable on

Create and attach disk

VBoxManage createhd --filename /mnt/disks/[machine_name]/root.vdi --format VDI --size 20480
VBoxManage storageattach [machine_name] --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium /mnt/disks/[machine_name]/root.vdi



Attach usb devices

VBoxManage list usbhost
VBoxManage controlvm [machine_name] usbattach [UUID]



Attach usb devices permanently

VBoxManage list usbfilters
VBoxManage usbfilter add 1 --target [machine_name] --name Mouse --vendorid [linux vendor id] --productid [linux product id]
VBoxManage controlvm [machine_name] usbattach [UUID]




To start the virtual machine (in headless mode), you can use this command:

VBoxManage startvm [machine_name] --type headless

If you want to access a GUI during the booting process, you can enable the buildin RDP with:
VBoxManage controlvm [machine_name] vrde on

And then connect with any Remote Desktop application to it on the server (at default port 3389).

When installing/modifying OS, it is often handy to change the booting device.

This will let you set the DVD to an ISO image of your choice (the name of my storagectl is "SATA Controller", but that is something you could have set to anything):
VBoxManage storageattach [machine_name] --storagectl "SATA Controller" --port 1 --device 0 --type dvddrive --medium /mnt/video/disks/VBoxGuestAdditions_4.1.20.iso

To mount the system's CD-ROM:
VBoxManage storageattach [machine_name] --storagectl "SATA Controller" --port 1 --device 0 --type dvddrive --medium Host:/dev/cdrom

To "eject" the ISO:
VBoxManage storageattach [machine_name] --storagectl "SATA Controller" --port 1 --device 0 --type dvddrive --medium emptydrive

This commands help set the DVD as a booting drive:
VBoxManage modifyvm [machine_name] --boot1 dvd

And change it back to HDD:
VBoxManage modifyvm [machine_name] --boot1 disk


modifyvm is also the command to change a lot of the virtual machine's settings, like how much RAM is allocated, what OS is going to be installed on it and other similar hardware and other settings. The virtual machine has to be power off for most of those changes to be made.


To shutdown the machine, best option would be to emulate the pressing of the power button, which should trigger the OS shutdown procedures:
VBoxManage controlvm [machine_name] acpipowerbutton

If that doesn't do the job, you can do a hard plug with:
VBoxManage controlvm [machine_name] poweroff
To see the settings of a machine:
VBoxManage showvminfo [machine_name]

Show running virtual machines
VBoxManage list runningvms

To monitor the operation of a virtual machine, you can enable metrics:
VBoxManage metrics setup --period [refresh seconds] --samples [samples to keep] [machine_name]
as
VBoxManage metrics setup --period 1 --samples 1 [machine_name]

Show all statistics:
VBoxManage metrics query [machine_name]
Look CPU stats:
VBoxManage metrics query [machine_name] CPU/Load/User
Check RAM stats:
VBoxManage metrics query [machine_name] RAM/Usage/Used


This is all I've needed so far to operate virtual machines through the command line for all intents and purposes.

No comments: