From 973d786722dbddcbf2efd88020d5ecb259a87e7d Mon Sep 17 00:00:00 2001 From: Iustin Pop <iustin@google.com> Date: Tue, 8 Jan 2008 11:03:11 +0000 Subject: [PATCH] Add support for modifying the kernel/initrd path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch adds support in βgnt-instance modifyβ to set the kernel and initrd paths. The user can pass either 'default' or 'none' (none is not valid for kernel). Reviewed-by: imsnah --- lib/cmdlib.py | 30 +++++++++++++++++++++++++++++- lib/constants.py | 3 +++ lib/opcodes.py | 5 ++++- scripts/gnt-instance | 41 +++++++++++++++++++++++++++++++++++++++-- 4 files changed, 75 insertions(+), 4 deletions(-) diff --git a/lib/cmdlib.py b/lib/cmdlib.py index 298fc3882..6f1778e52 100644 --- a/lib/cmdlib.py +++ b/lib/cmdlib.py @@ -4101,7 +4101,11 @@ class LUSetInstanceParms(LogicalUnit): self.ip = getattr(self.op, "ip", None) self.mac = getattr(self.op, "mac", None) self.bridge = getattr(self.op, "bridge", None) - if [self.mem, self.vcpus, self.ip, self.bridge, self.mac].count(None) == 5: + self.kernel_path = getattr(self.op, "kernel_path", None) + self.initrd_path = getattr(self.op, "initrd_path", None) + all_parms = [self.mem, self.vcpus, self.ip, self.bridge, self.mac, + self.kernel_path, self.initrd_path] + if all_parms.count(None) == len(all_parms): raise errors.OpPrereqError("No changes submitted") if self.mem is not None: try: @@ -4130,6 +4134,24 @@ class LUSetInstanceParms(LogicalUnit): if not utils.IsValidMac(self.mac): raise errors.OpPrereqError('Invalid MAC address %s' % self.mac) + if self.kernel_path is not None: + self.do_kernel_path = True + if self.kernel_path == constants.VALUE_NONE: + raise errors.OpPrereqError("Can't set instance to no kernel") + + if self.kernel_path != constants.VALUE_DEFAULT: + if not os.path.isabs(self.kernel_path): + raise errors.OpPrereError("The kernel path must be an absolute" + " filename") + + if self.initrd_path is not None: + self.do_initrd_path = True + if self.initrd_path not in (constants.VALUE_NONE, + constants.VALUE_DEFAULT): + if not os.path.isabs(self.kernel_path): + raise errors.OpPrereError("The initrd path must be an absolute" + " filename") + instance = self.cfg.GetInstanceInfo( self.cfg.ExpandInstanceName(self.op.instance_name)) if instance is None: @@ -4161,6 +4183,12 @@ class LUSetInstanceParms(LogicalUnit): if self.mac: instance.nics[0].mac = self.mac result.append(("mac", self.mac)) + if self.do_kernel_path: + instance.kernel_path = self.kernel_path + result.append(("kernel_path", self.kernel_path)) + if self.do_initrd_path: + instance.initrd_path = self.initrd_path + result.append(("initrd_path", self.initrd_path)) self.cfg.AddInstance(instance) diff --git a/lib/constants.py b/lib/constants.py index 6caae1757..47db98e5e 100644 --- a/lib/constants.py +++ b/lib/constants.py @@ -60,6 +60,9 @@ EXPORT_CONF_FILE = "config.ini" XEN_KERNEL = _autoconf.XEN_KERNEL XEN_INITRD = _autoconf.XEN_INITRD +VALUE_DEFAULT = "default" +VALUE_NONE = "none" + # hooks-related constants HOOKS_BASE_DIR = _autoconf.SYSCONFDIR + "/ganeti/hooks" HOOKS_PHASE_PRE = "pre" diff --git a/lib/opcodes.py b/lib/opcodes.py index e44a731a5..c28423b9f 100644 --- a/lib/opcodes.py +++ b/lib/opcodes.py @@ -260,7 +260,10 @@ class OpQueryInstanceData(OpCode): class OpSetInstanceParms(OpCode): """Change the parameters of an instance.""" OP_ID = "OP_INSTANCE_SET_PARMS" - __slots__ = ["instance_name", "mem", "vcpus", "ip", "bridge", "mac"] + __slots__ = [ + "instance_name", "mem", "vcpus", "ip", "bridge", "mac", + "kernel_path", "initrd_path", + ] # OS opcodes diff --git a/scripts/gnt-instance b/scripts/gnt-instance index 07285f09c..602c9846d 100755 --- a/scripts/gnt-instance +++ b/scripts/gnt-instance @@ -134,6 +134,29 @@ def _ConfirmOperation(inames, text): return choice +def _TransformPath(user_input): + """Transform a user path into a canonical value. + + This function transforms the a path passed as textual information + into the constants that the LU code expects. + + """ + if user_input: + if user_input.lower() == "default": + result_path = constants.VALUE_DEFAULT + elif user_input.lower() == "none": + result_path = constants.VALUE_NONE + else: + if not os.path.isabs(user_input): + raise errors.OpPrereqError("Path '%s' is not an absolute filename" % + user_input) + result_path = user_input + else: + result_path = constants.VALUE_DEFAULT + + return result_path + + def ListInstances(opts, args): """List instances and their properties. @@ -627,13 +650,19 @@ def SetInstanceParms(opts, args): mac - the new MAC address of the instance """ - if not (opts.mem or opts.vcpus or opts.ip or opts.bridge or opts.mac): + if not (opts.mem or opts.vcpus or opts.ip or opts.bridge or opts.mac or + opts.kernel_path or opts.initrd_path): logger.ToStdout("Please give at least one of the parameters.") return 1 + kernel_path = _TransformPath(opts.kernel_path) + initrd_path = _TransformPath(opts.initrd_path) + op = opcodes.OpSetInstanceParms(instance_name=args[0], mem=opts.mem, vcpus=opts.vcpus, ip=opts.ip, - bridge=opts.bridge, mac=opts.mac) + bridge=opts.bridge, mac=opts.mac, + kernel_path=opts.kernel_path, + initrd_path=opts.initrd_path) result = SubmitOpCode(op) if result: @@ -815,6 +844,14 @@ commands = { make_option("--mac", dest="mac", help="MAC address", default=None, type="string", metavar="<MACADDRESS>"), + make_option("--kernel", dest="kernel_path", + help="Path to the instances' kernel (or" + " 'default')", default=None, + type="string", metavar="<FILENAME>"), + make_option("--initrd", dest="initrd_path", + help="Path to the instances' initrd (or 'none', or" + " 'default')", default=None, + type="string", metavar="<FILENAME>"), ], "<instance>", "Alters the parameters of an instance"), 'shutdown': (ShutdownInstance, ARGS_ANY, -- GitLab