Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 2 | # Copyright 2016 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
Mike Frysinger | 666566c | 2016-09-21 00:00:21 -0400 | [diff] [blame] | 6 | """Script for VM Management.""" |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 7 | |
| 8 | from __future__ import print_function |
| 9 | |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 10 | import argparse |
Achuith Bhandarkar | 22bfedf | 2017-11-08 11:59:38 +0100 | [diff] [blame] | 11 | import distutils.version |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 12 | import os |
Achuith Bhandarkar | 22bfedf | 2017-11-08 11:59:38 +0100 | [diff] [blame] | 13 | import re |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 14 | |
| 15 | from chromite.lib import commandline |
| 16 | from chromite.lib import cros_build_lib |
| 17 | from chromite.lib import cros_logging as logging |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 18 | from chromite.lib import osutils |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 19 | from chromite.lib import remote_access |
Achuith Bhandarkar | 2f8352f | 2017-06-02 12:47:18 -0700 | [diff] [blame] | 20 | from chromite.lib import retry_util |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 21 | |
| 22 | |
| 23 | class VMError(Exception): |
| 24 | """Exception for VM failures.""" |
| 25 | |
| 26 | def __init__(self, message): |
| 27 | super(VMError, self).__init__() |
| 28 | logging.error(message) |
| 29 | |
| 30 | |
| 31 | class VM(object): |
| 32 | """Class for managing a VM.""" |
| 33 | |
| 34 | SSH_PORT = 9222 |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 35 | |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 36 | def __init__(self, argv): |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 37 | """Initialize VM. |
| 38 | |
| 39 | Args: |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 40 | argv: command line args. |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 41 | """ |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 42 | opts = self._ParseArgs(argv) |
| 43 | opts.Freeze() |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 44 | |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 45 | self.qemu_path = opts.qemu_path |
Achuith Bhandarkar | 4125965 | 2017-11-14 10:31:02 -0800 | [diff] [blame] | 46 | self.qemu_bios_path = opts.qemu_bios_path |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 47 | self.enable_kvm = opts.enable_kvm |
Achuith Bhandarkar | f877da2 | 2017-09-12 12:27:39 -0700 | [diff] [blame] | 48 | # We don't need sudo access for software emulation or if /dev/kvm is |
| 49 | # writeable. |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 50 | self.use_sudo = self.enable_kvm and not os.access('/dev/kvm', os.W_OK) |
| 51 | self.display = opts.display |
| 52 | self.image_path = opts.image_path |
| 53 | self.ssh_port = opts.ssh_port |
| 54 | self.dry_run = opts.dry_run |
| 55 | |
| 56 | self.start = opts.start |
| 57 | self.stop = opts.stop |
| 58 | self.cmd = opts.args[1:] if opts.cmd else None |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 59 | |
Achuith Bhandarkar | ee3163d | 2016-10-19 12:58:35 -0700 | [diff] [blame] | 60 | self.vm_dir = os.path.join(osutils.GetGlobalTempDir(), 'cros_vm') |
| 61 | if os.path.exists(self.vm_dir): |
| 62 | # For security, ensure that vm_dir is not a symlink, and is owned by us or |
| 63 | # by root. |
| 64 | assert not os.path.islink(self.vm_dir), \ |
| 65 | 'VM state dir is misconfigured; please recreate: %s' % self.vm_dir |
| 66 | st_uid = os.stat(self.vm_dir).st_uid |
| 67 | assert st_uid == 0 or st_uid == os.getuid(), \ |
| 68 | 'VM state dir is misconfigured; please recreate: %s' % self.vm_dir |
| 69 | |
| 70 | self.pidfile = os.path.join(self.vm_dir, 'kvm.pid') |
| 71 | self.kvm_monitor = os.path.join(self.vm_dir, 'kvm.monitor') |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 72 | self.kvm_pipe_in = '%s.in' % self.kvm_monitor # to KVM |
| 73 | self.kvm_pipe_out = '%s.out' % self.kvm_monitor # from KVM |
| 74 | self.kvm_serial = '%s.serial' % self.kvm_monitor |
| 75 | |
Achuith Bhandarkar | 65d1a89 | 2017-05-08 14:13:12 -0700 | [diff] [blame] | 76 | self.remote = remote_access.RemoteDevice(remote_access.LOCALHOST, |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 77 | port=self.ssh_port) |
Achuith Bhandarkar | 65d1a89 | 2017-05-08 14:13:12 -0700 | [diff] [blame] | 78 | |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 79 | # TODO(achuith): support nographics, snapshot, mem_path, usb_passthrough, |
| 80 | # moblab, etc. |
| 81 | |
Achuith Bhandarkar | ee3163d | 2016-10-19 12:58:35 -0700 | [diff] [blame] | 82 | def _RunCommand(self, *args, **kwargs): |
| 83 | """Use SudoRunCommand or RunCommand as necessary.""" |
| 84 | if self.use_sudo: |
| 85 | return cros_build_lib.SudoRunCommand(*args, **kwargs) |
| 86 | else: |
| 87 | return cros_build_lib.RunCommand(*args, **kwargs) |
| 88 | |
| 89 | def _CleanupFiles(self, recreate): |
| 90 | """Cleanup vm_dir. |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 91 | |
| 92 | Args: |
Achuith Bhandarkar | ee3163d | 2016-10-19 12:58:35 -0700 | [diff] [blame] | 93 | recreate: recreate vm_dir. |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 94 | """ |
Mike Frysinger | 9708024 | 2017-09-13 01:58:45 -0400 | [diff] [blame] | 95 | osutils.RmDir(self.vm_dir, ignore_missing=True, sudo=self.use_sudo) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 96 | if recreate: |
Mike Frysinger | 9708024 | 2017-09-13 01:58:45 -0400 | [diff] [blame] | 97 | osutils.SafeMakedirs(self.vm_dir) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 98 | |
Achuith Bhandarkar | 22bfedf | 2017-11-08 11:59:38 +0100 | [diff] [blame] | 99 | @cros_build_lib.MemoizedSingleCall |
| 100 | def QemuVersion(self): |
| 101 | """Determine QEMU version.""" |
| 102 | version_str = self._RunCommand([self.qemu_path, '--version'], |
| 103 | capture_output=True).output |
| 104 | # version string looks like one of these: |
| 105 | # QEMU emulator version 2.0.0 (Debian 2.0.0+dfsg-2ubuntu1.36), Copyright (c) |
| 106 | # 2003-2008 Fabrice Bellard |
| 107 | # |
| 108 | # QEMU emulator version 2.6.0, Copyright (c) 2003-2008 Fabrice Bellard |
| 109 | # |
| 110 | # qemu-x86_64 version 2.10.1 |
| 111 | # Copyright (c) 2003-2017 Fabrice Bellard and the QEMU Project developers |
| 112 | m = re.search(r"version ([0-9.]+)", version_str) |
| 113 | if not m: |
| 114 | raise VMError('Unable to determine QEMU version from:\n%s.' % version_str) |
| 115 | return m.group(1) |
| 116 | |
| 117 | def _CheckQemuMinVersion(self): |
| 118 | """Ensure minimum QEMU version.""" |
| 119 | min_qemu_version = '2.6.0' |
| 120 | logging.info('QEMU version %s', self.QemuVersion()) |
| 121 | LooseVersion = distutils.version.LooseVersion |
| 122 | if LooseVersion(self.QemuVersion()) < LooseVersion(min_qemu_version): |
| 123 | raise VMError('QEMU %s is the minimum supported version. You have %s.' |
| 124 | % (min_qemu_version, self.QemuVersion())) |
| 125 | |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 126 | def Run(self): |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 127 | """Performs an action, one of start, stop, or run a command in the VM. |
| 128 | |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 129 | Returns: |
| 130 | cmd output. |
| 131 | """ |
| 132 | |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 133 | if not self.start and not self.stop and not self.cmd: |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 134 | raise VMError('Must specify one of start, stop, or cmd.') |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 135 | if self.start: |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 136 | self.Start() |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 137 | if self.cmd: |
| 138 | return self.RemoteCommand(self.cmd) |
| 139 | if self.stop: |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 140 | self.Stop() |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 141 | |
| 142 | def Start(self): |
| 143 | """Start the VM.""" |
| 144 | |
| 145 | self.Stop() |
| 146 | |
Achuith Bhandarkar | 022d69c | 2016-10-05 14:28:14 -0700 | [diff] [blame] | 147 | logging.debug('Start VM') |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 148 | if not self.qemu_path: |
| 149 | self.qemu_path = osutils.Which('qemu-system-x86_64') |
| 150 | if not self.qemu_path: |
Achuith Bhandarkar | 4125965 | 2017-11-14 10:31:02 -0800 | [diff] [blame] | 151 | raise VMError('qemu not found.') |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 152 | logging.debug('qemu path=%s', self.qemu_path) |
Achuith Bhandarkar | 4125965 | 2017-11-14 10:31:02 -0800 | [diff] [blame] | 153 | qemu_args = [self.qemu_path] |
| 154 | |
| 155 | if self.qemu_bios_path and os.path.isdir(self.qemu_bios_path): |
| 156 | qemu_args += ['-L', self.qemu_bios_path] |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 157 | |
| 158 | if not self.image_path: |
| 159 | self.image_path = os.environ.get('VM_IMAGE_PATH', '') |
| 160 | logging.debug('vm image path=%s', self.image_path) |
| 161 | if not self.image_path or not os.path.exists(self.image_path): |
Achuith Bhandarkar | 9788efd | 2017-11-07 12:34:23 +0100 | [diff] [blame] | 162 | raise VMError('No VM image path found. ' |
| 163 | 'Use cros chrome-sdk --download-vm.') |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 164 | |
| 165 | self._CleanupFiles(recreate=True) |
Mike Frysinger | 9708024 | 2017-09-13 01:58:45 -0400 | [diff] [blame] | 166 | # Make sure we can read these files later on by creating them as ourselves. |
| 167 | osutils.Touch(self.kvm_serial) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 168 | for pipe in [self.kvm_pipe_in, self.kvm_pipe_out]: |
| 169 | os.mkfifo(pipe, 0600) |
Mike Frysinger | 9708024 | 2017-09-13 01:58:45 -0400 | [diff] [blame] | 170 | osutils.Touch(self.pidfile) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 171 | |
Achuith Bhandarkar | 22bfedf | 2017-11-08 11:59:38 +0100 | [diff] [blame] | 172 | self._CheckQemuMinVersion() |
| 173 | |
Achuith Bhandarkar | 4125965 | 2017-11-14 10:31:02 -0800 | [diff] [blame] | 174 | qemu_args += [ |
| 175 | '-m', '2G', '-smp', '4', '-vga', 'virtio', '-daemonize', |
| 176 | '-pidfile', self.pidfile, |
| 177 | '-chardev', 'pipe,id=control_pipe,path=%s' % self.kvm_monitor, |
| 178 | '-serial', 'file:%s' % self.kvm_serial, |
| 179 | '-mon', 'chardev=control_pipe', |
| 180 | # Qemu-vlans are used by qemu to separate out network traffic on the |
| 181 | # slirp network bridge. qemu forwards traffic on a slirp vlan to all |
| 182 | # ports conected on that vlan. By default, slirp ports are on vlan |
| 183 | # 0. We explicitly set a vlan here so that another qemu VM using |
| 184 | # slirp doesn't conflict with our network traffic. |
| 185 | '-net', 'nic,model=virtio,vlan=%d' % self.ssh_port, |
| 186 | '-net', 'user,hostfwd=tcp:127.0.0.1:%d-:22,vlan=%d' |
| 187 | % (self.ssh_port, self.ssh_port), |
| 188 | '-drive', 'file=%s,index=0,media=disk,cache=unsafe,format=raw' |
| 189 | % self.image_path, |
| 190 | ] |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 191 | if self.enable_kvm: |
Achuith Bhandarkar | 4125965 | 2017-11-14 10:31:02 -0800 | [diff] [blame] | 192 | qemu_args.append('-enable-kvm') |
Achuith Bhandarkar | b891adb | 2016-10-24 18:43:22 -0700 | [diff] [blame] | 193 | if not self.display: |
Achuith Bhandarkar | 4125965 | 2017-11-14 10:31:02 -0800 | [diff] [blame] | 194 | qemu_args.extend(['-display', 'none']) |
| 195 | logging.info(cros_build_lib.CmdToStr(qemu_args)) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 196 | logging.info('Pid file: %s', self.pidfile) |
| 197 | if not self.dry_run: |
Achuith Bhandarkar | 4125965 | 2017-11-14 10:31:02 -0800 | [diff] [blame] | 198 | self._RunCommand(qemu_args) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 199 | |
| 200 | def _GetVMPid(self): |
| 201 | """Get the pid of the VM. |
| 202 | |
| 203 | Returns: |
| 204 | pid of the VM. |
| 205 | """ |
Achuith Bhandarkar | ee3163d | 2016-10-19 12:58:35 -0700 | [diff] [blame] | 206 | if not os.path.exists(self.vm_dir): |
| 207 | logging.debug('%s not present.', self.vm_dir) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 208 | return 0 |
| 209 | |
| 210 | if not os.path.exists(self.pidfile): |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 211 | logging.info('%s does not exist.', self.pidfile) |
| 212 | return 0 |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 213 | |
Mike Frysinger | 9708024 | 2017-09-13 01:58:45 -0400 | [diff] [blame] | 214 | pid = osutils.ReadFile(self.pidfile).rstrip() |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 215 | if not pid.isdigit(): |
Mike Frysinger | 9708024 | 2017-09-13 01:58:45 -0400 | [diff] [blame] | 216 | # Ignore blank/empty files. |
| 217 | if pid: |
| 218 | logging.error('%s in %s is not a pid.', pid, self.pidfile) |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 219 | return 0 |
| 220 | |
Achuith Bhandarkar | 022d69c | 2016-10-05 14:28:14 -0700 | [diff] [blame] | 221 | return int(pid) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 222 | |
| 223 | def IsRunning(self): |
| 224 | """Returns True if there's a running VM. |
| 225 | |
| 226 | Returns: |
| 227 | True if there's a running VM. |
| 228 | """ |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 229 | pid = self._GetVMPid() |
Achuith Bhandarkar | 022d69c | 2016-10-05 14:28:14 -0700 | [diff] [blame] | 230 | if not pid: |
| 231 | return False |
| 232 | |
| 233 | # Make sure the process actually exists. |
Mike Frysinger | 9708024 | 2017-09-13 01:58:45 -0400 | [diff] [blame] | 234 | return os.path.isdir('/proc/%i' % pid) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 235 | |
| 236 | def Stop(self): |
| 237 | """Stop the VM.""" |
Achuith Bhandarkar | 022d69c | 2016-10-05 14:28:14 -0700 | [diff] [blame] | 238 | logging.debug('Stop VM') |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 239 | |
| 240 | pid = self._GetVMPid() |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 241 | if pid: |
| 242 | logging.info('Killing %d.', pid) |
| 243 | if not self.dry_run: |
Achuith Bhandarkar | ee3163d | 2016-10-19 12:58:35 -0700 | [diff] [blame] | 244 | self._RunCommand(['kill', '-9', str(pid)], error_code_ok=True) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 245 | |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 246 | self._CleanupFiles(recreate=False) |
| 247 | |
Achuith Bhandarkar | 2f8352f | 2017-06-02 12:47:18 -0700 | [diff] [blame] | 248 | def _WaitForProcs(self): |
| 249 | """Wait for expected processes to launch.""" |
| 250 | class _TooFewPidsException(Exception): |
| 251 | """Exception for _GetRunningPids to throw.""" |
| 252 | |
| 253 | def _GetRunningPids(exe, numpids): |
| 254 | pids = self.remote.GetRunningPids(exe, full_path=False) |
| 255 | logging.info('%s pids: %s', exe, repr(pids)) |
| 256 | if len(pids) < numpids: |
| 257 | raise _TooFewPidsException() |
| 258 | |
| 259 | def _WaitForProc(exe, numpids): |
| 260 | try: |
| 261 | retry_util.RetryException( |
Achuith Bhandarkar | 0e7b850 | 2017-06-12 15:32:41 -0700 | [diff] [blame] | 262 | exception=_TooFewPidsException, |
Achuith Bhandarkar | 2f8352f | 2017-06-02 12:47:18 -0700 | [diff] [blame] | 263 | max_retry=20, |
| 264 | functor=lambda: _GetRunningPids(exe, numpids), |
| 265 | sleep=2) |
| 266 | except _TooFewPidsException: |
| 267 | raise VMError('_WaitForProcs failed: timed out while waiting for ' |
| 268 | '%d %s processes to start.' % (numpids, exe)) |
| 269 | |
| 270 | # We could also wait for session_manager, nacl_helper, etc, but chrome is |
| 271 | # the long pole. We expect the parent, 2 zygotes, gpu-process, renderer. |
| 272 | # This could potentially break with Mustash. |
| 273 | _WaitForProc('chrome', 5) |
| 274 | |
| 275 | def WaitForBoot(self): |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 276 | """Wait for the VM to boot up. |
| 277 | |
Achuith Bhandarkar | 2f8352f | 2017-06-02 12:47:18 -0700 | [diff] [blame] | 278 | Wait for ssh connection to become active, and wait for all expected chrome |
| 279 | processes to be launched. |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 280 | """ |
Achuith Bhandarkar | ee3163d | 2016-10-19 12:58:35 -0700 | [diff] [blame] | 281 | if not os.path.exists(self.vm_dir): |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 282 | self.Start() |
| 283 | |
Achuith Bhandarkar | 2f8352f | 2017-06-02 12:47:18 -0700 | [diff] [blame] | 284 | try: |
| 285 | result = retry_util.RetryException( |
Achuith Bhandarkar | 0e7b850 | 2017-06-12 15:32:41 -0700 | [diff] [blame] | 286 | exception=remote_access.SSHConnectionError, |
Achuith Bhandarkar | 2f8352f | 2017-06-02 12:47:18 -0700 | [diff] [blame] | 287 | max_retry=10, |
| 288 | functor=lambda: self.RemoteCommand(cmd=['echo']), |
| 289 | sleep=5) |
| 290 | except remote_access.SSHConnectionError: |
| 291 | raise VMError('WaitForBoot timed out trying to connect to VM.') |
| 292 | |
| 293 | if result.returncode != 0: |
| 294 | raise VMError('WaitForBoot failed: %s.' % result.error) |
| 295 | |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 296 | # Chrome can take a while to start with software emulation. |
| 297 | if not self.enable_kvm: |
| 298 | self._WaitForProcs() |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 299 | |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 300 | def RemoteCommand(self, cmd, **kwargs): |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 301 | """Run a remote command in the VM. |
| 302 | |
| 303 | Args: |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 304 | cmd: command to run. |
| 305 | kwargs: additional args (see documentation for RemoteDevice.RunCommand). |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 306 | """ |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 307 | if not self.dry_run: |
Achuith Bhandarkar | 65d1a89 | 2017-05-08 14:13:12 -0700 | [diff] [blame] | 308 | return self.remote.RunCommand(cmd, debug_level=logging.INFO, |
| 309 | combine_stdout_stderr=True, |
| 310 | log_output=True, |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 311 | error_code_ok=True, |
| 312 | **kwargs) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 313 | |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 314 | @staticmethod |
| 315 | def _ParseArgs(argv): |
| 316 | """Parse a list of args. |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 317 | |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 318 | Args: |
| 319 | argv: list of command line arguments. |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 320 | |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 321 | Returns: |
| 322 | List of parsed opts. |
| 323 | """ |
| 324 | parser = commandline.ArgumentParser(description=__doc__) |
| 325 | parser.add_argument('--start', action='store_true', default=False, |
| 326 | help='Start the VM.') |
| 327 | parser.add_argument('--stop', action='store_true', default=False, |
| 328 | help='Stop the VM.') |
| 329 | parser.add_argument('--image-path', type='path', |
| 330 | help='Path to VM image to launch with --start.') |
| 331 | parser.add_argument('--qemu-path', type='path', |
| 332 | help='Path of qemu binary to launch with --start.') |
Achuith Bhandarkar | 4125965 | 2017-11-14 10:31:02 -0800 | [diff] [blame] | 333 | parser.add_argument('--qemu-bios-path', type='path', |
| 334 | help='Path of directory with qemu bios files.') |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 335 | parser.add_argument('--disable-kvm', dest='enable_kvm', |
| 336 | action='store_false', default=True, |
| 337 | help='Disable KVM, use software emulation.') |
| 338 | parser.add_argument('--no-display', dest='display', |
| 339 | action='store_false', default=True, |
| 340 | help='Do not display video output.') |
| 341 | parser.add_argument('--ssh-port', type=int, default=VM.SSH_PORT, |
| 342 | help='ssh port to communicate with VM.') |
| 343 | parser.add_argument('--dry-run', action='store_true', default=False, |
| 344 | help='dry run for debugging.') |
| 345 | parser.add_argument('--cmd', action='store_true', default=False, |
| 346 | help='Run a command in the VM.') |
| 347 | parser.add_argument('args', nargs=argparse.REMAINDER, |
| 348 | help='Command to run in the VM.') |
| 349 | return parser.parse_args(argv) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 350 | |
| 351 | def main(argv): |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 352 | vm = VM(argv) |
| 353 | vm.Run() |