【Ubuntu】使用grub2挂载NFS根文件系统(rootfs)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wr132/article/details/81867457

经常调试arm开发板的同学应该比较熟悉uboot,它可以使用tftp自动下载内核并通过nfs挂载rootfs。其实,作为x86平台常见的bootloader,grub也可以做到通过nfs挂载rootfs。

安装grub2

目前新版的ubuntu都预装了grub2,如果希望手动编译安装,可以访问grub官网下载最新的源码。本文主要介绍通过ubuntu来安装。

  1. 启动ubuntu。你可以通过ubuntu的镜像启动,也可以使用已经安装好的ubuntu,甚至可以使用虚拟机中的ubuntu,尽量使用较新的版本。这里需要注意的是,目前的PC平台的启动模式分为EFI/BIOS两种,如果你通过BIOS启动ubuntu,那么安装的grub只能通过启动。
  2. 挂载grub的安装分区。可以是本地硬盘,也可以是U盘。

    mount /dev/sdXY /mnt
    mkdir /mnt/boot
  3. 安装grub,分为BIOS模式和UEFI模式。如果使用UEFI模式,那么grub必须安装在第一个分区,并且该分区需要使用MBR分区表和fat32文件系统。

    
    # BIOS
    
    grub-install --target=i386-pc --recheck --boot-directory=/mnt/boot /dev/sdX
    
    # UEFI
    
    grub-install --target x86_64-efi --removable --boot-directory=/mnt/boot --efi-directory=/mnt

配置grub

在grub的安装分区,可以找到/boot/grub目录,进入该目录,新建grub.cfg文件,并输入以下内容。

set timeout=10
set default=0

menuentry "netboot" {
   echo 'Loading Linux ...'
   linux /boot/bzImage ip=:::::eth0:dhcp root=/dev/nfs nfsroot=192.168.1.2:/nfsroot/rootfs rw
}

这里客户端使用dhcp获取ip地址,nfs服务器的ip是192.168.1.2。完整的cmdline配置参数信息参见内核文档Mounting the root filesystem via NFS

配置nfs服务器

这里以ubuntu的配置为例。

  1. 安装nfs服务

    sudo apt install nfs-kernel-server
  2. 配置nfs挂载点

    sudo vim /etc/exports
    /nfsroot/rootfs *(rw,async,no_root_squash,no_subtree_check)
  3. 重启nfs服务

    sudo service nfs-kernel-server restart

启动系统

将根文件系统中的目录解压到nfs挂载目录,将内核复制到grub分区中的/boot/bzImage,nfs服务器与客户端接到同一台路由器,启动客户端即可。

参考

https://wiki.archlinux.org/index.php/diskless_system
https://wiki.archlinux.org/index.php/Multiboot_USB_drive
https://www.pendrivelinux.com/install-grub2-on-usb-from-ubuntu-linux/

猜你喜欢

转载自blog.csdn.net/wr132/article/details/81867457