Mount a File Storage NAS Network File System (NFS) file system on a Linux ECS instance to allow multiple ECS instances to share and access its data.
Prerequisites
A Linux ECS instance is created. For more information, see Create an ECS instance.
You have created a NAS file system in the same region and VPC as the ECS instance and obtained its mount target address. For more information, see Create a file system.
Step 1: Install an NFS client
Before mounting an NFS file system on a Linux ECS instance, you must install an NFS client. The client only needs to be installed once per server.
Connect to the ECS instance. For more information, see Connect to an ECS instance.
Install an NFS client.
Operating system
Installation command
Alibaba Cloud Linux
sudo yum install nfs-utilsCentOS
Red Hat
Ubuntu
Run the following commands in order:
sudo apt-get updatesudo apt-get install nfs-common
Debian
(Optional) Configure the maximum number of concurrent NFS requests.
Run the following command to change the maximum number of concurrent client requests to 128. For more information, see How do I modify the maximum number of concurrent NFS requests?.
if (lsmod | grep sunrpc); then (modinfo sunrpc | grep tcp_max_slot_table_entries) && sysctl -w sunrpc.tcp_max_slot_table_entries=128 (modinfo sunrpc | grep tcp_slot_table_entries) && sysctl -w sunrpc.tcp_slot_table_entries=128 fi (modinfo sunrpc | grep tcp_max_slot_table_entries) && echo "options sunrpc tcp_max_slot_table_entries=128" >> /etc/modprobe.d/sunrpc.conf (modinfo sunrpc | grep tcp_slot_table_entries) && echo "options sunrpc tcp_slot_table_entries=128" >> /etc/modprobe.d/sunrpc.conf
Step 2: Mount the NFS file system
On Linux, you can mount a File Storage NAS NFS file system manually or automatically. Manual mounts are temporary and must be redone after a server reboot, whereas automatic mounts are persistent. To ensure the mount persists across reboots, we recommend configuring automatic mounting after a successful manual mount.
Manually mount the NFS file system
Mount the NFS file system on your Linux ECS instance by using the domain name of the mount target.
For optimal performance, we recommend that you mount the file system by using the NFSv3 protocol.
NFSv4.0 supports file locks, including range locks. If you need to modify a single file from multiple Linux ECS instances at the same time, mount the file system by using the NFSv4.0 protocol.
File Storage NAS supports one-click mounting of file systems in the console to provide a more convenient and faster user experience. We recommend that you use the one-click mount feature. For more information, see One-click mount an NFS file system.
Mount the NFS file system.
If you are using a General-purpose NAS file system, run one of the following commands.
NFSv3 protocol
sudo mount -t nfs -o vers=3,nolock,proto=tcp,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport file-system-id.region.nas.aliyuncs.com:/ /mntNFSv4.0 protocol
sudo mount -t nfs -o vers=4,minorversion=0,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport file-system-id.region.nas.aliyuncs.com:/ /mntIf you are using an Extreme NAS file system, run the following command.
sudo mount -t nfs -o vers=3,nolock,noacl,proto=tcp,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport file-system-id.region.extreme.nas.aliyuncs.com:/share /mnt
The following table describes the mount command options.
Parameter
Description
General-purpose NAS: file-system-id.region.nas.aliyuncs.com:/ /mnt
Extreme NAS: file-system-id.region.extreme.nas.aliyuncs.com:/share /mnt
The format is <domain name of the mount target>:<NAS file system directory> <local mount path on the current server>. Replace the placeholders with the actual values for your environment.
Domain name of the mount target: You can find the domain name of the mount target in the File Storage NAS console. Go to the File System List page, click Manage next to the target file system, and then go to the Mount Targets page. For more information, see Manage mount targets.
NAS file system directory:
General-purpose NAS: The root directory (/) or any subdirectory, such as /share. If you use a subdirectory, ensure that it already exists in the NAS file system.
Extreme NAS: The path must start with /share. For example, /share or /share/subdir. If you specify a subdirectory, make sure that the subdirectory already exists in the NAS file system.
Local mount path on the current server: any subdirectory on the Linux ECS instance (for example, /mnt). If you specify a subdirectory, make sure that it exists.
NoteIf the local mount path is not empty, its contents are hidden after a successful mount, and the directory displays the data from the NAS file system. We recommend that you use an empty directory for mounting.
vers
The protocol version of the file system.
vers=3: Mounts the file system by using the NFSv3 protocol.
vers=4: Mounts the file system by using the NFSv4 protocol.
In this parameter,
minorversionspecifies the minor version number of the protocol. For example, NAS supports version 4.0 of the NFS v4 protocol. Therefore, when you mount a file system by using the NFS v4 protocol, the minor version number is 0.
NoteGeneral-purpose NAS file systems support NFSv3 and NFSv4.0.
Extreme NAS file systems support only NFSv3 and do not support NFSv4.
rsize
Defines the maximum size of data blocks that the client can read from the file system.
Recommended value: 1048576.
wsize
Defines the maximum size of data blocks that the client can write to the file system.
Recommended value: 1048576.
hard
Without this option, the client may immediately return an error if File Storage NAS becomes unavailable and will not retry the operation. This can lead to data inconsistency or loss.
We recommend that you enable this option.
timeo
The time in deciseconds (0.1 seconds) that the NFS client waits for a response before it retries a request.
Recommended value: 600 (60 seconds).
retrans
The number of times the NFS client retries a request.
Recommended value: 2.
noresvport
Instructs the NFS client to use a new TCP port when a network connection is re-established. This ensures that the connection is not interrupted after a network failure and recovery.
We recommend that you enable this option.
NoteIf you must change the timeout parameter (timeo), we recommend that you use a value of 150 or greater. The unit for the timeo parameter is 0.1 seconds, so a value of 150 represents 15 seconds.
If you need to change the I/O size parameters (rsize and wsize), we recommend that you use the maximum value (1048576) to avoid performance degradation.
These parameters are optional. If used, separate them with commas.
ImportantWe do not recommend using the soft option because it risks data inconsistency.
To avoid performance degradation, do not use mount options other than the recommended defaults.
Verify the mount.
Run the following command:
mount -lSample output:
Output similar to the following indicates a successful mount.
mqueue on /dev/mqueue type mqueue (rw,relatime) hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime) tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,size=786256k,mode=700) 29xxxxx.nas.aliyuncs.com:/ on / type nfs4 (rw,relatime,vers=4.0,rsize=1048576,wsize=1048576,namlen=255,hard,noresvport,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.63.232,local_lock=none,addr=192.168.63.228) [root@iz0jlxxx ~]#After a successful mount, you can also run the
df -hcommand to view the capacity information of the file system.[root@iZ0jl0xxx ~]# df -h Filesystem Size Used Avail Use% Mounted on 29cxxx.nas.aliyuncs.com:/ 40G 2.1G 36G 6% / devtmpfs 3.8G 0 3.8G 0% /dev tmpfs 3.8G 0 3.8G 0% /dev/shm tmpfs 3.8G 424K 3.8G 1% /run tmpfs 3.8G 0 3.8G 0% /sys/fs/cgroup tmpfs 768M 0 768M 0% /run/user/0
If the mount fails, troubleshoot the issue. For more information, see Troubleshoot file system mount failures.
After a successful mount, you can read from and write to the NAS file system on the Linux ECS instance.
You can access and use the NAS file system like a regular directory. Example:
[root@ixxx ~]# mkdir /mnt/dir1 [root@ixxx ~]# mkdir /mnt/dir2 [root@ixxx ~]# touch /mnt/file1 [root@ixxx ~]# echo 'some file conent' > /mnt/file2 [root@ixxx ~]# ls /mnt dir1 dir2 file1 file2 tmp
(Optional) Automatically mount the NFS file system
You can configure the /etc/fstab file on a Linux ECS instance to automatically mount an NFS file system when the instance restarts.
Before you configure automatic mounting, ensure that the manual mount is successful. This prevents ECS startup failures.
If you are using an Extreme NAS file system, perform the operations in this step.
If you are using a General-purpose NAS file system, skip this step and go to Step 2.
vi /etc/systemd/system/sockets.target.wants/rpcbind.socketOpen the /etc/systemd/system/sockets.target.wants/rpcbind.socket file. You must comment out the IPv6-related rpcbind parameters (
BindIPv6Only=ipv6-only,ListenStream=[::]:111, andListenDatagram=[::]:111) and keep only the IPv4 listening address. Otherwise, therpcbindservice for NFS will fail to start automatically.Description=RPCbind Server Activation Socket [Socket] ListenStream=/var/run/rpcbind.sock # RPC netconfig can't handle ipv6/ipv4 dual sockets #BindIPv6Only=ipv6-only ListenStream=0.0.0.0:111 ListenDatagram=0.0.0.0:111 #ListenStream=[::]:111 #ListenDatagram=[::]:111 [Install] WantedBy=sockets.targetIf you configure automatic mounting on a CentOS 6.x system, you must also perform the following operations.
Run the
chkconfig netfs oncommand to ensure that the netfs service starts automatically on boot.Open the /etc/netconfig configuration file and comment out the inet6-related content as shown below. To disable the IPv6 protocol, add a
#symbol to the beginning of the lines for theudp6andtcp6entries.# udp tpi_clts v inet udp - - tcp tpi_cots_ord v inet tcp - - #udp6 tpi_clts v inet6 udp - - #tcp6 tpi_cots_ord v inet6 tcp - - rawip tpi_raw - inet - - - local tpi_cots_ord - loopback - - - unix tpi_cots_ord - loopback - - -
Open the /etc/fstab configuration file and add a mount configuration.
Replace the following variables in the configuration:
<file-system-id.region.nas.aliyuncs.com>: The NAS mount target address (for example, General-purpose NAS:
237*******-*****.cn-hangzhou.nas.aliyuncs.com, Extreme NAS:00a*****-****.cn-hangzhou.extreme.nas.aliyuncs.com).</mnt>: Replace with the local mount path. For example:
/mnt.
General-purpose NAS
To mount the file system by using the NFSv3 protocol:
file-system-id.region.nas.aliyuncs.com:/ /mnt nfs vers=3,nolock,proto=tcp,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,_netdev,noresvport 0 0To mount the file system by using the NFSv4.0 protocol:
file-system-id.region.nas.aliyuncs.com:/ /mnt nfs vers=4,minorversion=0,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,_netdev,noresvport 0 0
Extreme NAS
file-system-id.region.extreme.nas.aliyuncs.com:/share /mnt nfs vers=3,nolock,noacl,proto=tcp,noresvport,_netdev 0 0For descriptions of the main parameters in the example, see the Mount Command Parameter Description Table. The other parameters are described as follows.
Parameter
Description
_netdev
Prevents the client from attempting to mount the file system before the network is ready.
0 (first item after noresvport)
A non-zero value specifies that the
dumputility should back up the file system. For a NAS file system, this value must be 0.0 (second item after noresvport)
Specifies the order for the
fsckutility to check file systems at startup. For a NAS file system, this value must be 0 to disable the check.Run the following commands to configure the startup file /etc/rc.local.
[ ! -f /etc/rc.local ] && echo '#!/bin/bash' > /etc/rc.local; echo "for ((i=1; i<=10; i++)); do if ping -c 1 -W 3 aliyuncs.com; then break; else sleep 1; fi; done" >> /etc/rc.local; echo "sleep 3; mount -a -t nfs" >> /etc/rc.local; chmod +x /etc/rc.localRun the
rebootcommand to reboot the ECS instance.ImportantRestarting an ECS instance interrupts its services. We recommend performing this operation during off-peak hours to minimize disruption.
Verify that the automatic mount configuration is successful.
Within one minute after the ECS restart, run the
df -hcommand to confirm the mounted NAS file system.