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
- If you want to use the command-line examples in this guide, do the following:
- Install or update to the latest version of the Google Cloud CLI.
- Set a default region and zone.
- If you want to use the API examples in this guide, set up API access.
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 Google Cloud CLI, or the Compute Engine API.
If you create a disk in the Google Cloud console, the default disk type
is pd-balanced
. If you create a disk using the gcloud CLI or the
Compute Engine API, the default disk type is pd-standard
.
Console
Go to the VM instances page.
Check the box and click the name of the instance where you want to add a disk.
On the VM instance details page, click Edit.
Under Additional disks, click Add new disk.
Specify a name for the disk, configure the disk's properties, and select Blank as the Source type.
Click Done to complete the disk's configuration.
Click Save to apply your changes to the instance and add the new disk.
gcloud
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
.
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 diskDISK_NAME
: the name of the new disk that you are attaching to the instance.
Use the
gcloud compute disks describe
command to see a description of your disk.
Terraform
To create a disk, use the google_compute_disk
resource.
To attach the disk to a VM, use the google_compute_instance
resource.
To learn how to apply or remove a Terraform configuration, see Basic Terraform commands.
API
In the API, construct a
POST
request to create a zonal persistent disk by using thedisks.insert
method. Include thename
,sizeGb
, andtype
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
.
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 IDZONE
: the zone where your instance and new disk are locatedINSTANCE_NAME
: the name of the instance where you are adding the new persistent diskDISK_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
Go to the VM instances page.
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
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.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, specifysdb
to format the entire disk with no partition table.
Mounting the disk
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.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.
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.
Create a backup of your current
/etc/fstab
file.$ sudo cp /etc/fstab /etc/fstab.backup
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, ifsdb
is the device name for the disk,sdb1
might be the name for the partition.
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 stepMOUNT_DIR
: the directory where you mounted your diskMOUNT_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 Linuxfstab
documentation. To let the system boot even if the disk is unavailable, use thenofail
mount option.
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 the Windows Disk Management utility to format and mount the new disk on a Windows VM.
Go to the VM instances page.
Click the RDP button next to the VM that has the new blank disk attached. The browser opens an RDP connection to the instance.
Right-click the Windows Start button and select Disk Management.
Disk Management prompts you to select a partitioning scheme for the new disk. Select GPT and click OK.
After the disk initializes, right-click the unallocated disk space and select New Simple Volume.
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.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 aHealthy
status.
You can now write files to the disk.
What's next
- Learn how to resize your persistent disks.
- Learn how to regularly back up your disks using snapshots to prevent unintended data loss.
- Use regional persistent disks for synchronous replication between two zones.