Formater et installer un disque non amorçable sur une VM Linux
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Linux
Si vous avez associé un nouveau disque vide à votre VM, vous devez le formater et l'installer avant de pouvoir l'utiliser. Si vous avez associé un disque contenant déjà des données, vous devez l'installer avant de pouvoir l'utiliser.
Avant de commencer
Si ce n'est pas déjà fait, configurez l'authentification.
L'authentification valide votre identité pour accéder aux services et aux API Google Cloud . Pour exécuter du code ou des exemples depuis un environnement de développement local, vous pouvez vous authentifier auprès de Compute Engine en sélectionnant l'une des options suivantes :
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
Installez la Google Cloud CLI.
Une fois que la Google Cloud CLI est installée, initialisez-la en exécutant la commande suivante :
Pour utiliser les exemples API REST de cette page dans un environnement de développement local, vous devez utiliser les identifiants que vous fournissez à la gcloud CLI.
Installez la Google Cloud CLI.
Une fois que la Google Cloud CLI est installée, initialisez-la en exécutant la commande suivante :
Cliquez sur le bouton SSH en regard de l'instance contenant le nouveau disque associé. Le navigateur ouvre une connexion de terminal vers la VM.
Formater un disque non amorçable sur une VM Linux
Autorisations requises pour cette tâche
Pour effectuer cette tâche, vous devez disposer des autorisations suivantes :
compute.instances.setMetadata sur l'instance, de sorte que vous puissiez vous connecter via SSH
Si vous vous connectez à une instance de VM pouvant être exécutée en tant que compte de service, vous devez également accorder le rôle roles/iam.serviceAccountUser.
Dans le terminal, utilisez le lien symbolique créé pour votre disque associé afin de déterminer l'appareil à formater.
ls-l/dev/disk/by-id/google-*
Les disques non formatés ne contiennent pas de liens symboliques supplémentaires avec -partN dans le nom.
Dans cet exemple, le disque Persistent Disk a été créé avec le nom extra-scsi-disk. Le nom d'appareil pour le nouveau disque est sdb.
Formatez le disque à l'aide de l'outil mkfs.
Cette commande supprime toutes les données du disque spécifié. Assurez-vous donc de bien spécifier le disque concerné.
Vous pouvez utiliser le format de fichier de votre choix. Toutefois, nous vous recommandons de n'utiliser qu'un seul système de fichiers ext4 sans table de partition. Vous pouvez augmenter la taille de votre disque ultérieurement, sans avoir à modifier les partitions de disque.
Pour optimiser les performances du disque, utilisez les options de formatage recommandées dans l'option -E. Il n'est pas nécessaire de réserver de l'espace pour la racine sur ce disque secondaire. Par conséquent, spécifiez -m 0 pour utiliser tout l'espace disque disponible. La commande suivante formate le disque entier, sans définir de table de partitionnement.
FILE_SYSTEM_TYPE : le type de système de fichiers. Par exemple, ext2, ext3, ext4 ou xfs.
DEVICE_NAME : le nom de l'appareil du disque que vous formatez. Par exemple, en utilisant l'exemple de sortie de la première étape, vous devriez utiliser sdb comme nom d'appareil.
Installer le disque
Créez un répertoire servant de point d'installation pour le nouveau disque sur la VM.
Vous pouvez utiliser n'importe quel répertoire. Dans cet exemple, un nouveau répertoire est créé sous /mnt/disks/.
$ sudo mkdir -p /mnt/disks/MOUNT_DIR
Remplacez MOUNT_DIR par le répertoire dans lequel installer le disque.
Utilisez l'outil mount pour installer le disque sur l'instance et activez l'option discard :
$ sudo mount -o discard,defaults /dev/DEVICE_NAME /mnt/disks/MOUNT_DIR
Remplacez les éléments suivants :
DEVICE_NAME : nom du disque à installer.
MOUNT_DIR : répertoire dans lequel installer votre disque.
Configurez les autorisations de lecture et d'écriture sur l'appareil. Dans l'exemple ci-dessous, nous allons accorder à tous les utilisateurs un accès en écriture au disque :
$ sudo chmod a+w /mnt/disks/MOUNT_DIR
Remplacez MOUNT_DIR par le répertoire dans lequel vous avez installé le disque.
Configurer l'installation automatique au redémarrage de la VM
Ajoutez le disque à votre fichier /etc/fstab, afin qu'il soit installé de nouveau automatiquement au redémarrage de la VM. Sur les systèmes d'exploitation Linux, le nom de l'appareil peut changer à chaque redémarrage, mais l'UUID de l'appareil pointe toujours vers le même volume, même lorsque vous déplacez des disques entre les systèmes. C'est pourquoi nous vous recommandons d'utiliser l'UUID de l'appareil plutôt que son nom pour configurer l'installation automatique au redémarrage de la VM.
Créez une sauvegarde de votre fichier /etc/fstab actuel.
$ sudo cp /etc/fstab /etc/fstab.backup
Exécutez la commande blkid pour répertorier l'UUID du disque.
DEVICE_NAME : nom du disque que vous souhaitez installer automatiquement. Si vous avez créé une table de partition sur le disque, spécifiez la partition à installer en ajoutant le suffixe au nom de l'appareil. Par exemple, si sdb correspond au nom de l'appareil pour le disque, sdb1 peut être le nom de la partition.
Ouvrez le fichier /etc/fstab dans un éditeur de texte, puis créez une entrée comprenant l'UUID. Exemple :
UUID_VALUE : UUID du disque, répertorié dans le résultat de l'étape précédente.
MOUNT_DIR : répertoire dans lequel vous avez installé le disque.
FILE_SYSTEM_TYPE : le type de système de fichiers.
Par exemple, ext2, ext3, ext4, ou xfs.
MOUNT_OPTION : indique ce que le système d'exploitation fait s'il ne peut pas installer le disque zonal persistant au démarrage.
Pour connaître les valeurs valides, consultez le champ 4 dans la documentation Linux fstab.
Pour permettre au système de démarrer même si le disque n'est pas disponible, utilisez l'option d'installation nofail.
Utilisez la commande cat suivante pour vérifier que les entrées /etc/fstab sont correctes :
Gardez toujours le fichier /etc/fstab synchronisé avec les appareils associés à une VM. Si vous souhaitez dissocier un disque ou créer un instantané à partir du disque de démarrage d'une VM, modifiez le fichier /etc/fstab et supprimez l'entrée correspondant au disque. Même si vous définissez MOUNT_OPTION sur nofail ou nobootwait, supprimez l'entrée avant de créer l'instantané du disque de démarrage ou de dissocier le disque.
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/04 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 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."]]