跳转至

PXE无盘启动批量恢复系统

准备

准备需要批量恢复的种子系统(CentOS 6.6)

修改根/的挂载,将UUID改为/dev/sda1

Bash
[root@localhost ~]# vim /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sun Jun 26 04:59:38 2016
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/sda1               /                       ext4    defaults        1 1
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0

grub.confroot=UUID也改成root=/dev/sda1

Bash
root@pts/0 # vim /etc/grub.conf 
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You do not have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /, eg.
#          root (hd0,0)
#          kernel /boot/vmlinuz-version ro root=/dev/sda1
#          initrd /boot/initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/boot/grub/splash.xpm.gz
hiddenmenu
title CentOS 6 (2.6.32-504.el6.x86_64)
        root (hd0,0)
        kernel /boot/vmlinuz-2.6.32-504.el6.x86_64 ro root=/dev/sda1 rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
        initrd /boot/initramfs-2.6.32-504.el6.x86_64.img

删除网卡mac地址文件

Bash
[root@localhost ~]# rm -rf /etc/udev/rules.d/70-persistent-net.rules

关闭selinux

Bash
[root@localhost ~]# vim /etc/selinux/config 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

关闭iptables

Bash
1
2
3
4
[root@localhost ~]# iptables -F
[root@localhost ~]# /etc/init.d/iptables save
[root@localhost ~]# /etc/init.d/iptables stop
[root@localhost ~]# chkconfig iptables off

开始备份

Bash
1
2
3
4
5
[root@localhost /]# mkdir /back
[root@localhost /]# tar cvpzf /back/CentOS_6_6.tar.gz / --exclude=/proc --exclude=/lost+found --exclude=/back/CentOS_6_6.tar.gz --exclude=/mnt --exclude=/sys --exclude=var/tmp --exclude=/media

[root@localhost ~]# ll /back/CentOS_6_6.tar.gz 
-rw-r--r-- 1 root root 809386718 Jun 27 01:54 /back/CentOS_6_6.tar.gz

配置PXE无盘启动

Bash
1
2
3
4
[root@localhost ~]# mkdir -p /nodisk/host1
[root@localhost ~]# rsync -av --exclude='/proc' --exclude='/sys' --exclude='/tmp' --exclude='/var/tmp' --exclude='/etc/mtab' --exclude='/nodisk' --exclude='/back' --exclude='/media' /* /nodisk/host1/
[root@localhost ~]# cd /nodisk/host1/
[root@localhost host1]# mkdir -p proc sys tmp var/tmp etc/mtab media

调整系统模板的设备挂载配置文件/nodisk/host1/etc/fstab:删除所有的本地存储设备挂载信息(如://boot);添加系统模板的挂载信息

Bash
[root@localhost host1]# vim etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sun Jun 26 04:59:38 2016
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
192.168.30.3:/nodisk/host1      /               nfs     defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0

删除所有ifcfg-eth*网卡配置文件,只需保留ifcfg-lo

Bash
[root@localhost host1]# rm -rf /nodisk/host1/etc/sysconfig/network-scripts/ifcfg-eth*

修改免认证登陆

将本机密钥copy到/nodisk/host1/root/.ssh/authorized_keys

Bash
1
2
3
4
5
6
7
8
vim /nodisk/host1/etc/ssh/sshd_config 修改useDNS no

vim /etc/ssh/ssh_config 修改本机ssh客户端参数

不要key认证
StrictHostKeyChecking no
不要记录known_hsots文件
UserKnownHostsFile no

将整个工作站系统模板打包备份到系统服务器的/opt目录下,以作备用

Bash
drwxr-xr-x 22 root root 4096 Jun 27 03:21 host1
[root@localhost nodisk]# tar -cvf /opt/pxe_host1.tar host1

准备好工作站启动引导需要的文件

Bash
1
2
3
4
[root@localhost /]# yum install syslinux dracut dracut-network dhcp tftp-server nfs-utils rpcbind

[root@localhost /]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
[root@localhost /]# cp /boot/vmlinuz-2.6.32-504.el6.x86_64 /var/lib/tftpboot/

创建用于系统启动 镜像文件initrd.img(先执行命令 uname -r 查看内核版本,如:2.6.32-504.el6.x86_64

Bash
1
2
3
4
5
6
[root@localhost /]# uname -r
2.6.32-504.el6.x86_64

[root@localhost /]# dracut initrd-2.6.32-504.el6.x86_64 2.6.32-504.el6.x86_64
[root@localhost /]# chmod 644 initrd-2.6.32-504.el6.x86_64 
[root@localhost /]# mv initrd-2.6.32-504.el6.x86_64 /var/lib/tftpboot/

tftpboot/pxelinux.cfg/目录下创建默认的PXE引导配置文件default

Bash
1
2
3
4
5
6
7
8
[root@localhost /]# mkdir -p /var/lib/tftpboot/pxelinux.cfg
[root@localhost /]# cd /var/lib/tftpboot/pxelinux.cfg/
[root@localhost pxelinux.cfg]# vim default
    default auto
    label auto
    prompt 0
    kernel vmlinuz-2.6.32-504.el6.x86_64
    append initrd=initrd-2.6.32-504.el6.x86_64 root=nfs:192.168.30.3:/nodisk/host1/ selinux=0 ip=dhcp rw nomodeset vga=0x0314

编辑配置文件/etc/dhcp/dhcpd.conf

Bash
[root@localhost /]# vim /etc/dhcp/dhcpd.conf

ddns-update-style none;
ignore client-updates;
default-lease-time -1;
max-lease-time -1;
authritative;

subnet 192.168.30.0 netmask 255.255.255.0 {
range 192.168.30.100 192.168.30.200;
option routers 192.168.30.3;
option domain-name-servers 202.106.0.20;
next-server 192.168.30.3;
filename "pxelinux.0";

host host1 {
    hardware ethernet 00:0C:29:F5:E8:20;
    fixed-address 192.168.30.105;
    }
}

重启DHCP服务,且将DHCP服务设置成开机自启

Bash
1
2
3
4
5
[root@localhost /]# /etc/init.d/dhcpd start  
Starting dhcpd:                                            [  OK  ]
[root@localhost /]# chkconfig dhcpd on
[root@localhost /]# chkconfig |grep dhcp
dhcpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off

编辑配置/etc/xinetd.d/tftp

Bash
[root@localhost /]# vim /etc/xinetd.d/tftp 
disable                 = no

重启tftp服务,且将tftp服务设置成开机自启

Bash
1
2
3
4
5
[root@localhost /]# /etc/init.d/xinetd start
Starting xinetd:                                           [  OK  ]
[root@localhost /]# chkconfig xinetd on
[root@localhost /]# chkconfig |grep xinetd 
xinetd          0:off   1:off   2:on    3:on    4:on    5:on    6:off

编辑配置文件/etc/exports

Bash
1
2
3
[root@localhost /]# vim /etc/exports

/nodisk/host1 192.168.30.0/24(rw,async,no_root_squash)

重启nfs,设置开机启动

Bash
1
2
3
4
5
6
7
8
9
[root@localhost /]# /etc/init.d/nfs start
Starting NFS services:                                     [  OK  ]
Starting NFS quotas:                                       [  OK  ]
Starting NFS mountd:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]
Starting RPC idmapd:                                       [  OK  ]
[root@localhost /]# chkconfig nfs on
[root@localhost /]# chkconfig |grep nfs
nfs             0:off   1:off   2:on    3:on    4:on    5:on    6:off

无盘启动已经配置完成,将客户机改成网卡启动就可以了

接下来恢复系统到新硬盘

启动后先给磁盘分区:

Bash
fdisk /dev/sda

挂载分区到一个空目录

Bash
mount /dev/ada1 /mnt/

将打好的系统包复制到新硬盘,然后解压

Bash
cp /opt/CentOS_6_6.tar.gz /mnt/
tar xvpfz CentOS_6_6.tar.gz -C /mnt/

最后创建未打包的相应目录

Bash
mkdir -p proc sys mnt var/tmp media lost+found

修改ifcfg-eth0文件然后重启

reboot