lvmstrap: add explicit test for swap backends
authorIustin Pop <iustin@google.com>
Wed, 19 Jan 2011 13:49:50 +0000 (14:49 +0100)
committerIustin Pop <iustin@google.com>
Thu, 20 Jan 2011 12:05:37 +0000 (13:05 +0100)
Similar to mounted filesystems, recent kernel/userland report swap
backends:

    root@node4:~# fuser -avm /dev/sda6
                         USER        PID ACCESS COMMAND
    /dev/sda6:           root     kernel swap  /dev/sda6

But old ones not:

    node1# fuser -avm /dev/sda6
                         USER        PID ACCESS COMMAND
    /dev/sda6:

So we add an explicit test for swap backends using /proc/swaps.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>

tools/lvmstrap

index f2847cf..7f6d344 100755 (executable)
@@ -489,6 +489,14 @@ def GetMountInfo():
   return mounts
 
 
+def GetSwapInfo():
+  """Reads /proc/swaps and returns the list of swap backing stores.
+
+  """
+  swaplines = ReadFile("/proc/swaps").splitlines()[1:]
+  return [line.split(None, 1)[0] for line in swaplines]
+
+
 def DevInfo(name, dev, mountinfo):
   """Computes miscellaneous information about a block device.
 
@@ -637,12 +645,20 @@ def CheckMounted(name):
   return dev not in minfo
 
 
+def CheckSwap(name):
+  """Check to see if a block device is being used as swap.
+
+  """
+  name = "/dev/%s" % name
+  return name not in GetSwapInfo()
+
+
 def InUse(name):
   """Returns if a disk is in use or not.
 
   """
   return not (CheckSysfsHolders(name) and CheckReread(name) and
-              CheckMounted(name))
+              CheckMounted(name) and CheckSwap(name))
 
 
 def WipeDisk(name):