VirtualBox (简体中文)
| 摘要 |
|---|
| 本文介绍 VirtualBox 基本使用,包括在 Arch 上运行 VirtualBox 软件,在 VirtualBox 中运行 Arch 系统。 |
| 必备软件 |
| VirtualBox |
| 相关页面 |
| VirtualBox Extras |
| PhpVirtualBox |
| VirtualBox Arch Linux Guest On Physical Drive |
| Installing Arch Linux from VirtualBox |
| Moving an existing install into (or out of) a virtual machine |
VirtualBox 是类似 VMware 的虚拟 PC 模拟器,处于不断的开发中。使用 Qt 图形界面,提供了无界面运行和 SDL 命令行工具进行运行管理。它包含guest additions为一些虚拟系统提供附加功能,包括文件共享、剪贴板和图形加速,支持 “无缝” 窗口整合模式。
Contents |
在主机上安装
安装 GPL 版本的 virtualbox 软件包。作为依赖安装的 virtualbox-host-modules 包含了archlinux 默认内核使用的模块。
如果使用linux-lts内核,需要安装virtualbox-host-modules-lts.
要使用基于 Qt 的 VirtualBox 命令,需要安装 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/\ /\//')
然后装载模块:
# modprobe vboxdrv
若要开机时自动加载/编译 VirtualBox 模块,你可以啟用 dkms.service:
# systemctl enable dkms.service
每次更新内核时, 自动重编 VirtualBox 主系统模块
这个功能得益于AUR中的 virtualbox-hook。在 virtualbox-hook 中,每次更新 linux-headers 包之后,mkinitcpio 上的 vbox hook 会完成 'automatic re-compilation' 。你需要将 'vbox' 加入 /etc/mkinitcpio.conf 中的 HOOKS 数组,同样地,需要加入到 'linux-headers' 里和你自己定制的内核头文件里的 /etc/pacman.conf 中的 SyncFirst 数组中以使其生效。
这个 hook 会调用 dkms 命令去更新 VirtualBox 模块以与新的内核版本相适应。
设置
将需要运行 Virtualbox 的用户名添加到 vboxusers 用户组,文件夹共享和其它功能需要正确的组才能工作。新组设置不会应用到当前会话,请重新登录或者通过命令 newgrp 或 sudo -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
啟动 VirtualBox 图形管理员:
$ VirtualBox
為了保证桥接网络具有完整功能,请确认除了加载 vboxnetadp、vboxnetflt 以及 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 附加软件包
手动装入内核模块:
# 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寄宿模块。
开啟共享服务
在安装 virtualbox-guest-utils 之后,你应该运行 VBoxClient-all 啟动共享剪贴板、调整屏幕大小等服务。
- 若你运行会啟动
/etc/xdg/autostart/vboxclient.desktop的服务,例如 GNOME 或 KDE,便不需再进行额外动作。 - If you use
.xinitrcto launch things instead, you must add the following to your.xinitrcbefore launching your WM.
# VBoxClient-all &
使用 USB 摄像头 / 麦克风
- 要确保没有在运行虚拟机,以及没有使用摄像头 / 麦克风。
- 进入VirtualBox主界面并打开Arch系统的设置界面,到USB设备页。
- 要确保勾选上“启用USB控制器”选项。 还要确保选择“启用USB 2.0(EHCI)控制器”选项。
- 点击“从设备列表中添加筛选器”按钮 (就是那个有“+”图标的连接线).
- 从列表中选择你的USB摄像头 / 麦克风设备。
- 然后再点击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.
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
更多信息请看这个主题。
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 won't 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
虚拟系统使用串行端口
确认你的串行端口权限
$ /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 didn't test it correctly, but I assume this works, else check the wiki page for downgrading: https://wiki.archlinux.org/index.php/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 版本释出时已解决