From 13c564e72a018c38a112e523578e970064d1d53d Mon Sep 17 00:00:00 2001
From: Balazs Lecz <leczb@google.com>
Date: Thu, 8 Jul 2010 18:12:09 +0100
Subject: [PATCH] LXC: Create per-instance log files

This replaces the single global log file with per-instance logs.
The instance log file is not truncated when the instance is started.

Signed-off-by: Balazs Lecz <leczb@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 lib/hypervisor/hv_lxc.py | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/lib/hypervisor/hv_lxc.py b/lib/hypervisor/hv_lxc.py
index 9176ad7fe..ad10ba679 100644
--- a/lib/hypervisor/hv_lxc.py
+++ b/lib/hypervisor/hv_lxc.py
@@ -65,7 +65,6 @@ class LXCHypervisor(hv_base.BaseHypervisor):
 
   """
   _ROOT_DIR = constants.RUN_GANETI_DIR + "/lxc"
-  _LOG_FILE = constants.LOG_DIR + "hv_lxc.log"
   _DEVS = [
     "c 1:3",   # /dev/null
     "c 1:5",   # /dev/zero
@@ -123,6 +122,13 @@ class LXCHypervisor(hv_base.BaseHypervisor):
     """
     return utils.PathJoin(cls._ROOT_DIR, instance_name + ".conf")
 
+  @classmethod
+  def _InstanceLogFile(cls, instance_name):
+    """Return the log file for an instance.
+
+    """
+    return utils.PathJoin(cls._ROOT_DIR, instance_name + ".log")
+
   @classmethod
   def _GetCgroupMountPoint(cls):
     for _, mountpoint, fstype, _ in utils.GetMounts():
@@ -268,6 +274,15 @@ class LXCHypervisor(hv_base.BaseHypervisor):
     conf_file = self._InstanceConfFile(instance.name)
     utils.WriteFile(conf_file, data=self._CreateConfigFile(instance, root_dir))
 
+    log_file = self._InstanceLogFile(instance.name)
+    if not os.path.exists(log_file):
+      try:
+        utils.WriteFile(log_file, data="", mode=constants.SECURE_FILE_MODE)
+      except EnvironmentError, err:
+        raise errors.HypervisorError("Creating hypervisor log file %s for"
+                                     " instance %s failed: %s" %
+                                     (log_file, instance.name, err))
+
     if not os.path.ismount(root_dir):
       if not block_devices:
         raise HypervisorError("LXC needs at least one disk")
@@ -276,9 +291,9 @@ class LXCHypervisor(hv_base.BaseHypervisor):
       result = utils.RunCmd(["mount", sda_dev_path, root_dir])
       if result.failed:
         raise HypervisorError("Can't mount the chroot dir: %s" % result.output)
-    # TODO: replace the global log file with a per-instance log file
     result = utils.RunCmd(["lxc-start", "-n", instance.name,
-                           "-o", self._LOG_FILE, "-l", "DEBUG",
+                           "-o", log_file,
+                           "-l", "DEBUG",
                            "-f", conf_file, "-d"])
     if result.failed:
       raise HypervisorError("Running the lxc-start script failed: %s" %
-- 
GitLab