3 Gathers availability data for the VM host it's running on.
6 from subprocess import PIPE, Popen, call
11 # return the amount of memory in kibibytes represented by s
13 num, unit = s.split(' ')
14 assert unit == 'kB', "unexpected unit"
19 Calculate the amount of memory available for new VMs
20 The numbers returned by xm info and xm info -c are in MiB
21 The numbers in /proc/xen/balloon are in KiB
22 All math is done in kibibytes for consistency
25 Bail if /etc/invirt/nocreate exists
28 os.stat('/etc/invirt/nocreate')
34 p = Popen(['/usr/sbin/xm', 'info'], stdout=PIPE)
35 output = p.communicate()[0]
37 raise RuntimeError("Command '%s' returned non-zero exit status %d"
38 % ('/usr/sbin/xm info', p.returncode))
39 xminfo = yaml.load(output, yaml.CSafeLoader)
41 free_memory = int(xminfo['free_memory']) * 1024
43 ballooninfo = yaml.load(open('/proc/xen/balloon', 'r').read())
44 currentallocation = parseUnits(ballooninfo['Current allocation'])
45 minimumtarget = parseUnits(ballooninfo['Minimum target'])
47 p = Popen(['/usr/sbin/xm', 'info', '-c'], stdout=PIPE)
48 output = p.communicate()[0]
50 raise RuntimeError("Command '%s' returned non-zero exit status %d"
51 % ('/usr/sbin/xm info -c', p.returncode))
52 xminfoc = yaml.load(output, yaml.CSafeLoader)
55 dom0minmem = int(xminfoc['dom0-min-mem']) * 1024
57 dom0_spare_memory = currentallocation - max(minimumtarget, dom0minmem)
58 print int((free_memory + dom0_spare_memory)/1024)
61 if __name__ == '__main__':
62 sys.exit(main(sys.argv))