VirtualBox (简体中文)

Tango-preferences-desktop-locale.png

Tango-preferences-desktop-locale.png

本页面或部分需要翻译,部分内容可能已经与英文文章脱节。如果您希望贡献翻译,请访问简体中文翻译组

附注: please use the first argument of the template to provide more detailed indications.

翻译状态: 本文是英文页面 VirtualBox翻译,最后翻译时间:2014-10-07,点击这里可以查看翻译后英文页面的改动。

VirtualBox 是类似 VMware 的虚拟 PC 模拟器,处于不断的开发中。使用 Qt 图形界面,提供了无界面运行和 SDL 命令行工具进行运行管理。它包含guest additions为一些虚拟系统提供附加功能,包括文件共享、剪贴板和图形加速,支持 “无缝” 窗口整合模式。

See the Wikipedia article on this subject for more information: VirtualBox

Contents

在主机上安装

安装 GPL 版本的 virtualbox 软件包。作为依赖安装的 virtualbox-host-modules 包含了archlinux 默认内核使用的模块。

如果使用linux-lts内核,需要安装virtualbox-host-modules-lts.

要使用基于 QtVirtualBox 命令,需要安装 qt4 软件包。如果使用简单的基于 SDL 的 VBoxSDL 命令或者 VBoxHeadless 命令,则不需要安装 qt4

定制内核的主机

Virtualbox 不需要标准 ARCH 内核也能正常工作在 Linux-ck 等定制内核上。为了不让 pacman 同时安装 Arch 内核软件包,需要用 virtualbox-host-dkms 安装 virtualbox 内核模块, 原理参见 FS#26721

virtualbox-host-dkms安装完成后,只要用下面命令生成内核模块:

# dkms install vboxhost/<virtualbox-host-source 版本> -k <你的自制内核模块版本>/<你的架构>

懒人做法是键入以下命令:

# dkms install vboxhost/$(pacman -Q virtualbox|awk {'print $2'}|sed 's/\-.\+//') -k $(uname -rm|sed 's/\ /\//')
Note: You are probably missing linux-headers if you are getting an error like Your kernel headers for kernel <your custom kernel's version> cannot be found at/usr/lib/modules/<your custom kernel's version>/build or /usr/lib/modules/<your custom kernel's version>/source

然后装载模块:

# modprobe vboxdrv

若要开机时自动加载/编译 VirtualBox 模块,你可以啟用 dkms.service:

# systemctl enable dkms.service

每次更新内核时, 自动重编 VirtualBox 主系统模块

这个功能得益于AUR中的 virtualbox-hook[broken link: package not found]。在 virtualbox-hook 中,每次更新 linux-headers 包之后,mkinitcpio 上的 vbox hook 会完成 'automatic re-compilation' 。你需要将 'vbox' 加入 /etc/mkinitcpio.conf 中的 HOOKS 数组,同样地,需要加入到 'linux-headers' 里和你自己定制的内核头文件里的 /etc/pacman.conf 中的 SyncFirst 数组中以使其生效。

Warning: SyncFirst 选项不再随附于 pacman 4.1。请改用
# pacman -Sy linux-headers && pacman -Su
以使 linux-headers 优先升级。请参阅这段解释

这个 hook 会调用 dkms 命令去更新 VirtualBox 模块以与新的内核版本相适应。

Note: 如果你使用这种方法,观察linux 或者其他内核的包的安装过程是很重要的。如果有问题出现,vboxhost hook 会显示相应信息。

配置

网络

VirtualBox 客户端可以通过不同的方式连接网络;其中,有#NAT#桥接链接。#NAT是最简单的且作为一个新虚拟机的默认方式。

VirtualBox手册涵盖了主机模式和内网选项。这些都被忽略了,因为在大多数情况下与操作系统无关。

NAT

在VirtualBox中:

  • 访问虚拟机的设置菜单;
  • 点击左边的网络‘’;最后,
  • 在“连接方式”的下拉列表中选择NAT

与VirtualBox捆绑的DHCP服务使得客户端系统能够与DHCP一起配置,第一张卡的NAT IP地址是 10.0.2.0,第二张是10.0.3.0,往后以此类推。

桥接

桥接网络可能被以多种方式启动;其中,有要求以较少控制为代价进行最小启动的原生方式。对于较新版本,VirtualBox可以在没有第三方工具的帮助下,在客户端和无线主机接口间进行桥接。

在继续之前,加载必要模块:

# modprobe vboxnetflt

在VirtualBox中:

  • 访问虚拟机的设置菜单;
  • 点击左边列表中的网络
  • 链接方式的下拉列表中选择Bridged Adapter(桥接适配器);最后,
  • 界面名称下拉列表中,选择客户端操作系统被包含在内,主机用于连接网络的接口。

Start the virtual machine and configure its network as usual; e.g., DHCP or static. 打开虚拟机,像往常一样配置其网络;例如 DHCP 或 static(静态网络)。

客户端增强包

客户端增强包(The Guest Additions)能够激活共享文件夹功能,改善显卡加速支持和在主机端及客户端之间启用双向剪贴板。鼠标集成是另一项功能,用于减少在客户端中使用鼠标后将其释放的需要。

Arch Linux 客户端

参阅Arch Linux VirtualBox客户端

Windows 客户端

在你的虚拟机中安装Windows(XP 等等)后,只需选择设备 → 安装增强功能

这将会挂载ISO镜像,接着Windows应该自动运行客户端增强包安装向导(The guest additions installer)。按照说明进行到底。

主机端和客户端之间的键盘和鼠标

  • 为了捕获键盘和鼠标,点击虚拟机内部。
  • 想要释放,按下右 Ctrl.

想要获得在主机端和客户端之间的无缝鼠标集成功能,在客户端内安装#客户端增强包

主机端和客户端间的共享文件夹

在虚拟机的设置中,找到数据空间标签,然后加入你想要共享的文件夹。

  • 注意:为了使用这个功能你需要安装客户端增强包。
在Linux主机中,设备 → 安装增强功能
确定(被要求下载CD镜像时)
挂载(被要求注册和挂载时)

In a Linux host, create one or more folders for sharing files, then set the shared folders via the virtualbox menu (guest window). 在Linux主机端中,为共享的文件创建一个或更多的文件夹,然后通过Virtualbox菜单中设置(Windows客户端)

在Windows客户端中,从VirtualBox 1.5.0开始,共享文件夹是可浏览的,所以在Windows资源管理器中是可视的。打开Windows资源管理器,在我的网络位置(My Networking Places) → 整个网络(Entire Network) → VirtualBox Shared Folders(VirtualBox共享文件夹)

启动Windows资源管理器(运行资源管理器命令),游览 网络位置(network places) -> (+)号展开: 整个网络(entire network)&rarr; VirtualBox Shared Folders(VirtualBox共享文件夹) &rarr;\\Vboxsvr → 然后你就可以在此展开所有已配置的文件夹了,并且在客户端文件系统中为Linux文件夹创建快捷方式。你也可以使用“添加网络位置向导(Add network place wizard)”找到“VBoxsvr”。

此外,在Windows命令行提示符中,你也可以使用以下命令:

net use x: \\VBOXSVR\sharename

虽然VBOXSVR是一个固定名称,但请用你所想要用于共享的盘符替代x:,用VBoxManage指定的共享名替换sharename。

在Windows客户端中,为了以VirtualBox共享文件夹改善文件的读取与保存(如MS Office),编辑c:\windows\system32\drivers\etc\hosts如下:

127.0.0.1 localhost vboxsvr

在Linux客户端中,实用以下命令:

# mount -t vboxsf [-o OPTIONS] sharename mountpoint
  (注意:共享名是任意的,或者和VirtualBox对话框中选定的一样(在主机端文件系统中共享目录的挂载点)。
自动挂载共享文件夹可以通过Linux客户端 /etc/fstab 文件来实现。你可以指定uid=#,gid=#(# 用实际的数字uid和gid替换),以便以普通用户权限(而不是root权限)来挂载共享文件夹。(这个对于为了在Linux客户端中使用而挂载主机上~/home的一部分是很有用的。为了做到这点,依照以下格式添加一个条目到Linux客户端中的/etc/fstab:
sharename mountpoint vboxsf uid=#,gid=# 0 0

用在VBoxManage中指定的共享名替换sharename,并用你想要共享的路径替换mountpoint(如 /mnt/share)。通常的挂载申请,就是说,如果还没有的话,先创建这个文件夹。注意,如果你已经让VirtualBox“自动挂载”这个共享文件夹,这一步可能就不必了,而且你的文件夹可以在/media 下找到。

除了mount命令提供的标准选项外,以下是可选的:

iocharset=CHARSET

设置用于I/O操作的字符集(默认utf8),并且:

convertcp=CHARSET

用于指定用于共享文件夹名称的字符集(默认utf8)

在Windows客户端中激活D3D加速

Recent versions of Virtualbox have support for accelerating OpenGL inside guests. This can be enabled with a simple checkbox in the machine's settings, right below where video ram is set, and installing the Virtualbox guest additions. However, most Windows games use Direct3D (part of DirectX), not OpenGL, and are thus not helped by this method. However, it is possible to gain accelerated Direct3D in your Windows guests by borrowing the d3d libraries from Wine, which translate d3d calls into OpenGL, which is then accelerated.

After enabling OpenGL acceleration as described above, go to http://www.nongnu.org/wined3d/ in your Windows guest and grab the "Latest version (Installer):". Reboot the guest into safe mode (press F8 before the Windows screen appears but after the Virtualbox screen disappears), and install wined3d, accepting the defaults during the install. (You may check the box for DirectX 10 support if you like, dont touch anything else.) Reboot back to normal mode and you should have accelerated Direct3D.

Note: This hack may or may not work for some games depending on what hardware checks they make and what parts of D3D they use.
Note: This has only been tried on Windows XP and Windows 7 RC guests AFAIK, and does not work on the Windows 7 guest. If you have experience with this on a different windows version, please add that data here.

压缩磁盘映像

Linux 客户端

启动Linux客户端并移除所有多余的东西(多余软件包,临时文件等)。满意之后,用dd或更好的dcfldd来擦除空闲空间:

$ dcfldd if=/dev/zero of=fillfile bs=4M

当填充文件达到了虚拟硬盘的限度,大量的用户空间(非保留块)将会被填满。或者,以Root的身份运行该命令以便填满所有空间。示例信息:"8192 blocks (8192Mb) written.dcfldd:: No space left on device."

这种情况下,只需移除填充文件并关闭虚拟机:

$ rm -f fillfile && sudo shutdown -hF now
注意: 考虑到经过压缩操作, -F 开关将会在重启前强迫磁盘检查。

现在,压缩磁盘:

$ VBoxManage modifyhd /path/to/your.vdi --compact

设置

将需要运行 Virtualbox 的用户名添加到 vboxusers 用户组,文件夹共享和其它功能需要正确的组才能工作。新组设置不会应用到当前会话,请重新登录或者通过命令 newgrpsudo -u username -s 启动一个新环境。

# gpasswd -a username vboxusers

加载内核模块

VirtualBox 在 Linux 上运行需要使用自己的内核模块,包括一个必须的 vboxdrv。这个模块必须在虚拟机运行前启动。如果需要,可以在 Arch Linux 启动时自动加载。

手动装入模块:

# modprobe vboxdrv

若要啟动时加载 VirtualBox 驱动,在 /etc/modules-load.d 创建 *.conf 文件 (例如:virtualbox.conf),包括所有应加载的模块:

/etc/modules-load.d/virtualbox.conf
vboxdrv
Note: You may need to update the kernel modules db in order to avoid 'no such file or directory' error when loading vboxdrv. Run: depmod -a.

啟动 VirtualBox 图形管理员:

$ VirtualBox

為了保证桥接网络具有完整功能,请确认除了加载 vboxnetadpvboxnetflt 以及 vboxpci 内核模块 之外,也安装了 net-tools 软件包。

虚拟系统附加光盘

“virtualbox” 软件包建议在运行 VirtualBox 的主机系统上安装virtualbox-guest-iso。这个包是一个磁盘镜像,用来安装虚拟系统的附加功能。

启动 live 系统光盘

点击'新建' 按钮创建虚拟环境。起个合适的名字然后选择操作系统类型和版本。选择基本内存大小。(大部分操作系统需要至少 512MB)。创建一个新的硬盘镜像(硬盘镜像是一个包含操作系统文件系统和文件的文件)。

创建完硬盘镜像后,点击'设置', 选择 CD/DVD-ROM, 点击'挂载 CD/DVD 磁盘' 然后选择一个 ISO 镜像。

使用服务啟动虚拟机

详阅 Systemd/Services#VirtualBox virtual machines 学习如何设置每台虚拟机的 systemd 服务。

高级设置

高级设置请阅读 VirtualBox Extras.

虚拟机上安装 Arch Linux

在 VirtualBox 中安装 Arch 非常简单直接,而且最好通过 pacman 安装 Guest Addition,不要使用 VirtualBox 中的 "Install Guest Additions" 或挂载的 ISO 安装。

Guest 附加软件包

安装 virtualbox-guest-utils

手动装入内核模块:

# modprobe -a  vboxguest vboxsf vboxvideo

自动装入模块: 在 /etc/modules-load.d/ 中创建文件vbox.conf,加入如下几行:

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

每次更新内核时, 自动重编 VirtualBox 客户机模块

这个功能得益于AUR 中的 vboxguest-hook。在 vboxguest-hook 中,每次更新 linux-headers 包之后,mkinitcpio 上的 vboxguest hook 会完成 'automatic re-compilation'。你需要将 vboxguest 加入 /etc/mkinitcpio.conf 中的 HOOKS 数组。你也需要在升级 linux-headers 包后手动重建 initramfs。

这个Hook会调用dkms 命令来更新新内核版本的VirtualBox寄宿模块。

注意: 在使用这项功能的时候, 请留意 linux (或者任何其它内核) 安装包的安装过程。 vboxguest hook 会提示是否有错误的。

开啟共享服务

在安装 virtualbox-guest-utils 之后,你应该运行 VBoxClient-all 啟动共享剪贴板、调整屏幕大小等服务。

  • 若你运行会啟动 /etc/xdg/autostart/vboxclient.desktop 的服务,例如 GNOME 或 KDE,便不需再进行额外动作。
  • If you use .xinitrc to launch things instead, you must add the following to your .xinitrc before launching your WM.
# VBoxClient-all &

使用 USB 摄像头 / 麦克风

Note: 在遵照下列步骤前,你需要安装 VirtualBox 扩展包。详见 VirtualBox_Extras#Extension_pack
  1. 要确保没有在运行虚拟机,以及没有使用摄像头 / 麦克风。
  2. 进入VirtualBox主界面并打开Arch系统的设置界面,到USB设备页。
  3. 要确保勾选上“启用USB控制器”选项。 还要确保选择“启用USB 2.0(EHCI)控制器”选项。
  4. 点击“从设备列表中添加筛选器”按钮 (就是那个有“+”图标的连接线).
  5. 从列表中选择你的USB摄像头 / 麦克风设备。
  6. 然后再点击OK,启动你的VM

在 VirtualBox EFI 模式下使用 Arch

我对这方面的设置有非常不好的经历,但是依然可以办到。

UPD. Using efibootmgr has the same effect as using VirtualBox boot menu (see the note below): settings disappear after VM shutdown.首先要说的是 efibootmgr *完全无效*。虽然看起来是正常的,但是所有更动似乎在重啟后就失效。在进行正常 UEFI/GPT 安装后重啟,你会被丢到 EFI shell。送出 exit 后会看到菜单,选中 Boot Management Manager -> Boot Options -> Add Boot Option。使用文件浏览器找到 grub efi 文件并选中,添加你要的标签。之后,菜单选中 Change Boot Order,使用方向键选中你的 Arch 选项,按下 + 移到最上方。现在 GRUB 应该默认啟动了。

其它选项: 1) 将引导程序移到 \EFI\boot\bootx64.efi, 2) 创建 \startup.nsh 脚本,运行选定的引导程序,就像这样:

\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.

Arch Linux 客户机共享文件夹

共享文件夹是主机上的 VirtualBox 程序控制。可以添加、自动挂载并设置成只读。主机系统创建的共享文件夹位于 /media/sf_SHAREDFOLDERNAME。要使用共享文件,需要在安装完 Guest Additions 软件包之后执行:

$sudo groupadd vboxsf
$sudo gpasswd -a $USER vboxsf

和主机系统同步日期

要同步系统间的日期,

Systemd

下次开机开始同步:

# systemctl enable vboxservice.service

立即启动同步:

# systemctl start vboxservice.service

解决问题

modprobe Exec 格式错误

确认你使用的是最新系统:

pacman -Syu

VBOX_E_INVALID_OBJECT_STATE (0x80BB0007)

这种情形可能会在虚拟机没有正常退出时发生,解除锁定虚拟机并不难:

VBoxManage controlvm nArch poweroff

USB 子系统在宿主机和虚拟机没有作用

Sometimes the USB subsystem is not auto-detected resulting in an error (eg: 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. See this topic [1] for details.

若要 USB 子系统运作,只要添加

VBOX_USB=usbfs

~/.bashrc,重啟系统或打开新的 bash 实例即可。

同时也要确定你的用户是 storage 组成员。

主机模式网络接口创建失败

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. It's possible to load these kernel modules manually with

# modprobe -a vboxnetadp vboxnetflt

若要开机自动加载,每个模块添加一行到 /etc/modules-load.d/virtualbox.conf:

vboxdrv
vboxnetadp
vboxnetflt
Note: 以前这些都要添加到 /etc/rc.confMODULES 数组,现此方法已过时。

更多信息请看这个主题。

WinXP: 位深不能大于 16

若你运行于 16 位色深,图标可能看起来糊糊的。但是当你试图调到更高色深,系统可能会受限于较低的分辨率,甚至根本不允许更改色深。若要修正此问题,运行 regedit 并添加下列键值到虚拟 XP 注册表:

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

接著在桌面属性窗口改变色深。若没有反应,可透过一些方法强制屏幕重绘 (按下 Host+F 重绘/进入全屏)。

挂载 .vdi 镜像

This just work with static size vdi images! Dynamic size will not be easy mountable! First we need one information from your .vdi image:

$ VBoxManage internalcommands dumphdinfo Arch_64min.vdi |grep offData
Header: offBlocks=4096 offData=69632

现在,添加到你的 offData 32256。例如:32256 + 69632 = 101888

现在你可以挂载 vdi 镜像:

# mount -t ext4 -o rw,noatime,noexec,loop,offset=101888 Arch_64min.vdi /mnt/

挂载失败导致的啟动问题

若你在内核升级后 systemd 设定遇到问题,你应该透过 init=/bin/bash 开啟系统 (如果应急 shell 对你没有作用)。

root=/dev/mapper/vg_main-lv_root ro vga=792 resume=/dev/mapper/vg_main-lv_swap init=/bin/bash

接著附加写入权限挂载 root-文件系统:

# mount / -o remount,rw

根据 #Arch Linux 客户机共享文件夹 更改 /etc/fstab]],然后在 Bash shell 运行 systemd:

# exec /bin/systemd

复制和粘贴在 Arch Linux 客户机没有作用

Since updating virtualbox-guest-additions to version 4.2.0-2 copy&paste from Host OS to Arch Linux Guest stopped working. It seems to be due to VBoxClient-all requiring root access. In previous versions adding VBoxClient-all & to ~/.xinitrc was sufficient to make copy&paste work. Update ~/.xinitrc to match sudo VBoxClient-all & and add the line , NOPASSWD: /usr/bin/VBoxClient-all to your username in the sudoers file 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: 使用 visudo 编辑 sudoer 文件,这会在存储时检查语法错误。

虚拟系统使用串行端口

确认你的串行端口权限

$ /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

添加你的用户到 uucp 组。

# gpasswd -a $USER uucp

然后重新登陆。

唤醒后异常

有个已知臭虫导致唤醒后异常: https://www.virtualbox.org/ticket/11289。避开的方法很简单: 每次都按 Host+q 或菜单关闭虚拟机。

Btrfs 系统镜像

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 repoened. This can be fixed by enabling the use of the host I/O cache, which is disabled by default with virtual SATA interfaces.

vagrant 啟动问题

在最新版的 VirtualBox(4.2.14-1),运行 vagrant up 命令伴随以下错误:

Command: ["import",
"/Users/username/.vagrant.d/boxes/precise32/virtualbox/box.ovf"]
Stderr: 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Interpreting
/Users/username/.vagrant.d/boxes/precise32/virtualbox/box.ovf...
OK.
0%...
Progress object failure: NS_ERROR_CALL_FAILED

这个修正释出前,你需要使用其它方法解决,或是将 VirtualBox 降级。

有个临时的解决方法是在 ~/.vagrant.d/boxes/BoxName/virtualbox 為每个 box 创建 manifest:

openssl sha1 *.vmdk *.ovf > box.mf

你可以降级 VirtualBox。若你的缓存有旧的软件包,可以透过下列命令降级:

sudo pacman -U /var/cache/pacman/pkg/virtualbox-4.2.12-3-x86_64.pkg.tar.xz

这个错误似乎同时出现在所有平台: http://www.marshut.com/pzisi/progress-object-failure-ns-error-call-failed-when-running-vagrant-up-in-getting-started-guide.html#qhihz

It's unclean for the moment. It could be regression inside Virtualbox or a issue inside Vagrant. When you delete the cache you can downgrade via ArchLinux downgrader (I did not test it correctly, but I assume this works, else check the wiki page for downgrading: Downgrading packages

更多信息请到 GitHub issue 页面查看 Clean install on OS X 10.8.4 w/ latest VirtualBox not working

根据 Vagrant creator on Twitter,这是 VirtualBox 的臭虫。在 2013 年 06 月 25 日,他说他们在 SVN 修正此臭虫,并且正在等待释出。同时,我可以确认这是跨平台的问题,4.2.14 在我的 Win7 上面是坏的。

这个问题在 virtualbox-4.2.16-1 这份 VirtualBox 版本释出时已解决

外部链接

技巧和窍门

获得可探测的Web-cam和其他USB设备

Make sure you filter any devices that are not a keyboard or a mouse so they don't start up at boot and this insures that Windows will detect the device at start-up.

向客户端发送CTRL+ALT+F1

如果你的客户端操作系统是Linux发行版,且你又想打开一个新的tty文本shell或者通过键入Ctrl+Alt+F1来退出 X ,你可以简单地敲击你的'Host Key'向客户端操作系统发送这个命令(通常为键盘右边的Ctrl) + F1F2,以此类推。

在运行于无显示器的服务器上的系统上启动虚拟机

Add this line to /etc/rc.local

exec /bin/su -c 'VBoxManage startvm --type headless <UUID|NAME>' PREFERED_USER >/dev/null 2>&1

Where <UUID|NAME> is the guest identifier, and PREFERRED_USER is the user profile that contains the VM definitions and .vdi files.

Since exec replaces the currently running process, you will not be able to start a second VM, or execute any other commands, after the exec. Try the following if this is a problem:

su -c 'VBoxHeadless -s <UUID|NAME> &' -s /bin/sh PREFERED_USER >/dev/null 2>&1

Using fully qualified path to su and VBoxHeadless is recommend. Add additional lines like above to start additional VMs. Commands following these in rc.local will be executed. Based on some rooting around in the VBox documentation, I get the impression this will be a little more robust than 'VBoxManage ... --type headless' in future VBox releases.

To determine the available VMs for a user:

su -c 'VBoxManage list vms' PREFERED_USER

To save the state of a running VM:

su -c 'VBoxManage controlvm <UUID|NAME> savestate' PREFERED_USER

rc.local.shutdown would be a good spot for this.

从主机端访问虚拟机上的服务器

To access apache on a VM from the Host machine ONLY, simply execute the following lines on the Host:

$ VBoxManage setextradata GuestName "VBoxInternal/Devices/pcnet/0/LUN#0/Config/Apache/HostPort" 8888
$ VBoxManage setextradata GuestName "VBoxInternal/Devices/pcnet/0/LUN#0/Config/Apache/GuestPort" 80
$ VBoxManage setextradata GuestName "VBoxInternal/Devices/pcnet/0/LUN#0/Config/Apache/Protocol" TCP

Where 8888 is the port the host should listen on and 80 is the port the VM will send Apache's signal on. To use a port lower than 1024 on the host machine changes need to be made to the firewall on the host machine. This can also be set up to work with SSH, etc.. by changing "Apache" to whatever service and using different ports.

Note: "pcnet" refers to the network card of the VM. If you use an Intel card in your VM settings change "pcnet" to "e1000"

It might also be necessary to allow connections from the outside to the server in your VM. E.g. if the guest OS is Arch, you may want to add the line

httpd: ALL

to your /etc/hosts.allow file.

守护进程工具

While VirtualBox can mount ISO images without a problem, there are some image formats which cannot reliably be converted to ISO. For instance, ccd2iso ignores .ccd and .sub files, which can give disk images with broken files. cdemu, fuseiso, and MagicISO will do the same. In this case there is no choice but to use Daemon Tools inside VirtualBox.

Recent Daemon Tools versions won't install, so use this old one: [3]

在USB key上使用VirtualBox

When using VirtualBox on a USB key, for example to start an installed machine with an ISO image, you will manually have to create VDMKs from the existing drives. However, once the new VMDKs are saved and you move on to another machine, you may experience problems launching an appropriate machine again. To get rid of this issue, you can use the following script to launch VirtualBox. This script will clean up and unregister old VMDK files and it will create new, proper VMDKs for you:

#!/bin/bash

# Erase old VMDK entries
rm ~/.VirtualBox/*.vmdk

# Clean up VBox-Registry
sed -i '/sd/d' ~/.VirtualBox/VirtualBox.xml

# Remove old harddisks from existing machines
find ~/.VirtualBox/Machines -name \*.xml | while read file; do
  line=`grep -e "type\=\"HardDisk\"" -n $file | cut -d ':' -f 1`
  if [ -n "$line" ]; then
    sed -i ${line}d $file
    sed -i ${line}d $file
    sed -i ${line}d $file
  fi
  sed -i "/rg/d" $file
done

# Delete prev-files created by VirtualBox
find  ~/.VirtualBox/Machines -name \*-prev -exec rm '{}' \;

# Recreate VMDKs
ls -l /dev/disk/by-uuid | cut -d ' ' -f 9,11 | while read ln; do
  if [ -n "$ln" ]; then
    uuid=`echo "$ln" | cut -d ' ' -f 1`
    device=`echo "$ln" | cut -d ' ' -f 2 | cut -d '/' -f 3 | cut -b 1-3`

    # determine whether drive is mounted already
    checkstr1=`mount | grep $uuid`
    checkstr2=`mount | grep $device`
    checkstr3=`ls ~/.VirtualBox/*.vmdk | grep $device`
    if [[ -z "$checkstr1" && -z "$checkstr2" && -z "$checkstr3" ]]; then
      VBoxManage internalcommands createrawvmdk -filename ~/.VirtualBox/$device.vmdk -rawdisk /dev/$device -register
    fi
  fi
done

# Start VirtualBox
VirtualBox

Note that your user has to be added to the "disk" group to create VMDKs out of existing drives.

phpVirtualBox

An open source, AJAX implementation of the VirtualBox user interface written in PHP. As a modern web interface, it allows you to access and control remote VirtualBox instances. Much of its verbage and some of its code is based on the (inactive) vboxweb project. It allows the administrator to remotely, graphically, administer their virtual machines without having to log in to their headless VirtualBox servers.

This requires the PUEL edition for VirtualBox.

An installation guide is available here: http://code.google.com/p/phpvirtualbox/wiki/Installation

Arch Linux users should uncomment these 2 extensions in /etc/php/php.ini

extension=json.so
extension=soap.so

See also PhpVirtualBox

从其他虚拟机中迁移

qemu-img 程序可以用来将映像从一种格式转换到另一种格式,或为一个映像添加压缩或加密。

  # pacman -S qemu

从QEMU映像转换

To convert a QEMU image for use with VirtualBox, first convert it to raw format, then use VirtualBox's conversion utility to convert and compact it in its native format.

  $ qemu-img convert -O raw test.qcow2 test.raw
  $ VBoxManage modifyvdi /full/path/to/test.vdi compact
or 
  $ qemu-img convert -O raw test.qcow2 test.raw
    (of course you must have installed qemu package for that)
  $ VBoxManage convertfromraw /full/path/to/test.raw /full/path/to/test.vdi
  $ VBoxManage modifyvdi      /full/path/to/test.vdi compact

从VMware映像转换

运行

  $ VBoxManage clonehd source.vmdk target.vdi --format VDI

对于当前VirtualBox版本来说也许是不必要的(有待证实)