Adding or Resizing Persistent Disks

This page shows you how to add a secondary persistent disk or resize a persistent disk when your instances require additional storage space.

Compute Engine manages the hardware behind persistent disks so you can add and resize your disks without handling striping or redundancy. Either attach one large secondary disk and resize it as you require additional space, or attach multiple smaller disks to separate your data into multiple volumes.

If standard persistent disks and SSD persistent disks do not meet all of your performance or flexibility requirements, you can add additional storage options to your instances.

Before you begin

Adding a persistent disk to your instance

Create a standard persistent disk or an SSD persistent disk and attach it to an existing instance. If you do not have any instances, you must first create and start a new instance where you can attach this secondary disk.

Create and attach a persistent disk through the Google Cloud Platform Console, the gcloud tool, or the API.

Console


Create and attach a persistent disk in the Google Cloud Platform Console:

  1. Go to the VM instances page.

    Go to the VM instances page

  2. Click the name of the instance where you want to add a disk.
  3. At the top of the instance details page, click Edit.
  4. Under Additional disks, click Add item.
  5. In the Name column for additional lists, click the Select a disk drop down menu and select Create disk.
  6. Configure the properties for your new disk. Specify a name for the disk and select the Blank disk option.
  7. Click Create to create the disk.
  8. At the bottom of the instance details page, click Save to apply your changes to the instance and attach the new disk.
  9. After you create and attach a new disk to an instance, you must format and mount the disk so that the operating system can use the available storage space.

gcloud


Create and attach a persistent disk with the gcloud tool:

  1. Use the gcloud compute disks create command to create a new persistent disk. If you need an SSD persistent disk for additional throughput or IOPS, include the --type flag and specify pd-ssd.

    gcloud compute disks create [DISK_NAME] --size [DISK_SIZE] --type [DISK_TYPE]
    

    where:

    • [DISK_NAME] is the name of the new disk.
    • [DISK_SIZE] is the size of the new disk in GB.
    • [DISK_TYPE] is the type of persistent disk. Either pd-standard or pd-ssd.
  2. After you create the disk, attach it to any running or stopped instance. Use the gcloud compute instances attach-disk command:

    gcloud compute instances attach-disk [INSTANCE_NAME] --disk [DISK_NAME]
    

    where:

    • [INSTANCE_NAME] is the name of the instance where you are adding the new persistent disk.
    • [DISK_NAME] is the name of the new disk that you are attaching to the instance.

After you create and attach a new disk to an instance, you must format and mount the disk so that the operating system can use the available storage space.

API


  1. In the API, construct a POST request to create a persistent disk. In the request body, initialize a new disk and specify the disk size and type with the diskSizeGb and type properties. Because this is a data disk, do not include the sourceImage property, which creates a blank disk:

    POST https://www.googleapis.com/compute/v1/projects/[PROJECT_ID]/zones/[ZONE]/disks
    
    {
     "name": "[DISK_NAME]",
     "sizeGb": "[DISK_SIZE]"
     "type": "zones/[ZONE]/diskTypes/[DISK_TYPE]"
    }
    

    where:

    • [PROJECT_ID] is your project ID.
    • [ZONE] is the zone where your instance and new disk are located.
    • [DISK_NAME] is the name of the new disk.
    • [DISK_SIZE] is the size of the new disk in GB.
    • [DISK_TYPE] is the type of persistent disk. Either pd-standard or pd-ssd.
  2. Construct a POST request to the compute.instances.attachDisk method, and include the URL to the persistent disk that you just created:

    POST https://www.googleapis.com/compute/v1/projects/[PROJECT_ID]/zones/[ZONE]/instances/[INSTANCE_NAME]/attachDisk
    
    {
     "source": "/compute/v1/projects/my-project/zones/[ZONE]/disks/[DISK_NAME]"
    }
    

    where:

    • [PROJECT_ID] is your project ID.
    • [ZONE] is the zone where your instance and new disk are located.
    • [INSTANCE_NAME] is the name of the instance where you are adding the new persistent disk.
    • [DISK_NAME] is the name of the new disk.

After you create and attach a new disk to an instance, you must format and mount the disk so that the operating system can use the available storage space.

Formatting and mounting a persistent disk

After you create and attach a new disk to an instance, you must format and mount the disk so that the operating system can use the available storage space. The formatting process is different on Linux instances and Windows instances.

Linux instances


Format and mount the new disk on your Linux instance. You can use any partition format and configuration that you need, but it is easiest to create a single ext4 file system without a partition table. This makes it simple to resize your disk later if you need more storage space.

  1. Go to the VM instances page.

    Go to the VM instances page

  2. Click the SSH button next to the instance that has the new attached disk. The browser opens a terminal connection to the instance.

  3. In the terminal, use the ls command to list the disks that are attached to your instance and find the disk that you want to format and mount.

    $ ls /dev/disk/by-id
    
    google-example-instance       scsi-0Google_PersistentDisk_example-instance
    google-example-instance-part1 scsi-0Google_PersistentDisk_example-instance-part1
    google-[DISK_NAME]            scsi-0Google_PersistentDisk_[DISK_NAME]
    

    where [DISK_NAME] is the name of the persistent disk that you attached to the instance.

    The disk ID usually includes the name of your persistent disk with a google- prefix or a scsi-0Google_PersistentDisk_ prefix. You can use either ID to specify your disk, but this example uses the ID with the google- prefix.

  4. Format the disk with a single ext4 filesystem using the mkfs tool. This command deletes all data from the specified disk. To maximize disk performance, use the recommended formatting options in the -E flag:

    $ sudo mkfs.ext4 -F -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/disk/by-id/google-[DISK_NAME]
    

    where [DISK_NAME] is the name of the persistent disk that you are formatting.

  5. Create a directory that serves as the mount point for the new disk.

    $ sudo mkdir -p /mnt/disks/[MNT_DIR]
    

    where: [MNT_DIR] is the directory where you will mount your persistent disk.

  6. Use the mount tool to mount the disk to the instance with the discard option enabled:

    $ sudo mount -o discard,defaults /dev/disk/by-id/google-[DISK_NAME] /mnt/disks/[MNT_DIR]
    

    where:

    • [DISK_NAME] is the name of the persistent disk that you are formatting.
    • [MNT_DIR] is the directory where you will mount your persistent disk.
  7. Configure read and write access to the device. For this example, grant write access to the device for all users.

    $ sudo chmod a+w /mnt/disks/[MNT_DIR]
    

    where: [MNT_DIR] is the directory where you mounted your persistent disk.

Optionally, you can add the persistent disk to the /etc/fstab file so that the device automatically mounts again when the instance restarts.

  1. Create the /etc/fstab entry. Use the blkid command to find the UUID for the file system on the device and edit the /etc/fstab file to include that UUID with the mount options. You can complete this step with a single command. For example:

    $ echo UUID=`sudo blkid -s UUID -o value /dev/disk/by-id/google-[DISK_NAME]` /mnt/disks/[MNT_DIR] ext4 discard,defaults,[NOFAIL] 0 2 | sudo tee -a /etc/fstab
    

    where:

    • [DISK_NAME] is the name of the persistent disk that you want to automatically mount.
    • [MNT_DIR] is the directory where you mounted your persistent disk.
    • [NOFAIL] is the option that specifies what the system should do if it is unable to mount this persistent disk. For most distributions, specify nofail. For instances running Ubuntu 12.04 or Ubuntu 14.04, specify the nobootwait option.
  2. Use the cat command to verify that your /etc/fstab entries are correct:

    $ cat /etc/fstab
    

If you detach this persistent disk or create a snapshot from the boot disk for this instance, edit the /etc/fstab file and remove the entry for this persistent disk. Even with the nofail or nobootwait options in place, keep the /etc/fstab file in sync with the devices that are attached to your instance and remove these entries before you create your boot disk snapshot or when you detach persistent disks.

Windows instances


Use the Windows Disk Management tool to format and mount the new disk on a Windows instance.

  1. Go to the VM instances page.

    Go to the VM instances page

  2. Click the RDP button next to the instance that has the resized disk. The browser opens an RDP connection to the instance.

  3. Right click the Windows Start button and select Disk Management.

    Selecting the Windows Disk Manager tool from the right-click menu on the Windows Start button.

  4. If you have not initialized the persistent disk before, the tool prompts you to select a partitioning scheme for the new disk. Select GPT and click OK.

    Selecting a partition scheme in the disk initialization window.

  5. After the disk initializes, right click on the unallocated disk space and select New Simple Volume.

    Creating a New Simple Volume from the attached disk.

  6. Follow the instructions in the New Simple Volume Wizard to configure the new volume. You can use any partition format that you like, but for this example select NTFS. Also, check Perform a quick format to speed up the formatting process. Optionally, set the cluster size in the Allocation unit size field. The cluster size limits the maximum size of the partition. If you try to resize the persistent disk and this partition later.

    Selecting the partition format type in the New Simple Volume Wizard.

  7. After you complete the wizard and the volume finishes formatting, check the Status column on the list of attached disks to ensure that the new disk has a Healthy status.

    Viewing the list of disks that are recognized by Windows, verify that the instance is Online with a Healthy status.

That's it! You can now write files to the persistent disk.

Resizing a persistent disk

You can resize persistent disks when your instances require more storage space, and attach multiple secondary disks only when you need to separate your data into unique partitions.

The process for resizing root disks is different than the process for resizing secondary storage disks.

You can resize disks at any time, regardless of whether the disk is attached to a running instance.

Console


  1. Go to the Disks page to see a list of persistent disks in your project.

    Go to the Disks page

  2. Click the name of the disk that you want to resize.
  3. At the top of the disk details page, click Edit.
  4. In the Size field, enter the new size for your disk.
  5. At the bottom of the disk details page, click Save to apply your changes to the disk.
  6. After you resize the disk, you must resize the file system so that the operating system can access the additional space.

gcloud


In the gcloud tool, use the disks resize command and specify the --size flag with the desired disk size in GB.

gcloud compute disks resize [DISK_NAME] --size [DISK_SIZE]

where:

  • [DISK_NAME] is the name of the disk that you are resizing.
  • [DISK_SIZE] is the new size for the disk in GB.

After you resize the disk, you must resize the file system so that the operating system can access the additional space.

API


In the API, construct a POST request to the compute.disks.resize method. In the request body, specify the sizeGb parameter and set it to the desired disk size in GB.

POST https://www.googleapis.com/compute/v1/projects/[PROJECT_ID]/zones/[ZONE]/disks/[DISK_NAME]/resize

{
 "sizeGb": "[DISK_SIZE]"
}

where:

  • [PROJECT_ID] is your project ID.
  • [ZONE] is the zone where your disk is located.
  • [DISK_NAME] is the name of the disk that you are resizing.
  • [DISK_SIZE] is the new size for the disk in GB.

After you resize the disk, you must resize the file system so that the operating system can access the additional space.

Resizing the file system or partitions on a persistent disk

After you resize your disk, you must resize the file system so that the operating system can access the additional space. If you resized a root disk, the file system might resize automatically. See Repartitioning a Root Persistent Disk.

Optionally, create a snapshot of your persistent disk before you modify the file system.

Linux instances


On Linux instances, resize the file system on the persistent disk with the following process:

  1. Go to the VM instances page.

    Go to the VM instances page

  2. Click the SSH button next to the instance that has the new attached disk. The browser opens a terminal connection to the instance.

  3. Extend the file system on the resized disk. If your disk has a partition table or uses a volume management system, you must resize your partitions first. If you originally formatted the persistent disk with a single ext4 file system and no partition table, you can run the resize2fs command to resize the file system to include the extra disk space.

    $ sudo resize2fs /dev/disk/by-id/google-[DISK_NAME]
    

    where [DISK_NAME] is the name of the disk where you are resizing the file system.

  4. Optionally, use the df -h command to verify that the file system is resized.

    $ df -h /dev/disk/by-id/google-[DISK_NAME]
    
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sdb        246G   60M  234G   1% /mnt/disks/[MNT_DIR]
    

    where:

    • [DISK_NAME] is the name of the disk where you want to see the file system configuration.
    • [MNT_DIR] is the directory where you mounted your persistent disk.

Windows instances


Use the Windows Disk Management tool to resize partitions on a Windows instance.

  1. Go to the VM instances page.

    Go to the VM instances page

  2. Click the RDP button next to the instance that has the resized disk. The browser opens an RDP connection to the instance.
  3. Right click the Windows Start button and select Disk Management to open the Disk Management tool.

    Selecting the Windows Disk Manager tool from the right-click menu on the Windows Start button.

  4. Refresh the Disk Management tool so that it recognizes the additional space on your persistent disk. At the top of the Disk Management window, click Action and select Refresh.

    Clicking the Action menu and selecting Refresh to update the persistent disk information in the Disk Management tool.

  5. On the disk that you resized, right-click on the formatted partition and select Extend Volume.

    Right-clicking the formatted portion of the disk and selecting the Extend Volume option.

  6. Follow the instructions in the Extend Volume Wizard to extend your existing partition to include the extra disk space. If the existing partition is formatted in NTFS, the maximum partition size is limited by its cluster size settings.

  7. After you complete the wizard and the volume finishes formatting, check the Status column on the list of attached disks to ensure that the new disk has a Healthy status.

    Viewing the list of disks that are recognized by Windows, verify that the instance is Online with a Healthy status.

You can now use the extra persistent disk space to store data.

Setting the auto-delete state of a persistent disk

Read-write persistent disks can be automatically deleted when the associated virtual machine instance is deleted. This behavior is controlled by the autoDelete property on the virtual machine instance for a given attached persistent disk and can be updated at any time. Similarly, you can also prevent a persistent disk from being deleted as well by marking the autoDelete value as false.

Console


  1. Go to the VM instances page.
  2. On the VM Instances page, select the instance from which you want to detach the disk.
  3. Under Boot disks and local disks, check to see if the Delete boot disk when instance is deleted option is selected. If you want to unselect it, click the Edit button at the top of the page.
  4. Save your changes.

gcloud


To set the auto-delete state of a persistent disk, use the gcloud compute instances set-disk-auto-delete command:

gcloud compute instances set-disk-auto-delete example-instance \
  [--auto-delete|--no-auto-delete] --disk example-disk

API


If you are using the API, make a POST request to the following URI:

https://www.googleapis.com/compute/v1/projects/example-project/zones/us-central1-f/instances/example-instance/setDiskAutoDelete?deviceName=deviceName,autoDelete=true

If you are using the client library, use the instances().setDiskAutoDelete method:

def setAutoDelete(gce_service, auth_http):
  request = gce_service.instances().setDiskAutoDelete(project=example-project, zone=us-central1-f, deviceName=my-new-device, instance=example-instance, autoDelete=True)
  response = request.execute(http=auth_http)

  print response

Share a persistent disk between multiple instances

You can attach a non-root persistent disk to more than one virtual machine instance in read-only mode, which allows you to share static data between multiple instances. Sharing static data between multiple instances from one persistent disk is cheaper than replicating your data to unique disks for individual instances.

If you attach a persistent disk to multiple instances, all of those instances must attach the persistent disk in read-only mode. It is not possible to attach the persistent disk to multiple instances in read-write mode. If you need to share dynamic storage space between multiple instances, connect your instances to Cloud Storage or create a network file server.

If you have a persistent disk with data that you want to share between multiple instances, detach it from any read-write instances and attach it to one or more instances in read-only mode.

Console


  1. Go to the Instances page to see the list of instances in your project.

    Go to the Instances page

  2. In the Name column, click the name of the instance where you want to attach the disk. The instance details page opens.
  3. At the top of the instance details page, click Edit.
  4. In the Additional disks section, click Add item to add a disk item to the list of attached non-root persistent disks.
  5. In the Name column, click Select a disk and click the name of the disk that you want to attach.
  6. In the Mode column, click Read/write and change it to Read only.
  7. At the bottom of the instance details page, click Save to apply your changes to the instance.
  8. Connect to the instance and mount the disk.
  9. Repeat this process to add the disk to other instances in read-only mode.

gcloud


In the gcloud tool, use the instances attach-disk command and specify the --mode flag with the ro option.

gcloud compute instances attach-disk [INSTANCE_NAME] --disk [DISK_NAME] --mode ro

where:

  • [INSTANCE_NAME] is the name of the instance where you want to attach the persistent disk.
  • [DISK_NAME] is the name of the disk that you want to attach.

After you attach the disk, connect to the instance and mount the disk.

Repeat this command for each instance where you want to add this disk in read-only mode.

API


In the API, construct a POST request to the compute.instances.attachDisk method. In the request body, specify the mode parameter as READ_ONLY.

POST https://www.googleapis.com/compute/v1/projects/[PROJECT_ID]/zones/[ZONE]/instances/[INSTANCE_NAME]/attachDisk

{
 "source": "zones/[ZONE]/disks/[DISK_NAME]",
 "mode": "READ_ONLY"
}

where: - [INSTANCE_NAME] is the name of the instance where you want to attach the persistent disk. - [PROJECT_ID] is your project ID. - [ZONE] is the zone where your disk is located. - [DISK_NAME] is the name of the disk that you are resizing.

After you attach the disk, connect to the instance and mount the disk.

Repeat this request for each instance where you want to add this disk in read-only mode.

What's next

Send feedback about...

Compute Engine Documentation