Detaching and Reattaching Boot Disks

Previously, boot disks were permanently attached to their VM instances. Now you can detach boot disks from your instance. This feature simplifies the process for repairing boot disks by allowing you to mount them to another 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

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 only attach or detach a boot disk from a stopped VM instance.
  • You can only attach one boot disk per VM instance.
  • If using the gcloud tool or the API, the boot disk must already exist beforehand, before you can attach it to an instance.

Detaching a boot disk

Before detaching the boot disk from the instance, you must stop the instance. There is no need to unmount the disk.

Console

  1. Go to the VM instances page.

    Go to the VM instances page

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

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]

where:

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

API

In the API, 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]

where:

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

Reattaching 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 the boot disk boots properly after it is attached.

Console

  1. Go to the VM instances page.

    Go to the VM instances page

  2. If prompted, select your project and click Continue.
  3. Click on 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. Next, click the Edit button at the top of the page.
  6. Scroll down to the Boot Disk section.
  7. Click on Add item.
  8. Select a boot disk from the dropdown menu.
  9. Save your changes.

gcloud

Use the gcloud compute instances attach-disk to attach or reattach a boot disk to a VM instance. Include the --boot flag to indicate that the disk should be the boot disk for the instance.

gcloud compute instances attach-disk [INSTANCE_NAME] --disk=[DISK_NAME] --boot

where:

  • [INSTANCE_NAME] is the name of the instance.
  • [DISK_NAME] is the name of the disk to attach. The disk name may be the same as the instance name.
  • --boot is an optional flag indicating that the attached disk must be used as the boot disk for the instance. If this flag is absent, the disk is attached as a secondary disk.

API

In the API, construct a POST request to attach the disk. Include the "boot": true parameter to indicate that this disk is the boot 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]"
}

where:
  • "boot": true is an optional parameter indicating that the attached disk must be used as the boot disk for the instance. If this parameter is absent, the disk is attached as a secondary disk.
  • [PROJECT_ID] is your project ID.
  • [ZONE] is the zone where your instance and new disk are located.
  • [INSTANCE_NAME] is the name of the instance where you are adding the new persistent disk.
  • [DISK_NAME] is the name of the new disk.

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

Updating a boot disk for an instance

In the Cloud Console only, you can 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 only available in the console and is not available using the gcloud tool or through the API.

  1. Go to the VM instances page.

    Go to the VM instances page

  2. If prompted, select your project and click Continue.
  3. Click on 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. Next, click the Edit button 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 dropdown menu.
  10. Save your changes.

The console will proceed to detach your existing booth disk and attach the new boot disk that you specified. Restart the instance once the process is complete.

What's next?

هل كانت هذه الصفحة مفيدة؟ يرجى تقييم أدائنا:

إرسال تعليقات حول...

Compute Engine Documentation