一、Clonezilla

Clonezilla是由台湾网络中心研发的开源软件,可用于备份还原各类操作系统,包含Linux (ext2, ext3, ext4, reiserfs, reiser4, xfs, jfs), Mac OS (HFS+), Windows (fat, ntfs), FreeBSD, NetBSD, OpenBSD (UFS),以及VMware ESX (VMFS),对于不支持的文件系统,Clonezilla采用全部复制(dd)的方式处理。Clonezilla支持群播,结合PXE还可以远程大规模的备份还原系统。
Clonezilla有两个版本:Clonezilla live和Clonezilla server edition;Clonezilla live可放置在U盘等便携存储上,Clonezilla server edition可配合PXE远程大量备份还原操作系统。

二、PXE

PXE是Inter公司设计的协议,它可以使计算机通过网络启动。协议分为client和server两端,PXE client在网卡的ROM中,当计算机引导时,BIOS把PXE client调入内存执行,并显示出命令菜单,经用户选择后,PXE client将放置在远端的操作系统通过TFTP下载到本地运行。

三、PXE+Clonezilla示例

1、配置DHCP服务器
1、安装
yum install dhcp* -y
2、更改配置文件
vim  /etc/dhcp/dhcpd.conf
  1 subnet 192.168.92.0 netmask 255.255.255.0 {
  2 range 192.168.92.1 192.168.92.128;
  3 filename  "pxelinux.0";
  4 next-server 192.168.92.10;
  5 ddns-update-style none;
  6 ignore client-updates;
  7 default-lease-time 86400;
  8 max-lease-time 518400;
  9 option routers 192.168.92.254;
 10 option domain-name-servers 192.168.92.10;                                                                     
 11 }

我本机地址192.168.92.10,dhcp地址池是1到5,默认网关是254

3、修改网卡文件
vim /etc/sysconfig/network-scripts/ifcfg-eno16777736 
  1 TYPE=Ethernet
  2 BOOTPROTO=static
  3 IPADDR=192.168.92.10                                                                                        
  4 NETMASK=255.255.255.0
  5 GATEWAY=192.168.92.254
  6 DNS1=8.8.8.8

systemctl restart network

4、开启dhcp服务
systemctl start dhcpd

[root@eric-jia ~]# netstat -tunlp | grep 67
udp        0      0 0.0.0.0:67              0.0.0.0:*                           13828/dhcpd     

5关闭防火墙和selinux

2、PXE和TFTP配置

1、安装
yum install xinetd
yum install syslinux
yum install tftp*
2、编辑tftp配置文件
vim /etc/xinetd.d/tftp 
service tftp
{
    socket_type     = dgram
    protocol        = udp
    wait            = yes
    user            = root
    server          = /usr/sbin/in.tftpd
    server_args     = -s /var/lib/tftpboot
    disable         = no
    per_source      = 11
    cps         = 100 2
    flags           = IPv4
}

systemctl restart xinetd.service 

3、复制引导文件
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
cp /usr/share/syslinux/vesamenu.c32  /var/lib/tftpboot/

4、复制内核文件及引导菜单
mount -o loop clonezilla-live-2.5.2-17-amd64.iso /mnt
cp -r /mnt/live/ /var/lib/tftpboot/
cp /var/lib/tftpboot/live/initrd.img /var/lib/tftpboot
cp /var/lib/tftpboot/live/vmlinuz /var/lib/tftpboot
cp /mnt/syslinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default

5、安装httpd

1、yum install httpd -y
2、cp /mnt/live/filesystem.squashfs /var/www/html/
3、systemctl start httpd

6、修改PXE配置文件

vim /var/lib/tftpboot/pxelinux.cfg/default   # 加入http服务器地址fetch=。。。
kernel /live/vmlinuz
 38   append initrd=/live/initrd.img boot=live union=overlay username=user config components quiet noswap edd=on n    omodeset locales= keyboard-layouts= ocs_live_run="ocs-live-general" ocs_live_extra_param="" ocs_live_batch="no    " vga=788 ip= net.ifnames=0  nosplash i915.blacklist=yes radeonhd.blacklist=yes nouveau.blacklist=yes vmwgfx.e    nable_fbdev=1 fetch=http://192.168.92.10/filesystem.squashfs

7、实验
虚拟机1:192.168.92.10,作为tftp,http,dhcp服务器,在vmnet8中,vmnet8是NAT模式,不开启VMware自带DHCP。虚拟机1需开启http,dhcp,tftp服务,并将再生龙镜像挂载在/mnt目录下。虚拟机2也连接在net8中,开机后进入再生龙界面。

四、PXE + redhat7.2示例

配置PXE安装红帽只需要在上面的基础上稍作修改。

1、挂载redhat7.2.iso
mount -o loop -t iso9660 /dev/sr0 /media
2、复制相应文件
[root@eric-jia ~]# mkdir /var/lib/tftpboot/redhat
[root@eric-jia ~]# cp /media/images/pxeboot/initrd.img /var/lib/tftpboot/redhat/
[root@eric-jia ~]# cp /media/images/pxeboot/vmlinuz /var/lib/tftpboot/redhat/
[root@eric-jia ~]# cp /media/images/pxeboot/vesamenu.c32 /var/lib/tftpboot/redhat/

[root@eric-jia ~]# mkdir /var/lib/tftpboot/pxe
[root@eric-jia ~]# mv /var/lib/tftpboot/initrd.img /var/lib/tftpboot/pxe
[root@eric-jia ~]# mv /var/lib/tftpboot/vmlinuz /var/lib/tftpboot/pxe
[root@eric-jia ~]# mv /var/lib/tftpboot/vesamenu.c32 /var/lib/tftpboot/pxe

[root@eric-jia tftpboot]# cp /var/lib/tftpboot/redhat/* /var/lib/tftpboot/
3、配置ks
yum install system-config-kickstart.noarch 
可以在图形界面system-config-kickstart配置ks文件,我这里直接用anaconda-ks.cfg文件
cp /root/anaconda-ks.cfg /var/www/html/ks.cfg
4、挂载镜像至http根目录下
mkdir /var/www/html/redhat
umount /media
mount -o loop -t iso9660 /dev/sr0 /var/www/html/redhat/
5、配置引导菜单
vim /var/lib/tftpboot/pxelinux.cfg/default.redhat
  1 default vesamenu.c32
  2  timeout 60
  3  menu title RedHat 7.2 Pxe Menu
  4 label redhat 7.2 --  one
  5  kernel redhat/vmlinuz
  6  append initrd=redhat/initrd.img inst.repo=http://192.168.92.10/redhat inst.ks=http://192.168.92.10/ks.cfg
  7  MENU LABEL Install RedHat 7.2 --> kickstart
  8  MENU default
  9 
 10 label redhat 7.2 --  two
 11  kernel redhat/vmlinuz
 12  append initrd=redhat/initrd.img inst.repo=http://192.168.92.10/redhat
 13  MENU LABEL Install RedHat 7.2  

6、更换引导菜单
[root@eric-jia ~]# mv /var/lib/tftpboot/pxelinux.cfg/default /var/lib/tftpboot/pxelinux.cfg/default.pxe 
[root@eric-jia ~]# mv /var/lib/tftpboot/pxelinux.cfg/default.redhat /var/lib/tftpboot/pxelinux.cfg/default 

7、开启tftp,http,dhcp服务
systemctl start dhcpd
systemctl start httpd
systemctl start xinetd
[root@eric-jia ~]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1590/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1928/master         
tcp6       0      0 :::80                   :::*                    LISTEN      6505/httpd          
tcp6       0      0 :::22                   :::*                    LISTEN      1590/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1928/master         
tcp6       0      0 :::443                  :::*                    LISTEN      6505/httpd          
udp        0      0 127.0.0.1:323           0.0.0.0:*                           1325/chronyd        
udp        0      0 0.0.0.0:67              0.0.0.0:*                           6499/dhcpd          
udp        0      0 0.0.0.0:69              0.0.0.0:*                           1604/xinetd         
udp6       0      0 ::1:323                 :::*                                1325/chronyd  

之后就是测试了,有两种方式,kickstart是无人值守安装,下一种是手动安装,实际工作中,都有用武之地。
PXE+Clonezilla+kickstart_第1张图片

附切换脚本:

#!/bin/bash
#
cat <<-EOF
===============================================
    Please enter the service you want to start 
        1、clonezilla
        2、redhat 7.2
        3、quit
===============================================
EOF
read -p "Please enter your choice: "  SER

case $SER in
    "1")
        #Mount Mirror
        mount -o loop /root/clonezilla-live-2.5.2-17-amd64.iso /mnt 

        #Replace the PXE required files
        rm -rf /var/lib/tftpboot/{initrd.img,vesamenu.c32,vmlinuz} &> /dev/null
        cp /var/lib/tftpboot/pxe/* /var/lib/tftpboot/

        #Replace the boot boot menu file
        rm -f /var/lib/tftpboot/pxelinux.cfg/default
        cp /var/lib/tftpboot/pxelinux.cfg/default.pxe /var/lib/tftpboot/pxelinux.cfg/default

        #Start a service
        systemctl restart httpd
        systemctl restart dhcpd
        systemctl restart xinetd
        ;;
    "2")
        #Mount Mirror
        mount -o loop -t iso9660 /dev/sr0 /var/www/html/redhat/ 

        #Replace the PXE required files
        rm -rf /var/lib/tftpboot/{initrd.img,vesamenu.c32,vmlinuz} &> /dev/null
        cp /var/lib/tftpboot/redhat/* /var/lib/tftpboot/

        #Replace the boot boot menu file
        rm -f /var/lib/tftpboot/pxelinux.cfg/default
        cp /var/lib/tftpboot/pxelinux.cfg/default.redhat /var/lib/tftpboot/pxelinux.cfg/default

        #Start a service
        systemctl restart httpd
        systemctl restart dhcpd
        systemctl restart xinetd
        ;;
     "3")
        exit 0
        ;;

     *)
        echo "input error"
        exit 1
        ;;

esac