Skip to content

Commit c8ecc86

Browse files
committed
feat: Go implement blacklist
Signed-off-by: Fred Rolland <[email protected]>
1 parent cd40b4c commit c8ecc86

File tree

3 files changed

+635
-0
lines changed

3 files changed

+635
-0
lines changed

entrypoint/internal/driver/driver.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"context"
2121
"fmt"
2222
"os"
23+
"path/filepath"
24+
"strings"
2325

2426
"github.com/go-logr/logr"
2527

@@ -117,6 +119,12 @@ func (d *driverMgr) Build(ctx context.Context) error {
117119
// Load is the default implementation of the driver.Interface.
118120
func (d *driverMgr) Load(ctx context.Context) (bool, error) {
119121
// TODO: Implement
122+
if err := d.generateOfedModulesBlacklist(ctx); err != nil {
123+
return false, err
124+
}
125+
if err := d.removeOfedModulesBlacklist(ctx); err != nil {
126+
return false, err
127+
}
120128
return true, nil
121129
}
122130

@@ -143,3 +151,71 @@ func (d *driverMgr) prepareGCC(ctx context.Context) error {
143151
}
144152
return nil
145153
}
154+
155+
// generateOfedModulesBlacklist creates a blacklist file for OFED modules to prevent
156+
// inbox or host OFED driver loading. This function writes module blacklist entries
157+
// to the configured blacklist file.
158+
func (d *driverMgr) generateOfedModulesBlacklist(ctx context.Context) error {
159+
log := logr.FromContextOrDiscard(ctx)
160+
log.V(1).Info("Generating OFED modules blacklist")
161+
162+
// Ensure the directory exists
163+
dir := filepath.Dir(d.cfg.OfedBlacklistModulesFile)
164+
if err := d.os.MkdirAll(dir, 0o755); err != nil {
165+
log.Error(err, "Failed to create directory for blacklist file", "dir", dir)
166+
return fmt.Errorf("failed to create directory %s: %w", dir, err)
167+
}
168+
169+
// Create the blacklist file
170+
file, err := d.os.Create(d.cfg.OfedBlacklistModulesFile)
171+
if err != nil {
172+
log.Error(err, "Failed to create blacklist file", "file", d.cfg.OfedBlacklistModulesFile)
173+
return fmt.Errorf("failed to create blacklist file %s: %w", d.cfg.OfedBlacklistModulesFile, err)
174+
}
175+
defer file.Close()
176+
177+
// Build the entire content first
178+
var content strings.Builder
179+
content.WriteString("# blacklist ofed-related modules on host to prevent inbox or host OFED driver loading\n\n")
180+
181+
// Add blacklist entries for each module
182+
for _, module := range d.cfg.OfedBlacklistModules {
183+
module = strings.TrimSpace(module)
184+
if module == "" {
185+
continue
186+
}
187+
content.WriteString(fmt.Sprintf("blacklist %s\n", module))
188+
log.V(2).Info("Added module to blacklist", "module", module)
189+
}
190+
191+
// Write all content at once
192+
if _, err := file.WriteString(content.String()); err != nil {
193+
log.Error(err, "Failed to write blacklist content to file")
194+
return fmt.Errorf("failed to write blacklist content to file: %w", err)
195+
}
196+
197+
log.Info("Successfully generated OFED modules blacklist", "file", d.cfg.OfedBlacklistModulesFile, "modules", d.cfg.OfedBlacklistModules)
198+
return nil
199+
}
200+
201+
// removeOfedModulesBlacklist removes the OFED modules blacklist file from the host.
202+
// This function is typically called during cleanup or when the blacklist is no longer needed.
203+
func (d *driverMgr) removeOfedModulesBlacklist(ctx context.Context) error {
204+
log := logr.FromContextOrDiscard(ctx)
205+
log.V(1).Info("Removing OFED modules blacklist file")
206+
207+
// Check if file exists before attempting to remove
208+
if _, err := d.os.Stat(d.cfg.OfedBlacklistModulesFile); os.IsNotExist(err) {
209+
log.V(1).Info("Blacklist file does not exist, nothing to remove", "file", d.cfg.OfedBlacklistModulesFile)
210+
return nil
211+
}
212+
213+
// Remove the blacklist file
214+
if err := d.os.RemoveAll(d.cfg.OfedBlacklistModulesFile); err != nil {
215+
log.Error(err, "Failed to remove blacklist file", "file", d.cfg.OfedBlacklistModulesFile)
216+
return fmt.Errorf("failed to remove blacklist file %s: %w", d.cfg.OfedBlacklistModulesFile, err)
217+
}
218+
219+
log.Info("Successfully removed OFED modules blacklist file", "file", d.cfg.OfedBlacklistModulesFile)
220+
return nil
221+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright 2025, NVIDIA CORPORATION & AFFILIATES
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package driver
18+
19+
import (
20+
"testing"
21+
22+
. "github.com/onsi/ginkgo/v2"
23+
. "github.com/onsi/gomega"
24+
)
25+
26+
func TestDriver(t *testing.T) {
27+
RegisterFailHandler(Fail)
28+
RunSpecs(t, "Driver Suite")
29+
}

0 commit comments

Comments
 (0)