Install Debian(11.1.0) Linux VM on FreeBSD(13.0) with grub2-bhyve and ZFS without GUI

Preparing the Host

The first step to creating a virtual machine in bhyve is configuring the host system. 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 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
 

Creating a Debian Linux Guest with ZFS


Using ZFS volumes instead of disk image files can provide significant performance benefits for the guest VMs. A ZFS volume can be created by:
 
# zfs create -V88G -o volmode=dev zroot/debiandisk0
 
List ZFS volume disk image files:
% zfs list 

Destroy A ZFS volume disk image file:
#  zfs destroy zroot/debiandisk0

Download an Debian DVD image to install.

The Linux® kernel is loaded with sysutils/grub2-bhyve. Create a device.map that grub will use to map the virtual devices to the files on the host system:
(hd0)  /dev/zvol/zroot/debiandisk0
(cd0)  /path/to/debian-11.1.0-amd64-DVD-1.iso

Install grub2-bhyve:
# pkg install sysutils/grub2-bhyve

Use sysutils/grub2-bhyve to load the Linux® kernel from the ISO image:
# grub-bhyve -m device.map -r cd0 -M 1024M guestdebian

This will start grub. If the installation CD contains a grub.cfg, a menu will be displayed. If not, the vmlinuz and initrd files must be located and loaded manually(GNU GRUB show up press c for command-line):
grub> ls
(hd0) (cd0) (cd0,apple2) (cd0,apple1) (cd0,msdos2) (host)
grub> ls (cd0)/
autorun.inf boot/ css/ debian dists/ doc/ EFI/ firmware/ g2ldr g2ldr.mbr instal
l/ install.amd/ isolinux/ md5sum.txt pics/ pool/ README.html README.source READ
ME.txt README.mirrors.html README.mirrors.txt setup.exe tools/ win32-loader.ini
grub> ls (cd0)/install.amd/
gtk/ initrd.gz install.bat vmlinuz xen/
grub> linux (cd0)/install.amd/vmlinuz
grub> initrd (cd0)/install.amd/initrd.gz
grub> boot

Now that the Linux® kernel is loaded, the guest can be started:
# bhyve -A -H -P -s 0:0,hostbridge -s 1:0,lpc  -s 3:0,virtio-blk,/dev/zvol/zroot/debiandisk0 -s 4:0,ahci-cd,/path/to/debian-11.1.0-amd64-DVD-1.iso -l com1,stdio -c 1 -m 1024M guestdebian

Note: Change the /path/to/debian-11.1.0-amd64-DVD-1.iso to your own path.
 
If you compare my method to https://docs.freebsd.org/en/books/handbook/virtualization/#virtualization-host-bhyve. You will found it I remove the network which is -s 2:0,virtio-net,tap0. And I can put it back after the installing finished.
 
Why? Because debian get packets from the network when installing debian it take 2-3 hours. But if I remove the virtual network device the install process only take several minutes(less than 10 minutes).
 
In the process of installing you can choose no ethernet card on th Detect network hardware as follows:


 As blow we can see the virtual disk name is Virtual disk 1 (vda).
 


On the software selection window,  the only one software I choosed is standard system utilities as  follows:
 

Install the GRUB boot loader as follows:


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=guestdebian 
 
Now the guest can be started directly from the virtual disk. Load the kernel:
# grub-bhyve -m device.map -r hd0,msdos1 -M 1024M guestdebian
 
GNU GRUB windows will show up press c for command-line:
grub> ls
(hd0) (hd0,msdos5) (hd0,msdos1) (cd0) (cd0,apple2) (cd0,apple1) (cd0,msdos2) (h
ost)
grub> ls (hd0,msdos1)/
lost+found/ etc/ media/ vmlinuz.old var/ bin usr/ sbin lib lib32 lib64 libx32 b
oot/ dev/ home/ proc/ root/ run/ sys/ tmp/ mnt/ srv/ opt/ initrd.img.old vmlinu
z initrd.img
grub> linux (hd0,msdos1)/vmlinuz root=/dev/vda1 
grub> initrd (hd0,msdos1)/initrd.img
grub> boot 

root=/dev/vda1  is come from pictures "Partition disks" and "Install the GRUB boot loader".
 
Boot the virtual machine(I add the virtual network device to it):
# 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/debiandisk0 -l com1,stdio -c 1 -m 1024M guestdebian

Linux® will now boot in the virtual machine and eventually present you with the login prompt. Login and use the virtual machine. When you are finished, reboot the virtual machine to exit bhyve. Destroy the virtual machine instance:
# bhyvectl  --destroy  --vm=guestdebian

So the start_debian.sh should be as follows:
#!/bin/sh
grub-bhyve -m device.map -r hd0,msdos1 -M 1024M guestdebian

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/debiandisk0 -l com1,stdio -c 1 -m 1024M guestdebian

bhyvectl --destroy --vm=guestdebian


Persistent Configuration

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

1. /etc/sysctl.conf

net.link.tap.up_on_open=1

2. /etc/rc.conf

cloned_interfaces="bridge0 tap0"
ifconfig_bridge0="addm em0 addm tap0"
kld_list="nmdm vmm"  

Fix the network of Debian Linux

After entering the system, when you ping google.com, the error message is as follows:
ping: google.com: Temporary failure in name resolution
 
What you need to do is set static ip for debian linux and create the file  /etc/resolv.conf which is used to write DNS ip address.
 
Set static ip for debian linux:
# cp /etc/network/interfaces /etc/network/interfaces.bak
 
Use the command below to check the network device on debian linux:
# ip addr
 
In my case it is enp0s2.
So after edited the file /etc/network/interfaces is as follows:
auto lo
iface lo inet loopback
auto enp0s2
iface enp0s2 inet static
address 192.168.1.68
netmask 255.255.255.0
gateway 192.168.1.1
 
 
The content of /etc/resolv.conf is as follows:
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 1.0.0.1

And restart the network:
# /etc/init.d/networking stop
# /etc/init.d/networking start


 

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 Dockerizing LEMP Stack with Docker-Compose

FreeBSD安装SCIM中文输入法(csh/tcsh)