嵌入式linux开发,开机NFS文件系统启动,使用nfs挂载根文件系统

        NFS( Network File System)即网络文件系统,是 FreeBSD 支持的文件系统中的一种,它允许网络中的计算机之间通过 TCP/IP 网络共享资源。在 NFS 的应用中, NFS 的客户端应用可以透明地读写位于远端 NFS 服务器上的文件,就像访问本地文件一样。使用 NFS,可以很方便的将 PC 上编译好的程序或者一些其他文件复制到开发板上调试,省去了频繁插拔 U 盘的繁琐。要实现 NFS 文件系统,需要现在 PC 的 Ubuntu 系统搭建好 NFS 服务器,并设置共享目录,开发板挂载该共享目录,达到文件传输的功能。

        在开发初期,开发者可能需要经常更改或更换 rootfs,把 rootfs 用 NFS 的方式挂载, 这样可以大幅减少开发的时程。

一、修改内核配置。

1、修改bootargs,Boot options。

Boot options --->
    Default kernel coomand string

CONFIG_CMDLINE="noinitrd nfsroot=198.168.1.18:/mnt/nfs/nuc970/rootfs,v3,nolock rw ip=198.168.1.25:198.168.1.18:198.168.1.1:255.255.255.0::eth0:off init=/linuxrc console=ttyS0,115200n8 rdinit=/sbin/init mem=64M"

 或通ubboot过命令配置

setenv bootargs noinitrd nfsroot=198.168.1.18:/mnt/nfs/nuc970/rootfs,v3,nolock rw ip=198.168.1.25:198.168.1.18:198.168.1.1:255.255.255.0::eth0:off init=/linuxrc console=ttyS0,115200n8 rdinit=/sbin/init mem=64M

说明:bootargs

a、root=/dev/nfs

/dev/nfs并非真的设备,而是一个告诉内核要通过网络取得根文件系统。

b、nfsroot=[<server-ip>:]<root-dir>[,<nfs-options>]

参数nfsroot告诉内核以哪一台机器的哪个目录以及哪个网络文件系统选项作为根文件系统使用。

<server-ip> 指定网络文件系统服务端的IP地址。如果没有指定定,则使用nfsaddrs变量指定的值。

<root-dir> 服务端上要作为根文件系统要挂载的目录名称。

<nfs-options>--标准的网络文件系统选项,所有选项都以逗号分开。如果没有给定此选项栏位则使用下列的预设值:

        port = as given by server portmap daemon

        rsize = 1024

        wsize = 1024

        timeo = 7

        retrans = 3

        acregmin = 3

        acregmax = 60

        acdirmin = 30

        acdirmax = 60

        flags = hard, nointr, noposix, cto, ac

c、nfsaddrs=<my-ip>:<serv-ip>:<gw-ip >:<netmask>:<name>:<dev>:<auto>

参数ip设定网络通讯所需的各种网络接口地址。 如果没有给定这个参数,则内核核会试着使用反向地址解析协议或是启动协议(BOOTP)以找出这些参数。

<ipaddr> 客户端的IP地址。

<serverip> 网络文件系统服务端的IP地址。

<gatewayip> 网关(gateway)的IP地址。

<netmask> 本地网络的网络掩码。如果为空白,则掩码由客户端的IP地址导出。

<name> 客户端的名称。如果空白,则使用客户端IP地址的ASCII标记值。

<dev> 要使用的网络设备名称。如果你只有一个设备,那么你可以不管它。一般指定为eth0。

<auto> 用以作为自动配置的方法。,可以是on可以是off。

d、init=/linuxrc

指定初始化文件

e、console=ttySAC2,115200

控制台选择

2、启动网络功能,Networking options。

[*] Networking support --->
    Networking options --->
        [*]  IP: kernel level autoconfiguration

 3、启动 NFS 文件系统的功能。

File systems --->
    [*]  Network File Systems --->
        <*>  NFS client support
        [*]  Root file system on NFS

 4、重新编译并烧录内核。

 二、挂载 NFS 网络文件系统为系统根目录。

1、复制嵌入式linux文件系统到 NFS 共享路径下。

2、直接重新启动系统。

        首次系统会进行ip适配。

VFS: Mounted root (nfs filesystem) on device 0:11.
devtmpfs: mounted
Freeing unused kernel memory: 272K (c0560000 - c05a4000)
nfs: server 198.168.1.18 not responding, still trying
nfs: server 198.168.1.18 not responding, still trying
nfs: server 198.168.1.18 OK
nfs: server 198.168.1.18 OK


BusyBox v1.22.1 (2016-02-03 14:11:04 CST) built-in shell (ash)
Enter 'help' for a list of built-in commands.

~ #

         第二次启动系统。

VFS: Mounted root (nfs filesystem) on device 0:11.
devtmpfs: mounted
Freeing unused kernel memory: 272K (c0560000 - c05a4000)


BusyBox v1.22.1 (2016-02-03 14:11:04 CST) built-in shell (ash)
Enter 'help' for a list of built-in commands.

~ #

3、uboot下配置uboot网络参数相关环境变量。

        设置启动参数。开机 uboot 倒计时时输入任意键,进入 uboot 命令模式。

设置arm板的 ip

setenv ipaddr 198.168.1.25

设置 ubuntu 服务器的 ip

setenv serverip 198.168.1.18

 设置子网掩码 

setenv netmask 255.255.255.0

保存环境变量

saveenv

        测试 uboot 下是否可以接入网络。设置完成后arm板重上电,再次进入uboot 命令模式, 使用 ping 命令测试联网是否成功,若出现 host 198.168.1.18
is alive, 说明网络已经连接好了。

=> setenv ipaddr 198.168.1.25
=> setenv serverip 198.168.1.18
=> ping 198.168.1.1
Using emac device
host 198.168.1.1 is alive
=>

        arm板重新上电重启。 NFS 网络文件系统加载成功如下。

..........................................nuc970-emac0 nuc970-emac0: eth0 is OPENED
...................IP-Config: Complete:
     device=eth0, hwaddr=08:00:27:00:01:92, ipaddr=198.168.1.25, mask=255.255.255.0, gw=198.168.1.1
......     host=198.168.1.25, domain=, nis-domain=(none)

     bootserver=198.168.1.18, rootserver=198.168.1.18, rootpath=........
ALSA device list:
  #0: nuc970_IIS
........................................................................................
nt99141 2-002a: sensor_read: i2c read error, reg: 0x3000
nt99141 2-002a: sensor_read: i2c read error, reg: 0x3001

Sensor Chip_Version_H = 0x30(0x14) Chip_Version_L = 0x60(0x10)

driver i2c initial fail
nuvoton_cap_device_probe video0, main sensor registeration fail.
platform nuc970-videoin: Driver nuc970-cap requests probe deferral
VFS: Mounted root (nfs filesystem) on device 0:11.
devtmpfs: mounted
Freeing unused kernel memory: 272K (c0560000 - c05a4000)


BusyBox v1.22.1 (2016-02-03 14:11:04 CST) built-in shell (ash)
Enter 'help' for a list of built-in commands.

~ #

4、启动参数打印。

Kernel command line: noinitrd root=/dev/nfs nfsroot=198.168.1.18:/mnt/nfs/nuc970/rootfs,v3,nolock rw ip=198.168.1.25:198.168.1.18:198.168.1.1:255.255.255.0::eth0:off init=/linuxrc console=ttyS0,115200n8 mem=64M
..........................................nuc970-emac0 nuc970-emac0: eth0 is OPENED
...................IP-Config: Complete:
     device=eth0, hwaddr=08:00:27:00:01:92, ipaddr=198.168.1.25, mask=255.255.255.0, gw=198.168.1.1
......     host=198.168.1.25, domain=, nis-domain=(none)

     bootserver=198.168.1.18, rootserver=198.168.1.18, rootpath=........
ALSA device list:
  #0: nuc970_IIS
........................................................................................
nt99141 2-002a: sensor_read: i2c read error, reg: 0x3000
nt99141 2-002a: sensor_read: i2c read error, reg: 0x3001

Sensor Chip_Version_H = 0x30(0x14) Chip_Version_L = 0x60(0x10)

driver i2c initial fail
nuvoton_cap_device_probe video0, main sensor registeration fail.
platform nuc970-videoin: Driver nuc970-cap requests probe deferral
VFS: Mounted root (nfs filesystem) on device 0:11.
devtmpfs: mounted
Freeing unused kernel memory: 272K (c0560000 - c05a4000)


BusyBox v1.22.1 (2016-02-03 14:11:04 CST) built-in shell (ash)
Enter 'help' for a list of built-in commands.

~ #

附:

1、打印uboot环境变量

=> printenv
baudrate=115200
bootcmd=nboot 0x7fc0 0 0x200000; bootm 0x7fc0
bootdelay=1
ethact=emac
ethaddr=00:00:00:11:66:88
ipaddr=198.168.1.25
netmask=255.255.255.0
serverip=198.168.1.18
stderr=serial
stdin=serial
stdout=serial

Environment size: 240/65532 bytes
=>

2、nfsroot参数详解

        有关nfs的详细说明,在内核文档中:kernel//Documentation/filesystems/nfsroot.txt。

Mounting the root filesystem via NFS (nfsroot)
===============================================

Written 1996 by Gero Kuhlmann <[email protected]>
Updated 1997 by Martin Mares <[email protected]>
Updated 2006 by Nico Schottelius <[email protected]>
Updated 2006 by Horms <[email protected]>



In order to use a diskless system, such as an X-terminal or printer server
for example, it is necessary for the root filesystem to be present on a
non-disk device. This may be an initramfs (see Documentation/filesystems/
ramfs-rootfs-initramfs.txt), a ramdisk (see Documentation/initrd.txt) or a
filesystem mounted via NFS. The following text describes on how to use NFS
for the root filesystem. For the rest of this text 'client' means the
diskless system, and 'server' means the NFS server.




1.) Enabling nfsroot capabilities
    -----------------------------

In order to use nfsroot, NFS client support needs to be selected as
built-in during configuration. Once this has been selected, the nfsroot
option will become available, which should also be selected.

In the networking options, kernel level autoconfiguration can be selected,
along with the types of autoconfiguration to support. Selecting all of
DHCP, BOOTP and RARP is safe.




2.) Kernel command line
    -------------------

When the kernel has been loaded by a boot loader (see below) it needs to be
told what root fs device to use. And in the case of nfsroot, where to find
both the server and the name of the directory on the server to mount as root.
This can be established using the following kernel command line parameters:


root=/dev/nfs

  This is necessary to enable the pseudo-NFS-device. Note that it's not a
  real device but just a synonym to tell the kernel to use NFS instead of
  a real device.


nfsroot=[<server-ip>:]<root-dir>[,<nfs-options>]

  If the `nfsroot' parameter is NOT given on the command line,
  the default "/tftpboot/%s" will be used.

  <server-ip>   Specifies the IP address of the NFS server.
                The default address is determined by the `ip' parameter
                (see below). This parameter allows the use of different
                servers for IP autoconfiguration and NFS.

  <root-dir>    Name of the directory on the server to mount as root.
                If there is a "%s" token in the string, it will be
                replaced by the ASCII-representation of the client's
                IP address.

  <nfs-options> Standard NFS options. All options are separated by commas.
                The following defaults are used:
                        port            = as given by server portmap daemon
                        rsize           = 4096
                        wsize           = 4096
                        timeo           = 7
                        retrans         = 3
                        acregmin        = 3
                        acregmax        = 60
                        acdirmin        = 30
                        acdirmax        = 60
                        flags           = hard, nointr, noposix, cto, ac


ip=<client-ip>:<server-ip>:<gw-ip>:<netmask>:<hostname>:<device>:<autoconf>:
   <dns0-ip>:<dns1-ip>

  This parameter tells the kernel how to configure IP addresses of devices
  and also how to set up the IP routing table. It was originally called
  `nfsaddrs', but now the boot-time IP configuration works independently of
  NFS, so it was renamed to `ip' and the old name remained as an alias for
  compatibility reasons.

  If this parameter is missing from the kernel command line, all fields are
  assumed to be empty, and the defaults mentioned below apply. In general
  this means that the kernel tries to configure everything using
  autoconfiguration.

  The <autoconf> parameter can appear alone as the value to the `ip'
  parameter (without all the ':' characters before).  If the value is
  "ip=off" or "ip=none", no autoconfiguration will take place, otherwise
  autoconfiguration will take place.  The most common way to use this
  is "ip=dhcp".

  <client-ip>   IP address of the client.

                Default:  Determined using autoconfiguration.

  <server-ip>   IP address of the NFS server. If RARP is used to determine
                the client address and this parameter is NOT empty only
                replies from the specified server are accepted.

                Only required for NFS root. That is autoconfiguration
                will not be triggered if it is missing and NFS root is not
                in operation.

                Default: Determined using autoconfiguration.
                         The address of the autoconfiguration server is used.

  <gw-ip>       IP address of a gateway if the server is on a different subnet.

                Default: Determined using autoconfiguration.

  <netmask>     Netmask for local network interface. If unspecified
                the netmask is derived from the client IP address assuming
                classful addressing.

                Default:  Determined using autoconfiguration.

  <hostname>    Name of the client. May be supplied by autoconfiguration,
                but its absence will not trigger autoconfiguration.
                If specified and DHCP is used, the user provided hostname will
                be carried in the DHCP request to hopefully update DNS record.

                Default: Client IP address is used in ASCII notation.

  <device>      Name of network device to use.

                Default: If the host only has one device, it is used.
                         Otherwise the device is determined using
                         autoconfiguration. This is done by sending
                         autoconfiguration requests out of all devices,
                         and using the device that received the first reply.

  <autoconf>    Method to use for autoconfiguration. In the case of options
                which specify multiple autoconfiguration protocols,
                requests are sent using all protocols, and the first one
                to reply is used.

                Only autoconfiguration protocols that have been compiled
                into the kernel will be used, regardless of the value of
                this option.

                  off or none: don't use autoconfiguration
                                (do static IP assignment instead)
                  on or any:   use any protocol available in the kernel
                               (default)
                  dhcp:        use DHCP
                  bootp:       use BOOTP
                  rarp:        use RARP
                  both:        use both BOOTP and RARP but not DHCP
                               (old option kept for backwards compatibility)

                Default: any

  <dns0-ip>     IP address of first nameserver.
                Value gets exported by /proc/net/pnp which is often linked
                on embedded systems by /etc/resolv.conf.

  <dns1-ip>     IP address of secound nameserver.
                Same as above.


nfsrootdebug

  This parameter enables debugging messages to appear in the kernel
  log at boot time so that administrators can verify that the correct
  NFS mount options, server address, and root path are passed to the
  NFS client.


rdinit=<executable file>

  To specify which file contains the program that starts system
  initialization, administrators can use this command line parameter.
  The default value of this parameter is "/init".  If the specified
  file exists and the kernel can execute it, root filesystem related
  kernel command line parameters, including `nfsroot=', are ignored.

  A description of the process of mounting the root file system can be
  found in:

    Documentation/early-userspace/README




3.) Boot Loader
    ----------

To get the kernel into memory different approaches can be used.
They depend on various facilities being available:


3.1)  Booting from a floppy using syslinux

        When building kernels, an easy way to create a boot floppy that uses
        syslinux is to use the zdisk or bzdisk make targets which use zimage
        and bzimage images respectively. Both targets accept the
        FDARGS parameter which can be used to set the kernel command line.

        e.g.
           make bzdisk FDARGS="root=/dev/nfs"

        Note that the user running this command will need to have
        access to the floppy drive device, /dev/fd0

        For more information on syslinux, including how to create bootdisks
        for prebuilt kernels, see http://syslinux.zytor.com/

        N.B: Previously it was possible to write a kernel directly to
             a floppy using dd, configure the boot device using rdev, and
             boot using the resulting floppy. Linux no longer supports this
             method of booting.

3.2) Booting from a cdrom using isolinux

        When building kernels, an easy way to create a bootable cdrom that
        uses isolinux is to use the isoimage target which uses a bzimage
        image. Like zdisk and bzdisk, this target accepts the FDARGS
        parameter which can be used to set the kernel command line.

        e.g.
          make isoimage FDARGS="root=/dev/nfs"

        The resulting iso image will be arch/<ARCH>/boot/image.iso
        This can be written to a cdrom using a variety of tools including
        cdrecord.

        e.g.
          cdrecord dev=ATAPI:1,0,0 arch/x86/boot/image.iso

        For more information on isolinux, including how to create bootdisks
        for prebuilt kernels, see http://syslinux.zytor.com/

3.2) Using LILO
        When using LILO all the necessary command line parameters may be
        specified using the 'append=' directive in the LILO configuration
        file.

        However, to use the 'root=' directive you also need to create
        a dummy root device, which may be removed after LILO is run.

        mknod /dev/boot255 c 0 255

        For information on configuring LILO, please refer to its documentation.

3.3) Using GRUB
        When using GRUB, kernel parameter are simply appended after the kernel
        specification: kernel <kernel> <parameters>

3.4) Using loadlin
        loadlin may be used to boot Linux from a DOS command prompt without
        requiring a local hard disk to mount as root. This has not been
        thoroughly tested by the authors of this document, but in general
        it should be possible configure the kernel command line similarly
        to the configuration of LILO.

        Please refer to the loadlin documentation for further information.

3.5) Using a boot ROM
        This is probably the most elegant way of booting a diskless client.
        With a boot ROM the kernel is loaded using the TFTP protocol. The
        authors of this document are not aware of any no commercial boot
        ROMs that support booting Linux over the network. However, there
        are two free implementations of a boot ROM, netboot-nfs and
        etherboot, both of which are available on sunsite.unc.edu, and both
        of which contain everything you need to boot a diskless Linux client.

3.6) Using pxelinux
        Pxelinux may be used to boot linux using the PXE boot loader
        which is present on many modern network cards.

        When using pxelinux, the kernel image is specified using
        "kernel <relative-path-below /tftpboot>". The nfsroot parameters
        are passed to the kernel by adding them to the "append" line.
        It is common to use serial console in conjunction with pxeliunx,
        see Documentation/serial-console.txt for more information.

        For more information on isolinux, including how to create bootdisks
        for prebuilt kernels, see http://syslinux.zytor.com/




4.) Credits
    -------

  The nfsroot code in the kernel and the RARP support have been written
  by Gero Kuhlmann <[email protected]>.

  The rest of the IP layer autoconfiguration code has been written
  by Martin Mares <[email protected]>.

  In order to write the initial version of nfsroot I would like to thank
  Jens-Uwe Mager <[email protected]> for his help.

Guess you like

Origin blog.csdn.net/weixin_43782998/article/details/121236902