利用PXE实现自动化系统部署
Posted lidanyue
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用PXE实现自动化系统部署相关的知识,希望对你有一定的参考价值。
本实验实现在centos8系统上利用PXE自动化安装Centos6,7,8三个系统
(1) 安装前准备
关闭防火墙和SELINUX,DHCP服务器静态IP
网络要求:关闭Vmware软件中的DHCP服务,基于NAT模式
(2) 安装相关软件包并启动
[root@centos8 ~]#dnf -y install dhcp-server tftp-server httpd syslinux-nonlinux
[root@centos8 ~]#systemctl enable --now httpd tftp dhcpd
(3)配置DHCP服务
[root@centos8 ~]#cp /usr/share/doc/dhcp-server/dhcpd.conf.example /etc/dhcp/dhcpd.conf
[root@centos8 ~]#vim /etc/dhcp/dhcpd.conf
1 option domain-name "example.com";
2 option domain-name-servers 180.76.76.76,223.6.6.6;
3 default-lease-time 600;
4 max-lease-time 7200;
5 log-facility local7;
6 subnet 10.0.0.0 netmask 255.255.255.0 {
#分配的IP范围
7 range 10.0.0.100 10.0.0.200;
#路由网关
8 option routers 10.0.0.2;
#本机IP
9 next-server 10.0.0.8;
10 filename "pxelinux.0";
11 }
[root@centos8 ~]#systemctl start dhcpd
(4)准备yum 源和相关目录(将centos8系统上安装三个光盘,分别对应相应的ios文件)
[root@centos8 ~]#mkdir -pv /var/www/html/centos/{6,7,8}/os/x86_64/
#centos6的为sr0,centos7的为sr2,centos8的为sr1
[root@centos8 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 100G 0 part /
├─sda3 8:3 0 50G 0 part /data
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 4G 0 part [SWAP]
sr0 11:0 1 3.7G 0 rom
sr1 11:1 1 7G 0 rom
sr2 11:2 1 10.3G 0 rom
[root@centos8 ~]#mount /dev/sr0 /var/www/html/centos/6/os/x86_64/
[root@centos8 ~]#mount /dev/sr1 /var/www/html/centos/8/os/x86_64/
[root@centos8 ~]#mount /dev/sr2 /var/www/html/centos/7/os/x86_64/
(5)准备kickstart文件
[root@centos8 ~]$mkdir /var/www/html/ks/
[root@centos8 ~]$cd /var/www/html/ks/
[root@centos8 ks]$vim centos6.cfg
1 install
2 text
3 reboot
4 url --url=http://10.0.0.8/centos/6/os/x86_64/
5 lang en_US.UTF-8
6 keyboard us
7 network --onboot yes --device eth0 --bootproto dhcp --noipv6
8 rootpw --iscrypted $6$j9YhzDUnQVnxaAk8$qv7rkMcPAEbV5yvwsP666DXWYadd3jYjkA9fpxAo9qYotjGGBUclCGoP1TRvgHBpqgc5n0RypMsPTQnVDcpO01
9 firewall --disabled
10 authconfig --enableshadow --passalgo=sha512
11 selinux --disabled
12 timezone Asia/Shanghai
13 bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
14 zerombr
15 clearpart --all --initlabel
16 part /boot --fstype=ext4 --size=1024
17 part / --fstype=ext4 --size=50000
18 part /data --fstype=ext4 --size=30000
19 part swap --size=2048
20 %packages
21 @core
22 @server-policy
23 @workstation-policy
24 autofs
25 vim-enhanced
26 %end
27 %post
28 useradd wang
29 echo magedu | passwd --stdin wang &> /dev/null
30 mkdir /etc/yum.repos.d/bak
31 mv /etc/yum.repos.d/* /etc/yum.repos.d/bak
32 cat > /etc/yum.repos.d/base.repo <<EOF
33 [base]
34 name=base
35 baseurl=file:///misc/cd
36 gpgcheck=0
37 EOF
38 %end
[root@centos8 ks]$vim centos7.cfg
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 --plaintext magedu
9 # System language
10 lang en_US
11 # System authorization information
12 auth --useshadow --passalgo=sha512
13 # Use text mode install
14 text
15 firstboot --disable
16 # SELinux configuration
17 selinux --disabled
18
19
20 # Firewall configuration
21 firewall --disabled
22 # Network information
23 network --bootproto=static --device=eth0 --ip=10.0.0.123 --netmask=255.255.255.0
24 # Reboot after installation
25 reboot
26 # System timezone
27 timezone Asia/Shanghai
28 # Use network installation
29 url --url="http://10.0.0.8/centos/7/os/x86_64"
30 # System bootloader configuration
31 bootloader --append="net.ifnames=0" --location=mbr
32 # Partition clearing information
33 clearpart --all
34 # Disk partitioning information
35 part / --fstype="xfs" --size=100000
36
37 %post
38 useradd mage
39 echo magedu |passwd --stdin mage
40 mkdir /etc/yum.repos.d/backup
41 mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup
42 cat > /etc/yum.repos.d/base.repo <<EOF
43 [base]
44 baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
45 http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
46 http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/
47 gpgcheck=1
48 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
49 EOF
50 mkdir /root/.ssh -m 700
51 cat > /root/.ssh/authorized_keys <<EOF
52 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC4fLgICiPzngrZ3J55lqfGH/yAifTe39nHi/9Z/2kC0WLYyyLvQGmyXP2I+y0sIaIpqvGHSglrXpsMszu2YoO72ydm+baSCazoH3pBUKvNa34+nukm9kUXsuNchFSzJGt6yOMEZNfvdenFYAAkRo5 /hYNy/jgaGbCSejxxBjx1oPB1ik7DQUXuZJKeNQEHYuQLMeNOatR7eQSWPH/e6vSgu6kERtn4QKvIa7VTt1hL8oE2VaxlmtuQEEhuuU1TELn5atkoGX2bSCxPjJxfBLqLKA75DGmaIKZl849JCOsQH/4nSGnGsF1BDrV/JqfeTsDBlt3/DKIlNZ+5/B VBg4UPgAnKWFrrE52ub3L87SsqteES5Zh1K6yv3qA4Xagf1UTKFA7KhGZA2y+AwUTglQMeoYB1CNDQQjf35RDqjW25FD2IzqFurnVSkVEOU7u9mdN6v6KVZjAadOrFhXRoWtsJnEbwD/Dh1g8RWt55xIE0f4RekLNsmfgQxp3MiZSv8XU= root@cen tos8.localdomain
53 EOF
54 chmod 600 /root/.ssh/authorized_keys
55 %end
56
57 %packages
58 @web-server
59
60 %end
[root@centos8 ks]$vim centos8.cfg
1 ignoredisk --only-use=sda
2 zerombr
3 text
4 reboot
5 clearpart --all --initlabel
6 selinux --disabled
7 firewall --disabled
8 url --url=http://10.0.0.8/centos/8/os/x86_64/
9 keyboard --vckeymap=us --xlayouts=‘us‘
10 lang en_US.UTF-8
11 network --bootproto=dhcp --device=ens160 --ipv6=auto --activate
12 network --hostname=centos8.magedu.com
13 rootpw --iscrypted $6$j9YhzDUnQVnxaAk8$qv7rkMcPAEbV5yvwsP666DXWYadd3jYjkA9fpxAo9qYotjGGBUclCGoP1TRvgHBpqgc5n0RypMsPTQnVDcpO01
14 firstboot --enable
15 skipx
16 services --disabled="chronyd"
17 timezone Asia/Shanghai --isUtc --nontp
18 user --name=wang --password=6oUfb/02CWfLb5l8f$sgEZeR7c7DpqfpmFDH6huSmDbW1XQNR4qKl2EPns.gOXqlnAIgv9pTogtFVaDtEpMOC.SWXKYqxfVtd9MCwxb1 --iscrypted --gecos="wang"
19 part / --fstype="xfs" --ondisk=sda --size=102400
20 part /data --fstype="xfs" --ondisk=sda --size=51200
21 part swap --fstype="swap" --ondisk=sda --size=2048
22 part /boot --fstype="ext4" --ondisk=sda --size=1024
23 %packages
24 @^minimal-environment
25 kexec-tools
26 %end
27 %addon com_redhat_kdump --enable --reserve-mb=‘auto‘
28 %end
29 %anaconda
30 pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
31 pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
32 pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
33 %end
34
35 %post
36 useradd mage
37 echo magedu | passwd --stdin mage &> /dev/null
38 %end
(6) 准备PXE启动相关文件
[root@centos8 ~]#mkdir /var/lib/tftpboot/centos{6,7,8}
#准备CentOS6,7,8各自的内核相关文件
[root@centos8 ~]#cp /var/www/html/centos/6/os/x86_64/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos6
[root@centos8 ~]#cp /var/www/html/centos/7/os/x86_64/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos7
[root@centos8 ~]#cp /var/www/html/centos/8/os/x86_64/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos8
[root@centos8 ~]#cp /usr/share/syslinux/{pxelinux.0,menu.c32} /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/
#生成安装菜单文件
[root@centos8 ~]#mkdir /var/lib/tftpboot/pxelinux.cfg/
[root@centos8 ~]#cp /var/www/html/centos/8/os/x86_64/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
#最终目录结构如下
[root@centos8 ~]#tree /var/lib/tftpboot
.
├── centos6
│ ├── initrd.img
│ └── vmlinuz
├── centos7
│ ├── initrd.img
│ └── vmlinuz
├── centos8
│ ├── initrd.img
│ └── vmlinuz
├── ldlinux.c32
├── libcom32.c32
├── libutil.c32
├── menu.c32
├── pxelinux.0
└── pxelinux.cfg
└── default
4 directories, 12 files
(7) 准备启动菜单文件
[root@centos8 ~]#vim /var/lib/tftpboot/pxelinux.cfg/default
default menu.c32
timeout 600
menu title Install CentOS Linux
label linux8
menu label Auto Install CentOS Linux ^8
kernel centos8/vmlinuz
append initrd=centos8/initrd.img ks=http://10.0.0.8/ks/centos8.cfg
label linux7
menu label Auto Install CentOS Linux ^7
kernel centos7/vmlinuz
append initrd=centos7/initrd.img ks=http://10.0.0.8/ks/centos7.cfg
label linux6
menu label Auto Install CentOS Linux ^6
kernel centos6/vmlinuz
append initrd=centos6/initrd.img ks=http://10.0.0.8/ks/centos6.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.8/centos/8/os/x86_64/
label rescue
menu label ^Rescue a CentOS Linux system 8
kernel centos8/vmlinuz
append initrd=centos8/initrd.img
inst.repo=http://10.0.0.8/centos/8/os/x86_64/ rescue
label local
menu default
menu label Boot from ^local drive
localboot 0xffff
最后自己试一下是否能够安装就好啦
以上是关于利用PXE实现自动化系统部署的主要内容,如果未能解决你的问题,请参考以下文章
运维部署自动化实践(二)PXE+Preseed自动安装Ubuntu16.04 server
运维部署自动化实践(一)利用PXE远程自动化安装Ubuntu16.04 server