Creating and attaching a disk


You can either create a blank persistent disk, or create a disk from a data source. 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 do the following:

  • Create a blank, non-boot zonal persistent disk and attach it to your VM instance.
  • Format and mount the disk because it starts with no data or file systems.

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

Before you begin

Restrictions

  • During instance creation, you can attach up to 127 secondary non-boot zonal persistent disks.
  • You can have a total attached capacity of 257 TB per instance. For information about how to ensure maximum performance with large volumes, see Logical volume size.

Adding a non-boot disk to your VM

Create and attach a non-boot zonal disk by using the Google Cloud Console, the gcloud command-line tool, or the Compute Engine API.

If you create a disk in the Cloud Console, the default disk type is pd-balanced. If you create a disk using the gcloud tool or the Compute Engine API, the default disk type is pd-standard.

Console

  1. Go to the VM instances page.

    Go to the VM instances page

  2. Check the box and click the name of the instance where you want to add a disk.

  3. On the VM instance details page, click Edit.

  4. Under Additional disks, click Add new disk.

  5. Specify a name for the disk, configure the disk's properties, and select Blank as the Source type.

  6. Click Done to complete the disk's configuration.

  7. Click Save to apply your changes to the instance and add the new disk.

gcloud

  1. Use the gcloud compute disks create command to create the zonal persistent disk.

    gcloud compute disks create DISK_NAME \
      --size DISK_SIZE \
      --type DISK_TYPE
    

    Replace the following:

    • DISK_NAME: the name of the new disk.
    • DISK_SIZE: the size, in gigabytes, of the new disk. Acceptable sizes range, in 1 GB increments, from 10 GB to 65,536 GB inclusive.
    • DISK_TYPE: full or partial URL for the type of the persistent disk. For example, https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/diskTypes/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
    

    Replace the following:

    • INSTANCE_NAME: the name of the instance where you are adding the new zonal persistent disk
    • DISK_NAME: the name of the new disk that you are attaching to the instance.
  3. Use the gcloud compute disks describe command to see a description of your disk.

API

  1. In the API, construct a POST request to create a zonal persistent disk by using the disks.insert method. Include the name, sizeGb, and type properties. To create this disk as an empty and unformatted non-boot disk, do not specify a source image or a source snapshot.

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

    Replace the following:

    • PROJECT_ID: your project ID.
    • ZONE: the zone where your instance and new disk are located.
    • DISK_NAME: the name of the new disk.
    • DISK_SIZE: the size, in gigabytes, of the new disk. Acceptable sizes range, in 1 GB increments, from 10 GB to 65,536 GB inclusive.
    • DISK_TYPE: full or partial URL for the type of the persistent disk. For example, https://www.googleapis.com/compute/v1/projects/PROJECT_ID /zones/ZONE/diskTypes/pd-ssd.
  2. Construct a POST request to the compute.instances.attachDisk method, and include the URL to the zonal persistent disk that you just created:

    POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/attachDisk
    
    {
     "source": "/compute/v1/projects/PROJECT_ID/zones/ZONE/disks/DISK_NAME"
    }
    

    Replace the following:

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

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

Formatting and mounting a non-boot disk on a Linux VM

Connect to the VM

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

Formatting the disk

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

      $ sudo lsblk
      
      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  250G  0 disk
      

    In this example, sdb is the device name for the new blank persistent disk.

  2. Format the disk using the mkfs tool. This command deletes all data from the specified disk, so make sure that you specify the disk device correctly.

    You can use any file format that you need, but we recommend a single ext4 file system without a partition table. You can increase the size of your disk later without having to modify disk partitions.

    To maximize disk performance, use the recommended formatting options in the -E flag. It is not necessary to reserve space for the root volume on this secondary disk, so specify -m 0 to use all of the available disk space.

     $ sudo mkfs.ext4 -m 0 -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/DEVICE_NAME
     

    Replace DEVICE_NAME with the device name of the disk that you are formatting. For this example, specify sdb to format the entire disk with no partition table.

Mounting the disk

  1. Create a directory that serves as the mount point for the new disk on the VM. You can use any directory. The following example creates a directory under /mnt/disks/.

    $ sudo mkdir -p /mnt/disks/MOUNT_DIR
    

    Replace MOUNT_DIR with the directory at which to mount disk.

  2. Use the mount tool to mount the disk to the instance, and enable the discard option:

    $ sudo mount -o discard,defaults /dev/DEVICE_NAME /mnt/disks/MOUNT_DIR
    

    Replace the following:

    • DEVICE_NAME: the device name of the disk to mount.
    • MOUNT_DIR: the directory in which to mount your disk.
  3. Configure read and write permissions on the disk. For this example, grant write access to the disk for all users.

    $ sudo chmod a+w /mnt/disks/MOUNT_DIR
    

    Replace MOUNT_DIR with the directory where you mounted your disk.

Configuring automatic mounting on VM restart

Add the disk to your /etc/fstab file, so that the disk automatically mounts again when the VM restarts. On Linux operating systems, the device name can change with each reboot, but the device UUID always points to the same volume, even when you move disks between systems. Because of this, we recommend using the device UUID instead of the device name to configure automatic mounting on VM restart.

  1. Create a backup of your current /etc/fstab file.

    $ sudo cp /etc/fstab /etc/fstab.backup
    
  2. Use the blkid command to list the UUID for the disk.

    $ sudo blkid /dev/DEVICE_NAME
    

    Replace the following:

    • DEVICE_NAME: the device name of the disk that you want to automatically mount. If you created a partition table on the disk, specify the partition that you want to mount by adding the suffix appended to the device name. For example, if sdb is the device name for the disk, sdb1 might be the name for the partition.
  3. Open the /etc/fstab file in a text editor and create an entry that includes the UUID. For example:

    UUID=UUID_VALUE /mnt/disks/MOUNT_DIR ext4 discard,defaults,MOUNT_OPTION 0 2
    

    Replace the following:

    • UUID_VALUE: the UUID of the disk, listed in the output of the previous step
    • MOUNT_DIR: the directory where you mounted your disk
    • MOUNT_OPTION: specifies what the operating system does if it cannot mount the zonal persistent disk at boot time. For valid values, see The fourth field in the Linux fstab documentation. To let the system boot even if the disk is unavailable, use the nofail mount option.
  4. Use the cat command to verify that your /etc/fstab entries are correct:

    $ cat /etc/fstab
    
    LABEL=cloudimg-rootfs   /        ext4   defaults        0 0
    UUID=d761bdc5-7e2a-4529-a393-b9aefdb623b6 /mnt/disks/MOUNT_DIR ext4 discard,defaults,MOUNT_OPTION 0 2
    
    

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

Formatting and mounting a non-boot disk on a Windows VM

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

  1. Go to the VM instances page.

    Go to the VM instances page

  2. Click the RDP button next to the VM that has the new blank disk attached. The browser opens an RDP connection to the instance.

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

    Selecting Disk Management by right-clicking the Windows Start button.

  4. Disk Management 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 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. Keep this in mind if you try to resize the zonal 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 is formatted, check the Status column on the list of attached disks to ensure that the new disk has a Healthy status.

    Checking that the disk is online with a healthy status.

You can now write files to the disk.

What's next