Skip to content

Commit

Permalink
lxd/instance/drivers/driver_lxc: Configure LXC to use our custom CDI …
Browse files Browse the repository at this point in the history
…hook if CDI devices have been configured

Signed-off-by: Gabriel Mougard <[email protected]>
  • Loading branch information
gabrielmougard committed Jun 6, 2024
1 parent 9b05b90 commit 6b149e2
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lxd/instance/drivers/driver_lxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/canonical/lxd/lxd/db/cluster"
"github.com/canonical/lxd/lxd/db/operationtype"
"github.com/canonical/lxd/lxd/device"
"github.com/canonical/lxd/lxd/device/cdi"
deviceConfig "github.com/canonical/lxd/lxd/device/config"
"github.com/canonical/lxd/lxd/device/nictype"
"github.com/canonical/lxd/lxd/idmap"
Expand Down Expand Up @@ -2053,6 +2054,7 @@ func (d *lxc) startCommon() (string, []func() error, error) {
// Create the devices
nicID := -1
nvidiaDevices := []string{}
cdiHookFiles := []string{}

sortedDevices := d.expandedDevices.Sorted()
startDevices := make([]device.Device, 0, len(sortedDevices))
Expand Down Expand Up @@ -2223,6 +2225,10 @@ func (d *lxc) startCommon() (string, []func() error, error) {
if entry.Key == device.GPUNvidiaDeviceKey {
nvidiaDevices = append(nvidiaDevices, entry.Value)
}

if entry.Key == cdi.CDIHookFilepathKey {
cdiHookFiles = append(cdiHookFiles, entry.Value)
}
}
}
}
Expand All @@ -2235,6 +2241,24 @@ func (d *lxc) startCommon() (string, []func() error, error) {
}
}

if len(cdiHookFiles) > 0 {
err = lxcSetConfigItem(cc, "lxc.environment", fmt.Sprintf("CDI_HOOK_FILES=%s", strings.Join(cdiHookFiles, ",")))
if err != nil {
return "", nil, fmt.Errorf("Unable to set CDI_HOOK_FILES in LXC environment: %w", err)
}

lxdCDIHookPath := os.Getenv("LXD_CDI_HOOK")
if lxdCDIHookPath == "" {
return "", nil, fmt.Errorf("LXD_CDI_HOOK environment variable should be set to use custom CDI hooks")
}

// If CDI is used, we override the default `nvidia` LXC hook to use the CDI LXD hook.
err = lxcSetConfigItem(cc, "lxc.hook.mount", lxdCDIHookPath)
if err != nil {
return "", nil, fmt.Errorf("Unable to set CDI hook in LXC: %w", err)
}
}

// Load the LXC raw config.
err = d.loadRawLXCConfig(cc)
if err != nil {
Expand Down

0 comments on commit 6b149e2

Please sign in to comment.