Article title: a deep understanding of the Linux network file system. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Author: Ma Luyao
Network File System (NFS) is one of the easiest services to configure in Linux. Although its rules are simple, they have rich connotations. This publication introduced how to build NFS servers on Linux in Issue 1 this year. However, NFS still has many Unix/Linux enthusiasts, and even system administrators should pay attention to it. This article aims to help readers learn more about NFS.
NFS Server configuration
First, we will introduce the meaning of the Server and NFS-related files:
◆/Etc/exports is one of the most basic configuration files of the NFS Server. the file lists the shared file systems and hosts that allow access to these file systems. This file can represent the client list in the form of wildcards, network segments, and a group of host names. It has great flexibility and is generally configured by the system administrator.
◆/Proc/fs/nfs/exports kernel view of the exported file list, including the exported parameters.
◆/Var/lib/nfs/etab status file, which lists the current advanced list. This file contains all projects in the same format in/etc/exports and projects manually imported by exportfs-I.
◆/Var/lib/nfs/rmtab status file, which lists the remote clients attached to the exported file.
◆ Var/lib/nfs/xtab status file, current low-level export list.
For example,/etc/exports has the following line/data * .flying.com.cn (rw). Obviously, multiple hosts can match this domain. If both hosts, such as mail.flying.com.cn and ftp.flying.com.cn, request access to the file system, the/var/lib/nfs/xtab file contains two identical projects of the file system.
/Etc/exports is a plain text file. empty lines and lines starting with "#" are ignored. long lines can be divided into multiple lines by the slash. The file lists the file systems that allow NFS clients to access the server, and defines the export file system and access permissions.
The format of any row in the/etc/exports file is as follows:
Vol_name (volume name) indicates the file system to be exported. Other parameters except the volume name can be left blank. if the volume name is not displayed, the default parameter is used. If host_name1 (host name) is not written, any host can request the file system. You can specify either a host or a group of hosts. You can use the following four methods to describe:
◆ A single host can use a short name or a fully qualified name, or an IP address. for example, student01, student01.flying.com.cn, or 192.168.10.1 are valid host names.
◆ A network group can list all hosts defined in the/etc/netgroup file or NFS network group ING. The network group name starts with "@", for example, @ teacher or @ students.
◆ A wildcard host can use wildcards to list all hosts in a domain. the wildcard can use "*" and "?" "*" Matches 0 or more characters and "?" The wildcard character cannot match the dot in the host name. * .Flying.com.cn can match ftp01.flying.com.cn, but cannot match backup.ftp01.flying.com.cn, and *. * .flying.com.cn can match backup.ftp01.flying.com.cn. Ftp ??. Flying.com.cn can match ftp01.flying.com.cn and ftp02.flying.com.cn.
◆ The host network can be used to export specific subnets or subnets. the host network can match all hosts from 192.168.10.1 to 192.168.10.255 using 192.168.10.0/255.255.255.0. The export options are separated by commas (,), but cannot contain spaces. The length can be set as needed. Most options are Boolean, with values True or False. the options are related to performance or security. When the/etc/exports file does not provide a value, the system automatically uses the default value. You can use the man exportfs command to obtain the detailed usage of the options.
After changing the/etc/exports file, you can use the exportfs command to notify the server of the file change, or ignore the/etc/exports file and directly use the exportfs command to export the shared volume. Exportfs can specify the options that affect the export or non-export volumes. the options that affect one or more volumes are shown in Table 1.
NFS lock and remote quota monitoring program
You can lock the file in Unix to ensure that no two users write the same part of the file at the same time to ensure data consistency. The key to ensuring consistency is that the kernel needs to handle arbitration locks and write requests.
In distributed file systems such as NFS and Samba, many customers may require writing and locking the same file at the same time. At this time, only the server itself can assume the role of arbitration. The Unix/Linux method is to add an RPC protocol to handle the Lock operation, which is the NFS Lock Monitoring Program (NLM, NFS Lock Manager ). You can open the service without worrying about its specific running mode.
The rpc. rquotad monitoring program implements the RQUOTA protocol. in Linux, NFS servers always execute quotas, regardless of whether rpc. rquotad is running.
In fact, the reader can follow/etc/rc. in files under d/rc3.d or rc5.d, the number of portmap and nfslock nfs files starting with "S" is used to determine which service should be started first. On the author's machine, they are S13portmap, S14nfslock, and S60nfs, which clearly demonstrate the order of the above startup.
If the NFS Server is disabled before the client cancels the mounting, an error is obvious, so avoid it as much as possible.
NFS Client configuration
You can configure the NFS client by configuring the/etc/fstab file or running the mount command.
For file systems that need to be mounted at startup, they are often written to the/etc/fstab file, the host name, IP address, file system, Mount point, and options of the NFS server must be included. Most NFS shared volumes that are temporarily needed are manually mounted using the mount command. The parameters of the two methods are basically the same. The following uses the/etc/fstab file as an example to describe how to use the parameters when mounting an NFS volume.
The NFS volume in this file is generally written as follows:
Servername:/locations place mountpoint nfs option01, option02,... optionX
Table 2 lists general options for all file systems.
For NFS file systems, there are some unique options that can be used. they can be used together with general options in any order. Table 3 lists some mounting options available for NFS clients.
Note that the options of the NFS client and server are not necessarily the same, and sometimes there are conflicts. For example, if the server exports data in read-only mode, but the client mounts the data in writable mode, an error occurs when writing the data. Generally, the configuration of the server prevails when the server and client are in conflict.
NFS performance optimization and testing
1. synchronous or asynchronous writing
After learning about the NFS configuration options, let's take a look at the impact of these options on performance and security. Synchronous write (sync) on the Server can provide better stability, but it will slow down the disk speed. On high-load servers, applications often spend a lot of time waiting for the write to complete. Asynchronous write (async) means that the server first saves the data to the kernel memory (buffer) instead of writing the data to the disk immediately, and plans to write the data at a later time, return the successful write information to the client quickly. This method greatly improves performance, but sacrifices reliability. if the system crashes before writing the content in the buffer zone to the disk, data will be lost forever.
The NFS client can also use asynchronous writing to cache data written by the user process on the client. this is the default mode in Linux. This method makes the client faster, and only the data of the client is lost when the client crashes. Obviously, asynchronous write on the client is less risky than asynchronous write on the server. Since Linux is a fairly stable system, it is worthwhile to adopt the client asynchronous writing method to achieve better NFS performance.
2. select TCP or UDP
In the early days, Sun designed NFS v2 to only use the UDP protocol, mainly because of the influence of the memory, network speed, and CPU of the machines at that time, and had to choose a method with a lighter burden on the machines. In NFS v3, Sun chose the TCP protocol as the default transmission mode. In Linux, UDP is the default protocol. As a server, there is no choice. However, as a client, you can use the TCP protocol to interconnect with other Unix NFS servers that use TCP.
3. NFS performance testing
You can test the NFS read/write speed by testing the hard disk read/write speed. Note the following:
◆ Ensure that the host and network work normally, and the network devices such as hubs, switches, and routers work normally;
◆ Perform tests when the network is quiet, normal, and busy, to truly understand the actual and ideal conditions and the worst conditions;
◆ There is no need to spend more energy to improve performance.
In the basic test, you can use the ping tool to check the network conditions, ping the client from the server, and ping the server from the client. If there is no asymmetric routing problem, the results of the two methods should be similar. If the packet loss rate is less than 5%, NFS performance will be poor.
Run the following command to complete the test program:
Time dd if =/dev/zero of = nfs. dat bs = 4 k count = 4098
This command is used in nfs. write 4098 4 KB blocks in the dat file, that is, a 16 MB file. you can also change the command parameters as needed. Note that different file names are used in each experiment, otherwise, it will be related to the buffer.
The returned results may be as follows:
4098 + 0 records in 4098 + 0 records out real 0m23. 984 s user 0m0. 260 s sys 0m4. 080 s
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.