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