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 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

This feature has the following restrictions:

  • During the Beta release of this feature, you cannot use the Cloud Console to attach or detach disks from instances.

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.

Use the gcloud command-line tool or API method to detach your boot disk from the instance:

gcloud

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

gcloud beta 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://www.googleapis.com/compute/beta/projects/[PROJECT_NAME]/zones/[ZONE]/instances/[INSTANCE_NAME]/detachDisk?deviceName=[DISK_NAME]

where:

  • [PROJECT_NAME] 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. If you customize a boot disk, you must ensure the boot disk boots properly after it is attached.

gcloud

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

gcloud beta 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://www.googleapis.com/compute/beta/projects/[PROJECT_ID]/zones/[ZONE]/instances/[INSTANCE_NAME]/attachDisk

{
 "boot": true,
 "source": "/compute/v1/projects/[PROJECT_ID]/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.

What's next?

Was this page helpful? Let us know how we did:

Send feedback about...

Compute Engine Documentation