읽기 전용 모드로 Compute Engine VM 인스턴스에 리전 디스크 연결
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
읽기-쓰기 모드로 Compute Engine VM 인스턴스에 리전 디스크를 연결합니다.
리전 디스크를 Google Compute Engine에서 실행되는 단일 가상 머신에 연결합니다. 또한 디스크를 사용하려면 운영체제에서 마운트해야 합니다.
디스크는 읽기 전용 모드로 연결됩니다. 디스크를 동시에 여러 VM 인스턴스에 연결할 수 있습니다.
코드 샘플
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 Oracle 계열사의 등록 상표입니다.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["이해하기 어려움","hardToUnderstand","thumb-down"],["잘못된 정보 또는 샘플 코드","incorrectInformationOrSampleCode","thumb-down"],["필요한 정보/샘플이 없음","missingTheInformationSamplesINeed","thumb-down"],["번역 문제","translationIssue","thumb-down"],["기타","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)."]]