QA: remove the --default-hypervisor option
[ganeti-local] / tools / lvmstrap
index 153de88..73b4834 100755 (executable)
@@ -44,11 +44,13 @@ import sys
 import optparse
 import time
 
-from ganeti.utils import RunCmd
+from ganeti.utils import RunCmd, ReadFile
 from ganeti import constants
+from ganeti import cli
 
-USAGE = ("\tlvmstrap.py diskinfo\n"
-         "\tlvmstrap.py [--vgname=NAME] { --alldisks | --disks DISKLIST }"
+USAGE = ("\tlvmstrap diskinfo\n"
+         "\tlvmstrap [--vgname=NAME] [--allow-removable]"
+         " { --alldisks | --disks DISKLIST }"
          " create")
 
 verbose_flag = False
@@ -110,6 +112,7 @@ class ParameterError(Error):
   """
   pass
 
+
 def Usage():
   """Shows program usage information and exits the program."""
 
@@ -142,6 +145,9 @@ def ParseOptions():
   parser.add_option("-v", "--verbose",
                     action="store_true", dest="verbose", default=False,
                     help="print command execution messages to stdout")
+  parser.add_option("-r", "--allow-removable",
+                    action="store_true", dest="removable_ok", default=False,
+                    help="allow and use removable devices too")
   parser.add_option("-g", "--vg-name", type="string",
                     dest="vgname", default="xenvg", metavar="NAME",
                     help="the volume group to be created [default: xenvg]")
@@ -262,7 +268,7 @@ def CheckSysDev(name, devnum):
    devnum: the device number, e.g. 0x803 (2051 in decimal) for sda3
 
   Returns:
-    None; failure of the check is signalled by raising a
+    None; failure of the check is signaled by raising a
       SysconfigError exception
   """
 
@@ -352,7 +358,7 @@ def ReadPV(name):
   return vgname
 
 
-def GetDiskList():
+def GetDiskList(opts):
   """Computes the block device list for this system.
 
   This function examines the /sys/block tree and using information
@@ -385,7 +391,7 @@ def GetDiskList():
     removable = int(f.read().strip())
     f.close()
 
-    if removable:
+    if removable and not opts.removable_ok:
       continue
 
     dev = ReadDev("/sys/block/%s" % name)
@@ -419,9 +425,7 @@ def GetMountInfo():
    a mountpoint: device number dictionary
   """
 
-  f = open("/proc/mounts", "r")
-  mountlines = f.readlines()
-  f.close()
+  mountlines = ReadFile("/proc/mounts").splitlines()
   mounts = {}
   for line in mountlines:
     device, mountpoint, fstype, rest = line.split(None, 3)
@@ -444,7 +448,7 @@ def GetMountInfo():
 
 
 def DevInfo(name, dev, mountinfo):
-  """Computes miscellaneous informations about a block device.
+  """Computes miscellaneous information about a block device.
 
   Args:
     name: the device name, e.g. sda
@@ -470,20 +474,27 @@ def DevInfo(name, dev, mountinfo):
   return mpath, whatvg, fileinfo
 
 
-def ShowDiskInfo():
+def ShowDiskInfo(opts):
   """Shows a nicely formatted block device list for this system.
 
-  This function shows the user a table with the informations gathered
+  This function shows the user a table with the information gathered
   by the other functions defined, in order to help the user make a
   choice about which disks should be allocated to our volume group.
 
   """
   mounts = GetMountInfo()
-  dlist = GetDiskList()
+  dlist = GetDiskList(opts)
 
   print "------- Disk information -------"
-  print ("%5s %7s %4s %5s %-10s %s" %
-         ("Name", "Size[M]", "Used", "Mount", "LVM?", "Info"))
+  headers = {
+      "name": "Name",
+      "size": "Size[M]",
+      "used": "Used",
+      "mount": "Mount",
+      "lvm": "LVM?",
+      "info": "Info"
+      }
+  fields = ["name", "size", "used", "mount", "lvm", "info"]
 
   flatlist = []
   # Flatten the [(disk, [partition,...]), ...] list
@@ -496,6 +507,7 @@ def ShowDiskInfo():
     for partname, partsize, partdev in parts:
       flatlist.append((partname, partsize, partdev, ""))
 
+  strlist = []
   for name, size, dev, in_use in flatlist:
     mp, vgname, fileinfo = DevInfo(name, dev, mounts)
     if mp is None:
@@ -510,8 +522,15 @@ def ShowDiskInfo():
     if len(name) > 3:
       # Indent partitions
       name = " %s" % name
-    print ("%-5s %7.2f %-4s %-5s %-10s %s" %
-           (name, float(size) / 1024 / 1024, in_use, mp, lvminfo, fileinfo))
+
+    strlist.append([name, "%.2f" % (float(size) / 1024 / 1024),
+                    in_use, mp, lvminfo, fileinfo])
+
+  data = cli.GenerateTable(headers, fields, None,
+                           strlist, numfields=["size"])
+
+  for line in data:
+    print line
 
 
 def CheckReread(name):
@@ -649,7 +668,7 @@ def ValidateDiskList(options):
     a list of disk names, e.g. ['sda', 'sdb']
   """
 
-  sysdisks = GetDiskList()
+  sysdisks = GetDiskList(options)
   if not sysdisks:
     raise PrereqError("no disks found (I looked for"
                       " non-removable block devices).")
@@ -688,7 +707,7 @@ def BootStrap():
   vgname = options.vgname
   command = args.pop(0)
   if command == "diskinfo":
-    ShowDiskInfo()
+    ShowDiskInfo(options)
     return
   if command != "create":
     Usage()