批量自动安装系统

指定内核参数启动

#在启动菜单界面,选中一项安装方法,按tab键,在后面增加参数
#在启动菜单界面,任意选中一项安装方法,按ESC键:boot: [linux] 参数设置

常见的内核参数

  • text:默认启动GUI安装接口,可以指定文本方式的安装界面
  • rescue:进入救援模式
  • inst.repo=path:指定安装源文件的路径,可以是以下格式
#Centos 6
DVD drive repo=cdrom :device
Hard Drive repo=hd:device/path
HTTP Server repo=http://host/path
HTTPS Server repo=https://host/path
FTP Server repo=ftp://username:password@host/path
NFS Server repo=nfs:server:/path
ISO images on an NFS Server repo=nfsiso:server:/path

#Centos 7
Any CD/DVD drive inst.repo=cdrom
Hard Drive inst.repo=hd:device:/path
HTTP Server inst.repo=http://host/path
HTTPS Server inst.repo=https://host/path
FTP Server inst.repo=ftp://username:password@host/path
NFS Server inst.repo=nfs:[options:]server:/path
  • ks=path: 指定自动化安装应答文件路径,如:initrd=initrd.img inst.ks=http://192.168.8.8/ksdir/ks8.cfg

  • ip= : 指定IP地址信息

ip=method,method #可以为dhcp
ip=interface:method #指定特定接口
ip=ip::gateway:netmask:hostname:interface:none #静态IP

应答文件(kickstart文件)

实现自动安装前,需要制作对应的安装应答文件,称为kickstart文件,用于保存安装过程需要指定的选项。

#kickstart文件在每个安装完的Linux系统家目录中存在
#其中记录了安装操作系统的整个过程
[root@centos8 ~]$ls ~
anaconda-ks.cfg
#对此文件稍作修改,即可实现自动安装
#注意!centos6和7,8文件格式略有不同

文件格式说明

kickstart文件主要包括三个部分:命令段程序包段脚本段

命令段:指明各种安装前配置,如键盘类型等

#命令段中的常见命令:
keyboard: #设定键盘类型
lang: #语言类型
zerombr:#清除mbr
clearpart:#清除分区
part: #创建分区
rootpw: #指明root的密码
timezone: #时区
text: #文本安装界面
network:#指定网络设置
firewall:#设置防火墙设置
selinux:#设置selinux设置
reboot:#安装完自动重启
user:#安装完成后为系统创建新用户
url: #指明安装源

#加密密码生成方式:

程序包段:指明要安装的程序包组或程序包,不安装的程序包等

%packages
@^environment group: #@……指定环境包组,(安装方法)如:@^minimal-environment
@group_name #@开头为指定包组
package
-package
%end

脚本段

%pre: #安装前脚本 
%post: #安装后脚本

注意!

  • CentOS 8,7,6 不同版本的kickstart文件格式不尽相同,不可混用
  • %addon, %packages, %onerror, %pre 、 %post 必须以%end结束,否则安装失败

文件使用过程

**1.**Create a Kickstart file.

**2.**Make the Kickstart file available on removable media, a hard drive or a network location.

**3.**Create boot media, which will be used to begin the installation.

**4.**Make the installation source available.

**5.**Start the Kickstart installation.

文件创建

  • 依据某模板修改并生成新配置,CentOS安装完后,会自动参考当前系统的安装过程,生成一个kickstart文件,所在位置:/root/anaconda-ks.cfg

文件语法检查

使用 ksvalidator 工具可以检查kickstart的文件格式是否有语法错误,来自于 pykickstart 包

#格式
ksvalidator kickstart.cfg  #如果有提示即说明有错误,没提示表示没错误

CentOS7应答文件参考

#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --plaintext magedu
# System language
lang en_US
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use text mode install
text
firstboot --disable
# SELinux configuration
selinux --disabled


# Firewall configuration
firewall --disabled
# Network information
network  --bootproto=static --device=eth0 --ip=10.0.0.123 --netmask=255.255.255.0
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# Use network installation
url --url="http://10.0.0.8/centos/7/os/x86_64"
# System bootloader configuration
bootloader --append="net.ifnames=0" --location=mbr
# Partition clearing information
clearpart --all
# Disk partitioning information
part / --fstype="xfs" --size=100000

%post
useradd mage
echo magedu |passwd --stdin mage
mkdir /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup
cat > /etc/yum.repos.d/base.repo <<EOF
[base]
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
EOF
mkdir /root/.ssh -m 700
cat > /root/.ssh/authorized_keys <<EOF
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC4fLgICiPzngrZ3J55lqfGH/yAifTe39nHi/9Z/2kC0WLYyyLvQGmyXP2I+y0sIaIpqvGHSglrXpsMszu2YoO72ydm+baSCazoH3pBUKvNa34+nukm9kUXsuNchFSzJGt6yOMEZNfvdenFYAAkRo5/hYNy/jgaGbCSejxxBjx1oPB1ik7DQUXuZJKeNQEHYuQLMeNOatR7eQSWPH/e6vSgu6kERtn4QKvIa7VTt1hL8oE2VaxlmtuQEEhuuU1TELn5atkoGX2bSCxPjJxfBLqLKA75DGmaIKZl849JCOsQH/4nSGnGsF1BDrV/JqfeTsDBlt3/DKIlNZ+5/BVBg4UPgAnKWFrrE52ub3L87SsqteES5Zh1K6yv3qA4Xagf1UTKFA7KhGZA2y+AwUTglQMeoYB1CNDQQjf35RDqjW25FD2IzqFurnVSkVEOU7u9mdN6v6KVZjAadOrFhXRoWtsJnEbwD/Dh1g8RWt55xIE0f4RekLNsmfgQxp3MiZSv8XU= root@centos8.localdomain
EOF
chmod 600 /root/.ssh/authorized_keys
%end

%packages
@web-server

%end

CentOS8应答文件参考

ignoredisk --only-use=sda
zerombr
text
reboot
clearpart --all --initlabel
selinux --disabled
firewall --disabled
url --url=http://10.0.0.8/centos/8/os/x86_64/
keyboard --vckeymap=us --xlayouts='us'
lang en_US.UTF-8
network  --bootproto=dhcp --device=eth0 --ipv6=auto --activate
network  --hostname=autosystem
rootpw --iscrypted $6$j9YhzDUnQVnxaAk8$qv7rkMcPAEbV5yvwsP666DXWYadd3jYjkA9fpxAo9qYotjGGBUclCGoP1TRvgHBpqgc5n0RypMsPTQnVDcpO01
firstboot --enable
skipx
services --disabled="chronyd"
timezone Asia/Shanghai --isUtc --nontp
user --name=xiang --password=6oUfb/02CWfLb5l8f$sgEZeR7c7DpqfpmFDH6huSmDbW1XQNR4qKl2EPns.gOXqlnAIgv9pTogtFVaDtEpMOC.SWXKYqxfVtd9MCwxb1 --iscrypted --gecos="wang"
part / --fstype="xfs" --ondisk=sda --size=102400
part /data --fstype="xfs" --ondisk=sda --size=51200
part swap --fstype="swap" --ondisk=sda --size=2048
part /boot --fstype="ext4" --ondisk=sda --size=1024

%packages
@^minimal-environment
kexec-tools
vim
%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

%post
sed -Ei.bak '/^GRUB_CMDLINE_LINUX/s/(.*)(")$/\1 net.ifnames=0\2/' /etc/default/grub
systemctl mask ctrl-alt-del.target
echo "PS1='\e[31;1m[\u@\h \W]$\e[0m'" > ssh_color.sh
mkdir /etc/yum.repos.d/repobak
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/repobak/
cat > /etc/yum.repos.d/CentOS-Base.repo << EOF
[base]
name=CentOS-\$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=https://mirrors.aliyun.com/centos/\$releasever/BaseOS/\$basearch/os/
        http://mirrors.aliyuncs.com/centos/\$releasever/BaseOS/\$basearch/os/
        http://mirrors.cloud.aliyuncs.com/centos/\$releasever/BaseOS/\$basearch/os/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
 
#additional packages that may be useful
[extras]
name=CentOS-\$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=https://mirrors.aliyun.com/centos/\$releasever/extras/\$basearch/os/
        http://mirrors.aliyuncs.com/centos/\$releasever/extras/\$basearch/os/
        http://mirrors.cloud.aliyuncs.com/centos/\$releasever/extras/\$basearch/os/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
 
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-\$releasever - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=https://mirrors.aliyun.com/centos/\$releasever/centosplus/\$basearch/os/
        http://mirrors.aliyuncs.com/centos/\$releasever/centosplus/\$basearch/os/
        http://mirrors.cloud.aliyuncs.com/centos/\$releasever/centosplus/\$basearch/os/
gpgcheck=1
enabled=0
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
 
[PowerTools]
name=CentOS-\$releasever - PowerTools - mirrors.aliyun.com
failovermethod=priority
baseurl=https://mirrors.aliyun.com/centos/\$releasever/PowerTools/\$basearch/os/
        http://mirrors.aliyuncs.com/centos/\$releasever/PowerTools/\$basearch/os/
        http://mirrors.cloud.aliyuncs.com/centos/\$releasever/PowerTools/\$basearch/os/
gpgcheck=1
enabled=0
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official


[AppStream]
name=CentOS-\$releasever - AppStream - mirrors.aliyun.com
failovermethod=priority
baseurl=https://mirrors.aliyun.com/centos/\$releasever/AppStream/\$basearch/os/
        http://mirrors.aliyuncs.com/centos/\$releasever/AppStream/$basearch/os/
        http://mirrors.cloud.aliyuncs.com/centos/\$releasever/AppStream/\$basearch/os/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
EOF
%end

基于网络实现自动安装(PXE)

基于网络实现自动安装操作系统是基于PXE实现的,但实现自动安装前需要装备dhcp服务器,tftp服务器,http服务器,以及安装syslinux-nonlinux包来获取pxelinux.0这个文件(可以在同一台主机搭建

yum install dhcp-server
cp /usr/share/doc/dhcp-server/dhcpd.conf.example /etc/dhcp/dhcpd.conf

vim /etc/dhcp/dhcpd.conf
option domain-name "xiangzheng.com";   #ping的时候自动补全地址
option domain-name-servers 180.76.76.76, 223.6.6.6;  #指定互联网的dns服务器

default-lease-time 86400; 	#IP地址租期,默认以秒为单位,86400为一天的秒数,根据企业情况设置
max-lease-time 106400;		#最大租期
#长租期:IP相对稳定,网络资源消耗较少,但是浪费IP资源
#短租期:IP相对不稳定,网络资源消耗较多,但是IP资源可以充分利用,可以实现较少IP为较多的主机服务

subnet 10.0.0.0 netmask 255.255.255.0 {  #指定地址池,网段
  range 10.0.0.100 10.0.0.200;	#指定地址范围
  option routers 10.0.0.2;#指定网关
  next-server 192.168.1.100; #指向TFTP服务器地址
  filename "pxelinux.0";     #bootloader启动文件的名称,实现基于网卡启动
}

systemctl enable --now dhcpd	#开机启动并立刻开启dhcp服务
ss -ntul 	#udp的67端口开启表示dhcp的服务端已启动
  • 准备tftp服务器
yum install tftp-server
systemctl enable --now tftpd
  • 准备http服务器
yum -y install httpd
systemctl enable --now httpd	
  • 安装syslinux-nonlinux
yum -y install syslinux-nonlinux

#主要为了获取pxelinux.0和menu.32文件
  • 准备仓库路径和yum源

    mkdir -p /var/www/html/centos/8/os/x86_64/
    mount /dev/sr0 /var/www/html/centos/8/os/x86_64/ 
    
    #正常生产中应该把光盘拷贝到服务器在挂载,注意挂载重启失效的问题,必要的话写到fstab文件中
    mount /data/iso/CentOS-8.3.2011-x86_64-dvd1.iso /var/www/html/centos/8/os/x86_64/
  • 准备ks文件并拷入到http中,并稍作修改

    mkdir /var/www/html/ks
    ls /var/www/html/ks
    centos8.cfg
    
    vim /var/www/html/ks/centos8.cfg
    url --url=http://10.0.0.28/centos/8/os/x86_64/   #指向yum源的路径,可以复制粘贴到网页中查看是否有问题
  • 准备PXE启动相关文件,放入到tftp服务器中

    mkdir /var/lib/tftpboot/centos8 #在tftp启动数据目录中创建文件夹(自己规划)
    
    #将centos8的内核相关文件拷入
    cp /var/www/html/centos/8/os/x86_64/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos8
    
    #将syslinux-nonlinux包中的pxelinux.0和menu.32文件拷入
    cp /usr/share/syslinux/{menu.c32,pxelinux.0} /var/lib/tftpboot/
    
    
    #以下三个文件是CentOS8安装所必须文件,CentOS6,7则不需要
    [root@centos8 ~]#cp /var/www/html/centos/8/os/x86_64/isolinux/{ldlinux.c32,libcom32.c32,libutil.c32} /var/lib/tftpboot/
    
    
    
    #生成菜单文件
    mkdir /var/lib/tftpboot/pxelinux.cfg/
    cp /var/www/html/centos/8/os/x86_64/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
  • 准备菜单文件

    #vim /var/lib/tftpboot/pxelinux.cfg/default 
    
    default menu.c32
    timeout 600
    
    menu title Azheng CentOS Linux 8
    
    label linux
      menu label ^Install CentOS Linux 8
      kernel centos8/vmlinuz
      append initrd=centos8/initrd.img ks=http://10.0.0.28/ks/centos8.cfg
    
    label manual
      menu label ^Manual Install CentOS Linux 8.0 
      kernel centos8/vmlinuz
      append initrd=centos8/initrd.img 
      inst.repo=http://10.0.0.28/centos/8/os/x86_64/
    
    label rescue
      menu label ^Rescue a CentOS Linux system
      kernel centos8/vmlinuz
      append initrd=centos8/initrd.img ks=http://10.0.0.28/ks/centos8.cfg
      inst.repo=http://10.0.0.28/centos/8/os/x86_64/ rescue
    
    label local
      menu default
      menu label Boot from ^local drive
      localboot 0xffff
    
    label returntomain
      menu label Return to ^main menu
      menu exit
    
    menu end

制作引导光盘和或U盘实现自动安装

  • 可以将定制安装光盘,并结合kickstart实现基于光盘启动的半自动化安装

实现过程

[root@centos8 ~]#mkdir –pv /data/myiso
[root@centos8 ~]#cp -r /mnt/isolinux /data/myiso/
[root@centos8 ~]#tree /data/myiso/
/data/myiso/
└── isolinux		#在系统光盘的isolinux目录下有和安装相关的文件
   ├── boot.cat		#相当于grub的第一阶段
   ├── boot.msg
   ├── grub.conf
   ├── initrd.img	#ramfs文件
   ├── isolinux.bin	#光盘引导程序,在mkisofs的选项中需要明确给出文件路径,这个文件属于SYSLINUX项目
   ├── isolinux.cfg	#启动菜单的配置文件,当光盘启动后(即运行isolinux.bin),会自动去isolinux.cfg文件
   ├── ldlinux.c32
   ├── libcom32.c32
   ├── libutil.c32
   ├── memtest		#内存检测程序
   ├── splash.png	#光盘启动菜单界面的背景图
   ├── TRANS.TBL
   ├── vesamenu.c32	#是光盘启动后的启动菜单图形界面,也属于SYSLINUX项目,menu.c32提供纯文本的菜单
   └── vmlinuz		#内核映像
1 directory, 14 files

#方法1:应答方件放在ISO文件里
1.cp /root/myks.cfg /data/myiso/	#将指定应答文件拷贝到光盘目录

2.vim /data/myiso/isolinux/isolinux.cfg
  label linux
  menu label ^Auto Install CentOS Linux 8
  kernel vmlinuz
  initrd=initrd.img text ks=cdrom:/myks.cfg #指定应答文件位置

3.dnf -y install mkisofs	#安装制作光盘的程序

#开始制作光盘,将/data/myiso/目录里的内容制作成一个可启动的光盘
4.mkisofs -R -J -T -v --no-emul-boot --boot-load-size 4 --boot-info-table -V "CentOS 8.0 x86_64 boot" -b isolinux/isolinux.bin -c isolinux/boot.cat -o /root/boot.iso /data/myiso/
#注意:以上相对路径都是相对于光盘的根,和工作目录无关

#方法2:应答方件放在http服务器上
label linux
 menu label ^Auto Install CentOS Linux 8
 kernel vmlinuz
 append initrd=initrd.img quiet ks=http://10.0.0.8/ksdir/centos8.cfg #走网络指定应答文件位置
label rescue
 menu label ^Rescue a CentOS Linux system
 kernel vmlinuz
 append initrd=initrd.img inst.repo=http://10.0.0.8/centos/8 rescue quiet #走网络
label local
 menu default
 menu label Boot from ^local drive
 localboot 0xffff