<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>phaq &#187; jails</title>
	<atom:link href="http://phaq.phunsites.net/category/howtos/virtualization/jails/feed/" rel="self" type="application/rss+xml" />
	<link>http://phaq.phunsites.net</link>
	<description>my daily IT madness</description>
	<lastBuildDate>Sun, 05 Feb 2012 21:06:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.3</generator>
		<item>
		<title>Use Sparse Files for FreeBSD jails?</title>
		<link>http://phaq.phunsites.net/2010/12/09/use-sparse-files-for-freebsd-jails/</link>
		<comments>http://phaq.phunsites.net/2010/12/09/use-sparse-files-for-freebsd-jails/#comments</comments>
		<pubDate>Thu, 09 Dec 2010 15:58:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[FreeBSD]]></category>
		<category><![CDATA[Operating Systems]]></category>
		<category><![CDATA[Virtualization]]></category>
		<category><![CDATA[jails]]></category>

		<guid isPermaLink="false">http://phaq.phunsites.net/?p=110</guid>
		<description><![CDATA[Thinking about FreeBSD jails and an elder post of mine about putting jails within loopback-mounted disk images to enforce disk quota, I asked myself if I should use sparse files or pre-allocated files as virtual disk image for jail-based userland separation. A sparse file designates a special file of a given size (e.g. 20 gigs), [...]]]></description>
			<content:encoded><![CDATA[<p>Thinking about FreeBSD jails and an <a href="http://phaq.phunsites.net/2006/06/22/implementing-filesystem-allocation-limits-on-freebsd-jails/">elder post of mine</a> about putting jails within loopback-mounted disk images to enforce disk quota, I asked myself if I should use sparse files or pre-allocated files as virtual disk image for jail-based userland separation.<br />
<span id="more-110"></span><br />
A sparse file designates a special file of a given size (e.g. 20 gigs), which is neither using nor reserving the whole disk space at once. It won&#8217;t effectively allocate disk blocks until data is written to the sparse file.</p>
<p>This comes in handy in creating disk images, which can be a very time consuming task.</p>
<p>Let&#8217;s look at the numbers of creating a 20gig file, which fills up available disk space at once but takes about five minutes to do so. Not to forget about the I/O load the task produces, which affects performance.</p>
<pre>
[root@localhost vz]# time dd if=/dev/zero of=test1.img bs=1024k count=20480
20480+0 records in
20480+0 records out
21474836480 bytes (21 GB) copied, 321.902 seconds, 66.7 MB/s

real    5m21.903s
user    0m0.021s
sys     0m55.282s
</pre>
<p>Creating a 20gig sparse file is done almost immediately, with the difference that disk blocks are not allocated right away.</p>
<pre>
[root@localhost ~]# dd of=test2.img bs=1024k count=0 seek=20480
0+0 records in
0+0 records out
0 bytes (0 B) copied, 1.4744e-05 seconds, 0.0 kB/s

real    0m0.002s
user    0m0.000s
sys     0m0.002s
</pre>
<p>Some possible drawbacks when using sparse files:</p>
<ul>
<li>Peformance degredation when having multiple sparse files to which data is written randomly. This may end up in heavy fragmentation as the sparse files are likely not to be written contiguously in that case, which in term causes slow read access performance</li>
<li>Race conditions when creating many sparse files which would exceed the available disk space (e.g. 20 sparse files of 20 gigs each, but the disk is only 200 gigs in size). To handle this, a special monitoring script would be needed to consider logical vs. physical space allocation.</li>
</ul>
<p>Up until today, I always used pre-allocated disk images, but at the expense of non-usable disk space.<br />
I think that sparse files might be a good choice actually, at least as long as there&#8217;s not much random data written to it, so the payload stays mostly identical over longer periods of time.<br />
For sure, a sparse file used as image for a jail-based database server is definitely a bad idea.<br />
Nevertheless, I&#8217;m keen to try and see, if this might be a considered options for some real-live scenarios.</p>
]]></content:encoded>
			<wfw:commentRss>http://phaq.phunsites.net/2010/12/09/use-sparse-files-for-freebsd-jails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Debian GNU/kFreeBSD inside native FreeBSD jail</title>
		<link>http://phaq.phunsites.net/2007/01/06/debian-gnukfreebsd-inside-native-freebsd-jail/</link>
		<comments>http://phaq.phunsites.net/2007/01/06/debian-gnukfreebsd-inside-native-freebsd-jail/#comments</comments>
		<pubDate>Sat, 06 Jan 2007 19:10:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Debian GNU/Linux]]></category>
		<category><![CDATA[FreeBSD]]></category>
		<category><![CDATA[jails]]></category>

		<guid isPermaLink="false">http://www.phunsites.net/wp/2007/01/06/debian-gnukfreebsd-inside-native-freebsd-jail/</guid>
		<description><![CDATA[It has been some time now since development on Debian GNU/kFreeBSD started, which aims at bringing together the FreeBSD kernel with a GNU userland. There exists a similar implementation called Gentoo GNU/kFreeBSD, although I had no time yet to review it. The Debian developers made some notable progress since last year, but there are still [...]]]></description>
			<content:encoded><![CDATA[<p>It has been some time now since development on <a target="_blank" href="http://www.debian.org/ports/kfreebsd-gnu/">Debian GNU/kFreeBSD</a> started, which aims at bringing together the FreeBSD kernel with a GNU userland.</p>
<p>There exists a similar implementation called Gentoo GNU/kFreeBSD, although I had no time yet to review it.<br />
<span id="more-70"></span><br />
The Debian developers made some notable progress since last year, but there are still lots of issues. Check out the <a target="_blank" href="http://wiki.debian.org/Debian_GNU/kFreeBSD">project website</a> for further details.</p>
<p>Now let&#8217;s look at an abscure idea, that struck me when I first looked at Debian GNU/kFreeBSD last spring.</p>
<p>Why not try to run Debian GNU/kFreeBSD inside a native FreeBSD jail (or inside a Debian GNU/kFreeBSD jail)?</p>
<p>One might argue where&#8217;s the point in doing so. But under that premise I could also ask, what&#8217;s the point then in having hundreds of Linux distros which basically do the same thing &#8212; only in a different way?</p>
<p>Let&#8217;s imagine the possibilities:</p>
<ul>
<li>Run native GNU/kFreeBSD hosts with both GNU/kFreeBSD and native FreeBSD userland jails</li>
<li>Run native FreeBSD hosts with both GNU/kFreeBSD and native FreeBSD userland jails</li>
</ul>
<p>If you&#8217;re familiar with the concepts of OpenVZ/Virtuozzo or the Linux VServer project, you&#8217;ll see some similarities and maybe even the reason for trying out this weird idea.</p>
<p>I must admit, that the current implementation is far away from production grade. There&#8217;s a lot of dirty handwork involved. Aparently there is no point in trying to streamline the process right now, since GNU/kFreebSD is still under heavy development. Automated setups should be easy to implement as soon as the Debian Installer is ported and package support is fixed in debootstrap.</p>
<p>Are you ready to read on? Ok, let&#8217;s go to work then&#8230;</p>
<p><!--more--></p>
<p>Let&#8217;s have a look at how to setup at installing Debian GNU/kFreeBSD inside a native FreeBSD jail.</p>
<p>First of all, get the Debian GNU/kFreeBSD netinst cd. You&#8217;ll find it at http://glibc-bsd.alioth.debian.org/install-cd</p>
<p># wget http://glibc-bsd.alioth.debian.org/install-cd/kfreebsd-i386/20061213/debian-20061213-kfreebsd-i386-install.iso</p>
<p>Now attach the iso image to a vnode and mount it.</p>
<p>#mdconfig -a -t vnode -f debian-20061213-kfreebsd-i386-install.iso<br />
md0<br />
#mkdir /mnt/debinst<br />
#mount_cd9660 /dev/md0 /mnt/debinst</p>
<p>Prepare your new root directory for the jail and untar the base dist. The base dist will  take up about 120 MB on disk.</p>
<p>#mkdir /var/jails/debian_jail<br />
#tar -xzpvf /mnt/debinst/base/base.tgz -C /var/jails/debian_jail</p>
<p>Edit the installer file at /var/jails/debian_jail/native-install and comment out the &#8216;set -e&#8217;  directive on line 31. The script will throw lot&#8217;s of errors when running inside the jail. If you leave the &#8216;set -e&#8217; directive as is, the script will abort and you will need to start over.</p>
<p>You&#8217;re ready now to start the jail. Please do it manually as shown, you will need to fiddle around with it.<br />
You can add it to /etc/rc.conf for regular startup later.</p>
<p>#ifconfig fxp0 inet alias 192.168.0.13/32<br />
#mount_procfs procfs /var/jails/debian_jail/proc<br />
#jail /var/jails/debian_jail debian_jail 192.168.0.13 /bin/sh</p>
<p>You are now inside the jail. Run the installer script from the root directory.<br />
The installer will ask you for time zone settings, set them as required.<br />
Answer to the popularity contest question to your own preferences.</p>
<p>#/native-install</p>
<p>The script will install and configure system packages. This will take a while.<br />
Don&#8217;t worry about errors and warnings, they&#8217;re ok for now.</p>
<p>After the script has finished it is time for some further work. Don&#8217;t leave the jail yet!</p>
<p>First create a valid /etc/resolv.conf. Then make sure your /etc/fstab is empty to prevent mount errors.</p>
<p>#echo&gt;/etc/fstab</p>
<p>And don&#8217;t forget to set the root password.</p>
<p>#passwd</p>
<p>Make sure init won&#8217;t tamper with /dev.</p>
<p>#update-rc.d -f makedev remove</p>
<p>Replace /etc/init.d/freebsd-utils by a dummy file as shown below and reconfigure the freebsd-utils package.</p>
<p>#echo exit 0&gt;/etc/init.d/freebsd-utils<br />
#dpkg-reconfigure freebsd-utils</p>
<p>Now you must leave the jail for a minute. &#8216;exit&#8217; should return you to your host.<br />
Back on the host you need to attach devfs to your jail.</p>
<p>#mount_devfs devfs /var/jails/debian_jail/dev</p>
<p>Then re-enter the jail:</p>
<p>#jail /var/jails/debian_jail debian_jail 192.168.0.13 /bin/sh</p>
<p>It&#8217;s time to import the gpg keys into apt and refresh your package list.</p>
<p>#gpg &#8211;keyserver subkeys.pgp.net &#8211;recv CD02E583 &amp;&amp; gpg &#8211;export CD02E583 | apt-key add -<br />
#apt-get update</p>
<p>As ssh is not part of the base system, I&#8217;d recommend to install it now.</p>
<p>#apt-get install ssh</p>
<p>Maybe you&#8217;d like to have aptitude as well? Feel free to install whatever you like.</p>
<p>#apt-get install aptitude</p>
<p>Depending on your software choice your jail will now use around 220 MB or more on disk.<br />
Leave the jail by entering &#8216;exit&#8217; on the prompt.<br />
Then try to start the jail as shown below.</p>
<p>#jail /var/jails/debian_jail debian_jail 192.168.0.13 /etc/init.d/rc 2<br />
Not starting internet superserver: no services enabled.<br />
Starting OpenBSD Secure Shell server: sshd.</p>
<p>Check if your jail shows up and your processes are running.</p>
<p>#jls<br />
JID  IP Address      Hostname                      Path<br />
71  192.168.0.13    debian_jail                   /var/jails/debian_jail</p>
<p>#pgrep -lfj 71<br />
95086 /usr/sbin/sshd</p>
<p>Your jail should now be accessible through SSH.</p>
<p>#ssh root@192.168.0.13<br />
The authenticity of host &#8217;192.168.0.13 (192.168.0.13)&#8217; can&#8217;t be established.<br />
DSA key fingerprint is c5:02:ad:c6:a8:43:30:25:4a:d0:bd:71:ca:cc:f0:25.<br />
Are you sure you want to continue connecting (yes/no)? yes<br />
Warning: Permanently added &#8217;192.168.0.13&#8242; (DSA) to the list of known hosts.<br />
root@192.168.0.13&#8242;s password:</p>
<p>The programs included with the Debian GNU/kFreeBSD system are free software;<br />
the exact distribution terms for each program are described in the<br />
individual files in /usr/share/doc/*/copyright.</p>
<p>Debian GNU/kFreeBSD comes with ABSOLUTELY NO WARRANTY, to the extent<br />
permitted by applicable law.<br />
debian_jail:~#</p>
<p>So far, so good. Your jail Debian GNU/kFreebSD is now up and running, sort of&#8230;</p>
<p>There&#8217;s a caveat that you may already have noticed. init(8) is not yet working as supposed to, so there&#8217;s actually no parent init process as it should. Neither is there proper resource or runlevel control. This is definitely an issue which would require further investigation.</p>
<p>Furthermore when playing around with it you&#8217;ll notice that there are other things not working properly, like process information tools (ps, top, etc), a multitude of errors in init scripts, most of them being access errors due to the jail enviroment, erratic behaviour on package configuration when it comes to accessing device nodes, and many, many more.</p>
<p>However one must consider Debian GNU/kFreeBSD still being under development on one hand, and it was never meant to be actually run inside a jail on the other.</p>
<p>It would be nice if it became a standing feature in the future, maybe even for sidekicks like Gentoo GNU/kFreeBSD and other distros out there.</p>
<p>Another task would be checking the jail functionality vice-versa to see, how a native FreeBSD userland can be run inside a jail on a Debian GNU/kFreeBSD host.<br />
As to my understanding of the existing implementation that shouldn&#8217;t be too tricky as FreeBSD userland is already fully jail-aware.</p>
<p>I&#8217;m looking forward to trying this on my own as soon as the jail utilities become available on Debian GNU/kFreeBSD one day.</p>
]]></content:encoded>
			<wfw:commentRss>http://phaq.phunsites.net/2007/01/06/debian-gnukfreebsd-inside-native-freebsd-jail/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>FreeBSD patch: enable fsck in mdmfs</title>
		<link>http://phaq.phunsites.net/2006/07/26/freebsd-patch-enable-fsck-in-mdmfs/</link>
		<comments>http://phaq.phunsites.net/2006/07/26/freebsd-patch-enable-fsck-in-mdmfs/#comments</comments>
		<pubDate>Wed, 26 Jul 2006 06:01:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[jails]]></category>

		<guid isPermaLink="false">http://www.phunsites.net/wp/2006/07/26/freebsd-patch-enable-fsck-in-mdmfs/</guid>
		<description><![CDATA[This patch is actually a follow-up development to my article on implementing filesystem allocation limits on FreeBSD jails. My previous article basically lined out how to place jails inside a vnode-backed memory device to enforce filesystem allocation limits. This became possible through a new flag introduced to mdmfs in FreeBSD-CURRENT which allowed to skip 'newfs' [...]]]></description>
			<content:encoded><![CDATA[<p>This patch is actually a follow-up development to my article on <a href="/wp/2006/06/22/implementing-filesystem-allocation-limits-on-freebsd-jails/">implementing filesystem allocation limits on FreeBSD jails</a>.<br />
<code><br />
My previous article basically lined out how to place jails inside a vnode-backed memory device to enforce filesystem allocation limits. This became possible through a new flag introduced to mdmfs in FreeBSD-CURRENT which allowed to skip 'newfs' (which requires mdmfs actually to be called mount_md to work properly).</p>
<p>This solution is quiet handy as it will automagically mount the container volumes as required. But since we do not live in a perfect world - and computers ain't perfect either - crashes do happen. File system corruption on volumes will prevent jails to startup as their (virtual) root device will fail to mount.<br />
This is where this patch comes in. It will enable mdmfs to optionally run an fsck on given volumes.</p>
<p>To apply the patch, create a new temporary build directory first:</p>
<p>#mkdir /root/mount_md<br />
#cd /root/mount_md</p>
<p>Then get the original source code from CVS. Maybe itâ€™s easiest to get this particular release through WebCSV at <a href="http://www.freebsd.org/cgi/cvsweb.cgi/%7Echeckout%7E/src/sbin/mdmfs/mdmfs.c?rev=1.27">http://www.freebsd.org/cgi/cvsweb.cgi/~checkout~/src/sbin/mdmfs/mdmfs.c?rev=1.27</a>.</p>
<p>Save this file to your previously created build directory.</p>
<p>If you have wget at hands you can also download it directly.</p>
<p>#wget -user-agent='Mozilla/5.0' 'http://www.freebsd.org/cgi/cvsweb.cgi/~checkout~/src/sbin/mdmfs/mdmfs.c?rev=1.27' -output-document=/root/mount_md/mount_md.c</p>
<p>Download and apply the patch: <a href="http://phaq.phunsites.net/files/2006/07/mount_md.c.diff_.txt" title="mount_md.c.diff">mount_md.c.diff</a></p>
<p>#wget -user-agent='Mozilla/5.0' 'http://www.phunsites.net/wp/wp-content/uploads/2006/07/mount_md.c.diff.txt' -output-document=/root/mount_md/mount_md.c.diff</p>
<p>#patch &lt; /root/mount_md/mount_md.c.diff</p>
<p>Compile the source:</p>
<p>#gcc /root/mount_md/mount_md.c -o /root/mount_md/mount_md</p>
<p>Then copy the file to some location you like, eg. /usr/sbin.</p>
<p>#cp /root/mount_md/mount_md /usr/sbin/mount_md<br />
Make sure you call the file as given in the example. It wonâ€™t work otherwise.</p>
<p>Special care must be taken that you DO NOT replace your existing mdmfs binary file by this new version. DO NOT overwrite it. Do not rename this patch to mdmfs. Use the names provided in this example instead.</p>
<p><strong>Setup Jailâ€™s fstab</strong></p>
<p>Now check out your jail's fstab and look for this line:</p>
<p>md /var/jails/192.168.0.1 md rw,-P,-F/mnt/r5_vol1/jails/192.168.0.1/rootfs.volume</p>
<p>Change it as follows:</p>
<p>md /var/jails/192.168.0.1 md rw,-P,-F/mnt/r5_vol1/jails/192.168.0.1/rootfs.volume,-Tufs</p>
<p>or</p>
<p>md /var/jails/192.168.0.1 md rw,-P,-F/mnt/r5_vol1/jails/192.168.0.1/rootfs.volume,-tufs</p>
<p>The difference in the two lies in the '-T' or '-t' argument. Both enable fsck before mounting the volume, hence '-T' runs 'fsck -y' while '-t' does not.</p>
<p>You need also to provide the filesystem type with either option for fsck to work properly.</p>
]]></content:encoded>
			<wfw:commentRss>http://phaq.phunsites.net/2006/07/26/freebsd-patch-enable-fsck-in-mdmfs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Streamline Userland Installation On FreeBSD Jails Using Sysinstall</title>
		<link>http://phaq.phunsites.net/2006/06/25/streamline-userland-installation-on-freebsd-jails-using-sysinstall/</link>
		<comments>http://phaq.phunsites.net/2006/06/25/streamline-userland-installation-on-freebsd-jails-using-sysinstall/#comments</comments>
		<pubDate>Sun, 25 Jun 2006 13:53:34 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[jails]]></category>

		<guid isPermaLink="false">http://phaq.phunsites.net/2006/06/25/streamline-userland-installation-on-freebsd-jails-using-sysinstall/</guid>
		<description><![CDATA[There is probably a dozen of ways to install the userland for use within a FreeBSD jail. Most of the documentation I saw (including the man page itself) refers to building/installing from the source tree. Sysinstall seems only to be considered for additional post-configuration tasks and is always run inside of the jail. While building/installing [...]]]></description>
			<content:encoded><![CDATA[<p>There is probably a dozen of ways to install the userland for use within a FreeBSD jail.</p>
<p>Most of the documentation I saw (including the man page itself) refers to building/installing from the source tree. Sysinstall seems only to be considered for additional post-configuration tasks and is always run inside of the jail.</p>
<p>While building/installing a jail may have it&#8217;s advantages in respect to security flaws or bugs in general, installing through sysinstall may be faster and probably less error prone.<br />
<span id="more-40"></span><br />
It is in fact very easy to run sysinstall in non-interactive mode on the host itself (not inside the jail!) from a shell script, which may optionally run some post-installation tasks afterwards.<br />
By the fact that sysinstall can use the same command line arguments as if given through a configuration file, a single line could be used in your script to achieve this:</p>
<p># sysinstall nonInteractive=yes \<br />
_ftpPath=ftp://ftp2.ch.freebsd.org/pub/FreeBSD \<br />
mediaSetFTP distSetMinimum installRoot=/var/jails/192.168.0.1 \<br />
releaseName=6.1-RELEASE installCommit</p>
<p>This line would cause sysinstall to run in non-interactive mode, useing ftp://ftp2.ch.freebsd.org/pub/FreeBSD as it&#8217;s download source. DistSetMinimum selects the smallest userland possible while installRoot is given to define destination directory.</p>
<p>releaseName ist required to choose the actual release and must be set to your main release (e.g. 6.0-RELEASE, 6.1-RELEASE, etc). If you don&#8217;t set it, sysinstall will use the release name of your currently installed userland (e.g. 6.1-RELEASE-p1) and will most definitly fail because there exists no such distribution set for download.<br />
It is also possible to choose a different release like 5.5-RELEASE to be installed inside a jail. You must consider though that most tools will run expect those that interact with the kernel directly (ps, top, etc). In generall I would not recommend mixing releases expect you have a very good reason to do so.</p>
<p>Don&#8217;t forget to add the installCommit command at the end otherwise sysinstall won&#8217;t do anything at all.</p>
<p>By examing the sysinstall man page you will find other options to select different (or build customer) dist sets.</p>
]]></content:encoded>
			<wfw:commentRss>http://phaq.phunsites.net/2006/06/25/streamline-userland-installation-on-freebsd-jails-using-sysinstall/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Implementing Filesystem Allocation Limits On FreeBSD Jails</title>
		<link>http://phaq.phunsites.net/2006/06/22/implementing-filesystem-allocation-limits-on-freebsd-jails/</link>
		<comments>http://phaq.phunsites.net/2006/06/22/implementing-filesystem-allocation-limits-on-freebsd-jails/#comments</comments>
		<pubDate>Wed, 21 Jun 2006 23:06:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[jails]]></category>

		<guid isPermaLink="false">http://phaq.phunsites.net/2006/06/22/implementing-filesystem-allocation-limits-on-freebsd-jails/</guid>
		<description><![CDATA[FreeBSD jails are very powerful indeed. While it is rather easy to setup a jail for encapsulation of single services (let&#8217;s think of it as a more luxury chroot environment then), it&#8217;s also no big thing to adapt them to create &#8216;virtual&#8217; servers similar to what Virtuozzo and OpenVZ for Linux try to achieve. Obviously [...]]]></description>
			<content:encoded><![CDATA[<p>FreeBSD jails are very powerful indeed. While it is rather easy to setup a jail for encapsulation of single services (let&#8217;s think of it as a more luxury chroot environment then), it&#8217;s also no big thing to adapt them to create &#8216;virtual&#8217; servers similar to what Virtuozzo and OpenVZ for Linux try to achieve.<br />
<span id="more-36"></span><br />
Obviously FreeBSD jails were not designed for this in the first place, probably the only reason why the implementation lacks some basic resource control functionality. This may of course change in the (not so distant) future, in the meantime we need to get along with the the limitations placed upon us.</p>
<p>This article as the first in a series covers a simple concept to implement file system allocation limits for FreeBSD jails in a virtual server environment.</p>
<p><strong>What is this about?</strong></p>
<p>As outlined above I will talk about setting up file system allocation limits. This is to restrict the maximum disk space a single jail (or virtual server if we go by that name) may allocate on the host system.</p>
<p>The basic idea is to use as much of the functionality already available in today&#8217;s FreeBSD 6.1-release.</p>
<p>At the end we&#8217;ll have jails that have their own encapsulated root filesystem which is dynamically (un-)mounted on jail startup/shutdown. Exceeding pre-allocated disk space will not be possible anymore using this approach, hardening the host against race conditions.</p>
<p><strong>What is this not about?</strong></p>
<p>This article is not about filesystem quota, though in theory they should work on top of the approach given.</p>
<p><strong>Prerequisites</strong></p>
<p>This has been tested on FreeBSD 6.1-RELEASE on Sparc64 but should work on other platforms, too.</p>
<p><strong>Patching mdmfs</strong></p>
<p>This approach will use functionality already available by today: mdmfs.</p>
<p>mdmfs will allow creation of so called vnode backed (or swap- or malloc-backed, there are actually three ways for this) memory devices (in other words a &#8220;RAM&#8221;-disk).</p>
<p>The idea is to mount a vnode-backed filesystem (which is actually stored inside a physical file on your hard drives) and place the jail&#8217;s root filesystem inside.</p>
<p>One caveat is that mdmfs will format the vnode&#8217;s loopback file so there&#8217;s is some patching involved.</p>
<p>Luckily the changes necessary are already available in FreeBSD 6-current and they can easily be backported to FreeBSD 6.1-RELEASE and possibly also elder releases.</p>
<p>Create a new temporary build directory first:</p>
<p>#mkdir /root/mount_md<br />
#cd /root/mount_md</p>
<p>Now get the source code from CVS. Maybe it&#8217;s easiest to get this particular release through WebCSV at <a href="http://www.freebsd.org/cgi/cvsweb.cgi/~checkout~/src/sbin/mdmfs/mdmfs.c?rev=1.27">http://www.freebsd.org/cgi/cvsweb.cgi/~checkout~/src/sbin/mdmfs/mdmfs.c?rev=1.27</a>.</p>
<p>Save this file to your previously created build directory.</p>
<p>If you have wget at hands you can also download it directly.</p>
<p>#wget &#8211;user-agent=&#8217;Mozilla/5.0&#8242; &#8216;http://www.freebsd.org/cgi/cvsweb.cgi/~checkout~/src/sbin/mdmfs/mdmfs.c?rev=1.27&#8242; &#8211;output-document=/root/mount_md/mount_md.c</p>
<p>Compile the source:</p>
<p>#gcc /root/mount_md/mount_md.c -o /root/mount_md/mount_md</p>
<p>Then copy the file to some location you like, eg. /usr/sbin.</p>
<p>#cp /root/mount_md/mount_md /usr/sbin/mount_md</p>
<p>Make sure you call the file as given in the example. It won&#8217;t work otherwise.</p>
<p>Special care must be taken that you DO NOT replace your existing mdmfs binary file by this new version. DO NOT overwrite it. Do not rename this patch to mdmfs. Use the names provided in this example instead.</p>
<p><strong>Create/Update Jail Directory Structure<br />
</strong></p>
<p>You may already have an existing directory structure to store you jail virtual servers. Some changes may be necessary according to your local setup. I&#8217;m referring to my own setup as an example.</p>
<p>I have a directory structure below /mnt/devname/jails where the virtual servers are stored in subdirectories named by their IP address. I use a host with the IP 192.168.0.1, which gives me this directory structure:</p>
<p>/mnt/r5_vol1/jails/192.168.0.1</p>
<p>Inside of this directory I have two files:</p>
<p>#ls -l /mnt/r5_vol1/jails/192.168.0.1<br />
total 1049106<br />
-rw-r&#8211;r&#8211;  1 root  wheel          61 May 31 22:36 fstab<br />
-rw-r&#8211;r&#8211;  1 root  wheel  1073741824 Jun 22 00:32 rootfs.volume</p>
<p>The &#8216;fstab&#8217; file contains mount information for the jail while the rootfs.volume will serve as the loopback filesystem container.</p>
<p>The loopback filesystem container will be mounted below /var/jails/192.168.0.1.</p>
<p><strong>Create the filesystem container</strong></p>
<p>Now it is time to create the filesystem container, in other words the file that will host the jail&#8217;s root filesystem.</p>
<p>You will use &#8216;dd&#8217; to achieve this. In the example a container file of 1 GB will be created:</p>
<p>#dd if=/dev/zero of=/mnt/r5_vol1/192.168.0.1/rootfs.volume bs=1m count=1024</p>
<p>Configure a vnode using mdconfig, this will give the id of the vnode (md3 in the example).</p>
<p>#mdconfig -a -t vnode -f /mnt/r5_vol1/192.168.0.1/rootfs.volume<br />
md3</p>
<p>Create the file system:</p>
<p>#newfs /dev/md3</p>
<p>Mount the container file:</p>
<p>#mount /dev/md3 /var/jails/192.168.0.1</p>
<p>Install the FreeBSD userland into the loopback filesystem as you would do usually. Umount the loopback device afterwards.</p>
<p>#umount /var/jails/192.168.0.1</p>
<p>Then remove the md vnode unit (md3 in the example will give &#8216;-u 3&#8242;):</p>
<p>#mdconfig -d -u 3</p>
<p><strong>Setup Jail&#8217;s fstab</strong></p>
<p>To have your jail regurlary use the loopback container as it&#8217;s root device you need to create a fstab for it. This is actually the fstab file I wrote about above.</p>
<p>Add these lines to it (you need both):</p>
<p>md /var/jails/192.168.0.1 md rw,-P,-F/mnt/r5_vol1/jails/192.168.0.1/rootfs.volume<br />
md /var/jails/192.168.0.1 ufs rw,noauto</p>
<p><strong>Update Jail Settings In rc.conf</strong></p>
<p>Finally you must tweak your jail settings in rc.conf. This is an example how I did it.</p>
<p># 192.168.0.1 jail<br />
#<br />
jail_192_168_0_1_rootdir=&#8221;/var/jails/192.168.0.1&#8243;<br />
jail_192_168_0_1_hostname=&#8221;example.phunsites.net&#8221;<br />
jail_192_168_0_1_ip=&#8221;192.168.0.1&#8243;<br />
jail_192_168_0_1_interface=&#8221;hme0&#8243;<br />
jail_192_168_0_1_devfs_enable=&#8221;YES&#8221;<br />
jail_192_168_0_1_mount_enable=&#8221;YES&#8221;<br />
jail_192_168_0_1_fstab=&#8221;/mnt/r5_vol1/jails/192.168.0.1/fstab&#8221;</p>
<p><strong>Conclusion</strong></p>
<p>This is a way how to enforce disk space usage limits for jail-based virtual servers.</p>
<p>It certainly has it&#8217;s drawbacks like fixed pre-allocation of disk space. It&#8217;s also slower than real disk access.</p>
<p>On the other hand it places strict limits on the virtual server. If cannot exceed it&#8217;s disk space allocation, takeing away possible race conditions from the host.</p>
<p>This approach will likely not be required anymore in the future as there is a project to create a <a href="http://code.google.com/soc/freebsd/appinfo.html?csaid=856AD0343916F1EE">storage virtualisation layer within geom</a>. This will provide us most certainly with a better solution on a long term basis.</p>
]]></content:encoded>
			<wfw:commentRss>http://phaq.phunsites.net/2006/06/22/implementing-filesystem-allocation-limits-on-freebsd-jails/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Installing Trimmed-Down Userland To FreeBSD Jails</title>
		<link>http://phaq.phunsites.net/2006/05/12/installing-trimmed-down-userland-to-freebsd-jails/</link>
		<comments>http://phaq.phunsites.net/2006/05/12/installing-trimmed-down-userland-to-freebsd-jails/#comments</comments>
		<pubDate>Fri, 12 May 2006 07:11:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[jails]]></category>

		<guid isPermaLink="false">http://phaq.phunsites.net/2006/05/12/installing-trimmed-down-userland-to-freebsd-jails/</guid>
		<description><![CDATA[For obvious reasons there is a lot of howto&#8217;s on FreeBSD jails. One of the IMHO best is, besides the man page , at section6wiki. While the howto explains everything you need to get started, I was fiddling around with a way to install a trimmed-down userland to a jail without editing or moving around [...]]]></description>
			<content:encoded><![CDATA[<p>For obvious reasons there is a lot of howto&#8217;s on FreeBSD jails. One of the IMHO best is, besides the man page <img src='http://phaq.phunsites.net/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> , at <a title="Section6wiki" target="_blank" href="http://www.section6.net/wiki/index.php/Creating_a_FreeBSD_Jail">section6wiki</a>.</p>
<p>While the howto explains everything you need to get started, I was fiddling around with a way to install a trimmed-down userland to a jail without editing or moving around /etc/make.conf. The reason to do this is simple: The system in question was not solely decicated to running jails and I wanted to avoid the toolchain within the jails at any cost. So I basically looked only for a simple and fast way to install the userland without tampering with my existing configuration.<br />
<span id="more-23"></span><br />
&#8216;man jail&#8217; lists dozens of variables that can be put into /etc/make.conf to enable or disable certain features. A current list can also be found at /usr/share/examples/etc/make.conf. I&#8217;d recommend to take your options out of the example make.conf, as the man page is not always up to date.<br />
If you don&#8217;t want to alter your existing /etc/make.conf (not even moving files around or such), the only way for a simple and straight forward install is by passing environment variables to &#8216;make&#8217;, eg.</p>
<p>make installworld DESTDIR=/my/path/to/jail NO_TOOLCHAIN=yes NO_BLUETOOTH=yes NO_BOOT=yes NO_CXX=yes NO_FORTRAN=yes NO_GDB=yes NO_GPIB=yes NO_I4B=yes NOINET6=yes NOATM=yes NO_USB=yes NO_LPR=yes NO_ACPI=yes NO_VINUM=yes NO_MAN=yes NO_SHAREDOCS=yes NO_GAMES=yes NO_INFO=yes NO_SHARE=yes NO_SENDMAIL=yes NO_BIND=yes NO_AUTHPF=yes NO_CVS=yes NO_PF=yes NO_IPFILTER=yes NO_MAILWRAPPER=yes NO_NIS=yes NO_NETCAT=yes</p>
<p>Of course the same environment variables must be used when running &#8216;make distribution&#8217; from /usr/src/etc.</p>
<p>This will install a trimmed down userland to a jail of around 60 MB, leaving me most tools at hand while omitting the more specialized ones usually not needed within a jail.</p>
<p>Take care though when installing a jail like this from your host&#8217;s source tree. If you are building jails on a regurlar basis, it maybe better to have a second source tree around for building jails.<br />
If you are using your host&#8217;s regurlar source tree, I&#8217;d recommend to first to a regurlar (eg. non-altered) &#8216;make buildworld&#8217; and running &#8216;make installworld&#8217; with the parameters given above later on. This will allow usage of the source tree for both your host and any subsequent jails.</p>
<p>Special attention must be given to the exclude parameters in this case though, as there are some dependencies which must be fulfilled. This is why you cannot exclude some subsets during &#8216;make installworld&#8217; after running a full-fledged &#8216;make buildworld&#8217;.</p>
<p>If you are testing things out it may be best to temporarily disable kernel securelevel, otherwise you won&#8217;t be able to delete the files from the jail tree due to &#8216;system immutable&#8217; flags on some files within the tree. The same holds true when you try to update an existing jail.</p>
<p>You can circumvent this requirement however if you choose to install your jails within loop-back mounted disk images, which might be a good idea for limiting disk quota anyway.</p>
]]></content:encoded>
			<wfw:commentRss>http://phaq.phunsites.net/2006/05/12/installing-trimmed-down-userland-to-freebsd-jails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

