Detaching and reattaching boot disks


You can detach boot disks from your VM instance and attach new boot disks. This feature simplifies the process of repairing boot disks by letting you mount them to another VM instance without deleting your original instance. Additionally, you can replace the boot disks for an instance rather than having to recreate the entire VM instance.

If a VM instance does not have a boot disk, attempting to start the instance generates an error. However, you can still edit other instance properties.

Before you begin

  • If you haven't already, set up authentication. Authentication is the process by which your identity is verified for access to Google Cloud services and APIs. To run code or samples from a local development environment, you can authenticate to Compute Engine as follows.

    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

    1. Install the Google Cloud CLI, then initialize it by running the following command:

      gcloud init
    2. 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, then initialize it by running the following command:

      gcloud init

Permissions required for this task

To perform this task, you must have the following permissions:

  • compute.instances.detachDisk on the instance
  • compute.instances.attachDisk on the instance

Restrictions

  • You can attach or detach a boot disk only from a stopped VM instance.
  • You can attach only one boot disk per VM instance.
  • If using the gcloud CLI or REST, you can attach only existing boot disks to a VM instance.

Detach a boot disk

Before detaching the boot disk from the VM instance, you must stop the instance. You don't need to unmount the disk.

Console

  1. Go to the VM instances page.

    Go to VM instances

  2. If prompted, select your project and click Continue.
  3. Click the instance for which you want to detach the boot disk.
  4. At the top of the page, click Stop.
  5. At the top of the page, click Edit.
  6. Scroll down to the Boot Disk section and click X next to the boot disk that you want to detach.
  7. Click Save.

gcloud

Use the gcloud compute instances detach-disk command to detach the boot disk from the instance:

gcloud compute instances detach-disk INSTANCE_NAME \
    --disk=DISK_NAME

Replace the following:

  • INSTANCE_NAME: the name of the instance.
  • DISK_NAME: the name of the disk to detach. The disk name is usually the same as the instance name.

REST

Construct a POST request to detach the disk:

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/detachDisk?deviceName=DISK_NAME

Replace the following:

  • PROJECT_ID: the ID of your project.
  • INSTANCE_NAME: the name of the instance.
  • ZONE: the zone where your instances is located.
  • DISK_NAME: the name of the disk to detach. The disk name is usually the same as the instance name.

Reattach a boot disk

Any disk can be reattached as a boot disk to an instance as long as the instance does not already have a boot disk attached and the boot disk is in the same zone as the VM instance. If you customize a boot disk, you must ensure that the boot disk boots properly after it is attached.

Both the existing disk and the new disk must either be UEFI-compatible or not. To create a UEFI-compatible disk, use the gcloud compute disk create command with the --guest-os-features flag set to UEFI_COMPATIBLE.

Console

  1. Go to the VM instances page.

    Go to VM instances

  2. If prompted, select your project and click Continue.
  3. Click the instance for which you want to attach the boot disk.
  4. If the VM instance is not already stopped, click Stop at the top of the page.
  5. Click Edit at the top of the page.
  6. Scroll down to the Boot Disk section.
  7. Click Add item.
  8. Select a boot disk from the drop-down menu.
  9. Click Save.

gcloud

Use the gcloud compute instances attach-disk command to attach or reattach a boot disk to a VM instance. Include the --boot flag to indicate that the disk is used as the boot disk for the instance. If this flag is absent, the disk is attached as a non-boot data disk.

gcloud compute instances attach-disk INSTANCE_NAME \
    --disk=DISK_NAME \
    --boot

Replace the following:

  • INSTANCE_NAME: the name of the instance.
  • DISK_NAME: the name of the disk to attach. The disk name may be the same as the instance name.

REST

Construct a POST request to attach the disk. Include the optional "boot": true parameter to indicate that this disk is the boot disk. If this parameter is absent, the disk is attached as a non-boot data disk.

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

Replace the following:

  • PROJECT_ID: the ID of your project.
  • INSTANCE_NAME: the name of the instance to which you're attaching the new Persistent Disk.
  • ZONE: the zone where your instance and new disk are located.
  • DISK_NAME: the name of the new disk.

After you attach the disk to your VM instance, restart the instance.

Update a boot disk for an instance

You can use only the Google Cloud console to update the boot disk for a VM in one step. Specifically, you can detach an existing boot disk and attach a new boot disk as one action.

This feature is not available through the gcloud CLI or REST.

  1. Go to the VM instances page.

    Go to VM instances

  2. If prompted, select your project and click Continue.
  3. Click the instance for which you want to attach the boot disk.
  4. If the VM instance is not already stopped, click Stop at the top of the page.
  5. Click Edit at the top of the page.
  6. Scroll down to the Boot Disk section.
  7. Click X next to the current boot disk.
  8. Click Add item.
  9. Select a boot disk from the drop-down menu.
  10. Click Save.

The Google Cloud console proceeds to detach your existing boot disk and attach the new boot disk that you specified. When this process is complete, restart the VM instance.

What's next?