How to use FreeBSD Jails with ZFS
In this guide we will see how to use FreeBSD Jails using the Z file system. This will allow us to create a skeleton jail (i.e., a base image) that will be copied every time we need to deploy a new service. One of the key advantages of this approach is that each Jail is initially identical to the skeleton, meaning that it consumes virtually no additional space until its data begins to diverge from the template.
Create a ZFS partition
To avoid assuming any prior steps, we will begin by creating a new ZFS partition:
Next, let's enable poll auto mount at boot by adding the following entry to the
/etc/rc.conf file:
Create a new pool
Let's now proceed by creating a new ZFS pool on the /dev/da0s1b partition:
Then, we can create two datasets called jails and jails/template, respectively. The former will store the actual Jails while the latter will provide a skeleton for creating new services.
Create the template Jail
Let's now set up the template Jail. We will begin by installing the base system on it
using bsdinstall(8). When prompted, we will omit the root password.
Now, we can proceed by creating a snapshot of the template Jail
The base template is ready for duplication, let's try it.
Create a new Jail
We are now ready to create a new Jail using the template we have created in the previous step.
Before starting it, let's create a simple configuration file under /etc/jail.conf:
Then, let's add the following two lines to /etc/rc.conf:
Finally, to start the Jail, we can issue the following command:
We can retrieve a list of running jails by running jls:
The Jail is now up and ready to use. As an example, we will try to configure the nginx web server:
Testing
Let's now make sure that the web server is running on port 80. We can verify
that by issuing the following command:
Destroying a Jail
Last but not least, to remove a Jail, shut it down using service jail stop www, destroy its ZFS
dataset using:
Then, remove it from the jail_list variable on /etc/rc.conf.