U-boot无法使用NFS从Ubuntu中下载文件

最近在调试一块Linux板卡的时候需要在U-boot中挂载Linux镜像,但是发现总是超时挂载失败。

失败时的log输出

因为板卡是可以ping通Ubuntu主机的,所以可以基本可以排除是本地网络的问题。

既然考虑是NFS的问题,那么就需要先确定NFS的配置是否正确

先查看/etc/exports文件中的配置是否正确,确认无误后确定NFS根目录的权限是否正常,这些都确认没有问题后就可以进行下一步的检查了。

首先查看nfs的版本:

sudo cat /proc/fs/nfsd/versions
输出:
-2 +3 +4 +4.1 +4.2

可以看到当前Ubuntu主机没有支持V2,但是U-boot需要使用V2,所以这里需要打开NFS V2的支持。

这里有两种方式打开,如果是Ubuntu18.04的话,需要编辑配置文件的内容。

sudo vim /etc/default/nfs-kernel-server 
# Number of servers to start up
#RPCNFSDCOUNT=8
RPCNFSDCOUNT="-V 2 8"

# Runtime priority of server (see nice(1))
RPCNFSDPRIORITY=0

# Options for rpc.mountd.
# If you have a port-based firewall, you might want to set up
# a fixed port here using the --port option. For more information, 
# see rpc.mountd(8) or http://wiki.debian.org/SecuringNFS
# To disable NFSv4 on the server, specify '--no-nfs-version 4' here
#RPCMOUNTDOPTS="--manage-gids"
RPCMOUNTDOPTS="-V 2 --manage-gids"

# Do you want to start the svcgssd daemon? It is only required for Kerberos
# exports. Valid alternatives are "yes" and "no"; the default is "no".
NEED_SVCGSSD=""

# Options for rpc.svcgssd.
#RPCSVCGSSDOPTS=""
RPCSVCGSSDOPTS="--nfs-version 2,3,4 --debug --syslog"

修改后保存,退出,然后重启NFS服务就可以了。

sudo /etc/init.d/nfs-kernel-server restart

如果是Ubuntu20.04需要修改NFS的配置文件

sudo vim /etc/nfs.conf
#
# This is a general configuration for the
# NFS daemons and tools
#
[general]
pipefs-directory=/run/rpc_pipefs
#
[exports]
# rootdir=/export
#
[exportfs]
# debug=0
#
[gssd]
# verbosity=0
# rpc-verbosity=0
# use-memcache=0
# use-machine-creds=1
# use-gss-proxy=0
# avoid-dns=1
# limit-to-legacy-enctypes=0
# context-timeout=0
# rpc-timeout=5
# keytab-file=/etc/krb5.keytab
# cred-cache-directory=
# preferred-realm=
#
[lockd]
# port=0
# udp-port=0
#
[mountd]
# debug=0
manage-gids=y
# descriptors=0
# port=0
# threads=1
# reverse-lookup=n
# state-directory-path=/var/lib/nfs
# ha-callout=
#
[nfsdcld]
# debug=0
# storagedir=/var/lib/nfs/nfsdcld
#
[nfsdcltrack]
# debug=0
# storagedir=/var/lib/nfs/nfsdcltrack
#
[nfsd]
# debug=0
# threads=8
# host=
# port=0
# grace-time=90
# lease-time=90
udp=y
# tcp=y
vers2=y
# vers3=y
# vers4=y
# vers4.0=y
# vers4.1=y
# vers4.2=y
# rdma=n
# rdma-port=20049
#
[statd]
# debug=0
# port=0
# outgoing-port=0
# name=
# state-directory-path=/var/lib/nfs/statd
# ha-callout=
# no-notify=0
#
[sm-notify]
# debug=0
# force=0
# retry-time=900
# outgoing-port=
# outgoing-addr=
# lift-grace=y
#
[svcgssd]
# principal=

这个文件需要修改两个地方,一个是开启Version2的支持,另一个如果当前NFS不支持UDP也需要开启UDP的支持。是否已经支持UDP可以使用:

$netstat -a | grep "nfs"
#没有支持
tcp        0      0 0.0.0.0:nfs             0.0.0.0:*               LISTEN     
tcp6       0      0 [::]:nfs                [::]:*                  LISTEN  

#支持
tcp        0      0 0.0.0.0:nfs             0.0.0.0:*               LISTEN     
tcp6       0      0 [::]:nfs                [::]:*                  LISTEN     
udp        0      0 0.0.0.0:nfs             0.0.0.0:*                          
udp6       0      0 [::]:nfs                [::]:*    

如果至此依旧不能解决问题,请确认系统防火墙以及杀毒软件是否误杀,可以先都关闭后尝试。