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 | b00bafc | 2018-09-11 16:30:13 -0700 | [diff] [blame] | 12 | import errno |
Po-Hsien Wang | 1cec8d1 | 2018-01-25 16:36:44 -0800 | [diff] [blame] | 13 | import multiprocessing |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 14 | import os |
Achuith Bhandarkar | 22bfedf | 2017-11-08 11:59:38 +0100 | [diff] [blame] | 15 | import re |
Achuith Bhandarkar | b00bafc | 2018-09-11 16:30:13 -0700 | [diff] [blame] | 16 | import socket |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 17 | |
Achuith Bhandarkar | 1297dcf | 2017-11-21 12:03:48 -0800 | [diff] [blame] | 18 | from chromite.cli.cros import cros_chrome_sdk |
| 19 | from chromite.lib import cache |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 20 | from chromite.lib import commandline |
Achuith Bhandarkar | 1297dcf | 2017-11-21 12:03:48 -0800 | [diff] [blame] | 21 | from chromite.lib import constants |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 22 | from chromite.lib import cros_build_lib |
| 23 | from chromite.lib import cros_logging as logging |
Mike Frysinger | 1066629 | 2018-07-12 01:03:38 -0400 | [diff] [blame] | 24 | from chromite.lib import memoize |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 25 | from chromite.lib import osutils |
Achuith Bhandarkar | 1297dcf | 2017-11-21 12:03:48 -0800 | [diff] [blame] | 26 | from chromite.lib import path_util |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 27 | from chromite.lib import remote_access |
Achuith Bhandarkar | 2f8352f | 2017-06-02 12:47:18 -0700 | [diff] [blame] | 28 | from chromite.lib import retry_util |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 29 | |
| 30 | |
| 31 | class VMError(Exception): |
| 32 | """Exception for VM failures.""" |
| 33 | |
| 34 | def __init__(self, message): |
| 35 | super(VMError, self).__init__() |
| 36 | logging.error(message) |
| 37 | |
| 38 | |
| 39 | class VM(object): |
| 40 | """Class for managing a VM.""" |
| 41 | |
| 42 | SSH_PORT = 9222 |
Nicolas Norvez | f527cdf | 2018-01-26 11:55:44 -0800 | [diff] [blame] | 43 | IMAGE_FORMAT = 'raw' |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 44 | |
Achuith Bhandarkar | 2beae29 | 2018-03-26 16:44:53 -0700 | [diff] [blame] | 45 | def __init__(self, opts): |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 46 | """Initialize VM. |
| 47 | |
| 48 | Args: |
Achuith Bhandarkar | 2beae29 | 2018-03-26 16:44:53 -0700 | [diff] [blame] | 49 | opts: command line options. |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 50 | """ |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 51 | self.qemu_path = opts.qemu_path |
Ben Pastene | 243302c | 2018-09-21 16:06:08 -0700 | [diff] [blame] | 52 | self.qemu_img_path = opts.qemu_img_path |
Achuith Bhandarkar | 4125965 | 2017-11-14 10:31:02 -0800 | [diff] [blame] | 53 | self.qemu_bios_path = opts.qemu_bios_path |
Po-Hsien Wang | 1cec8d1 | 2018-01-25 16:36:44 -0800 | [diff] [blame] | 54 | self.qemu_m = opts.qemu_m |
| 55 | self.qemu_cpu = opts.qemu_cpu |
| 56 | self.qemu_smp = opts.qemu_smp |
| 57 | if self.qemu_smp == 0: |
| 58 | self.qemu_smp = min(8, multiprocessing.cpu_count) |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 59 | self.enable_kvm = opts.enable_kvm |
Ben Pastene | 243302c | 2018-09-21 16:06:08 -0700 | [diff] [blame] | 60 | self.copy_on_write = opts.copy_on_write |
Achuith Bhandarkar | f877da2 | 2017-09-12 12:27:39 -0700 | [diff] [blame] | 61 | # We don't need sudo access for software emulation or if /dev/kvm is |
| 62 | # writeable. |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 63 | self.use_sudo = self.enable_kvm and not os.access('/dev/kvm', os.W_OK) |
| 64 | self.display = opts.display |
| 65 | self.image_path = opts.image_path |
Nicolas Norvez | f527cdf | 2018-01-26 11:55:44 -0800 | [diff] [blame] | 66 | self.image_format = opts.image_format |
Achuith Bhandarkar | 1297dcf | 2017-11-21 12:03:48 -0800 | [diff] [blame] | 67 | self.board = opts.board |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 68 | self.ssh_port = opts.ssh_port |
Achuith Bhandarkar | 195b951 | 2018-08-15 17:14:32 -0700 | [diff] [blame] | 69 | self.private_key = opts.private_key |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 70 | self.dry_run = opts.dry_run |
Achuith Bhandarkar | 195b951 | 2018-08-15 17:14:32 -0700 | [diff] [blame] | 71 | # log_level is only set if --log-level or --debug is specified. |
| 72 | self.log_level = getattr(opts, 'log_level', None) |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 73 | |
| 74 | self.start = opts.start |
| 75 | self.stop = opts.stop |
| 76 | self.cmd = opts.args[1:] if opts.cmd else None |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 77 | |
Achuith Bhandarkar | 02ef76b | 2018-04-05 10:47:33 +0200 | [diff] [blame] | 78 | self.cache_dir = os.path.abspath(opts.cache_dir) |
| 79 | assert os.path.isdir(self.cache_dir), "Cache directory doesn't exist" |
| 80 | |
Achuith Bhandarkar | 2beae29 | 2018-03-26 16:44:53 -0700 | [diff] [blame] | 81 | self.vm_dir = opts.vm_dir |
| 82 | if not self.vm_dir: |
| 83 | self.vm_dir = os.path.join(osutils.GetGlobalTempDir(), |
| 84 | 'cros_vm_%d' % self.ssh_port) |
Achuith Bhandarkar | fd5d185 | 2018-03-26 14:50:54 -0700 | [diff] [blame] | 85 | self._CreateVMDir() |
Achuith Bhandarkar | ee3163d | 2016-10-19 12:58:35 -0700 | [diff] [blame] | 86 | |
| 87 | self.pidfile = os.path.join(self.vm_dir, 'kvm.pid') |
| 88 | self.kvm_monitor = os.path.join(self.vm_dir, 'kvm.monitor') |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 89 | self.kvm_pipe_in = '%s.in' % self.kvm_monitor # to KVM |
| 90 | self.kvm_pipe_out = '%s.out' % self.kvm_monitor # from KVM |
| 91 | self.kvm_serial = '%s.serial' % self.kvm_monitor |
| 92 | |
Achuith Bhandarkar | 65d1a89 | 2017-05-08 14:13:12 -0700 | [diff] [blame] | 93 | self.remote = remote_access.RemoteDevice(remote_access.LOCALHOST, |
Achuith Bhandarkar | 195b951 | 2018-08-15 17:14:32 -0700 | [diff] [blame] | 94 | port=self.ssh_port, |
| 95 | private_key=self.private_key) |
Achuith Bhandarkar | 2beae29 | 2018-03-26 16:44:53 -0700 | [diff] [blame] | 96 | self.device_addr = 'ssh://%s:%d' % (remote_access.LOCALHOST, self.ssh_port) |
Achuith Bhandarkar | 65d1a89 | 2017-05-08 14:13:12 -0700 | [diff] [blame] | 97 | |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 98 | # TODO(achuith): support nographics, snapshot, mem_path, usb_passthrough, |
| 99 | # moblab, etc. |
| 100 | |
Achuith Bhandarkar | ee3163d | 2016-10-19 12:58:35 -0700 | [diff] [blame] | 101 | def _RunCommand(self, *args, **kwargs): |
Achuith Bhandarkar | 665a9b3 | 2018-05-17 11:39:19 -0700 | [diff] [blame] | 102 | """Use SudoRunCommand or RunCommand as necessary. |
| 103 | |
| 104 | Args: |
| 105 | args and kwargs: positional and optional args to RunCommand. |
| 106 | |
| 107 | Returns: |
| 108 | cros_build_lib.CommandResult object. |
| 109 | """ |
Achuith Bhandarkar | ee3163d | 2016-10-19 12:58:35 -0700 | [diff] [blame] | 110 | if self.use_sudo: |
| 111 | return cros_build_lib.SudoRunCommand(*args, **kwargs) |
| 112 | else: |
| 113 | return cros_build_lib.RunCommand(*args, **kwargs) |
| 114 | |
Achuith Bhandarkar | fd5d185 | 2018-03-26 14:50:54 -0700 | [diff] [blame] | 115 | def _CreateVMDir(self): |
| 116 | """Safely create vm_dir.""" |
Steven Bennetts | c57ca0e | 2018-09-18 09:33:59 -0700 | [diff] [blame] | 117 | if not osutils.SafeMakedirs(self.vm_dir): |
| 118 | # For security, ensure that vm_dir is not a symlink, and is owned by us. |
Achuith Bhandarkar | fd5d185 | 2018-03-26 14:50:54 -0700 | [diff] [blame] | 119 | error_str = ('VM state dir is misconfigured; please recreate: %s' |
| 120 | % self.vm_dir) |
Achuith Bhandarkar | 46d8387 | 2018-04-04 01:49:55 -0700 | [diff] [blame] | 121 | assert os.path.isdir(self.vm_dir), error_str |
Achuith Bhandarkar | fd5d185 | 2018-03-26 14:50:54 -0700 | [diff] [blame] | 122 | assert not os.path.islink(self.vm_dir), error_str |
Steven Bennetts | c57ca0e | 2018-09-18 09:33:59 -0700 | [diff] [blame] | 123 | assert os.stat(self.vm_dir).st_uid == os.getuid(), error_str |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 124 | |
Ben Pastene | 243302c | 2018-09-21 16:06:08 -0700 | [diff] [blame] | 125 | def _CreateQcow2Image(self): |
| 126 | """Creates a qcow2-formatted image in the temporary VM dir. |
| 127 | |
| 128 | This image will get removed on VM shutdown. |
| 129 | """ |
| 130 | cow_image_path = os.path.join(self.vm_dir, 'qcow2.img') |
| 131 | qemu_img_args = [ |
| 132 | self.qemu_img_path, |
| 133 | 'create', '-f', 'qcow2', |
| 134 | '-o', 'backing_file=%s' % self.image_path, |
| 135 | cow_image_path, |
| 136 | ] |
| 137 | if not self.dry_run: |
| 138 | self._RunCommand(qemu_img_args) |
| 139 | logging.info('qcow2 image created at %s.', cow_image_path) |
| 140 | else: |
| 141 | logging.info(cros_build_lib.CmdToStr(qemu_img_args)) |
| 142 | self.image_path = cow_image_path |
| 143 | self.image_format = 'qcow2' |
| 144 | |
Achuith Bhandarkar | fd5d185 | 2018-03-26 14:50:54 -0700 | [diff] [blame] | 145 | def _RmVMDir(self): |
| 146 | """Cleanup vm_dir.""" |
Mike Frysinger | 9708024 | 2017-09-13 01:58:45 -0400 | [diff] [blame] | 147 | osutils.RmDir(self.vm_dir, ignore_missing=True, sudo=self.use_sudo) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 148 | |
Achuith Bhandarkar | 02ef76b | 2018-04-05 10:47:33 +0200 | [diff] [blame] | 149 | def _GetCachePath(self, cache_name): |
Achuith Bhandarkar | f6bccdb | 2018-03-23 13:12:29 -0700 | [diff] [blame] | 150 | """Return path to cache. |
| 151 | |
| 152 | Args: |
| 153 | cache_name: Name of cache. |
| 154 | |
| 155 | Returns: |
| 156 | File path of cache. |
| 157 | """ |
Achuith Bhandarkar | 02ef76b | 2018-04-05 10:47:33 +0200 | [diff] [blame] | 158 | return os.path.join(self.cache_dir, |
Achuith Bhandarkar | f6bccdb | 2018-03-23 13:12:29 -0700 | [diff] [blame] | 159 | cros_chrome_sdk.COMMAND_NAME, |
| 160 | cache_name) |
| 161 | |
Mike Frysinger | 1066629 | 2018-07-12 01:03:38 -0400 | [diff] [blame] | 162 | @memoize.MemoizedSingleCall |
Achuith Bhandarkar | f6bccdb | 2018-03-23 13:12:29 -0700 | [diff] [blame] | 163 | def _SDKVersion(self): |
| 164 | """Determine SDK version. |
| 165 | |
| 166 | Check the environment if we're in the SDK shell, and failing that, look at |
| 167 | the misc cache. |
| 168 | |
| 169 | Returns: |
| 170 | SDK version. |
| 171 | """ |
| 172 | sdk_version = os.environ.get(cros_chrome_sdk.SDKFetcher.SDK_VERSION_ENV) |
Achuith Bhandarkar | 4e367c2 | 2018-03-27 15:32:48 -0700 | [diff] [blame] | 173 | if not sdk_version and self.board: |
Achuith Bhandarkar | f6bccdb | 2018-03-23 13:12:29 -0700 | [diff] [blame] | 174 | misc_cache = cache.DiskCache(self._GetCachePath( |
| 175 | cros_chrome_sdk.SDKFetcher.MISC_CACHE)) |
| 176 | with misc_cache.Lookup((self.board, 'latest')) as ref: |
| 177 | if ref.Exists(lock=True): |
| 178 | sdk_version = osutils.ReadFile(ref.path).strip() |
| 179 | return sdk_version |
| 180 | |
| 181 | def _CachePathForKey(self, key): |
Achuith Bhandarkar | fc75f69 | 2018-01-10 11:33:09 -0800 | [diff] [blame] | 182 | """Get cache path for key. |
| 183 | |
| 184 | Args: |
| 185 | key: cache key. |
| 186 | """ |
Achuith Bhandarkar | f6bccdb | 2018-03-23 13:12:29 -0700 | [diff] [blame] | 187 | tarball_cache = cache.TarballCache(self._GetCachePath( |
Achuith Bhandarkar | fc75f69 | 2018-01-10 11:33:09 -0800 | [diff] [blame] | 188 | cros_chrome_sdk.SDKFetcher.TARBALL_CACHE)) |
Achuith Bhandarkar | 4e367c2 | 2018-03-27 15:32:48 -0700 | [diff] [blame] | 189 | if self.board and self._SDKVersion(): |
Achuith Bhandarkar | f6bccdb | 2018-03-23 13:12:29 -0700 | [diff] [blame] | 190 | cache_key = (self.board, self._SDKVersion(), key) |
Achuith Bhandarkar | fc75f69 | 2018-01-10 11:33:09 -0800 | [diff] [blame] | 191 | with tarball_cache.Lookup(cache_key) as ref: |
| 192 | if ref.Exists(): |
| 193 | return ref.path |
| 194 | return None |
| 195 | |
Mike Frysinger | 1066629 | 2018-07-12 01:03:38 -0400 | [diff] [blame] | 196 | @memoize.MemoizedSingleCall |
Achuith Bhandarkar | 22bfedf | 2017-11-08 11:59:38 +0100 | [diff] [blame] | 197 | def QemuVersion(self): |
Achuith Bhandarkar | f6bccdb | 2018-03-23 13:12:29 -0700 | [diff] [blame] | 198 | """Determine QEMU version. |
| 199 | |
| 200 | Returns: |
| 201 | QEMU version. |
| 202 | """ |
Achuith Bhandarkar | 22bfedf | 2017-11-08 11:59:38 +0100 | [diff] [blame] | 203 | version_str = self._RunCommand([self.qemu_path, '--version'], |
| 204 | capture_output=True).output |
| 205 | # version string looks like one of these: |
| 206 | # QEMU emulator version 2.0.0 (Debian 2.0.0+dfsg-2ubuntu1.36), Copyright (c) |
| 207 | # 2003-2008 Fabrice Bellard |
| 208 | # |
| 209 | # QEMU emulator version 2.6.0, Copyright (c) 2003-2008 Fabrice Bellard |
| 210 | # |
| 211 | # qemu-x86_64 version 2.10.1 |
| 212 | # Copyright (c) 2003-2017 Fabrice Bellard and the QEMU Project developers |
| 213 | m = re.search(r"version ([0-9.]+)", version_str) |
| 214 | if not m: |
| 215 | raise VMError('Unable to determine QEMU version from:\n%s.' % version_str) |
| 216 | return m.group(1) |
| 217 | |
| 218 | def _CheckQemuMinVersion(self): |
| 219 | """Ensure minimum QEMU version.""" |
| 220 | min_qemu_version = '2.6.0' |
| 221 | logging.info('QEMU version %s', self.QemuVersion()) |
| 222 | LooseVersion = distutils.version.LooseVersion |
| 223 | if LooseVersion(self.QemuVersion()) < LooseVersion(min_qemu_version): |
| 224 | raise VMError('QEMU %s is the minimum supported version. You have %s.' |
| 225 | % (min_qemu_version, self.QemuVersion())) |
| 226 | |
Achuith Bhandarkar | 1297dcf | 2017-11-21 12:03:48 -0800 | [diff] [blame] | 227 | def _SetQemuPath(self): |
| 228 | """Find a suitable Qemu executable.""" |
Achuith Bhandarkar | fc75f69 | 2018-01-10 11:33:09 -0800 | [diff] [blame] | 229 | qemu_exe = 'qemu-system-x86_64' |
| 230 | qemu_exe_path = os.path.join('usr/bin', qemu_exe) |
| 231 | |
| 232 | # Check SDK cache. |
Achuith Bhandarkar | 1297dcf | 2017-11-21 12:03:48 -0800 | [diff] [blame] | 233 | if not self.qemu_path: |
Achuith Bhandarkar | f6bccdb | 2018-03-23 13:12:29 -0700 | [diff] [blame] | 234 | qemu_dir = self._CachePathForKey(cros_chrome_sdk.SDKFetcher.QEMU_BIN_KEY) |
Achuith Bhandarkar | fc75f69 | 2018-01-10 11:33:09 -0800 | [diff] [blame] | 235 | if qemu_dir: |
| 236 | qemu_path = os.path.join(qemu_dir, qemu_exe_path) |
| 237 | if os.path.isfile(qemu_path): |
| 238 | self.qemu_path = qemu_path |
| 239 | |
| 240 | # Check chroot. |
Achuith Bhandarkar | 1297dcf | 2017-11-21 12:03:48 -0800 | [diff] [blame] | 241 | if not self.qemu_path: |
Achuith Bhandarkar | fc75f69 | 2018-01-10 11:33:09 -0800 | [diff] [blame] | 242 | qemu_path = os.path.join( |
| 243 | constants.SOURCE_ROOT, constants.DEFAULT_CHROOT_DIR, qemu_exe_path) |
| 244 | if os.path.isfile(qemu_path): |
| 245 | self.qemu_path = qemu_path |
| 246 | |
| 247 | # Check system. |
| 248 | if not self.qemu_path: |
| 249 | self.qemu_path = osutils.Which(qemu_exe) |
| 250 | |
| 251 | if not self.qemu_path or not os.path.isfile(self.qemu_path): |
| 252 | raise VMError('QEMU not found.') |
Ben Pastene | 243302c | 2018-09-21 16:06:08 -0700 | [diff] [blame] | 253 | |
| 254 | if self.copy_on_write: |
| 255 | if not self.qemu_img_path: |
| 256 | # Look for qemu-img right next to qemu-system-x86_64. |
| 257 | self.qemu_img_path = os.path.join( |
| 258 | os.path.dirname(self.qemu_path), 'qemu-img') |
| 259 | if not os.path.isfile(self.qemu_img_path): |
| 260 | raise VMError('qemu-img not found. (Needed to create qcow2 image).') |
| 261 | |
Achuith Bhandarkar | fc75f69 | 2018-01-10 11:33:09 -0800 | [diff] [blame] | 262 | logging.debug('QEMU path: %s', self.qemu_path) |
Achuith Bhandarkar | 1297dcf | 2017-11-21 12:03:48 -0800 | [diff] [blame] | 263 | self._CheckQemuMinVersion() |
| 264 | |
| 265 | def _GetBuiltVMImagePath(self): |
| 266 | """Get path of a locally built VM image.""" |
Achuith Bhandarkar | fc75f69 | 2018-01-10 11:33:09 -0800 | [diff] [blame] | 267 | vm_image_path = os.path.join(constants.SOURCE_ROOT, 'src/build/images', |
| 268 | cros_build_lib.GetBoard(self.board), |
| 269 | 'latest', constants.VM_IMAGE_BIN) |
| 270 | 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] | 271 | |
Achuith Bhandarkar | fc75f69 | 2018-01-10 11:33:09 -0800 | [diff] [blame] | 272 | def _GetCacheVMImagePath(self): |
| 273 | """Get path of a cached VM image.""" |
Achuith Bhandarkar | f6bccdb | 2018-03-23 13:12:29 -0700 | [diff] [blame] | 274 | cache_path = self._CachePathForKey(constants.VM_IMAGE_TAR) |
Achuith Bhandarkar | fc75f69 | 2018-01-10 11:33:09 -0800 | [diff] [blame] | 275 | if cache_path: |
| 276 | vm_image = os.path.join(cache_path, constants.VM_IMAGE_BIN) |
| 277 | if os.path.isfile(vm_image): |
| 278 | return vm_image |
Achuith Bhandarkar | 1297dcf | 2017-11-21 12:03:48 -0800 | [diff] [blame] | 279 | return None |
| 280 | |
| 281 | def _SetVMImagePath(self): |
| 282 | """Detect VM image path in SDK and chroot.""" |
| 283 | if not self.image_path: |
Achuith Bhandarkar | fc75f69 | 2018-01-10 11:33:09 -0800 | [diff] [blame] | 284 | self.image_path = (self._GetCacheVMImagePath() or |
Achuith Bhandarkar | 1297dcf | 2017-11-21 12:03:48 -0800 | [diff] [blame] | 285 | self._GetBuiltVMImagePath()) |
| 286 | if not self.image_path: |
| 287 | raise VMError('No VM image found. Use cros chrome-sdk --download-vm.') |
| 288 | if not os.path.isfile(self.image_path): |
| 289 | raise VMError('VM image does not exist: %s' % self.image_path) |
| 290 | logging.debug('VM image path: %s', self.image_path) |
| 291 | |
Achuith Bhandarkar | b00bafc | 2018-09-11 16:30:13 -0700 | [diff] [blame] | 292 | def _WaitForSSHPort(self): |
| 293 | """Wait for SSH port to become available.""" |
| 294 | class _SSHPortInUseError(Exception): |
| 295 | """Exception for _CheckSSHPortBusy to throw.""" |
| 296 | |
| 297 | def _CheckSSHPortBusy(ssh_port): |
| 298 | """Check if the SSH port is in use.""" |
| 299 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 300 | try: |
| 301 | sock.bind((remote_access.LOCALHOST_IP, ssh_port)) |
| 302 | except socket.error as e: |
| 303 | if e.errno == errno.EADDRINUSE: |
| 304 | logging.info('SSH port %d in use...', self.ssh_port) |
| 305 | raise _SSHPortInUseError() |
| 306 | sock.close() |
| 307 | |
| 308 | try: |
| 309 | retry_util.RetryException( |
| 310 | exception=_SSHPortInUseError, |
| 311 | max_retry=7, |
| 312 | functor=lambda: _CheckSSHPortBusy(self.ssh_port), |
| 313 | sleep=1) |
| 314 | except _SSHPortInUseError: |
| 315 | raise VMError('SSH port %d in use' % self.ssh_port) |
| 316 | |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 317 | def Run(self): |
Achuith Bhandarkar | 665a9b3 | 2018-05-17 11:39:19 -0700 | [diff] [blame] | 318 | """Performs an action, one of start, stop, or run a command in the VM.""" |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 319 | if not self.start and not self.stop and not self.cmd: |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 320 | raise VMError('Must specify one of start, stop, or cmd.') |
Achuith Bhandarkar | 665a9b3 | 2018-05-17 11:39:19 -0700 | [diff] [blame] | 321 | |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 322 | if self.start: |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 323 | self.Start() |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 324 | if self.cmd: |
Achuith Bhandarkar | 665a9b3 | 2018-05-17 11:39:19 -0700 | [diff] [blame] | 325 | if not self.IsRunning(): |
| 326 | raise VMError('VM not running.') |
| 327 | self.RemoteCommand(self.cmd, stream_output=True) |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 328 | if self.stop: |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 329 | self.Stop() |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 330 | |
| 331 | def Start(self): |
| 332 | """Start the VM.""" |
| 333 | |
| 334 | self.Stop() |
Achuith Bhandarkar | b00bafc | 2018-09-11 16:30:13 -0700 | [diff] [blame] | 335 | self._WaitForSSHPort() |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 336 | |
Achuith Bhandarkar | 022d69c | 2016-10-05 14:28:14 -0700 | [diff] [blame] | 337 | logging.debug('Start VM') |
Achuith Bhandarkar | 1297dcf | 2017-11-21 12:03:48 -0800 | [diff] [blame] | 338 | self._SetQemuPath() |
| 339 | self._SetVMImagePath() |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 340 | |
Achuith Bhandarkar | fd5d185 | 2018-03-26 14:50:54 -0700 | [diff] [blame] | 341 | self._RmVMDir() |
| 342 | self._CreateVMDir() |
Ben Pastene | 243302c | 2018-09-21 16:06:08 -0700 | [diff] [blame] | 343 | if self.copy_on_write: |
| 344 | self._CreateQcow2Image() |
Mike Frysinger | 9708024 | 2017-09-13 01:58:45 -0400 | [diff] [blame] | 345 | # Make sure we can read these files later on by creating them as ourselves. |
| 346 | osutils.Touch(self.kvm_serial) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 347 | for pipe in [self.kvm_pipe_in, self.kvm_pipe_out]: |
Mike Frysinger | 0444f4c | 2018-08-03 15:12:46 -0400 | [diff] [blame] | 348 | os.mkfifo(pipe, 0o600) |
Mike Frysinger | 9708024 | 2017-09-13 01:58:45 -0400 | [diff] [blame] | 349 | osutils.Touch(self.pidfile) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 350 | |
Achuith Bhandarkar | 1297dcf | 2017-11-21 12:03:48 -0800 | [diff] [blame] | 351 | qemu_args = [self.qemu_path] |
| 352 | if self.qemu_bios_path: |
| 353 | if not os.path.isdir(self.qemu_bios_path): |
| 354 | raise VMError('Invalid QEMU bios path: %s' % self.qemu_bios_path) |
| 355 | qemu_args += ['-L', self.qemu_bios_path] |
Achuith Bhandarkar | 22bfedf | 2017-11-08 11:59:38 +0100 | [diff] [blame] | 356 | |
Achuith Bhandarkar | 4125965 | 2017-11-14 10:31:02 -0800 | [diff] [blame] | 357 | qemu_args += [ |
Po-Hsien Wang | 1cec8d1 | 2018-01-25 16:36:44 -0800 | [diff] [blame] | 358 | '-m', self.qemu_m, '-smp', str(self.qemu_smp), '-vga', 'virtio', |
| 359 | '-daemonize', '-usbdevice', 'tablet', |
Achuith Bhandarkar | 4125965 | 2017-11-14 10:31:02 -0800 | [diff] [blame] | 360 | '-pidfile', self.pidfile, |
| 361 | '-chardev', 'pipe,id=control_pipe,path=%s' % self.kvm_monitor, |
| 362 | '-serial', 'file:%s' % self.kvm_serial, |
| 363 | '-mon', 'chardev=control_pipe', |
Po-Hsien Wang | 1cec8d1 | 2018-01-25 16:36:44 -0800 | [diff] [blame] | 364 | # Append 'check' to warn if the requested CPU is not fully supported. |
| 365 | '-cpu', self.qemu_cpu + ',check', |
Lepton Wu | aa6cca4 | 2018-04-19 18:56:29 -0700 | [diff] [blame] | 366 | '-device', 'virtio-net,netdev=eth0', |
Achuith Bhandarkar | b00bafc | 2018-09-11 16:30:13 -0700 | [diff] [blame] | 367 | '-netdev', 'user,id=eth0,net=10.0.2.0/27,hostfwd=tcp:%s:%d-:22' |
| 368 | % (remote_access.LOCALHOST_IP, self.ssh_port), |
Nicolas Norvez | f527cdf | 2018-01-26 11:55:44 -0800 | [diff] [blame] | 369 | '-drive', 'file=%s,index=0,media=disk,cache=unsafe,format=%s' |
| 370 | % (self.image_path, self.image_format), |
Achuith Bhandarkar | 4125965 | 2017-11-14 10:31:02 -0800 | [diff] [blame] | 371 | ] |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 372 | if self.enable_kvm: |
Achuith Bhandarkar | 4125965 | 2017-11-14 10:31:02 -0800 | [diff] [blame] | 373 | qemu_args.append('-enable-kvm') |
Achuith Bhandarkar | b891adb | 2016-10-24 18:43:22 -0700 | [diff] [blame] | 374 | if not self.display: |
Achuith Bhandarkar | 4125965 | 2017-11-14 10:31:02 -0800 | [diff] [blame] | 375 | qemu_args.extend(['-display', 'none']) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 376 | logging.info('Pid file: %s', self.pidfile) |
| 377 | if not self.dry_run: |
Achuith Bhandarkar | 4125965 | 2017-11-14 10:31:02 -0800 | [diff] [blame] | 378 | self._RunCommand(qemu_args) |
Achuith Bhandarkar | be97748 | 2018-02-06 16:44:00 -0800 | [diff] [blame] | 379 | else: |
| 380 | logging.info(cros_build_lib.CmdToStr(qemu_args)) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 381 | |
| 382 | def _GetVMPid(self): |
| 383 | """Get the pid of the VM. |
| 384 | |
| 385 | Returns: |
| 386 | pid of the VM. |
| 387 | """ |
Achuith Bhandarkar | ee3163d | 2016-10-19 12:58:35 -0700 | [diff] [blame] | 388 | if not os.path.exists(self.vm_dir): |
| 389 | logging.debug('%s not present.', self.vm_dir) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 390 | return 0 |
| 391 | |
| 392 | if not os.path.exists(self.pidfile): |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 393 | logging.info('%s does not exist.', self.pidfile) |
| 394 | return 0 |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 395 | |
Mike Frysinger | 9708024 | 2017-09-13 01:58:45 -0400 | [diff] [blame] | 396 | pid = osutils.ReadFile(self.pidfile).rstrip() |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 397 | if not pid.isdigit(): |
Mike Frysinger | 9708024 | 2017-09-13 01:58:45 -0400 | [diff] [blame] | 398 | # Ignore blank/empty files. |
| 399 | if pid: |
| 400 | logging.error('%s in %s is not a pid.', pid, self.pidfile) |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 401 | return 0 |
| 402 | |
Achuith Bhandarkar | 022d69c | 2016-10-05 14:28:14 -0700 | [diff] [blame] | 403 | return int(pid) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 404 | |
| 405 | def IsRunning(self): |
| 406 | """Returns True if there's a running VM. |
| 407 | |
| 408 | Returns: |
| 409 | True if there's a running VM. |
| 410 | """ |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 411 | pid = self._GetVMPid() |
Achuith Bhandarkar | 022d69c | 2016-10-05 14:28:14 -0700 | [diff] [blame] | 412 | if not pid: |
| 413 | return False |
| 414 | |
| 415 | # Make sure the process actually exists. |
Mike Frysinger | 9708024 | 2017-09-13 01:58:45 -0400 | [diff] [blame] | 416 | return os.path.isdir('/proc/%i' % pid) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 417 | |
| 418 | def Stop(self): |
| 419 | """Stop the VM.""" |
Achuith Bhandarkar | 022d69c | 2016-10-05 14:28:14 -0700 | [diff] [blame] | 420 | logging.debug('Stop VM') |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 421 | |
| 422 | pid = self._GetVMPid() |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 423 | if pid: |
| 424 | logging.info('Killing %d.', pid) |
| 425 | if not self.dry_run: |
Achuith Bhandarkar | ee3163d | 2016-10-19 12:58:35 -0700 | [diff] [blame] | 426 | self._RunCommand(['kill', '-9', str(pid)], error_code_ok=True) |
Achuith Bhandarkar | fd5d185 | 2018-03-26 14:50:54 -0700 | [diff] [blame] | 427 | self._RmVMDir() |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 428 | |
Achuith Bhandarkar | 2f8352f | 2017-06-02 12:47:18 -0700 | [diff] [blame] | 429 | def _WaitForProcs(self): |
| 430 | """Wait for expected processes to launch.""" |
| 431 | class _TooFewPidsException(Exception): |
| 432 | """Exception for _GetRunningPids to throw.""" |
| 433 | |
| 434 | def _GetRunningPids(exe, numpids): |
| 435 | pids = self.remote.GetRunningPids(exe, full_path=False) |
| 436 | logging.info('%s pids: %s', exe, repr(pids)) |
| 437 | if len(pids) < numpids: |
| 438 | raise _TooFewPidsException() |
| 439 | |
| 440 | def _WaitForProc(exe, numpids): |
| 441 | try: |
| 442 | retry_util.RetryException( |
Achuith Bhandarkar | 0e7b850 | 2017-06-12 15:32:41 -0700 | [diff] [blame] | 443 | exception=_TooFewPidsException, |
Achuith Bhandarkar | b00bafc | 2018-09-11 16:30:13 -0700 | [diff] [blame] | 444 | max_retry=5, |
Achuith Bhandarkar | 2f8352f | 2017-06-02 12:47:18 -0700 | [diff] [blame] | 445 | functor=lambda: _GetRunningPids(exe, numpids), |
| 446 | sleep=2) |
| 447 | except _TooFewPidsException: |
| 448 | raise VMError('_WaitForProcs failed: timed out while waiting for ' |
| 449 | '%d %s processes to start.' % (numpids, exe)) |
| 450 | |
| 451 | # We could also wait for session_manager, nacl_helper, etc, but chrome is |
| 452 | # the long pole. We expect the parent, 2 zygotes, gpu-process, renderer. |
| 453 | # This could potentially break with Mustash. |
| 454 | _WaitForProc('chrome', 5) |
| 455 | |
| 456 | def WaitForBoot(self): |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 457 | """Wait for the VM to boot up. |
| 458 | |
Achuith Bhandarkar | 2f8352f | 2017-06-02 12:47:18 -0700 | [diff] [blame] | 459 | Wait for ssh connection to become active, and wait for all expected chrome |
| 460 | processes to be launched. |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 461 | """ |
Achuith Bhandarkar | ee3163d | 2016-10-19 12:58:35 -0700 | [diff] [blame] | 462 | if not os.path.exists(self.vm_dir): |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 463 | self.Start() |
| 464 | |
Achuith Bhandarkar | 2f8352f | 2017-06-02 12:47:18 -0700 | [diff] [blame] | 465 | try: |
| 466 | result = retry_util.RetryException( |
Achuith Bhandarkar | 0e7b850 | 2017-06-12 15:32:41 -0700 | [diff] [blame] | 467 | exception=remote_access.SSHConnectionError, |
Achuith Bhandarkar | 2f8352f | 2017-06-02 12:47:18 -0700 | [diff] [blame] | 468 | max_retry=10, |
| 469 | functor=lambda: self.RemoteCommand(cmd=['echo']), |
| 470 | sleep=5) |
| 471 | except remote_access.SSHConnectionError: |
| 472 | raise VMError('WaitForBoot timed out trying to connect to VM.') |
| 473 | |
| 474 | if result.returncode != 0: |
| 475 | raise VMError('WaitForBoot failed: %s.' % result.error) |
| 476 | |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 477 | # Chrome can take a while to start with software emulation. |
| 478 | if not self.enable_kvm: |
| 479 | self._WaitForProcs() |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 480 | |
Achuith Bhandarkar | bad755b | 2018-06-09 16:05:01 -0700 | [diff] [blame] | 481 | def RemoteCommand(self, cmd, stream_output=False, **kwargs): |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 482 | """Run a remote command in the VM. |
| 483 | |
| 484 | Args: |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 485 | cmd: command to run. |
Achuith Bhandarkar | bad755b | 2018-06-09 16:05:01 -0700 | [diff] [blame] | 486 | stream_output: Stream output of long-running commands. |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 487 | kwargs: additional args (see documentation for RemoteDevice.RunCommand). |
Achuith Bhandarkar | 665a9b3 | 2018-05-17 11:39:19 -0700 | [diff] [blame] | 488 | |
| 489 | Returns: |
| 490 | cros_build_lib.CommandResult object. |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 491 | """ |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 492 | if not self.dry_run: |
Achuith Bhandarkar | bad755b | 2018-06-09 16:05:01 -0700 | [diff] [blame] | 493 | kwargs.setdefault('error_code_ok', True) |
| 494 | if stream_output: |
| 495 | kwargs.setdefault('capture_output', False) |
| 496 | else: |
| 497 | kwargs.setdefault('combine_stdout_stderr', True) |
| 498 | kwargs.setdefault('log_output', True) |
| 499 | return self.remote.RunCommand(cmd, debug_level=logging.INFO, **kwargs) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 500 | |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 501 | @staticmethod |
Achuith Bhandarkar | 2beae29 | 2018-03-26 16:44:53 -0700 | [diff] [blame] | 502 | def GetParser(): |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 503 | """Parse a list of args. |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 504 | |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 505 | Args: |
| 506 | argv: list of command line arguments. |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 507 | |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 508 | Returns: |
| 509 | List of parsed opts. |
| 510 | """ |
| 511 | parser = commandline.ArgumentParser(description=__doc__) |
| 512 | parser.add_argument('--start', action='store_true', default=False, |
| 513 | help='Start the VM.') |
| 514 | parser.add_argument('--stop', action='store_true', default=False, |
| 515 | help='Stop the VM.') |
Ben Pastene | 243302c | 2018-09-21 16:06:08 -0700 | [diff] [blame] | 516 | parser.add_argument('--image-path', type='path', |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 517 | help='Path to VM image to launch with --start.') |
Nicolas Norvez | f527cdf | 2018-01-26 11:55:44 -0800 | [diff] [blame] | 518 | parser.add_argument('--image-format', default=VM.IMAGE_FORMAT, |
| 519 | help='Format of the VM image (raw, qcow2, ...).') |
Ben Pastene | 243302c | 2018-09-21 16:06:08 -0700 | [diff] [blame] | 520 | parser.add_argument('--qemu-path', type='path', |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 521 | help='Path of qemu binary to launch with --start.') |
Po-Hsien Wang | 1cec8d1 | 2018-01-25 16:36:44 -0800 | [diff] [blame] | 522 | parser.add_argument('--qemu-m', type=str, default='8G', |
| 523 | help='Memory argument that will be passed to qemu.') |
| 524 | parser.add_argument('--qemu-smp', type=int, default='0', |
| 525 | help='SMP argument that will be passed to qemu. (0 ' |
| 526 | 'means auto-detection.)') |
Po-Hsien Wang | c5b335f | 2018-02-06 18:36:16 -0800 | [diff] [blame] | 527 | # TODO(pwang): replace SandyBridge to Haswell-noTSX once lab machine |
| 528 | # running VMTest all migrate to GCE. |
Po-Hsien Wang | 1cec8d1 | 2018-01-25 16:36:44 -0800 | [diff] [blame] | 529 | parser.add_argument('--qemu-cpu', type=str, |
Po-Hsien Wang | c5b335f | 2018-02-06 18:36:16 -0800 | [diff] [blame] | 530 | default='SandyBridge,-invpcid,-tsc-deadline', |
Po-Hsien Wang | 1cec8d1 | 2018-01-25 16:36:44 -0800 | [diff] [blame] | 531 | help='CPU argument that will be passed to qemu.') |
Ben Pastene | 243302c | 2018-09-21 16:06:08 -0700 | [diff] [blame] | 532 | parser.add_argument('--qemu-bios-path', type='path', |
Achuith Bhandarkar | 4125965 | 2017-11-14 10:31:02 -0800 | [diff] [blame] | 533 | help='Path of directory with qemu bios files.') |
Ben Pastene | 243302c | 2018-09-21 16:06:08 -0700 | [diff] [blame] | 534 | parser.add_argument('--copy-on-write', action='store_true', default=False, |
| 535 | help='Generates a temporary copy-on-write image backed ' |
| 536 | 'by the normal boot image. All filesystem changes ' |
| 537 | 'will instead be reflected in the temporary ' |
| 538 | 'image.') |
| 539 | parser.add_argument('--qemu-img-path', type='path', |
| 540 | help='Path to qemu-img binary used to create temporary ' |
| 541 | 'copy-on-write images.') |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 542 | parser.add_argument('--disable-kvm', dest='enable_kvm', |
| 543 | action='store_false', default=True, |
| 544 | help='Disable KVM, use software emulation.') |
| 545 | parser.add_argument('--no-display', dest='display', |
| 546 | action='store_false', default=True, |
| 547 | help='Do not display video output.') |
| 548 | parser.add_argument('--ssh-port', type=int, default=VM.SSH_PORT, |
| 549 | help='ssh port to communicate with VM.') |
Achuith Bhandarkar | 195b951 | 2018-08-15 17:14:32 -0700 | [diff] [blame] | 550 | parser.add_argument('--private-key', help='Path to ssh private key.') |
Achuith Bhandarkar | 1297dcf | 2017-11-21 12:03:48 -0800 | [diff] [blame] | 551 | sdk_board_env = os.environ.get(cros_chrome_sdk.SDKFetcher.SDK_BOARD_ENV) |
| 552 | parser.add_argument('--board', default=sdk_board_env, help='Board to use.') |
Ben Pastene | 243302c | 2018-09-21 16:06:08 -0700 | [diff] [blame] | 553 | parser.add_argument('--cache-dir', type='path', |
Achuith Bhandarkar | 02ef76b | 2018-04-05 10:47:33 +0200 | [diff] [blame] | 554 | default=path_util.GetCacheDir(), |
| 555 | help='Cache directory to use.') |
Ben Pastene | 243302c | 2018-09-21 16:06:08 -0700 | [diff] [blame] | 556 | parser.add_argument('--vm-dir', type='path', |
Achuith Bhandarkar | 2beae29 | 2018-03-26 16:44:53 -0700 | [diff] [blame] | 557 | help='Temp VM directory to use.') |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 558 | parser.add_argument('--dry-run', action='store_true', default=False, |
| 559 | help='dry run for debugging.') |
| 560 | parser.add_argument('--cmd', action='store_true', default=False, |
| 561 | help='Run a command in the VM.') |
| 562 | parser.add_argument('args', nargs=argparse.REMAINDER, |
| 563 | help='Command to run in the VM.') |
Achuith Bhandarkar | 2beae29 | 2018-03-26 16:44:53 -0700 | [diff] [blame] | 564 | return parser |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 565 | |
| 566 | def main(argv): |
Achuith Bhandarkar | 2beae29 | 2018-03-26 16:44:53 -0700 | [diff] [blame] | 567 | opts = VM.GetParser().parse_args(argv) |
| 568 | opts.Freeze() |
| 569 | |
| 570 | vm = VM(opts) |
Achuith Bhandarkar | 2a39adf | 2017-10-30 10:24:45 +0100 | [diff] [blame] | 571 | vm.Run() |