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 diskinfo\n"
"\tlvmstrap [--vgname=NAME] [--allow-removable]"
Returns:
(options, args), as returned by OptionParser.parse_args
"""
- global verbose_flag
+ global verbose_flag # pylint: disable-msg=W0603
parser = optparse.OptionParser(usage="\n%s" % USAGE,
version="%%prog (ganeti) %s" %
parser.add_option("-d", "--disks", dest="disks",
help="Choose disks (e.g. hda,hdg)",
metavar="DISKLIST")
- parser.add_option("-v", "--verbose",
- action="store_true", dest="verbose", default=False,
- help="print command execution messages to stdout")
+ parser.add_option(cli.VERBOSE_OPT)
parser.add_option("-r", "--allow-removable",
action="store_true", dest="removable_ok", default=False,
help="allow and use removable devices too")
if os.getuid() != 0:
raise PrereqError("This tool runs as root only. Really.")
- osname, nodename, release, version, arch = os.uname()
+ osname, _, release, _, _ = os.uname()
if osname != 'Linux':
raise PrereqError("This tool only runs on Linux"
" (detected OS: %s)." % osname)
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
"""
path = "/dev/%s" % name
- for retries in range(40):
+ for _ in range(40):
if os.path.exists(path):
break
time.sleep(0.250)
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)
+ _, mountpoint, fstype, _ = line.split(None, 3)
# fs type blacklist
if fstype in ["nfs", "nfs4", "autofs", "tmpfs", "proc", "sysfs"]:
continue
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
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.
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
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:
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):
boolean, the in-use status of the device
"""
- for retries in range(3):
+ for _ in range(3):
result = ExecCommand("blockdev --rereadpt /dev/%s" % name)
if not result.failed:
break
" non-removable block devices).")
sysd_free = []
sysd_used = []
- for name, size, dev, part, used in sysdisks:
+ for name, _, _, _, used in sysdisks:
if used:
sysd_used.append(name)
else:
CreatePVOnDisk(disk)
CreateVG(vgname, disklist)
- status, lv_count, size, free = CheckVGExists(vgname)
+ status, lv_count, size, _ = CheckVGExists(vgname)
if status:
print "Done! %s: size %s GiB, disks: %s" % (vgname, size,
",".join(disklist))