Lima运行FreeBSD虚拟机

为了方便兼顾 Swift 开发,同时降低每次反复折腾 Linux桌面 (特别是 Gentoo Linux Sway fcitx中文输入 非常折腾),我现在重新把桌面回归到 macOS 。为了能够继续学习实践 FreeBSD 技术,改为采用 Lima: Linux Machines 虚拟机运行。不过,我也同时 在苹果Intel版Mac上安装FreeBSD ,确保能够在实际硬件和虚拟环境都能不断磨练FreeBSD相关技能。

备注

由于Lima目前不支持USB设备支持,而我近期考虑在USB设备上构建 LFS(Linux from scratch) ;并且,Lima实际上也是通过 QEMU 来运行FreeBSD。 我最终决定改为采用 utm 来统一运行虚拟化。

但是,目前不管是 Lima 还是 macOS平台QEMU实现:UTM ,都没有实现 Apple Virtualization 虚拟化下的USB设备支持。这导致不管怎样,都需要使用 QEMU 来运行 FreeBSD 以及需要访问USB设备的 Linux 虚拟机。

macOS平台QEMU实现:UTM 的优点是图形化交互界面,完全和商业软件 Studio环境中的VMware Fusion 对齐

Apple的Virtualization framework思考 之后,目前依然主力选择 Lima: Linux Machines

备注

本文记录我在 Lima: Linux Machines 尝试运行FreeBSD虚拟机的两种后端: QEMUApple Virtualization (VZ)。目前实践验证,FreeBSD 14.2 需要在 QEMU 环境运行。

完成运行FreeBSD之后,则进行 FreeBSD虚拟机初始化 (相对 FreeBSD初始化 会多一些环境设置步骤)

lima的FreeBSD模版

  • 准备模版文件 /usr/local/share/lima/templates/freebsd.yaml (具体模版目录可能根据lima安装发布,如果配置文件没有放到正确位置 lima start 会提示找不到 .../freebsd.yaml ,则根据提示将配置文件复制到对应位置即可) :

准备模版
images:
- location: https://mirrors.163.com/freebsd/releases/VM-IMAGES/14.2-RELEASE/amd64/Latest/FreeBSD-14.2-RELEASE-amd64-zfs.qcow2.xz
  arch: "x86_64"
- location: https://mirrors.163.com/freebsd/releases/VM-IMAGES/14.2-RELEASE/aarch64/Latest/FreeBSD-14.2-RELEASE-arm64-aarch64-zfs.qcow2.xz
  arch: "aarch64"

video:
  display: "default" 

备注

这里我使用了国内163的镜像网站

  • 启动:

启动(失败)
limactl start \
  --name=freebsd \
  --cpus=2 \
  --memory=4 \
  --vm-type=vz \
  --mount-type=virtiofs \
  --mount-writable \
  --network=vzNAT \
  template://freebsd

这里提示错误

运行提示
? Creating an instance "freebsd" Proceed with the current configuration
INFO[0006] Replacing "http_proxy" value "http://127.0.0.1:3128" with "http://192.168.5.2:3128" 
INFO[0006] Replacing "https_proxy" value "http://127.0.0.1:3128" with "http://192.168.5.2:3128" 
INFO[0006] Replacing "HTTP_PROXY" value "http://127.0.0.1:3128" with "http://192.168.5.2:3128" 
INFO[0006] Replacing "HTTPS_PROXY" value "http://127.0.0.1:3128" with "http://192.168.5.2:3128" 
INFO[0006] Starting the instance "freebsd" with VM driver "vz" 
INFO[0006] Attempting to download the image              arch=x86_64 digest= location="https://mirrors.163.com/freebsd/releases/VM-IMAGES/14.2-RELEASE/amd64/Latest/FreeBSD-14.2-RELEASE-amd64-zfs.qcow2.xz"
Downloading the image (FreeBSD-14.2-RELEASE-amd64-zfs.qcow2.xz)
800.66 MiB / 800.66 MiB [---------------------------------] 100.00% 396.67 KiB/s
INFO[2075] decompressing .xz with xz                    
INFO[2075] Decompressing data                           
800.66 MiB / 800.66 MiB [----------------------------------] 100.00% 81.62 MiB/s
INFO[2086] Downloaded the image from "https://mirrors.163.com/freebsd/releases/VM-IMAGES/14.2-RELEASE/amd64/Latest/FreeBSD-14.2-RELEASE-amd64-zfs.qcow2.xz" 
INFO[2086] Converting "/Users/huatai/.lima/freebsd/basedisk" (qcow2) to a raw disk "/Users/huatai/.lima/freebsd/diffdisk" 
6.03 GiB / 6.03 GiB [-------------------------------------] 100.00% 467.40 MiB/s
INFO[2099] Expanding to 100GiB                          
INFO[2099] Attempting to download the nerdctl archive    arch=x86_64 digest="sha256:91bfb8faec1673f3e7c3a020812acffc50a7d7dd82019461f6cfa46435240903" location="https://github.com/containerd/nerdctl/releases/download/v2.0.3/nerdctl-full-2.0.3-linux-amd64.tar.gz"
Downloading the nerdctl archive (nerdctl-full-2.0.3-linux-amd64.tar.gz)
218.50 MiB / 218.50 MiB [---------------------------------] 100.00% 393.17 KiB/s
INFO[2671] Downloaded the nerdctl archive from "https://github.com/containerd/nerdctl/releases/download/v2.0.3/nerdctl-full-2.0.3-linux-amd64.tar.gz" 
INFO[2671] [hostagent] Replacing "http_proxy" value "http://127.0.0.1:3128" with "http://192.168.5.2:3128" 
INFO[2671] [hostagent] Replacing "https_proxy" value "http://127.0.0.1:3128" with "http://192.168.5.2:3128" 
INFO[2671] [hostagent] Replacing "HTTP_PROXY" value "http://127.0.0.1:3128" with "http://192.168.5.2:3128" 
INFO[2671] [hostagent] Replacing "HTTPS_PROXY" value "http://127.0.0.1:3128" with "http://192.168.5.2:3128" 
INFO[2671] [hostagent] Replacing "http_proxy" value "http://127.0.0.1:3128" with "http://192.168.5.2:3128" 
INFO[2671] [hostagent] Replacing "https_proxy" value "http://127.0.0.1:3128" with "http://192.168.5.2:3128" 
INFO[2671] [hostagent] Replacing "HTTP_PROXY" value "http://127.0.0.1:3128" with "http://192.168.5.2:3128" 
INFO[2671] [hostagent] Replacing "HTTPS_PROXY" value "http://127.0.0.1:3128" with "http://192.168.5.2:3128" 
INFO[2673] [hostagent] hostagent socket created at /Users/huatai/.lima/freebsd/ha.sock 
INFO[2673] [hostagent] Starting VZ (hint: to watch the boot progress, see "/Users/huatai/.lima/freebsd/serial*.log") 
INFO[2673] [hostagent] reloading dns nameservers to []  
INFO[2674] SSH Local Port: 60094                        
INFO[2673] [hostagent] [VZ] - vm state change: running  
INFO[2673] [hostagent] Waiting for the essential requirement 1 of 2: "ssh" 
INFO[2674] [hostagent] 2025-01-24 21:41:28.446 limactl[83836:5655467] +[IMKClient subclass]: chose IMKClient_Modern 
INFO[2674] [hostagent] 2025-01-24 21:41:28.446 limactl[83836:5655467] +[IMKInputSession subclass]: chose IMKInputSession_Modern 
INFO[2683] [hostagent] Waiting for the essential requirement 1 of 2: "ssh" 
INFO[2687] [hostagent] 2025/01/24 21:41:41 tcpproxy: for incoming conn 127.0.0.1:60098, error dialing "192.168.5.15:22": connect tcp 192.168.5.15:22: no route to host 
INFO[2693] [hostagent] 2025-01-24 21:41:47.288 limactl[83836:5655558] VM <ObservableVZVirtualMachine: 0x7fa3ad708870> didStopWithError: Error Domain=VZErrorDomain Code=1 "The virtual machine stopped unexpectedly." UserInfo={NSLocalizedFailure=Internal Virtualization error., NSLocalizedFailureReason=The virtual machine stopped unexpectedly.} 
FATA[2693] exiting, status={Running:false Degraded:false Exiting:true Errors:[] SSHLocalPort:0} (hint: see "/Users/huatai/.lima/freebsd/ha.stderr.log")

可以看到启动的界面,但是运行时crash(虚拟机屏幕开始滚动输出时)

  • 修订启动,去掉VZ配置

不指定vm-type启动freebsd
# 不指定 --vm-type
limactl start \
  --name=bsd \
  --cpus=2 \
  --memory=4 \
  --mount-writable \
  template://freebsd

依然没有解决,看起来默认运行VZ了:

不指定VZ启动freebsd
...
INFO[0046] [hostagent] Waiting for the essential requirement 1 of 2: "ssh" 
INFO[0050] [hostagent] 2025/01/24 22:13:01 tcpproxy: for incoming conn 127.0.0.1:60265, error dialing "192.168.5.15:22": connect tcp 192.168.5.15:22: no route to host 
INFO[0055] [hostagent] 2025-01-24 22:13:07.107 limactl[84260:5680606] VM <ObservableVZVirtualMachine: 0x7fdfb3704800> didStopWithError: Error Domain=VZErrorDomain Code=1 "The virtual machine stopped unexpectedly." UserInfo={NSLocalizedFailure=Internal Virtualization error., NSLocalizedFailureReason=The virtual machine stopped unexpectedly.} 
FATA[0055] exiting, status={Running:false Degraded:false Exiting:true Errors:[] SSHLocalPort:0} (hint: see "/Users/huatai/.lima/bsd-dev/ha.stderr.log") 

备注

lima 1.0 开始,如果macOS >= 13.5 ,则新建实例默认使用 VZ ( Apple Virtualization 后端)。见 Lima Documentatation / Configuration guide / VM types

  • 修订启动,指定qemu

指定vm-type为qemu启动freebsd
limactl start \
  --name=bsd \
  --cpus=2 \
  --memory=4 \
  --mount-writable \
  --vm-type=qemu \
  template://freebsd

报错显示系统没有qemu安装

指定vm-type为qemu启动freebsd,发现没有qemu
INFO[0002] Replacing "HTTP_PROXY" value "http://127.0.0.1:3128" with "http://192.168.5.2:3128" 
INFO[0002] Replacing "HTTPS_PROXY" value "http://127.0.0.1:3128" with "http://192.168.5.2:3128" 
INFO[0002] Starting the instance "bsd" with VM driver "qemu" 
FATA[0002] failed to find the QEMU binary for the architecture "x86_64": exec: "qemu-system-x86_64": executable file not found in $PATH
安装 qemu
brew install qemu

解决了,果然在 macOS 环境下,使用 QEMU 运行 FreeBSD 是成功的:

../../../_images/lima_freebsd.png

通过 QEMU 虚拟化,lima可以运行FreeBSD虚拟机

小结

我的实践显示在 lima 中使用 VZ 虚拟化不能运行FreeBSD,但是我也有点疑惑,从之前查询到信息 Running GUI FreeBSD in a virtual machine on a Mac (我准备尝试一下 在Apple Virtualization中运行FreeBSD ),使用 Apple Virtualization 框架应该能够运行FreeBSD。具体是哪里出错,我还得再研究一下。

参考