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.
- First browse to ftp://ftp.freebsd.org/pub/FreeBSD/snapshots/VM-IMAGES/10.0-RELEASE/amd64/Latest/ And download the latest *.vhd.xz file, for example: ftp://ftp.freebsd.org/pub/FreeBSD/snapshots/VM-IMAGES/10.0-RELEASE/amd64/Latest/FreeBSD-10.0-RELEASE-amd64-20140116-r260789.vhd.xz
- 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:
PS > Resize-VHD -Path \<PATH>\FreeBSD-10.0-RELEASE-amd64-20140116-r260789.vhd -SizeBytes 21GB
- 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:
root@:~# gpart show 34 41942973 da0 GPT (21G) [CORRUPT] 34 1024 1 freebsd-boot (512K) 1058 2097152 2 freebsd-swap (1.0G) 2098210 39844797 3 freebsd-ufs (19G)
No problem, run the following command to fix up da0:
root@:~# gpart recover da0 da0 recovered root@:~# gpart show => 34 44040125 da0 GPT (21G) 34 1024 1 freebsd-boot (512K) 1058 2097152 2 freebsd-swap (1.0G) 2098210 39844797 3 freebsd-ufs (19G) 41943007 2097152 - free - (1.0G)
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:
root@:~# gpart resize -i 3 da0 da0p3 resized root@:~# gpart show => 34 44040125 da0 GPT (21G) 34 1024 1 freebsd-boot (512K) 1058 2097152 2 freebsd-swap (1.0G) 2098210 41941949 3 freebsd-ufs (20G) root@:~# growfs /dev/da0p3
Hurray! The VHD looks good, we’re now ready to prep our FreeBSD system for Azure.
Prepare the FreeBSD System for Azure
Now the easy part (yes, really). We just need to install some additional components and configure a few things to allow this system to run on Azure.
- Enable networking Configure the hn0 interface so we can use /etc/rc.d/netif to stop and start networking:
root@:~# echo 'ifconfig_hn0="SYNCDHCP"' >> /etc/rc.conf root@:~# service netif restart
- Enable SSH Enable the SSH daemon in /etc/rc.conf and create new host keys:
root@:~# echo 'sshd_enable="YES"' >> /etc/rc.conf root@:~# ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key root@:~# ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key root@:~# service sshd restartNote 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).
- Install Python 2.7 and required modules:
root@:~# pkg install python27 py27-asn1 The package management tool is not yet installed on your system. Do you want to fetch and install it now? [y/N]: y Bootstrapping pkg from pkg+http://pkg.FreeBSD.org/freebsd:10:x86:64/latest, please wait… Verifying signature with trusted certificate pkg.freebsd.org.2013102301… done Installing pkg-1.2.7_2… done If you are upgrading from the old package format, first run: # pkg2ng Updating repository catalogue digests.txz 100% 1071KB 1.1MB/s 1.1MB/s 00:00 packagesite.txz 100% 4929KB 4.8MB/s 4.8MB/s 00:01 Incremental update completed, 22926 packages processed: 0 packages updated, 0 removed and 22926 added. The following 4 packages will be installed: Installing gettext: 0.18.3.1 Installing python27: 2.7.6_4 Installing py27-setuptools27: 2.0.1 Installing py27-asn1: 0.1.4_1,1 The installation will require 81 MB more space 12 MB to be downloaded Proceed with installing packages [y/N]: yYou 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:
root@:~# pkg install wget ...... root@:~# wget https://raw.githubusercontent.com/Azure/WALinuxAgent/2.0/waagent root@:~# mv ./waagent /usr/sbin/ root@:~# chmod 755 /usr/sbin/waagent root@:~# /usr/sbin/waagent -installThe Azure agent is now installed and running!
- (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)? yNote: 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
- Next, use the Add-AzureVhd cmdlet to upload the VHD:
PS C:\> $destination = “http://MYACCOUNT.blob.core.windows.net/vhds/FreeBSD-10.0-RELEASE-amd64-20140116-r260789.vhd” PS C:\> $localfile = “C:\PATH\FreeBSD-10.0-RELEASE-amd64-20140116-r260789.vhd” PS C:\> Add-AzureVhd -Destination $destination -LocalFilePath $localfile
- After the upload has completed you can now use that VHD to create an Image, from which you may provision any number of FreeBSD virtual machines in Azure:
PS C:\> $destination = “http://MYACCOUNT.blob.core.windows.net/vhds/FreeBSD-10.0-RELEASE-amd64-20140116-r260789.vhd” PS C:\> Add-AzureVMImage -ImageName FreeBSD10.0 -MediaLocation $destination -OS LinuxNote 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:
- Log into the Azure management portal (https://manage.windowsazure.com/) and click on “NEW->COMPUTE->VIRTUAL MACHINE->FROM GALLERY”
- Click on “MY IMAGES”
- In the “Choose an Image” menu select “MY IMAGES”
- Select your FreeBSD10 image and go through the prompts to set up a hostname, password/SSH key, etc.