Arch in Bhyve on FreeNAS to Run Plex

It took some doing to get it working, but I think this is about as efficient a setup as I could have had.

I have a server running FreeNAS for backup/storage purposes. FreeNAS has a Plex plugin, but for Plex Pass users, which I am, the FreeBSD operating system on which FreeNAS is build doesn’t support some of the more advanced features. So in order to get all features I want out of Plex, you need to run it on Linux.

I previously had this working in a prior version of FreeNAS (9.10) using the VirtualBox capability they had. But in the latest version of FreeNAS (11.1), they no longer have VirtualBox. Instead they have a hypervisor called Behyve.

The following are the steps I performed in order to get Arch running inside Byhve, and install Plex on it.

There is some GUI functionality for using Behyve to create VMs, but I could not get it to work correctly. The biggest issue was the VNC stuff just didn’t seem to want to work, so I couldn’t connect to the machine for initial setup. But, they have some command line stuff available that I was able to find enough information on to get working. So here goes.

Get iohyve set up

First, log into the FreeNAS web interface and in System -> Tunables, create a couple of entries:

  • iohyve_enable, YES, rc.conf
  • iohyve_flags, kmod=1 net=em0, rc.conf

These make sure that the iohyve settings needed are persisted across reboots.

Next, you’ll want to ssh into your FreeNAS server as root so you can have a more convenient terminal window than the one in the web GUI.

Execute the following command to get iohyve set up, where poolname is the name of the ZFS pool you want your VMs stored on, and adapter is the name of the network adapter you want your VMs to be bridged to. Mine was em0.

iohyve setup pool=poolname kmod=1 net=adapter

Create a New Virtual Machine

Next, you’ll need to download a .iso file to install the guest OS from and put it somewhere on your FreeNAS server. Then execute the following commands so iohyve can store a copy of it, and you can make sure it worked.

iohyve cpiso /path/to/iso_file.iso
iohyve isolist

Then you can create, setup, and boot your VM using the .iso file with the following commands.

iohyve create plexserver 20G
iohyve set plexserver loader=grub-bhyve os=arch ram=8G cpu=4 con=nmdm1
iohyve install plexserver iso_file.iso

Set Up The Virtual Machine

Then, in a different terminal, ssh into the FreeNAS server and execute the following command to get a terminal connected to your new VM.

iohyve console plexserver

I was dropped to a login prompt, but you can login as root and be in the standard Arch install environment. I followed a similar procedure as to the one I used to install Arch on my laptop (see here).

Make sure the VM has internet access and set up ntp with the following commands.

timedatectl set-ntp true
timedatectl status

Then partition and format the disk and mount it for installation. Make a GPT partition table with a 550M EFI boot partition and the remainder taken up by a single Linux partition.

fdisk /dev/sda
mkfs.fat -F32 /dev/sda1
mkfs.ext4 /dev/sda2
mount /dev/sda2 /mnt
mkdir /mnt/boot
mount /dev/sda1 /mnt/boot

Edit the file /etc/pacman.d/mirrotlist and move the closest mirrors to the top of the list. Then install the base system with the necessary packages and chroot into the new system using the following commands.

pacstrap /mnt base base-devel grub efibootmgr nftables sudo zsh vim git elinks wget openssh netctl nfs-utils
genfstab -U /mnt > /mnt/etc/fstab
arch-chroot /mnt

Now we’ll do initial set up of the system.

ln -sf /usr/share/zoneinfo/Region/City /etc/localtime
hwclock --systohc
vim /etc/locale.gen # uncoment the line en_US.UTF-8 UTF-8
echo "LANG=en_US.UTF-8" > /etc/locale.conf
echo "yourcomputername" > /etc/hostname

Add the following to /etc/hosts. localhost
::1 localhost yourcomputername.localdomain yourcomputername

Now get the bootloader set up with the following commands.

mkinitcpio -p linux
grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=GRUB
grub-mkconfig -o /boot/grub/grub.cfg

Get accounts set up.

passwd # give root a password
visudo -f /etc/sudoers # and uncomment the sudo group
groupadd sudo
useradd -m -G sudo,wheel -s /usr/bin/zsh username # create your user
passwd username # give your user a password

Now enable the services we need.

systemctl enable nftables
systemctl enable sshd
systemctl enable dhcpcd
systemctl enable netctl

Now that the initial set up is down we can shut the machine down.

umount -R /mnt
shutdown now

For whatever reason, Behyve wouldn’t seem to boot the system correctly without doing following. Do these while ssh‘d into the FreeNAS server. First, set the os of the VM to custom with iohyve set plexserver os=custom and then modify the file /iohyve/plexserver/grub.cfg to contain the following.

linux (hd0,gpt1)/vmlinuz-linux root=/dev/sda2
initrd (hd0,gpt1)/initramfs-linux.img

Then you can make a couple other adjustments and restart the VM.

iohyve set plexserver boot=1 # to have the VM start up when FreeNAS boots
iohyve set plexserver description="Plex Server"
iohyve start plexserver

Now you should be able to ssh into the VM directly and not have to go through FreeNAS first.

Get Plex Running

You’ll need to create NFS shares for your media collection folders using the FreeNAS GUI. Then create mount points for them in the VM by adding entries in the file /etc/fstab that are similar to the following, where the first is the IP address of your FreeNAS server.

ipaddress:/mnt/poolname/Media/Music /mnt/Media/Music nfs defaults,rsize=32768,wsize=32768,_netdev 0 0

Then run the following commands in the VM to download and install Plex Media Server.

tar -xf cower.tar.gz
cd cower
makepkg -s
sudo pacman -U cower*.pgk.tar.xz
cd ../
rm -rf cower
rm cower.tar.gz
cower -d plex-media-server-plexpass
cd plex-media-server-plexpass
makepkg -s
sudo pacman -U plex*.pkg.tar.xz
cd ../
rm -rf plex-media-server-plexpass

Then you need to open the appropriate ports in the firewall by adding the following lines to the file /etc/nftables.conf.

tcp dport { 32400, 32469 } ct state new accept
udp dport { 1900, 5353, 32410, 32412, 32413, 32414 } ct state new accept

Then enable and (re)start the services.

systemctl restart nftables
systemctl enable plexmediaserver
systemctl start plexmediaserver

Then you should be able to point your browser at <ip-address>:32400 in order to configure Plex.

Note for Encrypted Volumes

Since I use an encrypted volume for my storage on FreeNAS, I needed to fix a softlink so iohyve would work after a reboot. Point the softlink /iohyve directly to the location in your ZFS pool where iohyve stored its data. Probably /mnt/<poolname>/iohyve. Originally it pointed to /mnt/iohyve, but this wasn’t automatically recreated when I unlocked the volume after reboot.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s