Memasang disk regional ke instance VM Compute Engine dalam mode hanya baca
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Memasang disk regional ke instance VM Compute Engine dalam mode baca-tulis
Memasang disk regional ke satu mesin virtual yang berjalan di Google Compute Engine. Disk juga harus dipasang oleh sistem operasi sebelum dapat digunakan.
Disk dipasang dalam mode hanya baca. Disk dapat dipasang ke beberapa instance VM secara bersamaan.
Contoh kode
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","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)."]]