3 from invirt import database as db
4 from invirt.config import structs as config
5 import sqlalchemy as sa
6 from subprocess import Popen, PIPE
10 # First figure out what the database thinks our sets of disks and sizes are
12 s = sa.sql.select([db.machine_table.c.name, db.disk_table.c.guest_device_name,
13 db.disk_table.c.size],
14 db.machine_table.c.machine_id == db.disk_table.c.machine_id,
15 [db.machine_table, db.disk_table])
16 dbresult = db.session.execute(s)
18 for (machine, device, size) in dbresult.fetchall():
19 db_disks['d_' + machine + '_' + device] = int(size)
21 # Now figure out what the disk images actually in existence look like
22 p = Popen(['/sbin/lvs', '--noheadings', '-o', 'lv_name,lv_size', '--units', 'm',
23 '--unbuffered', '--nosuffix', '--separator', ':', config.lvm.vg],
25 lvresult = p.communicate()[0]
27 for lv in lvresult.split():
28 (lvname, size) = lv.split(':')
29 if not lvname.startswith(prefix):
31 # We only want the whole number of MiB, to match the database
32 lv_disks[lvname] = int(float(size))
36 size_discrepancies = []
39 if disk not in lv_disks:
40 missing_disks.append(disk)
41 elif db_disks[disk] != lv_disks[disk]:
42 size_discrepancies.append(disk)
44 if disk not in db_disks:
45 orphaned_disks.append(disk)
48 print '===== The following disks are in the DB but the LVs do not exist'
50 for disk in missing_disks:
51 print disk.ljust(45) + 'DB: %s' % str(db_disks[disk]).rjust(6)
54 print '===== The following LVs exist but are not assigned to a VM'
56 for disk in orphaned_disks:
57 print disk.ljust(59) + 'LVM: %s' % str(lv_disks[disk]).rjust(6)
59 if size_discrepancies:
60 print '===== The following disks have different sizes in the DB and LVM'
61 size_discrepancies.sort()
62 for disk in size_discrepancies:
63 print disk.ljust(45) +\
64 'DB: %s LVM: %s' % (str(db_disks[disk]).rjust(6),
65 str(lv_disks[disk]).rjust(6))
67 print 'There are %i inconsistencies between the DB and LVM at this time.' %\
68 (missing_disks.__len__() +
69 orphaned_disks.__len__() +
70 size_discrepancies.__len__())