将存储分区路由到组件

虽然大多数组件都通过文件系统使用其存储功能与存储空间进行交互,但某些组件可能需要能够直接读取和写入分区。本指南介绍了如何将分区路由到组件。

简要步骤如下:

  1. 在主板配置中定义语义标签:定义物理分区标签到“语义”标签的映射,该标签将是功能路由中使用的分区名称。
  2. 更新组件清单:将块设备的目录功能路由到组件。
  3. 在组件中访问块设备:组件使用路由的目录功能来访问块设备。

步骤

1. 在主板配置中定义语义标签

第一步是在主板的配置中为物理分区声明一个语义标签,该配置通常是位于 //boards/ 或供应商特定等效位置的 .bazel.bzl 文件。

在板级的配置中,向 filesystems 字典添加 block_devices 列表。此列表中的每个条目都是一个字典,用于定义新的命名设备。

  • 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. 将块设备路由到您的组件

配置好主板后,您必须将块设备的 capability 从 fshost 路由到您的组件。为此,请向拓扑中父组件的组件清单(.cml 文件)添加 offer 节。

该功能是 fshost 在路径 /block 下提供的 directory。 通过 offer stanza 中的 subdir 参数选择特定分区,该参数必须与您在主板配置中定义的 device 名称一致。

在此示例中,我们将分区路由到 core realm 中的组件,但对于其他 realm,步骤类似。修改组件的核心分片,以将分区路由到该分片:

// 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. 在组件中使用块设备

最后一步是让组件 use 路由目录功能。

在组件的清单 ( my_component.cml ) 中,为目录添加 use stanza。

示例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(())
}