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