Embedded Linux mounts NFS network file system

background

About NFS

NFS (Network File System) is a distributed file system protocol used to share files between different operating systems. It allows files and directories to be shared between different computers on the network, improving the efficiency and security of data sharing. Originally developed by Sun Microsystems, NFS is now an open standard and is widely used in various operating systems and network devices. NFS uses a client-server model, where client computers can access shared files on the server as if they were local files.
In daily embedded Linux development, it is usually limited by the size of the hardware memory, and debugging requires frequently copying files from the host to the development board. So we can use NFS to mount the host directory on the board. Convenient for file transfer and expansion of development board storage space.
Because embedded Linux is often tailored, the corresponding NFS tools need to be transplanted.

structure

The server is deployed on the host machine and the development board is used as the client to mount nfs.

Implementation steps

prerequisite

  1. First, the development board needs to be connected to the host network.
  2. The development board kernel supports NFS function. You can cat /proc/filesystemcheck whether the node contains the word "ntfs". If it is not turned on, you can refer to: Enable nfs in the linux+ kernel and enable the nfs service in the linux kernel
  3. Install the ubuntu system on the host (other systems can also be referred)
  4. Run the tailored linux development board

Host configuration

  1. Install NFS client and server software. NFS software can usually be installed in Linux using the following command:
sudo apt-get install nfs-kernel-server nfs-common
  1. Create shared directory
sudo mkdir /nfsroot
sudo chmod -R 777 /nfsroot
  1. Configure NFS server. Edit the following configuration files:
sudo vim /etc/exports

In this file, you can specify which directories will be shared and to which client machines they will be shared. For example:

/nfsroot *(rw,sync,no_root_squash)

This will share /nfsroot the directory and allow the client machine to mount it in read/write mode.

  1. Reload NFS server configuration:
sudo exportfs -a
  1. Start the NFS server:
sudo systemctl restart nfs-kernel-server
  1. Verify:
    Execute the command on the host showmount -eto check whether the service is started normally.
$ showmount -e
Export list for <HOST name>:
/nfsroot *

The following command verifies whether the nfs server is successfully configured

sudo mount -t nfs 127.0.0.1:/nfsroot /mnt -o nolock
$ mount | grep mnt
127.0.0.1:/nfsroot on /mnt type nfs4 (rw,relatime,vers=4.2,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=127.0.0.1,local_lock=none,addr=127.0.0.1)

The result is as shown above, which proves that the host side configuration is completed.

Client source code compilation and transplantation

  1. First add the cross compiler path,
export PATH=$PATH:/<交叉编译工具链路径>/bin
  1. Obtain and compile the source code of the dynamic library libtirpc that nfsutil depends on. Download the source
    code of libtirpc: libtirpc
    executes the following instructions to compile, where –host is the cross-compilation tool chain (other platforms need to switch to the corresponding tool chain), –prefix is ​​the dynamic library installed after the compilation is completed. path
./configure --disable-gssapi --host=aarch64-buildroot-linux-gnu --prefix=<安装的路径>
make -j2
make install
  1. nfs-util source code acquisition and compilation
    . CC points to the cross-compilation tool. It should be noted that the path to the libtirpc dynamic library installation must be added after LDFLAGS. Because compilation has dependencies
./configure --disable-ipv6 --disable-uuid --disable-nfsv4 --disable-gss CC=aarch64-buildroot-linux-gnu-gcc --host=aarch64-buildroot-linux-gnu --prefix=<安装的路径> LDFLAGS="-L/<libtirpc 动态库安装的路径>/lib/"
make -j4
make install DESTDIR=<安装的路径>

Copy the compiled libtirpc.so.3.0.0 and the corresponding soft link files libtirpc.so libtirpc.so.3 to the directory on the board /lib( /usr/libusually included in the LD_LIBRARY_PATH path by default). And copy the compiled mount.nfs to /sbinthe directory on the development board

verify

Execute the following instructions on the development board to mount the nfs service

mount.nfs 10.43.0.38:/nfsroot /mnt -o nolock

Guess you like

Origin blog.csdn.net/weixin_43328157/article/details/132695755