Linux Kickstart 自动化安装 Pre-Boot Execution Environment PXE通过网卡引导得技术 一、PXE启动过程 1 1.从客户端启动系统,选择从网卡启动 2 2.从DHCP服务器中获取到IP地址等信息(192.168.20.2) 3 3.还获取到了tftp server(192.168.20.2)及网络引导程序pxelinux.0 4 4.通过网卡读取到tftp server(/var/lib/tftpboot)上的pxelinux.0,读取到内存中 5 5.在内存中执行引导程序 6 6.读取引导程序得配置文件(/var/lib/tftpboot/pxelinux.cfg/default) 7 7.读取default中得内容 8 vmlinuz 内核 9 initrd.img 驱动程序(一定要匹配OS,不通用) 二、关闭不需要得服务及配置 1 [root@localhost ~]# systemctl stop firewalld && systemctl disable firewalld 2 3 [root@localhost ~]# systemctl stop NetworkManager && systemctl disable NetworkManager 4 5 [root@localhost ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config 6 7 [root@localhost ~]# reboot 三、搭建基础环境 1. 挂载镜像 1 虚拟机挂载对应ISO文件 2 mount /dev/cdrom /mnt 2. 安装服务 1 [root@localhost ~]# yum -y install httpd tftp-server dhcp xinetd 3. 配置dhcp服务 1 [root@localhost ~]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf 2 3 [root@localhost ~]# echo '' > /etc/dhcp/dhcpd.conf 4 5 [root@localhost ~]# vi /etc/dhcp/dhcpd.conf 6 #########################复制以下内容粘贴进去 #################################### 7 option space pxelinux; 8 option pxelinux.magic code 208 = string; 9 option pxelinux.configfile code 209 = text; 10 option pxelinux.pathprefix code 210 = text; 11 option pxelinux.reboottime code 211 = unsigned integer 32; 12 option architecture-type code 93 = unsigned integer 16; 13 14 subnet 192.168.20.0 netmask 255.255.255.0 { #填写地址段和掩码 15 option routers 192.168.20.2; #填写网关 16 range 192.168.20.30 192.168.20.50; #填写dhcp地址池范围 17 18 class "pxeclients" { 19 match if substring (option vendor-class-identifier, 0, 9) = "PXEClient"; 20 next-server 192.168.20.2; #填写tftp服务器地址,填本 机地址 21 22 if option architecture-type = 00:07 { 23 filename "UEFI/shim.efi"; 24 } else { 25 filename "BIOS/pxelinux/pxelinux.0"; 26 } 27 28 } } 29 30 #host client_centos_01 { #此部分是根据客户端mac地 址绑定IP 31 # hardware ethernet 00:50:56:27:70:95; 32 # fixed-address 192.168.70.98; 33 #} 34 35 #######################################END########################### ########## 36 37 [root@localhost ~]# systemctl restart dhcpd && systemctl enable dhcpd 4. 配置TFTP服务 1 [root@localhost ~]# vi 2 ################################################################ 3 service tftp 4 { 5 socket_type /etc/xinetd.d/tftp = dgram 6 protocol = udp 7 wait = yes 8 user = root 9 server = /usr/sbin/in.tftpd 10 server_args = -s /var/lib/tftpboot 11 disable = yes 12 per_source = 11 13 cps = 100 2 flags = IPv4 14 #yes改为no 15 } 16 ################################################################## 17 18 [root@localhost ~]# systemctl restart tftp && systemctl enable tftp 19 20 [root@localhost ~]# systemctl restart xinetd && systemctl enable xinetd 5. 配置http服务 1 在/var/www/html/创建一个目录,存放镜像文件 2 [root@localhost ~]# mkdir /var/www/html/redhat7.9 3 4 复制镜像文件到该目录 5 [root@localhost ~]# cp -a /mnt* /var/www/html/redhat7.9 6 7 [root@localhost ~]# systemctl restart httpd && systemctl enable httpd 四、配置kickstart 1 2 [root@localhost ~]# mkdir /var/lib/tftpboot/{UEFI,BIOS} 3 4 [root@localhost ~]# ls -l /var/lib/tftpboot/ 5 drwxrwxrwx 2 root root 6 Mar 15 01:59 BIOS 6 drwxrwxrwx 2 root root 6 Mar 15 01:59 UEFI 7 8 [root@localhost ~]# mkdir /soft 9 10 [root@localhost ~]# find /mnt -name syslinux*.rpm 11 /mnt/Packages/syslinux-4.05-15.el7.x86_64.rpm 12 13 [root@localhost ~]# find /mnt -name shim*.rpm 14 /mnt/Packages/shim-x64-15-11.el7.x86_64.rpm 15 16 [root@localhost ~]# find /mnt -name grub2-efi*.rpm 17 /mnt/Packages/grub2-efi-x64-2.02-0.87.el7.x86_64.rpm 18 19 [root@localhost ~]# cp -a /mnt/Packages/syslinux-4.05-15.el7.x86_64.rpm /soft/ 20 [root@localhost ~]# cp -a /mnt/Packages/shim-x64-15-11.el7.x86_64.rpm /soft/ 21 [root@localhost ~]# cp -a /mnt/Packages/grub2-efi-x64-2.020.87.el7.x86_64.rpm /soft/ 22 23 [root@localhost ~]# cd /soft/ 24 [root@localhost soft]# rpm2cpio syslinux-4.05-15.el7.x86_64.rpm | cpio dimv 25 [root@localhost soft]# rpm2cpio grub2-efi-x64-2.02-0.87.el7.x86_64.rpm | cpio -dimv 26 [root@localhost soft]# rpm2cpio shim-x64-15-11.el7.x86_64.rpm | cpio dimv 27 28 29 [root@localhost ~]# cp /soft/boot/efi/EFI/redhat/shim.efi /var/lib/tftpboot/UEFI/ 30 [root@localhost ~]# cp /soft/boot/efi/EFI/redhat/grubx64.efi /var/lib/tftpboot/UEFI/ 31 [root@localhost ~]# cp /soft/usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/BIOS/pxelinux 32 33 [root@localhost ~]# mkdir -p /var/lib/tftpboot/BIOS/pxelinux/pxelinux.cfg 34 [root@localhost ~]# mkdir -p /var/lib/tftpboot/UEFI/images/Redhat-7 35 [root@localhost ~]# mkdir -p /var/lib/tftpboot/BIOS/pxelinux/images/Redhat-7 36 37 [root@localhost ~]# cp -a /mnt/isolinux/{initrd.img,vmlinuz} /var/lib/tftpboot/UEFI/images/Redhat-7/ 38 39 [root@localhost ~]# cp -a /mnt/isolinux/{initrd.img,vmlinuz,boot.msg,vesamenu.c32,splash.png} /var/lib/tftpboot/BIOS/pxelinux/images/Redhat-7/ 40 41 [root@localhost ~]# vi /var/lib/tftpboot/BIOS/pxelinux/pxelinux.cfg/defalut 42 #########################复制以下内容粘贴进去 #################################### 43 default images/Redhat-7/vesamenu.c32 44 menu background images/Redhat-7/splash.png 45 timeout 100 46 47 display images/Redhat-7/boot.msg 48 49 label linux 50 menu label ^Install system 51 menu default 52 kernel images/Redhat-7/vmlinuz 53 append initrd=images/Redhat-7/initrd.img ip=dhcp ks=http://192.168.70.51/redhat7.9_BIOS.cfg 54 #######################################END############################### ###### 55 56 57 [root@localhost ~]# vi /var/lib/tftpboot/UEFI/grub.cfg 58 #########################复制以下内容粘贴进去 #################################### 59 set timeout=10 60 menuentry 'Redhat' { 61 linuxefi UEFI/images/Redhat-7/vmlinuz ip=dhcp ks=http://192.168.20.51/redhat7.9_UEFI.cfg 62 initrdefi UEFI/images/Redhat-7/initrd.img 63 } 64 #######################################END############################### ###### 65 66 [root@localhost ~]# cd /var/lib/tftpboot 67 [root@localhost ~]# chmod -R 777 /var/lib/tftpboot/{UEFI,BIOS} 68 69 [root@localhost ~]# systemctl restart dhcpd 70 [root@localhost ~]# systemctl restart httpd 71 [root@localhost ~]# systemctl restart tftp 72 [root@localhost ~]# systemctl restart xinetd 六、配置KS文件 UEFI的KS文件必须要有 part /boot/efi --fstype="efi" --size=200 , 而BIOS的ks文件 不需要 development 将yum仓库名称改为development,kickstat工具可以显示软件包选项 1. KS配置文件示例 1 #platform=x86, AMD64, or Intel EM64T 2 #version=DEVEL 3 # Install OS instead of upgrade 4 install 5 # Keyboard layouts 6 keyboard 'us' 7 # Root password 8 rootpw --iscrypted $1$CzNha2E3$WSRDesrQVgxDEAu6n50Qb0 9 # System language 10 lang en_US 11 # System authorization information 12 auth 13 # Use text mode install 14 text 15 # SELinux configuration 16 selinux --disabled 17 # Do not configure the X Window System 18 skipx --useshadow --passalgo=sha512 19 20 21 # Firewall configuration 22 firewall --disabled 23 # Reboot after installation 24 reboot 25 # System timezone 26 timezone Asia/Shanghai 27 # Use network installation 28 url --url="http://192.168.20.2/redhat7.9" 29 # System bootloader configuration 30 bootloader --location=mbr 31 # Clear the Master Boot Record 32 zerombr 33 # Partition clearing information 34 clearpart --all --initlabel 35 # Disk partitioning information 36 ignoredisk --only-use=sdb 37 part /boot/efi --fstype="efi" --size=200 38 part /boot --fstype="xfs" --size=1024 39 part swap --fstype="swap" --size=2048 40 part / --fstype="xfs" --grow --size=10240 41 42 %packages 43 @^minimal 44 %end 七、ESXI(BIOS&UEFI)未更新 1 [root@kvm ~]# yum -y install httpd tftp-server dhcpd syslinux system- config-kickstart(此包可以不安装) 2 [root@kvm ~]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf 3 option space pxelinux; 4 option pxelinux.magic code 208 = string; 5 option pxelinux.configfile code 209 = text; 6 option pxelinux.pathprefix code 210 = text; 7 option pxelinux.reboottime code 211 = unsigned integer 32; 8 option architecture-type code 93 = unsigned integer 16; 9 10 subnet 192.168.20.0 netmask 255.255.255.0 { 11 option routers 192.168.20.2; 12 range 192.168.20.30 192.168.20.50; 13 14 class "pxeclients" { 15 match if substring (option vendor-class-identifier, 0, 9) = "PXEClient"; 16 next-server 192.168.20.2; 17 18 if option architecture-type = 00:07 { 19 filename "mboot.efi"; 20 } else { 21 filename "pxelinux.0"; 22 } 23 24 } } 25 26 [root@kvm ~]# mkdir /var/lib/tftpboot/pxelinux.cfg 27 28 [root@kvm ~]# mkdir /var/lib/tftpboot/ESXi-6.5-test 29 30 [root@kvm ~]# vi /var/lib/tftpboot/pxelinux.cfg/default 31 DEFAULT install 32 NOHALT 1 33 LABEL install 34 KERNEL ESXi-6.x.x-XXXXXX/mboot.c32 35 APPEND -c ESXi-6.x.x-XXXXXX/boot.cfg 36 IPAPPEND 2 37 38 #如果希望所有主机都默认引导此 ESXi 安装程序,请将文件命名为 default。 39 # 如果只希望特定主机使用此文件进行引导,请使用目标主机的 MAC 地址 (01- mac_address_of_target_ESXi_host) 来命名此文件,例如 01-23-45-67-89-0a-bc。 40 41 42 #新版本的 mboot.efi 通常可以引导旧版本的 ESXi,但旧版本的 mboot.efi 可能无法引导新 版本的 ESXi。如果您计划配置不同的主机以引导不同版本的 ESXi 安装程序,请使用最新版本中 的 mboot.efi 43 44 [root@kvm ~]# cp /var/www/html/esxi6.5/efi/boot/bootx64.efi /var/lib/tftpboot/mboot.efi 45 46 [root@kvm ~]# cp -a /var/www/html/esxi6.5/* /var/lib/tftpboot/ESXi-6.5test/ 47 48 [root@kvm ~]# cp /var/lib/tftpboot/ESXi-6.5-test/boot.cfg /var/lib/tftpboot 49 #添加以下行 50 prefix=ESXi-6.x.x-xxxxxx 51 52 #如果 kernel= 和 modules= 行中的文件名以正斜杠 (/) 字符开头,请删除该字符 53 [root@kvm ~]# sed -i "s/\///g" boot.cfg 54 55 #在 boot.cfg 文件中内核命令后的一行添加 kernelopt 选项以指定安装脚本的位置 56 kernelopt=ks=http://XXX.XXX.XXX.XXX/esxi_ksFiles/ks.cfg ESXI——KS配置文件 1 accepteula 2 install --firstdisk --overwritevmfs 3 rootpw rootR007 4 reboot 5 6 network --bootproto=static --ip=192.168.20.2 --netmask=255.255.255.0 -gateway=192.168.20.2 --hostname=test --nameserver=114.114.114.114 - 7 -addvmportgroup=1 8 9 %firstboot --interpreter=busybox 10 vim-cmd hostsvc/enable_ssh 11 vim-cmd hostsvc/start_ssh 12 vim-cmd hostsvc/enable_esx_shell 13 vim-cmd hostsvc/start_esx_shell ESXI——KS配置文件(官方模板) 1 # 2 # Sample scripted installation file 3 # 4 5 # Accept the VMware End User License Agreement 6 vmaccepteula 7 8 # Set the root password for the DCUI and Tech Support Mode 9 rootpw myp@ssw0rd 10 11 # Install on the first local disk available on machine 12 install --firstdisk --overwritevmfs 13 14 # Set the network to DHCP on the first network adapter 15 network --bootproto=dhcp --device=vmnic0 16 17 # A sample post-install script 18 %post --interpreter=python --ignorefailure=true 19 import time 20 stampFile = open('/finished.stamp', mode='w') 21 stampFile.write( time.asctime() )