您可以透過現有的 Compute Engine Windows Server 映像檔,建立自訂 Windows Server 映像檔。使用這些自訂映像檔,建立開機磁碟與現有執行個體相同的執行個體。
對於在現有執行個體上保存作業系統的配置設定,以及重新使用相同的設定來建立其他執行個體,這些自訂映像檔非常實用。
本節未涵蓋以下任務:
- 請改為從公開映像檔或自訂映像檔建立 VM,並參閱「建立新的 Windows 執行個體」一文。
- 請排定永久磁碟的備份作業,如需瞭解相關資訊,請參閱「永久磁碟快照」。
- 匯入 Windows Server 或 SQL Server 系統;請改用 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
-
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.
- Set a default region and zone.
Go
To use the Go 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.
- Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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.
Java
To use the Java 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.
- Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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.
Node.js
To use the Node.js 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.
- Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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.
Python
To use the Python 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.
- Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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.
-
準備建立 Windows Server 映像檔
在您建立映像檔之前,請在執行個體上執行 GCESysprep
,以針對映像檔建立程序做好準備。
GCESysprep
會完成下列步驟,為執行個體做好成為 Compute Engine 映像檔的準備:
- 停用
GCEStartup
排程工作。 - 從暫存資料夾位置刪除所有檔案。
- 清除所有 Windows 事件記錄。
- 執行 sysprep.exe /generalize /oobe /quit /unattend
- 設定
instance_setup.ps1
,以便在 VM 執行個體初次啟動時執行。 - 刪除 RDP 憑證。
- 移除儲存的永久磁碟清單。
- 啟用 RDP 和 WinRM 防火牆規則。
- 停用 Google OS Config 服務。
- 關閉執行個體。
GCESysprep
作業會記錄到 Windows 事件記錄檔和序列埠 1。Sysprep 會寫入多個記錄檔案。
如何使用 GCESysprep
建立 Compute Engine 映像檔:
以管理員權限執行
GCESysprep
。GCESysprep
指定圖片位置
建立自訂映像檔時,您也可以指定映像檔的 Cloud Storage 位置,但不包括雙地區位置。藉由指定映像檔的儲存位置,您可以確保跨地區的備援功能,藉以遵循資料位置的法規並符合高可用性需求。
您可以自行選擇是否要使用儲存位置功能。如果未選擇位置,Compute Engine 會將映像檔儲存在最靠近映像檔來源的多地區位置。您可以從來源磁碟、映像檔、快照或儲存在 Cloud Storage 中的映像檔建立自訂映像檔。您可以使用這些映像檔,建立新的 VM 執行個體。
此功能推出前的所有現有映像檔都會保留在原來的位置。唯一的不同是您可以查看所有映像檔的所在位置。如要移動現有映像檔,您必須在需要的位置重新建立它。
建立 Windows 映像檔
您可從下列來源建立磁碟映像檔:
- 永久磁碟,即使該磁碟已連接執行個體。
- 永久磁碟的快照
- 專案中的另一個映像檔
- 從另一個專案共用的映像檔
儲存在 Cloud Storage 中的匯入映像檔
主控台
- 在 Google Cloud 控制台中,前往「Create a new image」(建立新的映像檔) 頁面。
- 指定映像檔「Name」(名稱)。
- 選擇「Source」(來源) 磁碟,這個磁碟應具有您要建立映像檔的 Windows 作業系統。
- 指定儲存映像檔的「Location」(位置)。從 [Based on source disk location (default)] (根據來源磁碟位置 (預設值)) 下拉式選單中選擇映像檔位置。例如,指定
us
會將映像檔儲存在us
多地區;指定us-central1
則會儲存在us-central1
地區。如果未選擇位置,Compute Engine 會將映像檔儲存在最靠近映像檔來源的多地區。 - 指定映像檔的屬性。例如,可以為映像檔指定映像檔系列名稱,以將此映像檔整理為映像檔系列的成員。
- 按一下 [建立]。
gcloud
使用 gcloud compute
和 images create
,並提供您要用來建立映像檔的來源永久磁碟。您可以選擇加入 --force
旗標,即使映像檔已連結至執行中的執行個體,仍可建立映像檔。
gcloud compute images create example-image --source-disk [DISK_NAME] \
--source-disk-zone [ZONE] \
--storage-location [LOCATION] \
[--force]
[DISK_NAME]
是要建立映像檔的來源磁碟名稱。[ZONE]
是磁碟的區域。[LOCATION]
是選用旗標,讓您指定要儲存映像檔的地區或多地區。例如,指定us
會將映像檔儲存在us
多地區;指定us-central1
則會儲存在us-central1
地區。如果未選擇位置,Compute Engine 會將映像檔儲存在最靠近映像檔來源的多地區。--force
是選用旗標,即使磁碟已連結至執行中的執行個體仍可建立映像檔。這個選項可能會危害映像檔的完整性。如果可以的話,請在建立映像檔之前,停止執行個體。
當您執行此指令時,gcloud compute
會根據您提供的永久磁碟建立一個新映像檔,並將該映像檔加到您的集合中。執行下列指令即可確認映像檔是否已成功建立:
gcloud compute images list
Go
在試用這個範例之前,請先按照 使用用戶端程式庫的 Compute Engine 快速入門中的操作說明設定 Go。詳情請參閱 Compute Engine Go API 參考資料說明文件。
如要向 Compute Engine 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Java
在試用這個範例之前,請先按照 使用用戶端程式庫的 Compute Engine 快速入門中的操作說明設定 Java。詳情請參閱 Compute Engine Java API 參考資料說明文件。
如要向 Compute Engine 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Node.js
Node.js
在試用這個範例之前,請先按照 使用用戶端程式庫的 Compute Engine 快速入門中的操作說明設定 Node.js。詳情請參閱 Compute Engine Node.js API 參考資料說明文件。
如要向 Compute Engine 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Python
Python
在試用這個範例之前,請先按照 使用用戶端程式庫的 Compute Engine 快速入門中的操作說明設定 Python。詳情請參閱 Compute Engine Python API 參考資料說明文件。
如要向 Compute Engine 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
REST
對 images().insert
方法提出主體包含 sourceDisk
網址的 POST
要求。
POST https://compute.googleapis.com/compute/v1/projects/[PROJECT_ID]/global/images?[FORCE_OPTION]
{
"name": "[IMAGE_NAME]",
"sourceDisk": "zones/[ZONE]/disks/[DISK_NAME]",
"storageLocations": "[LOCATION]",
}
其中:
[PROJECT_ID]
是這項要求的專案 ID。[FORCE_OPTION]
是用來建立映像檔的選項,即使來源磁碟已連結至執行中的執行個體。請在 POST 行列末端指定forceCreate=true
,即可設定此選項。這個選項可能會危害映像檔的完整性。如果可以的話,請在建立映像檔之前,停止執行個體。[IMAGE_NAME]
是您要為此映像檔指定的名稱。[ZONE]
是要從中建立映像檔的來源磁碟區域。[DISK_NAME]
是來源磁碟的名稱。[LOCATION]
是選用參數,用來選取儲存映像檔的多地區或地區。例如,指定us
會將映像檔儲存在us
多地區;指定us-central1
則會儲存在us-central1
地區。如果未選擇位置,Compute Engine 會將映像檔儲存在最靠近映像檔來源的多地區。
如需更多新增映像檔的資訊,請參閱映像檔參考資料。
更新 Windows 代理程式和指令碼
Compute Engine 偶爾會發佈包含最新代理程式和指令碼的新 Windows 映像檔。在處理 Windows 執行個體的啟動和關閉程序、帳戶管理以及位址管理等作業時,這些項目非常實用。
自 Windows 映像檔 v20160112
版起,Windows 代理程式會透過上游發布內容自行更新。您可將 disable-agent-updates
執行個體中繼資料鍵值設為 true
以停用代理程式更新。假如您有基於較舊映像檔版本的執行個體,則可以為這些執行個體手動更新 Windows 代理程式。