查看序列埠輸出內容


虛擬機器 (VM) 執行個體有四個虛擬序列埠。執行個體的作業系統、BIOS 及其他系統層級實體通常會將輸出內容寫入序列埠,因此序列埠輸出內容可在排解當機、啟動失敗、開機或關機問題時派上用場。

本頁面說明查看序列埠輸出內容的方法,包括使用 Cloud Logging 來保留序列埠輸出內容,即使在執行個體已停止或遭刪除後也可繼續保留。如果您需要在執行個體運作時傳送指令至序列埠,請參閱與序列主控台互動一文。

只有在 VM 執行個體運作時,才能透過 Google Cloud 控制台、gcloud CLI 和 REST 存取序列埠輸出。每個通訊埠的輸出記錄上限為 1 MB。

如果您啟用序列埠輸出記錄功能,Cloud Logging 每月提供前 50 GiB 的免費記錄,並將記錄保留 30 天

事前準備

  • 如要在 Cloud Logging 中記錄序列埠輸出內容,請先熟悉 Cloud Logging
  • 如果尚未設定驗證,請先完成設定。 「驗證」是指驗證身分的程序,確認您有權存取 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.
      3. REST

        如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 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.

        詳情請參閱 Google Cloud 驗證說明文件中的「Authenticate for using REST」。

啟用及停用序列埠輸出記錄

您可以透過設定專案層級或執行個體層級中繼資料,來控制執行個體是否將序列埠輸出傳送至 Cloud Logging。您也可以透過設定組織政策來停用機構中所有使用者的這項功能。

設定專案和執行個體中繼資料

根據預設,系統會停用將序列埠輸出記錄寫入 Cloud Logging 的功能。如果您的機構未限制將序列埠輸出記錄寫入至 Cloud Logging,可以透過將 serial-port-logging-enable 中繼資料項目設定為 truefalse,為專案和個別 VM 執行個體啟用或停用該功能。

如果您設定全專案中繼資料項目,專案中的所有 VM 執行個體都會繼承該設定。如果您設定執行個體中繼資料項目,無論專案設定為何,系統都只會針對該 VM 啟用中繼資料項目。

您可以使用 Google Cloud 控制台、gcloud CLI 或 Compute Engine API 設定中繼資料項目。詳情請參閱「設定自訂中繼資料」一節。

舉例來說,下列 gcloud CLI 指令會為專案啟用將序列埠輸出記錄寫入至 Cloud Logging 的功能:

gcloud compute project-info add-metadata \
    --metadata serial-port-logging-enable=true

同樣地,下列 gcloud CLI 指令會為特定執行個體啟用序列埠輸出記錄寫入 Cloud Logging 的功能:

gcloud compute instances add-metadata INSTANCE_NAME \
    --metadata serial-port-logging-enable=true

如要停用將序列埠輸出記錄寫入至 Cloud Logging 的功能,請將 serial-port-logging-enable 設為 false

gcloud compute instances add-metadata INSTANCE_NAME \
    --metadata serial-port-logging-enable=false

「排除」篩選器

您可以在 Cloud Logging 中建立排除篩選器,以從記錄檔探索器中移除特定序列埠項目。舉例來說,如果專案層級的中繼資料項目設為 serial-port-logging-enable=true,您可以使用進階篩選器,針對特定 VM 執行個體停用序列埠輸出記錄功能:

logName = "projects/PROJECT_ID/logs/serialconsole.googleapis.com%2Fserial_port_1_output"
resource.type = "gce_instance"
resource.labels.instance_id != "INSTANCE_1_ID"
resource.labels.instance_id != "INSTANCE_2_ID"

設定組織政策

您可以透過設定組織政策,為整個機構停用將序列埠輸出記錄寫入至 Cloud Logging 的功能,藉此限制 Google Cloud 資源的某些設定。具體來說,請設定下列布林限制:constraints/compute.disableSerialPortLogging。詳情請參閱「建立及管理組織政策」一文。

constraints/compute.disableSerialPortLogging 設為 true 來停用序列埠記錄功能的效力不溯及既往。如果現有 VM 執行個體的中繼資料項目啟用序列埠記錄功能,將記錄寫入 Cloud Logging,這些執行個體會繼續將記錄寫入 Cloud Logging,除非您重設這些執行個體的中繼資料

將此機構限制設為 true 後,您就無法為機構內的任何執行個體,將執行個體或專案中繼資料設定為啟用將序列埠輸出記錄寫入至 Cloud Logging 的功能。

查看序列埠輸出內容

主控台

  1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面

    前往 VM 執行個體頁面

  2. 選取要查看其序列埠輸出內容的 VM 執行個體。

  3. 在「Logs」(記錄) 底下,點選 [Serial port 1] (序列埠 1)、[Serial port 2] (序列埠 2)、[Serial port 3] (序列埠 3) 或 [Serial port 4] (序列埠 4)。系統層級的實體通常會使用第一個序列埠 (序列埠 1),又稱序列主控台

gcloud

使用 gcloud compute instances get-serial-port-output 指令

gcloud compute instances get-serial-port-output INSTANCE_NAME \
  --port PORT \
  --start START \
  --zone ZONE

更改下列內容:

  • INSTANCE_NAME:執行個體的名稱。
  • PORT:序列埠編號 (1234),視您想查看哪個序列埠的輸出而定。系統層級的實體通常會使用第一個序列埠 (序列埠 1),又稱序列主控台。系統預設會傳回第一個序列埠的輸出內容。
  • START:您要傳回的第一個位元組的位元組索引 (從零開始)。如果先前要求的輸出內容過長,無法一次傳回,而您想繼續查看該要求的輸出內容,請使用這個旗標。
  • ZONE:執行個體所在的可用區。

REST

在 API 中,建立目標為 instances.getSerialPortOutput 方法get 要求。

GET https://compute.googleapis.com/compute/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/serialPort

Cloud Logging

  1. 啟用將序列埠記錄寫入至 Cloud Logging 的功能
  2. 前往「VM instances」(VM 執行個體) 頁面。

    前往 VM 執行個體頁面

  3. 選取您要查看其啟動代理程式記錄的 VM 執行個體。

  4. 按一下「Logs」(記錄) 底下的「Cloud Logging」,查看 Cloud Logging 記錄。

    按一下「Cloud Logging」,即可查看 Cloud Logging 記錄。

如要進一步瞭解如何篩選記錄,請參閱「使用記錄檔探索工具」一文。

處理非 UTF8 字元

序列埠輸出內容是使用開放原始碼 Abseil C++ 程式庫的 CHexEscape() 方法逸出,因此非 UTF8 字元會以十六進位字串的形式編碼。您可以使用對應的 CUnescape() 方法,取得傳送至序列埠的確切輸出內容。

常見的序列埠錯誤

以下是您可能會在序列埠輸出中遇到的常見錯誤範例。

設定 IPv6 時發生錯誤

如果未設定 IPv6,OS 記錄檔可能會出現下列錯誤:

ERROR addresses.go:301 Error configuring IPv6: Internet Systems Consortium DHCP Client 4.2.5

請忽略這項錯誤。這個錯誤不會影響虛擬機器。