利用PXE批量进入救援模式修复多台主机的boot分区

利用PXE自动化安装centos7

前言

  PXE的功能及原理。大概解释一下意思就是:

  1. 启动计算机的时候如果没有插入U盘以及光驱等介质的话,boot启动项是有一个从PXE启动的选项,如果都没有则会从pxe启动。
  2. PXE上的DHCP客户端会向DHCP服务器,申请IP地址,DHCP服务器分配一个IP址地给它,同时DHCP服务器还会告诉客户端配置文件名称以及TFTP服务器的地址,让PXE客户端去下载。
  3. 配置文件(一般是pxelinux.0)告诉PXE客户端系统的安装启动项是在pxelinux.cfg目录下面的default这个文件里。

环境准备

  这里我提前安装了一台centos7,来当服务器。

服务器需要安装:

dhcp服务:用来给刚开机的客户端分配ip地址以及告知配置文件和tftp服务器的IP地址。
tftp服务:用来给客户端提供所需配置文件的下载路径。
httpd/vsftpd服务:两者选一即可,用来让客户端下载镜像。
xinetd服务:用来接管tftp服务,来做一些策略。
syslinux: 需要从这里来拷贝pxelinux.0等文件。
总之一条命令:

[root@localhost tftpboot]# yum install dhcpd tftp-server httpd syslinux xinetd -y

#其中httpd可换成vsftpd,我这里就用httpd做了,就不安装vsftpd了
服务器需要的配置:
  1. 关闭防火墙
关闭防火墙
      systemctl stop firewalld.service     
      #关闭防火墙
      systemctl disable firewalld.service  
      #取消防火墙开机自启动
      setenforce 0     
      #临时关闭防火墙
      getenforce       
      #获取防火墙状态
      把/etc/selinux/config 里的SELINUX=enforcing 改成 SELINUX=disabled 
      #来永久关闭selinux
      #centos 7 现在使用firewalld.service代替iptables,并且使用systemctl来管理比较方便#

  1. 配置静态ip
    因为dhcp服务器需要,所以需要把服务器更改成静态ip
    [root@localhost ~]#vim /etc/sysconfig/network-scripts/ifcfg-ens37
      TYPE="Ethernet"
      PROXY_METHOD="none"
      BROWSER_ONLY="no"
      BOOTPROTO="static"
      DEFROUTE="yes"
      IPV4_FAILURE_FATAL="no"
      IPADDR=192.168.2.1
      NETMASK=255.255.255.0
      NAME="ens37"
      DEVICE="ens37"
      ONBOOT="yes"
    #我这里是第二块网卡。
    #然后ifdown ens37;ifup ens37  来使网卡生效。
    #通过ip a来查看是否成功
  1. 配置dhcp服务
    [root@localhost ~]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf

    #把dhcp 样例文件拷贝到文件夹里做修改,核心内容如下:
    subnet 192.168.2.0 netmask 255.255.255.0 {
     range 192.168.2.10 192.168.2.100;
     option routers 192.168.2.1;
     option subnet-mask 255.255.255.0;
     default-lease-time 600;
     max-lease-time 7200;
     next-server 192.168.2.1;    #告诉客户端pxelinux.0从哪里下载
     filename "pxelinux.0";      #告诉客户端有个配置文件可以下载
     #然后重启服务systemctl restart dhcpd
     #开机自启动服务 systemctl enable dhcpd(后面不再一一重复)
}
  1. 挂载镜像
    mount /dev/sr0 /mnt
    mkdir /var/www/html/rhel7.2  
    cp -a /mnt /var/www/html/rhel7.2  
    # 把光盘文件拷贝到http服务目录,方便客户端下载
  1. 配置tftp目录和服务
  1)首先配置一下tftp的文件目录
    mkdir /tftpboot
    mkdir /tftpboot/pxelinux.cfg
    cp /usr/share/syslinux/pxelinux.0 /tftpboot/
    cp /usr/share/syslinux/chain.c32 /tftpboot/
    cp /usr/share/syslinux/mboot.c32 /tftpboot/
    cp /usr/share/syslinux/memdisk /tftpboot/
    cp /usr/share/syslinux/menu.c32 /tftpboot/
    cp /var/www/html/media/images/pxeboot/vmlinuz /tftpboot/
    cp /var/www/html/media/images/pxeboot/initrd.img /tftpboot/
    cp /var/www/html/media/isolinux/isolinux.cfg  /tftpboot/pxelinux.cfg/default
    最后文件的目录结构是
    /tftpboot/
    ├── chain.c32
    ├── initrd.img
    ├── mboot.c32
    ├── memdisk
    ├── menu.c32
    ├── pxelinux.0
    ├── pxelinux.cfg
    │   └── default
    └── vmlinuz
  2)配置tftp服务使用xinetd托管,更改内容我用#号注释
    vi /etc/xinetd.d/tftp 内容如下
      service tftp
      {
        socket_type             = dgram
        protocol                = udp
        wait                    = yes
        user                    = root
        server                  = /usr/sbin/in.tftpd
        server_args             = -s /tftpboot #更改tftp目录
        disable                 = no   #启动服务
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
      }
    然后 systemctl stop tftp.socket  关闭tftp
    使用xinetd打开tftp  systemctl restart xinetd.service
  1. 配置tftp目录里pxelinux.cfg/default的文件内容
[root@localhost ~]# vi //tftpboot/pxelinux.cfg/default
default menu.c32     
#首先这里需要改成menu.c32 从isolinux拷贝的文件配置默认是vesamenu.c32
timeout 100
prompt 0
menu title PXE MENU

label rescue
  menu rescue red hat 
  kernel vmlinuz
  append initrd=initrd.img repo=http://192.168.2.1/rhel7.2 rescue quiet
  # 这里需要写http里的.cfg 的路径,可以自己检查是否能打开
  1. 编译安装squashfs
    从网站https://sourceforge.net/projects/squashfs/files/squashfs/ 下载 squashfs4.3.tar.gz

    [root@localhost youyou]# tar xvf squashfs4.3.tar.gz
    [root@localhost youyou]# cd squashfs4.3/squashfs-tools/
    [root@localhost squashfs-tools]# vi Makefile 
    # 修改Makefile 去掉 XZ_SUPPORT 1 这一行首的 # 号
    [root@localhost squashfs-tools]# make && make install 
    
  2. 修改救援模式使用的镜像文件

    [root@localhost youyou]# cp /var/www/html/rhel7.2/LiveOS/squashfs.img /var/www/html/rhel7.2/LiveOS/squashfs.img.bak
    # 把好的镜像备份一份,防止修改错误
    [root@localhost LiveOS]# unsquashfs squashfs.img
    [root@localhost LiveOS]# mkdir test
    [root@localhost LiveOS]# mount squashfs-root/LiveOS/rootfs.img test
    [root@localhost LiveOS]# chmod +x test/etc/rc.d
    [root@localhost LiveOS]# chmod +x test/etc/rc.d/rc.local
    [root@localhost LiveOS]# vi test/etc/init.d/a.sh
    #!/bin/bash
    reboot
    [root@localhost LiveOS]# chmod +x test/etc/init.d/a.sh
    [root@localhost LiveOS]# vi test/etc/rc.d/rc.local
    #!/bin/bash
    vgchange -a y
    mkdir -p /youyou/boot
    mount /dev/mapper/rhel-root /youyou
    mount /dev/sda1 /youyou/boot
    mount -o bind /dev /youyou/dev
    mount -o bind /proc /youyou/proc
    mount -o bind /run /youyou/run
    mount -o bind /sys /youyou/sys
    sshpass
    tar xvf
    sshpass
    chroot /youyou/ grub2-mkconfig -o /boot/grub2/grub.cfg
    touch /youyou/.autorelabel
    reboot
    [root@localhost LiveOS]# vi test/usr/lib/systemd/system/test.service
    [Unit]
    Description=jioaben
    After=rc-local.service
    [Service]
    Type=forking
    User=root
    ExecStart=/etc/init.d/a.sh
    [Install]
    WantedBy=rescue.target
    [root@localhost LiveOS]# vi test/usr/lib/systemd/system/rc-local.service
    ##This file is part of systemd.
    #systemd is free software; you can redistribute it and/or modify it
    #under the terms of the GNU Lesser General Public License as published by
    #the Free Software Foundation; either version 2.1 of the License, or
    #(at your option) any later version.
    #This unit gets pulled automatically into multi-user.target by
    #systemd-rc-local-generator if /etc/rc.d/rc.local is executable.
    [Unit]
    Description=/etc/rc.d/rc.local Compatibility
    ConditionFileIsExecutable=/etc/rc.d/rc.local
    After=network.target
    [Service]
    Type=forking
    ExecStart=/etc/rc.d/rc.local start
    TimeoutSec=0
    RemainAfterExit=yes
    [Install]
    WantedBy=rescue.target
    [root@localhost LiveOS]# cd test/usr/lib/systemd/system/rescue.target.wants/
    [root@localhost rescue.target.wants]#ln -sv ../test.service test.service
    [root@localhost rescue.target.wants]#ln -sv ../rc-local.service rc-local.service
    [root@localhost rescue.target.wants]# cd /var/www/html/rh7.2/LiveOS
    [root@localhost LiveOS]# umount test
    [root@localhost LiveOS]# mksquashfs squashfs-root squashfs.img -comp xz
    
    
  3. 注意事项
    编译安装squashfs,需要安装xz-devel,ziib-devel

    打包进sshpass ssh -o StrictHostKeyChecking 选项可以免秘钥

    touch .autolabel文件 在修改boot后,需要在原系统的目录的根目录,创建这个文件,否则用户无法登录

    注意system启动顺序,通过systemd-analyze plot > boot.svg 查看启动顺序
    开机自动运行就是把服务放在 wants里面的超链接,就行了

    chroot /youyou ls chroot 可以单次切根,并在切过的目录执行一个命令

    挂载激活lvm 注意原来的系统时放在lvm上,还是放在磁盘上的

    mount -o bind 绑定切根之前的mout 切根之前需要绑定目录,否则会提示无法找到dev设备

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值