虛擬機器 (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
-
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.
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.
-
啟用及停用序列埠輸出記錄
您可以透過設定專案層級或執行個體層級中繼資料,來控制執行個體是否將序列埠輸出傳送至 Cloud Logging。您也可以透過設定組織政策來停用機構中所有使用者的這項功能。
設定專案和執行個體中繼資料
根據預設,系統會停用將序列埠輸出記錄寫入至 Cloud Logging 的功能。如果您的機構未限制將序列埠輸出記錄寫入至 Cloud Logging,可以將 serial-port-logging-enable
中繼資料項目設為 true
或 false
,為專案和個別 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 中建立排除篩選器,以從 Logs Explorer 中移除特定序列埠項目。舉例來說,如果專案層級的中繼資料項目設為 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 的功能。
查看序列埠輸出內容
主控台
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
選取要查看其序列埠輸出內容的 VM 執行個體。
在「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
:您想查看輸出內容的埠編號 (1
、2
、3
或4
)。系統層級的實體通常會使用第一個序列埠 (序列埠 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
- 啟用將序列埠記錄寫入至 Cloud Logging 的功能。
前往「VM instances」(VM 執行個體) 頁面。
選取您要查看其啟動代理程式記錄的 VM 執行個體。
按一下「Logs」下方的「Cloud Logging」,即可查看 Cloud Logging 記錄。
展開「All logs」下拉式選單,選取您要查看的序列埠輸出內容。系統層級的執行個體通常會使用第一個序列埠 (序列埠 1),又稱序列主控台。如果下拉式選單中沒有出現某個序列埠,表示這個序列埠沒有可查看的輸出內容。
您也可以展開「Any log level」(任何記錄層級) 下拉式選單,選擇隱藏低於您要查看的嚴重性等級的記錄。例如,如果您選取 [Info] (資訊) 記錄層級,將會隱藏「Debug」(偵錯) 記錄項目。「Debug」(偵錯) 項目通常只有在啟動時由 BIOS 使用。
如需進一步瞭解篩選功能,請參閱「使用記錄檔檢視器」一文。
處理非 UTF8 字元
序列埠輸出內容是使用開放原始碼 Abseil C++ 程式庫的 CHexEscape()
方法逸出,因此非 UTF8 字元會以十六進位字串的形式編碼。您可以使用對應的 CUnescape()
方法,取得傳送至序列埠的確切輸出內容。
常見的序列埠錯誤
以下是您在序列埠輸出中可能會遇到的常見錯誤示例。
設定 IPv6 時發生錯誤
如果未設定 IPv6,OS 日誌記錄可能會出現以下錯誤:
ERROR addresses.go:301 Error configuring IPv6: Internet Systems Consortium DHCP Client 4.2.5
請忽略這項錯誤。這個錯誤不會影響您的 VM。