Working with persistent disks


You can use a persistent disk as a boot disk for a virtual machine (VM) instance, or as a data disk that you attach to a VM. This document explains how to modify existing persistent disks to do the following:

  • Increase storage space.
  • Recover inaccessible instances.
  • Auto-delete disks when attached VMs are deleted.
  • Switch to a different disk type.

For general information about persistent disks and the types of disks that are available, read the persistent disk overview.

Resizing a persistent disk

You can increase the size of your persistent disk when your virtual machine (VM) instance requires additional storage space or higher performance limits. You can only resize a persistent disk to increase its size. You cannot reduce the size of a persistent disk.

You can resize disks at any time, whether or not the disk is attached to a running VM.

Compute Engine manages the hardware behind persistent disks, so that you can add and resize your disks without handling striping or redundancy.

Resize the disk

To increase the size of a boot or non-boot disk, use the following procedure:

Console

  1. In the Google Cloud Console, go to the Disks page.

    Go to Disks

  2. In the list of persistent disks in your project, click the name of the disk that you want to resize.

  3. On the disk details page, click Edit.

  4. In the Size field, enter the new size for your disk. Disks with MBR partition tables can resize only up to 2 TB.

  5. Click Save to apply your changes to the disk.

gcloud

In the gcloud tool, use the disks resizecommand and specify the --size flag with the desired disk size, in gigabytes.

gcloud compute disks resize DISK_NAME --size DISK_SIZE

Replace the following:

  • DISK_NAME: the name of the disk that you are resizing.
  • DISK_SIZE: the new size, in gigabytes, for the disk. Disks with MBR partition tables can resize only up to 2 TB.

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 gigabytes.

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

{
 "sizeGb": "DISK_SIZE"
}

Replace the following:

  • PROJECT_ID: your project ID.
  • ZONE: the zone where your disk is located.
  • DISK_NAME: the name of the disk to resize.
  • DISK_SIZE: the new size, in gigabytes, for the disk. Disks with MBR partition tables can resize only up to 2 TB.

Back up the disk

Resizing a disk doesn't delete or modify disk data, but as a best practice, always back up your disk by creating a snapshot before you make any changes to the file system or partitions.

Resize the file system and partitions

After resizing your disk, you might need to resize its file system and partitions.

  • Boot disk: VMs using public images automatically resize the root partition and file system after you've resized the boot disk on the VM and restarted the VM. If you are using an image that does not support this functionality, you must manually resize the root partition and file system.

  • Non-boot disk: After resizing the disk, you must extend the file system on the disk to use the added space.

The following example shows how to manually resize the root partition and file system of a boot disk, and how to manually resize the file system of a non-boot data disk with no partition table.

Linux instances

  1. In the Google Cloud Console, go to the VM instances page.

    Go to VM instances

  2. Next to the instance that has the new attached disk, click the SSH button. The browser opens a terminal connection to the instance.

  3. Use the df command and the lsblk command to list the size of the file system and find the device names for your disks.

    $ sudo df -Th
    
    Filesystem      Type     Size   Used  Avail  Use%  Mounted on
    /dev/sda1       ext4     9.7G   1.2G   8.5G   12%  /
    /dev/sdb        ext4     250G    60M   250G    1%  /mnt/disks/disk-1
    
    
    $ sudo lsblk
    
    NAME    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda       8:0    0   20G  0 disk
    └─sda1    8:1    0   10G  0 part /
    sdb       8:16   0  500G  0 disk /mnt/disks/disk-1
    
    

    This example includes the following disks:

    • Boot disk: The /dev/sda1 partition is on a boot disk that has been resized to 20 GB. The partition table and the file system provide only 9.7 GB to the operating system.
    • Non-boot data disk: The /dev/sdb non-boot data disk has no partition table, but the file system on that disk provides only 250 GB to the operating system. The mount directory for this disk is /mnt/disks/disk-1.

    The df command shows that the /dev/sda1 partition is mounted as the root file system, and the /dev/sdb disk is mounted as a non-boot data disk at /mnt/disks/disk-1. Make note of the Type column, which indicates if your disk uses an ext4 or xfs file system.

  4. Resize the root partition and file system on the boot disk. This example assumes the VM image does not support automatic root partition and file system resize.

    1. Resize the root partition by using parted. For example, the following command expands partition 1 of /dev/sda to the maximum possible size:

      sudo parted -sm /dev/sda -- resizepart 1 -1
      
    2. Move GPT data structures to the end of the disk to align the GPT table.

      sudo sgdisk --move-second-header /dev/sda
      
    3. Read the new partition table using partprobe.

      sudo partprobe /dev/sda
      
    4. If you are using ext4, use the resize2fs command to extend the file system:

      sudo resize2fs /dev/sda1
      
    5. If you are using xfs, use the xfs_growfs command to extend the file system:

      sudo xfs_growfs -d /
      
  5. Resize the file system on the non-boot data disk.

    1. If you are using ext4, use the resize2fs command to extend the file system:

      sudo resize2fs /dev/DEVICE_NAME
      

      Replace DEVICE_NAME with the device name for the disk. In this example, the device name is /dev/sdb.

    2. If you are using xfs, use the xfs_growfs command to extend the file system:

      sudo xfs_growfs MOUNT_DIR
      

      Replace MOUNT_DIR with the mount point of the device. You can find the mount point listed in the MOUNTPOINT column in the output of the lsblk command.

  6. Use the df command to verify that the file system is extended. For example:

    df -h /dev/sdb
    
    Filesystem        Size  Used Avail Use% Mounted on
    /dev/sdb  493G   70M  492G   1% /mnt/disks/disk-1
    

Windows instances

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

  1. In the Google Cloud Console, go to the VM instances page.

    Go to VM instances

  2. Next to the instance that has the resized disk, click the RDP button. 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 zonal 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 zonal persistent disk information in the Disk Management tool.

  5. On the disk that you resized, right-click 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 do not need to restart your instance after you complete this process. You can now use the added disk space to store data.

Recovering VMs or full boot disks

If your VM is inaccessible or corrupted, you can recover the VM or its full boot disk.

Recovering an inaccessible VM or a full boot disk

If you can't connect to your instance, or your boot disk is full and you can't resize it, you must create an instance and recreate the boot disk from a snapshot to resize it. You must know the size of the boot disk you're recreating.

  1. In the Google Cloud Console, go to the VM instances page.

    Go to VM instances

    1. Click the instance name to open the VM instance details page.
    2. Click Stop.
    3. In the Boot disk section, note the boot disk's size and name.
  2. In the Google Cloud Console, go to the Create a snapshot page.

    Go to Create a snapshot

    1. Enter a snapshot Name.
    2. Select the boot disk from the Source disk drop-down list.
    3. Click Create.
  3. In the Google Cloud Console, go to the Create an instance page.

    Go to Create an instance

  4. Enter the instance details.

  5. Create a new boot disk from the snapshot of the old boot disk.

    1. Under Boot disk, select Change.
    2. Select Snapshots.
    3. Select the snapshot of the old boot disk from the Snapshot drop-down list.
    4. Select the Boot disk type.
    5. Enter the new size for the disk.
    6. Click Select to confirm your disk options.
  6. Click Create.

Recovering a corrupted VM or a full boot disk

If a VM's OS is corrupted, you can retrieve the boot disk data to recover the VM. To recover the boot disk, you must first create a temporary VM using a Google-provided image, because the snapshot of the source VM might be corrupted.

To retrieve data from the VM, follow these steps:

  1. Create a snapshot from the boot disk of the source VM.
  2. Create a temporary VM using a public image.
  3. Check if you're able to connect to the temporary VM using SSH.
  4. Add an additional disk to the temporary VM by following these steps:

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

      Go to VM instances

    2. Select the temporary VM that you created.

    3. Click Edit.

    4. Under Additional disks, click Add new disk, and then do the following:

      1. Add the disk name.
      2. For Source type, select the Snapshot tab.
      3. In the Source snapshot drop-down menu, select the snapshot of the source VM that you created earlier in these steps.
      4. Click Done.
    5. Click Save.

  5. Connect to the VM using SSH.

  6. Check the disk and partitions in the VM and make a note of the disk name—for example /dev/sdb1:

     lsblk
     

    The output is similar to the following:

     NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
     sda      8:0    0   10G  0 disk
     └─sda1   8:1    0   10G  0 part /
     sdb      8:16   0  100G  0 disk
     ├─sdb1   8:17   0   96G  0 part
     ├─sdb2   8:18   0    1K  0 part
     └─sdb5   8:21   0    4G  0 part
     

  7. Create a mount point at /mnt/newdisk:

     sudo mkdir /mnt/newdisk
     

  8. Mount the additional disk partition to the mount point /mnt/newdisk:

     sudo mount -o discard,defaults DISK_NAME /mnt/newdisk
     

    Replace DISK_NAME with the disk name that you noted earlier in these steps—for example, /dev/sdb1.

    The snapshot's file system is mounted at /mnt/newdisk. You can navigate the directories and retrieve data.

To create a VM with the recovered boot disk, follow these steps:

  1. Create an image from the disk of the temporary VM.
  2. Create a VM with the image of the temporary VM.

After the VM is created, you can transfer the recovered data to the VM.

Setting the auto-delete state of a zonal persistent disk

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

Console

  1. In the Google Cloud Console, go to the VM instances page.

    Go to VM instances

  2. Select the instance that has the disks associated with it.

  3. Click the instance name. The VM instance details page appears.

  4. Click Edit.

  5. Scroll to Additional disks.

  6. Click the pencil to edit the disk's Deletion Rule.

  7. Click Done to save your changes.

  8. Click Save to update your instance.

gcloud

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

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

Replace DISK_NAME with the name of the disk.

API

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

https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/setDiskAutoDelete?deviceName=DISK_NAME,autoDelete=true

Replace the following:

  • PROJECT_ID: your project ID
  • ZONE: the zone where your instance and disk are located
  • INSTANCE_NAME: the name of your instance
  • DISK_NAME: the name of your disk

Changing the type of your persistent disk

You can change the type of your persistent disk at any time. Persistent disk types differ in terms of pricing and performance characteristics, so you might need to change the type of an existing persistent disk to better suit your workload.

You can change the type of your persistent disk using snapshots. For example, to change your standard persistent disk to an SSD persistent disk, use the following process:

Console

  1. Create a snapshot of your standard persistent disk.
  2. Create a new persistent disk based on the snapshot. From the Type drop-down list, select "SSD persistent disk".

gcloud

  1. Create a snapshot of your standard persistent disk.
  2. Create a new persistent disk based on the snapshot. Include the --type flag and specify pd-ssd.

API

  1. Create a snapshot of your standard persistent disk.
  2. Create a new persistent disk based on the snapshot. In the type field, specify "zones/ZONE/diskTypes/pd-ssd" and replace ZONE with the zone where your instance and new disk are located.

Troubleshooting

To find methods for diagnosing and resolving issues related to full disks and disk resizing, see Troubleshooting full disks and disk resizing.

What's next