From 705ee6df9cc8517c8947e017c090a1f3859333c5 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Wed, 19 Jan 2011 14:49:50 +0100
Subject: [PATCH] lvmstrap: add explicit test for swap backends

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 | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/tools/lvmstrap b/tools/lvmstrap
index f2847cff9..7f6d34410 100755
--- a/tools/lvmstrap
+++ b/tools/lvmstrap
@@ -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):
-- 
GitLab