> For the complete documentation index, see [llms.txt](https://handbook.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://handbook.bsdcn.org/di-25-zhang-xu-ni-hua/25.4.-shi-yong-virtualbox-an-zhuang-freebsd.md).

# 25.4.使用 VirtualBox™ 安装 FreeBSD

FreeBSD 在 VirtualBox™ 中作为虚拟机运行效果良好。该虚拟化软件可在大多数常见操作系统上运行，包括 FreeBSD 本身。

VirtualBox™ 的虚拟机增强功能提供以下支持：

* 剪贴板共享。
* 鼠标指针集成。
* 宿主时间同步。
* 窗口缩放。
* 无缝模式。

若要运行 Xorg，在安装 Guest Additions 之前，请确保在虚拟机配置的 Display 部分的 Screen 设置中选择 **VBoxSVGA**。请勿选择 VMSVGA，因为它是面向 VMware 虚拟机的，在 FreeBSD 上不可用，会导致 Xorg 无法正常运行。VBoxSVGA 不存在此问题，可以无错误地运行 X11。

> **注意**
>
> 以下命令在 FreeBSD 虚拟机中执行。

在 FreeBSD 虚拟机中安装软件包或 Port [emulators/virtualbox-ose-additions](https://cgit.freebsd.org/ports/tree/emulators/virtualbox-ose-additions/)。这将安装该 Port：

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

在 **/etc/rc.conf** 中添加以下内容：

```sh
vboxguest_enable="YES"
vboxservice_enable="YES"
```

如果使用了 [ntpd(8)](https://man.freebsd.org/cgi/man.cgi?query=ntpd\&sektion=8\&format=html) 或 [ntpdate(8)](https://man.freebsd.org/cgi/man.cgi?query=ntpdate\&sektion=8\&format=html)，应禁用宿主时间同步：

```sh
vboxservice_flags="--disable-timesync"
```

Xorg 会自动识别 `vboxvideo` 驱动。也可以在 **/etc/X11/xorg.conf** 中手动指定：

```sh
Section "Device"
	Identifier "Card0"
	Driver "vboxvideo"
	VendorName "InnoTek Systemberatung GmbH"
	BoardName "VirtualBox Graphics Adapter"
EndSection
```

若要使用 `vboxmouse` 驱动，请修改 **/etc/X11/xorg.conf** 中的鼠标部分：

```sh
Section "InputDevice"
	Identifier "Mouse0"
	Driver "vboxmouse"
EndSection
```

在宿主机和虚拟机之间传输文件的共享文件夹，可通过 `mount_vboxvfs` 挂载访问。可以使用 VirtualBox 图形界面或 `vboxmanage` 创建共享文件夹。例如，要为名为 *BSDBox* 的虚拟机创建一个名为 *myshare* 的共享文件夹，主机路径为 **/mnt/bsdboxshare**，请执行：

```sh
# vboxmanage sharedfolder add 'BSDBox' --name myshare --hostpath /mnt/bsdboxshare
```

注意，共享文件夹名称不得包含空格。在虚拟机系统中挂载共享文件夹的命令如下：

```sh
# mount_vboxvfs -w myshare /mnt
```
