Attach a regional disk to a Compute Engine VM instance in read-only mode
Stay organized with collections
Save and categorize content based on your preferences.
Attach a regional disk to a Compute Engine VM instance in read-write mode
Attach a regional disk to a single virtual machine running in Google Compute Engine. The disk also needs to be mounted by the operating system before it can be used.
The disk is attached in read-only mode. The disk can be attached to multiple VM instances simultaneously.
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License
, and code samples are licensed under the Apache 2.0 License
. For details, see the Google Developers Site Policies
. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis code sample demonstrates how to attach a regional disk to a Google Compute Engine virtual machine instance.\u003c/p\u003e\n"],["\u003cp\u003eThe disk is attached in read-only mode, allowing it to be simultaneously attached to multiple VM instances.\u003c/p\u003e\n"],["\u003cp\u003eThe provided Go code snippet uses the Compute Engine API to perform the disk attachment operation.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication is required to use the Compute Engine API, using Application Default Credentials for a local development environment.\u003c/p\u003e\n"],["\u003cp\u003eBefore the disk can be used it needs to be mounted by the operating system.\u003c/p\u003e\n"]]],[],null,["# Attach a regional disk to a Compute Engine VM instance in read-only mode\n\nAttach a regional disk to a Compute Engine VM instance in read-write mode\n\nAttach a regional disk to a single virtual machine running in Google Compute Engine. The disk also needs to be mounted by the operating system before it can be used.\n\nThe disk is attached in read-only mode. The disk can be attached to multiple VM instances simultaneously.\n\nCode sample\n-----------\n\n### Go\n\n\nBefore trying this sample, follow the Go setup instructions in the\n[Compute Engine quickstart using\nclient libraries](/compute/docs/api/using-libraries).\n\n\nFor more information, see the\n[Compute Engine Go API\nreference documentation](/go/docs/reference/cloud.google.com/go/compute/latest/apiv1).\n\n\nTo authenticate to Compute Engine, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \tcompute \"cloud.google.com/go/compute/apiv1\"\n \tcomputepb \"cloud.google.com/go/compute/apiv1/computepb\"\n )\n\n // Attaches the provided regional disk in read-only mode to the given VM.\n // Read-only disks can be attached to multiple VMs at once.\n func attachRegionalDiskReadOnly(w io.Writer, projectID, zone, instanceName, diskUrl string) error {\n \t// projectID := \"your_project_id\"\n \t// zone := \"us-west3-a\" // refers to the instance, not the disk\n \t// instanceName := \"your_instance_name\"\n \t// diskUrl := \"projects/your_project/regions/europe-west3/disks/your_disk\"\n\n \tctx := context.Background()\n \tinstancesClient, err := compute.https://cloud.google.com/go/docs/reference/cloud.google.com/go/compute/latest/apiv1.html#cloud_google_com_go_compute_apiv1_InstancesClient_NewInstancesRESTClient(ctx)\n \tif err != nil {\n \t\treturn err\n \t}\n \tdefer instancesClient.Close()\n\n \tmode := \"READ_ONLY\"\n\n \treq := &computepb.AttachDiskInstanceRequest{\n \t\tAttachedDiskResource: &computepb.AttachedDisk{\n \t\t\tSource: &diskUrl,\n \t\t\tMode: &mode,\n \t\t},\n \t\tInstance: instanceName,\n \t\tProject: projectID,\n \t\tZone: zone,\n \t}\n\n \top, err := instancesClient.https://cloud.google.com/go/docs/reference/cloud.google.com/go/compute/latest/apiv1.html#cloud_google_com_go_compute_apiv1_InstancesClient_AttachDisk(ctx, req)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"unable to attach disk: %w\", err)\n \t}\n\n \tif err = op.Wait(ctx); err != nil {\n \t\treturn fmt.Errorf(\"unable to wait for the operation: %w\", err)\n \t}\n\n \tfmt.Fprintf(w, \"Disk attached\\n\")\n\n \treturn nil\n }\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=compute)."]]