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