diff --git a/lxd/instance/drivers/driver_lxc.go b/lxd/instance/drivers/driver_lxc.go index 8f0369a5fb86..d3e042359b54 100644 --- a/lxd/instance/drivers/driver_lxc.go +++ b/lxd/instance/drivers/driver_lxc.go @@ -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" @@ -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)) @@ -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) + } } } } @@ -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 {