贊助商連結

FreeBSD Multi Jails

多 Jails 設置,如果要跑多個 Jails 很多東西可以共用,像是 lib 之類的

建立 Base System

1
2
3
# mkdir /home/jails /home/jails/base
# cd /usr/src
# make installworld DESTDIR=/home/jails/base

安裝 cpdup

1
2
# cd /usr/ports/sysutils/cpdup/
# make install clean

複製 /usr/src 到 Jail base system

1
# cpdup /usr/src /home/jails/base/usr/src

打造骨架

1
2
3
4
5
6
# mkdir /home/jails/skel /home/jails/skel/home /home/jails/skel/usr-X11R6/home/jails/skel/distfiles
# mv etc /home/jails/skel
# mv usr/local /home/jails/skel/usr-local
# mv tmp /home/jails/skel
# mv var /home/jails/skel
# mv root /home/jails/skel

安裝設定檔

1
2
3
# mergemaster -t /home/jails/skel/var/tmp/temproot -D /home/jails/skel -i
# cd /home/jails/skel/
# rm -R bin boot lib libexec mnt proc rescue sbin sys usr dev

連結檔案

1
2
3
4
5
6
7
8
9
10
# cd /home/jails/base
# mkdir s
# ln -s s/etc etc
# ln -s s/home home
# ln -s s/root root
# ln -s ../s/usr-local usr/local
# ln -s ../s/usr-X11R6 usr/X11R6
# ln -s ../../s/distfiles usr/ports/distfiles
# ln -s s/tmp tmp
# ln -s s/var var

編輯設定檔

1
2
# joe /home/jails/skel/etc/make.conf
WRKDIRPREFIX?= /s/portbuild

編輯 fstab

1
2
3
4
5
# joe /etc/fstab
/home/jails/base /home/jails/wiki1 nullfs ro 0 0
/home/jails/base /home/jails/wiki2 nullfs ro 0 0
/home/vs/wiki1 /home/jails/wiki1/s nullfs rw 0 0
/home/vs/wiki2 /home/jails/wiki2/s nullfs rw 0 0

編輯 rc.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
jail_enable="YES"
jail_set_hostname_allow="NO"
jail_list="wiki1 wiki2"
jail_wiki1_rootdir="/home/jails/wiki1"
jail_wiki1_hostname="wiki1.j.mis.au.edu.tw"
jail_wiki1_ip="192.168.1.100"
jail_wiki1_devfs_enable="YES"
jail_wiki2_rootdir="/home/jails/wiki2"
jail_wiki2_hostname="wiki2.j.mis.au.edu.tw"
jail_wiki2_ip="192.168.1.101"
jail_wiki2_devfs_enable="YES"

建立 mount point

1
# mkdir /home/jails/wiki1 /home/jails/wiki2

建立子 Jails 及複製骨架

1
2
3
# mkdir /home/vs
# cpdup /home/jails/skel /home/vs/wiki1
# cpdup /home/jails/skel /home/vs/wiki2

設定 IP

1
2
# ifconfig em1 inet 192.168.1.100 alias
# ifconfig em1 inet 192.168.1.101 alias

啟動 Jails

1
2
3
4
5
6
# mount -a
# /etc/rc.d/jail start
# jls
JID IP Address Hostname Path
2 192.168.1.101 wiki2.j.mis.au.edu.tw /home/jails/wiki2
1 192.168.1.100 wiki1.j.mis.au.edu.tw /home/jails/wiki1

連結 Host 的 Ports

1
2
3
# mkdir -p /home/jails/base/usr/ports/
# mount_nullfs /usr/ports /home/jails/wiki1/usr/ports/
# mount_nullfs /usr/ports /home/jails/wiki2/usr/ports/

這樣就大致完成