Skip to content

Commit c65d8a1

Browse files
committed
feat: automatically create and copy vnpu core files
Signed-off-by: ashergaga <1214443299@qq.com>
1 parent bacb496 commit c65d8a1

3 files changed

Lines changed: 81 additions & 44 deletions

File tree

README.md

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,6 @@ make all
4141
docker buildx build -t $IMAGE_NAME .
4242
```
4343

44-
### Host Environment Preparation
45-
46-
Before launching any containers, the **Global Shared Memory (SHM) Region** must be initialized on the host to allow inter-Pod coordination.
47-
48-
#### 1. Create the Shared Directory
49-
50-
```bash
51-
sudo mkdir -p /tmp/hami-shared-region
52-
sudo chmod 777 /tmp/hami-shared-region
53-
```
54-
55-
#### 2. Deploy hami-vnpu-core Components
56-
57-
Place the following files in a fixed host path (`/usr/local/hami-vnpu-core/`) for mounting into containers:
58-
59-
```
60-
/usr/local/hami-vnpu-core/
61-
├── limiter # Manager daemon binary (compiled from hami-vnpu-core)
62-
├── libvnpu.so # Interception library for LD_PRELOAD
63-
└── ld.so.preload # Global preload config
64-
```
65-
6644
## Deployment
6745

6846
### Label the Node with `ascend=on`

README_cn.md

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,6 @@ make all
3939
docker buildx build -t $IMAGE_NAME .
4040
```
4141

42-
### 宿主机环境准备
43-
44-
在启动任何容器之前,必须在宿主机上初始化 **全局共享内存 (SHM) 区域**,以便进行 Pod 间的协同。
45-
46-
1. **创建共享目录**
47-
48-
```
49-
sudo mkdir -p /usr/local/hami-shared-region
50-
sudo chmod 777 /usr/local/hami-shared-region
51-
```
52-
53-
2. **部署 hami-vnpu-core 组件**
54-
55-
将以下文件放置在固定的宿主机路径(`/usr/local/hami-vnpu-core/`)中,以便挂载到容器内:
56-
57-
```
58-
/usr/local/hami-vnpu-core/
59-
├── limiter # Manager daemon binary (compiled from hami-vnpu-core)
60-
├── libvnpu.so # Interception library for LD_PRELOAD
61-
└── ld.so.preload # Global preload config
62-
```
63-
6442
## 部署
6543

6644
### 给 Node 打 ascend 标签

internal/server/server.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,88 @@ func NewPluginServer(mgr *manager.AscendManager, nodeName string, checkIdleVNPUI
8989
}, nil
9090
}
9191

92+
93+
// Automatically creates directories, sets permissions, and copies core files on the host
94+
func prepareHostResources() error {
95+
klog.Info("Starting host resource preparation for HAMi vNPU core...")
96+
97+
// 1. Create shared memory directory
98+
sharedRegionPath := "/usr/local/hami-shared-region"
99+
if err := os.MkdirAll(sharedRegionPath, 0777); err != nil {
100+
if !os.IsExist(err) {
101+
return fmt.Errorf("failed to create %s: %v", sharedRegionPath, err)
102+
}
103+
}
104+
if err := os.Chmod(sharedRegionPath, 0777); err != nil {
105+
return fmt.Errorf("failed to chmod %s: %v", sharedRegionPath, err)
106+
}
107+
klog.Infof("Successfully prepared directory: %s", sharedRegionPath)
108+
109+
// 2. Prepare /usr/local/hami-vnpu-core/ directory
110+
targetDir := "/usr/local/hami-vnpu-core"
111+
if err := os.MkdirAll(targetDir, 0775); err != nil {
112+
return fmt.Errorf("failed to create %s: %v", targetDir, err)
113+
}
114+
115+
// Specify the in-container assets directory (can be overridden via environment variable, default follows standard DevicePlugin convention)
116+
assetsDir := os.Getenv("HAMI_VNPU_ASSETS_PATH")
117+
if assetsDir == "" {
118+
assetsDir = "/usr/local/hami-vnpu-core-assets"
119+
}
120+
121+
// Define files to copy: source path in container -> target path on host
122+
filesToCopy := map[string]string{
123+
"limiter": filepath.Join(targetDir, "limiter"),
124+
"libvnpu.so": filepath.Join(targetDir, "libvnpu.so"),
125+
"ld.so.preload": filepath.Join(targetDir, "ld.so.preload"),
126+
}
127+
128+
for srcName, destPath := range filesToCopy {
129+
srcPath := filepath.Join(assetsDir, srcName)
130+
if err := copyFile(srcPath, destPath); err != nil {
131+
return fmt.Errorf("failed to copy %s to %s: %v", srcPath, destPath, err)
132+
}
133+
klog.Infof("Copied %s -> %s", srcPath, destPath)
134+
}
135+
136+
klog.Info("Host resource preparation completed successfully.")
137+
return nil
138+
}
139+
140+
// A standard file copy implementation that preserves the original file permissions
141+
func copyFile(src, dst string) error {
142+
srcFile, err := os.Open(src)
143+
if err != nil {
144+
return err
145+
}
146+
defer srcFile.Close()
147+
148+
dstFile, err := os.Create(dst)
149+
if err != nil {
150+
return err
151+
}
152+
defer dstFile.Close()
153+
154+
if _, err = io.Copy(dstFile, srcFile); err != nil {
155+
return err
156+
}
157+
158+
// Sync source file permissions (ensure the limiter binary retains executable permission)
159+
srcInfo, err := srcFile.Stat()
160+
if err != nil {
161+
return err
162+
}
163+
return os.Chmod(dst, srcInfo.Mode())
164+
}
165+
166+
92167
func (ps *PluginServer) Start() error {
168+
// Automatically prepare host environment when the plugin starts
169+
if err := prepareHostResources(); err != nil {
170+
klog.Errorf("Failed to prepare host resources: %v. vNPU core functionality will be impaired.", err)
171+
return err
172+
}
173+
93174
ps.stopCh = make(chan interface{})
94175
err := ps.mgr.UpdateDevice()
95176
if err != nil {

0 commit comments

Comments
 (0)