@@ -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+
92167func (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