FreeBSD 14.1系统下 Bhyve 安装 Windows 虚拟机

Bhyve虚拟机从 FreeBSD 10 开始即成为了 FreeBSD 的基本系统之一。作为一个 FreeBSD 系统的初学者,尝试在 Bhyve 下安装 Windows 就成了一件心心念念的事了。

1. 宿主机配置介绍

  • Main Board: Asrock Rack ROMED8-2T
  • CPU: Epyc 7R32
  • RAM: DDR4 RDIMM 32G*8
  • GPU: nVidia RTX A4000
  • NVME Disk: GeiL P4P 2T

2. 宿主机系统

  • OS: FreeBSD 14.1
  • GUI: GNOME 42.7

3. 安装虚拟机管理系统

  • 安装虚拟机管理系统
    1
    root@freebsd:~ # pkg install vm-bhyve-devel bhyve-firmware edk2-bhyve dnsmasq grub2-bhyve tigervnc-viewer rdesktop
  • 创建用于虚拟机的 ZFS 资料集
    1
    root@freebsd:~ # zfs create -o mountpoint=/vm zroot/vm
  • 复制虚拟机模板到 vm 的模板目录
    1
    root@freebsd:~ # cp -a /usr/local/share/examples/vm-bhyve /vm/.templates
  • 修改/etc/rc.conf
    1
    2
    3
    4
    vm_enable=YES
    vm_dir="zfs:zroot/vm"
    vm_list=""
    vm_delay=3
  • 启动虚拟机管理系统
    1
    root@freebsd:~ # service vm start

4. 网络设置

1
鉴于我创建的虚拟机使用的是与宿主机相同网段的固定 ip 地址,这次暂未对网络进行特别设置,待后续完善。

5. 安装虚拟机的系统镜像

每台虚拟机在安装系统时都需要的有 ISO 镜像文件,有两种方式给 vm-bhyve 提供镜像:

  • 一是直接从网络上拉取
    1
    2
    3
    4
    5
    6
    root@freebsd:~ # vm iso https://download.freebsd.org/releases/amd64/amd64/ISO-IMAGES/14.1/FreeBSD-14.1-RELEASE-amd64-disc1.iso
    https://download.freebsd.org/releases/amd64/amd64/ISO-IMAGES/14.1/FreeBSD-14.1-RELEASE-amd64-disc1.iso
    root@freebsd:~ # vm iso
    DATASTORE FILENAME
    default FreeBSD-14.1-RELEASE-amd64-disc1.iso
    root@freebsd:~ #
  • 二是直接复制到 /vm/.iso 目录下
    1
    2
    3
    4
    5
    root@freebsd:~ # cp /mnt/e/software/win/microsoft/cn_windows_7_professional_with_sp1_vl_build_x64_dvd_u_677816.iso /vm/.iso
    root@freebsd:~ # vm iso
    DATASTORE FILENAME
    default cn_windows_7_professional_with_sp1_vl_build_x64_dvd_u_677816.iso
    default FreeBSD-14.1-RELEASE-amd64-disc1.iso

6. 安装 Windows 客户机

  • 安装 Winodws 7

    Windows 7 不再被 Bhyve 正式支持了。但依然还是可以进行安装的,需要对模板进行一定的修改

    • 以下是原版的 Windows 模板:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      root@freebsd:~ # cat /vm/.templates/windows.conf
      loader="uefi"
      graphics="yes"
      xhci_mouse="yes"
      cpu=2
      memory=32G

      # put up to 8 disks on a single ahci controller.
      # without this, adding a disk pushes the following network devices onto higher slot numbers,
      # which causes windows to see them as a new interface
      ahci_device_limit="8"

      # ideally this should be changed to virtio-net and drivers installed in the guest
      # e1000 works out-of-the-box
      network0_type="e1000"
      network0_switch="public"

      disk0_type="nvme"
      disk0_name="disk0.img"

      # windows expects the host to expose localtime by default, not UTC
      utctime="no"
      bhyve_options="-s 8,hda,play=/dev/dsp,rec=/dev/dsp"
    • 将模板复制成 windows7.conf 并修改成以下:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      root@freebsd:~ # cat /vm/.templates/windows7.conf
      loader="uefi"
      graphics="yes"
      # xhci_mouse="yes"
      cpu=2
      memory=8G

      # put up to 8 disks on a single ahci controller.
      # without this, adding a disk pushes the following network devices onto higher slot numbers,
      # which causes windows to see them as a new interface
      ahci_device_limit="8"

      # ideally this should be changed to virtio-net and drivers installed in the guest
      # e1000 works out-of-the-box
      network0_type="e1000"
      network0_switch="public"

      disk0_type="ahci-hd"
      disk0_name="disk0.img"
      disk0_opts="sectorsize=512"
      # windows expects the host to expose localtime by default, not UTC
      utctime="no"
      bhyve_options="-s 8,hda,play=/dev/dsp,rec=/dev/dsp"

      如果设置 xhci_mouse=”yes” 的话,在 VNC 连接客户机的时候你将无法使用鼠标
      客户机磁盘选项设置成 disk0_opts=”sectorsize=512”,不然安装时会出错,如下图:

    • 创建 Windows 7 客户机:
      1
      root@Ffreebsd:~ # vm create -t windows7 -s 160G win7
    • 安装 Windows 7 系统:
      1
      2
      3
      4
      5
      6
      7
      root@freebsd:~ # vm install win7 cn_windows_7_professional_with_sp1_vl_build_x64_dvd_u_677816.iso
      Starting win7
      * found guest in /vm/win7
      * booting...
      root@freebsd:~ # vm list
      NAME DATASTORE LOADER CPU MEMORY VNC AUTO STATE
      win7 default uefi 2 8G 0.0.0.0:5900 No Running (6779)
    • 此时你就可以打开刚安装的 TigerVNC Viewer 查看并按常规步骤进行 Windows 7 的安装了。
      1
      root@Ffreebsd:~ # vncviewer -SendClipboard -AcceptClipboard -LowColorLevel -QualityLevel 6 :5900 &
    • 安装完毕后在 Windows 7 中打开远程桌面,并将 ip 地址设置为静态地址,如下图:

    • 使用 rdesktop 进行远程连接:
      1
      2
      3
      4
      5
      6
      root@freebsd:~ # rdesktop -u <user name> -p <user password> -P -N -z -g 2560x1440 -a 24 -r sound:local -r disk:HOME=/home/xxx <ip address>
      Autoselecting keyboard map 'en-us' from locale
      Core(warning): Certificate received from server is NOT trusted by this system, an exception has been added by the user to trust this specific certificate.
      Connection established using SSL.
      Protocol(warning): process_pdu_logon(), Unhandled login infotype 1
      Clipboard(error): xclip_handle_SelectionNotify(), unable to find a textual target to satisfy RDP clipboard text request

      由于我已远程连接过了 Windows 7,仅显示如上信息,如是第一次连接的话,会显示如下一系列信息,届时选择 “yes” 就行。

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      root@freebsd:~ # rdesktop -u <user name> -p <user password> -P -N -z -g 2560x1440 -a 24 -r sound:local -r disk:HOME=/home/xxx <ip address>
      Autoselecting keyboard map 'en-us' from locale

      ATTENTION! The server uses and invalid security certificate which can not be trusted for
      the following identified reasons(s);

      1. Certificate issuer is not trusted by this system.

      Issuer: CN=vbox


      Review the following certificate info before you trust it to be added as an exception.
      If you do not trust the certificate the connection atempt will be aborted:

      Subject: CN=vbox
      Issuer: CN=vbox
      Valid From: Mon Sep 11 00:58:25 2024
      To: Mon Jan 9 23:58:25 2025

      Certificate fingerprints:

      sha1: 4ad853c40a8aa0560af315b691038202506e07ce
      sha256: 44ec8f7650486aef6261aea42da99caba4e84d7bc58341c0ca1bb8e28b81d222


      Do you trust this certificate (yes/no)? yes
      Connection established using SSL.

      rdesktop 参数解释:
      1
      2
      3
      4
      5
      6
      7
      -u <user name> : 远程登录的用户名
      -p <user password> : 远程登录的用户密码
      -P : 允许缓存图像来改善性能
      -N : 在宿主机与客户机中同步数字键(numlock)
      -z : 允许压缩远程桌面数据
      -g 2560x1440 : 设置的远程桌面分辨率
      -r disk:/home/xxx : 在宿主机与客户机之间的共享文件夹,如下图所示:


      1
      -r sound:local : 将宿主机的声音系统带入客户机。

  • 安装 Windows 10

    安装 Windows 10 的设置基本与安装 Windows 7 相同,只要使用虚拟机模板中的 windows 通用模板就行,也可根据自己需要修改 cpu 数量与 memory 的容量就行。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    root@freebsd:~ # cat /vm/.templates/windows.conf 
    loader="uefi"
    graphics="yes"
    xhci_mouse="yes"
    cpu=2
    memory=32G

    # put up to 8 disks on a single ahci controller.
    # without this, adding a disk pushes the following network devices onto higher slot numbers,
    # which causes windows to see them as a new interface
    ahci_device_limit="8"

    # ideally this should be changed to virtio-net and drivers installed in the guest
    # e1000 works out-of-the-box
    network0_type="e1000"
    network0_switch="public"

    disk0_type="nvme"
    disk0_name="disk0.img"

    # windows expects the host to expose localtime by default, not UTC
    utctime="no"
    bhyve_options="-s 8,hda,play=/dev/dsp,rec=/dev/dsp"

7. Windows 7 虚拟机使用感受:

就我个人而言,我觉得比在同硬件系统 Winodws 10 下的 Virtual Box 或 QEMU 中安装的 Windows 7 使用起来更丝滑,磁盘性能更好。待日后有空时再进行测试对比。