PXE自動化安裝centos6和centos7

PXE(Preboot Excution Environment): 預啓動執行環境,Intel公司研發,基於Client/Server的網絡模式,支持遠程主機經過網絡從遠端服務器下載映像,並由此支持經過網絡啓動操做系統。 PXE能夠引導和安裝Windows,linux等多種操做系統
PXE工做原理:
  客戶端向PXE服務器上的dhcp服務發送IP地址請求消息,dhcp服務檢測客戶端請求是否合法,主要是檢測客戶機的網卡MAC地址,若是合法則分配給客戶端IP地址,同時將啓動文件名pxelinux.0和啓動文件位置信息一併傳送給客戶端。
  客戶端向PXE 服務器上的tftp服務發送獲取啓動文件pxelinux.0請求消息,tftp服務在接收到消息以後向客戶端發送pxelinux.0大小信息,詢問客戶端是否贊成使用,當tftp服務收到客戶端發回的贊成使用信息以後,正式向客戶端發送pxelinux.0文件。
  客戶端執行接收到的pxelinux.0文件。
  客戶端向tftp服務器發送針對本機的配置信息文件(在tftp 服務器的pxelinux.cfg目錄下),tftp服務器將配置文件發回客戶端,繼而客戶端根據配置文件執行後續操做。
  客戶端向tftp服務器發送Linux內核請求信息,tftp服務器在接收到消息以後將內核文件發送給客戶端
  客戶端向tftp服務器發送根文件請求信息,tftp服務器接收到消息以後返回Linux根文件系統給客戶端。
  客戶端啓動Linux內核,下載安裝源文件,讀取自動化安裝腳本html

環境準備:  linux

  關閉防火牆
  systemctl stop firewalld.service
  systemctl disable firewalld.serviceredis

  systemctl stop iptables
  systemctl disable iptablesexpress

  關閉selinux
  setenforce 0 
  vim /etc/selinux/config修改SELINUX=disabledvim

  設置靜態IPcentos

  vim /etc/sysconfig/network-scripts/ifcfg-ens33服務器

 1 TYPE=Ethernet  2 BOOTPROTO=static #網絡協議  3 NAME=ens33  4 UUID=c7c0aaf8-2a87-4217-819b-fdbc1d08a7e8  5 DEVICE=ens33  6 ONBOOT=yes #開機啓動  7 DNS1=114.114.114.114
 8 DNS2=8.8.8.8
 9 IPADDR=192.168.121.10
10 NETMASK=255.255.255.0
11 GATEWAY=192.168.121.2

安裝相關軟件包網絡

  yum install -y dhcp tftp-server httpd syslinuxapp

啓動httpd服務
  systemctl enable httpd
  systemctl start httpdless

cp /etc/xinetd.d/tftp /etc/xinetd.d/tftp.bak

vim /etc/xinetd.d/tftp

 1 service tftp  2 {  3     socket_type             = dgram  4     protocol                = udp  5     wait                    = yes  6     user                    = root  7     server                  = /usr/sbin/in.tftpd  8     server_args             = -s /var/lib/tftpboot  9     disable                 = no 10     per_source              = 11 
11     cps                     = 100 2 
12     flags                   = IPv4 13 }

啓動tftpd服務
  systemctl enable tftp.socket
  systemctl start tftp.socket

cp -f /usr/share/doc/dhcp*/dhcpd.conf.example  /etc/dhcp/dhcpd.conf.bak

vim /etc/dhcp/dhcpd.conf

 1 ddns-update-style interim;  2 ignore client-updates;  3 subnet 192.168.121.0 netmask 255.255.255.0 {  4 
 5     option routers                  192.168.121.2;  6     option subnet-mask              255.255.255.0;  7 
 8     filename "pxelinux.0";  9     next-server 192.168.121.10; 10     range dynamic-bootp 192.168.121.11 192.168.121.100; 11     default-lease-time 21600; 12     max-lease-time 43200; 13 }

啓動dcpd服務

  systemctl enable dhcpd
  systemctl start dhcpd

製做centos7和centos6的安裝源文件

  mkdir -pv /var/www/html/centos/{6,7}
  mkdir -pv /media/cdrom{6,7}

  mount /dev/sr0 /media/cdrom6/
  cp /media/cdrom6 /* /var/www/html/centos/6/ -r
  mount /dev/sr1 /media/cdrom7/
  cp  /media/cdrom7/* /var/www/html/centos/7/ -r

製做centos6和centos7的kickstart文件 

  修改anaconda.cfg文件,此文件會在每次裝完系統後生成於/root/目錄下.
  使用system-config-kickstart命令生成(須要安裝X Windows 和Desktop)
    yum groupinstall "X Window System"
    yum groupinstall Desktop

  mkdir /var/www/html/ksdir/
  vim /var/www/html/ksdir/ks7-1.cfg

 1 #version=DEVEL  2 # System authorization information  3 auth --enableshadow --passalgo=sha512  4 # Use CDROM installation media  5 url --url=http://192.168.121.10/centos/7
 6 # Use graphical install
 7 graphical  8 # Run the Setup Agent on first boot  9 firstboot --enable 10 ignoredisk --only-use=sda 11 # Keyboard layouts 12 keyboard --vckeymap=us --xlayouts='us'
13 # System language 14 lang en_US.UTF-8
15 
16 # Network information 17 network  --bootproto=dhcp --device=ens33 --onboot=on --ipv6=auto --activate 18 network  --hostname=localhost.localdomain 19 
20 # Root password 21 rootpw --iscrypted $6$SNhR8RU2Euwg1ydI$gPITLqKSRMfm11hIyaIqPbmRo579k7hhpsOp4cQRhBO/dEH8vWt2b4upxYiQEXKzgzS55FccpqcWiz/7/LL4w. 22 # System timezone 23 timezone Asia/Shanghai --isUtc --ntpservers=0.centos.pool.ntp.org 24 # System bootloader configuration 25 bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda 26 # Partition clearing information 27 zerombr 28 clearpart --all 29 reboot 30 # Disk partitioning information 31 part /boot --fstype="xfs" --ondisk=sda --size=200
32 part swap  --fstype="swap" --ondisk=sda --size=2048 
33 part /  --fstype="xfs" --ondisk=sda --grow --size=1 
34 
35 
36 %packages 37 @^minimal 38 @core 39 kexec-tools 40 
41 %end 42 
43 %addon com_redhat_kdump --enable --reserve-mb='auto'
44 
45 %end 46 
47 %anaconda 48 pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty 49 pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok 50 pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty 51 %end 52 
53 %post 54 systemctl restart network.service 55 yum install vim wget net-tools  xorg-x11-xauth    screen -y 56 cd /etc/yum.repos.d/
57 rm -rf *
58 wget -c http://mirrors.163.com/.help/CentOS7-Base-163.repo
59 systemctl disable firewalld.service 60 cp /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-ens33.bak 61 sed -i 's/dhcp/static/g' /etc/sysconfig/network-scripts/ifcfg-ens33.bak 62 echo 'IPADDR=192.168.121.121'>> /etc/sysconfig/network-scripts/ifcfg-ens33.bak 63 echo 'NETMASK=255.255.255.0'>> /etc/sysconfig/network-scripts/ifcfg-ens33.bak 64 echo 'GATEWAY=192.168.121.2'>> /etc/sysconfig/network-scripts/ifcfg-ens33.bak 65 echo 'DNS1=114.114.114.114' >> /etc/sysconfig/network-scripts/ifcfg-ens33.bak 66 sed -i 's/enforcing/disabled/g' /etc/selinux/config 67 %end

vim /var/www/html/ksdir/ks6-1.cfg

 1 #version=DEVEL  2 install
 3 url --url=http://192.168.121.10/centos/6
 4 lang en_US.UTF-8
 5 keyboard us  6 network --onboot yes --device eth0 --bootproto dhcp --noipv6  7 rootpw  --iscrypted $6$55ArpbZ9EoUhTKt5$pYnhDc2zbpdOt1aR4rIf7keHxy0aZ7hi/C.1XmLV5IOpYgkv5zTC61trhk3oDfDocracX6CtpBx2kZ7frHvP0.  8 firewall --service=ssh
 9 authconfig --enableshadow --passalgo=sha512 10 selinux --enforcing 11 timezone Asia/Shanghai 12 bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
13 # The following is the partition information you requested 14 # Note that any partitions you deleted are not expressed 15 # here so unless you clear all partitions first, this is 16 # not guaranteed to work 17 zerombr 18 clearpart --all 19 reboot 20 part /boot --fstype=ext4 --size=200
21 part swap --size=2000
22 part / --fstype=ext4 --grow --size=200
23 
24 #repo --name="CentOS"  --baseurl=cdrom:sr0 --cost=100
25 
26 %packages 27 @core 28 %end 29 
30 %post 31 service network restart 32 yum install vim wget net-tools -y 33 cd /etc/yum.repos.d/
34 rm -rf *
35 wget -c http://mirrors.163.com/.help/CentOS6-Base-163.repo
36 chkconfig iptables off 37 cp /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-ens33.bak 38 sed -i 's/dhcp/static/g' /etc/sysconfig/network-scripts/ifcfg-ens33.bak 39 echo 'IPADDR=192.168.121.121'>> /etc/sysconfig/network-scripts/ifcfg-ens33.bak 40 echo 'NETMASK=255.255.255.0'>> /etc/sysconfig/network-scripts/ifcfg-ens33.bak 41 echo 'GATEWAY=192.168.121.2'>> /etc/sysconfig/network-scripts/ifcfg-ens33.bak 42 echo 'DNS1=114.114.114.114' >> /etc/sysconfig/network-scripts/ifcfg-ens33.bak 43 sed -i 's/enforcing/disabled/g' /etc/selinux/config 44 %end

 

檢驗ks文件

  yum install system-config-kickstart -y
  ksvalidator /var/www/html/ksdir/ks7-1.cfg
  ksvalidator /var/www/html/ksdir/ks6-1.cfg

製做PXE相關文件

  mkdir /var/lib/tftpboot/centos{6,7}
  mkdir /var/lib/tftpboot/pxelinux.cfg/

  cp /usr/share/syslinux/{pxelinux.0,menu.c32} /var/lib/tftpboot/
  cp /var/www/html/centos/7/isolinux/{initrd.img,vmlinuz} /var/lib/tftpboot/centos7
  cp /var/www/html/centos/6/isolinux/{initrd.img,vmlinuz} /var/lib/tftpboot/centos6
  cp /var/www/html/centos/7/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default

tree /var/lib/tftpboot/
  /var/lib/tftpboot/
  ├── centos6
  │ ├── initrd.img
  │ └── vmlinuz
  ├── centos7
  │ ├── initrd.img #僞文件系統文件
  │ └── vmlinuz #內核文件
  ├── menu.c32 #純文本菜單
  ├── pxelinux.0 #引導文件,至關於grub。
  └── pxelinux.cfg
   └── default #啓動菜單文件

製做啓動菜單
vim /var/lib/tftpboot/pxelinux.cfg/default

 1 default menu.c32  2 timeout 600
 3 menu title CentOS Linux PXE Install  4 
 5 label centos7 #自動安裝centos7  6       menu label Auto Install CentOS Linux ^7
 7  menu default #默認啓動項  8       kernel centos7/vmlinuz  9       append initrd=centos7/initrd.img ks=http://192.168.121.10/ksdir/ks7-1.cfg #應答文件所在路徑
10 
11 label centos6 #自動安裝centos6 12       menu label Auto Install CentOS Linux ^6
13       kernel centos6/vmlinuz 14       append initrd=centos6/initrd.img ks=http://192.168.121.10/ksdir/ks6-1.cfg
15 
16 label manual7 #手動安裝centos7 17       menu label ^Manual Install CentOS Linux 7
18       kernel centos7/vmlinuz 19       append initrd=centos7/initrd.img inst.repo=http://192.168.121.10/centos/7
20 
21 label manual6 #手動安裝centos6 22       menu label Manual ^Install CentOS Linux 6
23       kernel centos6/vmlinuz 24       append initrd=centos6/initrd.img inst.repo=http://192.168.121.10/centos/6
25 
26 label local #本地硬盤啓動 27      menu label Boot from ^local drive 28      localboot 0xffff
29 menu end
相關文章
相關標籤/搜索