Skip to content

Instantly share code, notes, and snippets.

@bretton
Last active June 9, 2023 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bretton/22e51652f0fb747b120ae7d3daa5f1fc to your computer and use it in GitHub Desktop.
Save bretton/22e51652f0fb747b120ae7d3daa5f1fc to your computer and use it in GitHub Desktop.
Optimal Virtualbox on FreeBSD, running FreeBSD virtual machines

Optimal Virtualbox on FreeBSD, running FreeBSD images

For best results:

  • Use only a single core per VM (cpus setting). This is critical, more causes problems.
  • Set the sysctl.conf settings below

Configure sysctl.conf and reboot

Add to /etc/sysctl.conf and reboot

# VIRTUALBOX aio(4) SETTINGS
# https://vermaden.wordpress.com/2022/04/
vfs.aio.max_aio_queue_per_proc=65536
vfs.aio.max_aio_per_proc=8192
vfs.aio.max_aio_queue=65536
vfs.aio.max_buf_aio=8192

then reboot.

Install and configure Virtualbox

Install and configure Virtualbox as follows:

pkg install virtualbox-ose
service vboxnet enable
    
(sudo) pw groupmod vboxusers -m <username>

mkdir -p /usr/local/etc/vbox
vi /usr/local/etc/vbox/networks.conf

(add, with asterisk; this is extremely broad)

* 0.0.0.0/0

Symlink to expected file (may be fixed in future)

mkdir -p /etc/vbox
ln -s /usr/local/etc/vbox/networks.conf /etc/vbox/networks.conf

Add to /etc/rc.conf because the host needs to be a router for private networks in Virtualbox to get internet access

gateway_enable="YES"

Restart networking (may pause ssh session for a bit)

sudo service netif restart && sudo service routing restart

Start virtualbox networking

service vboxnet start

Make directories and download install media

Note: these directories could be ZFS datasets!

mkdir /iso
mkdir /vms

Download install media

fetch https://download.freebsd.org/releases/amd64/amd64/ISO-IMAGES/13.1/FreeBSD-13.1-RELEASE-amd64-disc1.iso -o /iso/FreeBSD-13.1-RELEASE-amd64-disc1.iso

Setup virtualbox parameters

VBoxManage createvm --ostype FreeBSD_64 --register --basefolder /vms --name "servername"
VBoxManage modifyvm "servername" --memory 8192 --ioapic on --cpus 1 --chipset ich9 --nic1 bridged --nictype1 virtio --bridgeadapter1 re0
VBoxManage createhd --size 40000 --filename "/vms/servername/servername.vdi"
VBoxManage storagectl "servername" --name "SATA Controller" --add sata --portcount 4 --bootable on
VBoxManage storagectl "servername" --name "SATA Controller" --hostiocache on

Attach storage and installation media

VBoxManage storageattach "servername" --storagectl "SATA Controller" --port 1 --type hdd --medium "/vms/servername/servername.vdi"
VBoxManage storageattach "servername" --storagectl "SATA Controller" --port 2 --type dvddrive --medium "/iso/FreeBSD-13.1-RELEASE-amd64-disc1.iso"

Set VNC password

VBoxManage modifyvm "servername" --vrdeproperty VNCPassword=password

Start VM in headless mode, listening for VNC on port 5001

VBoxHeadless --startvm "servername" --vrde on --vrdeproperty TCP/Ports=5001

Connect to VNC session

Using a VNC client, connect to hostip:5001 and login with password.

Proceed to follow the steps in the BSD installer.

FreeBSD Installation steps

During user creation, the prompt to invite user to additional groups, set wheel here

Before reboot, exit to shell when prompted, then download your SSH public keys to:

  • /root/.ssh/authorized_keys
  • /home/username/.ssh/authorized_keys and chown to your user, set 600 perms.

For example

mkdir .ssh
cd .ssh
fetch http://your.host.name/keys.txt -o authorized_keys
chown youruser authorized_keys
chmod 600 authorized_keys
cd ..
chmod 700 .ssh

Exit to reboot, important: remove CDROM to prevent boot loop

During reboot, watch for bios level boot screen in VNC, and at that moment run the following in another terminal session

VBoxManage storageattach servername --storagectl "SATA Controller" --port 2 --medium emptydrive
@bretton
Copy link
Author

bretton commented May 27, 2023

If using Virtualbox on a Mac, and creating FreeBSD virtual machines, make sure you set a large enough disk size during setup, such as 50GB or 60GB, and pre-allocate the disk space.

If too small, and using allocate on demand, you might end up in a situation where the system always sees insufficient disk space once the initial amount has been exceeded once.

@bretton
Copy link
Author

bretton commented Jun 9, 2023

Virtualbox is broken on FreeBSD-13.2, re-install virtualbox from ports is only option to get it working again

pkg delete -f virtualbox-ose virtualbox-ose-kmod

fetch -o /tmp ftp://ftp.freebsd.org/pub/`uname -s`/releases/`uname -m`/`uname -r | cut -d'-' -f1,2`/src.txz
tar -C / -xvf /tmp/src.txz

portsnap fetch update
portsnap extract
portsnap update

cd /usr/ports/emulators/virtualbox-ose

make config
make install clean

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment