HomeBlogMagic

Virtualbox in der Commandline (VBoxManage)

Beispiel script für das anlegen einer Virtualbox Machine TestVM von der commandline.
Unter Windows sollte dazu VBoxManage von PATH aus erreichbar sein: In der Standard Commandline:

SET PATH=%PATH%;C:\Program Files\Oracle\VirtualBox

In der Powershell:

$env:PATH =  $env:PATH + ";C:\Program Files\Oracle\VirtualBox"

Die VM wird im Standard Verzeichnis für VMs erstellt, die VDI allerdings in dem aktuellen Verzeichnis.
Um den Speicherort der vdi zu ändern, muss der filename durch einen absoluten Pfad ersetzt werden.

Die Befehle berufhen auf die original Dokumentation unter https://www.virtualbox.org/manual/ch08.html

Create VM

VBoxManage createvm --name TestVM --register 
VBoxManage modifyvm TestVM --ostype Linux_64

VBoxManage createhd --filename TestVM.vdi --size 32768
VBoxManage storagectl TestVM --name "SATA Controller" --add sata --controller IntelAHCI
VBoxManage storageattach TestVM --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium TestVM.vdi

VBoxManage modifyvm TestVM --boot1 dvd --boot2 disk --boot3 none --boot4 none
VBoxManage modifyvm TestVM --memory 1024 --vram 16
VBoxManage modifyvm TestVM --pae on
VBoxManage modifyvm TestVM --nic1 nat --nictype1 82540EM

Start VM

VBoxManage startvm TestVM --type headless

"--type headless" startet die VM im Hintergrund.

Stop VM

VBoxManage controlvm TestVM poweroff

Remove VM

VBoxManage storageattach TestVM --storagectl "SATA Controller" --port 0  --device 0 --type hdd --medium none
VBoxManage closemedium disk TestVM.vdi --delete
VBoxManage unregistervm TestVM --delete

Send Keyboard-Keys

# Windows-Key
VBoxManage controlvm "TestVM" keyboardputscancode e0 5b e0 db
# CTRL-ALT-DEL
VBoxManage controlvm "TestVM" keyboardputscancode 1d 38 53 d3 b8 9d

Manipulate VDI/VMDK/VHD

Clone to new Format vdi/vmdk/vhd

# VMDK to VDI
VBoxManage clonehd "source.vmdk" "cloned.vdi" --format vdi
# VHD to VMDK
VBoxManage clonehd "source.vhd" "cloned.vmdk" --format vmdk
# RAW image to vdi
VBoxManage convertfromraw "source.hddimg" "cloned.vdi" --format vdi

Resize virtual disk (resize in MB, here 256 GB)

VBoxManage modifyhd "HDD.vdi" --resize 262144

Shrink virtual disk ( nur für VDI )

VBoxManage modifyhd "HDD.vdi" compact

Convert Windows Hard Drive to vdi

# Check if Physical drive exists
VBoxManage internalcommands listpartitions -rawdisk \\.\PhysicalDrive0
# Start converting
VBoxManage convertfromraw \\.\PhysicalDrive0 MyImage.vdi --format VDI

Change uuid ( if more than one with same uuid is added to VBox)

VBoxManage internalcommands setvdiuuid disk2.vdi
VBoxManage internalcommands sethduuid disk2.vhd

Read only Virtual Drive, so writes to disk will be stored temporary and are lost if system is powered down.

VBoxManage modifyhd --type immutable disk2.vhd

Network Settings

Für Hintergrundinformationen kann oft die original Dokumentation für Netzwerkadapter helfen: https://www.virtualbox.org/manual/ch06.html#networkingmodes

Aufsetzen eines internen Netzwerks für eine VM

VBoxManage modifyvm TestVM --nic4 intnet --nictype1 82540EM --cableconnected on

Display Settings

Für einen Screenshot von der Commandline aus:

vboxmanage controlvm "TestVM" screenshotpng TestVM.png

Einstellen der Auflösung:

VBoxManage controlvm "TestVM" setvideomodehint 1366 768 32

Example for setting up a bridged vm on linux

VM_NAME=TestVM
VM_DIR=$(pwd)

rm $VM_DIR/$VM_NAME.vdi
VBoxManage unregistervm $VM_NAME --delete

VBoxManage createvm --name $VM_NAME --register 
VBoxManage modifyvm $VM_NAME --ostype Linux_64

VBoxManage createhd --filename $VM_DIR/$VM_NAME.vdi --size 250000
VBoxManage storagectl $VM_NAME --name "SATA Controller" --add sata --controller IntelAHCI
#VBoxManage storageattach $VM_NAME --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium $VM_DIR/$VM_NAME.vdi
vboxmanage storageattach $VM_NAME --storagectl "SATA Controller" --port 0 --device 0 --type dvddrive --medium $VM_DIR/linuxmint-20-mate-64bit.iso

VBoxManage modifyvm $VM_NAME --cpus 4
VBoxManage modifyvm $VM_NAME --boot1 dvd --boot2 disk --boot3 none --boot4 none
VBoxManage modifyvm $VM_NAME --memory 8192 --vram 16
VBoxManage modifyvm $VM_NAME --pae on
VBoxManage modifyvm $VM_NAME --nic1 bridged --nictype1 82540EM --bridgeadapter1 wlx000b819609d3
VBoxManage setproperty vrdeextpack "Oracle VM VirtualBox Extension Pack"

VBoxManage modifyvm $VM_NAME --vrde on
VBoxManage modifyvm $VM_NAME --vrdeauthtype null
VBoxManage startvm $VM_NAME --type headless

VBoxManage controlvm $VM_NAME poweroff
Permalink: https://adirmeier.de/Blog/ID_119
Tags: Blog, Notizenvon am 2017-05-16