一、 PXE:
Preboot Excution Environment預啟動執行環境 ,由intel公司研發
pxe工作模式:基于C/S的網絡模式,支持遠程主機通過網絡從遠端服務器下載映像,并由此支持通過網絡啟動操作系統
pxe技術的實現流程:
- 客戶端的網卡要支持網絡引導機制;如果有操作系統的話,應該調整為第一引導設備;在主機被喚醒之后,開始加載網絡引導應用時,此網卡會通過在本地局域網中廣播一個rarp協議,第一步獲得一個ip地址;
- 獲得ip地址的同時還會從dhcp那里獲得引導文件名稱和文件服務器地址;隨后會去找文件服務器主機,加載對應的文件,所需要的文件加載完成后,在內存中展開,而后基于此文件,可以實現去加載一個內核文件,此內核文件也一樣通過tftp文件服務器獲取,內核通常依賴于initrd虛根來完成對于真實根所在設備的驅動加載;
- 加載完成以后,這個內核文件通常是專用于為系統安裝所設定的,因此,如果配置了網絡屬性,這個內核文件還需要基于網絡配置在內核上的ip地址,基于網絡把自己扮演成某種協議的客戶端,加載一個能夠啟動安裝程序的程序包,在本地完成安裝并啟動這個應用程序,而此程序在已經不再tftp文件服務器上面;
- 網絡引導安裝方式中,依賴于網絡上應該存在一個基礎程序包鏡像倉庫,因此,此時還依賴于外部有一個基于ftp或http再或者nfs服務所提供的yum倉庫;還要通過這個yum倉庫加載安裝程序,以及安裝程序啟動完后,很可能要讀取kickstart文件,可以根據kickstart文件內容,解決依賴關系以后,基于這個yum倉庫,完成后續的所有安裝過程;
- 因此,對于centos系列主機,整個pxe技術大概要依賴以上所提到的服務,dhcp服務、tftp服務、能夠提供yum倉庫的服務;還要提供:kickstart文件(如果不想使用自動化可以不用提供此文件。
配置示例:
-
dhcp server:
[root@localhost ~]# yum -y install dhcp [root@localhost ~]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf #在打開的dhcpd.conf配置文件中添加如下內容 subnet 172.16.100.0 netmask 255.255.255.0 { range 172.16.100.10 172.16.100.20; option routers 172.16.100.1; filename "pxelinux.0"; next-server 172.16.100.10; } [root@localhost ~]# systemctl start dhcpd [root@localhost ~]# ss -unlp State Recv-Q Send-Q Local Address:Port Peer Address:Port UNCONN 0 0 *:67 *:* users:(("dhcpd",pid=4557,fd=7)) UNCONN 0 0 *:68 *:* users:(("dhclient",pid=4485,fd=6)) #至此dhcp啟動成功,添加開機自啟動 [root@localhost ~]# systemctl enable dhcpd Created symlink from /etc/systemd/system/multi-user.target.wants/dhcpd.service to /usr/lib/systemd/system/dhcpd.service. [root@localhost ~]# systemctl is-enabled dhcpd enabled
-
tftp-server
[root@localhost ~]# yum -y install tftp tftp-server [root@localhost ~]# systemctl start tftp [root@localhost ~]# ss -unl State Recv-Q Send-Q Local Address:Port Peer Address:Port UNCONN 0 0 *:67 *:* UNCONN 0 0 *:68 *:* UNCONN 0 0 :::69 :::* # tftp監聽在udp69端口,至此tftp-server服務啟動,默認工作目錄為: /var/lib/tftpboot/ [root@localhost ~]# systemctl enable tftp Created symlink from /etc/systemd/system/sockets.target.wants/tftp.socket to /usr/lib/systemd/system/tftp.socket. [root@localhost ~]# systemctl is-enabled tftp indirect
-
vsftpd server
此處使用vsftpd服務來提供yum源訪問[root@localhost ~]# yum -y install vsftpd #vsftpd默認工作目錄為: /var/ftp/pub/ [root@localhost ~]# systemctl start vsftpd [root@localhost ~]# ss -tnlp ... LISTEN 0 32 :::21 :::* users:(("vsftpd",pid=5338,fd=3)) #vsftpd服務監聽在tcp/21端口 #提供光盤鏡像里的yum源訪問路徑 [root@localhost ~]# mount -r /dev/sr0 /var/ftp/pub/centos/7 [root@localhost ~]# ls /var/ftp/pub/centos/7 CentOS_BuildTag EULA images LiveOS repodata RPM-GPG-KEY-CentOS-Testing-7 EFI GPL isolinux Packages RPM-GPG-KEY-CentOS-7 TRANS.TBL #提供一個kickstart文件放在/var/ftp/pub/kickstart/目錄下 [root@localhost ~]# mkdir /var/ftp/pub/kickstart [root@localhost ~]# cd /var/ftp/pub/kickstart/ [root@localhost kickstart]# ls centos7-ks.cfg [root@localhost kickstart]# chmod 644 centos7-ks.cfg
-
準備安裝系統需要的內核等文件,放在tftp服務工作目錄中
[root@localhost ~]# yum -y install syslinux [root@localhost ~]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/ [root@localhost ~]# cp /usr/share/syslinux/{memdisk,mboot.c32,chain.c32,menu.c32} /var/lib/tftpboot/ [root@localhost ~]# cp /var/ftp/pub/centos/7/images/pxeboot/{initrd.img,vmlinuz} /var/lib/tftpboot/ #提供目錄,創建專門用來引導時顯示的菜單默認文件:默認配置要放pxelinux.cfg的目錄,文件名為default: [root@localhost ~]# mkdir /var/lib/tftpboot/pxelinux.cfg [root@localhost ~]# vim /var/lib/tftpboot/pxelinux.cfg/default default menu.c32 prompt 5 timeout 30 MENU TITLE CentOS 7 PXE Menu LABEL linux MENU LABEL Install Centos 7 x86_64 KERNEL vmlinuz APPEND initrd=initrd.img ks=ftp://172.16.100.10/pub/kickstart /centos7-ks.cfg inst.repo=ftp://172.16.100.10/pub/centos/7
-
創建一個新的虛擬機以網絡自動安裝系統測試
menu.png
至此自動安裝開始 PXE測試通過
kickstart文件內容附錄如下:
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
url --url="ftp://172.16.100.10/pub/centos/7"
# Use graphical install
text
firewall --disabled
selinux --disabled
reboot
# Run the Setup Agent on first boot
firstboot --disable
ignoredisk --only-use=sda
# Keyboard layouts
keyboard --vckeymap=cn --xlayouts='cn'
# System language
lang zh_CN.UTF-8
timezone Asia/Shanghai --isUtc
# Network information
network --bootproto=dhcp --device=ens33 --onboot=on --ipv6=auto --no-activate
network --hostname=localhost.localdomain
# Root password
rootpw --iscrypted $6$CMUBzCualheXycom$I1OADgCKcXi7lK9LKgqt.efENmylM.09mCt8.aesLmsQJ8uLYgu2Ck9G.tarugXP73TN4rFxQVVsAdIU8yN/V/
# System services
services --enabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc
user --groups=wheel --name=inspur --password=$6$bx479ICdaF3SObqw$2F9YqiKtNp/SI5wPc.QJZggrWEhIfcXSbE1t74k222jaALrna.mqkH7PfWo.19mzCGjHrO1WwP1MXMDGZIrb7/ --iscrypted --gecos="inspur"
# X Window System configuration information
xconfig --startxonboot
# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
# Partition clearing information
clearpart --all --initlabel
zerombr
# Disk partitioning information
part /boot --fstype="xfs" --ondisk=sda --size=1024
part swap --fstype="swap" --ondisk=sda --size=2049
part /home --fstype="xfs" --ondisk=sda --size=10240
part pv.254 --fstype="lvmpv" --ondisk=sda --size=20484
volgroup centos --pesize=4096 pv.254
logvol / --fstype="xfs" --size=20480 --name=root --vgname=centos
%packages
@base
@core
@dial-up
@directory-client
@fonts
@gnome-desktop
@guest-agents
@input-methods
@internet-browser
@java-platform
@multimedia
@network-file-system-client
@networkmanager-submodules
@x11
chrony
kexec-tools
%end
%addon com_redhat_kdump --enable --reserve-mb='auto'
%end
%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
二、Cobbler
快速網絡安裝linux操作系統的服務,支持多種的linux發行版,也可以支持網絡安裝windows
- Cobbler是PXE的二次封裝,將多種參數封裝到一個菜單;是用python語言編寫
Cobbler的工作流程:
- client裸機配置了從網絡啟動后,開機后會廣播包請求DHCP服務器(cobbler server)發送其分配好的一個ip
- DHCP收到請求后發送responese,包括其ip
- client拿到ip后再向cobbler server發送請求OS引導文件的請求
- cobbler server告訴client 裸機OS引導文件的名字和TFTP server的ip和port
- client通過上面告知的tftp server地址通信,下載引導文件
- client執行該引導文件,確定加載的信息,選擇要按住的os,期間會在想cobbler server請求kickstart文件和 os image
- cobbler server發送請求的kickstart 和 os image
- celient加載kickstart文件
- client接收os image,安裝該os image
cobbler安裝
cobbler 基于EPEL源,需要先配置好epel的yum源; cobbler服務集成:PXE,DHCP,rsync,Http,DNS,Kickstart,IPMI電源管理
......
一、安裝:
[root@localhost ~]# yum install cobbler cobbler-web pykickstart debmirror
[root@localhost ~]# systemctl start httpd cobblerd
[root@localhost ~]# cobbler check #檢查存在的問題
1 : The 'server' field in /etc/cobbler/settings must be set to something other than localhost, or kickstarting features will not work. This should be a resolvable hostname or IP for the boot server as reachable by all machines that will use it.
2 : For PXE to be functional, the 'next_server' field in /etc/cobbler/settings must be set to something other than 127.0.0.1, and should match the IP of the boot server on the PXE network.
3 : some network boot-loaders are missing from /var/lib/cobbler/loaders, you may run 'cobbler get-loaders' to download them, or, if you only want to handle x86/x86_64 netbooting, you may ensure that you have installed a *recent* version of the syslinux package installed and can ignore this message entirely. Files in this directory, should you want to support all architectures, should include pxelinux.0, menu.c32, elilo.efi, and yaboot. The 'cobbler get-loaders' command is the easiest way to resolve these requirements.
4 : change 'disable' to 'no' in /etc/xinetd.d/rsync
5 : comment 'dists' on /etc/debmirror.conf for proper debian support
6 : comment 'arches' on /etc/debmirror.conf for proper debian support
7 : The default password used by the sample templates for newly installed machines (default_password_crypted in /etc/cobbler/settings) is still set to 'cobbler' and should be changed, try: "openssl passwd -1 -salt 'random-phrase-here' 'your-password-here'" to generate new one
8 : fencing tools were not found, and are required to use the (optional) power management features. install cman or fence-agents to use them
Restart cobblerd and then run 'cobbler sync' to apply changes.
如上各問題的解決方法如下所示:
1、修改/etc/cobbler/settings文件中的server參數的值為提供cobbler服務的主機相應的IP地址或主機名,如172.16.100.15;
2、修改/etc/cobbler/settings文件中的next_server參數的值為提供PXE服務的主機相應的IP地址,如172.16.100.16;
3、如果當前節點可以訪問互聯網,執行“cobbler get-loaders”命令即可;否則,需要安裝syslinux程序包,而后復制/usr/share/syslinux/{pxelinux.0,memu.c32}等文件至/var/lib/cobbler/loaders/目錄中;
4、執行“chkconfig rsync on”命令即可;
5、注釋/etc/debmirror.conf文件中的“@dists="sid";”一行;
6、注釋/etc/debmirror.conf文件中的“@arches="i386";”一行;
7、執行“openssl passwd -1 -salt $(openssl rand -hex 4)”生成密碼,并用其替換/etc/cobbler/settings文件中default_password_crypted參數的值;
8、執行“yum install cman fence-agents”命令安裝相應的程序包即可;
接著重啟cobblerd,而后執行“cobbler sync”同步新的配置至cobbler。
二、配置及啟動cobbler所依賴的各服務
cobbler的運行依賴于dhcp、tftp、rsync及dns服務。其中dhcp可由dhcpd(isc)提供,也可由dnsmasq提供;tftp可由tftp-server程序包提供,也可由cobbler自帶的tftp功能提供;rsync由rsync程序包提供;dns可由bind提供,也可由dnsmasq提供。
cobbler可自行管理這些服務中的部分甚至是全部,但需要配置/etc/cobbler/settings文件中的“manage_dhcp”、“manage_tftpd”、“manage_rsync”和“manage_dns”分別進行定義。另外,由于每種服務都有著不同的實現方式,如若需要進行自定義,需要通過修改/etc/cobbler/modules.conf配置文件中各服務的模塊參數的值來實現。
本文采用了獨立管理的方式,即不通過cobbler來管理這些服務
-
配置dhcp服務
定義好所需的“subnet”及其它參數或選項,而后啟動dhcpd守護進程即可。本示例中所用的dhcpd的配置如下所示:option domain-name "magedu.com"; option domain-name-servers 192.168.10.254,172.16.0.1; default-lease-time 43200; max-lease-time 86400; log-facility local7; subnet 172.16.0.0 netmask 255.255.0.0 { range 172.16.100.121 172.16.200; option routers 172.16.100.10; } next-server 172.16.100.10; filename="pxelinux.0";
接著使用“service dhcpd start
”啟動服務即可。
-
配置tftp服務
# chkconfig tftp on # service xinetd restart
三、配置cobbler
cobbler的各主要組件間的關系如下圖所示。
- 管理distro
使cobbler變得可用的第一步為定義distro,其可以通過為其指定外部的安裝引導內核及ramdisk文件的方式實現。而如果已經有完整的系統安裝樹(如CentOS6的安裝鏡像)則推薦使用import直接導入的方式進行。
- 例如,對于已經掛載至/media/cdrom目錄的CentOS 6.5 x86_64的安裝鏡像,則可以使用類似如下命令進行導入
cobbler import --name=centos-6.5-x86_64 --path=/media/cdrom
- 可使用“
cobbler distro list
”列出所有的distro。 - 如果有kickstart文件,也可以使用“
--kickstart=/path/to/kickstart_file
”進行導入,因此import會自動為導入的distro生成一個profile。
- 管理profile
cobbler使用profile來為特定的需求類別提供所需要安裝配置,即在distro的基礎上通過提供kickstart文件來生成一個特定的系統安裝配置。distro的profile可以出現在PXE的引導菜單中作為安裝的選擇之一。
因此,如果需要為前面創建的centos-6.5-x86_64這個distro提供一個可引導安裝條目,其用到的kickstart文件為/tmp/centos-6.5-x86_64.cfg(只提供了最基本的程序包),則可通過如下命令實現。
cobbler profile add --name=centos-6.5-x86_64-basic --distro=centos-6.5-x86_64 --kickstart=/tmp/centos-6.5-x86_64.cfg
可使用“cobbler profile list
”查看已經創建的profile。
四、使用cobbler_web
- 配置cobbler_web的認證功能
cobbler_web支持多種認證方式,如authn_configfile、authn_ldap或authn_pam等,默認為authn_denyall,即拒絕所有用戶登錄。下面說明兩種能認證用戶登錄cobbler_web的方式。
使用authn_pam模塊認證cobbler_web用戶
首先修改modules中[authentication]段的module參數的值為authn_pam。
接著添加系統用戶,用戶名和密碼按需設定即可,例如下面的命令所示。
# useradd cblradmin
# echo 'cblrpass' | passwd --stdin cblradmin
而后將cblradmin用戶添加至cobbler_web的admin組中。修改/etc/cobbler/users.conf文件,將cblradmin用戶名添加為admin參數的值即可,如下所示。
[admins] admin = "cblradmin"
最后重啟cobblerd服務,通過http://YOUR_COBBLERD_IP/cobbler_web訪問即可。
- 使用authn_configfile模塊認證cobbler_web用戶
首先修改modules.conf中[authentication]段的module參數的值為authn_configfile。
接著創建其認證文件/etc/cobbler/users.digest,并添加所需的用戶即可。需要注意的是,添加第一個用戶時,需要為htdigest命令使用“-c”選項,后續添加其他用戶時不能再使用;另外,cobbler_web的realm只能為Cobbler。如下所示。
# htdigest -c /etc/cobbler/users.digest Cobbler cblradmin
最后重啟cobblerd服務,通過http://YOUR_COBBLERD_IP/cobbler_web訪問即可。