ネットワークブートによるCentOSの自動インストール環境を構築する
wrote by hiro1357
この方法を使うと、ネットワークブートしたマシンに問答無用でCentOSがインストールされます。大量にVMを作成したい場合などに便利。
準備するもの
- CentOSの入ったマシン
- http://boot.ipxe.org/undionly.kpxe
- http://ftp.riken.jp/Linux/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-1804.iso
kickstartファイルの作成
CentOSの自動インストールにはkickstartを使用する。起動時にkickstartファイル(ks.cfg)を読み込ませると、記述された内容で自動インストールされる。
ks.cfgは一度CentOSをインストールすると、 /root にインストール時の設定のks.cfgが保存されている。それを編集する。ks.cfgのオプションを書く順番はインストールの順序に関係がない。
インストールがうまくいかないときは、エラーが表示されるので、ksを修正する。(おそらく完全に自動インストールできるようになるまでは何回かデバッグが必要)
https://access.redhat.com/documentation/ja-jp/red_hat_enterprise_linux/6/html/installation_guide/installation_procedure_overview-s390-automating
インストールソースの指定
ks.cfg内に以下の内容を記述する。[固定IP]の部分には、最後に設定する固定IPアドレスを指定する。
url --url http://[固定IP]/iso
Rootパスワードの設定
生で書きたくない場合は --iscryptedというオプションが存在する
# Root password
rootpw ThisIsRootPassword0123
一般ユーザーの作成
こちらも --iscryptedオプションがある。 wheelグループを指定しておくことでsudoが使えるユーザーになる。
user --groups=wheel --name=user --password=ThisIsUserPassword --gecos="user"
その他の自動化に必要なオプション
cdromオプションはコメントアウトする
# Use CDROM installation media
#cdrom
テキストモードを有効にする
text
自動ステップを有効にする
autostep
インストールするディスクを設定する
# fix selection of disk
ignoredisk --only-use=sda
# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
autopart --type=lvm
# Partition clearing information
clearpart --all --drives=sda
言語の設定
# Keyboard layouts
keyboard --vckeymap=jp --xlayouts='jp','us'
# System language
lang en_US.UTF-8 --addsupport=ja_JP.UTF-8
ネットワークの設定
# Network information
network --bootproto=dhcp --device=ens192 --ipv6=auto --activate
network --hostname=localhost.localdomain
インストール後のカスタマイズ
インストール後に追加のアプリケーションをインストールするなどカスタマイズしたい場合、シェルスクリプトを実行することができる。
%post~%endをks.cfg末尾に追加し、その間に記述する。
%post
echo "shell script" >> /root/test
%end
必要なファイルをダウンロードする
wget http://boot.ipxe.org/undionly.kpxe
wget http://ftp.riken.jp/Linux/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-1804.iso
ソフトウェアのインストール
yum install epel-release
yum install nginx dhcp tftp-server xinetd
tftp-serverにundionly.kpxeを配置する
cp undionly.kpxe /var/lib/tftproot
nginxの設定
設定が必要なら、設定する。特に何も考えないならば、デフォルトの設定を使用する。
ISOファイルの展開
CentOSのISOファイルの中身を展開して、nginxの公開ディレクトリにコピーする
mkdir iso
sudo mount CentOS-7-x86_64-DVD-1804.iso ./iso
cp -arpv iso /usr/share/nginx/html/
sudo umount ./iso
ks.cfgの配置
ks.cfgをnginxの公開ディレクトリにコピーする。
cp ks.cfg /usr/share/nginx/html/
iPXEスクリプトの作成
iPXEがLinuxのカーネルとセットアップをhttp経由で読み込んでくれるように、スクリプトを作成する。ksdeviceはもしかしたらマシンに合わせて変更が必要かも。
vimなどでipxe.txtを作成する。
#!ipxe
kernel http://[設定した固定IP]/iso/images/pxeboot/vmlinuz ks=http://[設定した固定IP]/ks.cfg ksdevice=ens192
initrd http://[設定した固定IP]/iso/images/pxeboot/initrd.img
boot
iPXEスクリプトの配置
iPXEスクリプトをnginxの公開ディレクトリにコピーする。
cp ipxe.txt /usr/share/nginx/html/ipxe.txt
isc-dhcpの設定
/usr/share/doc/dhcp-4.2.5/dhcpd.conf.example
にサンプルがあるので、/etc/dhcp/dhcpd.conf
にコピーして通常のDHCPサーバの設定をしておく。
https://access.redhat.com/documentation/ja-jp/red_hat_enterprise_linux/7/html/networking_guide/sec-dhcp-configuring-server
iPXEとiPXEスクリプトをチェインローディングできるように/etc/dhcp/dhcpd.conf
のsubnet{}の中に以下のような設定を追加する。
http://ipxe.org/howto/dhcpd
if exists user-class and option user-class = "iPXE" {
option bootfile-name "http://[固定IP]/ipxe.txt";
}else{
option bootfile-name "/undionly.kpxe";
next-server [固定IP];
}
固定IPアドレスの設定
固定IPアドレスを設定する
nmtui
systemctl restart network
自動起動設定
systemctl enable nginx
systemctl enable xinetd
systemctl enable dhcpd
chkconfig tftp on
サーバ起動
systemctl start nginx
systemctl start xinetd
systemctl start dhcpd