# 24.5.在 FreeBSD 上安装 VirtualBox™

VirtualBox™ 是一款正在积极开发的完整虚拟化套件，适用于包括 Windows®、macOS®、Linux® 和 FreeBSD 在内的大多数操作系统。它同样可以运行 Windows® 或类 UNIX® 的虚拟机系统。它作为开源软件发布，但某些组件以闭源扩展包的形式单独提供。这些组件包括对 USB 2.0 设备的支持。更多信息可参考 [VirtualBox™ Wiki 的下载页面](http://www.virtualbox.org/wiki/Downloads)。目前，这些扩展在 FreeBSD 上尚不可用。

## 24.5.1. 安装 VirtualBox™

VirtualBox™ 可通过 FreeBSD 的软件包或 Ports 系统获取，位于 [emulators/virtualbox-ose](https://cgit.freebsd.org/ports/tree/emulators/virtualbox-ose/)。使用以下命令安装该 Ports：

```sh
# cd /usr/ports/emulators/virtualbox-ose
# make install clean
```

Ports 的配置菜单中有一个实用选项 `GuestAdditions`，它提供多项对虚拟机操作系统有用的功能，例如鼠标指针集成（允许在宿主机与虚拟机之间共享鼠标，无需按快捷键切换）和更快的视频渲染，尤其是在 Windows® 虚拟机中更为明显。安装完成虚拟机系统后，可在 **Devices** 菜单中使用该增强功能。

在第一次启动 VirtualBox™ 前，需要进行一些配置。该 Ports 会在 **/boot/modules** 中安装一个内核模块，需将其加载进当前运行的内核中：

```sh
# kldload vboxdrv
```

为了在系统重启后自动加载该模块，请在 **/boot/loader.conf** 中添加以下行：

```sh
vboxdrv_load="YES"
```

若要使用支持桥接或仅主机网络的内核模块，请在 **/etc/rc.conf** 中添加以下内容并重启计算机：

```sh
vboxnet_enable="YES"
```

安装 VirtualBox™ 时会创建 `vboxusers` 用户组。所有需要使用 VirtualBox™ 的用户都必须添加到该组中。可以使用 `pw` 命令添加用户：

```sh
# pw groupmod vboxusers -m yourusername
```

默认情况下，**/dev/vboxnetctl** 的权限较为严格，需更改权限以启用桥接网络：

```sh
# chown root:vboxusers /dev/vboxnetctl
# chmod 0660 /dev/vboxnetctl
```

若要使该权限更改永久生效，请在 **/etc/devfs.conf** 中添加以下内容：

```ini
own     vboxnetctl root:vboxusers
perm    vboxnetctl 0660
```

在 Xorg 会话中启动 VirtualBox™ 的命令如下：

```sh
% VirtualBox
```

关于 VirtualBox™ 的更多配置与使用信息，请参考 [官方网站](http://www.virtualbox.org/)。有关 FreeBSD 的特定信息与故障排除，可参阅 [FreeBSD Wiki 中相关页面](http://wiki.freebsd.org/VirtualBox)。

## 24.5.2. VirtualBox™ USB 支持

VirtualBox™ 可配置将 USB 设备传递给虚拟机操作系统。OSE 版本的宿主机控制器仅能模拟 USB 1.1 设备，直到支持 USB 2.0 和 3.0 的扩展包在 FreeBSD 上可用为止。

为使 VirtualBox™ 能识别已连接的 USB 设备，用户必须是 `operator` 用户组的成员：

```sh
# pw groupmod operator -m yourusername
```

然后，在 **/etc/devfs.rules** 中添加以下内容（若该文件尚不存在则需新建）：

```ini
[system=10]
add path 'usb/*' mode 0660 group operator
```

要加载这些新规则，请在 **/etc/rc.conf** 中添加以下内容：

```ini
devfs_system_ruleset="system"
```

接着重启 devfs 服务：

```sh
# service devfs restart
```

重新登录会话并重启 VirtualBox™ 使上述更改生效，并根据需要创建 USB 过滤器。

## 24.5.3. VirtualBox™ 宿主机访问 DVD/CD

虚拟机系统访问宿主机 DVD/CD 驱动器是通过共享物理设备实现的。在 VirtualBox™ 中，可通过虚拟机设置中的 Storage 窗口进行配置。如有需要，先创建一个空的 IDECD/DVD 设备。然后在虚拟 CD/DVD 选择菜单中选择 Host Drive。会出现一个名为 `Passthrough` 的复选框，勾选该项后虚拟机会直接访问硬件。例如，音频 CD 或刻录功能仅在该选项启用时可用。

为使用户能够使用 VirtualBox™ 的 DVD/CD 功能，必须具有对 **/dev/xpt0**、**/dev/cdN** 和 **/dev/passN** 的访问权限。通常将用户加入 `operator` 用户组即可。还需通过在 **/etc/devfs.conf** 中添加以下内容来修正这些设备的权限：

```ini
perm cd* 0660
perm xpt0 0660
perm pass* 0660
```

```sh
# service devfs restart
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://book.bsdcn.org/hanbook/di-24-zhang-xu-ni-hua/24.5.-zai-freebsd-shang-an-zhuang-virtualbox.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
