{"id":595,"date":"2012-01-08T06:17:31","date_gmt":"2012-01-08T06:17:31","guid":{"rendered":"http:\/\/poojanwagh.opalstacked.com\/techblog\/?p=595"},"modified":"2012-01-17T13:21:23","modified_gmt":"2012-01-17T13:21:23","slug":"set-up-vimagevnet-jail-on-freebsd-8-2","status":"publish","type":"post","link":"https:\/\/tech.poojanblog.com\/blog\/unix-linux\/set-up-vimagevnet-jail-on-freebsd-8-2\/","title":{"rendered":"Set up vimage\/vnet jail on FreeBSD 8.2"},"content":{"rendered":"<p>With epair, there are two interfaces created <code>epair0a<\/code> and <code>eapir0b<\/code> that are direct connections between each other (like an Ethernet cable). When used with jails using an <code>ifconfig<\/code> <code>vnet<\/code> command, one side (<code>epair0b<\/code> for example) sgoes inside the jail. Since the other side of this virtual direct-connection stays on the outside of the jail, the <code>epair<\/code> pairs act as an Ethernet tunnel inside\/outside the jail. You can use firewall rules either outside the jail or inside the jail to control traffic.<\/p>\n<p><!--more--><\/p>\n<p>I&#8217;m using ezjail-admin with the following patch to \/etc\/rc.d\/jail (taken from the <a title=\"Information About Vimage in jail\" href=\"http:\/\/forums.freebsd.org\/showthread.php?t=9006\" target=\"_blank\">FreeBSD Forums<\/a>):<\/p>\n<code>\n<pre>638,639c638,644\r\n&lt;               eval ${_setfib} jail ${_flags} -i ${_rootdir} ${_hostname} \\\r\n&lt;                       \\\"${_addrl}\\\" ${_exec_start} &gt; ${_tmp_jail} 2&gt;&amp;1\r\n---\r\n&gt;               if [ -z \"${_addrl}\" ] ; then\r\n&gt;                       eval ${_setfib} jail -i ${_flags} path=${_rootdir} host.hostname=${_hostname} \\\r\n&gt;                               command=${_exec_start} &gt; ${_tmp_jail} 2&gt;&amp;1\r\n&gt;               else\r\n&gt;                       eval ${_setfib} jail -i ${_flags} ${_rootdir} ${_hostname} \\\r\n&gt;                                 \\\"${_addrl}\\\" ${_exec_start} &gt; ${_tmp_jail} 2&gt;&amp;1\r\n&gt;               fi\r\n<\/code><\/pre>\n<p>For example, I have two jails. I create the interfaces like so in [ccie]\/etc\/rc.conf[\/ccie]:<\/p>\n<code><\/p>\n<p># ezjail with vimage\/epair config<br \/>\n# http:\/\/forums.freebsd.org\/showthread.php?t=9006<br \/>\n# http:\/\/lifanov.com\/doc\/vimage.html<br \/>\ncloned_interfaces=&#8221;epair0 epair1 bridge0&#8243;<br \/>\nifconfig_epair0a=&#8221;up&#8221;<br \/>\nifconfig_epair1a=&#8221;up&#8221;<br \/>\nifconfig_bridge0=&#8221;name jailbridge addm re0 up&#8221;<\/p>\n<p><\/code>\n<p>On the inside of the jail, I just need to set the IP address of the <code>epair0b<\/code> and <code>epair1b<\/code> interfaces. I&#8217;m using ezjail Here&#8217;s a sample [ccie]\/usr\/local\/etc\/ezjail\/&lt;jail_name&gt;[\/ccie], in this case for a subsonic server:<\/p>\n<code><\/p>\n<p># To specify the start up order of your ezjails, use these lines to<br \/>\n# create a Jail dependency tree. See rcorder(8) for more details.<br \/>\n#<br \/>\n# PROVIDE: standard_ezjail<br \/>\n# REQUIRE:<br \/>\n# BEFORE:<br \/>\n#<\/p>\n<p># vnet stuff: http:\/\/forums.freebsd.org\/showthread.php?t=9006<br \/>\n# http:\/\/lifanov.com\/doc\/vimage.html<br \/>\n# http:\/\/zewaren.net\/site\/?q=node\/71<br \/>\nexport jail_subsonic_flags=&#8221;-c vnet name=subsonic&#8221;<br \/>\n#export jail_subsonic_exec_prestart0=&#8221;ifconfig epair0 create&#8221;<br \/>\n#export jail_subsonic_exec_prestart1=&#8221;ifconfig epair0a up&#8221;<\/p>\n<p># sometiems rc.conf doesn&#8217;t do this:<br \/>\nexport jail_subsonic_exec_prestart0=&#8221;ifconfig jailbridge addm re0&#8243;<br \/>\nexport jail_subsonic_exec_prestart1=&#8221;ifconfig jailbridge addm epair0a up&#8221;<br \/>\nexport jail_subsonic_exec_prestart2=&#8221;mount_nullfs -o ro \/tank\/music \/usr\/jails\/subsonic\/var\/music&#8221;<br \/>\nexport jail_subsonic_exec_prestart3=&#8221;mount_nullfs -o rw \/tank\/music\/Playlists \/usr\/jails\/subsonic\/var\/playlists&#8221;<\/p>\n<p>export jail_subsonic_exec_poststart0=&#8221;ifconfig epair0b vnet subsonic&#8221;<br \/>\n#export jail_subsonic_exec_poststart1=&#8221;jexec subsonic \/etc\/rc.d\/ipfw start&#8221;<br \/>\nexport jail_subsonic_exec_poststart1=&#8221;jexec subsonic ifconfig lo0 127.0.0.1&#8243;<br \/>\nexport jail_subsonic_exec_poststart2=&#8221;jexec subsonic ifconfig epair0b 192.168.1.9 netmask 255.255.255.0&#8243;<br \/>\nexport jail_subsonic_exec_poststart3=&#8221;jexec subsonic route add default 192.168.1.1&#8243;<br \/>\nexport jail_subsonic_exec_poststop0=&#8221;umount \/usr\/jails\/subsonic\/var\/music&#8221;<br \/>\nexport jail_subsonic_exec_poststop1=&#8221;umount \/usr\/jails\/subsonic\/var\/playlists&#8221;<\/p>\n<p>export jail_subsonic_hostname=&#8221;subsonic&#8221;<br \/>\nexport jail_subsonic_ip=&#8221;&#8221;<br \/>\nexport jail_subsonic_rootdir=&#8221;\/usr\/jails\/subsonic&#8221;<br \/>\nexport jail_subsonic_exec_start=&#8221;\/bin\/sh \/etc\/rc&#8221;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # \/bin\/sh \/etc\/rc by default<br \/>\nexport jail_subsonic_exec_stop=&#8221;&#8221;<br \/>\nexport jail_subsonic_mount_enable=&#8221;YES&#8221;<br \/>\nexport jail_subsonic_devfs_enable=&#8221;YES&#8221;<br \/>\nexport jail_subsonic_devfs_ruleset=&#8221;devfsrules_jail2&#8243;<br \/>\nexport jail_subsonic_procfs_enable=&#8221;NO&#8221;<br \/>\nexport jail_subsonic_fdescfs_enable=&#8221;NO&#8221;<br \/>\nexport jail_subsonic_image=&#8221;&#8221;<br \/>\nexport jail_subsonic_imagetype=&#8221;&#8221;<br \/>\nexport jail_subsonic_attachparams=&#8221;&#8221;<br \/>\nexport jail_subsonic_attachblocking=&#8221;&#8221;<br \/>\nexport jail_subsonic_forceblocking=&#8221;&#8221;<br \/>\nexport jail_subsonic_zfs_datasets=&#8221;&#8221;<br \/>\nexport jail_subsonic_cpuset=&#8221;&#8221;<br \/>\nexport jail_subsonic_fib=&#8221;&#8221;<br \/>\n<\/code>\n<p>Also, don&#8217;t forget that you need something like the following within your jail&#8217;s <code>\/etc\/resolv.conf<\/code>:<\/p>\n<code><br \/>\nnameserver 192.168.1.1<br \/>\n<\/code>\n<p>I&#8217;m using FreeBSD&#8217;s IPFW. I have it set up so that it by default passes all packets. I can then control all firewall rules from the host (rather than in the jail). The last line of my host rules is a deny all to implementing the blocking on the host. I leave default rules within the jails (with epair\/vnet each jail gets its own firewall rules). I have things set up like:<\/p>\n<code><\/p>\n<p>oif=&#8221;re0&#8243;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # out interface<br \/>\njail_brif=&#8221;jailbridge&#8221;\u00a0 # jail interface<br \/>\nsubsonic_ip=&#8221;192.168.1.9&#8243; # jail IP<br \/>\nipdns=&#8221;192.168.1.1&#8243;<br \/>\nks=&#8221;keep-state&#8221;<br \/>\ncmd=&#8221;\/sbin\/ipfw -q add &#8221;<\/p>\n<p>&#8230;<\/p>\n<p>$cmd 02000 allow ip from any to any via $oif<br \/>\n$cmd 03300 allow log icmp from $ipdns to $subsonic_ip $ks<br \/>\n$cmd 03310 allow log icmp from $ipdns to $jabber_ip $ks<br \/>\n$cmd 03350 allow log icmp from $subsonic_ip to $ipdns $ks<br \/>\n$cmd 03355 allow log icmp from $jabber_ip to $ipdns $ks<br \/>\n# DNS:<br \/>\n$cmd 03400 allow udp from $subsonic_ip to $ipdns 53 $ks<br \/>\n# subsonic:<br \/>\n$cmd 04500 allow ip from any to $subsonic_ip dst-port 8180 setup $ks<br \/>\n# allow last.fm scrobbling (etc):<br \/>\n$cmd 06600 allow log ip from $subsonic_ip to any dst-port 80 setup $ks<br \/>\n$cmd 06700 allow log ip from $subsonic_ip to any dst-port 443 setup $ks<br \/>\n$cmd 65500 deny log ip from any to any<br \/>\n<\/code>\n<p>It&#8217;s been a while since I set this up, and I&#8217;m not sure if the allow all on <code>$oif<\/code> (command 02000) is really necessary.<\/p>\n<div class='wp_likes' id='wp_likes_post-595'><a class='like' href=\"javascript:wp_likes.like(595);\" title='Like' ><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/tech.poojanblog.com\/blog\/wp-content\/plugins\/wp-likes\/images\/like.png\" alt='' border='0'\/><\/a><span class='text'><b>4<\/b> people like this post.<\/span><\/p>\n<div class='like' ><a href=\"javascript:wp_likes.like(595);\">Like<\/a><\/div>\n<div class='unlike' ><a href=\"javascript:wp_likes.unlike(595);\">Unlike<\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>With epair, there are two interfaces created and that are direct connections between each other (like an Ethernet cable). When used with jails using an command, one side ( for example) sgoes inside the jail. Since the other side of this virtual direct-connection stays on the outside of the jail, the pairs act as an [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[10],"tags":[179,176,177,178],"class_list":["post-595","post","type-post","status-publish","format-standard","hentry","category-unix-linux","tag-epair","tag-jail","tag-vimage","tag-vnet"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/posts\/595","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/comments?post=595"}],"version-history":[{"count":10,"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/posts\/595\/revisions"}],"predecessor-version":[{"id":641,"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/posts\/595\/revisions\/641"}],"wp:attachment":[{"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/media?parent=595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/categories?post=595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/tags?post=595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}