元件 ID 索引

術語

範圍

本文件說明如何定義索引,將執行個體 ID 對應至別名。這項對應作業對於使用隔離式儲存空間功能的元件至關重要。

總覽

元件執行個體 ID 索引的目標,是為元件執行個體指派穩定的 ID。方法是將例項 ID 對應至路徑名稱。為元件執行個體指派 ID 後,系統會使用這個例項 ID 在磁碟上識別其永久資源。這樣一來,您就能變更元件的網址或領域,同時保留其資源的歸屬,只要這個索引也已更新即可。

元件執行階段發現執行個體 ID -> 路徑名稱對應項目時,會自動將元件執行個體的現有儲存空間目錄移至其執行個體 ID 下的鍵。

只有使用儲存空間功能的元件才需要納入索引。測試元件不應納入索引。

定義新索引

索引檔案是 JSON5 格式的檔案,可將元件的例項 ID 對應至其路徑名稱。建構樹狀結構中可能會有多個索引檔案,但這些檔案會合併為單一索引檔案,並提供給元件執行階段使用。這個合併索引檔案是不可變動的,只能透過其他系統更新來更新。

以下範例說明索引檔案的結構定義:

// Index files are written in JSON5, so you may use comments!
{
  // A list of entries, where each entry maps an instance ID to a moniker.
  instances: [
    // An entry, mapping an instance ID to a moniker.
    {
      // Instance IDs are randomly generated, 256-bits of base-16 encoded
      // strings (in lower case). To generate a new instance ID, set this to
      // an empty string (or other invalid value) and run the build; the build
      // will fail and suggest a new instance ID which you can copy-paste here.
      instance_id: "2bd6cc2bd10243354b873a4ddb8a188b1d29171e26eebac06567bcdc36614af6",
      // The `instance_id` above is associated to the following moniker:
      moniker: "/core/account/credential_manager",
    },

    // More than one entry can be included. However, all entries must be distinct:
    // * Two entries cannot reference the same `instance_id`
    // * Two entries cannot reference the same `moniker`
    {
      instance_id: "7db7e88479772e241229682b47f1794e12ac5d692f8d67421acd9d7ff318a975",
      moniker: "/core/account/password_authenticator",
    }
  ]
}

如要為建構作業提供索引檔案,請使用 component_id_index() GN 範本:

component_id_index("my_component_id_index") {
  source = "my_component_id_index.json5"
}

在索引中新增元件

找出適當的索引檔案

如要將元件新增至索引,您必須在適當的索引檔案中插入項目。目前,fuchsia.git 的元件會列於 core_component_id_index.json5 索引檔案中。

在索引中新增項目

第一步是決定元件執行個體的路徑名稱。您可以執行 ffx component show,找出特定產品的工程版本中元件的路徑名稱。

接著,使用元件的路徑名稱,在 instances 清單中附加項目。將 instance_id 欄位設為無效值 (例如空字串),讓建構作業失敗,並建議您可使用的新值。

範例

在這個範例中,元件 fuchsia-pkg://example.com/my_other_package#meta/my_other_component.cm 會新增至索引。

如要判斷元件執行個體的路徑名稱,您可以執行 ffx component show fuchsia-pkg://example.com/my_other_package#meta/my_other_component.cm

$ ffx component show fuchsia-pkg://example.com/my_other_package#meta/my_other_component.cm
               Moniker:  /core/my_other_component
                   URL:  fuchsia-pkg://example.com/my_other_package#meta/my_other_component.cm
                      ...

上方輸出內容顯示這個例項的路徑名稱為 /core/my_other_component

將這個項目附加到 core_component_id_index.json5instances 清單中,並使用無效的空白 instance_id 新增 /core/my_other_component

  {
    instance_id: "",
    moniker: "/core/my_other_component"
  }

現在請執行建構作業。這會導致建構作業失敗,並建議您使用新的執行個體 ID:

$ fx build
.
.
Product Assembly Failed
    ...
    4.  parsing obj/bundles/assembly/resources/core_component_id_index.json5
    5.  Invalid instance ID: invalid length; must be 64 characters

Here is a valid, randomly generated ID: 4251833a8d2e47b473732a23de84437e0b346a151ec1e7bdfd43b91e02f894a7

複製建議的 instance_id 欄位,更新您新增的項目。建構作業現在應該會通過。

在系統組合中加入元件 ID 索引

本節的目標對象為設定系統組合的產品擁有者

本節說明如何在系統組合中加入元件 ID 索引。

如果系統組合包含使用隔離儲存空間的元件,則應包含元件 ID 索引。任何以 core 產品為基礎的產品,其組合中都已包含元件 ID 索引,因此您可能不需要執行下列操作說明。

系統版本中的所有 component_id_index() 都會使用 component_id_index_config() 範本合併。此範本目前用於 assembled_system.gni,如果您同時定義自己的範本和 assembled_system.gni 的範本,組合作業就會失敗。

步驟

  1. 定義要納入系統的任何 component_id_index()
  2. assembled_system() 目標中,將這些目標新增為 base_packages 的依附元件。