[lxc-devel] [PATCH 2/2] python: Add new lxc-device tool

Stéphane Graber stgraber at ubuntu.com
Thu Nov 22 23:08:12 UTC 2012


Add a new lxc-device tool which uses the new add_device() function of
the python API and lets you add a new device node to a running container.

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 src/lxc/Makefile.am |  1 +
 src/lxc/lxc-device  | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+)
 create mode 100644 src/lxc/lxc-device

diff --git a/src/lxc/Makefile.am b/src/lxc/Makefile.am
index 9219ba6..de227c7 100644
--- a/src/lxc/Makefile.am
+++ b/src/lxc/Makefile.am
@@ -97,6 +97,7 @@ bin_SCRIPTS = \
 	lxc-destroy
 
 if ENABLE_PYTHON
+    bin_SCRIPTS += lxc-device
     bin_SCRIPTS += lxc-ls
     bin_SCRIPTS += lxc-start-ephemeral
 endif
diff --git a/src/lxc/lxc-device b/src/lxc/lxc-device
new file mode 100644
index 0000000..6c91e67
--- /dev/null
+++ b/src/lxc/lxc-device
@@ -0,0 +1,59 @@
+#!/usr/bin/python3
+#
+# lxc-device: Add devices to a running container
+#
+# This python implementation is based on the work done in the original
+# shell implementation done by Serge Hallyn in Ubuntu (and other contributors)
+#
+# (C) Copyright Canonical Ltd. 2012
+#
+# Authors:
+# Stéphane Graber <stgraber at ubuntu.com>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+
+# NOTE: To remove once the API is stabilized
+import warnings
+warnings.filterwarnings("ignore", "The python-lxc API isn't yet stable")
+
+import argparse
+import gettext
+import lxc
+import sys
+
+_ = gettext.gettext
+gettext.textdomain("lxc-device")
+
+# Begin parsing the command line
+parser = argparse.ArgumentParser(description=_("LXC: Manage devices"),
+                                 formatter_class=argparse.RawTextHelpFormatter)
+
+parser.add_argument("-n", dest="container", metavar="CONTAINER",
+                    help=_("Container to add the device to"), required=True)
+
+parser.add_argument("--add", action="append", default=[], metavar="DEVICE",
+                    help=_("Add a device"), required=True)
+
+args = parser.parse_args()
+
+container = lxc.Container(args.container)
+if not container.running:
+    print("The container must be running.")
+    sys.exit(1)
+
+for device in args.add:
+    container.add_device(device)
+    print("Added '%s' to '%s'." % (device, container.name))
-- 
1.8.0





More information about the lxc-devel mailing list