# Mount a Filestore instance to Cloud TPU VMs

# Mount a Filestore instance to
Cloud TPU VMs

Filestore is a fully managed network attached storage (NAS) for
Compute Engine. Filestore offers seamless compatibility with existing
enterprise applications and supports any NFSv3-compatible client.

## Before you begin

To mount a Filestore instance on your Cloud TPU VM, you
add a Filestore role (Cloud Filestore Editor or Cloud
Filestore Viewer) to the service account associated with your
Cloud TPU VM. If you don't specify a custom service account when
you create a Cloud TPU VM, your Cloud TPU VM uses the default
Compute Engine service account. For more information about specifying a
service account when you create a Cloud TPU VM, see
[Set up the Cloud TPU environment](https://docs.cloud.google.com/tpu/docs/setup-gcp-account-compute#prepare-to-request).
For more information about creating a service account, see
[Create service accounts](https://docs.cloud.google.com/iam/docs/service-accounts-create).

### Add the Filestore Editor role to a service account

This section describes how to add the Cloud Filestore
Editor role to the default Compute Engine service account. Follow these
instructions to add the Cloud Filestore Editor role to any service
account. In step 2, search for and use a custom service account instead of the
default Compute Engine service account.

1. [**Go to IAM console**](https://console.cloud.google.com/iam-admin/iam).

2. Select the **View by principals** tab and type `Name:Compute Engine default
   service account` in the **Filter** field.

3. Click **edit** next to the service account.
   The IAM console displays a dialog that lists the roles assigned
   to the default Compute Engine service account.

4. Click **add** to add another role.

5. Expand the **Select a role** menu, type `Filestore` in the
   filter, and select **Cloud Filestore editor**.

6. Click **Save** to close the dialog. You have added the Filestore
   Editor role to the default Compute Engine service account.

For more information about service accounts, see [Service Account Overview](https://docs.cloud.google.com/iam/docs/service-account-overview).
To learn more about IAM roles, see [Roles and permissions](https://docs.cloud.google.com/iam/docs/roles-overview).

### Create a Filestore instance

Create a Filestore instance by following the instructions in
[Creating Filestore instances](https://docs.cloud.google.com/filestore/docs/creating-instances).

## Mount a Filestore instance on a Cloud TPU VM

The commands to mount a Filestore instance on a Cloud TPU VM
depend on whether you use a single Cloud TPU VM or a Cloud TPU
slice.

### Mount a Filestore instance on a single Cloud TPU VM

1. Connect to your Cloud TPU VM using SSH.

       gcloud compute ssh TPU_NAME \
         --zone=ZONE

   Replace the following placeholders:
   - `TPU_NAME`: The name of your TPU VM.
   - `ZONE`: The zone of your TPU VM.
2. Install the `nfs-common` package.

       sudo apt-get update --allow-releaseinfo-change \
         && sudo apt-get -y update \
         && sudo apt-get -y install nfs-common

3. Create a directory to mount NFS.

       sudo mkdir -p MOUNT_DIR \
         && sudo chmod ugo+rw MOUNT_DIR

   Replace the following placeholder:
   - `MOUNT_DIR`: The directory where you want to mount the Filestore instance.
4. Find the IP address of the Filestore.

       gcloud filestore instances describe FILESTORE_INSTANCE_NAME \
         --location FILESTORE_REGION

   Replace the following placeholders:
   - `FILESTORE_INSTANCE_NAME`: The name of your Filestore instance.
   - `FILESTORE_REGION`: The region of your Filestore instance.
5. Mount the Filestore instance on your Cloud TPU VM to your
   mount directory.

       sudo mount FILESTORE_IP:FILE_SHARE_NAME MOUNT_DIR

   Replace the following placeholders:
   - `FILESTORE_IP`: The IP address of your Filestore instance.
   - `FILE_SHARE_NAME`: The name of the file share on your Filestore instance.
   - `MOUNT_DIR`: The directory where you want to mount the Filestore instance.

### Mount a Filestore instance on a Cloud TPU slice

Cloud TPU slices are provisioned as Compute Engine managed instance
groups (MIGs). Because MIGs distribute VMs across compute capacity, use a startup
script attached to your instance template to mount a Filestore instance
to all machines in your slice. This ensures the mount automatically becomes
available whenever instances are created or automatically healed.

1. Find the IP address of your Filestore instance.

       gcloud filestore instances describe FILESTORE_INSTANCE_NAME \
         --location FILESTORE_REGION

   Replace the following placeholders:
   - `FILESTORE_INSTANCE_NAME`: The name of your Filestore instance.
   - `FILESTORE_REGION`: The region of your Filestore instance.
2. Create a startup script. The script installs the `nfs-common`
   package, creates the mount directory, and attaches the Filestore
   instance. Save the following code as a local file named `mount-filestore.sh`.

       #!/bin/bash
       sudo apt-get update --allow-releaseinfo-change && sudo apt-get -y update && sudo apt-get -y install nfs-common
       sudo mkdir -p MOUNT_DIR
       sudo chmod ugo+rw MOUNT_DIR
       sudo mount FILESTORE_IP:FILE_SHARE_NAME MOUNT_DIR

   Replace the following placeholders:
   - `FILESTORE_IP`: The IP address of your Filestore instance.
   - `FILE_SHARE_NAME`: The name of the file share on your Filestore instance.
   - `MOUNT_DIR`: The directory where you want to mount the Filestore instance.
3. Apply the startup script to your instance template.
   When you create the instance template for your MIG, use the
   `--metadata-from-file` flag to supply your script.

       gcloud compute instance-templates create INSTANCE_TEMPLATE_NAME \
         --metadata-from-file=startup-script=mount-filestore.sh \
         ...

   Replace the following placeholders:
   - `INSTANCE_TEMPLATE_NAME`: The name of your instance template.

## Writing data to Filestore instance

Grant Linux read and write permissions on the directory where you mount the Filestore instance. You can then use the directory as you
would your local file system.