Formatta e monta un disco non di avvio su una VM Linux
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Linux
Se hai collegato un nuovo disco vuoto alla VM, prima di poterlo utilizzare devi
formattarlo e montarlo. Se hai collegato un disco che già contiene dati,
devi montarlo prima di poterlo utilizzare.
Prima di iniziare
Se non l'hai ancora fatto, configura l'autenticazione.
L'autenticazione è
il processo mediante il quale la tua identità viene verificata per l'accesso a servizi e API di Google Cloud .
Per eseguire codice o esempi da un ambiente di sviluppo locale, puoi autenticarti su
Compute Engine selezionando una delle seguenti opzioni:
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:
In questo esempio, il nuovo Persistent Disk è stato creato con il nome
extra-scsi-disk. Il nome del dispositivo per il nuovo disco è sdb.
Formatta l'unità disco utilizzando lo
strumento mkfs.
Questo comando elimina tutti i dati dal disco specificato, quindi assicurati
di specificare correttamente l'unità disco.
Puoi utilizzare qualsiasi formato file necessario, ma ti consigliamo un singolo
file system ext4 senza tabella di partizione. Puoi
aumentare le dimensioni del disco
in un secondo momento senza dover modificare le partizioni del disco.
Per massimizzare le prestazioni del disco, utilizza le opzioni di formattazione consigliate
con il flag -E. Non è necessario dedicare spazio per il volume root su questo disco secondario,
quindi specifica -m 0 per utilizzare tutto lo
spazio su disco disponibile. Il seguente comando formatta l'intero disco senza
tabella di partizione.
FILE_SYSTEM_TYPE: il tipo di file system. Ad esempio, ext2,
ext3, ext4, o xfs.
DEVICE_NAME: il nome del dispositivo del
disco che stai formattando. Ad esempio, utilizzando l'output di esempio del
primo passaggio, dovrai utilizzare sdb per il nome del dispositivo.
Monta il disco
Crea una directory che funga da punto di montaggio per il nuovo disco sulla VM.
Puoi utilizzare qualsiasi directory. L'esempio seguente crea una directory in
/mnt/disks/.
$ sudo mkdir -p /mnt/disks/MOUNT_DIR
Sostituisci MOUNT_DIR con la directory in cui
montare il disco.
Utilizza lo strumento mount
per montare il disco sull'istanza e attiva l'opzione discard:
$ sudo mount -o discard,defaults /dev/DEVICE_NAME /mnt/disks/MOUNT_DIR
Sostituisci quanto segue:
DEVICE_NAME: il nome del dispositivo del disco da
montare.
MOUNT_DIR: la directory in cui montare
il disco.
Configura le autorizzazioni di lettura e scrittura sul disco. Per questo esempio,
concedi l'accesso in scrittura al disco per tutti gli utenti.
$ sudo chmod a+w /mnt/disks/MOUNT_DIR
Sostituisci MOUNT_DIR con la directory in cui
hai montato il disco.
Configura il montaggio automatico al riavvio della VM
Aggiungi il disco al file /etc/fstab in modo che venga montato di nuovo
automaticamente al riavvio della VM. Sui sistemi operativi Linux, il
nome del dispositivo può cambiare a ogni riavvio, ma l'UUID del dispositivo fa sempre riferimento
allo stesso volume, anche quando sposti i dischi da un sistema all'altro. Per questo motivo,
consigliamo di utilizzare l'UUID del dispositivo anziché il nome del dispositivo per configurare
il montaggio automatico al riavvio della VM.
Crea un backup del file /etc/fstab corrente.
$ sudo cp /etc/fstab /etc/fstab.backup
Utilizza il comando blkid per visualizzare l'UUID del disco.
DEVICE_NAME: il nome del dispositivo del disco che
vuoi montare automaticamente. Se hai creato una tabella di partizione sul
disco, specifica la partizione che vuoi montare aggiungendo il suffisso
collegato al nome del dispositivo. Ad esempio, se sdb è il nome del dispositivo
per il disco, il nome della partizione potrebbe essere sdb1.
Apri il file /etc/fstab in un editor di testo e crea una voce che
includa l'UUID. Ad esempio:
UUID_VALUE: l'UUID del disco, riportato
nell'output del passaggio precedente.
MOUNT_DIR: la directory in cui hai montato
il disco
FILE_SYSTEM_TYPE: il tipo di file system.
Ad esempio, ext2, ext3, ext4, oo xfs.
MOUNT_OPTION: specifica cosa fa il sistema
operativo se non riesce a montare il disco permanente a livello di zona al momento dell'avvio.
Per i valori validi, consulta Il quarto campo nella documentazione
di Linux fstab.
Per consentire l'avvio del sistema anche se il disco non è disponibile, utilizza l'opzione di montaggio
nofail.
Usa il comando cat per verificare che le voci /etc/fstab siano corrette:
Mantieni sempre sincronizzato il file /etc/fstab con i dispositivi collegati a
una VM. Se vuoi scollegare un disco o creare uno snapshot dal disco di avvio
per una VM, modifica il file /etc/fstab e rimuovi la voce relativa al disco. Anche
se imposti MOUNT_OPTION su nofail o nobootwait,
rimuovi la voce prima di creare lo snapshot del disco di avvio o scollegare il disco.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-04 UTC."],[[["\u003cp\u003eNew or pre-existing disks attached to a Linux VM must be formatted and/or mounted before they can be used.\u003c/p\u003e\n"],["\u003cp\u003eFormatting a disk with the \u003ccode\u003emkfs\u003c/code\u003e tool deletes all data, requiring correct disk device specification, and \u003ccode\u003eext4\u003c/code\u003e is recommended for maximizing disk performance.\u003c/p\u003e\n"],["\u003cp\u003eMounting a disk involves creating a mount point directory and using the \u003ccode\u003emount\u003c/code\u003e tool, and you can set permissions such as universal write access using \u003ccode\u003echmod\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eTo ensure disks automatically mount on VM restart, you should add an entry to the \u003ccode\u003e/etc/fstab\u003c/code\u003e file using the disk's UUID instead of its device name.\u003c/p\u003e\n"],["\u003cp\u003eIt is crucial to keep the \u003ccode\u003e/etc/fstab\u003c/code\u003e file updated to match the current disk configuration and remove disk entries before detaching or snapshotting.\u003c/p\u003e\n"]]],[],null,["# Format and mount a non-boot disk on a Linux VM\n\nLinux\n\n*** ** * ** ***\n\nIf you attached a new, blank disk to your VM, before you can use it you must\nformat and mount the disk. If you attached a disk that already contains data,\nthen you must mount the disk before you can use it.\n\nBefore you begin\n----------------\n\n- If you haven't already, set up [authentication](/compute/docs/authentication). 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:\n\n Select the tab for how you plan to use the samples on this page: \n\n ### Console\n\n\n When you use the Google Cloud console to access Google Cloud services and\n APIs, you don't need to set up authentication.\n\n ### gcloud\n\n 1.\n [Install](/sdk/docs/install) the Google Cloud CLI.\n\n After installation,\n [initialize](/sdk/docs/initializing) the Google Cloud CLI by running the following command:\n\n ```bash\n gcloud init\n ```\n\n\n If you're using an external identity provider (IdP), you must first\n [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n | **Note:** If you installed the gcloud CLI previously, make sure you have the latest version by running `gcloud components update`.\n 2. [Set a default region and zone](/compute/docs/gcloud-compute#set_default_zone_and_region_in_your_local_client).\n\n ### REST\n\n\n To use the REST API samples on this page in a local development environment, you use the\n credentials you provide to the gcloud CLI.\n 1. [Install](/sdk/docs/install) the Google Cloud CLI. After installation, [initialize](/sdk/docs/initializing) the Google Cloud CLI by running the following command: \n\n ```bash\n gcloud init\n ```\n 2. If you're using an external identity provider (IdP), you must first [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n\n For more information, see\n [Authenticate for using REST](/docs/authentication/rest)\n in the Google Cloud authentication documentation.\n\nConnect to the VM\n-----------------\n\n1. Go to the **VM instances** page.\n\n [Go to the VM instances page](https://console.cloud.google.com/compute/instances)\n2. Click the **SSH** button next to the instance that has the new attached\n disk. The browser opens a terminal connection to the VM.\n\nFormat a non-boot disk on a Linux VM\n------------------------------------\n\n#### Permissions required for this task\n\nTo perform this task, you must have the following\n[permissions](/iam/docs/overview#permissions):\n\n\n- `compute.instances.setMetadata` on the instance, so that you can connect using SSH\n\n\u003cbr /\u003e\n\nIf you are connecting to a VM instance that can run as a service account,\nyou must also grant the `roles/iam.serviceAccountUser` role.\n\n1. In the terminal, use the [symlink](/compute/docs/disks/disk-symlinks)\n created for your attached disk to determine which device to format.\n\n ls -l /dev/disk/by-id/google-*\n\n Unformatted disks don't have additional symlinks with\n `-part`\u003cvar translate=\"no\"\u003eN\u003c/var\u003e in the name. \n\n google-extra-scsi-disk -\u003e ../../sdb\n google-instance-2 -\u003e ../../sda\n google-instance-2-part1 -\u003e ../../sda1\n google-instance-2-part14 -\u003e ../../sda14\n google-instance-2-part15 -\u003e ../../sda15\n google-local-nvme-ssd-0 -\u003e ../../nvme0n1\n google-local-nvme-ssd-1 -\u003e ../../nvme0n2\n\n In this example, the new Persistent Disk was created with the name\n `extra-scsi-disk`. The device name for the new disk is `sdb`.\n2. Format the disk device using the\n [`mkfs` tool](http://manpages.ubuntu.com/manpages/xenial/man8/mkfs.8.html).\n This command **deletes** all data from the specified disk, so make sure\n that you specify the disk device correctly.\n\n You can use any file format that you need, but we recommend a single\n `ext4` file system without a partition table. You can\n [increase the size of your disk](/compute/docs/disks/resize-persistent-disk)\n later without having to modify disk partitions.\n\n To maximize disk performance, use the [recommended formatting options](/compute/docs/disks/optimizing-pd-performance#formatting_parameters)\n with the `-E` flag. It is not necessary to reserve space for the root\n volume on this secondary disk, so specify `-m 0` to use all of the\n available disk space. The following command formats the entire disk with no\n partition table.\n\n \u003cbr /\u003e\n\n ```\n $ sudo mkfs.FILE_SYSTEM_TYPE -m 0 -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/DEVICE_NAME\n \n ```\n\n \u003cbr /\u003e\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eFILE_SYSTEM_TYPE\u003c/var\u003e: the file system type. For example, `ext2`, `ext3`, `ext4`, or `xfs`.\n - \u003cvar translate=\"no\"\u003eDEVICE_NAME\u003c/var\u003e: the device name of the disk that you are formatting. For example, using the example output from the first step, you would use `sdb` for the device name.\n\nMount the disk\n--------------\n\n1. Create a directory that serves as the mount point for the new disk on the VM.\n You can use any directory. The following example creates a directory under\n `/mnt/disks/`.\n\n ```\n $ sudo mkdir -p /mnt/disks/MOUNT_DIR\n ```\n\n Replace \u003cvar translate=\"no\"\u003eMOUNT_DIR\u003c/var\u003e with the directory at which to\n mount disk.\n2. Use the [mount tool](http://manpages.ubuntu.com/manpages/xenial/man8/mount.8.html)\n to mount the disk to the instance, and enable the `discard` option:\n\n ```\n $ sudo mount -o discard,defaults /dev/DEVICE_NAME /mnt/disks/MOUNT_DIR\n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eDEVICE_NAME\u003c/var\u003e: the device name of the disk to mount.\n - \u003cvar translate=\"no\"\u003eMOUNT_DIR\u003c/var\u003e: the directory in which to mount your disk.\n3. Configure read and write permissions on the disk. For this example,\n grant write access to the disk for all users.\n\n ```\n $ sudo chmod a+w /mnt/disks/MOUNT_DIR\n ```\n\n Replace \u003cvar translate=\"no\"\u003eMOUNT_DIR\u003c/var\u003e with the directory where you\n mounted your disk.\n\n### Configure automatic mounting on VM restart\n\nAdd the disk to your `/etc/fstab` file, so that the disk automatically\nmounts again when the VM restarts. On Linux operating systems, the\n*device name* can change with each reboot, but the *device UUID* always points\nto the same volume, even when you move disks between systems. Because of this,\nwe recommend using the device UUID instead of the device name to configure\nautomatic mounting on VM restart.\n| **Note:** If you use Container-Optimized OS, modifications to `/etc/fstab` don't persist across system reboots. To ensure the device is checked and mounted during boot, run the `fsck` and `mount` operations on the disk from your cloud-config's `bootcmd` section. See [Mounting and formatting\n| disks](/container-optimized-os/docs/concepts/disks-and-filesystem#mounting_and_formatting_disks) in the Container-Optimized OS documentation.\n\n1. Create a backup of your current `/etc/fstab` file.\n\n ```\n $ sudo cp /etc/fstab /etc/fstab.backup\n ```\n2. Use the `blkid` command to list the UUID for the disk.\n\n ```\n $ sudo blkid /dev/DEVICE_NAME\n ``` \n\n ```\n /dev/DEVICE_NAME: UUID=\"a9e1c14b-f06a-47eb-adb7-622226fee060\" BLOCK_SIZE=\"4096\"\n TYPE=\"ext4\" PARTUUID=\"593b3b75-108f-bd41-823d-b7e87d2a04d1\"\n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eDEVICE_NAME\u003c/var\u003e: 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.\n3. Open the `/etc/fstab` file in a text editor and create an entry that\n includes the UUID. For example:\n\n ```\n UUID=UUID_VALUE /mnt/disks/MOUNT_DIR FILE_SYSTEM_TYPE discard,defaults,MOUNT_OPTION 0 2\n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eUUID_VALUE\u003c/var\u003e: the UUID of the disk, listed in the output of the previous step\n - \u003cvar translate=\"no\"\u003eMOUNT_DIR\u003c/var\u003e: the directory where you mounted your disk\n - \u003cvar translate=\"no\"\u003eFILE_SYSTEM_TYPE\u003c/var\u003e: the file system type. For example, `ext2`, `ext3`, `ext4`, or `xfs`.\n - \u003cvar translate=\"no\"\u003eMOUNT_OPTION\u003c/var\u003e: 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`\n documentation](https://man7.org/linux/man-pages/man5/fstab.5.html). To let the system boot even if the disk is unavailable, use the `nofail` mount option.\n4. Use the `cat` command to verify that your `/etc/fstab` entries are correct:\n\n ```\n $ cat /etc/fstab\n\n UUID=6B33-A686 /boot/efi vfat defaults 0 0\n UUID=\u003cvar translate=\"no\"\u003eUUID_VALUE\u003c/var\u003e /mnt/disks/\u003cvar translate=\"no\"\u003eMOUNT_DIR\u003c/var\u003e \u003cvar translate=\"no\"\u003eFILE_SYSTEM_TYPE\u003c/var\u003e discard,defaults,\u003cvar translate=\"no\"\u003eMOUNT_OPTION\u003c/var\u003e 0 2\n\n ```\n\nAlways keep the `/etc/fstab` file in sync with the devices that are attached to\na VM. If you want to detach a disk or create a snapshot from the boot disk\nfor a VM, edit the `/etc/fstab` file and remove the entry for the disk. Even\nif you set \u003cvar translate=\"no\"\u003eMOUNT_OPTION\u003c/var\u003e to `nofail` or `nobootwait`,\nremove the entry before you create your boot disk snapshot or detach the disk.\n\nWhat's next\n-----------\n\n- [Configure persistent device names](/compute/docs/disks/set-persistent-device-name-in-linux-vm) for your added disks.\n- Learn how to [resize your persistent disks](/compute/docs/disks/resize-persistent-disk).\n- Learn how to regularly [back up your disks using snapshots](/compute/docs/disks/create-snapshots) to prevent unintended data loss."]]