Hi,
VirtualBox offers a very powerful command inferface: vboxmanage. With vboxmanage you can create new virtual machine, add and modify harddisk and much more. All the things you could do in the GUI, and a lot more are also possible at command line. In this post I will create, modify, control and delete a complete virtual machine.
At the bottom of the post the whole script can be downloaded.
I used a debian linux system for my tests. To install the latest VirtualBox version use the following commands:
1 2 3 4 5 6 7 8 9 10 | echo "deb http://download.virtualbox.org/virtualbox/debian wheezy contrib non-free" >> /etc/apt/sources.list# Add Oracle Keyringwget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | apt-key add -# Update repositoriesapt-get update# Install latest virtualbox versionapt-get -y install virtualbox-4.3# Installing the Extentionpack for USB2 and RDP supportwget http://download.virtualbox.org/virtualbox/4.3.6/Oracle_VM_VirtualBox_Extension_Pack-4.3.6-91406.vbox-extpackVBoxManage extpack install Oracle_VM_VirtualBox_Extension_Pack-4.3.6-91406.vbox-extpack |
I define some variables so you can adapt the following VBoxManage commands easy for your needs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | export VMFOLDER=/vmif [ ! -d "$VMFOLDER" ]; then mkdir VMFOLDER; fiexport VIRTUALDSDA_OS=$VMFOLDER/hda.vdiexport VIRTUALDSDB=$VMFOLDER/hdc.vdiexport VIRTUALMACHINE_CONFIG_DIR=/etc/vboxexport VIRTUALMACHINE_CERT_DIR=$VIRTUALMACHINE_CONFIG_DIR/tlsif [ ! -d "$VIRTUALMACHINE_CERT_DIR" ]; then mkdir $VIRTUALMACHINE_CERT_DIR; fiexport VIRTUALMACHINE_NAME="TestMachine"export VIRTUALMACHINE_MAC="0050563a2d1c"export VIRTUALMACHINE_RAM=1024export VIRTUALMACHINE_MASSSTORAGE_CONTROLLER_NAME="SATA Controller"export VIRTUALMACHINE_MASSSTORAGE_CONTROLLER_DVD_NAME="IDE Controller"export VIRTUALMACHINE_SDA_OS_SIZE=512export VIRTUALMACHINE_SDB_SIZE=1024export VIRTUALMACHINE_RDP_PORT=3389export VIRTUALMACHINE_RDP_USER=Adminexport VIRTUALMACHINE_RDP_PASSWORD=AdminsPasswordexport VIRTUALMACHINE_RDP_ENCRYPT_CA_KEY=$VIRTUALMACHINE_CERT_DIR/cakey.pemexport VIRTUALMACHINE_RDP_ENCRYPT_CA_CERT=$VIRTUALMACHINE_CERT_DIR/ca.certexport VIRTUALMACHINE_RDP_ENCRYPT_CA_PASSWORD=YourCAPasswordexport VIRTUALMACHINE_RDP_SERVER_KEY=$VIRTUALMACHINE_CERT_DIR/srvkey.pemexport VIRTUALMACHINE_RDP_SERVER_CERT=$VIRTUALMACHINE_CERT_DIR/srv.certexport VIRTUALMACHINE_RDP_SERVER_SIGN_REQUEST=$VIRTUALMACHINE_CERT_DIR/srvreq.pemexport VIRTUAL_FLOPPY_FILE_FREEDOS=/tmp/fdos1440.img |
Create a new virtual machine for OS type linux
1 | VBoxManage createvm --name "$VIRTUALMACHINE_NAME" --ostype Linux --register |
Some modifications 🙂
- BIOS RTC uses UTC
- enable ACPI
- Memory 1024MB
- NIC 1 -> Network Bride mode on host interface eth0
- 2 Serial Interfaces, ttyS0 connected to host ttyS0, ttyS1 disconnected
1 | VBoxManage modifyvm "$VIRTUALMACHINE_NAME" --memory $VIRTUALMACHINE_RAM --rtcuseutc on --acpi on --nic1 bridged --bridgeadapter1 eth0 --macaddress1 $VIRTUALMACHINE_MAC --uart1 0x3f8 4 --uart2 0x2f8 3 --uartmode2 disconnected --uartmode1 /dev/ttyS0 |
Or if you want to use NAT. Changing network parameters. MAC Address and Network to which the virtual machine should connect
1 | VBoxManage modifyvm "$VIRTUALMACHINE_NAME" --macaddress1 $VIRTUALMACHINE_MAC --nic1 nat |
Some DMI Information
Board Serial
1 | VBoxManage setextradata "$VIRTUALMACHINE_NAME" "VBoxInternal/Devices/pcbios/0/Config/DmiSystemSerial" "string:YE123456" |
The “virtual” BIOS Vendor
1 | VBoxManage setextradata "$VIRTUALMACHINE_NAME" "VBoxInternal/Devices/pcbios/0/Config/DmiBIOSVendor" "BIOS Vendor" |
BIOS Version String
1 | VBoxManage setextradata "$VIRTUALMACHINE_NAME" "VBoxInternal/Devices/pcbios/0/Config/DmiBIOSVersion" "5.0.1.2" |
BIOS Date
1 | VBoxManage setextradata "$VIRTUALMACHINE_NAME" "VBoxInternal/Devices/pcbios/0/Config/DmiBIOSReleaseDate" "02/07/2014" |
Bios Version
1 2 3 4 | VBoxManage setextradata "$VIRTUALMACHINE_NAME" "VBoxInternal/Devices/pcbios/0/Config/DmiBIOSReleaseMajor" 5VBoxManage setextradata "$VIRTUALMACHINE_NAME" "VBoxInternal/Devices/pcbios/0/Config/DmiBIOSReleaseMinor" 0VBoxManage setextradata "$VIRTUALMACHINE_NAME" "VBoxInternal/Devices/pcbios/0/Config/DmiBIOSFirmwareMajor" 1VBoxManage setextradata "$VIRTUALMACHINE_NAME" "VBoxInternal/Devices/pcbios/0/Config/DmiBIOSFirmwareMinor" 2 |
For more DMI settings see VirtualBox Manual, Chapter 9.12
If your virtual maschine should run in background and start if the host system starts
1 | VBoxManage setextradata "$VIRTUALMACHINE_NAME" autostart headless |
Create a new harddisk
1 | VBoxManage createhd --filename "$VIRTUALDSDA_OS" --size $VIRTUALMACHINE_SDA_OS_SIZE --format VDI --variant fixed |
and a second one
1 | VBoxManage createhd --filename "$VIRTUALDSDB" --size $VIRTUALMACHINE_SDB_SIZE --format VDI --variant fixed |
Add a SATA Massstorge controller
1 | VBoxManage storagectl "$VIRTUALMACHINE_NAME" --add sata --controller IntelAHCI --name "$VIRTUALMACHINE_MASSSTORAGE_CONTROLLER_NAME" |
Attach a harddisk to a controller
1 | VBoxManage storageattach "$VIRTUALMACHINE_NAME" --storagectl "$VIRTUALMACHINE_MASSSTORAGE_CONTROLLER_NAME" --port 0 --device 0 --type hdd --medium $VIRTUALDSDA_OS |
Attach the other disk
1 | VBoxManage storageattach "$VIRTUALMACHINE_NAME" --storagectl "$VIRTUALMACHINE_MASSSTORAGE_CONTROLLER_NAME" --port 1 --device 0 --type hdd --medium $VIRTUALDSDB |
Add a IDE Controller for DVDs
1 | VBoxManage storagectl "$VIRTUALMACHINE_NAME" --add ide --controller PIIX3 --name "$VIRTUALMACHINE_MASSSTORAGE_CONTROLLER_DVD_NAME" |
Add a DVD Drive, no medium
1 | VBoxManage storageattach "$VIRTUALMACHINE_NAME" --storagectl "$VIRTUALMACHINE_MASSSTORAGE_CONTROLLER_DVD_NAME" --port 0 --device 0 --type dvddrive --medium emptydrive |
Add a floppy drive
1 | VBoxManage storagectl "$VIRTUALMACHINE_NAME" --add floppy --name "Floppy Controller" |
Attaching a floppy image file
1 | VBoxManage storageattach "$VIRTUALMACHINE_NAME" --storagectl "Floppy Controller" --device 0 --port 0 --type fdd --medium "$VIRTUAL_FLOPPY_FILE_FREEDOS" |
The Virtual Machine is ready to install operating system. Mount a iso image
1 | VBoxManage storageattach "$VIRTUALMACHINE_NAME" --storagectl "$VIRTUALMACHINE_MASSSTORAGE_CONTROLLER_DVD_NAME" --port 0 --device 0 --type dvddrive --medium /tmp/kali-linux.iso |
And remove the “virtual” DVD
1 | VBoxManage storageattach "$VIRTUALMACHINE_NAME" --storagectl "$VIRTUALMACHINE_MASSSTORAGE_CONTROLLER_DVD_NAME" --port 0 --device 0 --type dvddrive --medium emptydrive |
Time to start the VM. If you have a X11 display active start a VM in foreground
1 | VBoxManage startvm "$VIRTUALMACHINE_NAME" |
Or if no display available start virtual machine in background
1 | VBoxManage startvm "$VIRTUALMACHINE_NAME" --type headless |
Enable RDP with simple authentication to view the console screen over network.
Note: RDP does only work when the extension pack is installed!
1 2 3 4 5 | VBoxManage setproperty vrdeauthlibrary "VBoxAuthSimple"VBoxManage modifyvm $VIRTUALMACHINE_NAME --vrdeauthtype external --vrdeauthlibrary VBoxAuthSimpleexport PASSHASH=`VBoxManage internalcommands passwordhash $VIRTUALMACHINE_RDP_PASSWORD|sed -e 's/Password hash: //g'`VBoxManage setextradata $VIRTUALMACHINE_NAME "VBoxAuthSimple/users/"$VIRTUALMACHINE_RDP_USER $PASSHASHVBoxManage modifyvm $VIRTUALMACHINE_NAME --vrde on --vrdemulticon on --vrdeextpack default --vrdeport $VIRTUALMACHINE_RDP_PORT --vrdeauthtype external --vrdeauthlibrary VBoxAuthSimple |
Now you can connect with the Remote Desktop Client to the console of the virtual machine.
Note: There is a strange behaviour when connecting with the Windows Remote Desktop Client. The checkbox “Allow to save my credentials” must be enabled otherwise no connection could established. Any ideas?
In a secure environment encrypt the RDP Session
Create a simple CA only for the reason to create TLS Certificate, its recommended to set a password the CA and the RDP Key
1 | openssl req -new -x509 -days 18250 -extensions v3_ca -keyout $VIRTUALMACHINE_RDP_ENCRYPT_CA_KEY -out $VIRTUALMACHINE_RDP_ENCRYPT_CA_CERT |
Create RDP Key
1 | openssl genrsa -out $VIRTUALMACHINE_RDP_SERVER_KEY |
Create signing request
1 | openssl req -new -key $VIRTUALMACHINE_RDP_SERVER_KEY -out $VIRTUALMACHINE_RDP_SERVER_SIGN_REQUEST |
Sign certifcate with the CA
1 | openssl x509 -req -days 18250 -in $VIRTUALMACHINE_RDP_SERVER_SIGN_REQUEST -CA $VIRTUALMACHINE_RDP_ENCRYPT_CA_CERT -CAkey $VIRTUALMACHINE_RDP_ENCRYPT_CA_KEY -set_serial 01 -out $VIRTUALMACHINE_RDP_SERVER_CERT |
# Add Certificate to the RDP Config and enable it
1 2 3 4 | vboxmanage modifyvm $VIRTUALMACHINE_NAME --vrdeproperty "Security/CACertificate=$VIRTUALMACHINE_RDP_ENCRYPT_CA_CERT"vboxmanage modifyvm $VIRTUALMACHINE_NAME --vrdeproperty "Security/ServerCertificate=$VIRTUALMACHINE_RDP_SERVER_CERT"vboxmanage modifyvm $VIRTUALMACHINE_NAME --vrdeproperty "Security/ServerPrivateKey=$VIRTUALMACHINE_RDP_SERVER_KEY"vboxmanage modifyvm $VIRTUALMACHINE_NAME --vrdeproperty "Security/Method=negotiate" |
Some other commands. Poweroff a VM
1 | VBoxManage controlvm "$VIRTUALMACHINE_NAME" poweroff |
Deleting a VM
1 | VBoxManage unregistervm "$VIRTUALMACHINE_NAME" --delete |
How to change the UUID of a virtual disk is descripted here.
Enjoy!
Michael