3838 "gpu" : true ,
3939 "gaudi" : true ,
4040 }
41- version = "v0.2 .0"
41+ version = "v0.3 .0"
4242)
4343
4444func main () {
@@ -50,6 +50,60 @@ func main() {
5050 }
5151}
5252
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+
53107func newCommand () * cobra.Command {
54108 cmd := & cobra.Command {
55109 Use : "intel-cdi-specs-generator [--cdi-dir=<cdi directory>] [--naming=<style>] <gpu | gaudi>" ,
@@ -69,69 +123,38 @@ func newCommand() *cobra.Command {
69123
70124 return nil
71125 },
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 ,
116127 }
117128
118129 cmd .Version = version
119130 cmd .Flags ().BoolP ("version" , "v" , false , "Show the version of the binary" )
120131 cmd .Flags ().String ("cdi-dir" , "/etc/cdi" , "CDI spec directory" )
121132 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" )
122134 cmd .SetVersionTemplate ("Intel CDI Specs Generator Version: {{.Version}}\n " )
123135
124136 return cmd
125137}
126138
127- func handleGPUDevices (cdiCache * cdiapi.Cache , namingStyle string ) error {
139+ func handleGPUDevices (cdiCache * cdiapi.Cache , namingStyle string , dryRun bool ) error {
128140 sysfsDir := gpuDevice .GetSysfsRoot ()
129141
142+ fmt .Println ("Scanning for GPUs" )
143+
130144 detectedDevices := gpuDiscovery .DiscoverDevices (sysfsDir , namingStyle )
131145 if len (detectedDevices ) == 0 {
132146 fmt .Println ("No supported devices detected" )
133147 }
134148
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+
135158 // syncDetectedDevicesWithCdiRegistry overrides uid in detecteddevices from existing cdi spec
136159 if err := gpuCdihelpers .SyncDetectedDevicesWithRegistry (cdiCache , detectedDevices , true ); err != nil {
137160 fmt .Printf ("unable to sync detected devices to CDI registry: %v" , err )
@@ -141,14 +164,25 @@ func handleGPUDevices(cdiCache *cdiapi.Cache, namingStyle string) error {
141164 return nil
142165}
143166
144- func handleGaudiDevices (cdiCache * cdiapi.Cache , namingStyle string ) error {
167+ func handleGaudiDevices (cdiCache * cdiapi.Cache , namingStyle string , dryRun bool ) error {
145168 sysfsDir := gaudiDevice .GetSysfsRoot ()
146169
170+ fmt .Println ("Scanning for Gaudi accelerators" )
171+
147172 detectedDevices := gaudiDiscovery .DiscoverDevices (sysfsDir , namingStyle )
148173 if len (detectedDevices ) == 0 {
149174 fmt .Println ("No supported devices detected" )
150175 }
151176
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+
152186 // syncDetectedDevicesWithCdiRegistry overrides uid in detecteddevices from existing cdi spec
153187 if err := gaudiCdihelpers .SyncDetectedDevicesWithRegistry (cdiCache , detectedDevices , true ); err != nil {
154188 fmt .Printf ("unable to sync detected devices to CDI registry: %v" , err )
0 commit comments