Linux可以有三种形式运行,在硬盘中、网络中或者内存中。Linux运行在硬盘中的方式是最常见的操作系统运行方式。在网络和内存中运行,都可以使用PXE服务器实现,下面来测试安装下这两种方式。
0.准备工作
先来安装一个PXE服务器
- 安装需要的几个包
 
[root@pxe ~]# yum -y install syslinux xinetd tftp-server dhcp
- 准备
pxelinux.0 
[root@pxe ~]# mkdir /var/lib/tftpboot/pxelinux.cfg
[root@pxe ~]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
- 启动
tftp服务 
[root@pxe ~]# vi /etc/xinetd.d/tftp
# disable = no
[root@pxe ~]# systemctl start xinetd
[root@pxe ~]# systemctl enable xinetd
- 启动
dhcp服务器 
[root@pxe ~]# vi /etc/dhcp/dhcpd.conf
subnet 192.168.56.0 netmask 255.255.255.0 {
        range 192.168.56.100 192.168.56.200;
        option subnet-mask 255.255.255.0;
        next-server 192.168.56.171;
        filename "/pxelinux.0";
       }
[root@pxe ~]# systemctl start dhcpd
[root@pxe ~]# systemctl enable dhcpd
1. 网络启动(pxe+nfs)
这种方式的原理主要是通过tftp服务器提供的vmlinuz和initrd镜像启动后,将原来使用本地硬盘的根目录,采用NFS的网络方式来实现。这种方式的优势在于不需要使用本地硬盘,不过需要依赖于网络。
- 安装依赖包
 
yum -y install dracut-network nfs-utils
- 安装一个可网络运行的操作系统
 
[root@pxe ~]# mkdir /centos7-netboot/
[root@pxe ~]# yum groupinstall "Minimal Install" --releasever=/ --installroot=/centos7-netboot/
# 下面这两个包不确定是不是必须的
[root@pxe ~]# yum install nfs-utils dracut-network --releasever=/ --installroot=/centos7-netboot/  
- 修改
root密码 
[root@pxe pxelinux.cfg]# openssl passwd -1
Password: 123456
Verifying - Password: 123456
$1$iQnz.381$2Yhkd1es5R5FcIqdh4R97/
[root@pxe pxelinux.cfg]# vi /centos7-netboot/etc/shadow
- 修改
fstab 
[root@pxe pxelinux.cfg]# cat /centos7-netboot/etc/fstab 
none    /tmp        tmpfs   defaults   0 0
tmpfs   /dev/shm    tmpfs   defaults   0 0
sysfs   /sys        sysfs   defaults   0 0
proc    /proc       proc    defaults   0 0
- 下载
vmlinuz和initrd 
cd /var/lib/tftpboot
wget http://mirrors.163.com/centos/7/os/x86_64/images/pxeboot/vmlinuz
wget http://mirrors.163.com/centos/7/os/x86_64/images/pxeboot/initrd.img
- 新建default,可以使用
rw或者ro 
[root@pxe tftpboot]# cat pxelinux.cfg/default
default centos7
label centos7
    kernel vmlinuz
    append initrd=initrd.img root=nfs:192.168.56.171:/centos7-netboot/ rw selinux=0
启动一个虚拟机,测试一下

2.内存启动(pxe+livecd)
这种方式的原理是将livecd的内存操作系统使用pxe远程网络加载到目标服务器直接启动,好处是不需要占用其他服务器的存储资源,但是数据不能保留,操作系统数据都在内存中,重启后就将丢失。
- 安装一些包
 
[root@pxe tftpboot]# yum install livecd-tools
# livecd-iso-to-pxeboot 需要这个工具进行转换
[root@pxe tftpboot]# livecd-
livecd-creator         livecd-iso-to-disk     livecd-iso-to-pxeboot  
- 准备一个
livecd,可以自己制作,也可以使用现成的 - 转换一下
livecd,之前已准备好一个migration-tools.iso,制作过程略 
[root@pxe ~]# livecd-iso-to-pxeboot  migration-tools.iso
# 当前目录生成tftpboot目录,将文件全部复制,注意是否覆盖部分文件
[root@pxe ~]# cp -rf tftpboot/* /var/lib/tftpboot/
[root@pxe ~]# cp migration-tools.iso /var/lib/tftpboot/
- 就可以直接启动一个虚拟机来测试了
 

参考
- https://github.com/livecd-tools/livecd-tools/blob/master/tools/livecd-iso-to-pxeboot.sh
 - 
    
https://xcat-docs.readthedocs.io/en/2.11/advanced/hierarchy/provision/diskful_sn.html
 - https://www.server-world.info/en/note?os=CentOS_7&p=pxe&f=4
 - http://cobbler.github.io/manuals/2.8.0/Appendix/G_-_Booting_Live_CDs.html