建立新的永久磁碟磁碟區


您可以建立空白的永久磁碟磁碟區,也可以從資料來源建立磁碟。您可以將永久磁碟用於虛擬機器 (VM) 執行個體的開機磁碟,或用於連結至 VM 的資料磁碟。

本文說明如何建立空白的非開機區域永久磁碟磁區,並將其連接至 VM。

如要建立及新增其他類型的磁碟,請參閱以下文章:

事前準備

  • 如果尚未設定,請先設定驗證機制。驗證是指驗證身分,以便存取 Google Cloud 服務和 API 的程序。如要在本機開發環境中執行程式碼或範例,您可以選取下列任一選項,向 Compute Engine 進行驗證:

    Select the tab for how you plan to use the samples on this page:

    Console

    When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.

    gcloud

    1. After installing the Google Cloud CLI, initialize it by running the following command:

      gcloud init

      If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    2. Set a default region and zone.

    Terraform

    To use the Terraform samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.

    1. Install the Google Cloud CLI.
    2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    3. To initialize the gcloud CLI, run the following command:

      gcloud init
    4. If you're using a local shell, then create local authentication credentials for your user account:

      gcloud auth application-default login

      You don't need to do this if you're using Cloud Shell.

      If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

    For more information, see Set up authentication for a local development environment.

    REST

    To use the REST API samples on this page in a local development environment, you use the credentials you provide to the gcloud CLI.

      After installing the Google Cloud CLI, initialize it by running the following command:

      gcloud init

      If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    For more information, see Authenticate for using REST in the Google Cloud authentication documentation.

限制

  • 建立 VM 時,您最多可以連接 127 個次要非開機區域性永久磁碟磁碟區。
  • 每個 VM 的總連接容量為 257 TB。如要瞭解如何使用大型磁碟區來提高效能,請參閱「邏輯磁碟區大小」一文。

將非開機磁碟新增至 VM

使用 Google Cloud 控制台Google Cloud CLIREST 建立及連接非開機區域磁碟。

如果您在 Google Cloud 控制台中建立磁碟,預設的磁碟類型pd-balanced。如果您使用 gcloud CLI 或 REST 建立磁碟,預設磁碟類型為 pd-standard

將磁碟連接至 VM 時,請指定自訂裝置名稱。您指定的名稱會用於在來賓作業系統中為磁碟產生符號連結,方便識別。

主控台

  1. 前往「VM instances」(VM 執行個體) 頁面。

    前往 VM 執行個體頁面

  2. 按一下要新增磁碟的 VM 名稱。

  3. 在詳細資料頁面中,按一下「編輯」

  4. 在「Additional disks」(其他磁碟) 下方,點選 [Add new disk] (增加新磁碟)

  5. 指定磁碟的名稱,設定磁碟的屬性,然後選取「空白」做為「來源類型」

  6. 選用:在「裝置名稱」標題下方,選取「使用自訂裝置名稱」選項。您輸入的名稱會用於產生磁碟的符號連結,方便您識別磁碟。

  7. 按一下[Done] (完成) 即可完成磁碟的設定。

  8. 按一下「儲存」,將變更套用至 VM 並新增新磁碟。

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. 使用 gcloud compute disks create 指令建立區域永久磁碟磁區。

    gcloud compute disks create DISK_NAME \
      --size DISK_SIZE \
      --type DISK_TYPE
    

    更改下列內容:

    • DISK_NAME:新磁碟的名稱。
    • DISK_SIZE:新磁碟的大小 (以 GB 為單位)。可接受的大小範圍為 10 GB 到 65,536 GB (含首尾),調整的單位為 1 GB。
    • DISK_TYPE:永久磁碟磁碟區的類型完整或部分網址。例如:https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/diskTypes/pd-ssd
  3. 建立磁碟之後,請將其連接至任何執行中或已停止的 VM。使用 gcloud compute instances attach-disk 指令

    gcloud compute instances attach-disk VM_NAME \
      --disk DISK_NAME --device-name=DEVICE_NAME
    

    更改下列內容:

    • VM_NAME:要新增區域永久磁碟磁區的 VM 名稱
    • DISK_NAME:要連接至 VM 的新磁碟名稱。
    • DEVICE_NAME:選用:供來賓作業系統用來識別磁碟的名稱。
  4. 使用 gcloud compute disks describe 指令查看磁碟說明。

Terraform

如要建立磁碟,請使用 google_compute_disk 資源

# Using pd-standard because it's the default for Compute Engine

resource "google_compute_disk" "default" {
  name = "disk-data"
  type = "pd-standard"
  zone = "us-west1-a"
  size = "5"
}

如要將磁碟連接至 VM,請使用 google_compute_instance 資源

resource "google_compute_instance" "test_node" {
  name         = "test-node"
  machine_type = "f1-micro"
  zone         = "us-west1-a"

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-11"
    }
  }
  attached_disk {
    source      = google_compute_disk.default.id
    device_name = google_compute_disk.default.name
  }

  network_interface {
    network = "default"
    access_config {
      # Ephemeral IP
    }
  }

  # Ignore changes for persistent disk attachments
  lifecycle {
    ignore_changes = [attached_disk]
  }


}

如要瞭解如何套用或移除 Terraform 設定,請參閱「基本 Terraform 指令」。

REST

  1. 使用 disks.insert 方法建構 POST 要求,以建立區域永久磁碟。請加入 namesizeGbtype 屬性。如要將這個磁碟建立為空白且未格式化的非開機磁碟,請勿指定來源映像檔或來源快照。

    POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/disks
    
    {
     "name": "DISK_NAME",
     "sizeGb": "DISK_SIZE",
     "type": "DISK_TYPE"
    }
    

    更改下列內容:

    • PROJECT_ID:您的專案 ID。
    • ZONE:VM 和新磁碟所在的可用區。
    • DISK_NAME:新磁碟的名稱。
    • DISK_SIZE:新磁碟的大小 (以 GB 為單位)。可接受的大小範圍為 10 GB 到 65,536 GB (含首尾),調整的單位為 1 GB。
    • DISK_TYPE:永久磁碟的類型完整或部分網址。例如:https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/diskTypes/pd-ssd
  2. compute.instances.attachDisk 方法建構 POST 要求,並加入您剛建立的區域永久磁碟磁區的網址:

    POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/attachDisk
    
    {
     "source": "/compute/v1/projects/PROJECT_ID/zones/ZONE/disks/DISK_NAME",
     "deviceName": DEVICE_NAME
    }
    

    更改下列內容:

    • PROJECT_ID:您的專案 ID
    • ZONE:VM 和新磁碟所在的可用區
    • VM_NAME:要新增永久磁碟磁區的 VM 名稱
    • DISK_NAME:新磁碟的名稱
    • DEVICE_NAME:選用:供來賓作業系統用來識別磁碟的名稱。

建立新磁碟並將其連接至 VM 後,您必須格式化及掛接磁碟,讓作業系統能使用可用的儲存空間。

後續步驟