Installing and Running a Gentoo Linux guest in bhyve
This guide will help you install and run a Gentoo Linux guest in bhyve. I assume you have familiarity installing Gentoo, and have read the Handbook section FreeBSD as a Host with bhyve.
Setup
- You must be root.
- Setup bhyve and a network interface as described in the handbook under “Preparing the Host.” I assume
tap0andbridge0for this guide.
- Create a zfs volume to store the Gentoo disk. In the handbook see “Using ZFS with bhyve Guests.” For example, to create a 32 GB volume under the zroot zpool, do this:
zfs create -V32G -o volmode=dev zroot/gentoo
- install grub-bhyve
pkg install grub-bhyve
- create a directory on the host for all of your Gentoo bhyve-specific stuff: I assume
/root/gentoofor this guide.
- download the Gentoo minimal install CD. The file path should be something like
/root/gentoo/install-amd64-minimal-20190129T214502Z.iso
Configuring grub-bhyve
Create /root/gentoo/device.map. Do not use ~ in your paths, it doesn't work correctly with grub-bhyve.
(cd0) /root/gentoo/install-amd64-minimal-20190129T214502Z.iso (hd0) /dev/zvol/zroot/gentoo
Running the Gentoo installer
- now run grub-bhyve. In this example I'm giving the guest 8GB RAM, and calling the vm
gentoo
grub-bhyve -m /root/gentoo/device.map -r cd0 -M 8192M -d /grub gentoo
- select the first boot option in grub or let the timeout expire. If you see a grub prompt and no boot options, something is wrong.
- grub-bhyve will return you to a terminal: that's good.
- now that we've loaded the installer kernel via Grub, run bhyve. For this command I'm giving the guest 6 cores and 8GB RAM, and continuing to call the vm
gentoo
bhyve -AHP -s 0:0,hostbridge -s 1:0,lpc -s 2:0,virtio-net,tap0 -s 3:0,virtio-blk,/dev/zvol/zroot/gentoo -s 4:0,ahci-cd,/root/gentoo/install-amd64-minimal-20190129T214502Z.iso -l com1,stdio -c 6 -m 8192M gentoo
- at this point you should be sitting at a livecd prompt, ready to install Gentoo.
Tips for configuring the Gentoo guest
By default, Gentoo's /etc/inittab has the serial consoles commented out. Make sure you uncomment s0 and s1 or you will not be able to login to your Gentoo guest with bhyve!
- When partitioning the disk, the ZFS disk volume in Linux is
/dev/vda. - Make sure you open your firewall on the host for the guest with something like this:
pass quick on tap0 all pass quick on bridge0 all
- Helpful config options for the kernel:
- Set the config options recommended in the Gentoo handbook first. https://wiki.gentoo.org/wiki/Handbook:AMD64/Installation/Kernel#Activating_required_options
- Add these options for a Virtualized Linux guest: https://wiki.gentoo.org/wiki/QEMU/Linux_guest#Kernel
- If you want the hardware random number generator from your host machine:
Y Device Drivers->Character Devices->Hardware Random Number Generator Core Support->VirtIO Random Number Generator support
- For the clock to be the same on both the host and the guest, do this.
- set your timezone to the same timezone as the host, as described in the Gentoo handbook.
- edit
/etc/conf.d/hwclock:
clock="local" clock_hctosys="YES" clock_systohc="NO"
- The terminal size defaults to 80×25 which is horrible when you're trying to configure kernel options with make menuconfig. Since Linux thinks this is a serial console, we can set the size like this:
stty rows 50 cols 100 dmesg
the dmesg seems to help it expand.
- Some helpful info here: https://github.com/churchers/vm-bhyve/issues/186
Grub2 Install on Gentoo Guest
- we're using BIOS, not UEFI, so edit /etc/portage/make.conf:
GRUB_PLATFORMS="pc efi-64" - install grub2 and make sure it got the options right:
emerge --ask --verbose sys-boot/grub:2 - install grub to the “disk”
grub-install /dev/vda - edit /etc/default/grub:
GRUB_TERMINAL=console GRUB_GFXPAYLOAD_LINUX=text
- run mkconfig:
grub-mkconfig -o /boot/grub.cfg
Running Gentoo guest with bhyve
- exit out of your chroot as described in the Gentoo handbook, and shutdown the installer environment cleanly.
- kill the vm:
bhyvectl --destroy --vm=gentoo - Startup grub-bhyve and point it to the /boot partition on your Gentoo drive. I created my /boot on partition 2 of the disk, so it is
-r hd0,2. I'm giving the vm 8GB of RAM.
grub-bhyve -m /root/gentoo/device.map -M 8192M -r hd0,2 -d /grub gentoo
- Startup bhyve with 6 cores and 8 GB of RAM:
bhyve -AHP -s 0:0,hostbridge -s 1:0,lpc -s 2:0,virtio-net,tap0 -s 3:0,virtio-blk,/dev/zvol/zroot/gentoo -s 4:0,virtio-rnd,/dev/random -l com1,stdio -c 6 -m 8192M gentoo