Skip to content

Commit a36b968

Browse files
committed
geat: Go implement blacklist
Signed-off-by: Fred Rolland <[email protected]>
1 parent fab58e0 commit a36b968

File tree

3 files changed

+601
-0
lines changed

3 files changed

+601
-0
lines changed

entrypoint/internal/driver/driver.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
118119
func (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+
}
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)