Add support for modifying the kernel/initrd path
authorIustin Pop <iustin@google.com>
Tue, 8 Jan 2008 11:03:11 +0000 (11:03 +0000)
committerIustin Pop <iustin@google.com>
Tue, 8 Jan 2008 11:03:11 +0000 (11:03 +0000)
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
lib/constants.py
lib/opcodes.py
scripts/gnt-instance

index 298fc38..6f1778e 100644 (file)
@@ -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)
 
index 6caae17..47db98e 100644 (file)
@@ -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"
index e44a731..c28423b 100644 (file)
@@ -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
index 07285f0..602c984 100755 (executable)
@@ -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,