FAT47の底辺インフラ議事録

学んだことのメモ帳です

CentOS5.6でPXEブートサーバを構築&kickstartによるインストール自動化

PXEブートサーバを構築すると、OSのインストールが楽チンになります。
Kickstartファイルもつくると、自動でOSインストールを行うことが可能になります。


TFTPサーバ準備

yum install tftp-server tftp
rpm -qa | grep tftp
tftp-server-0.49-2.el5.centos
tftp-0.49-2.el5.centos


xinetdの設定変更

vi /etc/xinetd.d/tftp
# default: off
# description: The tftp server serves files using the trivial file transfer \
#       protocol.  The tftp protocol is often used to boot diskless \
#       workstations, download configuration files to network-aware printers, \
#       and to start the installation process for some operating systems.
service tftp
{
        socket_type             = dgram
        protocol                = udp
        wait                    = yes
        user                    = root
        server                  = /usr/sbin/in.tftpd
        server_args             = -s /tftpboot
        disable                 = no ##### ここの項目をnoにする ####
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}

xinetdの再起動

service xinetd restart

CentOSイメージファイルのマウント

mkdir -p /var/www/html/centos5
mount -t iso9660 -o loop /tmp/CentOS-5.6-x86_64-bin-DVD-1of2.iso /var/www/html/centos5

PXEサーバがインストールされているか確認

rpm -qa | grep syslinux
syslinux-3.11-4

されていなかったらインストール

yum install syslinux

pxelinux.0をコピー

mkdir /tftpboot/linux-install
cp /usr/lib/syslinux/pxelinux.0 /tftpboot/linux_install/

ISOのブートイメージをTFTPサーバ上にコピー

cp /var/www/html/centos5/images/pxeboot/vmlinuz /tftpboot/linux-install
cp /var/www/html/centos5/images/pxeboot/initrd.img /tftpboot/linux-install

PXEブート用設定ファイルの作成

mkdir /tftpboot/linux-install/pxelinux.cfg
vi /tftpboot/linux-install/pxelinux.cfg/default
default centos5

label centos5
kernel vmlinuz
append load initrd=initrd.img devfs=nomount

HTTPでイメージを提供

apacheのインストールは下記参照
http://d.hatena.ne.jp/fat47/20110905/1315189217

vi /usr/local/apache2/conf/httpd.conf
User httpd
Group httpd

ServerName 192.168.50.101 ##環境に合わせる(PXEサーバのIPアドレス)

DocumentRoot "/var/www/html/"

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Order deny,allow
    Allow from all
</Directory>

DHCPサーバの構築

yum install dhcp
rpm -qa | grep dhcp
dhcp-3.0.5-23.el5_6.4
dhcpv6-client-1.0.10-20.el5
vi /etc/dhcpd.conf
# dhcpd.conf
ddns-update-style interim;
ignore client-updates;

subnet 192.168.50.0 netmask 255.255.255.0 {  ###環境に合わせて変更

# --- default gateway
        option routers                  192.168.50.1;   ###ルータのIPアドレス指定
        option subnet-mask              255.255.255.0;

        #option nis-domain              "domain.org";
        #option domain-name             "domain.org";
        option domain-name-servers      192.168.50.1;  ###DNSサーバのIPアドレス指定###

        option time-offset              -18000; # Eastern Standard Time
        filename "/linux-install/pxelinux.0";
        next-server                     192.168.50.101; ###PXEサーバのIPアドレス指定
#       option ntp-servers              192.168.50.1;
#       option netbios-name-servers     192.168.50.1;
# --- Selects point-to-point node (default is hybrid). Don't change this unless
# -- you understand Netbios very well
#       option netbios-node-type 2;

        range dynamic-bootp 192.168.50.10 192.168.50.90; ###DHCPで配るIPアドレスの範囲###
        default-lease-time 21600;
        max-lease-time 43200;

        # we want the nameserver to appear at a fixed address
#        host ns {
#                next-server marvin.redhat.com;
#                hardware ethernet 12:34:56:78:AB:CD;
#                fixed-address 207.175.42.254;
#        }
}
サーバ ポート プロトコル
DHCP 67 UDP
TFTP 69 UDP
PXE 4011 UDP

ファイアウォール設定で上記のポートを許可

iptables -A INPUT -p udp -s 192.168.50.0/24 --dport 67 -j ACCEPT
iptables -A INPUT -p udp -s 192.168.50.0/24 --dport 69 -j ACCEPT
iptables -A INPUT -p udp -s 192.168.50.0/24 --dport 4011 -j ACCEPT

サーバ設定ここまで


OSインストールしたいマシンを立ち上げる

DHCPでアドレスが割り振られたのち、インストーラが起動

[HTTP]を選び、インタフェースを選択

Web site nameにPXEサーバのIPアドレス
その下には centos5 と入力する

OKボタンを押してしばらくするとCentOSGUIインストーラ起動

エラーが出る場合は、今までの設定が間違っていないか、
ディレクトリのパーミッションなどを確認してください。



Kickstartの設定
先ほどは手動でインストールを進めたが、Kickstartファイルを用意することで、自動インストールが可能になる
OSをインストールすると、/root/anaconda-ks.cfgというファイルが生成されている。
これはサーバのインストール情報が書かれたファイルで、このファイルを利用してkickstartを行う。
今回はPXEサーバの構築時に作られたanaconda-ks.cfgファイルを使う。
anacondaファイルをhttpでアクセスできる場所へコピー

cp /root/anaconda-ks.cfg /var/www/html/ks.cfg

kickstartの設定

# Kickstart file automatically generated by anaconda.

install
url --url http://192.168.50.101/centos5 ###PXEサーバの場所を指定
lang ja_JP.UTF-8
keyboard jp106
network --device eth1 --bootproto dhcp --hostname pxetest ### クライアントのホスト名を指定
rootpw --iscrypted ********************** ### 暗号化されたルートパスワードが記載されている
firewall --enabled --port=22:tcp ### ファイアウォール設定 無効にしたければdisabledに
authconfig --enableshadow --enablemd5
selinux --disabled
timezone --utc Asia/Tokyo
bootloader --location=mbr --driveorder=sda --append="rhgb quiet"
text ###テキストモードでインストールするため追記
reboot ###インストール終了後 自動で再起動させる
# The following is the partition information you requested
# Note that any partitions you deleted are not expressed
# here so unless you clear all partitions first, this is
# not guaranteed to work
clearpart --all --drives=sda ##パーティションの全削除
autopart ###パーティションの自動作成
#part /boot --fstype ext3 --size=100 --ondisk=sda
#part pv.2 --size=0 --grow --ondisk=sda
#volgroup VolGroup00 --pesize=32768 pv.2
#logvol / --fstype ext3 --name=LogVol00 --vgname=VolGroup00 --size=1024 --grow
#logvol swap --fstype swap --name=LogVol01 --vgname=VolGroup00 --size=1008 --grow --maxsize=2016

%packages
@admin-tools
@base
@core
@development-libs
@development-tools
@editors
@japanese-support
@legacy-network-server
@legacy-software-development
@legacy-software-support
@network-server
@text-internet
@x-software-development
@base-x
keyutils
kexec-tools
iscsi-initiator-utils
trousers
fipscheck
device-mapper-multipath
imake
vnc-server
mesa-libGLU-devel
xorg-x11-server-Xnest
xorg-x11-server-Xvfb

%packageセクションでは、インストールするパッケージを指定できる
%postセクションを追加してコマンドを書くと、インストール完了後にコマンドを実行してくれる
これを利用して、自動で自動でwebサーバの構築等も可能になる


あとは先ほどと同じようにインストールしたいマシンをブートさせれば、見事インストールが自動で完了しているはずです。