Skip to content

Instantly share code, notes, and snippets.

@WintersMichael
Last active December 27, 2022 00:55
Show Gist options
  • Save WintersMichael/ae2f7f486487d815db07dbd538d900ef to your computer and use it in GitHub Desktop.
Save WintersMichael/ae2f7f486487d815db07dbd538d900ef to your computer and use it in GitHub Desktop.
Virtualbox notes

1: Creation

Creating a VM with an existing disk image:

export VM_NAME="Neon Dev"

VBoxManage list ostypes | less

VBoxManage createvm --name "${VM_NAME}" --ostype Ubuntu22_LTS_64 --register

VBoxManage modifyvm "${VM_NAME}" \
  --memory=16384 \
  --cpus=8 \
  --vram=64 \
  --acpi=on \
  --graphicscontroller=vmsvga \
  --paravirt-provider=kvm \
  --mouse=usbtablet \
  --rtc-use-utc=on \
  --snapshot-folder="$(pwd)" \
  --default-frontend=headless \
  --usb-ehci=on \
  --nic-type1=virtio \
  --nat-pf1 "guestmisc,tcp,,2222,,2222" \

VBoxManage storagectl "${VM_NAME}" --name "ide01" --add ide --controller PIIX4
VBoxManage storageattach "${VM_NAME}" --storagectl="ide01" --device=1 --port=1 --type=dvddrive --medium=additions

# This was with a disk image that was created previously
VBoxManage storagectl "${VM_NAME}" --name "sata01" --add sata --controller IntelAHCI
VBoxManage storageattach "${VM_NAME}" --storagectl="sata01" --device=0 --port=0 --type=hdd --nonrotational=on --medium="$(pwd)/neondev.vdi"

Creating a new disk image:

VBoxManage createmedium [ disk | dvd | floppy ] <--filename=filename> [ --size=megabytes | --sizebyte=bytes ] [--diffparent= UUID | filename ] [--format= VDI | VMDK | VHD ] [--variant Standard,Fixed,Split2G,Stream,ESX,Formatted,RawDisk]

2: Remote Config

VirtualBox has a plugin system called "extpack", and officially it ships with an RDP server extpack called "Oracle VM VirtualBox Extension Pack". It's common for distros (like Arch) to instead ship with a VNC extpack called "VNC". You can list available extpacks with VBoxManage list extpacks.

Authentication for RDP access will by default use the guest PAM. You can change this to VirtualBox-managed auth type "external".

Base setup

VBoxManage modifyvm "${VM_NAME}" \
  --vrde=on \
  --vrde-auth-type=external \
  --vrde-port=5001 \
  --vrde-address=127.0.0.1

VNC setup

Note that VNC doesn't have users, only a global password, and it isn't encrypted.

VBoxManage setproperty vrdeextpack VNC
VBoxManage modifyvm "$VM_NAME" --vrde-property VNCPassword='secret'

RDP setup

VBoxManage setproperty vrdeextpack "Oracle VM VirtualBox Extension Pack"

VBoxManage setproperty vrdeauthlibrary "VBoxAuthSimple"
VBoxManage setextradata "${VM_NAME}" "VBoxAuthSimple/users/auser" $(VBoxManage internalcommands passwordhash "secret" | cut -d ' ' -f 3)

To verify password hash:

VBoxManage getextradata "$VM_NAME" "VBoxAuthSimple/users/auser"

3: Run

Verify VM config

VBoxManage list vms --long | less

Start VM

VBoxManage startvm "$VM_NAME" --type headless # will go to background, less debug info
# or
VBoxHeadless --startvm "$VM_NAME" --vrde config # stays foreground, more debug info

Control running VM

VBoxManage controlvm <uuid | vmname> pause
VBoxManage controlvm <uuid | vmname> resume
VBoxManage controlvm <uuid | vmname> reset
VBoxManage controlvm <uuid | vmname> poweroff
VBoxManage controlvm <uuid | vmname> savestate
VBoxManage controlvm "$VM_NAME" acpipowerbutton
VBoxManage controlvm <uuid | vmname> acpisleepbutton
VBoxManage controlvm <uuid | vmname> reboot
VBoxManage controlvm "$VM_NAME" shutdown [--force]

VBoxManage controlvm <uuid | vmname> vrde <on | off>
VBoxManage controlvm <uuid | vmname> vrdeport <port>
VBoxManage controlvm <uuid | vmname> vrdeproperty <prop-name=prop-value>
VBoxManage controlvm <uuid | vmname> vrdevideochannelquality <percentage>

4: Autorun

Edit this systemd template to match your host username and group:

sudo echo '[Unit]
Description=VirtualBox VM %I
After=network.target vboxdrv.service
Before=runlevel2.target shutdown.target

[Service]
User=m
Group=m
Type=simple
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=none

ExecStart=/usr/bin/VBoxHeadless --startvm %i --vrde config
ExecStop=/usr/bin/VBoxManage controlvm %i shutdown

[Install]
WantedBy=multi-user.target' > '/etc/systemd/system/vbox_vm@.service'

sudo systemctl enable "vbox_vm@$VM_NAME"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment