@@ -20,6 +20,7 @@ import (
2020 "context"
2121 "fmt"
2222 "os"
23+ "strings"
2324
2425 "github.com/go-logr/logr"
2526
@@ -116,7 +117,12 @@ func (d *driverMgr) Build(ctx context.Context) error {
116117
117118// Load is the default implementation of the driver.Interface.
118119func (d * driverMgr ) Load (ctx context.Context ) (bool , error ) {
119- // TODO: Implement
120+ if err := d .generateOfedModulesBlacklist (ctx ); err != nil {
121+ return false , err
122+ }
123+ if err := d .removeOfedModulesBlacklist (ctx ); err != nil {
124+ return false , err
125+ }
120126 return true , nil
121127}
122128
@@ -143,3 +149,64 @@ func (d *driverMgr) prepareGCC(ctx context.Context) error {
143149 }
144150 return nil
145151}
152+
153+ // generateOfedModulesBlacklist creates a blacklist file for OFED modules to prevent
154+ // inbox or host OFED driver loading. This function writes module blacklist entries
155+ // to the configured blacklist file.
156+ func (d * driverMgr ) generateOfedModulesBlacklist (ctx context.Context ) error {
157+ log := logr .FromContextOrDiscard (ctx )
158+ log .V (1 ).Info ("Generating OFED modules blacklist" )
159+
160+ // Create the blacklist file
161+ file , err := d .os .Create (d .cfg .OfedBlacklistModulesFile )
162+ if err != nil {
163+ log .Error (err , "Failed to create blacklist file" , "file" , d .cfg .OfedBlacklistModulesFile )
164+ return fmt .Errorf ("failed to create blacklist file %s: %w" , d .cfg .OfedBlacklistModulesFile , err )
165+ }
166+ defer file .Close ()
167+
168+ // Build the entire content first
169+ var content strings.Builder
170+ content .WriteString ("# blacklist ofed-related modules on host to prevent inbox or host OFED driver loading\n \n " )
171+
172+ // Add blacklist entries for each module
173+ for _ , module := range d .cfg .OfedBlacklistModules {
174+ module = strings .TrimSpace (module )
175+ if module == "" {
176+ continue
177+ }
178+ content .WriteString (fmt .Sprintf ("blacklist %s\n " , module ))
179+ log .V (2 ).Info ("Added module to blacklist" , "module" , module )
180+ }
181+
182+ // Write all content at once
183+ if _ , err := file .WriteString (content .String ()); err != nil {
184+ log .Error (err , "Failed to write blacklist content to file" )
185+ return fmt .Errorf ("failed to write blacklist content to file: %w" , err )
186+ }
187+
188+ log .Info ("Successfully generated OFED modules blacklist" , "file" , d .cfg .OfedBlacklistModulesFile , "modules" , d .cfg .OfedBlacklistModules )
189+ return nil
190+ }
191+
192+ // removeOfedModulesBlacklist removes the OFED modules blacklist file from the host.
193+ // This function is typically called during cleanup or when the blacklist is no longer needed.
194+ func (d * driverMgr ) removeOfedModulesBlacklist (ctx context.Context ) error {
195+ log := logr .FromContextOrDiscard (ctx )
196+ log .V (1 ).Info ("Removing OFED modules blacklist file" )
197+
198+ // Check if file exists before attempting to remove
199+ if _ , err := d .os .Stat (d .cfg .OfedBlacklistModulesFile ); os .IsNotExist (err ) {
200+ log .V (1 ).Info ("Blacklist file does not exist, nothing to remove" , "file" , d .cfg .OfedBlacklistModulesFile )
201+ return nil
202+ }
203+
204+ // Remove the blacklist file
205+ if err := d .os .RemoveAll (d .cfg .OfedBlacklistModulesFile ); err != nil {
206+ log .Error (err , "Failed to remove blacklist file" , "file" , d .cfg .OfedBlacklistModulesFile )
207+ return fmt .Errorf ("failed to remove blacklist file %s: %w" , d .cfg .OfedBlacklistModulesFile , err )
208+ }
209+
210+ log .Info ("Successfully removed OFED modules blacklist file" , "file" , d .cfg .OfedBlacklistModulesFile )
211+ return nil
212+ }
0 commit comments