Instance stash file is used to keep information that is necessary to
complete instance destruction, until the instance is stopped.
At this point, the name of loopback device allocated to create the block
device partition mapping, is a concrete example of this kind of
information.

Signed-off-by: Yuto KAWAMURA(kawamuray) <[email protected]>
---
 lib/hypervisor/hv_lxc.py | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/lib/hypervisor/hv_lxc.py b/lib/hypervisor/hv_lxc.py
index 1442372..c21a4df 100644
--- a/lib/hypervisor/hv_lxc.py
+++ b/lib/hypervisor/hv_lxc.py
@@ -32,6 +32,7 @@ from ganeti import errors # pylint: disable=W0611
 from ganeti import utils
 from ganeti import objects
 from ganeti import pathutils
+from ganeti import serializer
 from ganeti.hypervisor import hv_base
 from ganeti.errors import HypervisorError
 
@@ -118,6 +119,41 @@ class LXCHypervisor(hv_base.BaseHypervisor):
     return utils.PathJoin(cls._ROOT_DIR, instance_name + ".log")
 
   @classmethod
+  def _InstanceStashFilePath(cls, instance_name):
+    """Return the stash file path for an instance.
+
+    Stash file is used to keep information that needs to complete
+    instance destruction during instance life.
+
+    """
+    return utils.PathJoin(cls._ROOT_DIR, instance_name + ".stash")
+
+  def _SaveInstanceStash(self, instance_name, data):
+    """Save data to instance stash file in serialized format.
+
+    """
+    stash_file = self._InstanceStashFilePath(instance_name)
+    serialized = serializer.Dump(data)
+    try:
+      utils.WriteFile(stash_file, data=serialized,
+                      mode=constants.SECURE_FILE_MODE)
+    except EnvironmentError, err:
+      raise HypervisorError("Failed to save instance stash file %s : %s" %
+                            (stash_file, err))
+
+  def _LoadInstanceStash(self, instance_name):
+    """Load information stashed in file which was created by
+    L{_SaveInstanceStash}.
+
+    """
+    stash_file = self._InstanceStashFilePath(instance_name)
+    try:
+      return serializer.Load(utils.ReadFile(stash_file))
+    except EnvironmentError, err:
+      raise HypervisorError("Failed to load instance stash file %s : %s" %
+                            (stash_file, err))
+
+  @classmethod
   def _GetCgroupMountPoint(cls):
     for _, mountpoint, fstype, _ in utils.GetMounts():
       if fstype == "cgroup":
-- 
1.8.5.5

Reply via email to