【FreeBSD】Jail環境の作成を自動化してみた(qjail版) そのに!多重flavorだよ!
昨日書いたスクリプト(【FreeBSD】Jail環境の作成を自動化してみた(qjail版) - くんすとの備忘録)の改良版です。
目標
目標を書いてなかったで……一応書いておくと、
最終的には「flavorをバージョン管理」して「flavorをデプロイ」することで気軽に破壊可能なJail運用をすることを目指しています。
おもいっきりimmutable infrastructureの影響を受けていますが……
前回からの変更点、及び特徴
- flavorの運用について
- qjailのflavorに加えて、ユーザーの作成したflavorを使用します(user_flavor)。
- プロダクトごとにユーザーflavorをバージョン管理し、Jail作成時に任意に組み込むことができます。
- システムのflavorはなるべく「qjail install」時に生成されたままのものを使用し、用途ごとの設定を、別途ユーザーflavorで更に味付けするイメージです。
- ユーザーflavorの直下に「rc.root」と「rc.user」の起動スクリプト置くと、Jail作成直後に1度だけ実行されます。
- 中で色々やるので、やっぱりsudoで実行してね。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
USAGE(){ | |
cat <<++EOS >&2 | |
USAGE:`basename $0` jail_name ip_address user_flavor [ -y root_password default_user default_user_password ] | |
++EOS | |
} | |
# const | |
PREFIX=/usr/jails | |
NIC=em1 | |
# ---------- PREPARING ---------- | |
# parse input | |
if [ "_$3" = "_" ] ; then | |
USAGE | |
exit 9 | |
fi | |
jail_name=$1 | |
ip_address=$2 | |
user_flavor=$3 | |
if [ "_$4" = "_-y" ] ; then | |
if [ "_$7" = "_" ] ; then | |
USAGE | |
exit 9 | |
fi | |
is_silent=$4 | |
root_password=$5 | |
default_user=$6 | |
default_user_password=$7 | |
else | |
is_silent="" | |
echo -n "Input root password: " | |
read root_password | |
echo -n "Input default username: " | |
read default_user | |
echo -n "Input default user password: " | |
read default_user_password | |
fi | |
# check input | |
if [ "$user_flavor" != "NONE" ] ; then | |
if [ ! -d "$user_flavor" ] ; then | |
echo "not existing or not directory: ${user_flavor}" | |
exit 9 | |
fi | |
fi | |
if qjail list | awk '{print $5}' | grep -x -q "${jail_name}" ; then | |
echo "existing jail_name: ${jail_name}" | |
exit 9 | |
fi | |
if qjail list | awk '{print $4}' | grep -x -q "${ip_address}" ; then | |
echo "existing ip_address: ${ip_address}" | |
exit 9 | |
fi | |
cat <<++EOS | |
I will create a Jail with the following. | |
jail_name :${jail_name} | |
ip_address :${ip_address} | |
user_flavor :${user_flavor} | |
root_password :${root_password} | |
default_user :${default_user} | |
default_user_password:${default_user_password} | |
++EOS | |
if [ "${is_silent}" != "-y" ] ; then | |
echo -n 'create? [y/n]: ' | |
read YN | |
if [ "$YN" != "y" -a "$YN" != "yes" ]; then | |
exit 9 | |
fi | |
fi | |
# ---------- MAIN ---------- | |
echo | |
echo "--- SETUP START ---" | |
echo "Creating a Jail" | |
qjail create -n $NIC -4 $ip_address $jail_name | |
qjail config -k $jail_name | |
echo | |
echo "Modifiying global settings" | |
echo " /etc/ssh/sshd_config" | |
cp -p ${PREFIX}/${jail_name}/etc/ssh/sshd_config ${PREFIX}/${jail_name}/etc/ssh/sshd_config.org | |
cat ${PREFIX}/${jail_name}/etc/ssh/sshd_config.org \ | |
| sed "s/^#Port 22/Port 22/g" \ | |
| sed "s/^#ListenAddress 0.0.0.0/ListenAddress ${ip_address}/g" \ | |
| sed "s/^#Protocol 2/Protocol 2/g" \ | |
| sed "s/^#PermitRootLogin no/PermitRootLogin no/g" \ | |
| sed "s/^#RSAAuthentication yes/RSAAuthentication yes/g" \ | |
| sed "s/^#PubkeyAuthentication yes/PubkeyAuthentication yes/g" \ | |
| sed "s/^#PasswordAuthentication no/PasswordAuthentication no/g" \ | |
| sed "s/^#PermitEmptyPasswords no/PermitEmptyPasswords no/g" \ | |
| sed "s/^#ChallengeResponseAuthentication yes/ChallengeResponseAuthentication no/g" \ | |
| sed "s/^#UseDNS yes/UseDNS no/g" \ | |
> ${PREFIX}/${jail_name}/etc/ssh/sshd_config | |
echo " /etc/rc.conf" | |
cp -p ${PREFIX}/${jail_name}/etc/rc.conf ${PREFIX}/${jail_name}/etc/rc.conf.org | |
cat <<++EOS >> ${PREFIX}/${jail_name}/etc/rc.conf | |
sshd_enable="YES" | |
++EOS | |
echo | |
echo "Starting Jail" | |
qjail start $jail_name | |
echo | |
echo "Setuping users" | |
echo " setuping root password" | |
jexec $jail_name sh -c "echo ${root_password} | pw usermod -n root -h 0" | |
echo " setuping default_user" | |
jexec $jail_name sh -c "echo ${default_user_password} | pw useradd -n ${default_user} -G wheel -m -h 0" | |
# apply user_flavor | |
if [ "$user_flavor" != "NONE" ]; then | |
echo | |
echo "Applying user_flavor" | |
cp -vR ${user_flavor}/* ${PREFIX}/${jail_name}/ | |
jexec $jail_name sh -c "chown -R ${default_user}:${default_user} /home/${default_user}/" | |
fi | |
# run scripts | |
if [ -x ${user_flavor}/rc.root ] ; then | |
echo | |
echo "Running /rc.root" | |
jexec $jail_name /rc.root | |
fi | |
if [ -x ${user_flavor}/rc.user ] ; then | |
echo | |
echo "Running /rc.user" | |
jexec -U ${default_user} $jail_name /rc.user | |
fi | |
echo | |
echo "--- SETUP END ---" |