Difference between revisions of "VirtualBox"

From ArchWiki
Jump to: navigation, search
(Hosts running a custom kernel)
m (Article rewrite: post warning flag.)
Line 1: Line 1:
 +
{{Stub|The VirtualBox and derivative articles are currently being rewritten. Please post to the discussion page before editing in order to avoid overriding the changes I made offline and have not published yet. Only the first section "Installation steps for the host" is considered final for now. The rewrite is taking much more time than needed as all assumptions made in the documentation are being verified before publishing.-- [[User:wget|wget]] ([[User talk:wget|talk]]) 16:19, 12 November 2013 (UTC)}}
 
[[Category:Emulators]]
 
[[Category:Emulators]]
 
[[Category:Virtualization]]
 
[[Category:Virtualization]]

Revision as of 16:19, 12 November 2013

Tango-document-new.pngThis article is a stub.Tango-document-new.png

Notes: The VirtualBox and derivative articles are currently being rewritten. Please post to the discussion page before editing in order to avoid overriding the changes I made offline and have not published yet. Only the first section "Installation steps for the host" is considered final for now. The rewrite is taking much more time than needed as all assumptions made in the documentation are being verified before publishing.-- wget (talk) 16:19, 12 November 2013 (UTC) (Discuss in Talk:VirtualBox#)
Template:Article summary start

Template:Article summary text Template:Article summary heading Template:Article summary link Template:Article summary heading Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary end

VirtualBox is a hypervisor used to run operating systems in a special environment, called virtual machine, on top of the existing operating system. It is in constant development and new features are implemented continuously. It comes with a Qt GUI interface, as well as headless and SDL command-line tools for managing and running virtual machines.

In order to integrate functions of the host system to the guests, including shared folders and clipboard, video acceleration and a seamless window integration mode, guest additions are provided for some guest operating systems.

Template:Wikipedia

Contents

Installation steps for the host

In order to launch VirtualBox virtual machines on your Arch Linux box, follow these installation steps.

Basic packages

First, from the official repositories, install the virtualbox package which contains the GPL-licensed VirtualBox suite. This package comes with the SDL and headless command-line tools included.

This package comes with the qt4 package as an optional dependency, install it as well in order to use the graphical interface which is based on Qt. This is not required if you intend to use VirtualBox in command-line only. See below to learn the differences.

VirtualBox kernel modules

Next, in order for VirtualBox to virtualize your guest installation, you will need to add kernel modules to your host kernel. The needed package you need to install depends if you are using a kernel from the official repositories or a custom one (like those available in AUR).

Hosts running an official kernel

Note: Each time an official Arch Linux kernel package gets upgraded, the VirtualBox kernel modules packages get updated accordingly. In order to use the newly updated modules, you need to reload them manually.

Hosts running a custom kernel

The virtualbox-host-modules package is reported to work fine with some custom kernels such as Linux-ckAUR. However, if you attempt to install the virtualbox-host-modules package, the official Arch Linux kernel (linux) will be installed with it as well, as it comes as a dependency. If you want to prevent your package manager from installing these unneeded packages, please install the virtualbox-host-dkms package instead. The latter comes bundled with the source of the VirtualBox kernel modules that will be compiled to generate these modules for your kernel.

Note: As the virtualbox-host-dkms package requires compilation, make sure you have the kernel headers corresponding to your custom kernel version to prevent this error from happening Your kernel headers for kernel your custom kernel version cannot be found at /usr/lib/modules/your custom kernel version/build or /usr/lib/modules/your custom kernel version/source.

Once virtualbox-host-dkms is installed, simply generate the kernel modules for your custom kernel by running the following command structure:

# dkms install vboxhost/virtualbox-host-source version -k your custom kernel version/your architecture
Tip: Use this all-in-one command instead, if you do not want to adapt the above command:
# dkms install vboxhost/$(pacman -Q virtualbox|awk {'print $2'}|sed 's/\-.\+//') -k $(uname -rm|sed 's/\ /\//')

To automatically recompile/reload the VirtualBox kernel modules when your custom kernel gets upgraded and avoid to use the above dkms install command manually, enable the dkms service with:

# systemctl enable dkms
Note: If you do not have the dkms service enabled while the kernel is being updated, the VirtualBox modules will not be updated and will not work with your new kernel version any more. You will have to type in the dkms install command described above manually.

If you want to keep that dkms deamon disabled, there is another way to perform the VirtualBox module recompilation automatically, but it requires reboot. You can use for that an initramfs hook that will automatically call the dkms install command described above at boot time.

To enable that feature, install the vboxhost-hookAUR package from the AUR and add vboxhost to your HOOKS array in /etc/mkinitcpio.conf. Again, make sure the right linux headers are available for the new kernel otherwize the compilation will fail.

Tip: Like the dkms command, the vboxhost hook will tell you if anything goes wrong during the recompilation of the VirtualBox modules.

Load the VirtualBox kernel modules

Among the kernel modules VirtualBox uses, there is a mandatory module named vboxdrv, which must be loaded before any virtual machines can run. It can be automatically loaded when Arch Linux starts up, or it can be loaded manually when necessary.

To load the module manually:

# modprobe vboxdrv

To load the VirtualBox module at boot time, refer to Kernel_modules#Loading and use the file name virtualbox.conf

Note: In order to avoid no such file or directory errors when loading vboxdrv, you may need to update the kernel modules database with depmod -a.

To ensure full functionality of bridged networking, ensure that the vboxnetadp, vboxnetflt and vboxpci kernel modules are loaded too and that the net-tools package is installed.

Add usernames to the vboxusers group

To use the USB ports of your host machine in your virtual machines, add to the vboxusers group the usernames that will be authorized to use this feature. The new group does not automatically apply to existing sessions; the user has to log out and log in again, or start a new environment with a command like newgrp or with sudo -u $USER -s. To add the current user to the vboxusers group, type:

# gpasswd -a $USER vboxusers

Guest additions disc

It is also recommended to install the virtualbox-guest-iso package on the host running VirtualBox. This package will act as a disc image that can be used to install the guest additions onto guest systems. See the section below to learn how to use these additions.

Use the right front-end

Now you are ready to use VirtualBox. Congratulations!

Multiple front-ends are available to you which two are available by default:

  • If you want to use VirtualBox in command-line only (only launch and configure virtual machines), you can use the VBoxSDL command. VBoxSDL does only provide a simple window that contains only the pure virtual machine, without menus or other controls.
  • If you want to use VirtualBox in command-line without any GUI running (e.g. on a server) to create, launch and configure virtual machines, use the VBoxHeadless which produces no visible output on the host at all, but instead only delivers VRDP data.

If you installed the qt4 optional dependency, you also have a nice looking GUI interface with menus and usable with the mouse.

Finally, you can use PhpVirtualBox to administrate you virtual machines via a web interface.

Refer to the VirtualBox manual to learn how to create virtual machines.

Arch Linux as a guest in a Virtual Machine

Installing Arch Linux under VirtualBox is straightforward, and additions should be installed through pacman (following the instructions below), and not through the "Install Guest Additions" menu item in VirtualBox on your host machine, nor from a mounted ISO image.

Install the Guest Additions

Install the virtualbox-guest-utils package. Manually load the modules with:

# modprobe -a vboxguest vboxsf vboxvideo

Create a *.conf file (e.g. virtualbox.conf) in /etc/modules-load.d/ with these lines:

/etc/modules-load.d/virtualbox.conf
vboxguest
vboxsf
vboxvideo

Add the following line to the top of ~/.xinitrc above any exec options. (create a new file if it does not exist):

~/.xinitrc
/usr/bin/VBoxClient-all
Note: If you are creating a new ~/.xinitrc file you must also include a window manager or desktop environment.

Automatic re-compilation of the VirtualBox guest modules with every update of any kernel

This is possible thanks to vboxguest-hookAUR from the AUR. In vboxguest-hook, the 'automatic re-compilation' functionality is done by a vboxguest hook on mkinitcpio after forcing to update the linux-headers package. You will need to add vboxguest to the HOOKS array in /etc/mkinitcpio.conf. You may need to manually recreate the initramfs after an upgrade of the linux-headers package.

The hook will call the dkms command to update the VirtualBox guest modules for the version of your new kernel.

Note: If you are using this functionality, it is important to look at the installation process of the linux (or any other kernel) package. vboxguest hook will tell you if anything goes wrong.

Start the sharing services

After installing virtualbox-guest-utils above, you should start VBoxClient-all to start services for sharing the clipboard, resizing the screen, etc.

  • If you are running something that launches /etc/xdg/autostart/vboxclient.desktop, such as GNOME or KDE, then nothing needs to be done.
  • If you use .xinitrc to launch things instead, you must add the following to your .xinitrc before launching your WM.
# VBoxClient-all &

Using USB webcam / microphone

Note: You will need to have VirtualBox extension pack installed before following the steps below. See VirtualBox_Extras#Extension_pack for details.
  1. Make sure the virtual machine is not running and your webcam / microphone is not being used.
  2. Bring up the main VirtualBox window and go to settings for Arch machine. Go to USB section.
  3. Make sure "Enable USB Controller" is selected. Also make sure that "Enable USB 2.0 (EHCI) Controller" is selected too.
  4. Click the "Add filter from device" button (the cable with the '+' icon).
  5. Select your USB webcam/microphone device from the list.
  6. Now click OK and start your VM.

Using Arch under Virtualbox EFI mode

My experience with this configuration was pretty terrible, but it does work.

UPD. Using efibootmgr has the same effect as using VirtualBox boot menu (see the note below): settings disappear after VM shutdown. First, efibootmgr does *not* work. It will appear to work, but all changes it makes appear to be overwritten on reboot. After performing a standard UEFI/GPT installation, reboot and you should get dumped to the EFI shell. Type exit and you will get a menu. Select the Boot Management Manager, Boot Options, Add Boot Option. Use the file browser to find the grub efi file and select it. Add a label if you want. Afterwards, select Change Boot Order from the menu, use arrow keys to select your Arch option, and + to move it up to the top. GRUB should boot by default now.

Other options are: 1) move your loader to \EFI\boot\bootx64.efi, 2) create \startup.nsh script, which executes desirable loader, like this:

\startup.nsh
HD16a0a1:\EFI\refind\refindx64.efi

Here I'm using consistent mapping name (HD16a0a1). It is probably a good idea, because they do survive configuration changes.

Note: Another useful way to get back to the EFI menu after autobooting is working is to press the c key inside GRUB and type exit. Obviously, this will only work with grub-efi, not grub-bios.

Regenerating the grub.cfg file may also be required to fix broken UUIDs. Check with the lsblk -f command that they match.

Yet another useful way to get to VirtualBox boot menu is pressing F12 right after starting virtual machine. It comes in handy when using rEFInd + EFISTUB, for example.

Synchronize guest date with host

To keep the date and time synchronized, make sure you have virtualbox-guest-utils installed in your host (see above). To enable the service for subsequent boots, run

# systemctl enable vboxservice

To start immediately, run

# systemctl start vboxservice

You also need run this daemon in order to use the auto-mounting feature of shared folders that are mentioned above.

Enable shared folders

Shared folders are managed via the VirtualBox program on the host. They may be added, auto-mounted and made read-only from there.

If automounting is enabled, and the vboxservice is enabled, creating a shared folder from the VirtualBox program on the host will mount that folder in /media/sf_SHAREDFOLDERNAME on the guest. To have that folder created on the Arch Guest, after the Guest Additions have been installed, you need to add your username to the vboxsf group.

# groupadd vboxsf
# gpasswd -a $USER vboxsf
Note: For automounting to work, you have to enable the vboxservice service.

If you want a shared folder (e.g /media/sf_Dropbox) to be symlinked to another folder in your home directory for easy access, you can type on the guest:

$ ln -s /media/sf_Dropbox/* ~/dropbox

The VBoxLinuxAdditions.run script provided in the Guest Additions iso does this for you, however, Arch does not recommend using it.

If shared folders are not auto-mounted, try manually mount.

To prevent startup problems when you're using systemd, you should add comment=systemd.automount to your /etc/fstab. This way, they are mounted only when you access those mount points and not during startup. Otherwise your system might become unusable after a kernel upgrade (if you install your guest additions manually).

desktop   /media/desktop    vboxsf  uid=user,gid=group,rw,dmode=700,fmode=600,comment=systemd.automount 0 0

Don't waste your time to test the nofail option. mount.vboxsf is not able to handle this (2012-08-20).

desktop   /media/desktop    vboxsf  uid=user,gid=group,rw,dmode=700,fmode=600,nofail 0 0

Export VirtualBox virtual machines to other hypervisors

If you plan to use your virtual machine, created with VirtualBox, on another computer which has not necessarily VirtualBox installed, you might be interested in following the next steps.

Remove additions

If you have installed the VirtualBox additions to your VirtualBox virtual machine, please uninstall them first. Your guest, especially if it is using an OS from the Windows family, might behave weirdly, crash or even might not boot at all if you are still using the specific VirtualBox drivers in another hypervisor.

Tip: If you intend to use a virtualization solution from Parallels Inc for your Mac, the product Parallels Transporter can be used to create a virtual machine from a Windows or GNU/Linux virtual machine (or even from a native installation). With such a product, you do not need to apply follow the next step and can stop reading here.

Use the right virtual disk format

Supported formats by VirtualBox

VirtualBox comes with its own container for the virtual hard drives: the Virtual Disk Image (VDI) file format. Even if this format is used by default when you create a virtual machine with VirtualBox, you can specify another one. Indeed VirtualBox does flawlessly support other formats:

  • VMDK: this format has been initially developed by VMware for their products, but it is now an open format. If you intend to use any VMware product, you will need to use this format since it is the only one supported by VMware.
  • VHD: this is the format used by Microsoft in Windows Virtual PC and Hyper-V. If you intend to use any of these Microsoft products, you will have to choose this format.
Tip: Since Windows 7, this format can be mounted directly without any additional application.
  • Version 2 of the HDD format used by Parallels (Desktop for Mac).
  • QED and QCOW used by QEMU.

The format you will need to choose depends on the hypervisor that will be used.

Specific virtual disk format differences

Before converting your virtual drive, please keep in mind these specific virtual disk format differences:

  • The VMDK does offer the ability to be split into several files of up to 2GB. This feature is specially useful if you want to store the virtual machine on machines which do not support very large files. Other formats do not provide such an equivalent feature.
  • Changing the logical capacity of an existing virtual drive with VirtualBox VBoxManage command is only supported for VDI and VHD formats used in dynamic allocation mode to expand (not shrink) their capacity.

Convert your virtual disk format

VirtualBox only supports the virtual disk convertion between VDI, VMDK and VHD formats. Here is an example of convertion from a VDI to VMDK vitual drive.

$ VBoxManage clonehd ArchLinux_VM.vdi ArchLinux_VM.vmdk --format VMDK

If you want to replace the virtual disk you defined during the virtual machine creation process by the one you have just converted, use VBoxManage storagectl, or use the GUI, or modify the .vbox configuration file.

Create the VM configuration for your hypervisor

If your hypervisor (like VMware) does not support import of VirtualBox configuration files (.vbox), you will have to create a new virtual machine and specify its hardware configuration as close as possible as your initial VirtualBox virtual machine.

Note: Pay a close attention to the installation mode (BIOS or UEFI) used to install the guest operating system. While an option is available on VirtualBox to choose between these 2 modes, on VMware, you will have to add the following line to your .vmx file.
ArchLinux_vm.vmx
firmware = "efi"

Finally, ask your hypervisor to use the existing virtual disk you have converted and launch the virtual machine.

Tip: If you are using VMware products and do not want to run through the whole GUI to find the right location to add your new virtual drive device, you can replace the location of the current .vmdk file by editing your .vmx configuration file manually.

Advanced configuration

Replace the virtual disk manually from the .vbox file

If you think that editing a simple XML file is more convenient than playing with the GUI or with VBoxManage and you want to replace (or add) a virtual disk to your virtual machine, simply replace in the .vbox configuration file corresponding to your virtual machine the GUID, the file location and the format to your needs:

ArchLinux_vm.vbox
<HardDisk uuid="{670157e5-8bd4-4f7b-8b96-9ee412a712b5}" location="ArchLinux_vm.vdi" format="VDI" type="Normal"/>

then in the <AttachedDevice> sub-tag of <StorageController>, replace the GUID by the new one.

ArchLinux_vm.vbox
<AttachedDevice type="HardDisk" port="0" device="0">
  <Image uuid="{670157e5-8bd4-4f7b-8b96-9ee412a712b5}"/>
</AttachedDevice>
Note: If you do not know the GUID of the drive you want to add, but you have just used VBoxManage for the convertion, this command will output the GUID just after the convertion. Using a random GUID does not work, as each UUID is stored inside each disk images.

Starting virtual machines with a service

Find hereinafter the implementation details of a systemd service that will be used to consider a virtual machine as a service.

/etc/systemd/system/vboxvmservice@.service
[Unit]
Description=VBox Virtual Machine %i Service
Requires=systemd-modules-load.service
After=systemd-modules-load.service

[Service]
User=<user>
Group=vboxusers
ExecStart=/usr/bin/VBoxHeadless -s %i
ExecStop=/usr/bin/VBoxManage controlvm %i savestate

[Install]
WantedBy=multi-user.target
Note: Replace <user> with a user that is a member of the vboxusers group. Make sure the user chosen is the same user that will create/import virtual machines, otherwise the user will not see the VM appliances.

To enable the service that will launch the virtual machine at next boot, use:

# systemctl enable vboxvmservice@your virtual machine name

To start the service that will launch directly the virtual machine, use:

# systemctl start vboxvmservice@your virtual machine name

VirtualBox 4.2 introduces a new way for UNIX-like systems to have virtual machines started automatically, other than using a systemd service.

Troubleshooting

VBOX_E_INVALID_OBJECT_STATE (0x80BB0007)

This can occur if a VM is exited ungracefully. The solution to unlock the VM is trivial:

$ VBoxManage controlvm <your virtual machine name> poweroff

USB subsystem is not working on the host or guest

Sometimes, on old Linux hosts, the USB subsystem is not auto-detected resulting in an error Could not load the Host USB Proxy service: VERR_NOT_FOUND or in a not visible USB drive on the host, even when the user is in the vboxusers group. This problem is due to the fact that VirtualBox switched from usbfs to sysfs in version 3.0.8. If the host doesn't understand this change, you can revert to the old behaviour by defining the following environment variable in any file that is sourced by your shell (e.g. your ~/.bashrc if you're using bash):

~/.bashrc
VBOX_USB=usbfs

Then make sure, the environment has been made aware of this change (reconnect, source the file manually, launch a new shell instance or reboot).

Also make sure that your user is a member of the storage group.

Failed to create the host-only network interface

To be able to create a Host-Only Network Adapter or a Bridged Network Adapter, the kernel modules vboxnetadp and vboxnetflt need to be loaded, you also need to make sure the net-tools package is installed. You can load these kernel modules manually with:

# modprobe -a vboxdrv vboxnetadp vboxnetflt

To load these modules automatically at boot, refer to Kernel_modules#Loading and use a program name of virtualbox.

WinXP: Bit-depth cannot be greater than 16

If you are running at 16-bit color depth, then the icons may appear fuzzy/choppy. However, upon attempting to change the color depth to a higher level, the system may restrict you to a lower resolution or simply not enable you to change the depth at all. To fix this, run regedit in Windows and add the following key to the Windows XP VM's registry:

[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services]
"ColorDepth"=dword:00000004

Then update the color depth in the "desktop properties" window. If nothing happens, force the screen to redraw through some method (i.e. Host+f to redraw/enter full screen).

Mounting .vdi images

Mounting vdi images only works with fixed size (static) images; dynamic size images aren't easily mountable.

First we need one information from your .vdi image:

$ VBoxManage internalcommands dumphdinfo <your .vdi file location> | grep offData
Header: offBlocks=4096 offData=69632

Then, add to your offData 32256. (e.g. 32256 + 69632 = 101888)

Now you can mount your vdi image with the following command:

# mount -t ext4 -o rw,noatime,noexec,loop,offset=101888 <your .vdi file location> /mnt/

Copy and paste not working on Arch Linux guest

After virtualbox-guest-additions has been updated to version 4.2.0-2, the copy and paste feature from the host to Arch Linux guest does not work any more. It seems to be due to VBoxClient-all requiring root access. In previous versions adding VBoxClient-all & to ~/.xinitrc was sufficient to make copy and paste work again. Now, in order to solve the problem, update ~/.xinitrc to match sudo VBoxClient-all & and add the line , NOPASSWD: /usr/bin/VBoxClient-all to your username in /etc/sudoers and restart X. It should all work again. The line in the sudoers file should look similar to this:

# Allow sudo for user 'you' and let him run VBoxClient-all without requiring a password
you ALL = PASSWD: ALL, NOPASSWD: /usr/bin/VBoxClient-all
Note: Use visudo to edit the /etc/sudoers file. This will check for syntax errors when saving.

Use serial port in guest OS

Check you permission for the serial port:

$ /bin/ls -l /dev/ttyS*
crw-rw---- 1 root uucp 4, 64 Feb  3 09:12 /dev/ttyS0
crw-rw---- 1 root uucp 4, 65 Feb  3 09:12 /dev/ttyS1
crw-rw---- 1 root uucp 4, 66 Feb  3 09:12 /dev/ttyS2
crw-rw---- 1 root uucp 4, 67 Feb  3 09:12 /dev/ttyS3

Add your user to the uucp group.

# gpasswd -a $USER uucp 

and log out and log in again.

Abort on resume

There is a known bug that causes abort on resume. The workaround is simple: always use Host+q keyboard keys combination or use the menu to close the VM.

System images in Btrfs

Back in 2010, there were reports that OS disk images would not start if they were attached via a virtual SATA device. It was reportedly fixed, and seemed to be. But as of around March 2013, that particular bug report has been reopened. It can be fixed by enabling the use of the host I/O cache, which is disabled by default with virtual SATA interfaces.

Windows 8.x Error Code 0x000000C4

If you get this error code while booting, even if you choose OS Type Win 8, try to enable the CMPXCHG16B CPU instruction:

$ vboxmanage setextradata <your virtual machine name> VBoxInternal/CPUM/CMPXCHG16B 1

Windows 8 VM fails to boot with error "ERR_DISK_FULL"

Situation: Your Windows 8 VM refuses to start. VirtualBox throws an error stating the virtual disk is full. However, you are certain that the disk is not full. Bring up your VM's settings at Settings > Storage > Controller:SATA and select "Use Host I/O Cache".

External links