Skip to content

Commit

Permalink
lxd/device: Add support for discovering multiple unix hotplug devices
Browse files Browse the repository at this point in the history
Signed-off-by: Kadin Sayani <[email protected]>
  • Loading branch information
kadinsayani committed Oct 30, 2024
1 parent 0cbfbf7 commit cec2a29
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions lxd/device/unix_hotplug.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,29 +149,31 @@ func (d *unixHotplug) Start() (*deviceConfig.RunConfig, error) {
runConf := deviceConfig.RunConfig{}
runConf.PostHooks = []func() error{d.Register}

device := d.loadUnixDevice()
if d.isRequired() && device == nil {
devices := d.loadUnixDevices()
if d.isRequired() && devices == nil {
return nil, fmt.Errorf("Required Unix Hotplug device not found")
}

if device == nil {
if devices == nil {
return &runConf, nil
}

devnum := device.Devnum()
major := uint32(devnum.Major())
minor := uint32(devnum.Minor())

// setup device
var err error
if device.Subsystem() == "block" {
err = unixDeviceSetupBlockNum(d.state, d.inst.DevicesPath(), "unix", d.name, d.config, major, minor, device.Devnode(), false, &runConf)
} else {
err = unixDeviceSetupCharNum(d.state, d.inst.DevicesPath(), "unix", d.name, d.config, major, minor, device.Devnode(), false, &runConf)
}
for _, device := range devices {
devnum := device.Devnum()
major := uint32(devnum.Major())
minor := uint32(devnum.Minor())

// setup device
var err error
if device.Subsystem() == "block" {
err = unixDeviceSetupBlockNum(d.state, d.inst.DevicesPath(), "unix", d.name, d.config, major, minor, device.Devnode(), false, &runConf)
} else {
err = unixDeviceSetupCharNum(d.state, d.inst.DevicesPath(), "unix", d.name, d.config, major, minor, device.Devnode(), false, &runConf)
}

if err != nil {
return nil, err
if err != nil {
return nil, err
}
}

return &runConf, nil
Expand Down Expand Up @@ -203,9 +205,9 @@ func (d *unixHotplug) postStop() error {
return nil
}

// loadUnixDevice scans the host machine for unix devices with matching product/vendor ids
// and returns the first matching device with the subsystem type char or block.
func (d *unixHotplug) loadUnixDevice() *udev.Device {
// loadUnixDevices scans the host machine for unix devices with matching product/vendor ids
// and returns the matching devices with subsystem types of char or block.
func (d *unixHotplug) loadUnixDevices() []*udev.Device {
// Find device if exists
u := udev.Udev{}
e := u.NewEnumerate()
Expand All @@ -230,6 +232,7 @@ func (d *unixHotplug) loadUnixDevice() *udev.Device {
}

devices, _ := e.Devices()
var matchingDevices []*udev.Device
var device *udev.Device
for i := range devices {
device = devices[i]
Expand All @@ -248,9 +251,9 @@ func (d *unixHotplug) loadUnixDevice() *udev.Device {
}

if !strings.HasPrefix(device.Subsystem(), "usb") {
return device
matchingDevices = append(matchingDevices, device)
}
}

return nil
return matchingDevices
}

0 comments on commit cec2a29

Please sign in to comment.