The following pull request was submitted through Github.
It can be accessed and reviewed at: https://github.com/lxc/lxd/pull/2315

This e-mail was sent by the LXC bot, direct replies will not reach the author
unless they happen to be subscribed to this list.

=== Description (from pull-request) ===
* do a deep copy of the device entry so config doesn't accidentally get
  saved in the DB
* remember to remove the bus directory when a USB device goes away
* remove unused param to insertUnixDevice()

Closes #2306
Closes #2312

Signed-off-by: Tycho Andersen <tycho.ander...@canonical.com>
From 9e86e8f6e5cff63f10744f4847f501f623b39320 Mon Sep 17 00:00:00 2001
From: Tycho Andersen <tycho.ander...@canonical.com>
Date: Thu, 25 Aug 2016 10:56:57 -0400
Subject: [PATCH] minor USB fixes

* do a deep copy of the device entry so config doesn't accidentally get
  saved in the DB
* remember to remove the bus directory when a USB device goes away
* remove unused param to insertUnixDevice()

Closes #2306
Closes #2312

Signed-off-by: Tycho Andersen <tycho.ander...@canonical.com>
---
 lxd/container_lxc.go | 80 +++++++++++++++++++++++++++++++++++++---------------
 lxd/devices.go       | 14 ++-------
 2 files changed, 59 insertions(+), 35 deletions(-)

diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index 67d0068..50a1ec9 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -1130,12 +1130,17 @@ func (c *containerLXC) startCommon() (string, error) {
                                        }
                                }
 
-                               m["major"] = fmt.Sprintf("%d", usb.major)
-                               m["minor"] = fmt.Sprintf("%d", usb.minor)
-                               m["path"] = usb.path
+                               temp := shared.Device{}
+                               if err := shared.DeepCopy(&m, &temp); err != 
nil {
+                                       return "", err
+                               }
+
+                               temp["major"] = fmt.Sprintf("%d", usb.major)
+                               temp["minor"] = fmt.Sprintf("%d", usb.minor)
+                               temp["path"] = usb.path
 
                                /* it's ok to fail, the device might be hot 
plugged later */
-                               _, err := c.createUnixDevice(m)
+                               _, err := c.createUnixDevice(temp)
                                if err != nil {
                                        shared.Log.Debug("failed to create usb 
device", log.Ctx{"err": err, "device": k})
                                        continue
@@ -2516,27 +2521,17 @@ func (c *containerLXC) Update(args containerArgs, 
userRequested bool) error {
                                                continue
                                        }
 
-                                       m["major"] = fmt.Sprintf("%d", 
usb.major)
-                                       m["minor"] = fmt.Sprintf("%d", 
usb.minor)
-                                       m["path"] = usb.path
-
-                                       err = c.removeUnixDevice(m)
+                                       err := c.removeUSBDevice(m, usb)
                                        if err != nil {
-                                               shared.Log.Error("failed to 
remove usb device", log.Ctx{"err": err, "usb": usb, "container": c.Name()})
+                                               return err
                                        }
-
-                                       /* ok to fail here, there may be other 
usb
-                                        * devices on this bus still left in the
-                                        * container
-                                        */
-                                       os.Remove(filepath.Dir(usb.path))
                                }
                        }
                }
 
                for k, m := range addDevices {
                        if shared.StringInSlice(m["type"], 
[]string{"unix-char", "unix-block"}) {
-                               err = c.insertUnixDevice(k, m)
+                               err = c.insertUnixDevice(m)
                                if err != nil {
                                        return err
                                }
@@ -2563,11 +2558,7 @@ func (c *containerLXC) Update(args containerArgs, 
userRequested bool) error {
                                                continue
                                        }
 
-                                       m["major"] = fmt.Sprintf("%d", 
usb.major)
-                                       m["minor"] = fmt.Sprintf("%d", 
usb.minor)
-                                       m["path"] = usb.path
-
-                                       err = c.insertUnixDevice(k, m)
+                                       err = c.insertUSBDevice(m, usb)
                                        if err != nil {
                                                shared.Log.Error("failed to 
insert usb device", log.Ctx{"err": err, "usb": usb, "container": c.Name()})
                                        }
@@ -3788,7 +3779,7 @@ func (c *containerLXC) createUnixDevice(m shared.Device) 
(string, error) {
        return devPath, nil
 }
 
-func (c *containerLXC) insertUnixDevice(name string, m shared.Device) error {
+func (c *containerLXC) insertUnixDevice(m shared.Device) error {
        // Check that the container is running
        if !c.IsRunning() {
                return fmt.Errorf("Can't insert device into stopped container")
@@ -3822,6 +3813,19 @@ func (c *containerLXC) insertUnixDevice(name string, m 
shared.Device) error {
        return nil
 }
 
+func (c *containerLXC) insertUSBDevice(m shared.Device, usb usbDevice) error {
+       temp := shared.Device{}
+       if err := shared.DeepCopy(&m, &temp); err != nil {
+               return err
+       }
+
+       temp["major"] = fmt.Sprintf("%d", usb.major)
+       temp["minor"] = fmt.Sprintf("%d", usb.minor)
+       temp["path"] = usb.path
+
+       return c.insertUnixDevice(temp)
+}
+
 func (c *containerLXC) removeUnixDevice(m shared.Device) error {
        // Check that the container is running
        pid := c.InitPID()
@@ -3876,6 +3880,36 @@ func (c *containerLXC) removeUnixDevice(m shared.Device) 
error {
        return nil
 }
 
+func (c *containerLXC) removeUSBDevice(m shared.Device, usb usbDevice) error {
+       pid := c.InitPID()
+       if pid == -1 {
+               return fmt.Errorf("Can't remove device from stopped container")
+       }
+
+       temp := shared.Device{}
+       if err := shared.DeepCopy(&m, &temp); err != nil {
+               return err
+       }
+
+       temp["major"] = fmt.Sprintf("%d", usb.major)
+       temp["minor"] = fmt.Sprintf("%d", usb.minor)
+       temp["path"] = usb.path
+
+       err := c.removeUnixDevice(temp)
+       if err != nil {
+               shared.Log.Error("failed to remove usb device", log.Ctx{"err": 
err, "usb": usb, "container": c.Name()})
+               return err
+       }
+
+       /* ok to fail here, there may be other usb
+        * devices on this bus still left in the
+        * container
+        */
+       dir := fmt.Sprintf("/proc/%d/root/%s", pid, filepath.Dir(usb.path))
+       os.Remove(dir)
+       return nil
+}
+
 func (c *containerLXC) removeUnixDevices() error {
        // Check that we indeed have devices to remove
        if !shared.PathExists(c.DevicesPath()) {
diff --git a/lxd/devices.go b/lxd/devices.go
index ab51902..c2d4d78 100644
--- a/lxd/devices.go
+++ b/lxd/devices.go
@@ -491,28 +491,18 @@ func deviceUSBEvent(d *Daemon, usb usbDevice) {
                                continue
                        }
 
-                       m["major"] = fmt.Sprintf("%d", usb.major)
-                       m["minor"] = fmt.Sprintf("%d", usb.minor)
-                       m["path"] = usb.path
-
                        if usb.action == "add" {
-                               err := c.insertUnixDevice("unused", m)
+                               err := c.insertUSBDevice(m, usb)
                                if err != nil {
                                        shared.Log.Error("failed to create usb 
device", log.Ctx{"err": err, "usb": usb, "container": c.Name()})
                                        return
                                }
                        } else if usb.action == "remove" {
-                               err := c.removeUnixDevice(m)
+                               err := c.removeUSBDevice(m, usb)
                                if err != nil {
                                        shared.Log.Error("failed to remove usb 
device", log.Ctx{"err": err, "usb": usb, "container": c.Name()})
                                        return
                                }
-
-                               /* ok to fail here, there may be other usb
-                                * devices on this bus still left in the
-                                * container
-                                */
-                               os.Remove(filepath.Dir(usb.path))
                        } else {
                                shared.Log.Error("unknown action for usb 
device", log.Ctx{"usb": usb})
                                continue
_______________________________________________
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to