失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 嵌入式linux开发 开机NFS文件系统启动 使用nfs挂载根文件系统

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

时间:2024-05-08 04:52:52

相关推荐

嵌入式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=asgivenbyserverportmapdaemon

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、启动网络功能,Networkingoptions。

[*] Networking support --->Networkingoptions --->[*] 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: mountedFreeing unused kernel memory: 272K (c0560000 - c05a4000)nfs: server 198.168.1.18 not responding, still tryingnfs: server 198.168.1.18 not responding, still tryingnfs: server 198.168.1.18 OKnfs: server 198.168.1.18 OKBusyBox v1.22.1 (-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: mountedFreeing unused kernel memory: 272K (c0560000 - c05a4000)BusyBox v1.22.1 (-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.1Using emac devicehost 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: 0x3000nt99141 2-002a: sensor_read: i2c read error, reg: 0x3001Sensor Chip_Version_H = 0x30(0x14) Chip_Version_L = 0x60(0x10)driver i2c initial failnuvoton_cap_device_probe video0, main sensor registeration fail.platform nuc970-videoin: Driver nuc970-cap requests probe deferralVFS: Mounted root (nfs filesystem) on device 0:11.devtmpfs: mountedFreeing unused kernel memory: 272K (c0560000 - c05a4000)BusyBox v1.22.1 (-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: 0x3000nt99141 2-002a: sensor_read: i2c read error, reg: 0x3001Sensor Chip_Version_H = 0x30(0x14) Chip_Version_L = 0x60(0x10)driver i2c initial failnuvoton_cap_device_probe video0, main sensor registeration fail.platform nuc970-videoin: Driver nuc970-cap requests probe deferralVFS: Mounted root (nfs filesystem) on device 0:11.devtmpfs: mountedFreeing unused kernel memory: 272K (c0560000 - c05a4000)BusyBox v1.22.1 (-02-03 14:11:04 CST) built-in shell (ash)Enter 'help' for a list of built-in commands.~ #

附:

1、打印uboot环境变量

=> printenvbaudrate=115200bootcmd=nboot 0x7fc0 0 0x200000; bootm 0x7fc0bootdelay=1ethact=emacethaddr=00:00:00:11:66:88ipaddr=198.168.1.25netmask=255.255.255.0serverip=198.168.1.18stderr=serialstdin=serialstdout=serialEnvironment size: 240/65532 bytes=>

2、nfsroot参数详解

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

Mounting the root filesystem via NFS (nfsroot)===============================================Written 1996 by Gero Kuhlmann <gero@gkminix.han.de>Updated 1997 by Martin Mares <mj@atrey.karlin.mff.cuni.cz>Updated by Nico Schottelius <nico-kernel-nfsroot@>Updated by Horms <horms@.au>In order to use a diskless system, such as an X-terminal or printer serverfor example, it is necessary for the root filesystem to be present on anon-disk device. This may be an initramfs (see Documentation/filesystems/ramfs-rootfs-initramfs.txt), a ramdisk (see Documentation/initrd.txt) or afilesystem mounted via NFS. The following text describes on how to use NFSfor the root filesystem. For the rest of this text 'client' means thediskless system, and 'server' means the NFS server.1.) Enabling nfsroot capabilities-----------------------------In order to use nfsroot, NFS client support needs to be selected asbuilt-in during configuration. Once this has been selected, the nfsrootoption 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 ofDHCP, BOOTP and RARP is safe.2.) Kernel command line-------------------When the kernel has been loaded by a boot loader (see below) it needs to betold what root fs device to use. And in the case of nfsroot, where to findboth 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/nfsThis is necessary to enable the pseudo-NFS-device. Note that it's not areal device but just a synonym to tell the kernel to use NFS instead ofa 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 differentservers 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 bereplaced by the ASCII-representation of the client'sIP address.<nfs-options> Standard NFS options. All options are separated by commas.The following defaults are used:port = as given by server portmap daemonrsize = 4096wsize = 4096timeo = 7retrans = 3acregmin = 3acregmax = 60acdirmin = 30acdirmax = 60flags = hard, nointr, noposix, cto, acip=<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 devicesand also how to set up the IP routing table. It was originally called`nfsaddrs', but now the boot-time IP configuration works independently ofNFS, so it was renamed to `ip' and the old name remained as an alias forcompatibility reasons.If this parameter is missing from the kernel command line, all fields areassumed to be empty, and the defaults mentioned below apply. In generalthis means that the kernel tries to configure everything usingautoconfiguration.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, otherwiseautoconfiguration will take place. The most common way to use thisis "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 determinethe client address and this parameter is NOT empty onlyreplies from the specified server are accepted.Only required for NFS root. That is autoconfigurationwill not be triggered if it is missing and NFS root is notin 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 unspecifiedthe netmask is derived from the client IP address assumingclassful 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 willbe 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 usingautoconfiguration. This is done by sendingautoconfiguration requests out of all devices,and using the device that received the first reply.<autoconf> Method to use for autoconfiguration. In the case of optionswhich specify multiple autoconfiguration protocols,requests are sent using all protocols, and the first oneto reply is used.Only autoconfiguration protocols that have been compiledinto the kernel will be used, regardless of the value ofthis 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 DHCPbootp: use BOOTPrarp: use RARPboth: 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 linkedon embedded systems by /etc/resolv.conf.<dns1-ip>IP address of secound nameserver.Same as above.nfsrootdebugThis parameter enables debugging messages to appear in the kernellog at boot time so that administrators can verify that the correctNFS mount options, server address, and root path are passed to theNFS client.rdinit=<executable file>To specify which file contains the program that starts systeminitialization, administrators can use this command line parameter.The default value of this parameter is "/init". If the specifiedfile exists and the kernel can execute it, root filesystem relatedkernel command line parameters, including `nfsroot=', are ignored.A description of the process of mounting the root file system can befound in:Documentation/early-userspace/README3.) 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 syslinuxWhen building kernels, an easy way to create a boot floppy that usessyslinux is to use the zdisk or bzdisk make targets which use zimageand bzimage images respectively. Both targets accept theFDARGS 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 haveaccess to the floppy drive device, /dev/fd0For more information on syslinux, including how to create bootdisksfor prebuilt kernels, see /N.B: Previously it was possible to write a kernel directly toa floppy using dd, configure the boot device using rdev, andboot using the resulting floppy. Linux no longer supports thismethod of booting.3.2) Booting from a cdrom using isolinuxWhen building kernels, an easy way to create a bootable cdrom thatuses isolinux is to use the isoimage target which uses a bzimageimage. Like zdisk and bzdisk, this target accepts the FDARGSparameter 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.isoThis can be written to a cdrom using a variety of tools includingcdrecord.e.g.cdrecord dev=ATAPI:1,0,0 arch/x86/boot/image.isoFor more information on isolinux, including how to create bootdisksfor prebuilt kernels, see /3.2) Using LILOWhen using LILO all the necessary command line parameters may bespecified using the 'append=' directive in the LILO configurationfile.However, to use the 'root=' directive you also need to createa dummy root device, which may be removed after LILO is run.mknod /dev/boot255 c 0 255For information on configuring LILO, please refer to its documentation.3.3) Using GRUBWhen using GRUB, kernel parameter are simply appended after the kernelspecification: kernel <kernel> <parameters>3.4) Using loadlinloadlin may be used to boot Linux from a DOS command prompt withoutrequiring a local hard disk to mount as root. This has not beenthoroughly tested by the authors of this document, but in generalit should be possible configure the kernel command line similarlyto the configuration of LILO.Please refer to the loadlin documentation for further information.3.5) Using a boot ROMThis is probably the most elegant way of booting a diskless client.With a boot ROM the kernel is loaded using the TFTP protocol. Theauthors of this document are not aware of any no commercial bootROMs that support booting Linux over the network. However, thereare two free implementations of a boot ROM, netboot-nfs andetherboot, both of which are available on sunsite.unc.edu, and bothof which contain everything you need to boot a diskless Linux client.3.6) Using pxelinuxPxelinux may be used to boot linux using the PXE boot loaderwhich is present on many modern network cards.When using pxelinux, the kernel image is specified using"kernel <relative-path-below /tftpboot>". The nfsroot parametersare 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 bootdisksfor prebuilt kernels, see /4.) Credits-------The nfsroot code in the kernel and the RARP support have been writtenby Gero Kuhlmann <gero@gkminix.han.de>.The rest of the IP layer autoconfiguration code has been writtenby Martin Mares <mj@atrey.karlin.mff.cuni.cz>.In order to write the initial version of nfsroot I would like to thankJens-Uwe Mager <jum@anubis.han.de> for his help.

如果觉得《嵌入式linux开发 开机NFS文件系统启动 使用nfs挂载根文件系统》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。