3 Gathers availability data for the VM host it's running on.
6 from subprocess import PIPE, Popen, call
13 Calculate the amount of memory available for new VMs
14 The numbers returned by xm info and xm info -c are in MiB
15 The numbers in /proc/xen/balloon are in KiB
16 All math is done in kibibytes for consistency
19 Bail if /etc/invirt/nocreate exists
22 os.stat('/etc/invirt/nocreate')
28 p = Popen(['/usr/sbin/xm', 'info'], stdout=PIPE)
29 output = p.communicate()[0]
31 raise RuntimeError("Command '%s' returned non-zero exit status %d"
32 % ('/usr/sbin/xm info', p.returncode))
33 xminfo = yaml.load(output, yaml.CSafeLoader)
35 free_memory = int(xminfo['free_memory']) * 1024
37 currentallocation = int(open('/sys/devices/system/xen_memory/xen_memory0/info/current_kb', 'r').read())
38 # this information seems to have vanished in recent linux - we _want_ min_target_kb, but it doesn't seem to be
40 # minimumtarget = int(open('/sys/devices/system/xen_memory/xen_memory0/target_kb', 'r').read())
41 p = Popen(['/usr/sbin/xm', 'info', '-c'], stdout=PIPE)
42 output = p.communicate()[0]
44 raise RuntimeError("Command '%s' returned non-zero exit status %d"
45 % ('/usr/sbin/xm info -c', p.returncode))
46 xminfoc = yaml.load(output, yaml.CSafeLoader)
49 dom0minmem = int(xminfoc['dom0-min-mem']) * 1024
51 # dom0_spare_memory = currentallocation - max(minimumtarget, dom0minmem)
52 dom0_spare_memory = currentallocation - dom0minmem
53 print int((free_memory + dom0_spare_memory)/1024)
56 if __name__ == '__main__':
57 sys.exit(main(sys.argv))