將儲存空間分區路徑傳送至元件

雖然大多數元件會透過檔案系統使用儲存空間功能與儲存空間互動,但某些元件可能需要直接讀取及寫入分割區。本指南說明如何將分割區路由至元件。

大致步驟如下:

  1. 在主機板設定中定義語意標籤:定義實體分割區標籤到「語意」標籤的對應,這會是能力轉送中使用的分割區名稱。
  2. 更新元件資訊清單:將區塊裝置的目錄能力路徑傳送至元件。
  3. 在元件中存取區塊裝置:元件會使用已路由的目錄能力存取區塊裝置。

步驟

1. 在主面板設定中定義語意標籤

首先,請在主機板的設定中為實體分割區宣告語意標籤,通常是位於 //boards/ 或供應商專屬對等項目中的 .bazel.bzl 檔案。

在看板設定中,將 block_devices 清單新增至 filesystems 字典。這個清單中的每個項目都是字典,用於定義新的具名裝置。

  • device:您將用來在元件資訊清單中參照這個分割區的語意標籤。
  • from:指定磁碟上應找到分割區的位置。
    • label:要轉送的分區 GPT 標籤。
    • parent:父項裝置,通常是「gpt」。

範例:在開發板的 BUILD.bazel 檔案中:

# In a board definition
fuchsia_board_configuration(
    name = "my_board",
    ...
    filesystems = {
        "block_devices": [
            {
                "device": "my_partition",
                "from": {
                    "label": "my-partition-label",
                    "parent": "gpt",
                },
            },
            {
                "device": "my_other_partition",
                "from": {
                    "label": "my-other-partition-label",
                    "parent": "gpt",
                },
            },
        ],
        ...
    },
    ...
)

這項設定會提供給 fshost,後者會比對標籤為「my-partition-label」的 GPT 分區,並在「/block/my_partition」提供這些分區 (my-other-partition-label 也是如此)。

2. 將 Block 裝置路徑傳送至元件

設定好主機板後,您必須將區塊裝置的能力從 fshost 路由至元件。具體做法是在拓撲中的父項元件元件資訊清單 ( .cml 檔案) 中新增 offer 節。

這項能力是 fshost/block 路徑下提供的 directory。如要選取特定分割區,請在 offer 節中使用 subdir 參數,該參數必須與您在主機板設定中定義的 device 名稱相符。

在本範例中,我們會將分割區路由至 core 領域中的元件,但其他領域的步驟也大同小異。修改元件的核心分片,將分割區路徑傳送至該分片:

// my_component.core_shard.cml
{
    offer: [
        {
            directory: "block",
            from: "parent",
            to: "#my_component",
            subdir: "my_partition",
            as: "my_partition",
        },
        {
            directory: "block",
            from: "parent",
            to: "#my_component",
            subdir: "my_other_partition",
            as: "my_other_partition",
        },
        ...
    ],
    ...
}

3. 在元件中使用 Block Device

最後一個步驟是讓元件 use 成為已路由目錄能力。

在元件的資訊清單 ( my_component.cml ) 中,為目錄新增 use 節。

示例my_component.cml

{
    use: [
        {
            directory: "my_partition",
            rights: [ "r*" ],
            path: "/block/my_partition",
        },
        {
            directory: "my_other_partition",
            rights: [ "r*" ],
            path: "/block/my_other_partition",
        },
        ...
    ],
}

現在,您的元件可以在 /block 存取命名空間中的分割區。如要連線至特定分割區的 fuchsia.hardware.block.volume.Volume 通訊協定,請透過下列路徑連線:

use fidl_fuchsia_hardware_block_volume::VolumeMarker;
use block_client::RemoteBlockClient;

async fn connect_to_my_partition() -> Result<(), anyhow::Error> {
    let proxy = fuchsia_component::client::connect_to_protocol_at::<VolumeMarker>(
        "/block/my_partition/fuchsia.hardware.block.volume.Volume"
    )?;
    let block_client = RemoteBlockClient::new(proxy).await?;
    // ... use the block client
    Ok(())
}