VirtualBox - An x86 and AMD64/Intel64 virtualization product for enterprise as well as home use

mail

How to install VirtualBox on Debian ?

Table of contents

On Debian Bullseye (11)

Read this.

On Debian Buster (10)

Officially, you can't and the solution here is a dirty workaround.
  1. sudo apt update
  2. sudo apt upgrade
  3. wget -q https://www.virtualbox.org/download/oracle_vbox_2016.asc -O - | sudo apt-key add -
  4. wget -q https://www.virtualbox.org/download/oracle_vbox.asc -O - | sudo apt-key add -
  5. add-apt-repository "deb http://download.virtualbox.org/virtualbox/debian bionic contrib"
    If this fails with error :
    -bash: add-apt-repository: command not found
    • run (source, DIRTY!!!) :
    • or manually edit /etc/apt/sources.list :
      echo -e '\ndeb http://download.virtualbox.org/virtualbox/debian bionic contrib' >> /etc/apt/sources.list
      The leading carriage return is there just in case /etc/apt/sources.list doesn't end with a new line : in such situation, both lines would be concatenated.
  6. sudo apt update
  7. sudo apt install virtualbox-6.0

Alternate solution

/!\ NOT TESTED YET :
Define the install source in /etc/apt/sources.list :
	deb http://download.virtualbox.org/virtualbox/debian buster contrib
==> inspired by https://wiki.debian.org/VirtualBox#Oracle_Virtualbox_third-party_repository
mail

How to connect a virtual machine to the Internet using a smartphone as a USB modem ?

Situation

On a physical Debian machine, this is very easy :
  1. plug the USB cable
  2. configure the smartphone as a USB modem
  3. NetworkManager will do the rest of the job
  4. after a few seconds, ip a will list a new network interface
  5. enjoy !!!
But on a virtual machine, this can be tricky, especially when it comes to "plug" a USB device into a virtual machine. This is addressed in the article Android tethering with Linux host running as a VirtualBox guest.

Solution

The procedure below is more a workaround than an actual solution. The idea is actually to use the Android device as a modem on the host side, then share this connection to the guest .
  1. plug the USB cable
  2. configure the smartphone as a USB modem
  3. in its Network connections page, Windows should list a Remote NDIS based Internet Sharing Device
  4. if not already enabled : enable it
  5. configure the VM with a NAT network connection type
  6. enjoy !!!
mail

How to start a headless guest ?

To do so :
from the command line :
"C:\Program Files\Oracle\VirtualBox\vboxmanage.exe" startvm "C:\Users\bob\VirtualBox VMs\myVm\myVm.vbox" --type headless
from the VirtualBox virtual machines manager :
  1. in the list of available VMs, select the one you wish to start
  2. in the top row (with big icons), you'll see a little black "down arrow" next to the Start button (green right arrow)
  3. pick the startup mode you want
Screenshots of the console contents will still be visible in the VirtualBox virtual machines manager.
mail

How to delete + purge a virtual disk ?

Situation

An obvious preliminary to all this is to shut down the virtual machine using the virtual disk you're about to remove.

Unplugging a virtual disk from a virtual machine is easily done within the virtual machine Configuration | Storage screens but :

Solution

Proper way to delete + purge a virtual disk image file (source) :

This is performed on the host side.
  1. cd "C:\Program Files\Oracle\VirtualBox"
  2. vboxmanage list hdds
    UUID:		dfea5b73-2683-4d6a-9965-a34a51235162
    Parent UUID:	base
    State:		created
    Type:		normal (base)
    Location:	C:\Users\kevin\VirtualBox VMs\myVirtualMachine\NewVirtualDisk1.vdi
    Storage format:	VDI
    Capacity:	100 MBytes
    Encryption:	disabled
    Note the UUID of the disk you want to purge
  3. vboxmanage closemedium disk dfea5b73-2683-4d6a-9965-a34a51235162 --delete
    0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

Alternate solution

  1. open the VirtualBox GUI
  2. File | Media manager
  3. pick the virtual disk to remove then Delete
(source)
mail

How to create a new virtual disk ?

After you have stopped the target virtual machine, it all takes place in the VirtualBox GUI :
  1. open the virtual machine Configuration panel
  2. select the Storage category
  3. select the IDE or SATA controller, depending on the type of disk you want to create
  4. click the + hard drive icon
  5. follow the on-screen instructions

Once the new disk has been created, you can boot the virtual machine, log in, and confirm the disk is here with lsblk.

mail

How to fix the audio in Debian guest ?

Situation

Debian Stretch guest in Windows 7 host, both running together for months. I get crackling sound from the Debian guest whatever application I play sound with.

Details

There's been a VirtualBox update, lately...

Solution

Here are a few things to check (from obvious to technical ) :
  1. have you considered playing medias directly in the host system ? This is more a workaround than a solution (when possible), but it may do the trick and save hours of Googling
  2. make sure the volume is not muted on the host side
  3. make sure the volume is not muted on the guest side
  4. play a dummy sound on the guest : aplay /usr/share/sounds/alsa/Front_Center.wav
    1. shutdown the guest machine
    2. open the VirtualBox VM manager
    3. pick the VM | Configuration | Sound tab
    4. toggle the Audio controller from Intel audio HD to ICH AC97
    5. start the guest machine
    6. should be better now
  5. if still not ok (experimental) :
    
    kill -15 $(pgrep pulseaudio) && systemctl restart alsa-state.service; systemctl restart alsa-restore.service
    	==> :-/
    
    I'm afraid everything is fine on guest side, and things go wrong between guest/host :-(
    
    on host side, give higher priority to 'VboxSVC.exe' process
    	==> testing...
    	==> Virtualbox crashed 1 hour after that :-D (at least, that was expected!!!)
    
    after rebooting host + guest : untick "enable audio input" in VM settings
    	==> nope :-(
    
mail

How to enlarge a virtual machine disk ?

The concept :

  1. put the VM in a clean state (stopped, merge snapshots, backup image file(s))
  2. on the host side, enlarge the virtual disk with VirtualBox commands
  3. boot the VM
  4. make the guest operating system aware of the extra space
Below are 2 examples :

Windows host with Debian guest

Description

  1. put the VM in a clean state (stopped, merge snapshots, backup image file(s))
  2. enlarge the virtual disk with VirtualBox commands
  3. boot the VM, create a new partition on the added storage space
  4. change this partition into a physical volume
  5. extend the volume group onto this new physical volume
  6. extend logical volume(s) as needed
  7. extend filesystem(s)
An alternate solution (not tested, not described here) would be to :
  1. create a new virtual disk with VirtualBox commands
  2. plug it into the VM. This would become /dev/sdb
  3. add this disk to a volume group
  4. extend volume group(s) + logical volume(s) + filesystem(s)

Procedure

  1. on the guest side :
    1. the "before" status :
      df -h | grep '/dev/mapper'
      /dev/mapper/caramba--vg-root	8.2G	5.4G	2.4G	70%	/		this is getting tight
      /dev/mapper/caramba--vg-home	24G	1014M	22G	5%	/home
      /dev/mapper/caramba--vg-tmp	360M	2.1M	335M	1%	/tmp
      /dev/mapper/caramba--vg-var	2.7G	549M	2.0G	22%	/var
      pvs
      PV		VG		Fmt	Attr	PSize	PFree
      /dev/sda5	caramba-vg	lvm2	a--	39.76g	0
      fdisk -l
      Disk /dev/sda: 40 GiB, 53687091200 bytes, 104857600 sectors
      Units: sectors of 1 * 512 = 512 bytes
      Sector size (logical/physical): 512 bytes / 512 bytes
      I/O size (minimum/optimal): 512 bytes / 512 bytes
      Disklabel type: dos
      Disk identifier: 0x26504200
      
      Device		Boot	Start	End		Sectors		Size	Id	Type
      /dev/sda1	*	2048	499711		497664		243M	83	Linux
      /dev/sda2		501758	83884031	83382274	39.8G	5	Extended
      /dev/sda5		501760	83884031	83382272	39.8G	8e	Linux LVM	this is the one I'd like to enlarge
    2. Turn the guest system completely OFF (no hibernate / suspend / whatever mode)
  2. on the host side :
    1. backup vdi + snapshots (paranoid mode=activated)
    2. merge all snapshots (not sure this is required but just in case...)
    3. backup the vdi that has the snapshots merged (paranoid level=maximum)
    4. Enlarge the virtual machine storage (source) :
      Not sure the modifyhdd command has not been obsoleted... Check this first
      "C:\Program Files\Oracle\VirtualBox\vboxmanage.exe" modifyhd "C:\Users\kevin\VirtualBox VMs\Debian\Debian.vdi" --resize totalStorageSizeInMb
      0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
      This should be done in only a few seconds.
    5. At this point, this is over regarding VirtualBox, the remaining tasks are all guest-related.
  3. on the guest side :
    1. Some extra space should be visible to root :
      fdisk -l /dev/sda
      Disk /dev/sda: 80 GiB, 85899345920 bytes, 167772160 sectors
    2. Then check for unpartitioned space :
      fdisk /dev/sda + F
      Unpartitioned space /dev/sda: 40 GiB, 42950721536 bytes, 83888128 sectors
      Units: sectors of 1 * 512 = 512 bytes
      Sector size (logical/physical): 512 bytes / 512 bytes
      
      Start		End		Sectors		Size
      83884032	167772159	83888128	40G
    3. Without leaving fdisk, create new partition on this new space. On my specific/particular (?) case, I had to create a primary partition, and specify start/end sectors :
      Command (m for help): n
      Partition type
      	p	primary (1 primary, 1 extended, 2 free)
      	l	logical (numbered from 5)
      Select (default p): p
      Partition number (3,4, default 3):
      First sector (499712-167772159, default 499712): 83884032
      Last sector, +sectors or +size{K,M,G,T,P} (83884032-167772159, default 167772159): 
      
      Created a new partition 3 of type 'Linux' and of size 40 GiB.
    4. Check :
      Command (m for help): p
      Disk /dev/sda: 80 GiB, 85899345920 bytes, 167772160 sectors
      Units: sectors of 1 * 512 = 512 bytes
      Sector size (logical/physical): 512 bytes / 512 bytes
      I/O size (minimum/optimal): 512 bytes / 512 bytes
      Disklabel type: dos
      Disk identifier: 0x26504200
      
      Device	Boot	Start		End		Sectors		Size	Id	Type
      /dev/sda1	*	2048	499711		497664		243M	83	Linux
      /dev/sda2	501758		83884031	83382274	39.8G	5	Extended
      /dev/sda3	83884032	167772159	83888128	40G	83	Linux
      /dev/sda5	501760		83884031	83382272	39.8G	8e	Linux LVM
      
      Partition table entries are not in disk order.		true, but mainly cosmetic. Can be left as-is.
    5. Write the altered partition table and make the kernel aware of it :
      Command (m for help): w
      The partition table has been altered.
      Calling ioctl() to re-read partition table.
      Re-reading the partition table failed.: Device or resource busy
      
      The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).
      partprobe
    6. So now, I have /dev/sda3, with 40GB available. Let's make this a physical volume :
      pvcreate /dev/sda3
      Physical volume "/dev/sda3" successfully created.
    7. Check :
      pvs
      PV		VG		Fmt	Attr	PSize	PFree
      /dev/sda3			lvm2	---	40.00g	40.00g	here it is!
      /dev/sda5	caramba-vg	lvm2	a--	39.76g	0
    8. We'll have to append it to an existing volume group. Which one ?
      vgs
      VG		#PV	#LV	#SN	Attr	VSize	VFree
      caramba-vg	1	5	0	wz--n-	39.76g	0	only 1 VG, so no big trouble
    9. Time to enlarge things (using all of the 40 extra GiB) :
      vgextend caramba-vg /dev/sda3
      Volume group "caramba-vg" successfully extended
    10. Check :
      vgs
      VG		#PV	#LV	#SN	Attr	VSize	VFree
      caramba-vg	2	5	0	wz--n-	79.76g	40.00g
    11. Now we can extend the logical volume in need for GB :
      df -h | grep '/dev/mapper'
      /dev/mapper/caramba--vg-root		8.2G	5.4G	2.4G	70%	/		this one
      /dev/mapper/caramba--vg-home		24G	1014M	22G	5%	/home
      /dev/mapper/caramba--vg-tmp		360M	2.1M	335M	1%	/tmp
      /dev/mapper/caramba--vg-var		2.7G	549M	2.0G	22%	/var
    12. Let's enlarge things (again!) :
      lvextend -L 20G -r /dev/mapper/caramba--vg-root
      Size of logical volume caramba-vg/root changed from 8.38 GiB (2145 extents) to 20.00 GiB (5120 extents).
      Logical volume caramba-vg/root successfully resized.
      resize2fs 1.43.3 (04-Sep-2016)
      Filesystem at /dev/mapper/caramba--vg-root is mounted on /; on-line resizing required
      old_desc_blocks = 1, new_desc_blocks = 2
      The filesystem on /dev/mapper/caramba--vg-root is now 5242880 (4k) blocks long.
    13. Check :
      df -h | grep '/dev/mapper'
      /dev/mapper/caramba--vg-root	20G	5.4G	14G	29%	/
      /dev/mapper/caramba--vg-home	24G 1017M	22G	5%	/home
      /dev/mapper/caramba--vg-tmp	360M	2.1M	335M	1%	/tmp
      /dev/mapper/caramba--vg-var	2.7G	549M	2.0G	22%	/var
    14. I love it when a plan comes together

Ubuntu host with Windows 10 guest

The overall operation is pretty similar :

Description

  1. shutdown the VM in a clean state : stopped, merge snapshots, backup image file(s)
  2. enlarge the virtual disk with VirtualBox commands
  3. boot Windows 10
  4. let it know it may sprawl over some more gigabytes...

Procedure (source) :

  1. If the VM disk file is in .vmdk format, you'll have to convert it to .vdi first :
    cd "$HOME/VirtualBox VMs/vmName/" && VBoxManage clonemedium 'vmName-disk001.vmdk' 'converted.vdi' --format vdi
  2. resize the disk file to 80GB :
    VBoxManage modifymedium 'converted.vdi' --resize $((80*1024))
    You don't have to convert the enlarged disk file back to .vmdk : in VirtualBox GUI, in the Storage page, you'll just have to select the converted.vdi file as the VM hard disk.
  3. boot Windows 10
  4. right-click the desktop icon This PC | Manage | Computer management | Storage | Disk management (administrator privileges required)
  5. right-click the drive to extend : C: | Extend
  6. when prompted where to extend, select the unallocated space at the end of the disk, then - -
mail

How to update the virtual machines storage directory ?

What we're dealing with here is :
storageDir/VM_1/image.vdi
storageDir/VM_2/image.vdi

storageDir/VM_n/image.vdi
To change it :
mail

How to install the Extension Pack and Guest Additions ?

  • The Extension Pack is mandatory to enable extra features such as USB 2.0, dual screen, ...
  • When upgrading VirtualBox, there are 2 things to upgrade / reinstall :
    1. the Extension Pack on the host side
      • nothing prevents from installing this on the guest side, but it looks useless
      • After upgrading VirtualBox itself, the Extension Pack upgrade is generally offered at the next start of the application
    2. Guest Additions on the guest side

On the host side : the Extension Pack

As anybody having administrative rights on the host operating system (root / Administrator) :
  1. virtualboxVersion='5.1.6'; wget http://download.virtualbox.org/virtualbox/$virtualboxVersion/Oracle_VM_VirtualBox_Extension_Pack-${virtualboxVersion}.vbox-extpack
  2. It _may_ be possible to install it graphically :
    1. File | Preferences | Extensions | "Add" icon
    2. pick file | Ok | Upgrade | Agree
    3. Then you'll be prompted for the root / Administrator password, which will fail on GNU/Linux boxes with locked root account.
      Use the CLI install command below
    vboxmanage extpack install --replace Oracle_VM_VirtualBox_Extension_Pack-${virtualboxVersion}.vbox-extpack

On the guest side : Guest Additions

This works the same whether the VM is on or off, no need to shut it down if it's already running.

  1. get the latest version of the VirtualBox Guest Additions CD :
  2. insert the VirtualBox Guest Additions CD into the virtual machine's CD drive
  3. boot the virtual machine
  4. make sure you have installed the kernel headers
  5. as root :
    1. mount the CD :
      mount /media/cdrom
    2. launch install :
      cd /media/cdrom && sh autorun.sh
      • if this complains : Linux guest additions installer not found --try to start them manually.
      • Otherwise it will pop a new terminal window :
        Verifying archive integrity... All good.
        Uncompressing VirtualBox 5.2.18 Guest Additions for Linux........
        VirtualBox Guest Additions installer
        Removing installed version 5.2.16 of VirtualBox Guest Additions...
        Copying additional installer modules ...
        Installing additional modules ...
        VirtualBox Guest Additions: Building the VirtualBox Guest Additions kernel modules. This may take a while.
        VirtualBox Guest Additions: Running kernel modules will not be replaced until the system is restarted
        VirtualBox Guest Additions: Starting.
        VirtualBox Guest Additions: modprobe vboxsf failed		if you see this, read that
        Press Return to close this window...
    3. unmount the CD :
      cd && umount /media/cdrom
  6. eject the disk image from the virtual CD drive
  7. reboot the VM to load modified kernel modules (if the install said so)
  8. enjoy
mail

How to clone a virtual machine ?

Solution

  1. cd path/to/directory/containing/virtual/machines/images
  2. vboxmanage clonehd original.vdi clone.vdi
  3. Then some minor changes (?) to do in the GUI

Alternate solution

In the GUI, find the machine to clone from the list, then Rclick | Clone.
mail

How to perform a P2V ?

  1. dd if=/dev/sdc of=/path/to/diskImage.dd
  2. vboxmanage convertfromraw diskImage.dd diskImage.vdi --format VDI
  3. rm diskImage.dd