準備工作

vm-bhyve

$ pkg install vm-bhyve-devel uefi-edk2-bhyve-csm bhyve-firmware

vm pool

$ zfs create -o mountpoint=/vm zroot/vm

/etc/rc.conf

vm_enable="YES" 
vm_dir="zfs:zroot/vm"
vm_list=""  #開機自動啟動的VM
vm_delay=3

/boot/loader.conf

#PCI Passtrough
vmm_load="YES" 
#pptdevs="3/0/0 3/0/1" #GPU Passthrough

init

$ vm init

網路

$ vm switch create public
$ vm switch add public 網卡代號

安裝VM

ISO位置

$ vm iso ISO位置

$ vm iso
DATASTORE           FILENAME
default             Win11_23H2_English_x64v2.iso
default             windows.iso

設定檔

範例在 /usr/local/share/examples/vm-bhyve 裡,選擇要安裝的系統,

複製一份到 /vm/.templates/ 下

修改設定檔

以Windows為例

# If you want to pull a graphical console, you'll need the UEFI loader,
# no matter what OS you're installing on the guest.

loader="uefi"
graphics="yes"
xhci_mouse="yes"

# If not specified, cpu=n will give the guest n discrete CPU sockets.
# This is generally OK for Linux or BSD guests, but Windows throws a fit
# due to licensing issues, so we specify CPU topology manually here.

cpu=8
cpu_sockets=2
cpu_cores=4
cpu_threads=1

# Remember, a guest doesn’t need extra RAM for filesystem caching--
# the host handles that for it. 4G is ludicrously low for Windows on hardware,
# but it’s generally more than sufficient for a guest.
memory=8G

 # put up to 8 disks on a single ahci controller. This avoids the creation of
 #  a new “controller” on a new “PCIe slot” for each drive added to the guest.

ahci_device_limit="8"

 # e1000 works out-of-the-box, but virtio-net performs better. Virtio support
# is built in on FreeBSD and Linux guests, but Windows guests will need
# to have virtio drivers manually installed.

#network0_type="e1000"

network0_type="virtio-net"
network0_switch="public"

# bhyve/nvme storage is considerably faster than bhyve/virtio-blk
# storage in my testing, on Windows, Linux, and FreeBSD guests alike.

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

# This gives the guest a virtual "optical" drive. Specifying disk1_dev=”custom”
# allows us to provide a full path to the ISO.

disk1_type="ahci-cd"
disk1_dev="custom"
disk1_name="/vm/virtio-win.iso"

# windows expects the host to expose localtime by default, not UTC

utctime="no"

graphics_port="5900"
graphics_listen="本機地址 x.x.x.x"
graphics_res="1024x768"
graphics_wait="yes"

vnc_password="123456"

其中網路卡設定為virtio-net,這樣安裝Windows過程中就不會要求登入M$帳戶,

可以設定本機用戶安裝

網路等到系統安裝完再手動裝上驅動即可

virtio 驅動下載

https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/latest-virtio/

另外 graphics_wait 要設成 yes

這樣初次安裝時,會等待VNC連上之後,才跑開機程序,才有時間選擇用光碟開機

建立VM

vm create -t windows11 -s 100G windows11
vm install windows11 Win11_22H2_English_x64v2.iso

此時VM已經開機,用VNC軟體連上本機:5900,Mac使用內建即可

如果出現

bhyve

表示太晚按任意鍵,導致沒用光碟開機成功

等它跑完後,會出現下面畫面,輸入exit,

bhyve2

跳到bhyve的BIOS畫面,選擇Reset,即可再次重開機

bhyve3

然後記得按任意鍵,進入Windows安裝程式

如果是Windows 11可透過regedit方法,繞過硬體檢查

Shift+F10
REG ADD HKLM\SYSTEM\Setup\LabConfig /v BypassTPMCheck /t REG_DWORD /d 1
REG ADD HKLM\SYSTEM\Setup\LabConfig /v BypassSecureBootCheck /t REG_DWORD /d 1

安裝完成後,記得去把 /vm/windows11/windows11.conf中

graphics_wait=“yes” 改成"no"

不然每次開機後,會出現lock狀態,等待vnc連線成功,才會繼續開機。

$ vm list
NAME       DATASTORE  LOADER  CPU  MEMORY  VNC                 AUTO  STATE
windows    default    uefi    8    8G      -                   No    Stopped
windows11  default    uefi    8    8G      192.168.10.10:5900  No    Locked

同時也可以把virto-win.iso幾行注釋掉,這樣就會自動開機,不會再等待了

bhyve4

參考:

  1. https://vermaden.wordpress.com/2023/08/18/freebsd-bhyve-virtualization/
  2. https://github.com/churchers/vm-bhyve/wiki/Running-Windows
  3. https://klarasystems.com/articles/from-0-to-bhyve-on-freebsd-13-1/