OpenVZ テンプレートの作成


OpenVZ テンプレートの作成

Debian template creation

dockerのカスタムベースイメージを作成する

上記のサイトを参考にOpenVZのカスタムテンプレートを作成しました。

前回のProxmoxの構築した際の続きです。

ベースシステムの作成

debootstrapをインストールして、Debianのベースシステムを作成します。

apt-get install debootstrap
debootstrap --arch amd64 wheezy /var/lib/vz/private/777 http://cdn.debian.or.jp/debian

ベースシステムの更新

コンテナを起動してログインします。

vzctl start 777
vzctl enter 777

aptのリポジトリを修正します。

/etc/apt/sources.list
deb http://cdn.debian.or.jp/debian wheezy main
deb http://cdn.debian.or.jp/debian wheezy-updates main
deb http://security.debian.org/ wheezy/updates main

セキュリティアップデートして、必要なパッケージをインストールします。

apt-get update && apt-get upgrade
apt-get install ssh quota less

OpenVZでは不要なgettyを無効にします。

sed -i -e '/getty/d' /etc/inittab

I/Oパフォーマンスのために、ログを非同期で書き込むようにします。

sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/*syslog.conf

サービスを無効にします。
ls -l /etc/rc*.d/* | grep -e klogd -e exim4 -e inetd -e quotarpcの結果、quotarpcしか入っていなかったです。

#insserv -r klogd remove
#insserv -r exim4 remove
#insserv -r inetd remove
insserv -r quotarpc

初回起動時にsshのキーを作成するように設定します。

rm -f /etc/ssh/ssh_host_*
cat << EOF > /etc/init.d/ssh_gen_host_keys
#!/bin/sh
### BEGIN INIT INFO
# Provides:          Generates new ssh host keys on first boot
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:
# Short-Description: Generates new ssh host keys on first boot
# Description:       Generates new ssh host keys on first boot
### END INIT INFO
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ""
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ""
insserv -r /etc/init.d/ssh_gen_host_keys
rm -f \$0
EOF
chmod a+x /etc/init.d/ssh_gen_host_keys
insserv /etc/init.d/ssh_gen_host_keys

aptの高速化とdebファイルを残さないようにします。

echo 'force-unsafe-io' | sudo tee etc/dpkg/dpkg.cfg.d/02apt-speedup > /dev/null
echo 'DPkg::Post-Invoke {"/bin/rm -f /var/cache/apt/archives/*.deb || true";};' | sudo tee etc/apt/apt.conf.d/no-cache > /dev/null

vimのインストールして、viエディタをvim.basicに変更します。

apt-get install vim
dpkg --purge nano
update-alternatives --config editor

タイムゾーンをAsia/Tokyoに設定します。

dpkg-reconfigure tzdata

日本語localeにja_JP.UTF-8を設定します。

apt-get install locales
dpkg-reconfigure locales
update-locale LANG=ja_JP.UTF-8

ログを削除します。

> /var/log/messages; > /var/log/auth.log; > /var/log/kern.log; > /var/log/bootstrap.log; > /var/log/dpkg.log; > /var/log/syslog; > /var/log/daemon.log; > /var/log/apt/term.log; rm -f /var/log/*.0 /var/log/*.1 /var/log/*.gz

パッケージのキャッシュを削除します。

apt-get --purge clean

IPとホスト名の設定を削除して、コンテナを終了します。

vzctl set 777 --ipdell all --save
rm -f /var/lib/vz/private/777/etc/hostname
vzctl stop 777

テンプレートイメージの作成

tarballでイメージを作成します。

cat << EOF > /tmp/excludes
.bash_history
lost+found
/dev/*
/mnt/*
/tmp/*
/proc/*
/sys/*
/usr/src/*
EOF
cd /var/lib/vz/private/777
tar --numeric-owner -zcf /var/lib/vz/template/cache/debian-7.0-amd64-minimal.tar.gz . -X /tmp/excludes