def _bootup_vm(self, cores, memory):
"""Boot up the VM as, internal helper funtion.
Note that it opens temporarily file as self._vm_pidfile.
"""
LOG.debug("Spawning up VM to run jobs within")
drive = "file={0._disk},media=disk,discard=unmap,snapshot={0._snapshot},if=virtio".format(self)
netdev = ("user,id=fakenet0,net=172.16.6.0/24,restrict={0._restrict_net},"
"hostfwd=tcp:127.0.0.1:{0._ssh_port}-:22,").format(self)
self._vm_pidfile = tempfile.NamedTemporaryFile(mode='r', prefix="worker-vm", suffix="pid")
kvm_command = ["kvm", "-name", self._vm_name,
"-sandbox", self._sandbox,
"-machine", "pc-i440fx-1.7,accel=kvm,usb=off",
"-cpu", "SandyBridge",
"-smp", "{}".format(cores),
"-m", "{}M".format(memory),
"-snapshot",
"-drive", drive,
"-netdev", netdev,
"-net", "nic,netdev=fakenet0,model=virtio",
"-daemonize",
"-pidfile", self._vm_pidfile.name,
"-vnc", "none"]
try:
kvm_process = subprocess.Popen(kvm_command, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
except OSError as e:
LOG.error("Is KVM installed? Popen raised %s", e)
raise EnvironmentError("Unable to start VM, KVM process failed %s", e)
stdout, stderr = None, None
try:
stdout, stderr = kvm_process.communicate(timeout=self._kvm_timeout)
LOG.debug("stdout: %s", stdout)
LOG.debug("stderr: %s", stderr)
except subprocess.TimeoutExpired:
LOG.error("VM did not start within %s seconds, killing it", self._kvm_timeout)
LOG.debug("stdout: %s", stdout)
LOG.debug("stderr: %s", stderr)
kvm_process.terminate()
if self.vm_pid is not None:
os.kill(self.vm_pid, signal.SIGTERM)
LOG.warning("5 seconds grace period before forcefully killing VM")
time.sleep(5)
kvm_process.kill()
if self.vm_pid is not None:
os.kill(self.vm_pid, signal.SIGKILL)
raise EnvironmentError("KVM start did not boot up properly")
评论列表
文章目录