环境
Windows操作系统,VirtualBox Version 7.1.8 r168469 (Qt6.5.3)。想安装FreeBSD操作系统。
下载介质
FreeBSD官网提供虚机下载。
虚机提供多种格式,一定要先读README:
FreeBSD virtual machine disk images are available in various formats for several hypervisors. Disk image formats and the supported hypervisors are: vhd: VirtualPC, Hyper-V, Xen, VirtualBox vmdk: VMWare qcow2: Qemu, KVM raw: bhyve, other hypervisors that support unformatted raw disk image因为我的VMM是VirtualBox,所以要下载vhd格式。
我选择的是FreeBSD 14.4版本,即使是vhd格式,也有3个文件可供下载:
- FreeBSD-14.4-RELEASE-amd64-ufs.vhd.xz(经典安装)
- FreeBSD-14.4-RELEASE-amd64-zfs.vhd.xz (企业级安装,ZFS包括快照等功能)
- FreeBSD-14.4-RELEASE-amd64.vhd.xz (默认安装)
最终我选择的是默认安装。文件大小为600M:
$ls-1sh*.xz 605M FreeBSD-14.4-RELEASE-amd64.vhd.xz创建虚机
解压下载的文件,Windows下可以用7zip,解压后2.5G。
$ls-1sh*.vhd2.5G FreeBSD-14.4-RELEASE-amd64.vhd启动VirtualBox,选择’New’创建虚机:
设定VM Name,OS选择BSD:
内存配置为默认的1G:
然后展开Specify Virtual hardware,在Specify virtual hard disk下,选择Use an Existing Virtual Hard Disk File:
这个新的磁盘还不存在,需要通过Add添加:
磁盘大小为6.03G:
💡 使用已有磁盘时,也可以先把磁盘移动到比较规范的位置,如C:\Users\XiaoYu\VirtualBox VMs\FreeBSD。
扩容虚拟磁盘
其实这是事后才知道的。因为后续安装VirtualBox Guest Addition需要2G多的空间。
此时先不要启动虚机,因为虚拟磁盘的大小只有在关机时才能修改。
进入菜单File>Tools>Media,将磁盘的Virtual Size修改为10G。
启动虚机
启动FreeBSD,用户名输入root,口令回车即可:
当前空间状态:
root@freebsd:~# df -hFilesystem Size Used Avail Capacity Mounted on /dev/gpt/rootfs8.7G2.4G5.6G30% / devfs1.0K 0B1.0K0% /dev /dev/gpt/efiesp 32M 649K 31M2% /boot/efirootfs不到9G,因为还有1G的swap。
root@freebsd:~# gpart show -l ada0=>3420971446ada0 GPT(10G)341221bootfs(61K)156665842efiesp(33M)6674020971523swapfs(1G)2163892188075884rootfs(9G)配置SSH登录
我们把配置SSH放在第一步,是因为终端字符界面太丑了,而且也没法拷贝粘贴。
配置sshd,允许root登录:
vi/etc/ssh/sshd_config PermitRootLoginyes配置和启动sshd服务:
sysrcsshd_enable="YES"servicesshd start为root用户设置口令:
passwdroot为虚机配置port forwarding,例如将宿主机的8822端口映射为客户机的22端口。
然后PuTTY ssh登录成功,也可以拷贝粘贴了:
安装VirtualBox Guest Additions
先更新系统:
pkg update-f输出如下:
root@freebsd:~# pkg update -fThe package management tool is not yet installed on your system. Do you want to fetch andinstallit now?[y/N]: y Bootstrapping pkg from pkg+https://pkg.FreeBSD.org/FreeBSD:14:amd64/quarterly, please wait... Verifying signature with trusted certificate pkg.freebsd.org.2013102301...doneInstalling pkg-2.7.5... Extracting pkg-2.7.5:100% Updating FreeBSD repository catalogue... Fetching meta.conf:100%179B0.2kB/s 00:01 Fetching data:100%11MiB1.2MB/s 00:09 Processing entries:100% FreeBSD repository update completed.37777packages processed. Updating FreeBSD-kmods repository catalogue... Fetching meta.conf:100%179B0.2kB/s 00:01 Fetching data:100%36KiB37.0kB/s 00:01 Processing entries:100% FreeBSD-kmods repository update completed.245packages processed. All repositories are up to date.更新后空间状态,只用了0.1G:
root@freebsd:~# df -h /Filesystem Size Used Avail Capacity Mounted on /dev/gpt/rootfs8.7G2.5G5.5G31% /然后安装VirtualBox Guest Additions:
pkginstall-yvirtualbox-ose-additions日志如下:
root@freebsd:~# pkg install -y virtualbox-ose-additionsUpdating FreeBSD repository catalogue... FreeBSD repository is up to date. Updating FreeBSD-kmods repository catalogue... FreeBSD-kmods repository is up to date. All repositories are up to date. Updating database digests format:100% The following60package(s)will be affected(of0checked):... Number of packages to be installed:60The process will require2GiBmorespace.364MiB to be downloaded.... Checking integrity...done(0conflicting)...===>CreatinggroupsCreating group'messagebus'with gid'556'===>CreatingusersCreating user'messagebus'with uid'556'... VirtualBox Guest Additions are installed. Toenableand start the required services:# sysrc vboxguest_enable="YES"# sysrc vboxservice_enable="YES"To start the services, restart the system. In some situations, a panic will occur when the kernel module loads. Having nomorethan one virtual CPU might mitigate the issue. For features such as window scaling and clipboard sharing, membership of the wheel group is required. With username"jerry"as an example:# pw groupmod wheel -m jerryThe settings dialogueforFreeBSD guests encourages use of the VMSVGA graphics controller. Whilst this might suit installations of FreeBSD without a desktop environment(a common usecase), it is not appropriate where Guest Additions are installed. Where Guest Additions are installed:1. prefer VBoxSVGA2.donotenable3D acceleration(doing so will invisibly lose the preferenceforVBoxSVGA)You may ignore the yellow alert that encourages use of VMSVGA. --===>NOTICE: This port is deprecated;you may wish to reconsider installing it: Upstream EOL reaches on2024-01-31, use emulators/virtualbox-ose-additions-72 instead. It is scheduled to be removed on or after2026-12-31.从输出可知,此安装需要至少2 GiB 空间,这也是之前我们需要扩容的原因。
当前空间,没想到安装Guest Addition使用了2.7G:
root@freebsd:~# df -h .Filesystem Size Used Avail Capacity Mounted on /dev/gpt/rootfs8.7G5.2G2.8G65% /根据上面命令输出的提示,需要将virtualbox-ose-additions更新为virtualbox-ose-additions-72。
很快就完成了,空间几乎没变:
# pkg install -y virtualbox-ose-additions-72# pkg info | grep virtualbox-ose-additionsvirtualbox-ose-additions-72-7.2.12.1404000 VirtualBox additionsforFreeBSD guests设置服务并重启系统:
sysrcvboxguest_enable="YES"sysrcvboxservice_enable="YES"确认服务正常,以及模块已加载:
# service vboxservice statusvboxservice is running as pid766.# kldstat | grep vboxguest420xffffffff8281f000 36b40 vboxguest.ko配置主机客户机共享目录
在virtualbox中创建共享目录,例如名称为Downloads:
配置共享目录后,需重启才能生效。
确认共享文件系统的模块已加载,注意最新的模块名为vboxvfs,而非vboxsf了:
# find /boot -name "*.ko" | grep -i vbox/boot/modules/vboxvfs.ko /boot/modules/vboxguest.ko# kldload vboxvfs# kldstat | grep vbox420xffffffff8281f000 38d00 vboxguest.ko510xffffffff82858000 26bd1 vboxvfs.ko然后挂载共享文件系统:
# mount_vboxvfs Downloads /mnt/share# df -h /mnt/shareFilesystem Size Used Avail Capacity Mounted on Downloads 237G 190G 48G80% /mnt/share