Tuesday, November 15, 2011

First FreeBSD Jail

I want a jail to build packages while leaving other jails clean. This should result in only one jail getting cluttered with various source files and what-not. The steps below assume ezjail has never been initialized.

Initialize ezjail
In a previous post, I provided a few quick steps to getting a system ready for jails. The next step is to run ezjail-admin for the first time.

$ sudo ezjail-admin install -p

This will have initialized the jail system for the first time and created a copy of the ports tree. The default directory for the install is /usr/jails and will be created if not already present. Easy.

Create First Jail
A jail needs an IP address. If aliases were setup in rc.conf (and rebooted), then things should be good. If not, here is the syntax:

$ sudo ifconfig em0 inet 10.10.10.110 netmask 255.255.255.255 alias

Create and start the first jail.

$ sudo ezjail-admin create builder 10.10.10.110
$ sudo ezjail-admin start builder

Jail Configuration
There is a good chance the jail is somewhat useless at this point, as network settings and other pieces are missing. Time to use the console.

$ sudo ezjail-admin console builder

The above command results in a root account on the named jail. Fix the network, add a user, and install a few packages. The values below should be changed to match your environment.

# echo "nameserver 10.10.10.1" >> /etc/resolv.conf
# pw useradd -n builder -g builder -s /bin/sh -m -d /home/builder -c 'builder account'
# passwd builder
# pkg_add -r sudo
# sudoedit /usr/local/etc/sudoers
# vi /etc/ssh/sshd_config
# echo 'sshd_enable="YES"' >> /etc/rc.conf
# exit

Quick explanation:
  1. Create resolv.conf and add our nameserver/router
  2. Add user "builder" (-n builder)
  3. Set builder's password
  4. Install and then configur sudo
  5. Edit sshd_config so "builder" can log in. Be sure to change ListenAddress (10.10.10.110 for this example)
  6. Enable sshd in rc.conf
Restart the jail and log in.

$ sudo ezjail-admin stop builder
$ sudo ezjail-admin start builder
$ ssh 10.10.10.110

Create Packages
The original goal for this jail was to be able to build packages for other jails. Here is a quick run-down of how.

Logged in as "builder" to 10.10.10.110 jail:

$ cd /usr/ports/benchmarks/bonnie
$ sudo make install
$ cd ~
$ mkdir bonnie
$ cd bonnie
$ pkg_info | grep bonnie
$ pkg_create -b bonnie-2.0.6_1
$ ls
bonnie-2.0.6_1.tbz

Now to copy this out of the jail and into another. This can either be done by file transfer between jails (scp, ftp, etc) or from the host as shown below.

$ sudo cp /usr/jails/builder/home/builder/bonnie/bonnie-2.0.6_1.tbz /usr/jails/jail2/tmp/

And now load it in the other jail.

$ sudo pkg_add /tmp/bonnie-2.0.6_1.tbz

No comments:

Post a Comment