How to install Linux(Ubuntu Server 20.04) VM on FreeBSD 13.0 using bhyve and ZFS

1. Preparing the Host

First, load the bhyve kernel module:
# kldload vmm

Then, create a tap interface for the network device in the virtual machine to attach to. In order to for the network device to participate in the network, also create a bridge interface containing the tap interface and the physical interface as members. In this example, the physical interface is em0:
# ifconfig tap0 create
# sysctl net.link.tap.up_on_open=1
net.link.tap.up_on_open: 0 -> 1
# ifconfig bridge0 create
# ifconfig bridge0 addm em0 addm tap0
# ifconfig bridge0 up

2. Preparing for the Installation


In order to boot Linux systems, grub2-bhyve must be first installed as follows:
# pkg install grub2-bhyve   

3. Creating a Linux Guest

A ZFS volume can be created by:
# zfs create -V16G -o volmode=dev zroot/linuxdisk0

Create a device.map that grub will use to map the virtual device to the files on the host system:
(hd0)  /dev/zvol/zroot/linuxdisk0
(cd0)  /path/to/ubuntu-20.04.3-live-server-amd64.iso 

Use grub2-bhyve to load the Linux kernel from the ISO image:
# grub-bhyve -m device.map -r cd0 -M 1024M linuxguest 
 
This will start grub. 
Just press Enter. It won't print anything, and done.
 
Now that the Linux kernel is loaded, the guest can be started(change the path to ubuntu server ISO to your own path):
# bhyve -A -H -P -s 0:0,hostbridge -s 1:0,lpc -s 2:0,virtio-net,tap0 -s 3:0,virtio-blk,/dev/zvol/zroot/linuxdisk0 -s 4:0,ahci-cd,/path/to/ubuntu-20.04.3-live-server-amd64.iso -l com1,stdio -c 1 -m 1024M linuxguest
 

The system will boot and start the installer. After installing a system in the virtual machine, reboot the virtual machine. This will cause bhyve to exit. The instance of the virtual machine needs to be destroyed before it can be started again:
# bhyvectl --destroy --vm=linuxguest
 
Now the guest can be started directly from the virtual disk. Load the kernel:
# grub-bhyve -m device.map -r hd0 -M 1024M linuxguest
 
Located and loaded vmlinuz and initrd.img as show below.
 

 
grub> linux (hd0,gpt2)/vmlinuz-5.4.0-81-generic root=/dev/mapper/ubuntu--vg-ubu
ntu--lv
grub> initrd (hd0,gpt2)/initrd.img-5.4.0-81-generic
grub> boot
 
OR 
grub> linux (hd0,gpt2)/vmlinuz root=/dev/mapper/ubuntu--vg-ubuntu--lv 
grub> initrd (hd0,gpt2)/initrd.img
grub> boot

4. Automate manual processes

Above, I manually booted the Linux system, but blow I automate it.
 
Create the startlinux.sh and grub_hd.in file in the directory where device.map is located.
 
startlinux.sh is as follows:
#!/bin/sh
grub-bhyve -m device.map -r hd0,gpt2 -M 1024M linuxguest -d /grub

bhyve -A -H -P -s 0:0,hostbridge -s 1:0,lpc -s 2:0,virtio-net,tap0 -s 3:0,virtio-blk,/dev/zvol/zroot/linuxdisk0 -l com1,stdio -c 1 -m 1024M linuxguest 

bhyvectl --destroy --vm=linuxguest

 
# sh startlinux.sh
 
The option of grub-bhyve:

5.  Virtual Machine Consoles

It is advantageous to wrap the bhyve console in a session management tool such as sysutils/tmux or sysutils/screen in order to detach and reattach to the console.
# pkg install sysutils/tmux
# kldload nmdm
 
Change the stdio to /dev/nmdm0A in startlinux.sh as follow:
bhyve -A -H -P -s 0:0,hostbridge -s 1:0,lpc -s 2:0,virtio-net,tap0 -s 3:0,virtio-blk,/dev/zvol/zroot/linuxdisk0 -l com1,/dev/nmdm0A -c 1 -m 1024M linuxguest 

The /dev/nmdm devices are created automatically as needed, where each is a pair, corresponding to the two ends of the null modem cable (/dev/nmdm0A and /dev/nmdm0B). We must use nmdm0B connect to nmdm0A as following:
# cu -l /dev/nmdm0B

6. Persistent Configuration

In order to configure the system to start bhyve guest at boot time, the following configurations must be made in the specified files:

/etc/sysctl.conf
net.link.tap.up_on_open=1
 
/etc/rc.conf
cloned_interfaces="bridge0 tap0"
ifconfig_bridge0="addm em0 addm tap0"
kld_list="nmdm vmm"

 

Comments

Popular posts from this blog

BdsDex: failed to load Boot0001 "UEFI BHYVE SATA DISK BHYVE-OABE-20A5-E582" from PciRoot(0x0)/Pci (0x2, 0x0)/Stat(0x0,0xFFFF,0x0) : Not Found

How To Install Nginx, MySQL and PHP (FEMP) Stack on FreeBSD 13.0

Install samba on FreeBSD(on VMware Workstation) to share files with Window.