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