一、kickstart無人值守安裝
1.1:什么是kickstart
kickstart是一種無人值守的安裝方式。它的工作原理是在安裝過程中記錄人工干預的各種參數,並生成一個名為ks.cfg的文件,如果在自動安裝過程中出現要填寫參數的情況,安裝程序首先會查找ks.cfg文件,如果找到合適的參數,就會采用此參數,如果沒有就會彈出對話框讓安裝人員手工填寫,也就是說ks.cfg文件包含了安裝過程中所有要填寫的參數,那么安裝人員完全可以告訴程序從何下載ks.cfg文件,然后自己安裝。
1.2:什么是PXE
PXE:預啟動執行環境
通過網絡接口啟動計算機,不依賴本地存儲設備或者已經安裝的本地操作系統
C/S工作模式
PXE客戶端會調用網際協議(IP)、用戶數據報協議(UDP)、動態主機設定協議(DHCP)、小型文件傳輸協議(TFTP)等網絡協議
PXE客戶端這個術語是指在PXE啟動過程中的角色
1.3:kickstart的原理
1.4:DHCP配置
# 安裝 yum -y install dhcp # 查找配置文件 rpm -ql dhcp |grep "dhcpd.conf" # 修改配置文件(各位記得修改成自己的IP地址) cat /etc/dhcp/dhcpd.conf subnet 192.168.163.0 netmask 255.255.255.0 { range 192.168.163..100 192.168.163.200; option subnet-mask 255.255.255.0; default-lease-time 21600; max-lease-time 43200; next-server 192.168.163.2; filename "/pxelinux.0"; } # 解釋 range 192.168.163.100 192.168.163.200 # 可分配的起始IP到結束IP option subnet-mask 255.255.255.0 # 設定子網掩碼 default-lease-time 21600 # 設置默認IP的租用期限 max-lease-time 43200 # 設置最大的IP租用期限 next-server 192.168.163.2 # 告知客戶端TFTP服務器的IP filename "/pxelinux.0" # 告知客戶端從TFTP根目錄下載的pxelinux.0文件 # 啟動dhcp服務 systemctl start dhcpd # 檢查有沒有啟動成功 netstat -tunlp|grep dhcp # 配置dhcp監聽的網卡(提示項根據環境自行選擇) vim /etc/sysconfig/dhcpd DHCPDARGS=ens33
1.5:TFTP配置
TFTP(Trivial File Transfer Protocol,簡單文件傳輸協議)是TCP/IP協議族中的一個用來在客戶機與服務器之間進行簡單文件傳輸的協議,提供不復雜、開銷不大的文件傳輸服務。端口號為69 # 安裝 yum -y install tftp-server # 修改配置文件 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 /var/lib/tftpboot # 指定目錄,保持默認,不用修改 disable = no # 由原來的yes改為no per_source = 11 cps = 100 2 flags = IPv4 } # 啟動 啟動tftp-server服務,這里要注意的是啟動tftp.service之前必須得先啟動tftp.socket systemctl start tftp.socket systemctl start tftp.service # 檢查 netstat -tunlp|grep 69
1.6:HTTP配置
可以用Apache或Nginx提供HTTP服務 # 安裝apache yum -y install httpd # 設置ServerName sed -i "100i ServerName 127.0.0.1:80" /etc/httpd/conf/httpd.conf # 啟動apache systemctl start httpd # 創建鏡像目錄 mkdir /var/www/html/CentOS7 # 掛載鏡像 mount /dev/cdrom /var/www/html/CentOS7/ # 查看掛載情況 df -h PS: 不管怎么弄,只要把安裝光盤內容能通過web發布即可。因為是演示,如果復制鏡像就有點浪費時間。但生產環境就一定要復制了,光盤讀取速度有限。
1.7:配置支持PXE的啟動程序
PXE引導配置(bootstrap) syslinux是一個功能強大的引導加載程序,而且兼容各種介質。SYSLINUX是一個小型的Linux操作系統,它的目的是簡化首次安裝Linux的時間,並建立修護或其它特殊用途的啟動盤。如果沒有找到pxelinux.0這個文件,可以安裝一下。 # 安裝 yum -y install syslinux # CP pxelinux.0到tftp目錄下 cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/ # CP 鏡像文件到tftp目錄下 cp -a /var/www/html/CentOS-6.7/isolinux/* /var/lib/tftpboot/ # 查看tftp目錄 ls /var/lib/tftpboot/ # 新建一個pxelinux.cfg目錄,存放客戶端的配置文件 mkdir -p /var/lib/tftpboot/pxelinux.cfg cp /var/www/html/CentOS-6.7/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default # default文件詳解 cat /var/lib/tftpboot/pxelinux.cfg/default default vesamenu.c32 # 默認加載一個菜單 #prompt 1 # 開啟會顯示命令行'boot: '提示符。prompt值為0時則不提示,將會直接啟動'default'參數中指定的內容。 timeout 600 # timeout時間是引導時等待用戶手動選擇的時間,設為1可直接引導,單位為1/10秒。 display boot.msg # 菜單背景圖片、標題、顏色。 menu background splash.jpg menu title Welcome to CentOS 6.7! menu color border 0 #ffffffff #00000000 menu color sel 7 #ffffffff #ff000000 menu color title 0 #ffffffff #00000000 menu color tabmsg 0 #ffffffff #00000000 menu color unsel 0 #ffffffff #00000000 menu color hotsel 0 #ff000000 #ffffffff menu color hotkey 7 #ffffffff #ff000000 menu color scrollbar 0 #ffffffff #00000000 # label指定在boot:提示符下輸入的關鍵字,比如boot:linux[ENTER],這個會啟動label linux下標記的kernel和initrd.img文件。 label linux # 一個標簽就是前面圖片的一行選項。 menu label ^Install or upgrade an existing system menu default kernel vmlinuz # 指定要啟動的內核。同樣要注意路徑,默認是/tftpboot目錄。 append initrd=initrd.img # 指定追加給內核的參數,initrd.img是一個最小的linux系統 label vesa menu label Install system with ^basic video driver kernel vmlinuz append initrd=initrd.img nomodeset label rescue menu label ^Rescue installed system kernel vmlinuz append initrd=initrd.img rescue label local menu label Boot from ^local drive localboot 0xffff label memtest86 menu label ^Memory test kernel memtest append -
1.8:kickstart的ks.cfg文件創建方式
通常,我們在安裝操作系統的過程中,需要大量的和服務器交互操作,為了減少這個交互過程,kickstart就誕生了。使用這種kickstart,只需事先定義好一個Kickstart自動應答配置文件ks.cfg(通常存放在安裝服務器上),並讓安裝程序知道該配置文件的位置,在安裝過程中安裝程序就可以自己從該文件中讀取安裝配置,這樣就避免了在安裝過程中多次的人機交互,從而實現無人值守的自動化安裝。
生成kickstart配置文件的三種方式
① 以安裝好的機器的/root/anaconda-ks.cfg 文件為模板進行修改(常用的方法)
② 安裝kickstart的圖形化配置工具(沒有必要這樣干)
③ 閱讀kickstart配置文件手冊,用編輯器自己寫(高手這么干)
我們選擇第一種方式 cat anaconda-ks.cfg #version=DEVEL # System authorization information auth --enableshadow --passalgo=sha512 # Use CDROM installation media cdrom # Use graphical install graphical # Run the Setup Agent on first boot firstboot --enable ignoredisk --only-use=sda # Keyboard layouts keyboard --vckeymap=cn --xlayouts='cn' # System language lang zh_CN.UTF-8 # Network information network --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto --no-activate network --hostname=localhost.localdomain # Root password rootpw --iscrypted $6$9aYRXH1/R7085cgV$0S6Eb4oB4ZW0imlGfDL4cawpSrVuZfz2FmaC0.VIBJP99M4QabsJgLTWu9Xy6nNYe4qYDMXan2FdjQi8.Dgd2. # System services services --enabled="chronyd" # System timezone timezone Asia/Shanghai --isUtc # System bootloader configuration bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda # Partition clearing information clearpart --none --initlabel # Disk partitioning information part /home --fstype="xfs" --ondisk=sda --size=32566 part /boot --fstype="xfs" --ondisk=sda --size=200 part swap --fstype="swap" --ondisk=sda --size=8192 part / --fstype="xfs" --ondisk=sda --size=10240 %packages @^minimal @core @debugging @development 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
1.9:kickstart的ks.cfg文件詳解
ks.cfg文件組成大致分為3段 命令段 鍵盤類型,語言,安裝方式等系統的配置,有必選項和可選項,如果缺少某項必選項,安裝時會中斷並提示用戶選擇此項的選項 軟件包段 %packages @groupname:指定安裝的包組 package_name:指定安裝的包 -package_name:指定不安裝的包 在安裝過程中默認安裝的軟件包,安裝軟件時會自動分析依賴關系。 腳本段(可選) %pre:安裝系統前執行的命令或腳本(由於只依賴於啟動鏡像,支持的命令很少) %post:安裝系統后執行的命令或腳本(基本支持所有命令)
install 告知安裝程序,這是一次全新安裝,而不是升級upgrade。 url --url=" " 通過FTP或HTTP從遠程服務器上的安裝樹中安裝。 url --url="http://192.168.163.129/CentOS7/" url --url ftp://<username>:<password>@<server>/<dir> nfs 從指定的NFS服務器安裝。 nfs --server=nfsserver.example.com --dir=/tmp/install-tree text 使用文本模式安裝。 lang 設置在安裝過程中使用的語言以及系統的缺省語言。lang en_US.UTF-8 keyboard 設置系統鍵盤類型。keyboard us zerombr 清除mbr引導信息。 bootloader 系統引導相關配置。 bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet" --location=,指定引導記錄被寫入的位置.有效的值如下:mbr(缺省),partition(在包含內核的分區的第一個扇區安裝引導裝載程序)或none(不安裝引導裝載程序)。 --driveorder,指定在BIOS引導順序中居首的驅動器。 --append=,指定內核參數.要指定多個參數,使用空格分隔它們。 network 為通過網絡的kickstart安裝以及所安裝的系統配置聯網信息。 network --bootproto=dhcp --device=eth0 --onboot=yes --noipv6 --hostname=CentOS6 --bootproto=[dhcp/bootp/static]中的一種,缺省值是dhcp。bootp和dhcp被認為是相同的。 static方法要求在kickstart文件里輸入所有的網絡信息。 network --bootproto=static --ip=192.168.163.100 --netmask=255.255.255.0 --gateway=192.168.163.2 --nameserver=192.168.163.2 請注意所有配置信息都必須在一行上指定,不能使用反斜線來換行。 --ip=,要安裝的機器的IP地址. --gateway=,IP地址格式的默認網關. --netmask=,安裝的系統的子網掩碼. --hostname=,安裝的系統的主機名. --onboot=,是否在引導時啟用該設備. --noipv6=,禁用此設備的IPv6. --nameserver=,配置dns解析. timezone 設置系統時區。timezone --utc Asia/Shanghai authconfig 系統認證信息。authconfig --enableshadow --passalgo=sha512 設置密碼加密方式為sha512 啟用shadow文件。 rootpw root密碼 clearpart 清空分區。clearpart --all --initlabel --all 從系統中清除所有分區,--initlable 初始化磁盤標簽 part 磁盤分區。 part /boot --fstype=ext4 --asprimary --size=200 part swap --size=1024 part / --fstype=ext4 --grow --asprimary --size=200 --fstype=,為分區設置文件系統類型.有效的類型為ext2,ext3,swap和vfat。 --asprimary,強迫把分區分配為主分區,否則提示分區失敗。 --size=,以MB為單位的分區最小值.在此處指定一個整數值,如500.不要在數字后面加MB。 --grow,告訴分區使用所有可用空間(若有),或使用設置的最大值。 firstboot 負責協助配置redhat一些重要的信息。 firstboot --disable selinux 關閉selinux。selinux --disabled firewall 關閉防火牆。firewall --disabled logging 設置日志級別。logging --level=info reboot 設定安裝完成后重啟,此選項必須存在,不然kickstart顯示一條消息,並等待用戶按任意鍵后才重新引導,也可以選擇halt關機。
1.9:編寫自己的kickstart的ks.cfg文件
# 生成一個密碼備用 grub-crypt # 創建存放配置文件的目錄 mkdir /var/www/html/ks_config # 編寫文件 vim /var/www/html/ks_config/CentOS7-ks.cfg #version=DEVEL # System authorization information auth --enableshadow --passalgo=sha512 # Use CDROM installation media cdrom # Use graphical install graphical # Run the Setup Agent on first boot firstboot --enable ignoredisk --only-use=sda # Keyboard layouts keyboard --vckeymap=cn --xlayouts='cn' # System language lang zh_CN.UTF-8 # Network information network --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto --no-activate network --hostname=localhost.localdomain # Root password rootpw --iscrypted $6$9aYRXH1/R7085cgV$0S6Eb4oB4ZW0imlGfDL4cawpSrVuZfz2FmaC0.VIBJP99M4QabsJgLTWu9Xy6nNYe4qYDMXan2FdjQi8.Dgd2. # System services services --enabled="chronyd" # System timezone timezone Asia/Shanghai --isUtc # System bootloader configuration bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda # Partition clearing information clearpart --none --initlabel # Disk partitioning information part /home --fstype="xfs" --ondisk=sda --size=32566 part /boot --fstype="xfs" --ondisk=sda --size=200 part swap --fstype="swap" --ondisk=sda --size=8192 part / --fstype="xfs" --ondisk=sda --size=10240 firstboot --disable selinux --disabled firewall --disabled logging --level=info reboot %packages @^minimal @core @debugging @development chrony kexec-tools tree nmap sysstat lrzsz dos2unix telnet %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 wget -O /tmp/optimization.sh http://192.168.163.129/ks_config/optimization.sh &>/dev/null /bin/sh /tmp/optimization.sh %end
1.10:開機優化腳本
vim /var/www/html/ks_config/optimization.sh #!/bin/bash Ip=192.168.163.129 Port=80 ConfigDir=ks_config # Judge Http server is ok? PortNum=`nmap $Ip -p $Port 2>/dev/null|grep open|wc -l` [ $PortNum -lt 1 ] && { echo "Http server is bad!" exit 1 } # Defined result function function Msg(){ if [ $? -eq 0 ];then action "$1" /bin/true else action "$1" /bin/false fi } # Defined IP function function ConfigIP(){ Suffix=`ifconfig ens33|awk -F "[ .]+" 'NR==2 {print $6}'` cat >/etc/sysconfig/network-scripts/ifcfg-ens33 <<-END DEVICE=ens33 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=none IPADDR=192.168.163.$Suffix NETWORK=255.255.255.0 GATEWAY=192.168.163.2 DNS1=114.114.114.114 DEFROUTE=yes IPV4_FAILURE_FATAL=yes IPV6INIT=no NAME="System ens33" END Msg "config ens33" } # Defined Hide the system version number Functions function HideVersion(){ [ -f "/etc/issue" ] && >/etc/issue Msg "Hide issue" [ -f "/etc/issue.net" ] && > /etc/issue.net Msg "Hide issue.net" } # Defined OPEN FILES Functions function openfiles(){ [ -f "/etc/security/limits.conf" ] && { echo '* - nofile 65535' >> /etc/security/limits.conf Msg "open files" } } # Defined Kernel parameters Functions function kernel(){ KernelDir=/etc [ -f "$KernelDir/sysctl.conf" ] && /bin/mv $KernelDir/sysctl.conf{,.ori} wget -O $KernelDir/sysctl.conf http://$Ip:$Port/$ConfigDir/sysctl.conf &>/dev/null Msg "Kernel config" } # Defined System Startup Services Functions function boot(){ for oldboy in `chkconfig --list|grep "3:on"|awk '{print $1}'|grep -vE "crond|network|rsyslog|sshd|sysstat"` do chkconfig $oldboy off done Msg "BOOT config" } # Defined Time Synchronization Functions function Time(){ echo "#time sync by zhangyao at $(date +%F)" >>/var/spool/cron/root echo '*/5 * * * * /usr/sbin/ntpdate time.nist.gov &>/dev/null' >>/var/spool/cron/root Msg "Time Synchronization" } # Defined main Functions function main(){ ConfigIP HideVersion openfiles kernel boot Time } main
1.11:整合編輯default配置文件
vim /var/lib/tftpboot/pxelinux.cfg/default default ks prompt 0 label ks kernel vmlinuz append initrd=initrd.img ks=http://192.168.163.129/ks_config/CentOS7-ks.cfg # 告訴安裝程序ks.cfg文件在哪里 # append initrd=initrd.img ks=http://192.168.163.129/ks_config/CentOS7-ks.cfg ksdevice=ens33 # ksdevice=ens33代表當客戶端有多塊網卡的時候,要實現自動化需要設置從其他網卡安裝,不指定的話,安裝的時候系統會讓你選擇,那就不叫全自動化了。