欢迎光临
我们一直在努力

【NPU】Ascend Docker Runtime v26.0.1 系统级架构分析

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */
#content_views .toc,
/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */
#content_views.markdown_views > p:empty:has(+ .toc),
#content_views.markdown_views > .toc + p:empty,
/* 富文本旧版目录标记 */
#content_views.htmledit_views #main-toc,
#content_views.htmledit_views #hr-toc,
#content_views.htmledit_views p[id*=\”-toc\”] {
display: none !important;
}
/* 目录去掉后,紧跟的首个标题不再多出一块上边距 */
#content_views.markdown_views > .toc + h1,
#content_views.markdown_views > .toc + h2,
#content_views.markdown_views > .toc + h3,
#content_views.markdown_views > .toc + h4,
#content_views.markdown_views > .toc + p:empty + h1,
#content_views.markdown_views > .toc + p:empty + h2,
#content_views.markdown_views > .toc + p:empty + h3,
#content_views.markdown_views > .toc + p:empty + h4 {
margin-top: 0 !important;
}

Ascend Docker Runtime v26.0.1 系统级架构分析

基于源码的深度架构剖析 分析日期:2026-07-26 代码规模:13个Go源文件 + C/Shell辅助组件,约8,436行核心Go代码 项目路径:component/ascend-docker-runtime


目录

  • 第一章:项目整体架构
  • 第二章:子模块划分
  • 第三章:模块调用关系
  • 第四章:架构设计总结

第一章:项目整体架构

1.1 项目概述

Ascend Docker Runtime 是华为昇腾(Ascend) NPU 的容器化运行时插件,基于 OCI 标准实现,为 AI 训练/推理作业提供 NPU 容器化支持。它不修改 Docker 引擎,而是以插件方式在 runc 启动容器过程中注入 NPU 设备。

功能说明
NPU 设备挂载 根据 ASCEND_VISIBLE_DEVICES 将对应 NPU 设备挂载到容器 namespace
设备 cgroup 配置 在 Host 上配置 device cgroup,确保容器只使用指定 NPU
CANN 运行时库挂载 将 Host 上的 CANN Runtime Library 挂载到容器
vNPU 虚拟化 支持虚拟设备(vdavinci)切分与创建
Prestart Hook 在容器 created→running 过渡期执行设备注入
安装/卸载 自动修改 Docker/containerd 配置文件

1.2 整体设计思路

#mermaid-svg-hoeEoSp6UNhOahQ9{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-hoeEoSp6UNhOahQ9 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-hoeEoSp6UNhOahQ9 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-hoeEoSp6UNhOahQ9 .error-icon{fill:#552222;}#mermaid-svg-hoeEoSp6UNhOahQ9 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-hoeEoSp6UNhOahQ9 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-hoeEoSp6UNhOahQ9 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-hoeEoSp6UNhOahQ9 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-hoeEoSp6UNhOahQ9 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-hoeEoSp6UNhOahQ9 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-hoeEoSp6UNhOahQ9 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-hoeEoSp6UNhOahQ9 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-hoeEoSp6UNhOahQ9 .marker.cross{stroke:#333333;}#mermaid-svg-hoeEoSp6UNhOahQ9 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-hoeEoSp6UNhOahQ9 p{margin:0;}#mermaid-svg-hoeEoSp6UNhOahQ9 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-hoeEoSp6UNhOahQ9 .cluster-label text{fill:#333;}#mermaid-svg-hoeEoSp6UNhOahQ9 .cluster-label span{color:#333;}#mermaid-svg-hoeEoSp6UNhOahQ9 .cluster-label span p{background-color:transparent;}#mermaid-svg-hoeEoSp6UNhOahQ9 .label text,#mermaid-svg-hoeEoSp6UNhOahQ9 span{fill:#333;color:#333;}#mermaid-svg-hoeEoSp6UNhOahQ9 .node rect,#mermaid-svg-hoeEoSp6UNhOahQ9 .node circle,#mermaid-svg-hoeEoSp6UNhOahQ9 .node ellipse,#mermaid-svg-hoeEoSp6UNhOahQ9 .node polygon,#mermaid-svg-hoeEoSp6UNhOahQ9 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-hoeEoSp6UNhOahQ9 .rough-node .label text,#mermaid-svg-hoeEoSp6UNhOahQ9 .node .label text,#mermaid-svg-hoeEoSp6UNhOahQ9 .image-shape .label,#mermaid-svg-hoeEoSp6UNhOahQ9 .icon-shape .label{text-anchor:middle;}#mermaid-svg-hoeEoSp6UNhOahQ9 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-hoeEoSp6UNhOahQ9 .rough-node .label,#mermaid-svg-hoeEoSp6UNhOahQ9 .node .label,#mermaid-svg-hoeEoSp6UNhOahQ9 .image-shape .label,#mermaid-svg-hoeEoSp6UNhOahQ9 .icon-shape .label{text-align:center;}#mermaid-svg-hoeEoSp6UNhOahQ9 .node.clickable{cursor:pointer;}#mermaid-svg-hoeEoSp6UNhOahQ9 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-hoeEoSp6UNhOahQ9 .arrowheadPath{fill:#333333;}#mermaid-svg-hoeEoSp6UNhOahQ9 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-hoeEoSp6UNhOahQ9 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-hoeEoSp6UNhOahQ9 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-hoeEoSp6UNhOahQ9 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-hoeEoSp6UNhOahQ9 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-hoeEoSp6UNhOahQ9 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-hoeEoSp6UNhOahQ9 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-hoeEoSp6UNhOahQ9 .cluster text{fill:#333;}#mermaid-svg-hoeEoSp6UNhOahQ9 .cluster span{color:#333;}#mermaid-svg-hoeEoSp6UNhOahQ9 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-hoeEoSp6UNhOahQ9 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-hoeEoSp6UNhOahQ9 rect.text{fill:none;stroke-width:0;}#mermaid-svg-hoeEoSp6UNhOahQ9 .icon-shape,#mermaid-svg-hoeEoSp6UNhOahQ9 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-hoeEoSp6UNhOahQ9 .icon-shape p,#mermaid-svg-hoeEoSp6UNhOahQ9 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-hoeEoSp6UNhOahQ9 .icon-shape .label rect,#mermaid-svg-hoeEoSp6UNhOahQ9 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-hoeEoSp6UNhOahQ9 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-hoeEoSp6UNhOahQ9 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-hoeEoSp6UNhOahQ9 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

多后端抽象

DcDriverV1

Ascend 910/310 旧版

DcDriverV2

Ascend 910B/A5 新版

Hook 机制

Prestart Hook

容器 namespace 已创建

但作业未启动

挂载设备 + 配置 cgroup

挂载 CANN 库

核心设计

OCI 标准兼容

不修改 Docker 引擎

作为 Docker Runtime 插件

在 runc 前拦截 config.json

修改 OCI Spec 注入 NPU 设备

执行 runc 完成容器创建

1.3 整体运行流程

#mermaid-svg-8QutlRcEHmwwdv7p{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-8QutlRcEHmwwdv7p .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-8QutlRcEHmwwdv7p .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-8QutlRcEHmwwdv7p .error-icon{fill:#552222;}#mermaid-svg-8QutlRcEHmwwdv7p .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-8QutlRcEHmwwdv7p .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-8QutlRcEHmwwdv7p .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-8QutlRcEHmwwdv7p .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-8QutlRcEHmwwdv7p .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-8QutlRcEHmwwdv7p .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-8QutlRcEHmwwdv7p .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-8QutlRcEHmwwdv7p .marker{fill:#333333;stroke:#333333;}#mermaid-svg-8QutlRcEHmwwdv7p .marker.cross{stroke:#333333;}#mermaid-svg-8QutlRcEHmwwdv7p svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-8QutlRcEHmwwdv7p p{margin:0;}#mermaid-svg-8QutlRcEHmwwdv7p .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-8QutlRcEHmwwdv7p .cluster-label text{fill:#333;}#mermaid-svg-8QutlRcEHmwwdv7p .cluster-label span{color:#333;}#mermaid-svg-8QutlRcEHmwwdv7p .cluster-label span p{background-color:transparent;}#mermaid-svg-8QutlRcEHmwwdv7p .label text,#mermaid-svg-8QutlRcEHmwwdv7p span{fill:#333;color:#333;}#mermaid-svg-8QutlRcEHmwwdv7p .node rect,#mermaid-svg-8QutlRcEHmwwdv7p .node circle,#mermaid-svg-8QutlRcEHmwwdv7p .node ellipse,#mermaid-svg-8QutlRcEHmwwdv7p .node polygon,#mermaid-svg-8QutlRcEHmwwdv7p .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-8QutlRcEHmwwdv7p .rough-node .label text,#mermaid-svg-8QutlRcEHmwwdv7p .node .label text,#mermaid-svg-8QutlRcEHmwwdv7p .image-shape .label,#mermaid-svg-8QutlRcEHmwwdv7p .icon-shape .label{text-anchor:middle;}#mermaid-svg-8QutlRcEHmwwdv7p .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-8QutlRcEHmwwdv7p .rough-node .label,#mermaid-svg-8QutlRcEHmwwdv7p .node .label,#mermaid-svg-8QutlRcEHmwwdv7p .image-shape .label,#mermaid-svg-8QutlRcEHmwwdv7p .icon-shape .label{text-align:center;}#mermaid-svg-8QutlRcEHmwwdv7p .node.clickable{cursor:pointer;}#mermaid-svg-8QutlRcEHmwwdv7p .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-8QutlRcEHmwwdv7p .arrowheadPath{fill:#333333;}#mermaid-svg-8QutlRcEHmwwdv7p .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-8QutlRcEHmwwdv7p .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-8QutlRcEHmwwdv7p .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-8QutlRcEHmwwdv7p .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-8QutlRcEHmwwdv7p .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-8QutlRcEHmwwdv7p .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-8QutlRcEHmwwdv7p .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-8QutlRcEHmwwdv7p .cluster text{fill:#333;}#mermaid-svg-8QutlRcEHmwwdv7p .cluster span{color:#333;}#mermaid-svg-8QutlRcEHmwwdv7p div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-8QutlRcEHmwwdv7p .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-8QutlRcEHmwwdv7p rect.text{fill:none;stroke-width:0;}#mermaid-svg-8QutlRcEHmwwdv7p .icon-shape,#mermaid-svg-8QutlRcEHmwwdv7p .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-8QutlRcEHmwwdv7p .icon-shape p,#mermaid-svg-8QutlRcEHmwwdv7p .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-8QutlRcEHmwwdv7p .icon-shape .label rect,#mermaid-svg-8QutlRcEHmwwdv7p .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-8QutlRcEHmwwdv7p .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-8QutlRcEHmwwdv7p .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-8QutlRcEHmwwdv7p :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

否

是

否

是

Docker/Containerd 收到容器创建请求

调用 ascend-docker-runtime

runtime/main.go 启动

命令是 create?

直接 exec runc 透传

读取 config.json OCI Spec

解析 ASCEND_VISIBLE_DEVICES

需要 NPU 设备?

直接 exec runc

获取 NPU WorkerV1或V2驱动

注入 Prestart Hook 到 Spec

创建虚拟设备 vNPU如果 VIRTUAL 模式

挂载 davinci 设备到 Spec

挂载管理设备到 Spec

添加 device cgroup 规则

写入修改后的 config.json

exec runc 创建容器

runc 触发 Prestart Hook

hook/main.go 执行

解析挂载配置列表

调用 ascend-docker-cli 挂载文件/目录

容器启动,NPU 可用

1.4 系统整体架构图

#mermaid-svg-bI88XgY5gTckBj9I{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-bI88XgY5gTckBj9I .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-bI88XgY5gTckBj9I .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-bI88XgY5gTckBj9I .error-icon{fill:#552222;}#mermaid-svg-bI88XgY5gTckBj9I .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-bI88XgY5gTckBj9I .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-bI88XgY5gTckBj9I .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-bI88XgY5gTckBj9I .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-bI88XgY5gTckBj9I .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-bI88XgY5gTckBj9I .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-bI88XgY5gTckBj9I .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-bI88XgY5gTckBj9I .marker{fill:#333333;stroke:#333333;}#mermaid-svg-bI88XgY5gTckBj9I .marker.cross{stroke:#333333;}#mermaid-svg-bI88XgY5gTckBj9I svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-bI88XgY5gTckBj9I p{margin:0;}#mermaid-svg-bI88XgY5gTckBj9I .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-bI88XgY5gTckBj9I .cluster-label text{fill:#333;}#mermaid-svg-bI88XgY5gTckBj9I .cluster-label span{color:#333;}#mermaid-svg-bI88XgY5gTckBj9I .cluster-label span p{background-color:transparent;}#mermaid-svg-bI88XgY5gTckBj9I .label text,#mermaid-svg-bI88XgY5gTckBj9I span{fill:#333;color:#333;}#mermaid-svg-bI88XgY5gTckBj9I .node rect,#mermaid-svg-bI88XgY5gTckBj9I .node circle,#mermaid-svg-bI88XgY5gTckBj9I .node ellipse,#mermaid-svg-bI88XgY5gTckBj9I .node polygon,#mermaid-svg-bI88XgY5gTckBj9I .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-bI88XgY5gTckBj9I .rough-node .label text,#mermaid-svg-bI88XgY5gTckBj9I .node .label text,#mermaid-svg-bI88XgY5gTckBj9I .image-shape .label,#mermaid-svg-bI88XgY5gTckBj9I .icon-shape .label{text-anchor:middle;}#mermaid-svg-bI88XgY5gTckBj9I .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-bI88XgY5gTckBj9I .rough-node .label,#mermaid-svg-bI88XgY5gTckBj9I .node .label,#mermaid-svg-bI88XgY5gTckBj9I .image-shape .label,#mermaid-svg-bI88XgY5gTckBj9I .icon-shape .label{text-align:center;}#mermaid-svg-bI88XgY5gTckBj9I .node.clickable{cursor:pointer;}#mermaid-svg-bI88XgY5gTckBj9I .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-bI88XgY5gTckBj9I .arrowheadPath{fill:#333333;}#mermaid-svg-bI88XgY5gTckBj9I .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-bI88XgY5gTckBj9I .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-bI88XgY5gTckBj9I .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-bI88XgY5gTckBj9I .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-bI88XgY5gTckBj9I .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-bI88XgY5gTckBj9I .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-bI88XgY5gTckBj9I .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-bI88XgY5gTckBj9I .cluster text{fill:#333;}#mermaid-svg-bI88XgY5gTckBj9I .cluster span{color:#333;}#mermaid-svg-bI88XgY5gTckBj9I div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-bI88XgY5gTckBj9I .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-bI88XgY5gTckBj9I rect.text{fill:none;stroke-width:0;}#mermaid-svg-bI88XgY5gTckBj9I .icon-shape,#mermaid-svg-bI88XgY5gTckBj9I .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-bI88XgY5gTckBj9I .icon-shape p,#mermaid-svg-bI88XgY5gTckBj9I .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-bI88XgY5gTckBj9I .icon-shape .label rect,#mermaid-svg-bI88XgY5gTckBj9I .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-bI88XgY5gTckBj9I .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-bI88XgY5gTckBj9I .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-bI88XgY5gTckBj9I :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

底层

安全工具

install

ascend-docker-cli (C语言)

ascend-docker-hook

ascend-docker-runtime

容器运行时

create

修改Spec+注入Hook

Prestart Hook

exec

挂载设备

修改

Docker/Containerd

runc

runtime/main.go运行时入口

runtime/processSpec修改+设备注入

runtime/dcmiNPU设备管理

hook/main.goHook入口

hook/process挂载配置解析

cli/src/main.c实际挂载执行

install/main.go安装/卸载

install/process配置修改

mindxcheckutils路径/文件校验

libdcmi.so设备管理接口

NPU 硬件davinci0-N

1.5 技术栈概览

依赖用途
github.com/opencontainers/runtime-spec OCI 运行时规范
github.com/containerd/containerd containerd 配置解析
github.com/pelletier/go-toml TOML 配置文件解析
ascend-common 华为昇腾公共库(api/工具)
libdcmi.so (CGO) NPU 设备管理 C 库
hwlog 华为日志库

第二章:子模块划分

2.1 运行时入口 (runtime/)

核心作用:作为 Docker Runtime 插件入口,拦截容器创建请求,修改 OCI Spec 注入 NPU 设备,然后透传给 runc。

#mermaid-svg-4TbFJ79K2zdBLJMn{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-4TbFJ79K2zdBLJMn .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-4TbFJ79K2zdBLJMn .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-4TbFJ79K2zdBLJMn .error-icon{fill:#552222;}#mermaid-svg-4TbFJ79K2zdBLJMn .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-4TbFJ79K2zdBLJMn .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-4TbFJ79K2zdBLJMn .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-4TbFJ79K2zdBLJMn .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-4TbFJ79K2zdBLJMn .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-4TbFJ79K2zdBLJMn .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-4TbFJ79K2zdBLJMn .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-4TbFJ79K2zdBLJMn .marker{fill:#333333;stroke:#333333;}#mermaid-svg-4TbFJ79K2zdBLJMn .marker.cross{stroke:#333333;}#mermaid-svg-4TbFJ79K2zdBLJMn svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-4TbFJ79K2zdBLJMn p{margin:0;}#mermaid-svg-4TbFJ79K2zdBLJMn g.classGroup text{fill:#9370DB;stroke:none;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-4TbFJ79K2zdBLJMn g.classGroup text .title{font-weight:bolder;}#mermaid-svg-4TbFJ79K2zdBLJMn .cluster-label text{fill:#333;}#mermaid-svg-4TbFJ79K2zdBLJMn .cluster-label span{color:#333;}#mermaid-svg-4TbFJ79K2zdBLJMn .cluster-label span p{background-color:transparent;}#mermaid-svg-4TbFJ79K2zdBLJMn .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-4TbFJ79K2zdBLJMn .cluster text{fill:#333;}#mermaid-svg-4TbFJ79K2zdBLJMn .cluster span{color:#333;}#mermaid-svg-4TbFJ79K2zdBLJMn .nodeLabel,#mermaid-svg-4TbFJ79K2zdBLJMn .edgeLabel{color:#131300;}#mermaid-svg-4TbFJ79K2zdBLJMn .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-4TbFJ79K2zdBLJMn .label text{fill:#131300;}#mermaid-svg-4TbFJ79K2zdBLJMn .labelBkg{background:#ECECFF;}#mermaid-svg-4TbFJ79K2zdBLJMn .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-4TbFJ79K2zdBLJMn .classTitle{font-weight:bolder;}#mermaid-svg-4TbFJ79K2zdBLJMn .node rect,#mermaid-svg-4TbFJ79K2zdBLJMn .node circle,#mermaid-svg-4TbFJ79K2zdBLJMn .node ellipse,#mermaid-svg-4TbFJ79K2zdBLJMn .node polygon,#mermaid-svg-4TbFJ79K2zdBLJMn .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-4TbFJ79K2zdBLJMn .divider{stroke:#9370DB;stroke-width:1;}#mermaid-svg-4TbFJ79K2zdBLJMn g.clickable{cursor:pointer;}#mermaid-svg-4TbFJ79K2zdBLJMn g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-4TbFJ79K2zdBLJMn g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-4TbFJ79K2zdBLJMn .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-4TbFJ79K2zdBLJMn .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-4TbFJ79K2zdBLJMn .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-4TbFJ79K2zdBLJMn .dashed-line{stroke-dasharray:3;}#mermaid-svg-4TbFJ79K2zdBLJMn .dotted-line{stroke-dasharray:1 2;}#mermaid-svg-4TbFJ79K2zdBLJMn #compositionStart,#mermaid-svg-4TbFJ79K2zdBLJMn .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-4TbFJ79K2zdBLJMn #compositionEnd,#mermaid-svg-4TbFJ79K2zdBLJMn .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-4TbFJ79K2zdBLJMn #dependencyStart,#mermaid-svg-4TbFJ79K2zdBLJMn .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-4TbFJ79K2zdBLJMn #dependencyStart,#mermaid-svg-4TbFJ79K2zdBLJMn .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-4TbFJ79K2zdBLJMn #extensionStart,#mermaid-svg-4TbFJ79K2zdBLJMn .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-4TbFJ79K2zdBLJMn #extensionEnd,#mermaid-svg-4TbFJ79K2zdBLJMn .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-4TbFJ79K2zdBLJMn #aggregationStart,#mermaid-svg-4TbFJ79K2zdBLJMn .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-4TbFJ79K2zdBLJMn #aggregationEnd,#mermaid-svg-4TbFJ79K2zdBLJMn .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-4TbFJ79K2zdBLJMn #lollipopStart,#mermaid-svg-4TbFJ79K2zdBLJMn .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-4TbFJ79K2zdBLJMn #lollipopEnd,#mermaid-svg-4TbFJ79K2zdBLJMn .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-4TbFJ79K2zdBLJMn .edgeTerminals{font-size:11px;line-height:initial;}#mermaid-svg-4TbFJ79K2zdBLJMn .classTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-4TbFJ79K2zdBLJMn .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-4TbFJ79K2zdBLJMn .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-4TbFJ79K2zdBLJMn :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

args

+string bundleDirPath

+string cmd

process

+DoProcess() : error

-getArgs()(*args, error)

-modifySpecFile(path) : error

-execRunc() : error

-addDevice(w, spec, devices) : error

-addHook(w, spec, devices) : error

-addManagerDevice(w, spec) : error

-checkVisibleDevice(spec)([]int, error)

-addAscendDockerEnv(spec)

main

+main()

#mermaid-svg-rptMYT5rUj6vk4KJ{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-rptMYT5rUj6vk4KJ .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-rptMYT5rUj6vk4KJ .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-rptMYT5rUj6vk4KJ .error-icon{fill:#552222;}#mermaid-svg-rptMYT5rUj6vk4KJ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-rptMYT5rUj6vk4KJ .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-rptMYT5rUj6vk4KJ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-rptMYT5rUj6vk4KJ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-rptMYT5rUj6vk4KJ .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-rptMYT5rUj6vk4KJ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-rptMYT5rUj6vk4KJ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-rptMYT5rUj6vk4KJ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-rptMYT5rUj6vk4KJ .marker.cross{stroke:#333333;}#mermaid-svg-rptMYT5rUj6vk4KJ svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-rptMYT5rUj6vk4KJ p{margin:0;}#mermaid-svg-rptMYT5rUj6vk4KJ .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-rptMYT5rUj6vk4KJ .cluster-label text{fill:#333;}#mermaid-svg-rptMYT5rUj6vk4KJ .cluster-label span{color:#333;}#mermaid-svg-rptMYT5rUj6vk4KJ .cluster-label span p{background-color:transparent;}#mermaid-svg-rptMYT5rUj6vk4KJ .label text,#mermaid-svg-rptMYT5rUj6vk4KJ span{fill:#333;color:#333;}#mermaid-svg-rptMYT5rUj6vk4KJ .node rect,#mermaid-svg-rptMYT5rUj6vk4KJ .node circle,#mermaid-svg-rptMYT5rUj6vk4KJ .node ellipse,#mermaid-svg-rptMYT5rUj6vk4KJ .node polygon,#mermaid-svg-rptMYT5rUj6vk4KJ .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-rptMYT5rUj6vk4KJ .rough-node .label text,#mermaid-svg-rptMYT5rUj6vk4KJ .node .label text,#mermaid-svg-rptMYT5rUj6vk4KJ .image-shape .label,#mermaid-svg-rptMYT5rUj6vk4KJ .icon-shape .label{text-anchor:middle;}#mermaid-svg-rptMYT5rUj6vk4KJ .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-rptMYT5rUj6vk4KJ .rough-node .label,#mermaid-svg-rptMYT5rUj6vk4KJ .node .label,#mermaid-svg-rptMYT5rUj6vk4KJ .image-shape .label,#mermaid-svg-rptMYT5rUj6vk4KJ .icon-shape .label{text-align:center;}#mermaid-svg-rptMYT5rUj6vk4KJ .node.clickable{cursor:pointer;}#mermaid-svg-rptMYT5rUj6vk4KJ .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-rptMYT5rUj6vk4KJ .arrowheadPath{fill:#333333;}#mermaid-svg-rptMYT5rUj6vk4KJ .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-rptMYT5rUj6vk4KJ .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-rptMYT5rUj6vk4KJ .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-rptMYT5rUj6vk4KJ .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-rptMYT5rUj6vk4KJ .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-rptMYT5rUj6vk4KJ .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-rptMYT5rUj6vk4KJ .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-rptMYT5rUj6vk4KJ .cluster text{fill:#333;}#mermaid-svg-rptMYT5rUj6vk4KJ .cluster span{color:#333;}#mermaid-svg-rptMYT5rUj6vk4KJ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-rptMYT5rUj6vk4KJ .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-rptMYT5rUj6vk4KJ rect.text{fill:none;stroke-width:0;}#mermaid-svg-rptMYT5rUj6vk4KJ .icon-shape,#mermaid-svg-rptMYT5rUj6vk4KJ .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-rptMYT5rUj6vk4KJ .icon-shape p,#mermaid-svg-rptMYT5rUj6vk4KJ .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-rptMYT5rUj6vk4KJ .icon-shape .label rect,#mermaid-svg-rptMYT5rUj6vk4KJ .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-rptMYT5rUj6vk4KJ .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-rptMYT5rUj6vk4KJ .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-rptMYT5rUj6vk4KJ :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

否

是

是

否

DoProcess

cmd == create?

execRunc 透传

获取 bundle 路径

读取 config.json

checkVisibleDevice解析 ASCEND_VISIBLE_DEVICES

设备列表为空?

addAscendDockerEnv直接 exec runc

GetMatchingNpuWorker选择V1/V2驱动

addHook注入 Prestart + Poststop Hook

addDevice挂载 davinci 设备

addManagerDevice挂载管理设备

addUBDevice挂载UB设备

写入 config.json

execRunc

关键文件核心函数/方法作用
runtime/main.go main() 入口,初始化日志,调用 DoProcess
runtime/process/process.go DoProcess() 主流程:拦截create命令,修改Spec,exec runc
runtime/process/process.go modifySpecFile() 读取→修改→写回 config.json
runtime/process/process.go checkVisibleDevice() 解析 ASCEND_VISIBLE_DEVICES 环境变量
runtime/process/process.go addDevice() 挂载 davinci/vdavinci 设备到 Spec
runtime/process/process.go addManagerDevice() 根据芯片类型挂载管理设备
runtime/process/process.go addHook() 注入 Prestart Hook 和 Poststop Hook
runtime/process/process.go GetDeviceTypeByChipName() 根据芯片名获取设备类型
runtime/process/process.go parseDevices() 解析设备ID列表(支持范围"0-3"和单个"0,1,2")
runtime/process/process.go execRunc() 查找并执行 runc/docker-runc

2.2 DCMI 设备管理模块 (runtime/dcmi/)

核心作用:封装 NPU 设备管理接口,通过 CGO 调用 libdcmi.so,提供设备查询、虚拟设备创建/销毁能力。

#mermaid-svg-KHNon99rBqSd4Jk2{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-KHNon99rBqSd4Jk2 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-KHNon99rBqSd4Jk2 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-KHNon99rBqSd4Jk2 .error-icon{fill:#552222;}#mermaid-svg-KHNon99rBqSd4Jk2 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-KHNon99rBqSd4Jk2 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-KHNon99rBqSd4Jk2 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-KHNon99rBqSd4Jk2 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-KHNon99rBqSd4Jk2 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-KHNon99rBqSd4Jk2 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-KHNon99rBqSd4Jk2 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-KHNon99rBqSd4Jk2 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-KHNon99rBqSd4Jk2 .marker.cross{stroke:#333333;}#mermaid-svg-KHNon99rBqSd4Jk2 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-KHNon99rBqSd4Jk2 p{margin:0;}#mermaid-svg-KHNon99rBqSd4Jk2 g.classGroup text{fill:#9370DB;stroke:none;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-KHNon99rBqSd4Jk2 g.classGroup text .title{font-weight:bolder;}#mermaid-svg-KHNon99rBqSd4Jk2 .cluster-label text{fill:#333;}#mermaid-svg-KHNon99rBqSd4Jk2 .cluster-label span{color:#333;}#mermaid-svg-KHNon99rBqSd4Jk2 .cluster-label span p{background-color:transparent;}#mermaid-svg-KHNon99rBqSd4Jk2 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-KHNon99rBqSd4Jk2 .cluster text{fill:#333;}#mermaid-svg-KHNon99rBqSd4Jk2 .cluster span{color:#333;}#mermaid-svg-KHNon99rBqSd4Jk2 .nodeLabel,#mermaid-svg-KHNon99rBqSd4Jk2 .edgeLabel{color:#131300;}#mermaid-svg-KHNon99rBqSd4Jk2 .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-KHNon99rBqSd4Jk2 .label text{fill:#131300;}#mermaid-svg-KHNon99rBqSd4Jk2 .labelBkg{background:#ECECFF;}#mermaid-svg-KHNon99rBqSd4Jk2 .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-KHNon99rBqSd4Jk2 .classTitle{font-weight:bolder;}#mermaid-svg-KHNon99rBqSd4Jk2 .node rect,#mermaid-svg-KHNon99rBqSd4Jk2 .node circle,#mermaid-svg-KHNon99rBqSd4Jk2 .node ellipse,#mermaid-svg-KHNon99rBqSd4Jk2 .node polygon,#mermaid-svg-KHNon99rBqSd4Jk2 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-KHNon99rBqSd4Jk2 .divider{stroke:#9370DB;stroke-width:1;}#mermaid-svg-KHNon99rBqSd4Jk2 g.clickable{cursor:pointer;}#mermaid-svg-KHNon99rBqSd4Jk2 g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-KHNon99rBqSd4Jk2 g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-KHNon99rBqSd4Jk2 .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-KHNon99rBqSd4Jk2 .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-KHNon99rBqSd4Jk2 .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-KHNon99rBqSd4Jk2 .dashed-line{stroke-dasharray:3;}#mermaid-svg-KHNon99rBqSd4Jk2 .dotted-line{stroke-dasharray:1 2;}#mermaid-svg-KHNon99rBqSd4Jk2 #compositionStart,#mermaid-svg-KHNon99rBqSd4Jk2 .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-KHNon99rBqSd4Jk2 #compositionEnd,#mermaid-svg-KHNon99rBqSd4Jk2 .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-KHNon99rBqSd4Jk2 #dependencyStart,#mermaid-svg-KHNon99rBqSd4Jk2 .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-KHNon99rBqSd4Jk2 #dependencyStart,#mermaid-svg-KHNon99rBqSd4Jk2 .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-KHNon99rBqSd4Jk2 #extensionStart,#mermaid-svg-KHNon99rBqSd4Jk2 .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-KHNon99rBqSd4Jk2 #extensionEnd,#mermaid-svg-KHNon99rBqSd4Jk2 .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-KHNon99rBqSd4Jk2 #aggregationStart,#mermaid-svg-KHNon99rBqSd4Jk2 .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-KHNon99rBqSd4Jk2 #aggregationEnd,#mermaid-svg-KHNon99rBqSd4Jk2 .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-KHNon99rBqSd4Jk2 #lollipopStart,#mermaid-svg-KHNon99rBqSd4Jk2 .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-KHNon99rBqSd4Jk2 #lollipopEnd,#mermaid-svg-KHNon99rBqSd4Jk2 .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-KHNon99rBqSd4Jk2 .edgeTerminals{font-size:11px;line-height:initial;}#mermaid-svg-KHNon99rBqSd4Jk2 .classTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-KHNon99rBqSd4Jk2 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-KHNon99rBqSd4Jk2 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-KHNon99rBqSd4Jk2 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

«interface»

WorkerInterface

+Initialize() : error

+ShutDown()

+CreateVDevice(uniqueID int32, coreNum string)(VDeviceInfo, error)

+DestroyVDevice(uniqueID int32, vDevID int32) : error

+GetProductType()(string, error)

+GetChipName()(string, error)

«interface»

DcDriverV1Interface

+DcInitialize() : error

+DcShutDown()

+DcGetCardList()(int32, []int32, error)

+DcGetDeviceNumInCard(cardID)(int32, error)

+DcGetDeviceLogicID(card, device)(int32, error)

+DcGetProductType(card, device)(string, error)

+DcCreateVDevice(card, device, coreNum)(int32, error)

+DcDestroyVDevice(card, device, vDevID) : error

+DcGetChipInfo(card, device)(*ChipInfo, error)

«interface»

DcDriverV2Interface

+DcInitialize() : error

+DcShutDown()

+DcGetDeviceList()(int32, []int32, error)

+DcCreateVDevice(deviceID, coreNum)(int32, error)

+DcDestroyVDevice(deviceID, vDevID) : error

+DcGetChipInfo(deviceID)(*ChipInfo, error)

NpuV1Worker

+driver DcDriverV1Interface

+cardID int32

+deviceID int32

NpuV2Worker

+driver DcDriverV2Interface

+deviceID int32

DcV1Manager

-nvmllib nvml.Interface

DcV2Manager

-nvmllib nvml.Interface

VDeviceInfo

+int32 VdeviceID

+int32 CardID

+int32 DeviceID

ChipInfo

+string ChipName

+string ChipType

#mermaid-svg-xzFn8mtK1BQQ0048{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-xzFn8mtK1BQQ0048 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-xzFn8mtK1BQQ0048 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-xzFn8mtK1BQQ0048 .error-icon{fill:#552222;}#mermaid-svg-xzFn8mtK1BQQ0048 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-xzFn8mtK1BQQ0048 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-xzFn8mtK1BQQ0048 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-xzFn8mtK1BQQ0048 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-xzFn8mtK1BQQ0048 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-xzFn8mtK1BQQ0048 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-xzFn8mtK1BQQ0048 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-xzFn8mtK1BQQ0048 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-xzFn8mtK1BQQ0048 .marker.cross{stroke:#333333;}#mermaid-svg-xzFn8mtK1BQQ0048 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-xzFn8mtK1BQQ0048 p{margin:0;}#mermaid-svg-xzFn8mtK1BQQ0048 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-xzFn8mtK1BQQ0048 .cluster-label text{fill:#333;}#mermaid-svg-xzFn8mtK1BQQ0048 .cluster-label span{color:#333;}#mermaid-svg-xzFn8mtK1BQQ0048 .cluster-label span p{background-color:transparent;}#mermaid-svg-xzFn8mtK1BQQ0048 .label text,#mermaid-svg-xzFn8mtK1BQQ0048 span{fill:#333;color:#333;}#mermaid-svg-xzFn8mtK1BQQ0048 .node rect,#mermaid-svg-xzFn8mtK1BQQ0048 .node circle,#mermaid-svg-xzFn8mtK1BQQ0048 .node ellipse,#mermaid-svg-xzFn8mtK1BQQ0048 .node polygon,#mermaid-svg-xzFn8mtK1BQQ0048 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-xzFn8mtK1BQQ0048 .rough-node .label text,#mermaid-svg-xzFn8mtK1BQQ0048 .node .label text,#mermaid-svg-xzFn8mtK1BQQ0048 .image-shape .label,#mermaid-svg-xzFn8mtK1BQQ0048 .icon-shape .label{text-anchor:middle;}#mermaid-svg-xzFn8mtK1BQQ0048 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-xzFn8mtK1BQQ0048 .rough-node .label,#mermaid-svg-xzFn8mtK1BQQ0048 .node .label,#mermaid-svg-xzFn8mtK1BQQ0048 .image-shape .label,#mermaid-svg-xzFn8mtK1BQQ0048 .icon-shape .label{text-align:center;}#mermaid-svg-xzFn8mtK1BQQ0048 .node.clickable{cursor:pointer;}#mermaid-svg-xzFn8mtK1BQQ0048 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-xzFn8mtK1BQQ0048 .arrowheadPath{fill:#333333;}#mermaid-svg-xzFn8mtK1BQQ0048 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-xzFn8mtK1BQQ0048 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-xzFn8mtK1BQQ0048 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-xzFn8mtK1BQQ0048 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-xzFn8mtK1BQQ0048 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-xzFn8mtK1BQQ0048 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-xzFn8mtK1BQQ0048 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-xzFn8mtK1BQQ0048 .cluster text{fill:#333;}#mermaid-svg-xzFn8mtK1BQQ0048 .cluster span{color:#333;}#mermaid-svg-xzFn8mtK1BQQ0048 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-xzFn8mtK1BQQ0048 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-xzFn8mtK1BQQ0048 rect.text{fill:none;stroke-width:0;}#mermaid-svg-xzFn8mtK1BQQ0048 .icon-shape,#mermaid-svg-xzFn8mtK1BQQ0048 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-xzFn8mtK1BQQ0048 .icon-shape p,#mermaid-svg-xzFn8mtK1BQQ0048 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-xzFn8mtK1BQQ0048 .icon-shape .label rect,#mermaid-svg-xzFn8mtK1BQQ0048 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-xzFn8mtK1BQQ0048 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-xzFn8mtK1BQQ0048 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-xzFn8mtK1BQQ0048 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

V1 旧版

V2 新版

GetMatchingNpuWorker

检测驱动版本

NpuV1Worker+DcV1Manager

NpuV2Worker+DcV2Manager

Initialize: DcInitialize

Initialize: DcInitialize

FindDevice遍历Card+Device

DcGetDeviceList直接获取设备列表

GetChipNameDcGetChipInfo

GetProductTypeDcGetProductType

CreateVDevice切分虚拟设备

返回 VDeviceInfo

关键文件核心类型/函数作用
dcmi.go WorkerInterface 接口 NPU Worker 统一接口
dcmi.go NpuV1Worker V1驱动实现(Ascend 910/310)
dcmi.go NpuV2Worker V2驱动实现(Ascend 910B/A5)
dcmi.go GetMatchingNpuWorker() 工厂函数,选择V1/V2
dcmi.go ChipInfo 芯片信息结构体
dcmi_api.go VDeviceInfo 虚拟设备信息
dcmi_api.go CreateVDevice() 创建虚拟设备顶层API
dcmi_api.go extractVpuParam() 从OCI Spec提取vNPU参数
dcManager.go DcV1Manager V1 CGO调用封装
dcManager.go DcV2Manager V2 CGO调用封装
dcManager.go DcGetCardList() 获取NPU卡列表
dcManager.go DcGetChipInfo() 获取芯片信息
dcManager.go DcCreateVDevice() CGO创建虚拟设备
dcManager.go DcDestroyVDevice() CGO销毁虚拟设备

2.3 Hook 模块 (hook/)

核心作用:OCI Prestart Hook 实现,在容器 namespace 已创建但作业未启动时,解析挂载配置并调用 CLI 执行实际挂载。

#mermaid-svg-vHe6DLGGvW0hBPff{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-vHe6DLGGvW0hBPff .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-vHe6DLGGvW0hBPff .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-vHe6DLGGvW0hBPff .error-icon{fill:#552222;}#mermaid-svg-vHe6DLGGvW0hBPff .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-vHe6DLGGvW0hBPff .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-vHe6DLGGvW0hBPff .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-vHe6DLGGvW0hBPff .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-vHe6DLGGvW0hBPff .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-vHe6DLGGvW0hBPff .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-vHe6DLGGvW0hBPff .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-vHe6DLGGvW0hBPff .marker{fill:#333333;stroke:#333333;}#mermaid-svg-vHe6DLGGvW0hBPff .marker.cross{stroke:#333333;}#mermaid-svg-vHe6DLGGvW0hBPff svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-vHe6DLGGvW0hBPff p{margin:0;}#mermaid-svg-vHe6DLGGvW0hBPff .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-vHe6DLGGvW0hBPff .cluster-label text{fill:#333;}#mermaid-svg-vHe6DLGGvW0hBPff .cluster-label span{color:#333;}#mermaid-svg-vHe6DLGGvW0hBPff .cluster-label span p{background-color:transparent;}#mermaid-svg-vHe6DLGGvW0hBPff .label text,#mermaid-svg-vHe6DLGGvW0hBPff span{fill:#333;color:#333;}#mermaid-svg-vHe6DLGGvW0hBPff .node rect,#mermaid-svg-vHe6DLGGvW0hBPff .node circle,#mermaid-svg-vHe6DLGGvW0hBPff .node ellipse,#mermaid-svg-vHe6DLGGvW0hBPff .node polygon,#mermaid-svg-vHe6DLGGvW0hBPff .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-vHe6DLGGvW0hBPff .rough-node .label text,#mermaid-svg-vHe6DLGGvW0hBPff .node .label text,#mermaid-svg-vHe6DLGGvW0hBPff .image-shape .label,#mermaid-svg-vHe6DLGGvW0hBPff .icon-shape .label{text-anchor:middle;}#mermaid-svg-vHe6DLGGvW0hBPff .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-vHe6DLGGvW0hBPff .rough-node .label,#mermaid-svg-vHe6DLGGvW0hBPff .node .label,#mermaid-svg-vHe6DLGGvW0hBPff .image-shape .label,#mermaid-svg-vHe6DLGGvW0hBPff .icon-shape .label{text-align:center;}#mermaid-svg-vHe6DLGGvW0hBPff .node.clickable{cursor:pointer;}#mermaid-svg-vHe6DLGGvW0hBPff .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-vHe6DLGGvW0hBPff .arrowheadPath{fill:#333333;}#mermaid-svg-vHe6DLGGvW0hBPff .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-vHe6DLGGvW0hBPff .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-vHe6DLGGvW0hBPff .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-vHe6DLGGvW0hBPff .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-vHe6DLGGvW0hBPff .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-vHe6DLGGvW0hBPff .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-vHe6DLGGvW0hBPff .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-vHe6DLGGvW0hBPff .cluster text{fill:#333;}#mermaid-svg-vHe6DLGGvW0hBPff .cluster span{color:#333;}#mermaid-svg-vHe6DLGGvW0hBPff div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-vHe6DLGGvW0hBPff .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-vHe6DLGGvW0hBPff rect.text{fill:none;stroke-width:0;}#mermaid-svg-vHe6DLGGvW0hBPff .icon-shape,#mermaid-svg-vHe6DLGGvW0hBPff .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-vHe6DLGGvW0hBPff .icon-shape p,#mermaid-svg-vHe6DLGGvW0hBPff .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-vHe6DLGGvW0hBPff .icon-shape .label rect,#mermaid-svg-vHe6DLGGvW0hBPff .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-vHe6DLGGvW0hBPff .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-vHe6DLGGvW0hBPff .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-vHe6DLGGvW0hBPff :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

是

否

hook/main.go 启动

InitLogModule 日志初始化

StringChecker 安全校验

DoPrestartHook

getContainerConfig从stdin读取OCI State

getValueByKey检查 ASCEND_VISIBLE_DEVICES

visibleDevices 为空?

直接返回 不处理

parseMounts解析 ASCEND_RUNTIME_MOUNTS

readConfigsOfDir读取 /etc/ascend-docker-runtime.d/

addUBMount添加HCCL+Topo

parseRuntimeOptions解析 NODRV/VIRTUAL

parseSoftLinkMode解析软链接模式

查找 ascend-docker-cli

组装 CLI 参数

syscall.Exec执行 CLI 挂载

关键文件核心函数/方法作用
hook/main.go main() Hook入口,安全校验+调用DoPrestartHook
hook/process/process.go DoPrestartHook() 主逻辑:解析配置→调用CLI挂载
hook/process/process.go getContainerConfig() 从stdin读取OCI State,解析config.json
hook/process/process.go parseMounts() 解析 ASCEND_RUNTIME_MOUNTS 环境变量
hook/process/process.go readConfigsOfDir() 读取配置目录下的 .list 文件
hook/process/process.go parseRuntimeOptions() 解析 NODRV/VIRTUAL 选项
hook/process/process.go addUBMount() 添加UB挂载(hccl_rootinfo + topo)

2.4 安装模块 (install/)

核心作用:安装/卸载 ascend-docker-runtime,自动修改 Docker daemon.json 或 containerd config.toml。

#mermaid-svg-Y3qQq8VGpymp2YW4{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-Y3qQq8VGpymp2YW4 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-Y3qQq8VGpymp2YW4 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-Y3qQq8VGpymp2YW4 .error-icon{fill:#552222;}#mermaid-svg-Y3qQq8VGpymp2YW4 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Y3qQq8VGpymp2YW4 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-Y3qQq8VGpymp2YW4 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Y3qQq8VGpymp2YW4 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Y3qQq8VGpymp2YW4 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-Y3qQq8VGpymp2YW4 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Y3qQq8VGpymp2YW4 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Y3qQq8VGpymp2YW4 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Y3qQq8VGpymp2YW4 .marker.cross{stroke:#333333;}#mermaid-svg-Y3qQq8VGpymp2YW4 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Y3qQq8VGpymp2YW4 p{margin:0;}#mermaid-svg-Y3qQq8VGpymp2YW4 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-Y3qQq8VGpymp2YW4 .cluster-label text{fill:#333;}#mermaid-svg-Y3qQq8VGpymp2YW4 .cluster-label span{color:#333;}#mermaid-svg-Y3qQq8VGpymp2YW4 .cluster-label span p{background-color:transparent;}#mermaid-svg-Y3qQq8VGpymp2YW4 .label text,#mermaid-svg-Y3qQq8VGpymp2YW4 span{fill:#333;color:#333;}#mermaid-svg-Y3qQq8VGpymp2YW4 .node rect,#mermaid-svg-Y3qQq8VGpymp2YW4 .node circle,#mermaid-svg-Y3qQq8VGpymp2YW4 .node ellipse,#mermaid-svg-Y3qQq8VGpymp2YW4 .node polygon,#mermaid-svg-Y3qQq8VGpymp2YW4 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-Y3qQq8VGpymp2YW4 .rough-node .label text,#mermaid-svg-Y3qQq8VGpymp2YW4 .node .label text,#mermaid-svg-Y3qQq8VGpymp2YW4 .image-shape .label,#mermaid-svg-Y3qQq8VGpymp2YW4 .icon-shape .label{text-anchor:middle;}#mermaid-svg-Y3qQq8VGpymp2YW4 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-Y3qQq8VGpymp2YW4 .rough-node .label,#mermaid-svg-Y3qQq8VGpymp2YW4 .node .label,#mermaid-svg-Y3qQq8VGpymp2YW4 .image-shape .label,#mermaid-svg-Y3qQq8VGpymp2YW4 .icon-shape .label{text-align:center;}#mermaid-svg-Y3qQq8VGpymp2YW4 .node.clickable{cursor:pointer;}#mermaid-svg-Y3qQq8VGpymp2YW4 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-Y3qQq8VGpymp2YW4 .arrowheadPath{fill:#333333;}#mermaid-svg-Y3qQq8VGpymp2YW4 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-Y3qQq8VGpymp2YW4 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-Y3qQq8VGpymp2YW4 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Y3qQq8VGpymp2YW4 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-Y3qQq8VGpymp2YW4 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Y3qQq8VGpymp2YW4 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-Y3qQq8VGpymp2YW4 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-Y3qQq8VGpymp2YW4 .cluster text{fill:#333;}#mermaid-svg-Y3qQq8VGpymp2YW4 .cluster span{color:#333;}#mermaid-svg-Y3qQq8VGpymp2YW4 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-Y3qQq8VGpymp2YW4 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-Y3qQq8VGpymp2YW4 rect.text{fill:none;stroke-width:0;}#mermaid-svg-Y3qQq8VGpymp2YW4 .icon-shape,#mermaid-svg-Y3qQq8VGpymp2YW4 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Y3qQq8VGpymp2YW4 .icon-shape p,#mermaid-svg-Y3qQq8VGpymp2YW4 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-Y3qQq8VGpymp2YW4 .icon-shape .label rect,#mermaid-svg-Y3qQq8VGpymp2YW4 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Y3qQq8VGpymp2YW4 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-Y3qQq8VGpymp2YW4 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-Y3qQq8VGpymp2YW4 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

docker/isula

containerd

add

rm

cgroup v2

cgroup v1

add

rm

install/main.go

参数校验

场景判断

DockerProcess

ContainerdProcess

action

读取 daemon.json

读取 daemon.json

addDockerDaemon添加 ascend runtime

rmDockerDaemon删除 ascend runtime

设置 default-runtime

写入 daemon.json

cgroup 版本

changeCgroupV2BinaryNameConfig修改 BinaryName

action

changeCgroupV1RuntimeConfig修改 runtime 路径

恢复默认 runc

changeCgroupV1RuntimeTypeConfig修改 runtime_type

写入 config.toml

关键文件核心函数/方法作用
install/main.go main() 安装入口,参数校验,分发到Docker/Containerd处理
install/process/common.go CheckParamLength() 参数数量校验
install/process/constant.go 常量定义 命令长度、位置索引、场景名等
install/process/docker_process.go DockerProcess() 修改Docker daemon.json
install/process/docker_process.go addDockerDaemon() 添加ascend runtime配置
install/process/docker_process.go rmDockerDaemon() 移除ascend runtime配置
install/process/docker_process.go loadOriginJson() 读取原始daemon.json
install/process/containerd_process.go ContainerdProcess() 修改containerd config.toml
install/process/containerd_process.go editContainerdConfig() 编辑containerd配置
install/process/containerd_process.go changeCgroupV2BinaryNameConfig() cgroup v2配置
install/process/containerd_process.go changeCgroupV1Config() cgroup v1配置

2.5 安全检查模块 (mindxcheckutils/)

核心作用:提供路径安全校验、文件信息检查、字符串白名单过滤等安全工具函数。

#mermaid-svg-NWhvpMPEKQgemzVV{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-NWhvpMPEKQgemzVV .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-NWhvpMPEKQgemzVV .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-NWhvpMPEKQgemzVV .error-icon{fill:#552222;}#mermaid-svg-NWhvpMPEKQgemzVV .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-NWhvpMPEKQgemzVV .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-NWhvpMPEKQgemzVV .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-NWhvpMPEKQgemzVV .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-NWhvpMPEKQgemzVV .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-NWhvpMPEKQgemzVV .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-NWhvpMPEKQgemzVV .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-NWhvpMPEKQgemzVV .marker{fill:#333333;stroke:#333333;}#mermaid-svg-NWhvpMPEKQgemzVV .marker.cross{stroke:#333333;}#mermaid-svg-NWhvpMPEKQgemzVV svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-NWhvpMPEKQgemzVV p{margin:0;}#mermaid-svg-NWhvpMPEKQgemzVV g.classGroup text{fill:#9370DB;stroke:none;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-NWhvpMPEKQgemzVV g.classGroup text .title{font-weight:bolder;}#mermaid-svg-NWhvpMPEKQgemzVV .cluster-label text{fill:#333;}#mermaid-svg-NWhvpMPEKQgemzVV .cluster-label span{color:#333;}#mermaid-svg-NWhvpMPEKQgemzVV .cluster-label span p{background-color:transparent;}#mermaid-svg-NWhvpMPEKQgemzVV .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-NWhvpMPEKQgemzVV .cluster text{fill:#333;}#mermaid-svg-NWhvpMPEKQgemzVV .cluster span{color:#333;}#mermaid-svg-NWhvpMPEKQgemzVV .nodeLabel,#mermaid-svg-NWhvpMPEKQgemzVV .edgeLabel{color:#131300;}#mermaid-svg-NWhvpMPEKQgemzVV .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-NWhvpMPEKQgemzVV .label text{fill:#131300;}#mermaid-svg-NWhvpMPEKQgemzVV .labelBkg{background:#ECECFF;}#mermaid-svg-NWhvpMPEKQgemzVV .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-NWhvpMPEKQgemzVV .classTitle{font-weight:bolder;}#mermaid-svg-NWhvpMPEKQgemzVV .node rect,#mermaid-svg-NWhvpMPEKQgemzVV .node circle,#mermaid-svg-NWhvpMPEKQgemzVV .node ellipse,#mermaid-svg-NWhvpMPEKQgemzVV .node polygon,#mermaid-svg-NWhvpMPEKQgemzVV .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-NWhvpMPEKQgemzVV .divider{stroke:#9370DB;stroke-width:1;}#mermaid-svg-NWhvpMPEKQgemzVV g.clickable{cursor:pointer;}#mermaid-svg-NWhvpMPEKQgemzVV g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-NWhvpMPEKQgemzVV g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-NWhvpMPEKQgemzVV .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-NWhvpMPEKQgemzVV .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-NWhvpMPEKQgemzVV .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-NWhvpMPEKQgemzVV .dashed-line{stroke-dasharray:3;}#mermaid-svg-NWhvpMPEKQgemzVV .dotted-line{stroke-dasharray:1 2;}#mermaid-svg-NWhvpMPEKQgemzVV #compositionStart,#mermaid-svg-NWhvpMPEKQgemzVV .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-NWhvpMPEKQgemzVV #compositionEnd,#mermaid-svg-NWhvpMPEKQgemzVV .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-NWhvpMPEKQgemzVV #dependencyStart,#mermaid-svg-NWhvpMPEKQgemzVV .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-NWhvpMPEKQgemzVV #dependencyStart,#mermaid-svg-NWhvpMPEKQgemzVV .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-NWhvpMPEKQgemzVV #extensionStart,#mermaid-svg-NWhvpMPEKQgemzVV .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-NWhvpMPEKQgemzVV #extensionEnd,#mermaid-svg-NWhvpMPEKQgemzVV .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-NWhvpMPEKQgemzVV #aggregationStart,#mermaid-svg-NWhvpMPEKQgemzVV .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-NWhvpMPEKQgemzVV #aggregationEnd,#mermaid-svg-NWhvpMPEKQgemzVV .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-NWhvpMPEKQgemzVV #lollipopStart,#mermaid-svg-NWhvpMPEKQgemzVV .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-NWhvpMPEKQgemzVV #lollipopEnd,#mermaid-svg-NWhvpMPEKQgemzVV .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-NWhvpMPEKQgemzVV .edgeTerminals{font-size:11px;line-height:initial;}#mermaid-svg-NWhvpMPEKQgemzVV .classTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-NWhvpMPEKQgemzVV .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-NWhvpMPEKQgemzVV .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-NWhvpMPEKQgemzVV :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

mindxcheckutils

+CheckPath(path, allowLink) : error

+CheckFileInfo(file, size) : error

+RealFileChecker(path, checkParent, allowLink, size)(string, error)

+RealDirChecker(path, checkParent, allowLink)(string, error)

+FileChecker(path, allowDir, checkParent, allowLink, deep)(bool, error)

+StringChecker(text, minLen, maxLen, whiteList) : bool

+GetLogPrefix()(string, error)

+ChangeRuntimeLogMode(prefix) : error

关键文件核心函数作用
mindxcheckutils.go RealFileChecker() 文件路径安全校验(防软链接攻击)
mindxcheckutils.go RealDirChecker() 目录路径安全校验
mindxcheckutils.go FileChecker() 递归文件检查(深度限制)
mindxcheckutils.go StringChecker() 字符串白名单过滤(防注入)
mindxcheckutils.go CheckFileInfo() 文件大小检查
mindxcheckutils.go ChangeRuntimeLogMode() 日志文件权限修改

2.6 CLI 工具 (cli/) — C语言实现

核心作用:实际执行文件/目录挂载到容器 namespace 的 C 语言工具,被 Hook 调用。

#mermaid-svg-yUH8ve9riGhZqtTS{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-yUH8ve9riGhZqtTS .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-yUH8ve9riGhZqtTS .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-yUH8ve9riGhZqtTS .error-icon{fill:#552222;}#mermaid-svg-yUH8ve9riGhZqtTS .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-yUH8ve9riGhZqtTS .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-yUH8ve9riGhZqtTS .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-yUH8ve9riGhZqtTS .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-yUH8ve9riGhZqtTS .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-yUH8ve9riGhZqtTS .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-yUH8ve9riGhZqtTS .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-yUH8ve9riGhZqtTS .marker{fill:#333333;stroke:#333333;}#mermaid-svg-yUH8ve9riGhZqtTS .marker.cross{stroke:#333333;}#mermaid-svg-yUH8ve9riGhZqtTS svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-yUH8ve9riGhZqtTS p{margin:0;}#mermaid-svg-yUH8ve9riGhZqtTS .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-yUH8ve9riGhZqtTS .cluster-label text{fill:#333;}#mermaid-svg-yUH8ve9riGhZqtTS .cluster-label span{color:#333;}#mermaid-svg-yUH8ve9riGhZqtTS .cluster-label span p{background-color:transparent;}#mermaid-svg-yUH8ve9riGhZqtTS .label text,#mermaid-svg-yUH8ve9riGhZqtTS span{fill:#333;color:#333;}#mermaid-svg-yUH8ve9riGhZqtTS .node rect,#mermaid-svg-yUH8ve9riGhZqtTS .node circle,#mermaid-svg-yUH8ve9riGhZqtTS .node ellipse,#mermaid-svg-yUH8ve9riGhZqtTS .node polygon,#mermaid-svg-yUH8ve9riGhZqtTS .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-yUH8ve9riGhZqtTS .rough-node .label text,#mermaid-svg-yUH8ve9riGhZqtTS .node .label text,#mermaid-svg-yUH8ve9riGhZqtTS .image-shape .label,#mermaid-svg-yUH8ve9riGhZqtTS .icon-shape .label{text-anchor:middle;}#mermaid-svg-yUH8ve9riGhZqtTS .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-yUH8ve9riGhZqtTS .rough-node .label,#mermaid-svg-yUH8ve9riGhZqtTS .node .label,#mermaid-svg-yUH8ve9riGhZqtTS .image-shape .label,#mermaid-svg-yUH8ve9riGhZqtTS .icon-shape .label{text-align:center;}#mermaid-svg-yUH8ve9riGhZqtTS .node.clickable{cursor:pointer;}#mermaid-svg-yUH8ve9riGhZqtTS .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-yUH8ve9riGhZqtTS .arrowheadPath{fill:#333333;}#mermaid-svg-yUH8ve9riGhZqtTS .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-yUH8ve9riGhZqtTS .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-yUH8ve9riGhZqtTS .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-yUH8ve9riGhZqtTS .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-yUH8ve9riGhZqtTS .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-yUH8ve9riGhZqtTS .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-yUH8ve9riGhZqtTS .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-yUH8ve9riGhZqtTS .cluster text{fill:#333;}#mermaid-svg-yUH8ve9riGhZqtTS .cluster span{color:#333;}#mermaid-svg-yUH8ve9riGhZqtTS div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-yUH8ve9riGhZqtTS .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-yUH8ve9riGhZqtTS rect.text{fill:none;stroke-width:0;}#mermaid-svg-yUH8ve9riGhZqtTS .icon-shape,#mermaid-svg-yUH8ve9riGhZqtTS .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-yUH8ve9riGhZqtTS .icon-shape p,#mermaid-svg-yUH8ve9riGhZqtTS .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-yUH8ve9riGhZqtTS .icon-shape .label rect,#mermaid-svg-yUH8ve9riGhZqtTS .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-yUH8ve9riGhZqtTS .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-yUH8ve9riGhZqtTS .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-yUH8ve9riGhZqtTS :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

cli/src (C语言)

main.c入口

u_mount.c挂载逻辑

options.c参数解析

ns.cnamespace操作

cgrp.ccgroup配置

utils.c工具函数

logger.c日志

2.7 Destroy 工具 (destroy/) — C语言实现

核心作用:Poststop Hook 执行器,容器停止时销毁虚拟设备。

#mermaid-svg-eTBKhkJy5GPk0x0p{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-eTBKhkJy5GPk0x0p .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-eTBKhkJy5GPk0x0p .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-eTBKhkJy5GPk0x0p .error-icon{fill:#552222;}#mermaid-svg-eTBKhkJy5GPk0x0p .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-eTBKhkJy5GPk0x0p .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-eTBKhkJy5GPk0x0p .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-eTBKhkJy5GPk0x0p .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-eTBKhkJy5GPk0x0p .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-eTBKhkJy5GPk0x0p .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-eTBKhkJy5GPk0x0p .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-eTBKhkJy5GPk0x0p .marker{fill:#333333;stroke:#333333;}#mermaid-svg-eTBKhkJy5GPk0x0p .marker.cross{stroke:#333333;}#mermaid-svg-eTBKhkJy5GPk0x0p svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-eTBKhkJy5GPk0x0p p{margin:0;}#mermaid-svg-eTBKhkJy5GPk0x0p .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-eTBKhkJy5GPk0x0p .cluster-label text{fill:#333;}#mermaid-svg-eTBKhkJy5GPk0x0p .cluster-label span{color:#333;}#mermaid-svg-eTBKhkJy5GPk0x0p .cluster-label span p{background-color:transparent;}#mermaid-svg-eTBKhkJy5GPk0x0p .label text,#mermaid-svg-eTBKhkJy5GPk0x0p span{fill:#333;color:#333;}#mermaid-svg-eTBKhkJy5GPk0x0p .node rect,#mermaid-svg-eTBKhkJy5GPk0x0p .node circle,#mermaid-svg-eTBKhkJy5GPk0x0p .node ellipse,#mermaid-svg-eTBKhkJy5GPk0x0p .node polygon,#mermaid-svg-eTBKhkJy5GPk0x0p .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-eTBKhkJy5GPk0x0p .rough-node .label text,#mermaid-svg-eTBKhkJy5GPk0x0p .node .label text,#mermaid-svg-eTBKhkJy5GPk0x0p .image-shape .label,#mermaid-svg-eTBKhkJy5GPk0x0p .icon-shape .label{text-anchor:middle;}#mermaid-svg-eTBKhkJy5GPk0x0p .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-eTBKhkJy5GPk0x0p .rough-node .label,#mermaid-svg-eTBKhkJy5GPk0x0p .node .label,#mermaid-svg-eTBKhkJy5GPk0x0p .image-shape .label,#mermaid-svg-eTBKhkJy5GPk0x0p .icon-shape .label{text-align:center;}#mermaid-svg-eTBKhkJy5GPk0x0p .node.clickable{cursor:pointer;}#mermaid-svg-eTBKhkJy5GPk0x0p .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-eTBKhkJy5GPk0x0p .arrowheadPath{fill:#333333;}#mermaid-svg-eTBKhkJy5GPk0x0p .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-eTBKhkJy5GPk0x0p .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-eTBKhkJy5GPk0x0p .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-eTBKhkJy5GPk0x0p .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-eTBKhkJy5GPk0x0p .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-eTBKhkJy5GPk0x0p .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-eTBKhkJy5GPk0x0p .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-eTBKhkJy5GPk0x0p .cluster text{fill:#333;}#mermaid-svg-eTBKhkJy5GPk0x0p .cluster span{color:#333;}#mermaid-svg-eTBKhkJy5GPk0x0p div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-eTBKhkJy5GPk0x0p .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-eTBKhkJy5GPk0x0p rect.text{fill:none;stroke-width:0;}#mermaid-svg-eTBKhkJy5GPk0x0p .icon-shape,#mermaid-svg-eTBKhkJy5GPk0x0p .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-eTBKhkJy5GPk0x0p .icon-shape p,#mermaid-svg-eTBKhkJy5GPk0x0p .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-eTBKhkJy5GPk0x0p .icon-shape .label rect,#mermaid-svg-eTBKhkJy5GPk0x0p .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-eTBKhkJy5GPk0x0p .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-eTBKhkJy5GPk0x0p .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-eTBKhkJy5GPk0x0p :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

destroy/src/main.c

读取参数cardID, deviceID, vDevID

调用 dcmi 销毁vDevice

清理资源


第三章:模块调用关系

3.1 模块依赖关系图

#mermaid-svg-7NgyNOfhICeLyR1O{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-7NgyNOfhICeLyR1O .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-7NgyNOfhICeLyR1O .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-7NgyNOfhICeLyR1O .error-icon{fill:#552222;}#mermaid-svg-7NgyNOfhICeLyR1O .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-7NgyNOfhICeLyR1O .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-7NgyNOfhICeLyR1O .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-7NgyNOfhICeLyR1O .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-7NgyNOfhICeLyR1O .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-7NgyNOfhICeLyR1O .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-7NgyNOfhICeLyR1O .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-7NgyNOfhICeLyR1O .marker{fill:#333333;stroke:#333333;}#mermaid-svg-7NgyNOfhICeLyR1O .marker.cross{stroke:#333333;}#mermaid-svg-7NgyNOfhICeLyR1O svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-7NgyNOfhICeLyR1O p{margin:0;}#mermaid-svg-7NgyNOfhICeLyR1O .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-7NgyNOfhICeLyR1O .cluster-label text{fill:#333;}#mermaid-svg-7NgyNOfhICeLyR1O .cluster-label span{color:#333;}#mermaid-svg-7NgyNOfhICeLyR1O .cluster-label span p{background-color:transparent;}#mermaid-svg-7NgyNOfhICeLyR1O .label text,#mermaid-svg-7NgyNOfhICeLyR1O span{fill:#333;color:#333;}#mermaid-svg-7NgyNOfhICeLyR1O .node rect,#mermaid-svg-7NgyNOfhICeLyR1O .node circle,#mermaid-svg-7NgyNOfhICeLyR1O .node ellipse,#mermaid-svg-7NgyNOfhICeLyR1O .node polygon,#mermaid-svg-7NgyNOfhICeLyR1O .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-7NgyNOfhICeLyR1O .rough-node .label text,#mermaid-svg-7NgyNOfhICeLyR1O .node .label text,#mermaid-svg-7NgyNOfhICeLyR1O .image-shape .label,#mermaid-svg-7NgyNOfhICeLyR1O .icon-shape .label{text-anchor:middle;}#mermaid-svg-7NgyNOfhICeLyR1O .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-7NgyNOfhICeLyR1O .rough-node .label,#mermaid-svg-7NgyNOfhICeLyR1O .node .label,#mermaid-svg-7NgyNOfhICeLyR1O .image-shape .label,#mermaid-svg-7NgyNOfhICeLyR1O .icon-shape .label{text-align:center;}#mermaid-svg-7NgyNOfhICeLyR1O .node.clickable{cursor:pointer;}#mermaid-svg-7NgyNOfhICeLyR1O .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-7NgyNOfhICeLyR1O .arrowheadPath{fill:#333333;}#mermaid-svg-7NgyNOfhICeLyR1O .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-7NgyNOfhICeLyR1O .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-7NgyNOfhICeLyR1O .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-7NgyNOfhICeLyR1O .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-7NgyNOfhICeLyR1O .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-7NgyNOfhICeLyR1O .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-7NgyNOfhICeLyR1O .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-7NgyNOfhICeLyR1O .cluster text{fill:#333;}#mermaid-svg-7NgyNOfhICeLyR1O .cluster span{color:#333;}#mermaid-svg-7NgyNOfhICeLyR1O div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-7NgyNOfhICeLyR1O .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-7NgyNOfhICeLyR1O rect.text{fill:none;stroke-width:0;}#mermaid-svg-7NgyNOfhICeLyR1O .icon-shape,#mermaid-svg-7NgyNOfhICeLyR1O .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-7NgyNOfhICeLyR1O .icon-shape p,#mermaid-svg-7NgyNOfhICeLyR1O .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-7NgyNOfhICeLyR1O .icon-shape .label rect,#mermaid-svg-7NgyNOfhICeLyR1O .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-7NgyNOfhICeLyR1O .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-7NgyNOfhICeLyR1O .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-7NgyNOfhICeLyR1O :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

外部

工具层

设备层

处理层

入口层

exec

exec

注入Poststop

runtime/main.go

hook/main.go

install/main.go

runtime/process

hook/process

install/process

runtime/dcmi

libdcmi.soCGO

mindxcheckutils

hwlog日志库

ascend-common/api常量定义

runc

ascend-docker-cliC语言

ascend-docker-destroyC语言

3.2 容器创建主流程时序图

ascend-docker-cli

hook/main.go

runc

dcmi

runtime/process

runtime/main.go

Docker

ascend-docker-cli

hook/main.go

runc

dcmi

runtime/process

runtime/main.go

Docker

#mermaid-svg-fdrrwPfAQ0wwoFhL{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-fdrrwPfAQ0wwoFhL .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-fdrrwPfAQ0wwoFhL .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-fdrrwPfAQ0wwoFhL .error-icon{fill:#552222;}#mermaid-svg-fdrrwPfAQ0wwoFhL .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-fdrrwPfAQ0wwoFhL .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-fdrrwPfAQ0wwoFhL .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-fdrrwPfAQ0wwoFhL .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-fdrrwPfAQ0wwoFhL .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-fdrrwPfAQ0wwoFhL .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-fdrrwPfAQ0wwoFhL .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-fdrrwPfAQ0wwoFhL .marker{fill:#333333;stroke:#333333;}#mermaid-svg-fdrrwPfAQ0wwoFhL .marker.cross{stroke:#333333;}#mermaid-svg-fdrrwPfAQ0wwoFhL svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-fdrrwPfAQ0wwoFhL p{margin:0;}#mermaid-svg-fdrrwPfAQ0wwoFhL .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-fdrrwPfAQ0wwoFhL text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-fdrrwPfAQ0wwoFhL .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-fdrrwPfAQ0wwoFhL .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-fdrrwPfAQ0wwoFhL .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-fdrrwPfAQ0wwoFhL .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-fdrrwPfAQ0wwoFhL #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-fdrrwPfAQ0wwoFhL .sequenceNumber{fill:white;}#mermaid-svg-fdrrwPfAQ0wwoFhL #sequencenumber{fill:#333;}#mermaid-svg-fdrrwPfAQ0wwoFhL #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-fdrrwPfAQ0wwoFhL .messageText{fill:#333;stroke:none;}#mermaid-svg-fdrrwPfAQ0wwoFhL .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-fdrrwPfAQ0wwoFhL .labelText,#mermaid-svg-fdrrwPfAQ0wwoFhL .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-fdrrwPfAQ0wwoFhL .loopText,#mermaid-svg-fdrrwPfAQ0wwoFhL .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-fdrrwPfAQ0wwoFhL .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-fdrrwPfAQ0wwoFhL .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-fdrrwPfAQ0wwoFhL .noteText,#mermaid-svg-fdrrwPfAQ0wwoFhL .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-fdrrwPfAQ0wwoFhL .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-fdrrwPfAQ0wwoFhL .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-fdrrwPfAQ0wwoFhL .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-fdrrwPfAQ0wwoFhL .actorPopupMenu{position:absolute;}#mermaid-svg-fdrrwPfAQ0wwoFhL .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-fdrrwPfAQ0wwoFhL .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-fdrrwPfAQ0wwoFhL .actor-man circle,#mermaid-svg-fdrrwPfAQ0wwoFhL line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-fdrrwPfAQ0wwoFhL :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

alt

[VIRTUAL模式]

alt

[需要 NPU]

create –bundle /path

DoProcess()

getArgs() 解析参数

读取 config.json

checkVisibleDevice() 解析设备列表

GetMatchingNpuWorker()

选择V1/V2驱动

GetChipName() 获取芯片类型

addHook() 注入Prestart+Poststop

CreateVDevice() 创建虚拟设备

updateEnvAndPostHook()

addDevice() 挂载davinci设备

addManagerDevice() 挂载管理设备

addUBDevice() 挂载UB设备

写回 config.json

exec runc create

触发 Prestart Hook

DoPrestartHook()

解析挂载配置 .list文件

exec ascend-docker-cli –mount-file … –mount-dir …

挂载设备文件+目录到容器namespace

配置device cgroup

容器创建完成,NPU可用

3.3 安装流程时序图

文件系统

ContainerdProcess

DockerProcess

install/main.go

用户/脚本

文件系统

ContainerdProcess

DockerProcess

install/main.go

用户/脚本

#mermaid-svg-3hncCyMjmiTAusnY{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-3hncCyMjmiTAusnY .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-3hncCyMjmiTAusnY .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-3hncCyMjmiTAusnY .error-icon{fill:#552222;}#mermaid-svg-3hncCyMjmiTAusnY .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-3hncCyMjmiTAusnY .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-3hncCyMjmiTAusnY .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-3hncCyMjmiTAusnY .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-3hncCyMjmiTAusnY .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-3hncCyMjmiTAusnY .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-3hncCyMjmiTAusnY .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-3hncCyMjmiTAusnY .marker{fill:#333333;stroke:#333333;}#mermaid-svg-3hncCyMjmiTAusnY .marker.cross{stroke:#333333;}#mermaid-svg-3hncCyMjmiTAusnY svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-3hncCyMjmiTAusnY p{margin:0;}#mermaid-svg-3hncCyMjmiTAusnY .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-3hncCyMjmiTAusnY text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-3hncCyMjmiTAusnY .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-3hncCyMjmiTAusnY .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-3hncCyMjmiTAusnY .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-3hncCyMjmiTAusnY .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-3hncCyMjmiTAusnY #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-3hncCyMjmiTAusnY .sequenceNumber{fill:white;}#mermaid-svg-3hncCyMjmiTAusnY #sequencenumber{fill:#333;}#mermaid-svg-3hncCyMjmiTAusnY #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-3hncCyMjmiTAusnY .messageText{fill:#333;stroke:none;}#mermaid-svg-3hncCyMjmiTAusnY .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-3hncCyMjmiTAusnY .labelText,#mermaid-svg-3hncCyMjmiTAusnY .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-3hncCyMjmiTAusnY .loopText,#mermaid-svg-3hncCyMjmiTAusnY .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-3hncCyMjmiTAusnY .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-3hncCyMjmiTAusnY .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-3hncCyMjmiTAusnY .noteText,#mermaid-svg-3hncCyMjmiTAusnY .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-3hncCyMjmiTAusnY .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-3hncCyMjmiTAusnY .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-3hncCyMjmiTAusnY .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-3hncCyMjmiTAusnY .actorPopupMenu{position:absolute;}#mermaid-svg-3hncCyMjmiTAusnY .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-3hncCyMjmiTAusnY .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-3hncCyMjmiTAusnY .actor-man circle,#mermaid-svg-3hncCyMjmiTAusnY line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-3hncCyMjmiTAusnY :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

alt

[add]

[rm]

alt

[cgroup v2]

[cgroup v1]

alt

[Docker场景]

[Containerd场景]

add/rm <args>

参数校验 CheckParamLength

DockerProcess(command)

读取 daemon.json

addDockerDaemon()

设置 default-runtime=ascend

rmDockerDaemon()

删除 default-runtime

写入 daemon.json

ContainerdProcess(command)

读取 config.toml

editContainerdConfig()

changeCgroupV2BinaryNameConfig()

changeCgroupV1RuntimeConfig()

changeCgroupV1RuntimeTypeConfig()

写入 config.toml

3.4 vNPU 虚拟设备创建流程

#mermaid-svg-msYhq8NRxv809Oqi{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-msYhq8NRxv809Oqi .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-msYhq8NRxv809Oqi .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-msYhq8NRxv809Oqi .error-icon{fill:#552222;}#mermaid-svg-msYhq8NRxv809Oqi .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-msYhq8NRxv809Oqi .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-msYhq8NRxv809Oqi .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-msYhq8NRxv809Oqi .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-msYhq8NRxv809Oqi .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-msYhq8NRxv809Oqi .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-msYhq8NRxv809Oqi .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-msYhq8NRxv809Oqi .marker{fill:#333333;stroke:#333333;}#mermaid-svg-msYhq8NRxv809Oqi .marker.cross{stroke:#333333;}#mermaid-svg-msYhq8NRxv809Oqi svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-msYhq8NRxv809Oqi p{margin:0;}#mermaid-svg-msYhq8NRxv809Oqi .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-msYhq8NRxv809Oqi .cluster-label text{fill:#333;}#mermaid-svg-msYhq8NRxv809Oqi .cluster-label span{color:#333;}#mermaid-svg-msYhq8NRxv809Oqi .cluster-label span p{background-color:transparent;}#mermaid-svg-msYhq8NRxv809Oqi .label text,#mermaid-svg-msYhq8NRxv809Oqi span{fill:#333;color:#333;}#mermaid-svg-msYhq8NRxv809Oqi .node rect,#mermaid-svg-msYhq8NRxv809Oqi .node circle,#mermaid-svg-msYhq8NRxv809Oqi .node ellipse,#mermaid-svg-msYhq8NRxv809Oqi .node polygon,#mermaid-svg-msYhq8NRxv809Oqi .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-msYhq8NRxv809Oqi .rough-node .label text,#mermaid-svg-msYhq8NRxv809Oqi .node .label text,#mermaid-svg-msYhq8NRxv809Oqi .image-shape .label,#mermaid-svg-msYhq8NRxv809Oqi .icon-shape .label{text-anchor:middle;}#mermaid-svg-msYhq8NRxv809Oqi .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-msYhq8NRxv809Oqi .rough-node .label,#mermaid-svg-msYhq8NRxv809Oqi .node .label,#mermaid-svg-msYhq8NRxv809Oqi .image-shape .label,#mermaid-svg-msYhq8NRxv809Oqi .icon-shape .label{text-align:center;}#mermaid-svg-msYhq8NRxv809Oqi .node.clickable{cursor:pointer;}#mermaid-svg-msYhq8NRxv809Oqi .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-msYhq8NRxv809Oqi .arrowheadPath{fill:#333333;}#mermaid-svg-msYhq8NRxv809Oqi .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-msYhq8NRxv809Oqi .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-msYhq8NRxv809Oqi .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-msYhq8NRxv809Oqi .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-msYhq8NRxv809Oqi .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-msYhq8NRxv809Oqi .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-msYhq8NRxv809Oqi .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-msYhq8NRxv809Oqi .cluster text{fill:#333;}#mermaid-svg-msYhq8NRxv809Oqi .cluster span{color:#333;}#mermaid-svg-msYhq8NRxv809Oqi div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-msYhq8NRxv809Oqi .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-msYhq8NRxv809Oqi rect.text{fill:none;stroke-width:0;}#mermaid-svg-msYhq8NRxv809Oqi .icon-shape,#mermaid-svg-msYhq8NRxv809Oqi .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-msYhq8NRxv809Oqi .icon-shape p,#mermaid-svg-msYhq8NRxv809Oqi .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-msYhq8NRxv809Oqi .icon-shape .label rect,#mermaid-svg-msYhq8NRxv809Oqi .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-msYhq8NRxv809Oqi .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-msYhq8NRxv809Oqi .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-msYhq8NRxv809Oqi :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

否

是

V1

V2

检测到 VIRTUAL 选项

extractVpuParam从Spec提取切分参数

参数有效?

返回错误

Worker.CreateVDevice

Worker版本

DcV1Manager.DcCreateVDeviceCGO: 指定card+device

DcV2Manager.DcCreateVDeviceCGO: 指定deviceID

返回 vDevID

updateEnvAndPostHook

更新ASCEND_RUNTIME_OPTIONS追加VIRTUAL标记

注入Poststop Hook销毁vDevice

更新deviceIdList改为vDeviceID

3.5 数据流描述

设备发现数据流: ASCEND_VISIBLE_DEVICES环境变量 → getValueByDeviceKey() → parseDevices()/parseAscendDevices() → 设备ID列表 → GetMatchingNpuWorker() → NPU Worker → GetChipName() → 芯片类型

设备注入数据流: 设备ID列表 → addDevice() → /dev/davinci{N} → oci.DeviceFromPath() → Spec.Linux.Devices → Spec.Linux.Resources.Devices(cgroup) → 写入config.json → runc读取并创建设备

Hook挂载数据流: runc触发Prestart → hook/main.go → stdin读取OCI State → 解析config.json → 读取 /etc/ascend-docker-runtime.d/base.list → 组装CLI参数 → syscall.Exec(ascend-docker-cli) → CLI挂载文件/目录到容器namespace


第四章:架构设计总结

4.1 设计模式总结

#mermaid-svg-HZlod7JsVmdbkjrr{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-HZlod7JsVmdbkjrr .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-HZlod7JsVmdbkjrr .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-HZlod7JsVmdbkjrr .error-icon{fill:#552222;}#mermaid-svg-HZlod7JsVmdbkjrr .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-HZlod7JsVmdbkjrr .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-HZlod7JsVmdbkjrr .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-HZlod7JsVmdbkjrr .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-HZlod7JsVmdbkjrr .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-HZlod7JsVmdbkjrr .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-HZlod7JsVmdbkjrr .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-HZlod7JsVmdbkjrr .marker{fill:#333333;stroke:#333333;}#mermaid-svg-HZlod7JsVmdbkjrr .marker.cross{stroke:#333333;}#mermaid-svg-HZlod7JsVmdbkjrr svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-HZlod7JsVmdbkjrr p{margin:0;}#mermaid-svg-HZlod7JsVmdbkjrr .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-HZlod7JsVmdbkjrr .cluster-label text{fill:#333;}#mermaid-svg-HZlod7JsVmdbkjrr .cluster-label span{color:#333;}#mermaid-svg-HZlod7JsVmdbkjrr .cluster-label span p{background-color:transparent;}#mermaid-svg-HZlod7JsVmdbkjrr .label text,#mermaid-svg-HZlod7JsVmdbkjrr span{fill:#333;color:#333;}#mermaid-svg-HZlod7JsVmdbkjrr .node rect,#mermaid-svg-HZlod7JsVmdbkjrr .node circle,#mermaid-svg-HZlod7JsVmdbkjrr .node ellipse,#mermaid-svg-HZlod7JsVmdbkjrr .node polygon,#mermaid-svg-HZlod7JsVmdbkjrr .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-HZlod7JsVmdbkjrr .rough-node .label text,#mermaid-svg-HZlod7JsVmdbkjrr .node .label text,#mermaid-svg-HZlod7JsVmdbkjrr .image-shape .label,#mermaid-svg-HZlod7JsVmdbkjrr .icon-shape .label{text-anchor:middle;}#mermaid-svg-HZlod7JsVmdbkjrr .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-HZlod7JsVmdbkjrr .rough-node .label,#mermaid-svg-HZlod7JsVmdbkjrr .node .label,#mermaid-svg-HZlod7JsVmdbkjrr .image-shape .label,#mermaid-svg-HZlod7JsVmdbkjrr .icon-shape .label{text-align:center;}#mermaid-svg-HZlod7JsVmdbkjrr .node.clickable{cursor:pointer;}#mermaid-svg-HZlod7JsVmdbkjrr .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-HZlod7JsVmdbkjrr .arrowheadPath{fill:#333333;}#mermaid-svg-HZlod7JsVmdbkjrr .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-HZlod7JsVmdbkjrr .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-HZlod7JsVmdbkjrr .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-HZlod7JsVmdbkjrr .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-HZlod7JsVmdbkjrr .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-HZlod7JsVmdbkjrr .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-HZlod7JsVmdbkjrr .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-HZlod7JsVmdbkjrr .cluster text{fill:#333;}#mermaid-svg-HZlod7JsVmdbkjrr .cluster span{color:#333;}#mermaid-svg-HZlod7JsVmdbkjrr div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-HZlod7JsVmdbkjrr .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-HZlod7JsVmdbkjrr rect.text{fill:none;stroke-width:0;}#mermaid-svg-HZlod7JsVmdbkjrr .icon-shape,#mermaid-svg-HZlod7JsVmdbkjrr .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-HZlod7JsVmdbkjrr .icon-shape p,#mermaid-svg-HZlod7JsVmdbkjrr .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-HZlod7JsVmdbkjrr .icon-shape .label rect,#mermaid-svg-HZlod7JsVmdbkjrr .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-HZlod7JsVmdbkjrr .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-HZlod7JsVmdbkjrr .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-HZlod7JsVmdbkjrr :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

安全防御

mindxcheckutils

路径校验防软链接攻击

文件大小限制

字符串白名单过滤

递归深度限制

Hook模式

Prestart Hook

容器namespace已创建

挂载设备+配置cgroup

Poststop Hook

容器停止

销毁vDevice

拦截器模式

ascend-docker-runtime

拦截create命令

修改OCI Spec

透传给runc

工厂模式

V1

V2

GetMatchingNpuWorker

驱动版本检测

创建NpuV1Worker

创建NpuV2Worker

策略模式

运行时选择

WorkerInterface

NpuV1Worker旧版驱动策略

NpuV2Worker新版驱动策略

GetMatchingNpuWorker

4.2 关键设计决策

决策原因实现
不修改Docker引擎 保持与Docker原生兼容,升级无感知 OCI Runtime插件方式
Prestart Hook挂载 namespace已创建但作业未启动,安全窗口 OCI Spec注入Hook
V1/V2双驱动 新旧NPU硬件驱动接口不同 WorkerInterface策略模式
C语言CLI 挂载操作需要直接系统调用,C更高效 hook通过exec调用C CLI
安全检查模块 防止路径穿越、软链接攻击、注入攻击 mindxcheckutils统一校验
config.json修改 OCI标准方式,runc自动读取 读取→修改→写回
Poststop Hook vNPU虚拟设备需要销毁,避免资源泄漏 destroy C程序

4.3 扩展性分析

扩展点机制示例
新增NPU芯片类型 扩展 GetDeviceTypeByChipName() Ascend 910 → 910B → A5
新增驱动版本 实现 WorkerInterface V1 → V2
新增挂载配置 添加 .list 文件到配置目录 base.list + 自定义.list
新增运行时选项 扩展 validRuntimeOptions NODRV, VIRTUAL
新增容器运行时 扩展 install/process Docker, Containerd, iSula
新增管理设备 扩展 managerDevicesMap 芯片特定设备列表

4.4 架构特点总结

  • OCI标准兼容:完全基于OCI标准实现,不侵入Docker/containerd引擎
  • 分层设计:入口层(main) → 处理层(process) → 设备层(dcmi) → 底层(CGO/C库)
  • 安全优先:所有外部输入都经过 mindxcheckutils 安全校验
  • 多版本兼容:V1/V2双驱动策略,支持新旧NPU硬件
  • 声明式配置:通过 .list 文件声明挂载列表,非硬编码
  • Go+C混合实现:Go处理业务逻辑,C处理底层挂载操作
  • 赞(0)
    未经允许不得转载:171主机测评 » 【NPU】Ascend Docker Runtime v26.0.1 系统级架构分析
    分享到: 更多 (0)

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址