38
38
"gpu" : true ,
39
39
"gaudi" : true ,
40
40
}
41
- version = "v0.2 .0"
41
+ version = "v0.3 .0"
42
42
)
43
43
44
44
func main () {
@@ -50,6 +50,60 @@ func main() {
50
50
}
51
51
}
52
52
53
+ func cobraRunFunc (cmd * cobra.Command , args []string ) error {
54
+ cdiDir := cmd .Flag ("cdi-dir" ).Value .String ()
55
+ namingStyle := cmd .Flag ("naming" ).Value .String ()
56
+
57
+ fmt .Println ("Refreshing CDI registry" )
58
+ if err := cdiapi .Configure (cdiapi .WithSpecDirs (cdiDir )); err != nil {
59
+ fmt .Printf ("unable to refresh the CDI registry: %v" , err )
60
+ return err
61
+ }
62
+
63
+ cdiCache , err := cdiapi .NewCache (cdiapi .WithAutoRefresh (false ), cdiapi .WithSpecDirs (cdiDir ))
64
+ if err != nil {
65
+ return err
66
+ }
67
+
68
+ dryRun := false
69
+ if cmd .Flag ("dry-run" ).Value .String () == "true" {
70
+ dryRun = true
71
+ }
72
+
73
+ for _ , argx := range args {
74
+ switch strings .ToLower (argx ) {
75
+ case "gpu" :
76
+ if err := handleGPUDevices (cdiCache , namingStyle , dryRun ); err != nil {
77
+ return err
78
+ }
79
+ case "gaudi" :
80
+ if err := handleGaudiDevices (cdiCache , namingStyle , dryRun ); err != nil {
81
+ return err
82
+ }
83
+ }
84
+ }
85
+
86
+ if dryRun {
87
+ return nil
88
+ }
89
+
90
+ if err := cdiCache .Refresh (); err != nil {
91
+ return err
92
+ }
93
+
94
+ // Fix CDI spec permissions as the default permission (600) prevents
95
+ // use without root or sudo:
96
+ // https://github.com/cncf-tags/container-device-interface/issues/224
97
+ specs := cdiCache .GetVendorSpecs (gpuDevice .CDIVendor ) // Vendor is same for both gpu and gaudi
98
+ for _ , spec := range specs {
99
+ if err := os .Chmod (spec .GetPath (), 0o644 ); err != nil {
100
+ return err
101
+ }
102
+ }
103
+
104
+ return nil
105
+ }
106
+
53
107
func newCommand () * cobra.Command {
54
108
cmd := & cobra.Command {
55
109
Use : "intel-cdi-specs-generator [--cdi-dir=<cdi directory>] [--naming=<style>] <gpu | gaudi>" ,
@@ -69,69 +123,38 @@ func newCommand() *cobra.Command {
69
123
70
124
return nil
71
125
},
72
- RunE : func (cmd * cobra.Command , args []string ) error {
73
- cdiDir := cmd .Flag ("cdi-dir" ).Value .String ()
74
- namingStyle := cmd .Flag ("naming" ).Value .String ()
75
-
76
- fmt .Println ("Refreshing CDI registry" )
77
- if err := cdiapi .Configure (cdiapi .WithSpecDirs (cdiDir )); err != nil {
78
- fmt .Printf ("unable to refresh the CDI registry: %v" , err )
79
- return err
80
- }
81
-
82
- cdiCache , err := cdiapi .NewCache (cdiapi .WithAutoRefresh (false ), cdiapi .WithSpecDirs (cdiDir ))
83
- if err != nil {
84
- return err
85
- }
86
-
87
- for _ , argx := range args {
88
- switch strings .ToLower (argx ) {
89
- case "gpu" :
90
- if err := handleGPUDevices (cdiCache , namingStyle ); err != nil {
91
- return err
92
- }
93
- case "gaudi" :
94
- if err := handleGaudiDevices (cdiCache , namingStyle ); err != nil {
95
- return err
96
- }
97
- }
98
- }
99
-
100
- if err := cdiCache .Refresh (); err != nil {
101
- return err
102
- }
103
-
104
- // Fix CDI spec permissions as the default permission (600) prevents
105
- // use without root or sudo:
106
- // https://github.com/cncf-tags/container-device-interface/issues/224
107
- specs := cdiCache .GetVendorSpecs (gpuDevice .CDIVendor ) // Vendor is same for both gpu and gaudi
108
- for _ , spec := range specs {
109
- if err := os .Chmod (spec .GetPath (), 0o644 ); err != nil {
110
- return err
111
- }
112
- }
113
-
114
- return nil
115
- },
126
+ RunE : cobraRunFunc ,
116
127
}
117
128
118
129
cmd .Version = version
119
130
cmd .Flags ().BoolP ("version" , "v" , false , "Show the version of the binary" )
120
131
cmd .Flags ().String ("cdi-dir" , "/etc/cdi" , "CDI spec directory" )
121
132
cmd .Flags ().String ("naming" , "classic" , "Naming of CDI devices. Options: classic, machine" )
133
+ cmd .Flags ().BoolP ("dry-run" , "n" , false , "Dry-run, do not create CDI manifests" )
122
134
cmd .SetVersionTemplate ("Intel CDI Specs Generator Version: {{.Version}}\n " )
123
135
124
136
return cmd
125
137
}
126
138
127
- func handleGPUDevices (cdiCache * cdiapi.Cache , namingStyle string ) error {
139
+ func handleGPUDevices (cdiCache * cdiapi.Cache , namingStyle string , dryRun bool ) error {
128
140
sysfsDir := gpuDevice .GetSysfsRoot ()
129
141
142
+ fmt .Println ("Scanning for GPUs" )
143
+
130
144
detectedDevices := gpuDiscovery .DiscoverDevices (sysfsDir , namingStyle )
131
145
if len (detectedDevices ) == 0 {
132
146
fmt .Println ("No supported devices detected" )
133
147
}
134
148
149
+ fmt .Println ("Detected supported devices" )
150
+ for gpuName , gpu := range detectedDevices {
151
+ fmt .Printf ("GPU: %v=%v (%v)\n " , gpuDevice .CDIKind , gpuName , gpu .ModelName )
152
+ }
153
+
154
+ if dryRun {
155
+ return nil
156
+ }
157
+
135
158
// syncDetectedDevicesWithCdiRegistry overrides uid in detecteddevices from existing cdi spec
136
159
if err := gpuCdihelpers .SyncDetectedDevicesWithRegistry (cdiCache , detectedDevices , true ); err != nil {
137
160
fmt .Printf ("unable to sync detected devices to CDI registry: %v" , err )
@@ -141,14 +164,25 @@ func handleGPUDevices(cdiCache *cdiapi.Cache, namingStyle string) error {
141
164
return nil
142
165
}
143
166
144
- func handleGaudiDevices (cdiCache * cdiapi.Cache , namingStyle string ) error {
167
+ func handleGaudiDevices (cdiCache * cdiapi.Cache , namingStyle string , dryRun bool ) error {
145
168
sysfsDir := gaudiDevice .GetSysfsRoot ()
146
169
170
+ fmt .Println ("Scanning for Gaudi accelerators" )
171
+
147
172
detectedDevices := gaudiDiscovery .DiscoverDevices (sysfsDir , namingStyle )
148
173
if len (detectedDevices ) == 0 {
149
174
fmt .Println ("No supported devices detected" )
150
175
}
151
176
177
+ fmt .Println ("Detected supported devices" )
178
+ for gaudiName , gaudi := range detectedDevices {
179
+ fmt .Printf ("Gaudi: %v=%v (%v)\n " , gaudiDevice .CDIKind , gaudiName , gaudi .ModelName )
180
+ }
181
+
182
+ if dryRun {
183
+ return nil
184
+ }
185
+
152
186
// syncDetectedDevicesWithCdiRegistry overrides uid in detecteddevices from existing cdi spec
153
187
if err := gaudiCdihelpers .SyncDetectedDevicesWithRegistry (cdiCache , detectedDevices , true ); err != nil {
154
188
fmt .Printf ("unable to sync detected devices to CDI registry: %v" , err )
0 commit comments