1. 程式人生 > >實驗:實現PXE的網絡自動化安裝

實驗:實現PXE的網絡自動化安裝

grub 應答文件 光盤 listen ransac 客戶端 UNC red dir

PXE(Preboot Execution Environment):預啟動執行環境,PXE是由Intel設計的協議,它可以使計算機通過網絡而不是從本地硬盤、光驅等設備啟動。 基於Client/Server的網絡模式,支持遠程主機通過網絡從遠端服務器下載映像,並由此支持通過網絡啟動操作系統PXE可以引導和安裝Windows,linux等多種操作系統。

目標:通過網卡能夠下載安裝操作系統的安裝文件,安裝包,能夠讓主機不拿光盤,u盤,只拿網卡來引導,進而能夠讓它自動的啟動起來,安裝操作系統。只要主機網線插上,進行簡單的配置,就可以利用PXE芯片來實現自動安裝。
前提準備:centos6,7 IP:10.0.0.200/24 獨立的非橋接的網段:vmnet6 ,關閉SELinux策略,firewall防火墻

1、我們把httpd服務開啟,並把必要的安裝包安裝好
yum install dhcp tftp-server httpd syslinux
systemctl enable dhcpd httpd tftp.socket
systemctl start httpd
[root@centos7 tftpboot]# ss -ntlu
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
udp UNCONN 0 0 :5353

:
udp UNCONN 0 0
:49063 :
udp UNCONN 0 0 192.168.122.1:53 :
udp UNCONN 0 0 :67 :
udp UNCONN 0 0
%virbr0:67 :

udp UNCONN 0 0 :68 :
udp UNCONN 0 0
:111 :
udp UNCONN 0 0 :893 :
udp UNCONN 0 0 :::69 :::

udp UNCONN 0 0 :::111 :::
udp UNCONN 0 0 :::893 :::

tcp LISTEN 0 128 :111 :
tcp LISTEN 0 128
:6000 :
tcp LISTEN 0 5 192.168.122.1:53 :
tcp LISTEN 0 128 :22 :
tcp LISTEN 0 128 127.0.0.1:631
:
tcp LISTEN 0 100 127.0.0.1:25
:
tcp LISTEN 0 128 127.0.0.1:6010
:
tcp LISTEN 0 128 127.0.0.1:6015
:
tcp LISTEN 0 128 127.0.0.1:6016
:
tcp LISTEN 0 128 :::111 :::

tcp LISTEN 0 128 :::80 :::
tcp LISTEN 0 128 :::6000 :::

tcp LISTEN 0 128 :::22 :::
tcp LISTEN 0 128 ::1:631 :::

tcp LISTEN 0 100 ::1:25 :::
tcp LISTEN 0 128 ::1:6010 :::

tcp LISTEN 0 128 ::1:6015 :::
tcp LISTEN 0 128 ::1:6016 :::

這一項是查看到69,67,80端口已在運行,可以進行下一步操作,
2、我們要讓dhcp告訴客戶端tftp是誰,從tftp上下載什麽文件,要添加兩項內容:
[root@centos7 tftpboot]# vim /etc/dhcp/dhcpd.conf

Fixed IP addresses can also be specified for hosts. These addresses

should not also be listed as being available for dynamic assignment.

Hosts for which fixed IP addresses have been specified can boot using

BOOTP or DHCP. Hosts for which no fixed address is specified can only

be booted with DHCP, unless there is an address range on the subnet

to which a BOOTP client is connected which has the dynamic-bootp flag

set.

host fantasia {
hardware ethernet 08:00:07:26:c0:a5;
fixed-address fantasia.fugue.com;
}
host centos6 {
hardware ethernet 00:0c:29:d1:ac:34;
log-facility local7;

No service will be given on this subnet, but declaring it helps the

DHCP server to understand the network topology.

subnet 10.0.0.0 netmask 255.255.255.0 {
range 10.0.0.10 10.0.0.100;
option routers 10.0.0.1;
filename "pxelinux.0";
next-server 10.0.0.200;
}

This is a very basic subnet declaration.

subnet 10.254.239.0 netmask 255.255.255.224 {
range 10.254.239.10 10.254.239.20;
option routers rtr-239-0-1.example.org, rtr-239-0-2.example.org;
}

This declaration allows BOOTP clients to get dynamic addresses,

which we don‘t really recommend.

filename "pxelinux.0";
next-server 10.0.0.200; next-server就是說的tftp,後加自己的地址,

3、可以這樣搜索出想要下載的安裝包
[root@centos7 tftpboot]# yum search pxelinux
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
=============================== Matched: pxelinux ===============================
syslinux.x86_64 : Simple kernel loader which boots from a FAT filesystem
syslinux-extlinux.x86_64 : The EXTLINUX bootloader, for booting the local system.
syslinux-tftpboot.noarch : SYSLINUX modules in /var/lib/tftpboot, available for
: network booting
4、 [root@centos7 tftpboot]# yum install syslinux
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package syslinux.x86_64 0:4.05-15.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================
Package Arch Version Repository Size

Installing:
syslinux x86_64 4.05-15.el7 development 990 k

Transaction Summary

Install 1 Package

Total download size: 990 k
Installed size: 2.3 M
Is this ok [y/d/N]: y
Downloading packages:
syslinux-4.05-15.el7.x86_64.rpm | 990 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : syslinux-4.05-15.el7.x86_64 1/1
Verifying : syslinux-4.05-15.el7.x86_64 1/1

Installed:
syslinux.x86_64 0:4.05-15.el7

Complete!
[root@centos7 tftpboot]# rpm -ql syslinux
/usr/bin/gethostip
/usr/bin/isohybrid
/usr/bin/memdiskfind
/usr/bin/syslinux
/usr/share/syslinux/pwd.c32
/usr/share/syslinux/pxechain.com
/usr/share/syslinux/pxelinux.0
/usr/share/syslinux/reboot.c32
/usr/share/syslinux/rosh.c32
/usr/share/syslinux/sanboot.c32
可以看到安裝包中含有“ /syslinux/pxelinux.0 ”包,

4、[root@centos7 tftpboot]# vim /var/www/html/ks/ks7-mini.cfg
查看一下應答文件的url路徑是否有誤,
#version=DEVEL

System authorization information

auth --enableshadow --passalgo=sha512

Use CDROM installation media

url --url=http://192.168.141.130/centos/7/os/x86_64/

Use graphical install

text

Run the Setup Agent on first boot

firstboot --enable
ignoredisk --only-use=sda

Keyboard layouts

keyboard --vckeymap=us --xlayouts=‘us‘

System language

lang en_US.UTF-8

Network information

network --bootproto=dhcp --device=ens33 --onboot=on --ipv6=auto --activate
network --hostname=centos7.localdomain

Root password

rootpw --iscrypted $6$VYubI8zpV4R/R1M7$.vVqobGDm7t9YAHH924tWzNjqrKGAt4JiBMBu2iWwpIuW2RuupH5OgA1JbEgmZgh8oOSFlY6FeQMVBhN4yM8E0

System services

firewall --disabled
selinux --disabled
services --disabled="chronyd"

System timezone

timezone Asia/Shanghai --isUtc --nontp
"/var/www/html/ks/ks7-mini.cfg" 47L, 1472C

5、把安裝包中的文件拷貝到當前目錄,
[root@centos7 tftpboot]# cp /usr/share/syslinux/pxelinux.0 .
[root@centos7 tftpboot]# ls
f2 pxelinux.0

6、mkdir /var/www/html/centos/7 -pv
添加開機自動掛載光盤
vim /etc/fstab
/dev/sr1 /var/www/html/centos/7 iso9660 default 0 0
掛載磁盤
mount dev/sr1 /var/www/html/centos/7
mount /dev/sr0 /mnt
重新讀取磁盤掛載 mount -a
[root@centos7 ~]# 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 50G 0 part /
├─sda3 8:3 0 2G 0 part [SWAP]
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 10G 0 part /data
sdb 8:16 0 120G 0 disk
├─sdb1 8:17 0 40G 0 part /madedu_data
├─sdb2 8:18 0 50G 0 part
└─sdb3 8:19 0 30G 0 part
sr0 11:0 1 10G 0 rom /mnt
sr1 11:1 1 3.7G 0 rom /var/www/html/centos/7

7、[root@centos7 ~]# cd /var/www/html/centos/7/
[root@centos7 7]# ls
CentOS_BuildTag isolinux RPM-GPG-KEY-CentOS-Debug-6
EFI Packages RPM-GPG-KEY-CentOS-Security-6
EULA RELEASE-NOTES-en-US.html RPM-GPG-KEY-CentOS-Testing-6
GPL repodata TRANS.TBL
images RPM-GPG-KEY-CentOS-6
[root@centos7 7]# cd isolinux/
[root@centos7 isolinux]# ls
boot.cat grub.conf isolinux.bin memtest TRANS.TBL vmlinuz
boot.msg initrd.img isolinux.cfg splash.jpg vesamenu.c32

8、我們把所要用到的文件有選擇的拷貝過來,不需要的不用拷貝
[root@centos7 isolinux]# cp initrd.img vmlinuz /var/lib/tftpboot/
9、我們把“ isolinux.cfg ”文件也拷貝過來,它是開機菜單,不能直接放在/var/lib/tftpboot/下,要新建一個文件夾:“ mkdir /var/lib/tftpboot/pxelinux.cfg/ ” 拷貝過去還要更名為“ default ”
[root@centos7 isolinux]# ls
boot.cat grub.conf isolinux.bin memtest TRANS.TBL vmlinuz
boot.msg initrd.img isolinux.cfg splash.jpg vesamenu.c32
[root@centos7 isolinux]# mkdir /var/lib/tftpboot/pxelinux.cfg/
[root@centos7 isolinux]# cp isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default

10、 [root@centos7 isolinux]# cd /var/lib/tftpboot/
[root@centos7 tftpboot]# tree
.
├── initrd.img
├── pxelinux.0
├── pxelinux.cfg
│?? └── default
└── vmlinuz

1 directory, 4 files

11、[root@centos7 tftpboot]# rpm -ql syslinux
/usr/bin/gethostip
/usr/bin/isohybrid
/usr/bin/memdiskfind
/usr/share/syslinux/meminfo.c32
/usr/share/syslinux/menu.c32
/usr/share/syslinux/pcitest.c32
其中,/usr/share/syslinux/menu.c3該包也是一個簡潔的開機菜單,開機藍色背景界面,我們把它拷貝過來,
[root@centos7 tftpboot]# cp /usr/share/syslinux/menu.c32 .
[root@centos7 tftpboot]# ls
initrd.img menu.c32 pxelinux.0 pxelinux.cfg vmlinuz
[root@centos7 tftpboot]# tree
.
├── initrd.img
├── menu.c32
├── pxelinux.0
├── pxelinux.cfg
│?? └── default
└── vmlinuz

1 directory, 5 files
有了這幾個基本項,我們就可以實現安裝了,但是有些地方還要改,
[root@centos7 tftpboot]# vim pxelinux.cfg/default

default menu.c32
#prompt 1
timeout 600

menu title Welcome to www.magedu.com

label mini
menu label ^Install an Mini Centos7.6 system
Kernel vmlinuz
append initrd=initrd.img ks=http://192.168.141.130/ks/centos7-mini.cfg
label desktop
menu label Install an ^Desktop Centos7.6 system
Kernel vmlinuz
append initrd=initrd.img ks=http://192.168.141.130/ks/centos7-desktop.cfg
label local
menu label Boot from ^local drive
menu default
localboot 0xffff
以上是我們修改過的配置文件,配置了三個文件,分別是最小化安裝,桌面安裝,本地安裝, 查看其權限:
[root@centos7 tftpboot]# ll pxelinux.cfg/default
-r--r--r-- 1 root root 476 Feb 3 16:58 pxelinux.cfg/default
[root@centos7 tftpboot]# tree
.
├── initrd.img
├── menu.c32
├── pxelinux.0
├── pxelinux.cfg
│?? └── default
└── vmlinuz

1 directory, 5 files
我們的pxe安裝文件如上所示!!!

12、正常情況下,centos7有系統就會用系統來啟動,若無系統情況下,我們來看:

dd if=/dev/zero of=/dev/sda bs=1 count=446
446+0 records in
446+0 records out
446 bytes (446 B) copied, 0.00115566 s, 386 KB/s
reboot!
此時,系統已經被人為破壞,把CD光盤斷開,

註意:我們要把/var/www/html/ks/centos7-mini.cfg的路徑也修改為10.0.0.200,才能正確安裝。

實驗:實現PXE的網絡自動化安裝