From e557bae9fb6e3d23172c3a638f8973477a9283d9 Mon Sep 17 00:00:00 2001
From: Guido Trotter <ultrotter@google.com>
Date: Tue, 5 May 2009 10:45:43 +0200
Subject: [PATCH] Inform the OS create script of reinstalls

Sometimes reinstalls are slightly different than new installs. For
example certain partitions may need to be preserved accross reinstalls.
In order to do that on a per-os basis we pass in the INSTANCE_REINSTALL
variable to inform the create script about when a reinstall is
happening.

Signed-off-by: Guido Trotter <ultrotter@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 daemons/ganeti-noded         | 3 ++-
 lib/backend.py               | 6 +++++-
 lib/cmdlib.py                | 4 ++--
 lib/rpc.py                   | 4 ++--
 man/ganeti-os-interface.sgml | 5 +++++
 5 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/daemons/ganeti-noded b/daemons/ganeti-noded
index 3a2b4a092..ac9009084 100755
--- a/daemons/ganeti-noded
+++ b/daemons/ganeti-noded
@@ -365,7 +365,8 @@ class NodeHttpServer(http.server.HttpServer):
     """
     inst_s = params[0]
     inst = objects.Instance.FromDict(inst_s)
-    return backend.InstanceOsAdd(inst)
+    reinstall = params[1]
+    return backend.InstanceOsAdd(inst, reinstall)
 
   @staticmethod
   def perspective_instance_run_rename(params):
diff --git a/lib/backend.py b/lib/backend.py
index 18d439e86..183286b36 100644
--- a/lib/backend.py
+++ b/lib/backend.py
@@ -673,11 +673,13 @@ def GetAllInstancesInfo(hypervisor_list):
   return output
 
 
-def InstanceOsAdd(instance):
+def InstanceOsAdd(instance, reinstall):
   """Add an OS to an instance.
 
   @type instance: L{objects.Instance}
   @param instance: Instance whose OS is to be installed
+  @type reinstall: boolean
+  @param reinstall: whether this is an instance reinstall
   @rtype: boolean
   @return: the success of the operation
 
@@ -693,6 +695,8 @@ def InstanceOsAdd(instance):
               (os_name, os_dir, os_err))
 
   create_env = OSEnvironment(instance)
+  if reinstall:
+    create_env['INSTANCE_REINSTALL'] = "1"
 
   logfile = "%s/add-%s-%s-%d.log" % (constants.LOG_OS_DIR, instance.os,
                                      instance.name, int(time.time()))
diff --git a/lib/cmdlib.py b/lib/cmdlib.py
index ab4044929..c9eae6859 100644
--- a/lib/cmdlib.py
+++ b/lib/cmdlib.py
@@ -2995,7 +2995,7 @@ class LUReinstallInstance(LogicalUnit):
     _StartInstanceDisks(self, inst, None)
     try:
       feedback_fn("Running the instance OS create scripts...")
-      result = self.rpc.call_instance_os_add(inst.primary_node, inst)
+      result = self.rpc.call_instance_os_add(inst.primary_node, inst, True)
       msg = result.RemoteFailMsg()
       if msg:
         raise errors.OpExecError("Could not install OS for instance %s"
@@ -4763,7 +4763,7 @@ class LUCreateInstance(LogicalUnit):
     if iobj.disk_template != constants.DT_DISKLESS:
       if self.op.mode == constants.INSTANCE_CREATE:
         feedback_fn("* running the instance OS create scripts...")
-        result = self.rpc.call_instance_os_add(pnode_name, iobj)
+        result = self.rpc.call_instance_os_add(pnode_name, iobj, False)
         msg = result.RemoteFailMsg()
         if msg:
           raise errors.OpExecError("Could not add os for instance %s"
diff --git a/lib/rpc.py b/lib/rpc.py
index 70dd3128a..599de0ac9 100644
--- a/lib/rpc.py
+++ b/lib/rpc.py
@@ -524,14 +524,14 @@ class RpcRunner(object):
     return self._SingleNodeCall(node, "instance_reboot",
                                 [self._InstDict(instance), reboot_type])
 
-  def call_instance_os_add(self, node, inst):
+  def call_instance_os_add(self, node, inst, reinstall):
     """Installs an OS on the given instance.
 
     This is a single-node call.
 
     """
     return self._SingleNodeCall(node, "instance_os_add",
-                                [self._InstDict(inst)])
+                                [self._InstDict(inst), reinstall])
 
   def call_instance_run_rename(self, node, inst, old_name):
     """Run the OS rename script for an instance.
diff --git a/man/ganeti-os-interface.sgml b/man/ganeti-os-interface.sgml
index 5a127e498..c61ea5ab5 100644
--- a/man/ganeti-os-interface.sgml
+++ b/man/ganeti-os-interface.sgml
@@ -215,6 +215,11 @@
       according to this name. It can configure the IP statically or
       not, depending on the deployment environment.</para>
 
+      <para>The <envar>INSTANCE_REINSTALL</envar> variable is set to '1' when
+      this create request is reinstalling and existing instance, rather than
+      creating one anew. This can be used, for example, to preserve some
+      data in the old instance in an os-specific way.</para>
+
     </refsect2>
 
     <refsect2>
-- 
GitLab