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:
- Uses systemctl to start/stop VM instances. The various methods officially documented, including by virtualbox, to autostart VM instance when its host boots are impractical or don't work. This works.
-
Assigns ownership of all VM instances to a system user
vbox
and all users of a VM instance belong to groupvbox
.
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- Answer Y to license prompt on virtualbox extras pack.
- This is a big install (about 180 MB) and includes lots of Gnome GUI, which is unfortunate as we have headless host.
Policy: keep VM resources under one roof
:
/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
- Add all users who need access to a VM to group
vbox
.
~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
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.
Attach a new, bootable but empty 10GB virtual disk.
We will store the virtual disk file in vbox
's
home directory virtualbox space.
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.isoEnable 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 onInspect 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 =Allow systemctl to manage this VM instance:
vbox:~ exit # good practice: vbox does not sudo graham:~ sudo systemctl enable vm_instance@goblinAt 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-
This starts the VM instance i.e. it begins to boot up.
How long it takes to boot depends what is in the VM instance.
You can check its readiness for ssh login by
ping
ing it.
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:
- If set up for unattended install – unlikely unless it is a recent, custom image such as Raspbian bullseye – then allow 15-20 minutes for that to complete, then ssh should be available.
- If not unattended install, then you must use the (emulated) physical console (i.e. connect via RDP) to complete the installation. Make sure to enable OpenSSH when installing.
FYI, some problems encountered with Mac Mini as host running Ubuntu 21.10 when installing client OS:
-
Selecting next OS version fails due to invalid shim under
/boot
→ stay with version on install CD. - De-selecting LVM results in crash during next step → leave LVM selected.
- Virtual CD not unmounted cleanly at end of install so it tries and fails to boot from it on restart → coerce boot from hard disk. Once-off problem per install.
Stop VM instance
graham:~ sudo systemctl stop vm_instance@goblin-
This initiates a clean shutdown.
The
systemctl
command finishes immediately but the actual shutdown can take several minutes. You can monitor usingping
.
Alternatively – not recommended normal practice! – you can directly just 'pull the plug' by:
graham:~ sudo -u vbox VBoxManage controlvm goblin poweroff