mirror of
https://git.proxmox.com/git/mirror_lxc
synced 2025-08-14 20:42:50 +00:00
python: Update to the device related functions
This commit does the following changes to the python API: - Rename the add_device API call to add_device_node - Adds an extra check that the container is running to add_device_node - Introduces a new add_device_net function And the following changes to the lxc-device tool: - Change parser setup to better cope with variable number of arguments - Add support for network devices (currently auto-detected) - Support for different names on the host and in the container Signed-off-by: Stéphane Graber <stgraber@ubuntu.com> Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
This commit is contained in:
parent
b1eafd4aeb
commit
20cf2e976b
@ -42,24 +42,54 @@ gettext.textdomain("lxc-device")
|
|||||||
parser = argparse.ArgumentParser(description=_("LXC: Manage devices"),
|
parser = argparse.ArgumentParser(description=_("LXC: Manage devices"),
|
||||||
formatter_class=argparse.RawTextHelpFormatter)
|
formatter_class=argparse.RawTextHelpFormatter)
|
||||||
|
|
||||||
|
# Global arguments
|
||||||
parser.add_argument("-n", dest="container", metavar="CONTAINER",
|
parser.add_argument("-n", dest="container", metavar="CONTAINER",
|
||||||
help=_("Container to add the device to"), required=True)
|
help=_("Name of the container to add the device to"),
|
||||||
|
required=True)
|
||||||
|
|
||||||
parser.add_argument("--add", action="append", default=[], metavar="DEVICE",
|
# Commands
|
||||||
help=_("Add a device"), required=True)
|
subparsers = parser.add_subparsers()
|
||||||
|
subparser_add = subparsers.add_parser('add', help=_('Add a device'))
|
||||||
|
subparser_add.set_defaults(action="add")
|
||||||
|
|
||||||
|
subparser_add.add_argument(dest="device", metavar="DEVICE",
|
||||||
|
help=_("Add a device "
|
||||||
|
"(path to a node or interface name)"))
|
||||||
|
|
||||||
|
subparser_add.add_argument(dest="name", metavar="NAME", nargs="?",
|
||||||
|
help=_("Use an alternative path or name "
|
||||||
|
"in the container"))
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
# The user needs to be uid 0
|
# Some basic checks
|
||||||
|
if not hasattr(args, "action"):
|
||||||
|
parser.error(_("You must specify an action."))
|
||||||
|
|
||||||
|
## The user needs to be uid 0
|
||||||
if not os.geteuid() == 0:
|
if not os.geteuid() == 0:
|
||||||
parser.error(_("You must be root to run this script. Try running: sudo %s"
|
parser.error(_("You must be root to run this script. Try running: sudo %s"
|
||||||
% (sys.argv[0])))
|
% (sys.argv[0])))
|
||||||
|
|
||||||
|
## Don't rename if no alternative name
|
||||||
|
if not args.name:
|
||||||
|
args.name = args.device
|
||||||
|
|
||||||
|
## Check that the container is ready
|
||||||
container = lxc.Container(args.container)
|
container = lxc.Container(args.container)
|
||||||
if not container.running:
|
if not container.running:
|
||||||
print("The container must be running.")
|
parser.error("The container must be running.")
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
for device in args.add:
|
# Do the work
|
||||||
container.add_device(device)
|
if args.action == "add":
|
||||||
print("Added '%s' to '%s'." % (device, container.name))
|
if os.path.exists("/sys/class/net/%s/" % args.device):
|
||||||
|
ret = container.add_device_net(args.device, args.name)
|
||||||
|
else:
|
||||||
|
ret = container.add_device_node(args.device, args.name)
|
||||||
|
|
||||||
|
if ret:
|
||||||
|
print("Added '%s' to '%s' as '%s'." %
|
||||||
|
(args.device, container.name, args.name))
|
||||||
|
else:
|
||||||
|
print("Failed to add '%s' to '%s' as '%s'." %
|
||||||
|
(args.device, container.name, args.name))
|
||||||
|
@ -154,11 +154,14 @@ class Container(_lxc.Container):
|
|||||||
_lxc.Container.__init__(self, name)
|
_lxc.Container.__init__(self, name)
|
||||||
self.network = ContainerNetworkList(self)
|
self.network = ContainerNetworkList(self)
|
||||||
|
|
||||||
def add_device(self, path, destpath=None):
|
def add_device_node(self, path, destpath=None):
|
||||||
"""
|
"""
|
||||||
Add device to running container.
|
Add block/char device to running container.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if not self.running:
|
||||||
|
return False
|
||||||
|
|
||||||
if not destpath:
|
if not destpath:
|
||||||
destpath = path
|
destpath = path
|
||||||
|
|
||||||
@ -214,6 +217,25 @@ class Container(_lxc.Container):
|
|||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def add_device_net(self, name, destname=None):
|
||||||
|
"""
|
||||||
|
Add network device to running container.
|
||||||
|
"""
|
||||||
|
|
||||||
|
if not self.running:
|
||||||
|
return False
|
||||||
|
|
||||||
|
if not destname:
|
||||||
|
destname = name
|
||||||
|
|
||||||
|
if not os.path.exists("/sys/class/net/%s/" % name):
|
||||||
|
return False
|
||||||
|
|
||||||
|
return subprocess.call(['ip', 'link', 'set',
|
||||||
|
'dev', name,
|
||||||
|
'netns', str(self.init_pid),
|
||||||
|
'name', destname]) == 0
|
||||||
|
|
||||||
def append_config_item(self, key, value):
|
def append_config_item(self, key, value):
|
||||||
"""
|
"""
|
||||||
Append 'value' to 'key', assuming 'key' is a list.
|
Append 'value' to 'key', assuming 'key' is a list.
|
||||||
|
Loading…
Reference in New Issue
Block a user