PXE+kickstart無人值守安裝centos7

PXE+kickstart無人值守安裝centos7

1.1 PXE說明

所謂的PXE是Preboot Execution Environment的縮寫,字面上的意思是開機前的執行環境。

要達成PXE必須要有兩個環節:

(1)一個是客戶端的網卡必須要支持PXE用戶端功能,並且開機時選擇從網卡啓動,這樣系統纔會以網卡進入PXE客戶端的程序;

(2)一個是PXE服務器必須要提供至少含有DHCP以及TFTP的服務!

且其中:

​ · DHCP服務必須要能夠提供客戶端的網絡參數,還要告知客戶端TFTP所在的位置;

​ · TFTP則提供客戶端的boot loader及kernel file下載路徑。

還要加上NFS/FTP/HTTP(選擇一樣即可)等提供安裝文件(安裝鏡像的解壓文件),纔算是比較完整的PXE服務器。一般TFTP和DHCP服務都由同一臺服務器提供,且大多數時候還提供NFS/FTP/HTTP服務,所以PXE服務器一般是提供3合一的服務。

1.2 PXE流程

如下圖:圖片來源於網絡,雖不易理解,但細節描述的很好。

img

(1).Client向PXE Server上的DHCP發送IP地址請求消息,DHCP檢測Client是否合法(主要是檢測Client的網卡MAC地址),如果合法則返回Client的IP地址,同時將pxe環境下的Boot loader文件pxelinux.0的位置信息傳送給Client。

(2).Client向PXE Server上的TFTP請求pxelinux.0,TFTP接收到消息之後再向Client發送pxelinux.0大小信息,試探Client是否滿意,當TFTP收到Client發回的同意大小信息之後,正式向Client發送pxelinux.0。

(3).Client執行接收到的pxelinux.0文件

(4).Client向TFTP請求pxelinux.cfg文件(其實它是目錄,裏面放置的是是啓動菜單,即grub的配置文件),TFTP將配置文件發回Client,繼而Client根據配置文件執行後續操作。

(5).Client向TFTP發送Linux內核請求信息,TFTP接收到消息之後將內核文件發送給Client。

(6).Client向TFTP發送根文件請求信息,TFTP接收到消息之後返回Linux根文件系統。

(7).Client加載Linux內核(啓動參數已經在4中的配置文件中設置好了)。

(8).Client通過nfs/ftp/http下載系統安裝文件進行安裝。如果在4中的配置文件指定了kickstart路徑,則會根據此文件自動應答安裝系統。

1.3 部署環境說明

如下圖,192.168.38.137是PXE服務器,提供dhcp+tftp+http服務。其他該網段內的主機爲待安裝系統的主機羣。

PXE+kickstart無人值守安裝centos7

1.4 部署DHCP服務

首先安裝dhcp服務端程序。

yum -y install dhcp

DHCP主要是提供客戶端網絡參數與TFTP的位置,以及boot loader的文件名。同時,我們僅針對內網來告知TFTP的相關位置,所以可以編輯/etc/dhcp/dhcpd.conf在subnet的區塊內加入兩個參數即可。其中PXE上專門爲PXE客戶端下載的boot loader文件名稱爲pxelinux.0。

[root@server ~]# cat /etc/dhcp/dhcpd.conf
ddns-update-style none;
default-lease-time 259200;
max-lease-time 518400;    
option routers 192.168.38.2;
option domain-name-servers 192.168.38.2;
subnet 192.168.38.0 netmask 255.255.255.0 {
        range 192.168.38.200 192.168.38.220;
        option subnet-mask 255.255.255.0;
        next-server 192.168.38.137;             # 就是TFTP的位置
        filename "pxelinux.0";                  # 告知得從TFTP根目錄下載的boot loader文件名
}

重啓dhcp

systemctl restart dhcpd

1.5 部署TFTP

從流程圖中可以看出,boot loader文件pxelinux.0以及內核相關的配置文件(目錄pxelinux.cfg下)主要都是由TFTP來提供的!

TFTP的安裝很簡單,直接使用yum即可。不過要告訴客戶端TFTP的根目錄在哪裏,這樣客戶端才能找到相關文件。另外要注意,TFTP是由xinetd這個super daemon所管理的,因此設定好TFTP之後,要啓動的是xinetd。

yum install tftp-server

默認TFTP服務的根目錄是/var/lib/tftpboot/,默認就這個吧,然後disable改爲no即可

sed -ri '/disable/s/yes/no/' /etc/xinetd.d/tftp
cat /etc/xinetd.d/tftp

service tftp
{
        socket_type     = dgram
        protocol        = udp
        wait            = yes
        user            = root
        server          = /usr/sbin/in.tftpd
        server_args     = -s /var/lib/tftpboot
        disable         = no
        per_source      = 11
        cps             = 100 2
        flags           = IPv4
}

啓動TFTP並觀察之:

systemctl start tftp

ss -ltnup | grep tftp
udp    UNCONN     0      0        :::69                   :::*                   users:(("in.tftpd",pid=8425,fd=0),("systemd",pid=1,fd=28))

接下來的文件必須要放置於/var/lib/tftpboot/目錄下。

1.6 提供pxe的bootloader和相關配置文件

如果要使用PXE的開機引導的話,需要使用CentOS提供的syslinux包,從中copy兩個文件到tftp的根目錄/var/lib/tftpboot/下即可。整個過程如下:

yum -y install syslinux
cp -a /usr/share/syslinux/{menu.c32,vesamenu.c32,pxelinux.0} /var/lib/tftpboot/
mkdir /var/lib/tftpboot/pxelinux.cfg
ls -l /var/lib/tftpboot/
-rw-r--r--. 1 root root  55140 Oct 31  2018 menu.c32        # 提供圖形化菜單功能
-rw-r--r--. 1 root root  26759 Oct 31  2018 pxelinux.0      # bootloader文件
drwxr-xr-x. 2 root root      6 Sep  8 14:02 pxelinux.cfg    # 開機的菜單設定在這裏
-rw-r--r--. 1 root root 153104 Oct 31  2018 vesamenu.c32    # 也是提供圖形化菜單功能,但界面和menu.c32不同

pxelinux.cfg是個目錄,可以放置默認的開機選項,也可以針對不同的客戶端主機提供不同的開機選項。一般來說,可以在pxelinux.cfg目錄內建立一個名爲default的文件來提供默認選項。

如果沒有menu.c32或vesamenu.c32時,菜單會以純文字模式一行一行顯示。如果使用menu.c32或vesamenu.c32,就會有類似反白效果出現,此時可以使用上下鍵來選擇選項,而不需要看着屏幕去輸入數字鍵來選擇開機選項。經過測試,使用vesamenu.c32比menu.c32更加好看些。

這部分設定完畢後,就是內核相關的設定了。

1.7 從安裝鏡像中獲取Linux內核文件

要安裝Linux系統,必須提供Linux內核文件和initrd文件,這裏以64位版本的CentOS 7.2爲例。

這裏計劃將內核相關文件放在/tftpboot/CentOS7.2/目錄下。既然要從安裝鏡像中獲取內核相關文件,首先得要掛載鏡像。

mount /dev/cdrom /mnt
mkdir /var/lib/tftpboot/CentOS7.6
cp /mnt/isolinux/{initrd.img,vmlinuz} /var/lib/tftpboot/CentOS7.6/
cp /mnt/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default

其實僅需要vmlinuz和initrd.img兩個文件即可,不過這裏還將isolinux.cfg這個文件拷貝出來了,這個文件裏提供了開機選項,可以以它作爲修改開機選項和菜單的模板,這樣修改起來比較容易,也更便捷!

1.8 設置開機菜單並提供系統安裝文件

以下是CentOS 7.6中syslinux包中提供的isolinux.cfg中提供的默認內容。

[root@server ~]# cat /mnt/isolinux/isolinux.cfg 
default vesamenu.c32    # 這是必須項,或者使用menu.c32
timeout 600             # 超時等待時間,60秒內不操作將自動選擇默認的菜單來加載

display boot.msg        # 這是爲選項提供一些說明的文件

# Clear the screen when exiting the menu, instead of leaving the menu displayed.
# For vesamenu, this means the graphical background is still displayed without
# the menu itself for as long as the screen remains in graphics mode.
menu clear
menu background splash.png  # 背景圖片
menu title CentOS 7         # 大標題
menu vshift 8
menu rows 18
menu margin 8
#menu hidden
menu helpmsgrow 15
menu tabmsgrow 13

# Border Area
menu color border * #00000000 #00000000 none

# Selected item
menu color sel 0 #ffffffff #00000000 none

# Title bar
menu color title 0 #ff7ba3d0 #00000000 none

# Press [Tab] message
menu color tabmsg 0 #ff3a6496 #00000000 none

# Unselected menu item
menu color unsel 0 #84b8ffff #00000000 none

# Selected hotkey
menu color hotsel 0 #84b8ffff #00000000 none

# Unselected hotkey
menu color hotkey 0 #ffffffff #00000000 none

# Help text
menu color help 0 #ffffffff #00000000 none

# A scrollbar of some type? Not sure.
menu color scrollbar 0 #ffffffff #ff355594 none

# Timeout msg
menu color timeout 0 #ffffffff #00000000 none
menu color timeout_msg 0 #ffffffff #00000000 none

# Command prompt text
menu color cmdmark 0 #84b8ffff #00000000 none
menu color cmdline 0 #ffffffff #00000000 none

# Do not display the actual menu unless the user presses a key. All that is displayed is a timeout message.

menu tabmsg Press Tab for full configuration options on menu items.

menu separator # insert an empty line
menu separator # insert an empty line

label linux
  menu label ^Install CentOS 7  # 菜單文字
  kernel vmlinuz        # 內核文件路徑,注意相對路徑是從tftp的根路徑/tftpboot開始的,所以要改爲"./CentOS7.2/vmlinuz"
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 quiet
                        # 內核啓動選項,其中包括initrd的路徑,同樣要改爲"./CentOS7.6/initrd.img"
                        # stage2文件的搜索路徑,搜索的文件一般是".treeinfo",找不到該文件則找
label check
  menu label Test this ^media & install CentOS 7
  menu default          # menu default表示開機時光標一開始默認停留在此label上
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 rd.live.check quiet

menu separator # insert an empty line

# utilities submenu     # 子菜單項的設置方法
menu begin ^Troubleshooting
  menu title Troubleshooting

label vesa
  menu indent count 5
  menu label Install CentOS 7 in ^basic graphics mode
  text help
    Try this option out if you're having trouble installing
    CentOS 7.
  endtext
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 xdriver=vesa nomodeset quiet

label rescue
  menu indent count 5
  menu label ^Rescue a CentOS system
  text help
    If the system will not boot, this lets you access files
    and edit config files to try to get it booting again.
  endtext
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 rescue quiet

label memtest
  menu label Run a ^memory test
  text help
    If your system is having issues, a problem with your
    system's memory may be the cause. Use this utility to
    see if the memory is working correctly.
  endtext
  kernel memtest

menu separator # insert an empty line

label local
  menu label Boot from ^local drive
  localboot 0xffff

menu separator # insert an empty line
menu separator # insert an empty line

label returntomain
  menu label Return to ^main menu
  menu exit

menu end

所以,將其稍作修改,使其適合做pxe的菜單配置文件。

[root@server ~]# cat /var/lib/tftpboot/pxelinux.cfg/default
default vesamenu.c32
timeout 600

display boot.msg

menu clear
menu background splash.png
menu title CentOS 7
menu vshift 8
menu rows 18
menu margin 8
#menu hidden
menu helpmsgrow 15
menu tabmsgrow 13

menu color border * #00000000 #00000000 none
menu color sel 0 #ffffffff #00000000 none
menu color title 0 #ff7ba3d0 #00000000 none
menu color tabmsg 0 #ff3a6496 #00000000 none
menu color unsel 0 #84b8ffff #00000000 none
menu color hotsel 0 #84b8ffff #00000000 none
menu color hotkey 0 #ffffffff #00000000 none
menu color help 0 #ffffffff #00000000 none
menu color scrollbar 0 #ffffffff #ff355594 none
menu color timeout 0 #ffffffff #00000000 none
menu color timeout_msg 0 #ffffffff #00000000 none
menu color cmdmark 0 #84b8ffff #00000000 none
menu color cmdline 0 #ffffffff #00000000 none

label linux
  menu label ^Install CentOS 7 through pxe
  menu default
  kernel ./CentOS7.6/vmlinuz
  append initrd=./CentOS7.6/initrd.img inst.stage2=https://mirrors.huaweicloud.com/centos/7.6.1810/os/x86_64/ quiet net.ifnames=0 biosdevname=0

其中"net.ifnames=0 biosdevname=0"這兩個內核啓動參數是爲了讓網卡名稱爲ethN,而不是默認的eno16777728這樣的隨機名稱。

注意示例中stage2的路徑是使用了網絡源,最好本地搭建http,我偷懶了,當然ftp也可以

http的話直接把鏡像掛載到相應目錄即可

yum install httpd -y
systemctl start httpd
mkdir /var/www/html/CentOS7.6
mount /dev/cdrom  /var/www/html/CentOS7.6/

1.9 開機測試

新開一個虛擬機,進入bios界面設置從網卡啓動。將首先搜索DHCP服務器,找到DHCP後搜索bootloader文件,啓動菜單設置文件等,然後進入啓動菜單等待選擇要啓動的項。如下:

PXE+kickstart無人值守安裝centos7

PXE+kickstart無人值守安裝centos7

因爲只設置了一個啓動項,所以菜單中只有一項。啓動它,將加載一系列文件,直到出現安裝操作界面。

PXE+kickstart無人值守安裝centos7

然後就可以直接操作安裝系統了。但這樣畢竟是手動操作,無法實現批量系統安裝,所以要提供一個自動應答文件,每一次的手動操作步驟都由自動應答文件中給定的項來應答,這樣就能實現自動安裝操作系統,也就能實現批量系統安裝。

1.10 通過pxe+kickstart實現無人值守批量安裝操作系統

所謂的無人值守,就是自動應答,當安裝過程中需要人機交互提供某些選項的答案時(如如何分區),自動應答文件可以根據對應項自動提供答案。但是,無人值守並不完全是無人值守,至少設置bios從網卡啓動是必須人爲設置的,且安裝完系統後設置不從網卡啓動也是需要人爲設置的。除此之外,其他的基本上都可以實現無人值守安裝。

要配置無人值守的系統安裝環境,需要提供安裝過程中需要的各種答案,這些答案在kickstart的配置文件中設置,一般正常安裝完Linux系統在root用戶的家目錄下有一個anaconda-ks.cfg

參考:https://blog.csdn.net/yanghua1012/article/details/80426659

參考:http://ju.outofmemory.cn/entry/194801

也可以使用圖形化工具:system-config-kickstart

以下是修改後該文件中的內容,將用來做kickstart應答文件。並設置由ftp服務來提供該文件,所以將kickstart文件保存到ftp的pub目錄中。

mkdir /var/www/html/ks_file
cp ~/anaconda-ks.cfg /var/www/html/ks_file/ks7_mini.cfg
chmod +r /var/www/html/ks_file/ -R      # 必須要保證ks.cfg是全局可讀的

[root@server ~]# cat /var/www/html/ks_file/ks7_mini.cfg 
install
keyboard 'us'
rootpw  --iscrypted $6$TwMc7kHxAYSdICBU$yUVPcTo.SWi6FpWrZsx3.X.yjbrvqvgMxu0Jvqims55ZU6hQKPaR5DeQISwhcMBkmyVK/UJ1SFnpmu9E3S/Wu0
url --url="https://mirrors.huaweicloud.com/centos/7.6.1810/os/x86_64/"
# url --url="http://192.168.38.137/CentOS7.6/"
lang en_US
auth  --useshadow  --passalgo=sha512
text
firstboot --disable
selinux --disabled
firewall --disabled
network  --bootproto=dhcp --device=eth0
reboot
timezone Asia/Shanghai
user --name=qqq --password=$6$Vfzluz2el5OucNGd$oXvo557fWOVXnJVbaUBRUG25UDudEK9y0FaTHoVhyoWJju4EeiSsirf74dxQqphl10Yuc12MoiGsfqC0vJzVl/ --iscrypted --gecos="qqq"
bootloader --append="net.ifnames=0" --location=mbr
zerombr
clearpart --all --initlabel
part / --fstype="xfs" --size=10000
part /boot --fstype="xfs" --size=1000
part swap --fstype="swap" --size=1024

%post
sed '/^server/d' /etc/chrony.conf -i
sed '1a server ntp.aliyun.com iburst' /etc/chrony.conf -i
sed '1a server 0.cn.pool.ntp.org iburst' /etc/chrony.conf -i
sed '1a server ntp1.aliyun.com iburst' /etc/chrony.conf -i
systemctl enable chronyd
echo '*/30 * * * * /usr/sbin/ntpdate ntp.aliyun.com &>/dev/null' >> /var/spool/cron/root
sed '/^GSSAPIAuthentication/d' /etc/ssh/sshd_config -i
sed '/^UseDNS/d' /etc/ssh/sshd_config -i
echo "GSSAPIAuthentication no" >> /etc/ssh/sshd_config
echo "UseDNS no" >> /etc/ssh/sshd_config
ln -sf  /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
mkdir /root/.ssh
cd /root/.ssh
cat > authorized_keys <<EOF
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEArAgRomkl40uKp+Dz13prEbBf31cJGqVMqSir/zaxMyTZyTjd3BJc5qokWi6mBBsJ0TPEbciFAbnroy1BX/qsfSLWLAdzipp+3wsubx6gSkedT5s1G+xAOUUFoUiQMuwYwIjbAo8c+HKfDPHpzRVZ1d2vp0MbZjw6m87KftKUQvs=
EOF
chmod 700 /root/.ssh
chmod 600 /root/.ssh/authorized_keys
rm -f /etc/yum.repos.d/*
cat >>/etc/yum.repos.d/my.repo<<eof
[base]
name=sohu
baseurl=http://mirrors.sohu.com/centos/7/os/x86_64/
gpgcheck=0
enable=1
[epel]
name=epel
baseurl=http://mirrors.aliyun.com/epel/7Server/x86_64/
enable=1
gpgcheck=0
eof
%end
%packages
chrony
tree
vim-enhanced
%end

設置後,修改/var/lib/tftpboot/pxelinux.cfg/default 文件,在其中的內核啓動參數上加上一項kickstart文件的尋找路徑。

vim /var/lib/tftpboot/pxelinux.cfg/default 

label linux
  menu label ^Install CentOS 7 through pxe
  menu default
  kernel ./CentOS7.6/vmlinuz
  append initrd=./CentOS7.6/initrd.img  ks=http://192.168.38.137/ks_file/ks7_mini.cfg quiet net.ifnames=0 biosdevname=0 

#  append initrd=./CentOS7.6/initrd.img inst.stage2=http://192.168.38.137/CentOS7.6/ quiet net.ifnames=0 biosdevname=0

迴歸正題,現在已經設置好/var/lib/tftpboot/pxelinux.cfg/default 和/var/www/html/ks_file/ks7_mini.cfg,所以可以進行無人值守安裝Linux了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章