这篇文章上次修改于 294 天前,可能其部分内容已经发生变化,如有疑问可询问作者。

78888804_p02.png

记录下使用 iPXE 和 NFS 文件系统实现 Linux Live CD 网络启动服务搭建方法。
本次 iPXE 启动后将 vmlinuzinitrd 载入内存 ,然后由 Kernel 通过 NFS 文件系统加载 filesystem.squashfs
之前实现 Linux 发行版的 NetBoot 只能进行安装,局限性很大。

搭建 NFS 服务

安装软件包

Debian

sudo apt install nfs-kernel-server

CentOS

sudo yum install nfs-utils

编辑配置文件

vim /etc/exports
/data/images/ubuntu/    *(ro,sync,no_root_squash)
  • /data/images/ubuntu/ 的共享目录
  • * 可访问的主机(注意主机名和权限的小括号中间是没有空格的)
  • (ro,sync,no_root_squash) 权限

    • ro 只读
    • sync 数据会同步写入到内存与硬盘中
    • no_root_squash 登入 NFS 主机使用分享目录的使用者,如果是 root 的话,那么对于这个分享的目录来说,他就具有 root 的权限。这个参数权限很大,请妥善使用。

启动服务

Debian

sudo systemctl restart nfs-kernel-server

CentOS

sudo systemctl restart rpcbind 
sudo systemctl restart nfs

挂载 NFS

测试NFS服务是否可以使用

安装工具

Debian

sudo apt install nfs-common

CentOS

sudo yum install nfs-utils

Windows 10 用户请在 启用或关闭 Windows 功能 中勾选 NFS 客户端

挂载

在 Linux 平台挂载

mount [OPTION...] NFS_SERVER:EXPORTED_DIRECTORY MOUNT_POINT
sudo mount -t nfs 192.168.x.x:/data /mnt/nfs

如果需要开机自动挂载,需要修改配置文件 /etc/fstab

# <file system>     <dir>       <type>   <options>   <dump>    <pass>
192.168.x.x:/data /mnt/nfs  nfs      defaults    0       0

在 Windows 平台挂载

打开命令提示符,使用 showmount 查看 NFS 共享的目录

showmount
用法: showmount -e [server]    显示 NFS 服务器导出的所有共享。
showmount -a [server]    列出客户端主机名或 IP 地址,以及使用“主机:目录”格式显示的安装目录。
showmount -d [server]    显示 NFS 服务器上当前由某些 NFS 客户端安装的目录。

挂载和卸载

mount 
用法:  mount [-o options] [-u:username] [-p:<password | *>] <\\computername\sharename> <devicename | *>

-o rsize=size               设置读取缓冲区的大小(以 KB 为单位)。
-o wsize=size               设置写入缓冲区的大小(以 KB 为单位)。
-o timeout=time             设置 RPC 调用的超时值(以秒为单位)。
-o retry=number             设置软装载的重试次数。
-o mtype=soft|hard          设置装载类型。
-o lang=euc-jp|euc-tw|euc-kr|shift-jis|big5|ksc5601|gb2312-80|ansi
                            指定用于文件和目录名称的编码。
-o fileaccess=mode          指定文件的权限模式。
                            这些模式用于在 NFS 服务器上创建的
                            新文件。使用 UNIX 样式模式位指定。
-o anon                     作为匿名用户装载。
-o nolock                   禁用锁定。
-o casesensitive=yes|no     指定在服务器上执行区分大小写的文件查找。
-o sec=sys|krb5|krb5i|krb5p

挂载至盘符

mount \\服务器IP\共享的目录 盘符
mount \\192.168.x.x\volume\data\ Y:

卸载

使用 mount 命令查看已挂载的目录

mount
......
192.168.x.x:/data on /mnt/nfs type nfs4 (rw,relatime,vers=4.1,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.x.x,local_lock=none,addr=192.168.x.x)
......

卸载 NFS 目录

umount 挂载的目录
umount /mnt/nfs

在 Windows 平台卸载

umount 盘符
umount Y:

卸载全部 NFS 目录

umount -f -a

iPXE 和 Live CD 网络启动

以 Ubuntu 18.04 为例,在 NFS 共享中创建文件夹,并放入解压后的 ISO 镜像。

iPXE BootLoader 需要支持 NFS 功能,如果没有可以看下我之前的文章,编译 iPXE 部分

iPXE 启动脚本

#!ipxe

:start
menu PXE Boot Options
item --key l boot_ubuntu_livecd (L)Ubuntu 18.04 LiveCD

:boot_ubuntu_livecd
echo Boot Ubuntu 18.04 LiveCD 
set server_ip 192.168.x.x
set nfs_path /ubuntu
kernel nfs://guest@${server_ip}${nfs_path}/casper/vmlinuz
initrd nfs://guest@${server_ip}${nfs_path}/casper/initrd
imgargs vmlinuz initrd=initrd root=/dev/nfs boot=casper netboot=nfs nfsroot=${server_ip}:${nfs_path} ip=dhcp splash quiet --
boot ||  goto failed

vmlinuzinitrd 直接通过 NFS 文件系统获取 ,也可以使用 TFTP 等其他方式获取。

imgargs 在启动时给 Kernel 传递命令行参数

注意替换 server_ip nfs_path。另外 imgargsnfsroot NFS 服务器IP和路径直接有:隔开,没有会出现 Kernel panic

PXELINUX 配置

编辑 pxelinux.cfg/default 添加如下 label 块:

label Ubuntu 18.04 amd64
    menu label Ubuntu 18.04 amd64 LiveCD
    kernel ubuntu1804/vmlinuz
    append initrd=ubuntu1804/initrd boot=csasper netboot=nfs nfsroot=192.168.x.x:/netboot/nfs/ubuntu1804/ splash toram ---

HTTP 加载 Live CD

这个应该要修改 Kernel 之前没搞清楚,先咕咕咕咯😂

参考和链接

甘城なつき的插画 - pixiv

鸟哥的 Linux 私房菜 -- NFS 服务器

一步步搭建PXE网络装机

How to Set Up a NFS Server on Debian 10 Buster

How to Mount an NFS Share in Linux

iPXE - open source boot firmware cmd:imgargs

在Win10 Pro下挂载NFS 网络文件系统|Kenvix's Blog