PXE远程自动安装操作系统过程

  • 什么是PXE

PXE中文叫“预启动执行环境”。计算机在启动时需要加载操作系统,而加载的方式常见有:光驱启动(CDROM)、硬盘启动、网络启动。光驱启动一般用在光驱插入有系统盘的时候使用;当我们装完操作系统以后一般选择硬盘启动;网络启动就是通过网卡中的PXE芯片到远程服务器端下载启动文件。三种启动方式的先后顺序可在BIOS中进行设置。

  • 为什么通过PXE来安装操作系统

小弟单位虽然不大但也有上千台服务器,分布在三个机房,三机房离办公室最近的也有10KM以上。每次新上服务器都需要重装系统,可以想象如果按传统方式拿着系统盘到机房现场装系统,那画面真是太美……这种情况,利用PXE网络启动,坐办公室暖气吹吹就可以把远端机房的服务器系统装好;安装整个过程全自动,节省下的时间完全可以去做更有技术含量的事情。

  • 拓扑示意图

wKiom1ZRzoKzQkzlAABCgSpH1x8979.png

  • VMWARE虚拟机模拟环境

wKiom1ZR0_LAYfpaAAAc2ycXgAU114.png

 

  • 材料准备

一台linux服务器,作为网络服务器(一般为虚拟机,方便使用)。系统的IOS文件(网上下载需要安装的linux发行版IOS)。

  • 配置详细过程

一、TFTP服务器设置     

yum install -y tftp-server

[root@pxe ~]# vim /etc/xinetd.d/tftp 
  1 # default: off
  2 # description: The tftp server serves files using the trivial file transfer \
  3 #       protocol.  The tftp protocol is often used to boot diskless \
  4 #       workstations, download configuration files to network-aware printers, \
  5 #       and to start the installation process for some operating systems.
  6 service tftp
  7 {
  8         socket_type             = dgram
  9         protocol                = udp
 10         wait                    = yes
 11         user                    = root
 12         server                  = /usr/sbin/in.tftpd
 13         server_args             = -s    #修改主目录
 14         disable                 =              #启动服务器
 15         per_source              = 11
 16         cps                     = 100 2
 17         flags                   = IPv4
 18 }
 
[root@pxe tftpfiles]# chkconfig xinetd on      #设置开机启动
[root@pxe tftpfiles]# service xinetd restart    #重启TFTP服务
Stopping xinetd:                                           [  OK  ]
Starting xinetd:                                           [  OK  ]

二、系统安装文件准备

1、安装syslinux,完成后,拷贝pxelinux.0到tftp主目录(自创建):

yum install -y syslinux.x86_64

cp /usr/share/syslinux/pxelinux.0 /tftpfiles/

 2、挂在IOS系统镜像,并拷贝initrd.img、vmlinuz、boot.msg文件到主目录

mount -t iso9660 /dev/cdrom /mnt/centos/    #挂载光驱

cp /mnt/centos/p_w_picpaths/pxeboot/{vmlinuz,initrd.img} /tftpfiles/   #拷贝initrd.img、vmlinuz文件

cp /mnt/centos/isolinux/boot.msg  /tftpfiles/    #拷贝boot.msg文件

3、创建pxelinux.cfg文件夹,将IOS中isolinux.cfg文件拷到该文件夹更名为default

mkdir /tftpfiles/pxelinux.cfg

cp /mnt/centos/isolinux/isolinux.cfg /tftpfiles/pxelinux.cfg/default

vim default    #修改default
  1 default       #默认选择label为ks选项,可自行修改
  2 prompt 0
  3 timeout 600
  4 
  5 #display boot.msg
  6 
  7 menu background splash.jpg
  8 menu title Welcome to CentOS 6.5!
  9 menu color border 0 #ffffffff #00000000
 10 menu color sel 7 #ffffffff #ff000000
 11 menu color title 0 #ffffffff #00000000
 12 menu color tabmsg 0 #ffffffff #00000000
 13 menu color unsel 0 #ffffffff #00000000
 14 menu color hotsel 0 #ff000000 #ffffffff
 15 menu color hotkey 7 #ffffffff #ff000000
 16 menu color scrollbar 0 #ffffffff #00000000
 17 
 18 label               #设置你的系统标签为ks
 19   kernel vmlinuz        #内核
 20   append initrd=initrd.img ks=nfs:10.10.10.1:/tftpfiles/ks.cfg #指导客户机通过nfs到服务器获取ks.cfg,也可以选择http\ftp方式

4、在/tftpfiles下创建ks.cfg文件,ks.cfg文件也可以通过进入X-windows界面安装运行system-config-kickstart获取。ks.cfg用来配置安装系统时的各种参数,比如用户名密码、如何分区、配置网卡等,可按自己需求进行配置。下面是我的ks.cfg供参考:

vim  ks.cfg
  1 auth --useshadow --enablemd5    #密码加密方式
  2 install              #系统安装设置
  3 text                  #纯文本安装,不安装X-window
  4 firewall --disable     
  5 keyboard us
  6 lang en_US
  7 selinux --disable
  8 timezone --isUtc Aisa/Shanghai
  9 nfs --server 10.10.10.1 --dir=/mnt/centos #设置系统安装文件获取服务器地址及路径
 10 rootpw 123456    #--iscrypted 49ba59abbe56e057 设置root的密码,--iscrypted为md5后的值
 11 firstboot --disable
 12 logging --level=info
 13 reboot                #一定要加,不然安装过程还需要手动重启
 14 
 15 bootloader --append="rhgb quiet" --location=mbr --driveorder=sda
 16 zerombr
 17 network --bootproto=dhcp --device=eth0 --onboot=on  #也可以手动配置静态IP
 18 clearpart --all --initlabel    #删除所有分区后再重新分区
 19 part / --fstype="ext4" --size=10240
 20 part swap --fstype="swap" --size=2048
 21 part /boot --fstype="ext4" --size=200
 22 
 23 %packages   #选择默认要安装的包
 24 @base
 #ks很强大,还可以设置很多东东。比如安装前或安装后需要执行的命令,可深入学习。

综上,安装系统需要的文件已经准备齐全,default在pxelinux.cfg中,目前主目录内容如下:

[root@pxe tftpfiles]# ll
total 36676
-r--r--r--. 1 root root       84 Nov 22 21:58 boot.msg
-r--r--r--. 1 root root 33383679 Nov 22 21:58 initrd.img
-rw-r--r--. 1 root root      539 Nov 23 01:50 ks.cfg
-rw-r--r--. 1 root root    26759 Nov 23 07:09 pxelinux.0
drwxr-xr-x. 2 root root     4096 Nov 24 18:25 pxelinux.cfg
-r-xr-xr-x. 1 root root  4128368 Nov 22 21:58 vmlinuz

三、配置DHCP服务器

  DHCP服务器用来给客户机分配IP,让客户机与服务器能通信从服务器获取安装信息及文件。

     

yum install -y dhcp.x86_64              #安装dhcp服务器

[root@pxe tftpfiles]# vim /etc/dhcp/dhcpd.conf    #设置服务器参数
  1 #
  2 
  3 default-lease-time 600;
  4 max-lease-time 7200;
  5 
  6 log-facility local7;
  7 
  8 # DHCP server to understand the network topology.
  9 
 10 subnet 10.10.10.0 netmask 255.255.255.0 {
 11   range 10.10.10.2 10.10.10.254;
 12   option routers 10.10.10.1;               #10.10.10.1为本机与客户机互联网卡地址
 13   option subnet-mask 255.255.255.0;
 14   next-server 10.10.10.1;
 15   filename "pxelinux.0";
 16 }
 
 vim /etc/init.d/dhcpd  #第30、31行修改DHCPD使用权限为自己,否则DHCPD服务器启动失败
 30 user=root
 31 group=root

[root@pxe tftpfiles]# chkconfig dhcpd on
[root@pxe tftpfiles]# service dhcpd restart
Shutting down dhcpd:                                       [  OK  ]
Starting dhcpd:                                            [  OK  ]

四、配置NFS服务器

    除了NFS,还可以选择HTTP、FTP等方式传输文件

yum install -y nfs-utils.x86_64        #安装nfs服务器

[root@pxe tftpfiles]# vim /etc/exports     #设置开放的路径及用户地址段,ro为只读
  1 /tftpfiles 10.10.10.0/24(ro)    
  2 /mnt/centos/ 10.10.10.0/24(ro)
  
[root@pxe tftpfiles]# chkconfig nfs on     
[root@pxe tftpfiles]# chkconfig rpcbind on
[root@pxe tftpfiles]# service nfs restart
Shutting down NFS daemon:                                  [  OK  ]
Shutting down NFS mountd:                                  [  OK  ]
Shutting down NFS services:                                [  OK  ]
Shutting down RPC idmapd:                                  [  OK  ]
Starting NFS services:                                     [  OK  ]
Starting NFS mountd:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]
Starting RPC idmapd:                                       [  OK  ]
[root@pxe tftpfiles]# service rpcbind restart
Stopping rpcbind:                                          [  OK  ]
Starting rpcbind:                                          [  OK  ]
  • 客户机开始安装

    客户机必须有一张网卡与服务器在同一广播域,不然服务器将收不到客户机的DHCP请求。安装前关闭服务器iptables和selinux,以免tftp连接超时:service iptables stop;setenforce 0。如果是mini版的centos安装过程中可能会出现未知缺包提示。

1、通过DHCP获取IP、pxelinux.0文件,然后通过TFTP连接到服务器获取default文件,接着加载vmlinuz、initrd.img文件

wKiom1ZT_B7h9feDAAA59c4lvXY370.png

2、开始挂在文件系统、检测硬件

wKiom1ZT_B_BatovAAA8ateRpK8939.png

wKiom1ZT_CCD0DbMAAAtoXRb9z4903.png

3、安装系统软件

wKiom1ZT_CCAXiO7AAA1Bu-dkWY823.png

4、安装完毕,开始关闭进程,重启系统

wKiom1ZT_CGQUG-GAAAsNi7W13k257.png

5、重启完成,安装完毕!

wKiom1ZT_CGQaL1tAAAVU1krKxI729.png

https://blog.51cto.com/nihou/1716497

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值