[lxc-devel] [lxd/master] lxd/devices: be less optimistic about netlink info

brauner on Github lxc-bot at linuxcontainers.org
Wed Oct 19 09:17:25 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 581 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20161019/c54e030e/attachment.bin>
-------------- next part --------------
From e4eead7963ced7ec165e70474c444fe48ac645a0 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at canonical.com>
Date: Wed, 19 Oct 2016 11:15:10 +0200
Subject: [PATCH] lxd/devices: be less optimistic about netlink info

In case netlink does not give us the cheap way of retrieving the usb device path
let's fallback to using the busnum and devnum as before. In other news, I'm less
optimistic about netlink infos then before. :)

Signed-off-by: Christian Brauner <christian.brauner at canonical.com>
---
 lxd/devices.go | 32 +++++++++++++++++++++++++++++---
 1 file changed, 29 insertions(+), 3 deletions(-)

diff --git a/lxd/devices.go b/lxd/devices.go
index 2e21bcb..6a30a0f 100644
--- a/lxd/devices.go
+++ b/lxd/devices.go
@@ -55,7 +55,7 @@ type usbDevice struct {
 	minor int
 }
 
-func createUSBDevice(action string, vendor string, product string, major string, minor string, devname string) (usbDevice, error) {
+func createUSBDevice(action string, vendor string, product string, major string, minor string, busnum string, devnum string, devname string) (usbDevice, error) {
 	majorInt, err := strconv.Atoi(major)
 	if err != nil {
 		return usbDevice{}, err
@@ -67,8 +67,21 @@ func createUSBDevice(action string, vendor string, product string, major string,
 	}
 
 	path := devname
-	if !filepath.IsAbs(devname) {
-		path = fmt.Sprintf("/dev/%s", devname)
+	if devname == "" {
+		busnumInt, err := strconv.Atoi(busnum)
+		if err != nil {
+			return usbDevice{}, err
+		}
+
+		devnumInt, err := strconv.Atoi(devnum)
+		if err != nil {
+			return usbDevice{}, err
+		}
+		path = fmt.Sprintf("/dev/bus/usb/%03d/%03d", busnumInt, devnumInt)
+	} else {
+		if !filepath.IsAbs(devname) {
+			path = fmt.Sprintf("/dev/%s", devname)
+		}
 	}
 
 	return usbDevice{
@@ -180,11 +193,20 @@ func deviceNetlinkListener() (chan []string, chan []string, chan usbDevice, erro
 				if !ok {
 					continue
 				}
+
 				minor, ok := props["MINOR"]
 				if !ok {
 					continue
 				}
+
 				devname, ok := props["DEVNAME"]
+
+				busnum, ok := props["BUSNUM"]
+				if !ok {
+					continue
+				}
+
+				devnum, ok := props["DEVNUM"]
 				if !ok {
 					continue
 				}
@@ -203,6 +225,8 @@ func deviceNetlinkListener() (chan []string, chan []string, chan usbDevice, erro
 					zeroPad(parts[1], 4),
 					major,
 					minor,
+					busnum,
+					devnum,
 					devname,
 				)
 				if err != nil {
@@ -1025,6 +1049,8 @@ func deviceLoadUsb() ([]usbDevice, error) {
 			values["idProduct"],
 			parts[0],
 			parts[1],
+			values["busnum"],
+			values["devnum"],
 			values["devname"],
 		)
 		if err != nil {


More information about the lxc-devel mailing list