add pidfile for each used loop device
authorGui Chen <gui.chen@intel.com>
Mon, 21 Jan 2013 15:48:53 +0000 (23:48 +0800)
committerGui Chen <gui.chen@intel.com>
Mon, 21 Jan 2013 17:38:02 +0000 (01:38 +0800)
sometimes mic can't know if the device can be released,
and it will bump up to maxinum used number, now record
an pid file for each used device so that they can be
released when needed

Change-Id: I24308eebd8b2c10a46f2bf20cfdb70bf7f1b04a2
Signed-off-by: Gui Chen <gui.chen@intel.com>
mic/utils/fs_related.py

index 9b8ecee..7614132 100644 (file)
@@ -938,6 +938,8 @@ class LoopDevice(object):
         elif self.loopid:
             os.unlink(self.device)
 
+DEVICE_PIDFILE_DIR = "/var/tmp/mic/device"
+
 def get_loop_device(losetupcmd, lofile):
     import fcntl
     fp = open("/var/lock/__mic_loopdev.lock", 'w')
@@ -960,6 +962,14 @@ def get_loop_device(losetupcmd, lofile):
 
         devinst.reg_atexit()
 
+        # try to save device and pid
+        makedirs(DEVICE_PIDFILE_DIR)
+        pidfile = os.path.join(DEVICE_PIDFILE_DIR, os.path.basename(loopdev))
+        if os.path.exists(pidfile):
+            os.unlink(pidfile)
+        with open(pidfile, 'w') as wf:
+            wf.write(str(os.getpid()))
+
     except MountError, err:
         raise CreatorError("%s" % str(err))
     except: