def network(op): """Call a network control script. @param op: operation (start, stop) """ if op not in ["start", "stop"]: raise ValueError("Invalid operation: " + op) script = XendRoot.instance().get_network_script() if script: script.insert(1, op) os.spawnv(os.P_WAIT, script[0], script)
def network(op): """Call a network control script. @param op: operation (start, stop) """ if op not in ['start', 'stop']: raise ValueError('Invalid operation: ' + op) script = XendRoot.instance().get_network_script() if script: script.insert(1, op) os.spawnv(os.P_WAIT, script[0], script)
def free(required): """Balloon out memory from the privileged domain so that there is the specified required amount (in KiB) free. """ # We check whether there is enough free memory, and if not, instruct dom0 # to balloon out to free some up. Memory freed by a destroyed domain may # not appear in the free_memory field immediately, because it needs to be # scrubbed before it can be released to the free list, which is done # asynchronously by Xen; ballooning is asynchronous also. No matter where # we expect the free memory to come from, therefore, we need to wait for # it to become available. # # We are not allowed to balloon below dom0_min_mem, or if dom0_min_mem # is 0, we cannot balloon at all. Memory can still become available # through a rebooting domain, however. # # Eventually, we time out (presumably because there really isn't enough # free memory). # # We don't want to set the memory target (triggering a watch) when that # has already been done, but we do want to respond to changing memory # usage, so we recheck the required alloc each time around the loop, but # track the last used value so that we don't trigger too many watches. need_mem = (required + 1023) / 1024 + BALLOON_OUT_SLACK xroot = XendRoot.instance() xc = xen.lowlevel.xc.xc() try: dom0_min_mem = xroot.get_dom0_min_mem() retries = 0 sleep_time = SLEEP_TIME_GROWTH last_new_alloc = None while retries < RETRY_LIMIT: free_mem = xc.physinfo()['free_memory'] if free_mem >= need_mem: log.debug("Balloon: free %d; need %d; done.", free_mem, need_mem) return if retries == 0: log.debug("Balloon: free %d; need %d.", free_mem, need_mem) if dom0_min_mem > 0: dom0_alloc = get_dom0_current_alloc() new_alloc = dom0_alloc - (need_mem - free_mem) if (new_alloc >= dom0_min_mem and new_alloc != last_new_alloc): log.debug("Balloon: setting dom0 target to %d.", new_alloc) dom0 = XendDomain.instance().privilegedDomain() dom0.setMemoryTarget(new_alloc) last_new_alloc = new_alloc # Continue to retry, waiting for ballooning. time.sleep(sleep_time) retries += 1 sleep_time += SLEEP_TIME_GROWTH # Not enough memory; diagnose the problem. if dom0_min_mem == 0: raise VmError(('Not enough free memory and dom0_min_mem is 0, so ' 'I cannot release any more. I need %d MiB but ' 'only have %d.') % (need_mem, free_mem)) elif new_alloc < dom0_min_mem: raise VmError( ('I need %d MiB, but dom0_min_mem is %d and shrinking to ' '%d MiB would leave only %d MiB free.') % (need_mem, dom0_min_mem, dom0_min_mem, free_mem + dom0_alloc - dom0_min_mem)) else: raise VmError('The privileged domain did not balloon!') finally: del xc