前言:系統的安裝我們有很多種方式,光盤、硬盤、ftp、http等,而對於擁有多臺主機的實驗環境而言,單一的通過光盤等來手動顯然已經無法滿足需求,現在我們就來了解一下通過pxe/kickstart 實現通過網絡的無人值守的安裝。
1. 什麼是PXE
嚴格來說,PXE 並不是一種安裝方式,而是一種引導方式。進行 PXE 安裝的必要條件是在要安裝的計算機中必須包含一個 PXE 支持的網卡(NIC),即網卡中必須要有 PXE Client。PXE (Pre-boot Execution Environment)協議可以使計算機通過網絡啓動。此協議分爲 Client端和 Server 端,而PXE Client則在網卡的 ROM 中。當計算機引導時,BIOS 把 PXE Client 調入內存中執行,然後由 PXE Client 將放置在遠端的文件通過網絡下載到本地運行。運行 PXE 協議需要設置 DHCP 服務器和 TFTP 服務器。DHCP 服務器會給 PXE Client(將要安裝系統的主機)分配一個 IP 地址,由於是給 PXE Client 分配 IP 地址,所以在配置 DHCP 服務器時需要增加相應的 PXE 設置。此外,在 PXE Client 的 ROM 中,已經存在了 TFTP Client,那麼它就可以通過 TFTP 協議到 TFTP Server 上下載所需的文件了。
2. 什麼是Kickstart
Kickstart是一種無人值守的安裝方式。它的工作原理是在安裝過程中記錄典型的需要人工干預填寫的各種參數,並生成一個名爲 ks.cfg的文件。如果在安裝過程中(不只侷限於生成Kickstart安裝文件的機器)出現要填寫參數的情況,安裝程序首先會去查找 Kickstart生成的文件,如果找到合適的參數,就採用所找到的參數;如果沒有找到合適的參數,便需要安裝者手工干預了。所以,如果Kickstart文件涵蓋了安裝過程中可能出現的所有需要填寫的參數,那麼安裝者完全可以只告訴安裝程序從何處取ks.cfg文件,然後就去忙自己的事情。等安裝完畢,安裝程序會根據ks.cfg中的設置重啓系統,並結束安裝。
實現安裝示意圖:
1、完整的yum源
#掛載光盤
[root@localhost ~]# mount /dev/cdrom /media/cdrom/
mount: block device /dev/cdrom is write-protected, mounting read-only
#配置yum源
[root@localhost ~]# vim /etc/yum.repos.d/cdrom.repo
[rhel-server]
name=rhel-server
baseurl=file:///media/cdrom/Server
enabled=1
gpgcheck=1
gpgkey=file:///media/cdrom/RPM-GPG-KEY-redhat-release
[rhel-cluster]
name=rhel-cluster
baseurl=file:///media/cdrom/Cluster
enabled=1
gpgcheck=1
gpgkey=file:///media/cdrom/RPM-GPG-KEY-redhat-release
[rhel-clusterstorage]
name=rhel-clusterstorage
baseurl=file:///media/cdrom/ClusterStorage
enabled=1
gpgcheck=1
gpgkey=file:///media/cdrom/RPM-GPG-KEY-redhat-release
[rhel-VT]
name=rhel-vt
baseurl=file:///media/cdrom/VT
enabled=1
gpgcheck=1
gpgkey=file:///media/cdrom/RPM-GPG-KEY-redhat-release
2、dhcp服務
#安裝dhcp
[root@localhost ~]# yum install dhcp -y
#拷貝配置文件並編輯配置
[root@localhost ~]# cp /usr/share/doc/dhcp-3.0.5/dhcpd.conf.sample /etc/dhcpd.conf
[root@localhost ~]# vim /etc/dhcpd.conf
ddns-update-style interim;
ignore client-updates;
subnet 192.168.2.0 netmask 255.255.255.0 {
# --- default gateway
option routers 192.168.2.1;
option subnet-mask 255.255.255.0;
option nis-domain "domain.org";
option domain-name "domain.org";
option domain-name-servers 192.168.2.1;
next-server 192.168.2.1;
filename "pxelinux.0";
option time-offset -18000; # Eastern Standard Time
range dynamic-bootp 192.168.2.2 192.168.2.128;
default-lease-time 21600;
max-lease-time 43200;
}
[root@localhost ~]# service dhcpd start && chkconfig dhcpd on
3、tftp服務
#安裝
[root@localhost ~]# yum install tftp-server -y
#修改服務腳本
[root@localhost ~]# vim /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 //開啓tftp
per_source = 11
cps = 100 2
flags = IPv4
}
#啓動服務
[root@localhost ~]# service xinetd restart
Stopping xinetd: [ OK ]
Starting xinetd: [ OK ]
[root@localhost ~]# netstat -tulpn |grep 69
udp 0 0 0.0.0.0:69 0.0.0.0:* 13792/xinetd
#tftpboot目錄的配置修改
[root@localhost ~]# cd /tftpboot
[root@localhost tftpboot]# cp /usr/lib/syslinux/pxelinux.0 ./
[root@localhost tftpboot]# cp /media/cdrom/p_w_picpaths/pxeboot/vmlinuz ./
[root@localhost tftpboot]# cp /media/cdrom/p_w_picpaths/pxeboot/initrd.img ./
[root@localhost tftpboot]# mkdir pxelinux.cfg
[root@localhost tftpboot]# cp /media/cdrom/isolinux/isolinux.cfg pxelinux.cfg/default
[root@localhost tftpboot]# vim pxelinux.cfg/default
default linux
prompt 1
timeout 10 //等待時長爲10秒
display boot.msg
F1 boot.msg
F2 options.msg
F3 general.msg
F4 param.msg
F5 rescue.msg
label linux
kernel vmlinuz
append ks=ftp://192.168.2.1/pub/ks.cfg ksdevice=eth0 initrd=initrd.img
label text
kernel vmlinuz
append initrd=initrd.img text
label ks
kernel vmlinuz
append ks initrd=initrd.img
label local
localboot 1
label memtest86
kernel memtest
append -
~
"pxelinux.cfg/default" 24L, 411C written
4、ftp服務器
[root@localhost ~]# yum install vsftpd -y
#將光盤的文件及目錄均拷貝到ftp的目錄下
[root@localhost ~]# cp -a /media/cdrom/* /var/ftp/pub/
#開啓服務並設爲開啓自動啓動
[root@localhost ~]# service vsftpd start && chkconfig vsftpd on
5、kickstart產生ks.cfg文件
#安裝system-config-kickstart
[root@localhost ~]# yum install system-config-kickstart -y
注意若系統沒有安裝圖形,要先安裝桌面
[root@localhost ~]# yum groupinstall "GNOME Desktop Environment" " X Window System" -y
[root@localhost ~]# system-config-kickstart
保存爲ks.cfg
注意:key --skip如果是紅帽系統,此選項可以跳過輸入序列號過程;如果是CentOS 系列,則可以不保留此項內容;reboot 此選項必須存在,也必須文中設定位置,不然kickstart顯示一條消息,並等待用戶按任意鍵後才重新引導;clearpart --all --initlabel此條命令必須添加,不然系統會讓用戶手動選擇是否清除所有數據,這就需要人爲干預了,從而導致自動化過程失敗;因爲sendmail及cups開機時很費時,故將其關閉,也可根據需要定義。
#將修改過的文件保存到ftp的根目錄下 很重要
[root@localhost ~]#cp /root/ks.cfg /var/ftp/pub/
6、創建新虛擬機測試
安裝完成