一、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代表当客户端有多块网卡的时候,要实现自动化需要设置从其他网卡安装,不指定的话,安装的时候系统会让你选择,那就不叫全自动化了。