Running FreeBSD in Azure

NOTE: There is now an official article on how to configure a FreeBSD image for Microsoft Azure! You should probably be reading that one instead.

As many readers of this blog may know, for many years Microsoft has worked with the Linux community and contributed a number of device drivers to the Linux kernel, known as the Linux Integration Services. These drivers allow the Linux system to run well under Hyper-V and in Microsoft Azure. More recently, Microsoft has worked with the FreeBSD community to contribute equivalent drivers to FreeBSD 10. These are known as the BSD Integration Services (BIS) for FreeBSD. This article will provide instructions on how to prepare and upload a FreeBSD 10 image to run in Microsoft Azure. Disclaimer: While this article provides detailed instructions on running FreeBSD in Azure, note that FreeBSD is not currently an endorsed platform in Microsoft Azure and therefore is not supported.

Getting a Base FreeBSD Image

For this article we will focus on preparing a FreeBSD 10 x86_64 image for Microsoft Azure. To get started we first need to install FreeBSD locally on Hyper-V (Windows Server 2012 R2 recommended). To make our lives even easier, the FreeBSD community has provided pre-created FreeBSD images in VHD format.

Decompress the .xz file to extract the FreeBSD-10.0-RELEASE-amd64-*.vhd file. There are many utilities that can help here. I typically use 7-zip.

Create a new virtual machine in Hyper-V and attach the extracted VHD as an IDE disk:

Resize the FreeBSD VHD (If necessary)

If you opted to install FreeBSD yourself onto a Hyper-V VM via the installation ISO then you can skip this section. However, if using the pre-generated VHD from ftp.freebsd.org then you may need to resize the VHD to be compatible with Azure. When creating a VHD for Microsoft Azure the size of the VHD must be a whole number in megabytes, otherwise you will see an error similar to the following when you attempt to create your new FreeBSD image in Azure:

“The VHD http://<mystorageaccount>.blob.core.windows.net/vhds/FreeBSD-10.0-RELEASE-amd64-20140116-r260789.vhd has an unsupported virtual size of 21475270656 bytes. The size must be a whole number (in MBs).”

So in the example above, 21475270656 bytes is actually 20480.4140625 MB. And so to fix this simply resize the VHD to ensure the disk is an even 21000 MB. You can do this easily using either Powershell or the Hyper-V management console.

On the Hyper-V server, open a Powershell command prompt and run the Resize-VHD cmdlet to size up the VHD slightly:

Alternatively, you can also resize the disk from the Hyper-V management console. In the settings menu for your FreeBSD VM, click on the FreeBSD virtual IDE drive and then click “Edit” to open up the VHD editing wizard:

Next, start the FreeBSD system and log in as root. There is no root password by default on the pre-generated community VHDs, so you will not be prompted for a password. After logging in, run gpart and take a look at the disk partitions. You may see that the disk, da0, is marked as CORRUPT because we resized the VHD:

Optionally, you can now try to resize the FreeBSD root partition to reclaim that free space if you want. This is a little more trouble, however, as you may need to boot from a FreeBSD Live CD to resize the root file system. But if you really want that free space back, here is how you expand the root file system:

Note that if you would now like to SSH into the FreeBSD guest to continue the configuration you will need to either create a new unprivileged user, or set a password for the 'root' user and set "PermitRootLogin yes" in /etc/ssh/sshd_config (not recommended).

You may also need to symlink the new python 2.7 binary with /usr/bin/python so we can easily find it:

root@:~# ln -s /usr/local/bin/python2.7 /usr/bin/python

Install sudo We typically disable the root account on Azure and utilize sudo from an unprivileged user to run commands with elevated privileges:

root@:~# pkg install sudo

Install the Azure Linux Agent The latest release of the Azure Linux Agent can always be found on Github here - https://github.com/Azure/WALinuxAgent/releases. Version 2.0.5 or later is recommended for FreeBSD. However, the remote 2.0 branch is typically very stable, so for now we will just pull the latest agent directly from that branch. This will get you the latest agent with support for FreeBSD:

(Optional) Deprovision the system before uploading the VHD to Azure Deprovisioning will attempt to generalize the image. This operation will remove the SSH host keys, disable the root user and remove the password, remove DHCP cache entries, etc. This is typically a good practice, but if you are not planning to share this VHD widely then you may also skip this step:

root@:~# /usr/sbin/waagent –deprovision
WARNING! The waagent service will be stopped.
WARNING! All SSH host key pairs will be deleted.
WARNING! Cached DHCP leases will be deleted.
WARNING! Nameserver configuration in /etc/resolv.conf will be deleted.
WARNING! root password will be disabled. You will not be able to login as root.
Do you want to proceed (y/n)? y

Note: After running this command you may see a warning message because the ovf-env.xml file does not exist. This message can be ignored.

Shut down the FreeBSD virtual machine

All done! Wow, that was easy, right?

Upload the FreeBSD VHD to Azure

There are a few ways to upload the VHD to Azure. In this article we will use the Azure Powershell tools to upload the VHD to our storage account:

First, be sure to shut down the FreeBSD virtual machine on your Hyper-V server

Note that we must use the OS type of “Linux” here. Sadly, the Powershell tools do not currently support the “-OS FreeBSD” parameter :)

Create a FreeBSD Virtual Machine in Azure

Finally, we should now be ready to create new FreeBSD virtual machines in Azure. You may use the Powershell or CLI tools for this as well, or spin up the FreeBSD 10 image from the portal in the typical way: