Mounting file shares on Compute Engine clients

This page shows you how to mount a Filestore file share on a Compute Engine virtual machine (VM) instance. If you're looking to access Filestore instances from Google Kubernetes Engine clusters instead, see Accessing file shares from Google Kubernetes Engine clusters.

Client VMs must be in the same Google Cloud project and VPC network as the Filestore instance. To learn how to mount file shares on remote clients, such as an on-premises terminal or VM, see Mounting file shares on clients in a remote network.

Before you begin

Ensure that the network firewall is properly configured for use with Filestore. See Configuring firewall rules for details.

Mounting a file share on a Compute Engine VM instance

Use one of the following procedures to mount a Filestore file share on a Compute Engine VM:

Linux: mount

To manually mount a file share to a Linux client VM:

  1. In the Cloud Console, go to the VM Instances page.

    Go to the VM instances page

  2. Locate the Linux VM you want to use as a client, then click SSH to open a terminal on that VM.

  3. Optional: Mounting a file share on a client VM with multiple network interfaces through a secondary network.

    Details

    If the client VM has multiple network interfaces and you want to mount the file share through a secondary network, namely an interface other than nic0, you must configure the client VM's routing policy to ensure that the file share mounts through the correct network interface. To do this, run the following command on the client VM:

    sudo ip route \
        add filestore-reserved-address-range \
        via default-gateway-of-nic-to-filestore
    

    where:

    • filestore-reserved-address-range is the reserved address range for use by the Filestore instance.
    • default-gateway-of-nic-to-filestore is the default gateway IP address of the NIC connected to the VPC network that is shared with the Filestore instance.

    For more information on using Compute Engine instances with multiple NICs, see Configuring policy routing.

  4. Install NFS:

    Debian/Ubuntu

    Use the following commands to install NFS on Debian or Ubuntu.

    sudo apt-get -y update &&
    sudo apt-get install nfs-common

    RHEL/CentOS

    Use the following commands to install NFS on Red Hat Enterprise Linux or CentOS.

    sudo yum update &&
    sudo yum install nfs-utils

    SUSE

    Use the following commands to install NFS on SUSE.

    sudo zypper update &&
    sudo zypper -n install nfs-client
  5. Make a local directory to map to the Filestore file share:

    sudo mkdir -p mount-point-directory
    

    where mount-point-directory is the directory to create, for example /mnt/filedir.

  6. Mount the file share from the Filestore instance by running the mount command. You can use any NFS mount options. For the best performance, we recommend using the default NFS mount options.

    Default NFS mount options

    Default option Description
    hard The NFS client retries NFS requests indefinitely.
    timeo=600 The NFS client waits 600 deciseconds (60 seconds) before retrying an NFS request.
    retrans=3 The NFS client attempts NFS requests three times before taking further recovery action.
    rsize=1048576 The NFS client can receive a maximum of 1048576 bytes from the NFS server per READ request.
    wsize=1048576 The NFS client can receive a maximum of 1048576 bytes from the NFS server per WRITE request.
    resvport The NFS client uses a privileged source port when communicating with the NFS server for this mount point.
    async The NFS client delays sending application writes to the NFS server until certain conditions are met.
    Caution: Using the sync option significantly reduces performance.

    For reduced mounting latency, you should specify the -o tcp option.

    Run the mount command:

    sudo mount ip-address:/file-share mount-point-directory
    

    where:

    • ip-address is the IP address for the Filestore instance.
    • file-share is the name of the file share on the instance.
    • mount-point-directory is the path where you want to map the Filestore file share to.

    You can get the IP address and file share name of an instance from any of following sources:

    For example, given a Filestore instance with an IP address 10.0.1.2 and a file share named share1, the following command mounts the file share to the local mount point directory /mnt/render:

    sudo mount 10.0.1.2:/share1 /mnt/render
    
  7. Optional: Run chmod to set permissions on the mount directory so that someone other than the root user can write to it. Run the following command to set directory permissions:

    sudo chmod permissions mount-point-directory
    

    where:

    • permissions is the set of permissions to grant. For example, chmod go+rw makes the directory readable and writable by users in the group that owns the directory and all other users. For more information about setting permissions, see Configuring access on a file share.
    • mount-point-directory is the path to the directory where the Filestore file share is mounted.
  8. Optional: Confirm that the Filestore file share is mounted:

    df -h --type=nfs
    

    The output should contain an entry similar to the following:

    Filesystem        Size    Used  Avail  Use%  Mounted on
    10.0.1.2:/share1  1018G   76M   966G   1%    /mnt/render
    10.0.2.2:/vol3    1018G   76M   966G   1%    /mnt/filestore3
  9. If the applications you plan to use with the mounted Filestore file share require NFS file locking, and this client VM instance is using something other than the default VPC network with unchanged settings, you might need to set two ports on the instance in order to make firewall configuration easier. For more information on determining if you need to configure a firewall rule for the VPC network, see Configuring Firewall Rules. For more information on setting ports, see Setting NFS Ports.

Linux: /etc/fstab

You can make file shares automatically mount to client VMs during boot up by modifying the client's /etc/fstab file:

  1. In the Cloud Console, go to the VM Instances page.

    Go to the VM instances page

  2. Locate the Linux VM you want to use as a client, then click SSH to open a terminal on that VM.

  3. Optional: Mounting a file share on a client VM with multiple network interfaces through a secondary network.

    Details

    If the client VM has multiple network interfaces and you want to mount the file share through a secondary network, namely an interface other than nic0, you must configure the client VM's routing policy to ensure that the file share mounts through the correct network interface. To do this, modify the following files:

    • In /etc/default/instance_configs.cfg, set the setup flag for NetworkInterfaces to false:
      [NetworkInterfaces]
      dhclient_script = /sbin/google-dhclient-script
      dhcp_command =
      ip_forwarding = true
         setup = false
      
    • In /etc/network/interfaces, add the following lines:

      auto eth1
      iface eth1 inet dhcp
          up ip route add filestore-reserved-address-range via default-gateway-of-nic-to-filestore
      

    where:

    • filestore-reserved-address-range is the reserved address range for use by the Filestore instance.
    • default-gateway-of-nic-to-filestore is the default gateway IP address of the NIC connected to the VPC network that is shared with the Filestore instance.

    For more information on using Compute Engine instances with multiple NICs, see Configuring Policy Routing.

  4. Install NFS:

    Debian/Ubuntu

    Use the following commands to install NFS on Debian or Ubuntu.

    sudo apt-get -y update &&
    sudo apt-get install nfs-common

    RHEL/CentOS

    Use the following commands to install NFS on Red Hat Enterprise Linux or CentOS.

    sudo yum update &&
    sudo yum install nfs-utils

    SUSE

    Use the following commands to install NFS on SUSE.

    sudo zypper update &&
    sudo zypper -n install nfs-client
  5. Make a local directory to map to the Filestore file share:

    sudo mkdir -p mount-point-directory
    

    where mount-point-directory is the directory to create, for example /mnt/filedir.

  6. Open the /etc/fstab file:

    sudo vim /etc/fstab
    
  7. Add the following line in the /etc/fstab file:

    ip-address:/file-share mount-point-directory nfs defaults,_netdev 0 0
    

    where:

    • ip-address is the IP address for the Filestore instance.
    • file-share is the name of the file share on the instance.
    • mount-point-directory is the path you want to map the Filestore file share to.

    Example: The following line in /etc/fstab mounts file share vol1 of the Filestore instance with IP address 10.0.0.2 in the mount point directory /mnt with the default mount options:

    10.0.0.2:/vol1 /mnt nfs defaults,_netdev 0 0
    
  8. Mount everything in /etc/fstab:

    sudo mount -a
    

    The file share is now automatically mounted each time the client VM boots up.

Linux: autofs

You can configure autofs to automatically mount a file share only when it is accessed and keep it unmounted when it is not used. Compared to having a file share statically mounted in /etc/fstab, this method helps conserve client VM resources in situations where the client needs to map to many mount points.

Install NFS and autofs on the client VM

  1. In the Cloud Console, go to the VM Instances page.

    Go to the VM instances page

  2. Locate the Linux VM you want to use as a client, then click SSH to open a terminal on that VM.

  3. Optional: Mounting a file share on a client VM with multiple network interfaces through a secondary network.

    Details

    If the client VM has multiple network interfaces and you want to mount the file share through a secondary network, namely an interface other than nic0, you must configure the client VM's routing policy to ensure that the file share mounts through the correct network interface. To do this, modify the following files:

    • In /etc/default/instance_configs.cfg, set the setup flag for NetworkInterfaces to false:
      [NetworkInterfaces]
      dhclient_script = /sbin/google-dhclient-script
      dhcp_command =
      ip_forwarding = true
         setup = false
      
    • In /etc/network/interfaces, add the following lines:

      auto eth1
      iface eth1 inet dhcp
          up ip route add filestore-reserved-address-range via default-gateway-of-nic-to-filestore
      

    where:

    • filestore-reserved-address-range is the reserved address range for use by the Filestore instance.
    • default-gateway-of-nic-to-filestore is the default gateway IP address of the NIC connected to the VPC network that is shared with the Filestore instance.

    For more information on using Compute Engine instances with multiple NICs, see Configuring Policy Routing.

  4. Install NFS by running the following commands:

    Debian/Ubuntu

    Use the following commands to install NFS on Debian or Ubuntu.

    sudo apt-get -y update &&
    sudo apt-get install nfs-common

    RHEL/CentOS

    Use the following commands to install NFS on Red Hat Enterprise Linux or CentOS.

    sudo yum update &&
    sudo yum install nfs-utils

    SUSE

    Use the following commands to install NFS on SUSE.

    sudo zypper update &&
    sudo zypper -n install nfs-client
  5. From the terminal window of the client VM, install autofs:

    Debian/Ubuntu

    sudo apt-get install autofs
    

    RHEL/CentOS

    sudo yum install autofs
    

    SUSE

    sudo zypper -n install autofs
    

Configure autofs to dynamically mount the file share to the client VM

  1. Enable autofs:

    sudo systemctl enable --now autofs
    
  2. Make a local directory to map to the Filestore file share:

    sudo mkdir -p mount-point-directory
    

    where mount-point-directory is the directory to create. It must not contain any subdirectories. For the purpose of this guide, we use /nfs as the mount point directory.

  3. Open the /etc/auto.master file:

    sudo vim /etc/auto.master
    
  4. Add the following line in the /etc/auto.master file:

    /nfs /etc/auto.nfs --timeout seconds
    

    where seconds is the duration in seconds before the mount timeout. The default value is 300. Setting this value to 0 disables unmounting by autofs.

  5. Create a file called /etc/auto.nfs:

    sudo vim /etc/auto.nfs
    
  6. Add the following line in the /etc/auto.nfs file:

    mount-point-subdir options ip-address:/file-share
    

    where:

    • mount-point-subdir is the subdirectory in /nfs you want to map the Filestore file share to. This subdirectory is dynamically generated and should not exist in the client VM.
    • options are the NFS mount options. For automounting, we recommend specifying -o tcp to reduce the latency when mounting and unmounting.
    • ip-address is the IP address for the Filestore instance.
    • file-share is the name of the file share on the instance.

    Example: The following line in /etc/auto.nfs mounts file share vol1 of the Filestore instance with IP address 10.0.0.2 in the dynamically generated subdirectory file-shares with read/write access granted:

    file-shares -rw 10.0.0.2:/vol1
    

Test your configuration

  1. If the file share is currently mounted, you need to unmount it:

    sudo umount mount-point-directory
    

    where mount-point-directory is the path where the Filestore file share is mapped to.

    If there is an /etc/fstab entry that mounts the file share at system boot, make sure to remove it.

  2. Reload autofs:

    sudo service autofs reload
    
  3. Confirm that your configuration works:

    ls mount-point-directory/mount-point-subdir
    

    where:

    • mount-point-directory is the mount point directory indicated in the /etc/auto.master file.
    • mount-point-subdir is the mount point subdirectory indicated in the /etc/auto.nfs file.

    From our example, the command would be:

    ls /nfs/file-shares
    

    If the automount is configured correctly, the system will first mount the file share and then return the results of the ls command. The file share is automatically unmounted after a period of inactivity, which is defined by the timeout value specified in the /etc/auto.master file.

Windows

Connect to the Windows VM

  1. In the Cloud Console, go to the VM Instances page.

    Go to the VM instances page

  2. Locate the Windows VM that you want to use as a client, then click RDP to open a remote desktop connection to that VM. For more information, see Connecting to Windows instances.

Install NFS on the Windows VM

  1. On the Windows VM, open PowerShell as an administrator.
  2. In PowerShell, install the NFS client:

    Install-WindowsFeature -Name NFS-Client
    
  3. Restart the Windows VM instance as prompted, then open a new remote desktop connection.

Configure the user ID used by the NFS client

  1. In PowerShell, run the following commands to create two new registry entries, AnonymousUid and AnonymousGid:

    New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Default" `
        -Name "AnonymousUid" -Value "0" -PropertyType DWORD
    
    New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Default" `
        -Name "AnonymousGid" -Value "0" -PropertyType DWORD
    
  2. Restart the NFS client service:

    nfsadmin client stop
    
    nfsadmin client start
    

Map the file share to the Windows VM

  1. Exit PowerShell:

    exit
    
  2. From Command Prompt, map the file share to a drive letter:

    mount filestore-ip:/file-share-name drive-letter:
    

    where:

    • filestore-ip is the IP address of the Filestore instance.
    • file-share-name is the name of the file share on the Filestore instance.
    • drive-letter is the letter of the drive you want to assign for the mapping.

    Example

    The following command maps file share vol1 on the Filestore instance with the IP address 10.0.0.2 to drive z: on the client Windows VM:

    mount 10.0.0.2:/vol1 z:
    

Creating and mounting subdirectories on the file share

Once your file share is mounted on a client VM, you can create subdirectories on the file share, which clients can then mount directly instead of mounting the entire file share:

  1. To create a subdirectory on the mounted file share, run:

    sudo mkdir -p mount-point-directory/file-share-sub-dir
    

    where:

    • mount-point-directory is the path where you map the file share to.
    • file-share-sub-dir is the path of the subdirectory in the file share that you want to create.

    This command creates the subdirectory on both the client VM and the file share. For example, if you mounted file share 10.0.0.2:/vol1 to the directory /mnt on the client VM, the command sudo mkdir -p /mnt/nfs creates 10.0.0.2:/vol1/nfs on the file share and /mnt/nfs on the client VM.

  2. Optional: If you want to mount the file share subdirectory to the directory on the client VM where the file share is currently mounted, you must unmount the file share first:

    sudo umount mount-point-directory
    

    where mount-point-directory is the directory that the file share is currently mapped to.

  3. Mount the subdirectory:

    sudo mount ip-address:/file-share/file-share-sub-dir mount-point-directory
    

    where:

    • ip-address is the IP address for the Filestore instance.
    • file-share is the name of the file share on the instance.
    • file-share-sub-dir is the path of the subdirectory in the file share to mount.
    • mount-point-directory is the path where you want to map the Filestore file share to.

    Example: In the following command:

    sudo mount 10.0.0.2:/vol1/nfs /mnt/nfs
    
    • 10.0.0.2 is the IP address of the Filestore instance.
    • vol1 is the name of the file share.
    • nfs is a file share subdirectory.
    • /mnt/nfs is the directory on the client VM where the file share subdirectory nfs is mapped to.

What's next