Graham Eddy

virtualbox using CLI

Create a simple linux virtual machine using CLI (suitable for headless server).

However, the VM instance has an emulated physical console accessed via RDP, if required.

2021-06-22 Ubuntu 21.10-22.04


Main characteristics of this implementation:

There can be many VM instances. The example VM has configuration:

VM name
goblin
CPUs
1 x shared
RAM
1 MB
NICs
1 – bridged to local LAN, not separate subnet
HDD
1 x 10 MB
DVD
1

Installation

graham:~ sudo apt update graham:~ sudo apt install virtualbox virtualbox-ext-pack

Policy: keep VM resources under one roof:

graham:~ sudo adduser --gecos 'VM instances' vbox graham:~ sudo vi /etc/sudoers.d/vbox
/etc/sudoers.d/vbox new file
# members of group 'vbox' permitted to run VBox commands as user 'vbox'
%vbox ALL = (vbox) NOPASSWD: \
    /usr/bin/VBoxManage, \
    /usr/bin/VBoxHeadless, \
    /usr/bin/VBoxSDL, \
    /usr/bin/VBoxBalloonCtrl
graham:~ sudo adduser graham vbox graham:~ sudo -u vbox vi ~vbox/vm_instance@.service
~vbox/vm_instance@.service new file
# vm_instance - manage virtualbox VM instance

[Unit]
Description=VirtualBox VM %I
After=network.target virtualbox.service
Before=runlevel2.target shutdown.target

[Service]
User=vbox
Group=vbox
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
ExecStart=/usr/bin/VBoxManage startvm %i --type headless
ExecStop=/usr/bin/VBoxManage controlvm %i acpipowerbutton

[Install]
WantedBy=multi-user.target
graham:~ sudo cp ~vbox/vm_instance@.service /etc/systemd/system/ graham:~ sudo systemctl daemon-reload

Create a VM instance

graham:~ sudo -u vbox -i # VMs are owned by vbox vbox:~ VBoxManage createvm --name goblin # create VM instance vbox:~ VBoxManage modifyvm goblin --ioapic on vbox:~ VBoxManage modifyvm gobin --memory 1024 -vram 128 # add 1GB RAM (incl 128M video RAM)

Add a NIC. Rather than creating a new private subnet with NAT, our VM instance will be just another host on our LAN, to which we have already assigned its own IP address. We will share the host's NIC and subnet.

vbox:~ ifconfig -s Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg eth0 1500 2278777 0 117427 0 357062 0 0 0 BMRU lo 65536 19 0 0 0 19 0 0 0 LRU vbox:~ VBoxManage modifyvm goblin --nic1 bridged --bridgeadapter eth0

Attach a new, bootable but empty 10GB virtual disk. We will store the virtual disk file in vbox's home directory virtualbox space.

vbox:~ VBoxManage createhd --filename ~/'VirtualBox VMs'/goblin/hd0.vdi \ --size 1000 --variant Standard --format VDI vbox:~ VBoxManage storagectl goblin --name 'SATA Controller' --add sata \ --bootable on vbox:~ VBoxManage storageattach goblin --storagectl 'SATA Controller' \ --port 0 --device 0 --type hdd \ --medium ~/'VirtualBox VMs'/goblin/hd0.vdi

Obtain an ISO file of OS install image (download from somewhere) and store it in vbox's home directory e.g. ~vbox/ubuntu-21.04-live-server-amd64.iso.

Attach a new DVD drive and mount the OS install image.

vbox:~ VBoxManage storagectl goblin 'IDE Controller' --ade ide vbox:~ VBoxManage storageattach goblin --storagectl 'IDE Controller' \ --port 0 --device 0 --type dvddrive \ --medium ~/ubuntu-21.04-live-server-amd64.iso

Enable RDP on VM instance to emulate physical console. Unless the OS install image is able to autoconfigure itself unattended e.g. ssh enabled and working – a practice only started recently – to complete installation of the OS inside a new VM instance, access to (emulated) physical consoles required i.e. an RDP client.

vbox:~ VBoxManage modifyvm goblin --vrde on

Inspect our creation:

vbox:~ VBoxManage showvminfo goblin Name: goblin Groups: / Guest OS: Ubuntu (64-bit) UUID: ebb7b48b-c580-4a5c-ad22-a0f693b4376a Config file: /home/vbox/VirtualBox VMs/goblin/goblin.vbox Snapshot folder: /home/vbox/VirtualBox VMs/goblin/Snapshots Log folder: /home/vbox/VirtualBox VMs/goblin/Logs Hardware UUID: ebb7b48b-c580-4a5c-ad22-a0f693b4376a Memory size: 1024MB Page Fusion: disabled VRAM size: 128MB CPU exec cap: 100% HPET: disabled CPUProfile: host Chipset: piix3 Firmware: BIOS Number of CPUs: 1 PAE: enabled Long Mode: enabled Triple Fault Reset: disabled APIC: enabled X2APIC: enabled Nested VT-x/AMD-V: disabled CPUID Portability Level: 0 CPUID overrides: None Boot menu mode: message and menu Boot Device 1: Floppy Boot Device 2: DVD Boot Device 3: HardDisk Boot Device 4: Not Assigned ACPI: enabled IOAPIC: enabled BIOS APIC mode: APIC Time offset: 0ms RTC: local time Hardware Virtualization: enabled Nested Paging: enabled Large Pages: disabled VT-x VPID: enabled VT-x Unrestricted Exec.: enabled Paravirt. Provider: Default Effective Paravirt. Prov.: KVM State: running (since 2022-08-18T13:12:38.707000000) Graphics Controller: VBoxVGA Monitor count: 1 3D Acceleration: disabled 2D Video Acceleration: disabled Teleporter Enabled: disabled Teleporter Port: 0 Teleporter Address: Teleporter Password: Tracing Enabled: disabled Allow Tracing to Access VM: disabled Tracing Configuration: Autostart Enabled: disabled Autostart Delay: 0 Default Frontend: VM process priority: default Storage Controller Name (0): SATA Controller Storage Controller Type (0): IntelAhci Storage Controller Instance Number (0): 0 Storage Controller Max Port Count (0): 30 Storage Controller Port Count (0): 30 Storage Controller Bootable (0): on Storage Controller Name (1): IDE Controller Storage Controller Type (1): PIIX4 Storage Controller Instance Number (1): 0 Storage Controller Max Port Count (1): 2 Storage Controller Port Count (1): 2 Storage Controller Bootable (1): on SATA Controller (0, 0): /home/vbox/VirtualBox VMs/goblin/hd0.vdi (UUID: 5913aa9d-8dfb-49a9-8f22-5505061a5d54) IDE Controller (0, 0): Empty NIC 1: MAC: 0800271256CD, Attachment: Bridged Interface 'eth0', Cable connected: on,\ Trace: off (file: none), Type: 82540EM, Reported speed: 0 Mbps, Boot priority: 0, Promisc Policy: deny, Bandwidth group: none NIC 2: disabled NIC 3: disabled NIC 4: disabled NIC 5: disabled NIC 6: disabled NIC 7: disabled NIC 8: disabled Pointing Device: PS/2 Mouse Keyboard Device: PS/2 Keyboard UART 1: disabled UART 2: disabled UART 3: disabled UART 4: disabled LPT 1: disabled LPT 2: disabled Audio: enabled (Driver: ALSA, Controller: AC97, Codec: STAC9700) Audio playback: disabled Audio capture: disabled Clipboard Mode: disabled Drag and drop Mode: disabled Session name: headless Video mode: 1024x768x32 at 0,0 enabled VRDE: enabled (Address 0.0.0.0, Ports 3389, MultiConn: off, ReuseSingleConn: off, Authentication type: null) VRDE port: 3389 Video redirection: disabled VRDE property : TCP/Ports = "3389" VRDE property : TCP/Address = OHCI USB: disabled EHCI USB: disabled xHCI USB: disabled USB Device Filters: Available remote USB devices: Currently Attached USB Devices: Bandwidth groups: Shared folders: VRDE Connection: not active Clients so far: 0 Capturing: not active Capture audio: not active Capture screens: 0 Capture file: /home/vbox/VirtualBox VMs/goblin/goblin.webm Capture dimensions: 1024x768 Capture rate: 512kbps Capture FPS: 25kbps Capture options: Guest: Configured memory balloon size: 0MB OS type: Linux26_64 Additions run level: 1 Additions version: 6.0.0 r127566 Guest Facilities: Facility "VirtualBox Base Driver": active/running (last update: 2022/08/18 13:13:51 UTC) Facility "Seamless Mode": not active (last update: 2022/08/18 13:13:51 UTC) Facility "Graphics Mode": not active (last update: 2022/08/18 13:13:51 UTC)

Allow systemctl to manage this VM instance:

vbox:~ exit # good practice: vbox does not sudo graham:~ sudo systemctl enable vm_instance@goblin

At this point the new VM instance is inert (not booted), no OS has been installed, but the install CD is loaded.

Start VM instance

graham:~ sudo systemctl start vm_instance@goblin

First boot special needs

If this is the VM instance's first boot, it is looking to install the OS from the already-mounted OS image:

FYI, some problems encountered with Mac Mini as host running Ubuntu 21.10 when installing client OS:

Stop VM instance

graham:~ sudo systemctl stop vm_instance@goblin

Alternatively – not recommended normal practice! – you can directly just 'pull the plug' by:

graham:~ sudo -u vbox VBoxManage controlvm goblin poweroff