Skip to content

Instantly share code, notes, and snippets.

@kunst1080
Last active December 27, 2015 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kunst1080/7398385 to your computer and use it in GitHub Desktop.
Save kunst1080/7398385 to your computer and use it in GitHub Desktop.
自分用の、Jail環境構築スクリプト
#!/usr/local/bin/bash
# [前提]
# ①flavour/default に、以下のファイルをセットアップ済みであること
# - /etc/rc.conf
# - /etc/resolv.conf
# - /etc/make.conf
# - /etc/ssh/sshd_config
# - /var/ports/packages/
#
# ②また、ホスト側で仮想IPアドレスの設定が完了していること。
# (/etc/rc.cong でのネットワークデバイスのエイリアスを設定済みなど)
#
# ※メモ
# Jail環境の削除は:ezjail-admin delete [-w] <jail_name>
#
USAGE(){
cat <<++EOS>&2
USAGE:`basename $0` jail_name ip_address [ -y root_password default_user default_user_password ]
++EOS
}
PREFIX=/usr/jails
if [ "_$2" = "_" ] ; then
USAGE
exit 9
fi
jail_name=$1
ip_address=$2
if [ "_$3" = "_-y" ] ; then
if [ "_$6" = "_" ] ; then
USAGE
exit 9
fi
is_silent=$3
root_password=$4
default_user=$5
default_user_password=$6
else
is_silent=""
echo -n "Jail環境のrootユーザのパスワードを入力して下さい: "
read root_password
echo
echo -n "デフォルトのユーザ名を入力して下さい: "
read default_user
echo
echo -n "デフォルトユーザのパスワードを入力して下さい: "
read default_user_password
echo
fi
cat <<++EOS
以下の内容で Jail 環境を構築します
jail_name :${jail_name}
ip_address :${ip_address}
root_password :${root_password}
default_user :${default_user}
default_user_password:${default_user_password}
++EOS
if [ "${is_silent}" != "-y" ] ; then
echo -n 'よろしいですか ? [y/n]: '
read YN
if [ "$YN" != "y" -a "$YN" != "yes" ]; then
exit 9
fi
fi
echo
echo "########### SETUP START ###########"
set -v
# create jail
ezjail-admin create ${jail_name} ${ip_address}
# edit fstab
cat <<++EOD>>/etc/fstab.${jail_name//./_}
/usr/ports ${PREFIX}/${jail_name}/basejail/usr/ports nullfs ro 0 0
/usr/src ${PREFIX}/${jail_name}/basejail/usr/src nullfs ro 0 0
++EOD
# edit settings from flavour
cat ${PREFIX}/${jail_name}/etc/ssh/sshd_config \
| sed "s/\${ip_address}/${ip_address}/g" \
| sed "s/\${default_user}/${default_user}/g" \
> ${PREFIX}/${jail_name}/etc/ssh/sshd_config.tmp
mv -f ${PREFIX}/${jail_name}/etc/ssh/sshd_config.tmp ${PREFIX}/${jail_name}/etc/ssh/sshd_config
# start jail
service ezjail start ${jail_name}
# get jail ID
jls
jid=`jls | awk -v arg="$1" '$3==arg {print $1}'`
echo ${jid}
# user settings
jexec ${jid} sh -c "echo ${root_password} | pw usermod -n root -h 0"
jexec ${jid} sh -c "echo ${default_user_password} | pw useradd -n ${default_user} -G wheel -m -h 0"
set +v
echo "########### SETUP END ###########"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment