VirtualBox: Difference between revisions

From miki
Jump to navigation Jump to search
No edit summary
 
(48 intermediate revisions by the same user not shown)
Line 1: Line 1:
See also: [[VirtualBox]] — [[Vmware]] — [[Kvm]] — [[Qemu]]

== Reference ==
== Reference ==
* http://www.virtualbox.org
* http://www.virtualbox.org
* [http://www.virtualbox.org/manual/ch08.htm Chapter 8. VBoxManage] (all the <tt>VBoxManage</tt> commands)
* [http://www.virtualbox.org/manual/ch08.htm Chapter 8. VBoxManage] (all the <tt>VBoxManage</tt> commands)
* '''[https://forums.virtualbox.org/viewtopic.php?p=29272 All about VDI]'''
* '''[https://forums.virtualbox.org/viewtopic.php?p=29272 All about VDI]'''

=== Terminology ===
;P2V: Physical-2-Virtual. Conversion of an existing (physical) configuration such that it can run in a virtual machine.


== Installation ==
== Installation ==
Line 8: Line 13:
=== Installation using repository ===
=== Installation using repository ===
<ul>
<ul>
<li>For '''VirtualBox 5.x''':</li>
<ul>
<li>Add the following lines to <tt>/etc/apt/sources.list</tt> (here for Ubuntu Trusty &mdash; now in ''contrib''):</li>
deb http://download.virtualbox.org/virtualbox/debian trusty contrib
<li>Add the Oracle public key for apt-secure, then install
<source lang="bash" enclose="prevalid">
wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox_2016.asc -O- | apt-key add - # For Xenial
wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | sudo apt-key add - # For older Ubuntu release
sudo apt-get update
sudo apt-get install virtualbox-5.0
</source>
<li>Download the ''extension pack'' from [http://www.virtualbox.org/wiki/Downloads here], and install it using ''File &rarr; Preferences &rarr; Extensions'' menu, or with</li>
<source lang="bash">
VBoxManage extpack install Oracle_VM_VirtualBox_Extension_Pack-5*.vbox-extpack
</source>

</ul>
<li>For '''VirtualBox 4.x''':</li>
<li>For '''VirtualBox 4.x''':</li>
<ul>
<ul>
Line 50: Line 72:
The group '''vboxusers''' is created during installation.
The group '''vboxusers''' is created during installation.
* Users of the VirtualBox must be member of that group. New users can be added with
* Users of the VirtualBox must be member of that group. New users can be added with
{{pl2|<source lang="bash" enclose="prevalid">sudo usermod -a -G vboxusers username</source>}}
<source lang="bash" enclose="prevalid">sudo usermod -a -G vboxusers username</source>
* The daemon for the network interfaces will be assigned to that group.
* The daemon for the network interfaces will be assigned to that group.


Line 56: Line 78:
VirtualBox requires a kernel module '''vboxdrv'''. ''This module must be '''recompiled''' every time the kernel is updated'' (if not, there will be typically errors related to missing network interfaces).
VirtualBox requires a kernel module '''vboxdrv'''. ''This module must be '''recompiled''' every time the kernel is updated'' (if not, there will be typically errors related to missing network interfaces).
* The default location of the module is given by setting '''KDIR=[fullpath]''' in file <tt>/etc/default/virtualbox</tt>.
* The default location of the module is given by setting '''KDIR=[fullpath]''' in file <tt>/etc/default/virtualbox</tt>.
* The kernel module can be compiled with
* When using Oracle's package, the kernel module can be compiled with
{{pl2|<source lang="bash" enclose="prevalid">
<source lang="bash" enclose="prevalid">
sudo /etc/init.d/vboxdrv setup
sudo /etc/init.d/vboxdrv setup
</source>}}
</source>
:When using virtualbox from Debian/Ubuntu distribution, use
<source lang="bash" enclose="prevalid">
sudo dpkg-reconfigure virtualbox-dkms
</source>

* Enable ''[http://en.wikipedia.org/wikiDynamic_Kernel_Module_Support Dynamic Kernel Module Support]'' to deal with Kernel Upgrade
* Enable ''[http://en.wikipedia.org/wikiDynamic_Kernel_Module_Support Dynamic Kernel Module Support]'' to deal with Kernel Upgrade


Line 97: Line 124:


=== Launch VirtualBox as another user ===
=== Launch VirtualBox as another user ===
* Create user '''vbox''' ('''disabled''', uid '''7000''', group '''vboxusers''' + groups '''cdrom''', <s>'''audio''', </s>'''video''', '''plugdev''', '''fuse''', '''netdev'''). This user will store VirtualBox settings and disks, access bluetooth...
* Create user '''vbox''' ('''disabled''', uid '''7000''', group '''vbox''' + groups '''vboxusers''', '''cdrom''', <s>'''audio''', </s>'''video''', '''plugdev''', '''fuse''', '''netdev'''). This user will store VirtualBox settings and disks, access bluetooth...
sudo groupadd -g 7000 vbox
sudo useradd -u 7000 -g vbox -G vboxusers,cdrom,video,plugdev,fuse,netdev -m -s "" vbox
:Add user ''vbox'' to whatever groups it needs access to. For instance, add to current user group to give read access to current user's files:
sudo gpasswd -a vbox $USER
:Optionally, add current user to group '''vbox''':
sudo gpasswd -a $USER vbox
* To enable '''audio''', configure the current user to accept request from ''vbox'' pulseaudio server (see [[Linux sound#Tips|Linux sound]]). For the current user, edit file <tt>~/.pulse/default.pa</tt>:
* To enable '''audio''', configure the current user to accept request from ''vbox'' pulseaudio server (see [[Linux sound#Tips|Linux sound]]). For the current user, edit file <tt>~/.pulse/default.pa</tt>:
{{pl2|<source lang=text>
<source lang=text>
.include /etc/pulse/default.pa
.include /etc/pulse/default.pa


load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1
load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1
</source>}}
</source>


:Then for user '''vbox''', edit file <tt>/home/vbox/.pulse/client.conf</tt>:
:Then for user '''vbox''', edit file <tt>/home/vbox/.pulse/client.conf</tt>:
{{pl2|<source lang=text>
<source lang=text>
default-server = 127.0.0.1
default-server = 127.0.0.1
</source>}}
</source>


* If user '''vbox''' needs to access a partition directly (e.g. because using a raw drive), give '''RW access''' on the selected partition. For instance, to give RW access to partition <tt>/dev/sda2</tt>, create a new '''udev''' rule in file <tt>/etc/udev/rules.d/99-vbox.rules</tt>:
* If user '''vbox''' needs to access a partition directly (e.g. because using a raw drive), give '''RW access''' on the selected partition. For instance, to give RW access to partition <tt>/dev/sda2</tt>, create a new '''udev''' rule in file <tt>/etc/udev/rules.d/99-vbox.rules</tt>:
{{pl2|<source lang="bash">
<source lang="bash">
# Rules to change the ownership of /dev/sda2 to user "vbox" (vbox will boot /dev/sda2 in VirtualBox + follow least privilege principle)
# Rules to change the ownership of /dev/sda2 to user "vbox" (vbox will boot /dev/sda2 in VirtualBox + follow least privilege principle)
# reference: http://www.reactivated.net/writing_udev_rules.html#ownership
# reference: http://www.reactivated.net/writing_udev_rules.html#ownership
KERNEL=="sda2", SUBSYSTEM=="block", OWNER="vbox"
KERNEL=="sda2", SUBSYSTEM=="block", OWNER="vbox"
</source>}}
</source>
* Allow users to launch VirtualBox through user ''vbox''. Add to <tt>/etc/sudoers</tt>:
* Allow current user MYUSER to launch VirtualBox through user ''vbox''. Add to <tt>/etc/sudoers</tt>:
{{pl2|<source lang="text">ALL ALL=NOPASSWD: /bin/su vbox -c /usr/bin/VirtualBox</source>}}
<source lang="text">MYUSER ALL = (vbox) NOPASSWD: /usr/bin/virtualbox</source>
* Helper function to add to e.g. <tt>~/.bashrc</tt> :
* Helper script e.g. <tt>~/vbox.sh</tt> :
{{pl2|<source lang="bash" enclose=prevalid>
<source lang="bash">
#First allow user vbox to connect to X11
function winbox() {
xhost +SI:localuser:vbox
#First allow user vbox to connect to X11
exec sudo -H -u vbox /usr/bin/virtualbox&
xhost +SI:vbox
</source>
sudo su vbox -c /usr/bin/virtualbox&
}
</source>}}


* Add a launcher {{file|~/.local/share/applications/virtualbox-vbox.desktop}} (chmod 755, copied from {{file|/usr/share/applications/virtualbox.desktop}}:
== Creating a New Virtual Machine ==
<source lang=bash>
* To boot a Virtual Machine directly from a physical disk partitions, see section '''Advanced storage configuration''' in [http://www.virtualbox.org/manual/ch09.html Ch. 9 Advanced topics].
#!/usr/bin/env xdg-open


[Desktop Entry]
== Guests Installation ==
Version=1.0
=== Windows Guest ===
Type=Application
;Enabling 3D accelerated graphics
Terminal=false
:To enable 3D accelerated graphics, [http://www.virtualbox.org/manual/ch04.html#id2659680 install the GuestAdditions in '''SAFE mode''']. Otherwise Windows file protection mechanism will revert the files to the original ones.
Icon[en_US]=virtualbox
Name[en_US]=Oracle VM VirtualBox (vbox user)
Exec=/home/peetersm/bin/vbox.sh
Comment[en_US]=Run several virtual systems on a single host computer
Name=Oracle VM VirtualBox (vbox user)
Comment=Run several virtual systems on a single host computer
Icon=virtualbox
</source>


=== Migrating Windows ===
== Disable autostarting ==
If virtualbox is autostarting (and fails), remove it with:
Reference: http://www.virtualbox.org/wiki/Migrate_Windows
<source lang=bash>
sudo update-rc.d -f virtualbox remove
</source>
'''{{red|WRONG}}''' &mdash; This all disable other features!


== Headless mode ==
* Check HAL (enable or disable ''IO APIC'' in virtualbox as a result, or update the HAL in Windows by changing the files)
To start Virtualbox in headless mode [https://www.virtualbox.org/manual/ch07.html] (i.e. without any graphical interface on the host):
* Run MergeIDE to avoid the error message <code>STOP: 0x0000007B (0xF741B84C,0xC0000034,0x00000000,0x00000000) INACCESSIBLE_BOOT_DEVICE</code>
* Via <code>vboxmanage</code> (preferred method, starts in background):
* Delete <tt>agp440.sys</tt> (necessary on WinXP) and if needed <tt>intelppm.sys</tt>
VBoxManage startvm "VM name" --type headless
* Or running <code>VBoxHeadless</code> directly (might be helpful to catch early error messages).
<source lang=bash>
VBoxHeadless --startvm <uuid|name> # This enables VRDE by default
</source>
:In this mode, VRDE is always enabled by default, while it is enabled according to the VM config in other methods. Change this with option <code>--vrde</code>.
<source lang=bash>
VBoxHeadless --startvm <uuid|name> --vrde off # To explicitly disable VRDE
VBoxHeadless --startvm <uuid|name> --vrde config # To enable/disable VRDE according to config
</source>
* Or in the GUI, by holding the {{kb|Shift}} key while starting the VM.


== Windows guest installation ==
Other links:
=== Miscellaneous ===
* http://www.techspot.com/vb/topic21511.html
* For advanced installation like P2V (Physical-to-Virtual) or booting a VM directly from a physical disk partition, see section '''Advanced storage configuration''' in [http://www.virtualbox.org/manual/ch09.html Ch. 9 Advanced topics].
* http://forums.virtualbox.org/viewtopic.php?f=7&t=19847
* To enable 3D accelerated graphics, [http://www.virtualbox.org/manual/ch04.html#id2659680 install the GuestAdditions in '''SAFE mode''']. Otherwise Windows file protection mechanism will revert the files to the original ones.
* http://forum.driverpacks.net/viewtopic.php?id=3865
* http://ubuntuforums.org/showthread.php?t=769883


=== Migrating Windows 2000 ===
=== Windows 2000 P2V ===
Here are the steps I followed to migrate my W2K partition to a VirtualBox machine with .VDI disk:
Here are the steps I followed to migrate my W2K partition to a VirtualBox machine with .VDI disk:
* First copy the W2K partition (say <tt>/dev/sda1</tt>) to some file
* First copy the W2K partition (say <tt>/dev/sda1</tt>) to some file
Line 173: Line 227:
* http://support.microsoft.com/kb/822052
* http://support.microsoft.com/kb/822052


=== Booting Existing Windows XP from Physical Partition ===
=== Windows XP P2V ===
Reference: http://www.virtualbox.org/wiki/Migrate_Windows

* Check HAL (enable or disable ''IO APIC'' in virtualbox as a result, or update the HAL in Windows by changing the files)
* Run MergeIDE to avoid the error message <code>STOP: 0x0000007B (0xF741B84C,0xC0000034,0x00000000,0x00000000) INACCESSIBLE_BOOT_DEVICE</code>
* Delete <tt>agp440.sys</tt> (necessary on WinXP) and if needed <tt>intelppm.sys</tt>

Other links:
* http://www.techspot.com/vb/topic21511.html
* http://forums.virtualbox.org/viewtopic.php?f=7&t=19847
* http://forum.driverpacks.net/viewtopic.php?id=3865
* http://ubuntuforums.org/showthread.php?t=769883

=== Windows XP P2V - from Physical Partition ===
Host Setup
Host Setup
* Dell Latitude E6500 (with virtualization instructions)
* Dell Latitude E6500 (with virtualization instructions)
Line 188: Line 255:
* First, as described in [http://www.virtualbox.org/wiki/Migrate_Windows], '''delete <tt>agp440.sys</tt>''' (or rename) from <tt>C:\Windows\system32\drivers</tt>. This driver will conflict with the virtual video card installed by VirtualBox and will make it crash.
* First, as described in [http://www.virtualbox.org/wiki/Migrate_Windows], '''delete <tt>agp440.sys</tt>''' (or rename) from <tt>C:\Windows\system32\drivers</tt>. This driver will conflict with the virtual video card installed by VirtualBox and will make it crash.
* Next, create the raw disk as a vmdk volume. Update the MBR code to remove GRUB. <font color="red">Repeat this every time the partitions change</font>:
* Next, create the raw disk as a vmdk volume. Update the MBR code to remove GRUB. <font color="red">Repeat this every time the partitions change</font>:
{{pl2|<source lang="bash" enclose="prevalid">
<source lang="bash" enclose="prevalid">
sudo dd if=/dev/sda of=vm.mbr bs=512 count=1
sudo dd if=/dev/sda of=vm.mbr bs=512 count=1
sudo apt-get install mbr # For command "install-mbr"
sudo apt-get install mbr # For command "install-mbr"
sudo install-mbr -i n -p D -t 0 vm.mbr
sudo install-mbr -i n -p D -t 0 vm.mbr
sudo VBoxManage internalcommands createrawvmdk -filename nxl67002-sda2-mbr.vmdk -rawdisk /dev/sda -partitions 2 -relative -mbr vm.mbr
sudo VBoxManage internalcommands createrawvmdk -filename nxl67002-sda2-mbr.vmdk -rawdisk /dev/sda -partitions 2 -relative -mbr vm.mbr
</source>}}
</source>
* Boot the virtual machine with the raw disk above. When Windows is launched, install the '''VirtualBox guest additions'''.
* Boot the virtual machine with the raw disk above. When Windows is launched, install the '''VirtualBox guest additions'''.


Line 203: Line 270:
* [http://www.vistaclues.com/enable-bitlocker-without-a-tpm/ Enable BitLocker Without a TPM - Windows Vista and Windows 7 Help]
* [http://www.vistaclues.com/enable-bitlocker-without-a-tpm/ Enable BitLocker Without a TPM - Windows Vista and Windows 7 Help]


=== Windows 7 P2V (Physical-to-Virtual) Migration ===
=== Windows 7 P2V ===


* Disable BitLocker or any other disk encryption, if present.
'''Refererences - tips'''
* Remove restore points, hibernat.sys, cache file, etc.
*
* Shrink system partition (either via DISKPART.EXE or ntfsresize/gparted)
** Run MergeIDE
* Backup all partitions (with <code>ntfsclone</code>)
** Delete AGP440.SYS, intelppm.sys
* Fix bad BCD (Windows Boot Manager Error) &mdash; Boot once with Windows Recovery CD, and let auto-repair run
**...
* Fix bad BCD (BSOD 0x0000007B) &mdash; Boot again with Windows Recovery CD, go to command prompt:
* https://forums.virtualbox.org/viewtopic.php?f=6&t=33476 &mdash; BSOD x0000007B
<source lang=winbatch>
** Select another virtual disk controller, i.e. either ''IDE PIIX3'' or ''PIIX4''.
bcdedit /export C:\BCD_Backup
ren c:\boot/BCD bcd2.old
bootrec /rebuildbcd
</source>
* Fix missing drivers (BSOD 0x0000007B) &mdash; Still within Windows Recovery CD, import the following in '''regedit'''.
** If needed, create this file using a Linux Live CD.
** In regedit, mount SYSTEM hive as <tt>remote_SYSTEM</tt>, and merge file above into it.
** Then don't forget to unmount the hive.
<source lang=reg>
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\remote_SYSTEM\ControlSet001\Control\Class\{4D36E96A-E325-11CE-BFC1-08002BE10318}\0000]
"InfPath"="mshdc.inf"
"InfSection"="msahci_Inst"
"ProviderName"="Microsoft"
"DriverDateData"=hex:00,80,8c,a3,c5,94,c6,01
"DriverDate"="6-21-2006"
"DriverVersion"="6.1.7600.16385"
"MatchingDeviceId"="pci\\cc_010601"
"DriverDesc"="Standard AHCI 1.0 Serial ATA Controller"
"Migrated"=dword:00000001

[HKEY_LOCAL_MACHINE\remote_SYSTEM\ControlSet001\Control\PnP]
"DisableCDDB"=-

[HKEY_LOCAL_MACHINE\remote_SYSTEM\ControlSet001\services\atapi]
"Start"=dword:00000000

[HKEY_LOCAL_MACHINE\remote_SYSTEM\ControlSet001\services\msahci]
"Start"=dword:00000000
</source>
* Boot virtual box, and '''wait''' for all devices to be detected.
* '''DO NOT''' reboot when prompted, but instead start '''regedit''' again:
<source lang=reg>
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\nxl_SYSTEM\ControlSet001\Control\PnP]
"DisableCDDB"=-
</source>

'''Detailed troubleshooting of startup sequence''':
* See [[Windows 7 boot troubleshooting]].

'''Other general tips''':
* Run <code>MergeIDE</code>
* Delete AGP440.SYS, intelppm.sys
In case of <code>BSOD x0000007B</code>:
* Select another virtual disk controller, i.e. either ''IDE PIIX3'' or ''PIIX4'' [https://forums.virtualbox.org/viewtopic.php?f=6&t=33476]

=== Windows VMWare P2V Checklist ===
* [http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1016330 Troubleshooting checklist for VMware Converter (1016330)]

=== Windows 10 P2V ===
* Use VMWare Converter.

== Linux guest installation ==
=== Install VirtualBox Additions ===

First insert the CDROM using the Virtualbox Devices menu (''Insert Guest Additioons CD image...'').
Then:
<source lang=bash>
sudo mount /dev/cdrom /media/cdrom
sudo ./VBoxLinuxAdditions.run
</source>

=== Linux P2V on Windows Host ===

* Create a raw disk file from secondary sata drive (from [http://romaimperator.com/?p=29])
* Start ''Windows Disk Management'', and identify index of secondary drive (say <tt>Disk 1</tt>)
* Start {{file|cmd.exe}} as ''Administrator'', and type
<source lang=winbatch>
"c:\Program Files\Oracle\VirtualBox\VBoxManage.exe" internalcommands createrawvmdk -filename "filename.vmdk" -rawdisk "\\.\\PhysicalDrive1"
</source>
* Start VirtualBox as ''Administrator'', and add the raw disk to the virtual machine.


== How-To ==
== How-To ==
Line 230: Line 371:
* To bring the drive back online, open the ''Disk Management'' control panel
* To bring the drive back online, open the ''Disk Management'' control panel
* Click right on the drive, and select ''Online''.<br/>Doing so, Windows will generate a new ID and write it in the volume boot record.
* Click right on the drive, and select ''Online''.<br/>Doing so, Windows will generate a new ID and write it in the volume boot record.

=== Compact a dynamic .VDI disk ===
There are two options:

;Use [https://forums.virtualbox.org/viewtopic.php?f=6&t=22422 CloneVDI]
* ''CloneVDI'' is a very fast and easy-to-use tool to clone & compact a VDI disk. It is windows-only, but supports disk with Linux filesystem.
* After execution, we still have a difference between <code>df -h</code> and vdi size. Maybe CloneVDI did not clear the swap partition, so let's do that ourselves:
sudo swapoff -a
sudo sfdisk -l /dev/sda2
sudo dd if=/dev/zero of=/dev/sda2p1 bs=20M
halt
:Then in host:
vboxmanage mydisk.vdi --compact
:... well, turns out we don't gain anything, so maybe CloneVDI did a good job already?

;Use zerofree then compact
* ''zerofree'' is a linux utility that writes back to zero all non-zero free block on filesystem. Then we use vboxmanage to compact the image [https://askubuntu.com/questions/444598/how-do-i-shrink-a-dynamically-growing-vdi-disk-from-virtualbox], [https://superuser.com/questions/529149/how-to-compact-virtualboxs-vdi-file-size].

=== Grow an existing .VDI disk ===

The principle is to create a bigger {{file|.vdi}} disk and copy the old into the new one [https://undercovertechguy.wordpress.com/2010/10/21/growing-a-virtualbox-vdi-is-easy/]:

* Create the new {{file|.vdi}} disk.
* Release the old {{file|.vdi}} from any virtual machine.
* Clone the old into the new {{file|.vdi}} file:
VBoxManage clonehd --existing OLD.vdi NEW.vdi
* Now grow the partitions in the new disk using a tool like <code>gparted</code>.

=== Detach / attach USB and network ===
When using NAT network and Bluetooth USB on a Windows guest, it is recommended to detach / attach these devices when the network environment changed on the host, or after suspend/resume. The usual method is to go in the GUI. However we can also do via command-line using <code>vboxmanage</code>.

First we need the VM ''name'' and USB device ''UUID'':
<source lang=bash>
vboxmanage list runningvms # List our running vms - write down VM name
vboxmanage list usbhost # List available uid device - write down UID of Bluetooth devices
</source>

In our case, VM name is <code>WINGUEST</code> and USB UUID is <code>0cd50991-b2ad-4a2b-86cb-da08772bd5e2</code>. So now, we can write out script to automatically reset the connections when necessary:
<source lang=bash>
#! /bin/bash

vboxmanage controlvm WINGUEST setlinkstate1 off # Detach network cable (on 1st interface)
vboxmanage controlvm WINGUEST usbdetach 0cd50991-b2ad-4a2b-86cb-da08772bd5e2 # Detach USB bluetooth

sleep 3 # Wait 3 sec

vboxmanage controlvm WINGUEST setlinkstate1 on # Attach back network cable
vboxmanage controlvm WINGUEST usbattach 0cd50991-b2ad-4a2b-86cb-da08772bd5e2 # Attach back USB bluetooth
</source>
Note that we use <code>controlvm</code> and '''not''' <code>modifyvm</code>, which as the documentation explains, is only for VMs that are not running. Using the latter gives an error about ''locked session'':
VBoxManage: error: The machine 'WINGUEST' is already locked for a session (or being unlocked)

If the VM is running as another user (say <code>vbox</code>), and we want to extract the UUID automatically, we can adapt the script as follows :

<source lang=bash>
#! /bin/bash

# Detach / attach network cable / bluetooth on WINGUEST vm
# (to reset guest after suspend/resume or when network env. changed on the host)

# Extract Bluetooth USB UUID
BTUUID=$(sudo -Hu vbox /usr/bin/vboxmanage list usbhost|egrep -i "uuid|manufacturer"|sed -rn 'N; s/\n/ /; /Broadcom Corp/!d; s/.* ([0-9a-f-]+) .*/\1/; p')

echo "Detaching network and bluetooth..."
sudo -Hu vbox /usr/bin/vboxmanage controlvm WINGUEST setlinkstate1 off # Detach network cable (on 1st interface)
sudo -Hu vbox /usr/bin/vboxmanage controlvm WINGUEST usbdetach $BTUUID # Detach USB bluetooth

echo "Waiting..."
sleep 3 # Wait 3 sec

echo "Attaching network and bluetooth..."
sudo -Hu vbox /usr/bin/vboxmanage controlvm WINGUEST setlinkstate1 on # Attach back network cable
sudo -Hu vbox /usr/bin/vboxmanage controlvm WINGUEST usbattach $BTUUID # Attach back USB bluetooth
</source>

And add the following to {{file|/etc/sudoers}}, which only gives permission to user <code>john</code>:

<pre>
john ALL = (vbox) NOPASSWD: /usr/bin/vboxmanage list usbhost
john ALL = (vbox) NOPASSWD: /usr/bin/vboxmanage controlvm WINGUEST usbattach *
john ALL = (vbox) NOPASSWD: /usr/bin/vboxmanage controlvm WINGUEST usbdetach *
john ALL = (vbox) NOPASSWD: /usr/bin/vboxmanage controlvm WINGUEST setlinkstate1 *
</pre>

=== Clone and diversify a linux guest ===
* Change network adapter MAC address.
* Change clone hostname
<source lang=bash>
sudo vi /etc/hostname
sudo vi /etc/hosts
</source>
:usually changing the hostname has further impact. Check for all files (in particular in {{file|/etc}} whose name contains the old hostname, and / or contains the old hostname.

* Refresh SSL server key and certificate (see [[Apache]])
* Refresh SSH server key (see [[SSH]])

=== Clone and compact a Windows 10 disk (UEFI) ===
The source disk is a 500GB VMDK virtual disk containing the following partitions:
* A '''EFI''' partition (FAT32), containing the UEFI boot files.
* A '''Windows''' system partition (NTFS), containing the Windows 10 installation.
* A '''Recovery''' partition (NTFS).
* An '''Ubuntu''' partition (Ext4).

We want to clone it on a new 100GB VDI disk, keeping only the first two partitions. Most work can be done using '''gparted''' on a Live Ubuntu DVD, but some care are required to make sure that Windows will boot correctly (avoid the dreadful 0xc000000e blue screen error).

First boot with a '''Ubuntu Live DVD''' and launch '''gparted'''. We assume that the source disk is <code>/dev/sda</code> and the target disk is <code>/dev/sdb</code>.
* Select <code>/dev/sdb</code>, and create a new '''gpt''' partition table (Device menu).
* Then use the copy-paste feature of gparted to copy partitions <code>/dev/sda1</code> and <code>/dev/sda2</code> to the target disk. If necessary, resize the Windows system partition on the target disk to the available space.
* Finally, accept the changes and wait for the copy to complete.
* When done, we must edit the ''flags'' of the first partition, and select '''boot''' + '''esp''', so that the partition will be seen as an EFI partition.

Next, we need to remove Grub from the target disk:
<source lang="bash">
sudo mount /dev/sda1 /mnt
cd /mnt/EFI
sudo rm -rf ubuntu
sudo cp Windows/Boot/bootmgfw.efi Boot/bootx64.efi
cd
sudo umount /mnt
</source>
Note we use {{file|bootmgfw.efi}} since we are on a UEFI system. The file {{file|bootmgr.efi}} is for BIOS firmware [https://superuser.com/questions/1261860/difference-between-bootmgr-efi-and-bootmgfw-efi].

Finally, we need to fix the UUID on the target disk or Windows will not find the system disk.
* In a terminal, get the disk UUID and the partition UUID of both partitions:
<source lang="bash">
sudo sgdisk -p /dev/sda # Disk UUID xxxxxxxx-...
sudo sgdisk -u 1 /dev/sda # Partition unique GUID aaaaaaaa-...
sudo sgdisk -u 2 /dev/sda # Partition unique GUID bbbbbbbb-...
</source>
* Apply the disk UUID on the target disk:
<source lang="bash">
sudo sgdisk -U xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx /dev/sdb
</source>
* Apply the Partition unique GUID on the target disk:
<source lang="bash">
sudo sgdisk -u 1:aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa /dev/sdb
sudo sgdisk -u 2:bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb /dev/sdb
</source>
* Apply the partition labels on the target disk:
<source lang="bash">
sudo sgdisk -c 1:"Efi System" /dev/sdb
sudo sgdisk -c 2:"Basic Data" /dev/sdb
</source>

Now, we can boot the new cloned disk and Windows won't complain on the way.


== Benchmarks ==
== Benchmarks ==
Line 283: Line 569:
ls -Rl /dev/vboxusb
ls -Rl /dev/vboxusb
</source>
</source>

I experienced some strange problems with dev. tools on a windows guest:
- When running on a windows host, the connectivity with the device was flawless
- When running on a linux host, the connection only worked once, then failed.

The fix was to edit the BIOS settings:
* Disable '''USB 3.0''' support
* Disable '''USB legacy''' support

=== CIFS - Connection lost on linux guest ===

Guest mount via
# /etc/fstab
//10.0.2.2/c$ /mnt/c cifs username=beq....,uid=1000,gid=1000,iocharset=utf8,sec=ntlm,noauto 0 0

Tell windows to disable autodisconnect (see [http://support.microsoft.com/kb/297684#LetMeFixItMyselfAlways]):
* via regedit:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters => ffffffff (default 0...0f) (unit = minutes)
* or via command-line:
net config server /autodisconnect:-1

=== /dev/vboxdrv missing ===
Error message:
WARNING: The character device /dev/vboxdrv does not exist.

Solution [http://askubuntu.com/questions/465454/problem-with-the-installation-of-virtualbox]:
<source lang=bash>
sudo dpkg-reconfigure virtualbox-dkms
sudo dpkg-reconfigure virtualbox
sudo modprobe vboxdrv
sudo modprobe vboxnetflt
</source>

=== Graphic issues and artifacts with 3D acceleration and Ubuntu guests ===
;Links
* [https://www.virtualbox.org/ticket/12941 ticket 12941 - 3D not working on Ubuntu 14.04 -> fixed as of 27 May 2014 in VirtualBox 4.3.x and later releases after 27 May 2014]

As a fix, make sure that <code>glxinfo</code> reports the following lines
OpenGL vendor string: Humper
OpenGL renderer string: Chromium

Those lines are normal:
libGL error: core dri or dri2 extension not found
libGL error: failed to load driver: vboxvideo

Many commenters reports the artifact issue, also in Vim [https://www.virtualbox.org/ticket/12941#comment:24], [https://www.virtualbox.org/ticket/12941#comment:45], [https://www.virtualbox.org/ticket/12941#comment:49].
* To fix the artifacts issue, '''disable Framebuffer Object in Compiz Settings Manager &rarr; OpenGL''' [https://www.virtualbox.org/ticket/12941#comment:72]

=== Guest network lost after host suspend ===
Possible workaround on 4.3.8 and later (see bug [https://www.virtualbox.org/ticket/11755 11755] and [https://www.virtualbox.org/ticket/12441 12441]):
<source lang=bash>
VBoxManage modifyvm "VM name" --natdnshostresolver1 on
</source>

=== Mouse click issues ===
Example of issues:
* Left clicks behave as right clicks.
* Left clicks react as if selected from top left corner to clicked point.

Workaround:
* Press once on {{kb|R-Ctrl}}.
* In '''multi-screen''' setup, undock then redock while VM is active. After redock, must {{kb|Ctrl-F}} twice to fix fullscreen mode.

See also [[Common Issues]].


== Bugtracker ==
== Bugtracker ==

Latest revision as of 07:11, 18 October 2021

See also: VirtualBoxVmwareKvmQemu

Reference

Terminology

P2V
Physical-2-Virtual. Conversion of an existing (physical) configuration such that it can run in a virtual machine.

Installation

Reference instructions can be found here.

Installation using repository

  • For VirtualBox 5.x:
    • Add the following lines to /etc/apt/sources.list (here for Ubuntu Trusty — now in contrib):
    • deb http://download.virtualbox.org/virtualbox/debian trusty contrib
    • Add the Oracle public key for apt-secure, then install
      wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox_2016.asc -O- | apt-key add -    # For Xenial
      wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | sudo apt-key add -    # For older Ubuntu release
      sudo apt-get update
      sudo apt-get install virtualbox-5.0
      
    • Download the extension pack from here, and install it using File → Preferences → Extensions menu, or with
    • VBoxManage extpack install Oracle_VM_VirtualBox_Extension_Pack-5*.vbox-extpack
      
  • For VirtualBox 4.x:
    • Add the following lines to /etc/apt/sources.list (here for Ubuntu Lucid — now in contrib):
    • deb http://download.virtualbox.org/virtualbox/debian lucid contrib
      
    • Add the Oracle public key for apt-secure, then install
      wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | sudo apt-key add -
      sudo apt-get update
      sudo apt-get install virtualbox-4.0
      
    • Download the extension pack from here, and install it using File → Preferences → Extensions menu, or with
    • VBoxManage extpack install Oracle_VM_VirtualBox_Extension_Pack-4.0.0-69151.vbox-extpack
      
  • For VirtualBox 3.2.x:
    • Add the following lines to /etc/apt/sources.list (here for Ubuntu Lucid):
    • deb http://download.virtualbox.org/virtualbox/debian lucid non-free
      
    • Add the Oracle public key for apt-secure, then install
      wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | sudo apt-key add -
      sudo apt-get update
      sudo apt-get install virtualbox-3.2
      
  • For VirtualBox 3.1.x:
    • Add the following lines to /etc/apt/sources.list (here for Ubuntu Karmic):
    • deb http://download.virtualbox.org/virtualbox/debian karmic non-free
      
    • Add the Sun public key for apt-secure, then install
      wget -q http://download.virtualbox.org/virtualbox/debian/sun_vbox.asc -O- | sudo apt-key add -
      sudo apt-get update
      sudo apt-get install virtualbox-3.1
      

The group vboxusers is created during installation.

  • Users of the VirtualBox must be member of that group. New users can be added with
sudo usermod -a -G vboxusers username
  • The daemon for the network interfaces will be assigned to that group.

Recompiling the vboxdrv module after kernel update

VirtualBox requires a kernel module vboxdrv. This module must be recompiled every time the kernel is updated (if not, there will be typically errors related to missing network interfaces).

  • The default location of the module is given by setting KDIR=[fullpath] in file /etc/default/virtualbox.
  • When using Oracle's package, the kernel module can be compiled with
sudo /etc/init.d/vboxdrv setup
When using virtualbox from Debian/Ubuntu distribution, use
sudo dpkg-reconfigure virtualbox-dkms

Fixing DMKS issues

If you get error like

$ sudo /etc/init.d/vboxdrv setup
[sudo] password for beq06659: 
 * Stopping VirtualBox kernel                                 [ OK ] 
 * Uninstalling old VirtualBox DKMS kernel modules   
Error! Could not locate dkms.conf file.
File:  does not exist.                                        [ OK ]
 * Trying to register the VirtualBox kernel modules using DKMS
Error! DKMS tree already contains: vboxhost-4.2.12
You cannot add the same module/version combo more than once.

 * Failed, trying without DKMS
 * Recompiling VirtualBox kernel                              [ OK ] 
 * Starting VirtualBox kernel                                 [ OK ] 

See [1] (and then [2] [3]). In my case:

cd /var/lib/dkms/vboxhost
sudo rm -rf 4.2.10
sudo rm kernel*                # Delete all invalid symlinks

Configure ALSA on Host

On a Linux host, the standard VirtualBox interface allows to use ALSA as host driver, but doesn't let to specify the parameters. These can be given through environment variables [4]:

VBoxManage list vms
# "Winmachin-chose" {bfcc1fa1-d957-46a0-8cb6-899412a61fd7}
VBOX_ALSA_DAC_DEV="hw:2,0" VBOX_ALSA_ADC_DEV="hw:2,0" VBoxManage startvm bfcc1fa1-d957-46a0-8cb6-899412a61fd7

Launch VirtualBox as another user

  • Create user vbox (disabled, uid 7000, group vbox + groups vboxusers, cdrom, audio, video, plugdev, fuse, netdev). This user will store VirtualBox settings and disks, access bluetooth...
sudo groupadd -g 7000 vbox
sudo useradd -u 7000 -g vbox -G vboxusers,cdrom,video,plugdev,fuse,netdev -m -s "" vbox
Add user vbox to whatever groups it needs access to. For instance, add to current user group to give read access to current user's files:
sudo gpasswd -a vbox $USER
Optionally, add current user to group vbox:
sudo gpasswd -a $USER vbox
  • To enable audio, configure the current user to accept request from vbox pulseaudio server (see Linux sound). For the current user, edit file ~/.pulse/default.pa:
.include /etc/pulse/default.pa

load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1
Then for user vbox, edit file /home/vbox/.pulse/client.conf:
default-server = 127.0.0.1
  • If user vbox needs to access a partition directly (e.g. because using a raw drive), give RW access on the selected partition. For instance, to give RW access to partition /dev/sda2, create a new udev rule in file /etc/udev/rules.d/99-vbox.rules:
# Rules to change the ownership of /dev/sda2 to user "vbox" (vbox will boot /dev/sda2 in VirtualBox + follow least privilege principle)
# reference: http://www.reactivated.net/writing_udev_rules.html#ownership
KERNEL=="sda2", SUBSYSTEM=="block", OWNER="vbox"
  • Allow current user MYUSER to launch VirtualBox through user vbox. Add to /etc/sudoers:
MYUSER	ALL = (vbox) NOPASSWD: /usr/bin/virtualbox
  • Helper script e.g. ~/vbox.sh :
#First allow user vbox to connect to X11
xhost +SI:localuser:vbox
exec sudo -H -u vbox /usr/bin/virtualbox&
  • Add a launcher ~/.local/share/applications/virtualbox-vbox.desktop (chmod 755, copied from /usr/share/applications/virtualbox.desktop:
#!/usr/bin/env xdg-open

[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Icon[en_US]=virtualbox
Name[en_US]=Oracle VM VirtualBox (vbox user)
Exec=/home/peetersm/bin/vbox.sh
Comment[en_US]=Run several virtual systems on a single host computer
Name=Oracle VM VirtualBox (vbox user)
Comment=Run several virtual systems on a single host computer
Icon=virtualbox

Disable autostarting

If virtualbox is autostarting (and fails), remove it with:

sudo update-rc.d -f virtualbox remove

WRONG — This all disable other features!

Headless mode

To start Virtualbox in headless mode [5] (i.e. without any graphical interface on the host):

  • Via vboxmanage (preferred method, starts in background):
VBoxManage startvm "VM name" --type headless
  • Or running VBoxHeadless directly (might be helpful to catch early error messages).
VBoxHeadless --startvm <uuid|name>                # This enables VRDE by default
In this mode, VRDE is always enabled by default, while it is enabled according to the VM config in other methods. Change this with option --vrde.
VBoxHeadless --startvm <uuid|name> --vrde off     # To explicitly disable VRDE
VBoxHeadless --startvm <uuid|name> --vrde config  # To enable/disable VRDE according to config
  • Or in the GUI, by holding the Shift key while starting the VM.

Windows guest installation

Miscellaneous

  • For advanced installation like P2V (Physical-to-Virtual) or booting a VM directly from a physical disk partition, see section Advanced storage configuration in Ch. 9 Advanced topics.
  • To enable 3D accelerated graphics, install the GuestAdditions in SAFE mode. Otherwise Windows file protection mechanism will revert the files to the original ones.

Windows 2000 P2V

Here are the steps I followed to migrate my W2K partition to a VirtualBox machine with .VDI disk:

  • First copy the W2K partition (say /dev/sda1) to some file
sudo if=/dev/sda1 bs=4096 | gzip -c > w2k-sda1.dd.gz
  • On the host, create an empty .VDI file of sufficient size.
  • Attach the empty VDI to a new virtual machine, and boot from a Ubuntu Live CD or equivalent.
  • Create a new NTFS partition on the empty disk (with gparted), and copy the partition above:
scp user@machine:/path/to/w2k-sda1.dd.gz >(gzip -d -c | sudo dd of=/dev/sda1 bs=4096)
  • Again with gparted, resize the NTFS partition to full size (shrink then grow back if needed), and set the flag 'boot'.
  • Shutdown the virtual machine, and attach an existing W2K disk to the virtual machine as master, and the migrated partition disk as slave. Start the machine.
  • Delete the agp440.sys driver if present on the slave disk.
  • Follow the mergeide.reg procedure here. Basically:
    • Run regedt32, select HKEY_LOCAL_MACHINE, and load hive /WINNT/system32/config/system from slave disk in a key named aaaa. Close registry editor.
    • Click on mergeide.reg to register the new keys.
    • Run regedt32, and unload the hive aaaa. Close the editor.
  • Stop the machine, remove the existing W2K disk, and set the migrated partition as master. Start the machine.

Reference links:

Windows XP P2V

Reference: http://www.virtualbox.org/wiki/Migrate_Windows

  • Check HAL (enable or disable IO APIC in virtualbox as a result, or update the HAL in Windows by changing the files)
  • Run MergeIDE to avoid the error message STOP: 0x0000007B (0xF741B84C,0xC0000034,0x00000000,0x00000000) INACCESSIBLE_BOOT_DEVICE
  • Delete agp440.sys (necessary on WinXP) and if needed intelppm.sys

Other links:

Windows XP P2V - from Physical Partition

Host Setup

  • Dell Latitude E6500 (with virtualization instructions)
  • VirtualBox 3.2.0, Ubuntu 10.04 Lucid Lynx

Guest Setup

  • Windows XP SP3
  • Base Memory 1536 MB, 2 cpus, VT-x/AMD-V enabled, Nested Paging enabled
  • Video memory 48 MB, 2 monitors, 3D accel enabled, 2D accel enabled
  • Audio host driver PulseAudio, controller ICH AC97
  • Network PCnet-FAST III, bridged, eth0

Let's say the Windows partition is on partition /dev/sda2.

  • First, as described in [6], delete agp440.sys (or rename) from C:\Windows\system32\drivers. This driver will conflict with the virtual video card installed by VirtualBox and will make it crash.
  • Next, create the raw disk as a vmdk volume. Update the MBR code to remove GRUB. Repeat this every time the partitions change:
sudo dd if=/dev/sda of=vm.mbr bs=512 count=1
sudo apt-get install mbr                                 # For command "install-mbr"
sudo install-mbr -i n -p D -t 0 vm.mbr
sudo VBoxManage internalcommands createrawvmdk -filename nxl67002-sda2-mbr.vmdk -rawdisk /dev/sda -partitions 2 -relative -mbr vm.mbr
  • Boot the virtual machine with the raw disk above. When Windows is launched, install the VirtualBox guest additions.

This is done! When booting Windows natively, the VirtualBox guest additions will detect that VirtualBox is not running and will disable itself.

Windows Guest with BitLocker

Some interesting links:

Windows 7 P2V

  • Disable BitLocker or any other disk encryption, if present.
  • Remove restore points, hibernat.sys, cache file, etc.
  • Shrink system partition (either via DISKPART.EXE or ntfsresize/gparted)
  • Backup all partitions (with ntfsclone)
  • Fix bad BCD (Windows Boot Manager Error) — Boot once with Windows Recovery CD, and let auto-repair run
  • Fix bad BCD (BSOD 0x0000007B) — Boot again with Windows Recovery CD, go to command prompt:
bcdedit /export C:\BCD_Backup
ren c:\boot/BCD bcd2.old
bootrec /rebuildbcd
  • Fix missing drivers (BSOD 0x0000007B) — Still within Windows Recovery CD, import the following in regedit.
    • If needed, create this file using a Linux Live CD.
    • In regedit, mount SYSTEM hive as remote_SYSTEM, and merge file above into it.
    • Then don't forget to unmount the hive.
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\remote_SYSTEM\ControlSet001\Control\Class\{4D36E96A-E325-11CE-BFC1-08002BE10318}\0000]
"InfPath"="mshdc.inf"
"InfSection"="msahci_Inst"
"ProviderName"="Microsoft"
"DriverDateData"=hex:00,80,8c,a3,c5,94,c6,01
"DriverDate"="6-21-2006"
"DriverVersion"="6.1.7600.16385"
"MatchingDeviceId"="pci\\cc_010601"
"DriverDesc"="Standard AHCI 1.0 Serial ATA Controller"
"Migrated"=dword:00000001

[HKEY_LOCAL_MACHINE\remote_SYSTEM\ControlSet001\Control\PnP]
"DisableCDDB"=-

[HKEY_LOCAL_MACHINE\remote_SYSTEM\ControlSet001\services\atapi]
"Start"=dword:00000000

[HKEY_LOCAL_MACHINE\remote_SYSTEM\ControlSet001\services\msahci]
"Start"=dword:00000000
  • Boot virtual box, and wait for all devices to be detected.
  • DO NOT reboot when prompted, but instead start regedit again:
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\nxl_SYSTEM\ControlSet001\Control\PnP]
"DisableCDDB"=-

Detailed troubleshooting of startup sequence:

Other general tips:

  • Run MergeIDE
  • Delete AGP440.SYS, intelppm.sys

In case of BSOD x0000007B:

  • Select another virtual disk controller, i.e. either IDE PIIX3 or PIIX4 [7]

Windows VMWare P2V Checklist

Windows 10 P2V

  • Use VMWare Converter.

Linux guest installation

Install VirtualBox Additions

First insert the CDROM using the Virtualbox Devices menu (Insert Guest Additioons CD image...). Then:

sudo mount /dev/cdrom /media/cdrom
sudo ./VBoxLinuxAdditions.run

Linux P2V on Windows Host

  • Create a raw disk file from secondary sata drive (from [8])
  • Start Windows Disk Management, and identify index of secondary drive (say Disk 1)
  • Start cmd.exe as Administrator, and type
"c:\Program Files\Oracle\VirtualBox\VBoxManage.exe" internalcommands createrawvmdk -filename "filename.vmdk" -rawdisk "\\.\\PhysicalDrive1"
  • Start VirtualBox as Administrator, and add the raw disk to the virtual machine.

How-To

Clone a .VDI disk

Reference [9]

  • The supported method is either via the GUI, or using the command-line:
VBoxManage clonehd original.vdi clone.vdi
  • A much faster method however (but unsupported) is to copy the .VDI file as a regular file, and then change the UUID attached to the disk:
cp original.vdi copy.vdi                          # Both VDI will have identical UUID, which VirtualBox forbids
VBoxManage internalcommands setvdiuuid copy.vdi   # ... this sets a new random UUID to the new .VDI

READ THIS! — if you want to mount both disks in Windows:

  • Windows will see both drives as being identical and will only mount one of them (even if both have different UUID).
    The second drive will be offline.
  • To bring the drive back online, open the Disk Management control panel
  • Click right on the drive, and select Online.
    Doing so, Windows will generate a new ID and write it in the volume boot record.

Compact a dynamic .VDI disk

There are two options:

Use CloneVDI
  • CloneVDI is a very fast and easy-to-use tool to clone & compact a VDI disk. It is windows-only, but supports disk with Linux filesystem.
  • After execution, we still have a difference between df -h and vdi size. Maybe CloneVDI did not clear the swap partition, so let's do that ourselves:
 sudo swapoff -a
 sudo sfdisk -l /dev/sda2
 sudo dd if=/dev/zero of=/dev/sda2p1 bs=20M
 halt
Then in host:
vboxmanage mydisk.vdi --compact
... well, turns out we don't gain anything, so maybe CloneVDI did a good job already?
Use zerofree then compact
  • zerofree is a linux utility that writes back to zero all non-zero free block on filesystem. Then we use vboxmanage to compact the image [10], [11].

Grow an existing .VDI disk

The principle is to create a bigger .vdi disk and copy the old into the new one [12]:

  • Create the new .vdi disk.
  • Release the old .vdi from any virtual machine.
  • Clone the old into the new .vdi file:
VBoxManage clonehd --existing OLD.vdi NEW.vdi
  • Now grow the partitions in the new disk using a tool like gparted.

Detach / attach USB and network

When using NAT network and Bluetooth USB on a Windows guest, it is recommended to detach / attach these devices when the network environment changed on the host, or after suspend/resume. The usual method is to go in the GUI. However we can also do via command-line using vboxmanage.

First we need the VM name and USB device UUID:

vboxmanage list runningvms                  # List our running vms - write down VM name
vboxmanage list usbhost                     # List available uid device - write down UID of Bluetooth devices

In our case, VM name is WINGUEST and USB UUID is 0cd50991-b2ad-4a2b-86cb-da08772bd5e2. So now, we can write out script to automatically reset the connections when necessary:

#! /bin/bash

vboxmanage controlvm WINGUEST setlinkstate1 off                                 # Detach network cable (on 1st interface)
vboxmanage controlvm WINGUEST usbdetach 0cd50991-b2ad-4a2b-86cb-da08772bd5e2    # Detach USB bluetooth

sleep 3                                                                         # Wait 3 sec

vboxmanage controlvm WINGUEST setlinkstate1 on                                  # Attach back network cable
vboxmanage controlvm WINGUEST usbattach 0cd50991-b2ad-4a2b-86cb-da08772bd5e2    # Attach back USB bluetooth

Note that we use controlvm and not modifyvm, which as the documentation explains, is only for VMs that are not running. Using the latter gives an error about locked session:

VBoxManage: error: The machine 'WINGUEST' is already locked for a session (or being unlocked)

If the VM is running as another user (say vbox), and we want to extract the UUID automatically, we can adapt the script as follows :

#! /bin/bash

# Detach / attach network cable / bluetooth on WINGUEST vm
# (to reset guest after suspend/resume or when network env. changed on the host)

# Extract Bluetooth USB UUID
BTUUID=$(sudo -Hu vbox /usr/bin/vboxmanage list usbhost|egrep -i "uuid|manufacturer"|sed -rn 'N; s/\n/ /; /Broadcom Corp/!d; s/.* ([0-9a-f-]+) .*/\1/; p')

echo "Detaching network and bluetooth..."
sudo -Hu vbox /usr/bin/vboxmanage controlvm WINGUEST setlinkstate1 off    # Detach network cable (on 1st interface)
sudo -Hu vbox /usr/bin/vboxmanage controlvm WINGUEST usbdetach $BTUUID    # Detach USB bluetooth

echo "Waiting..."
sleep 3                                                                         # Wait 3 sec

echo "Attaching network and bluetooth..."
sudo -Hu vbox /usr/bin/vboxmanage controlvm WINGUEST setlinkstate1 on     # Attach back network cable
sudo -Hu vbox /usr/bin/vboxmanage controlvm WINGUEST usbattach $BTUUID    # Attach back USB bluetooth

And add the following to /etc/sudoers, which only gives permission to user john:

john    ALL = (vbox) NOPASSWD: /usr/bin/vboxmanage list usbhost
john    ALL = (vbox) NOPASSWD: /usr/bin/vboxmanage controlvm WINGUEST usbattach *
john    ALL = (vbox) NOPASSWD: /usr/bin/vboxmanage controlvm WINGUEST usbdetach *
john    ALL = (vbox) NOPASSWD: /usr/bin/vboxmanage controlvm WINGUEST setlinkstate1 *

Clone and diversify a linux guest

  • Change network adapter MAC address.
  • Change clone hostname
sudo vi /etc/hostname
sudo vi /etc/hosts
usually changing the hostname has further impact. Check for all files (in particular in /etc whose name contains the old hostname, and / or contains the old hostname.
  • Refresh SSL server key and certificate (see Apache)
  • Refresh SSH server key (see SSH)

Clone and compact a Windows 10 disk (UEFI)

The source disk is a 500GB VMDK virtual disk containing the following partitions:

  • A EFI partition (FAT32), containing the UEFI boot files.
  • A Windows system partition (NTFS), containing the Windows 10 installation.
  • A Recovery partition (NTFS).
  • An Ubuntu partition (Ext4).

We want to clone it on a new 100GB VDI disk, keeping only the first two partitions. Most work can be done using gparted on a Live Ubuntu DVD, but some care are required to make sure that Windows will boot correctly (avoid the dreadful 0xc000000e blue screen error).

First boot with a Ubuntu Live DVD and launch gparted. We assume that the source disk is /dev/sda and the target disk is /dev/sdb.

  • Select /dev/sdb, and create a new gpt partition table (Device menu).
  • Then use the copy-paste feature of gparted to copy partitions /dev/sda1 and /dev/sda2 to the target disk. If necessary, resize the Windows system partition on the target disk to the available space.
  • Finally, accept the changes and wait for the copy to complete.
  • When done, we must edit the flags of the first partition, and select boot + esp, so that the partition will be seen as an EFI partition.

Next, we need to remove Grub from the target disk:

sudo mount /dev/sda1 /mnt
cd /mnt/EFI
sudo rm -rf ubuntu
sudo cp Windows/Boot/bootmgfw.efi Boot/bootx64.efi
cd
sudo umount /mnt

Note we use bootmgfw.efi since we are on a UEFI system. The file bootmgr.efi is for BIOS firmware [13].

Finally, we need to fix the UUID on the target disk or Windows will not find the system disk.

  • In a terminal, get the disk UUID and the partition UUID of both partitions:
sudo sgdisk -p /dev/sda        # Disk UUID xxxxxxxx-...
sudo sgdisk -u 1 /dev/sda      # Partition unique GUID aaaaaaaa-...
sudo sgdisk -u 2 /dev/sda      # Partition unique GUID bbbbbbbb-...
  • Apply the disk UUID on the target disk:
sudo sgdisk -U xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx /dev/sdb
  • Apply the Partition unique GUID on the target disk:
sudo sgdisk -u 1:aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa /dev/sdb
sudo sgdisk -u 2:bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb /dev/sdb
  • Apply the partition labels on the target disk:
sudo sgdisk -c 1:"Efi System" /dev/sdb
sudo sgdisk -c 2:"Basic Data" /dev/sdb

Now, we can boot the new cloned disk and Windows won't complain on the way.

Benchmarks

Settings:

  • Dell Latitude E6500, 4GB RAM.
  • VirtualBox 3.1.6.
  • System: 1536 MB RAM, IO APIC, 2 CPU with PAE/NX, VT-x + Nested Paging.
  • Video: 32 MB Video + 2D accel
  • Storage: PIIX4 ctrl, raw vmdk image /dev/sda
  • Audio: PulseAudio + ICH AC97
  • Network: Bridged Adapter

task U9.04-32 U9.04-32 U9.04-32 U9.10-32 U9.10-32 U9.10-32 U9.10-64 U9.10-64 U9.10-64 U9.04-32 U9.04-32 Native Native Native
grub →hw profile 2.86 s 2.82 s 2.77 s 2.76 s 2.68 s 2.84 s 2.50 s 2.51 s 2.61 s 3.22 s 2.73 s 1.62 s 1.57 s 1.41 s
... →login screen 23.49 s 23.46 s 21.56 s 42.07 s 43.48 s 40.28 s 48.63 s 52.00 s 56.07 s 33.15 s 24.06 s 29.52 s 24.89 s 25.16 s
... →pwd typed 17.69 s 12.94 s 12.62 s 9.83 s 9.69 s 8.56 s 15.41 s 9.85 s 9.31 s 9.68 s 8.75 s 9.88 s 8.42 s 8.99 s
... →communicator 8.83 s 7.73 s 8.45 s 32.51 s 29.97 s 36.57 s 38.50 s 33.53 s 34.44 s 20.48 s 8.24 s 9.47 s 7.20 s 6.81 s

U9.04-32: Base Ubuntu 9.04 Jaunty i386 Live running on USB, 2.6.28-11-generic.
U9.10-32: Base Ubuntu 9.10 Karmic i386 Live running on USB
U9.10-64: Base Ubuntu 9.10 Karmic AMD64 running from HDD, 2.6.31-20-generic
Native: Running Windows directly, no emulation

Conclusions:

  • Ubuntu 9.04 + VirtualBox 3.1.6FAST !!!
  • Ubuntu 9.10 + VirtualBox 3.1.6SLOW !!!

External references:

Interesting post about step-by-step configuration to run an existing Windows XP installation.
  • Two posts about disabling IO APIC:[14] and [15]. But apparently this doesn't really fix anything (also the poster has a old cpu, etc).

Tuning

Improve the performance of your VirtualBox:

Troubleshooting

USB

lsusb                           # with or without -v
usb-devices
sudo VBoxManage list usbhost    # sudo if vbox run as root
ls -Rl /dev/vboxusb

I experienced some strange problems with dev. tools on a windows guest: - When running on a windows host, the connectivity with the device was flawless - When running on a linux host, the connection only worked once, then failed.

The fix was to edit the BIOS settings:

  • Disable USB 3.0 support
  • Disable USB legacy support

CIFS - Connection lost on linux guest

Guest mount via

# /etc/fstab
//10.0.2.2/c$           /mnt/c        cifs username=beq....,uid=1000,gid=1000,iocharset=utf8,sec=ntlm,noauto   0   0

Tell windows to disable autodisconnect (see [16]):

  • via regedit:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters => ffffffff  (default 0...0f)  (unit = minutes)
  • or via command-line:
net config server /autodisconnect:-1

/dev/vboxdrv missing

Error message:

WARNING: The character device /dev/vboxdrv does not exist.

Solution [17]:

sudo dpkg-reconfigure virtualbox-dkms
sudo dpkg-reconfigure virtualbox
sudo modprobe vboxdrv
sudo modprobe vboxnetflt

Graphic issues and artifacts with 3D acceleration and Ubuntu guests

Links

As a fix, make sure that glxinfo reports the following lines

OpenGL vendor string: Humper
OpenGL renderer string: Chromium

Those lines are normal:

libGL error: core dri or dri2 extension not found
libGL error: failed to load driver: vboxvideo

Many commenters reports the artifact issue, also in Vim [18], [19], [20].

  • To fix the artifacts issue, disable Framebuffer Object in Compiz Settings Manager → OpenGL [21]

Guest network lost after host suspend

Possible workaround on 4.3.8 and later (see bug 11755 and 12441):

VBoxManage modifyvm "VM name" --natdnshostresolver1 on

Mouse click issues

Example of issues:

  • Left clicks behave as right clicks.
  • Left clicks react as if selected from top left corner to clicked point.

Workaround:

  • Press once on R-Ctrl.
  • In multi-screen setup, undock then redock while VM is active. After redock, must Ctrl-F twice to fix fullscreen mode.

See also Common Issues.

Bugtracker