Format and mount a non-boot disk on a Linux VM Stay organized with collections Save and categorize content based on your preferences.
If you attached a new, blank disk to your VM, before you can use it you mustformat and mount the disk. If you attached a disk that already contains data,then you must mount the disk before you can use it.
Before you begin
- If you haven't already, set upauthentication. Authentication verifies your identity for access to Google Cloud services and APIs. To run code or samples from a local development environment, you can authenticate to Compute Engine by selecting one of the following options:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
Install the Google Cloud CLI. After installation,initialize the Google Cloud CLI by running the following command:
gcloudinit
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Note: If you installed the gcloud CLI previously, make sure you have the latest version by runninggcloud components update.- Set a default region and zone.
REST
To use the REST API samples on this page in a local development environment, you use the credentials you provide to the gcloud CLI.
Install the Google Cloud CLI. After installation,initialize the Google Cloud CLI by running the following command:
gcloudinit
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Note: If you installed the gcloud CLI previously, make sure you have the latest version by runninggcloud components update.For more information, seeAuthenticate for using REST in the Google Cloud authentication documentation.
Connect to the VM
Go to theVM instances page.
Click theSSH button next to the instance that has the new attached disk. The browser opens a terminal connection to the VM.
Format a non-boot disk on a Linux VM
Permissions required for this task
To perform this task, you must have the followingpermissions:
compute.instances.setMetadataon the instance, so that you can connect using SSH
If you are connecting to a VM instance that can run as a service account,you must also grant theroles/iam.serviceAccountUser role.
In the terminal, use thesymlink created for your attached disk to determine which device to format.
ls-l/dev/disk/by-id/google-*Unformatted disks don't have additional symlinks with
-partNin the name.google-extra-scsi-disk -> ../../sdb google-instance-2 -> ../../sda google-instance-2-part1 -> ../../sda1 google-instance-2-part14 -> ../../sda14 google-instance-2-part15 -> ../../sda15 google-local-nvme-ssd-0 -> ../../nvme0n1 google-local-nvme-ssd-1 -> ../../nvme0n2In this example, the new Persistent Disk was created with the name
extra-scsi-disk. The device name for the new disk issdb.Format the disk device using the
mkfstool. This commanddeletes 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
ext4file system without a partition table. You canincrease the size of your disk later without having to modify disk partitions.To maximize disk performance, use therecommended formatting options with the
-Eflag. It is not necessary to reserve space for the root volume on this secondary disk, so specify-m 0to use all of the available disk space. The following command formats the entire disk with no partition table.$sudo mkfs.FILE_SYSTEM_TYPE -m 0 -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/DEVICE_NAMEReplace the following:
FILE_SYSTEM_TYPE: the file system type. For example,ext2,ext3,ext4, orxfs.DEVICE_NAME: the device name of thedisk that you are formatting. For example, using the example output fromthe first step, you would usesdbfor the device name.
Mount 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_DIRReplace
MOUNT_DIRwith the directory at which tomount disk.Use themount tool to mount the disk to the instance, and enable the
discardoption:$sudo mount -o discard,defaults /dev/DEVICE_NAME /mnt/disks/MOUNT_DIRReplace the following:
DEVICE_NAME: the device name of the disk tomount.MOUNT_DIR: the directory in which to mountyour 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_DIRReplace
MOUNT_DIRwith the directory where youmounted your disk.
Configure automatic mounting on VM restart
Add the disk to your/etc/fstab file, so that the disk automaticallymounts again when the VM restarts. On Linux operating systems, thedevice name can change with each reboot, but thedevice UUID always pointsto 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 configureautomatic mounting on VM restart.
/etc/fstabdon't persist across system reboots. To ensure the device is checked andmounted during boot, run thefsck andmount operations on thedisk from your cloud-config'sbootcmd section. SeeMounting and formattingdisksin the Container-Optimized OS documentation.Create a backup of your current
/etc/fstabfile.$sudo cp /etc/fstab /etc/fstab.backupUse the
blkidcommand to list the UUID for the disk.$sudo blkid /dev/DEVICE_NAME/dev/DEVICE_NAME: UUID="a9e1c14b-f06a-47eb-adb7-622226fee060" BLOCK_SIZE="4096"TYPE="ext4" PARTUUID="593b3b75-108f-bd41-823d-b7e87d2a04d1"
Replace the following:
DEVICE_NAME: the device name of the disk thatyou want to automatically mount. If you created a partition table on thedisk, specify the partition that you want to mount by adding the suffixappended to the device name. For example, ifsdbis the device namefor the disk,sdb1might be the name for the partition.
Open the
/etc/fstabfile in a text editor and create an entry thatincludes the UUID. For example:UUID=UUID_VALUE /mnt/disks/MOUNT_DIRFILE_SYSTEM_TYPE discard,defaults,MOUNT_OPTION 0 2
Replace the following:
UUID_VALUE: the UUID of the disk, listed inthe output of the previous stepMOUNT_DIR: the directory where you mountedyour diskFILE_SYSTEM_TYPE: the file system type.For example,ext2,ext3,ext4, orxfs.MOUNT_OPTION: specifies what the operatingsystem does if it cannot mount the zonal persistent disk at boot time.For valid values, seeThe fourth field in theLinuxfstabdocumentation.To let the system boot even if the disk is unavailable, use thenofailmount option.
Use the
catcommand to verify that your/etc/fstabentries are correct:$cat /etc/fstabUUID=6B33-A686 /boot/efi vfat defaults 0 0UUID=UUID_VALUE /mnt/disks/MOUNT_DIRFILE_SYSTEM_TYPE discard,defaults,MOUNT_OPTION 0 2
Always keep the/etc/fstab file in sync with the devices that are attached toa VM. If you want to detach a disk or create a snapshot from the boot diskfor a VM, edit the/etc/fstab file and remove the entry for the disk. Evenif you setMOUNT_OPTION tonofail ornobootwait,remove the entry before you create your boot disk snapshot or detach the disk.
What's next
- Configure persistent device namesfor your added disks.
- Learn how toresize your persistent disks.
- Learn how to regularlyback up your disks using snapshotsto prevent unintended data loss.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-15 UTC.