blob: 89f69769fc681e7f038723c82953484149a0e442 [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
Achuith Bhandarkard8d19292016-05-03 14:32:58 -07002# 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 Frysinger666566c2016-09-21 00:00:21 -04006"""Script for VM Management."""
Achuith Bhandarkard8d19292016-05-03 14:32:58 -07007
8from __future__ import print_function
9
Achuith Bhandarkar2a39adf2017-10-30 10:24:45 +010010import argparse
Achuith Bhandarkar22bfedf2017-11-08 11:59:38 +010011import distutils.version
Achuith Bhandarkard8d19292016-05-03 14:32:58 -070012import os
Achuith Bhandarkar22bfedf2017-11-08 11:59:38 +010013import re
Achuith Bhandarkard8d19292016-05-03 14:32:58 -070014
Achuith Bhandarkar1297dcf2017-11-21 12:03:48 -080015from chromite.cli.cros import cros_chrome_sdk
16from chromite.lib import cache
Achuith Bhandarkard8d19292016-05-03 14:32:58 -070017from chromite.lib import commandline
Achuith Bhandarkar1297dcf2017-11-21 12:03:48 -080018from chromite.lib import constants
Achuith Bhandarkard8d19292016-05-03 14:32:58 -070019from chromite.lib import cros_build_lib
20from chromite.lib import cros_logging as logging
Achuith Bhandarkarf4950ba2016-10-11 15:40:07 -070021from chromite.lib import osutils
Achuith Bhandarkar1297dcf2017-11-21 12:03:48 -080022from chromite.lib import path_util
Achuith Bhandarkard8d19292016-05-03 14:32:58 -070023from chromite.lib import remote_access
Achuith Bhandarkar2f8352f2017-06-02 12:47:18 -070024from chromite.lib import retry_util
Achuith Bhandarkard8d19292016-05-03 14:32:58 -070025
26
27class VMError(Exception):
28 """Exception for VM failures."""
29
30 def __init__(self, message):
31 super(VMError, self).__init__()
32 logging.error(message)
33
34
35class VM(object):
36 """Class for managing a VM."""
37
38 SSH_PORT = 9222
Achuith Bhandarkard8d19292016-05-03 14:32:58 -070039
Achuith Bhandarkar2a39adf2017-10-30 10:24:45 +010040 def __init__(self, argv):
Achuith Bhandarkard8d19292016-05-03 14:32:58 -070041 """Initialize VM.
42
43 Args:
Achuith Bhandarkar2a39adf2017-10-30 10:24:45 +010044 argv: command line args.
Achuith Bhandarkard8d19292016-05-03 14:32:58 -070045 """
Achuith Bhandarkar2a39adf2017-10-30 10:24:45 +010046 opts = self._ParseArgs(argv)
47 opts.Freeze()
Achuith Bhandarkard8d19292016-05-03 14:32:58 -070048
Achuith Bhandarkar2a39adf2017-10-30 10:24:45 +010049 self.qemu_path = opts.qemu_path
Achuith Bhandarkar41259652017-11-14 10:31:02 -080050 self.qemu_bios_path = opts.qemu_bios_path
Achuith Bhandarkar2a39adf2017-10-30 10:24:45 +010051 self.enable_kvm = opts.enable_kvm
Achuith Bhandarkarf877da22017-09-12 12:27:39 -070052 # We don't need sudo access for software emulation or if /dev/kvm is
53 # writeable.
Achuith Bhandarkar2a39adf2017-10-30 10:24:45 +010054 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 Bhandarkar1297dcf2017-11-21 12:03:48 -080057 self.board = opts.board
Achuith Bhandarkar2a39adf2017-10-30 10:24:45 +010058 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 Bhandarkard8d19292016-05-03 14:32:58 -070064
Achuith Bhandarkare7d564a2018-01-17 10:23:52 -080065 self.vm_dir = os.path.join(osutils.GetGlobalTempDir(),
66 'cros_vm_%d' % self.ssh_port)
Achuith Bhandarkaree3163d2016-10-19 12:58:35 -070067 if os.path.exists(self.vm_dir):
68 # For security, ensure that vm_dir is not a symlink, and is owned by us or
69 # by root.
70 assert not os.path.islink(self.vm_dir), \
71 'VM state dir is misconfigured; please recreate: %s' % self.vm_dir
72 st_uid = os.stat(self.vm_dir).st_uid
73 assert st_uid == 0 or st_uid == os.getuid(), \
74 'VM state dir is misconfigured; please recreate: %s' % self.vm_dir
75
76 self.pidfile = os.path.join(self.vm_dir, 'kvm.pid')
77 self.kvm_monitor = os.path.join(self.vm_dir, 'kvm.monitor')
Achuith Bhandarkard8d19292016-05-03 14:32:58 -070078 self.kvm_pipe_in = '%s.in' % self.kvm_monitor # to KVM
79 self.kvm_pipe_out = '%s.out' % self.kvm_monitor # from KVM
80 self.kvm_serial = '%s.serial' % self.kvm_monitor
81
Achuith Bhandarkar65d1a892017-05-08 14:13:12 -070082 self.remote = remote_access.RemoteDevice(remote_access.LOCALHOST,
Achuith Bhandarkar2a39adf2017-10-30 10:24:45 +010083 port=self.ssh_port)
Achuith Bhandarkar65d1a892017-05-08 14:13:12 -070084
Achuith Bhandarkard8d19292016-05-03 14:32:58 -070085 # TODO(achuith): support nographics, snapshot, mem_path, usb_passthrough,
86 # moblab, etc.
87
Achuith Bhandarkaree3163d2016-10-19 12:58:35 -070088 def _RunCommand(self, *args, **kwargs):
89 """Use SudoRunCommand or RunCommand as necessary."""
90 if self.use_sudo:
91 return cros_build_lib.SudoRunCommand(*args, **kwargs)
92 else:
93 return cros_build_lib.RunCommand(*args, **kwargs)
94
95 def _CleanupFiles(self, recreate):
96 """Cleanup vm_dir.
Achuith Bhandarkard8d19292016-05-03 14:32:58 -070097
98 Args:
Achuith Bhandarkaree3163d2016-10-19 12:58:35 -070099 recreate: recreate vm_dir.
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700100 """
Mike Frysinger97080242017-09-13 01:58:45 -0400101 osutils.RmDir(self.vm_dir, ignore_missing=True, sudo=self.use_sudo)
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700102 if recreate:
Mike Frysinger97080242017-09-13 01:58:45 -0400103 osutils.SafeMakedirs(self.vm_dir)
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700104
Achuith Bhandarkarfc75f692018-01-10 11:33:09 -0800105 def _GetCachePath(self, key):
106 """Get cache path for key.
107
108 Args:
109 key: cache key.
110 """
111 tarball_cache = cache.TarballCache(os.path.join(
112 path_util.GetCacheDir(),
113 cros_chrome_sdk.COMMAND_NAME,
114 cros_chrome_sdk.SDKFetcher.TARBALL_CACHE))
115 lkgm = cros_chrome_sdk.SDKFetcher.GetChromeLKGM()
116 if lkgm:
117 cache_key = (self.board, lkgm, key)
118 with tarball_cache.Lookup(cache_key) as ref:
119 if ref.Exists():
120 return ref.path
121 return None
122
Achuith Bhandarkar22bfedf2017-11-08 11:59:38 +0100123 @cros_build_lib.MemoizedSingleCall
124 def QemuVersion(self):
125 """Determine QEMU version."""
126 version_str = self._RunCommand([self.qemu_path, '--version'],
127 capture_output=True).output
128 # version string looks like one of these:
129 # QEMU emulator version 2.0.0 (Debian 2.0.0+dfsg-2ubuntu1.36), Copyright (c)
130 # 2003-2008 Fabrice Bellard
131 #
132 # QEMU emulator version 2.6.0, Copyright (c) 2003-2008 Fabrice Bellard
133 #
134 # qemu-x86_64 version 2.10.1
135 # Copyright (c) 2003-2017 Fabrice Bellard and the QEMU Project developers
136 m = re.search(r"version ([0-9.]+)", version_str)
137 if not m:
138 raise VMError('Unable to determine QEMU version from:\n%s.' % version_str)
139 return m.group(1)
140
141 def _CheckQemuMinVersion(self):
142 """Ensure minimum QEMU version."""
143 min_qemu_version = '2.6.0'
144 logging.info('QEMU version %s', self.QemuVersion())
145 LooseVersion = distutils.version.LooseVersion
146 if LooseVersion(self.QemuVersion()) < LooseVersion(min_qemu_version):
147 raise VMError('QEMU %s is the minimum supported version. You have %s.'
148 % (min_qemu_version, self.QemuVersion()))
149
Achuith Bhandarkar1297dcf2017-11-21 12:03:48 -0800150 def _SetQemuPath(self):
151 """Find a suitable Qemu executable."""
Achuith Bhandarkarfc75f692018-01-10 11:33:09 -0800152 qemu_exe = 'qemu-system-x86_64'
153 qemu_exe_path = os.path.join('usr/bin', qemu_exe)
154
155 # Check SDK cache.
Achuith Bhandarkar1297dcf2017-11-21 12:03:48 -0800156 if not self.qemu_path:
Achuith Bhandarkarfc75f692018-01-10 11:33:09 -0800157 qemu_dir = self._GetCachePath(cros_chrome_sdk.SDKFetcher.QEMU_BIN_KEY)
158 if qemu_dir:
159 qemu_path = os.path.join(qemu_dir, qemu_exe_path)
160 if os.path.isfile(qemu_path):
161 self.qemu_path = qemu_path
162
163 # Check chroot.
Achuith Bhandarkar1297dcf2017-11-21 12:03:48 -0800164 if not self.qemu_path:
Achuith Bhandarkarfc75f692018-01-10 11:33:09 -0800165 qemu_path = os.path.join(
166 constants.SOURCE_ROOT, constants.DEFAULT_CHROOT_DIR, qemu_exe_path)
167 if os.path.isfile(qemu_path):
168 self.qemu_path = qemu_path
169
170 # Check system.
171 if not self.qemu_path:
172 self.qemu_path = osutils.Which(qemu_exe)
173
174 if not self.qemu_path or not os.path.isfile(self.qemu_path):
175 raise VMError('QEMU not found.')
176 logging.debug('QEMU path: %s', self.qemu_path)
Achuith Bhandarkar1297dcf2017-11-21 12:03:48 -0800177 self._CheckQemuMinVersion()
178
179 def _GetBuiltVMImagePath(self):
180 """Get path of a locally built VM image."""
Achuith Bhandarkarfc75f692018-01-10 11:33:09 -0800181 vm_image_path = os.path.join(constants.SOURCE_ROOT, 'src/build/images',
182 cros_build_lib.GetBoard(self.board),
183 'latest', constants.VM_IMAGE_BIN)
184 return vm_image_path if os.path.isfile(vm_image_path) else None
Achuith Bhandarkar1297dcf2017-11-21 12:03:48 -0800185
Achuith Bhandarkarfc75f692018-01-10 11:33:09 -0800186 def _GetCacheVMImagePath(self):
187 """Get path of a cached VM image."""
188 cache_path = self._GetCachePath(constants.VM_IMAGE_TAR)
189 if cache_path:
190 vm_image = os.path.join(cache_path, constants.VM_IMAGE_BIN)
191 if os.path.isfile(vm_image):
192 return vm_image
Achuith Bhandarkar1297dcf2017-11-21 12:03:48 -0800193 return None
194
195 def _SetVMImagePath(self):
196 """Detect VM image path in SDK and chroot."""
197 if not self.image_path:
Achuith Bhandarkarfc75f692018-01-10 11:33:09 -0800198 self.image_path = (self._GetCacheVMImagePath() or
Achuith Bhandarkar1297dcf2017-11-21 12:03:48 -0800199 self._GetBuiltVMImagePath())
200 if not self.image_path:
201 raise VMError('No VM image found. Use cros chrome-sdk --download-vm.')
202 if not os.path.isfile(self.image_path):
203 raise VMError('VM image does not exist: %s' % self.image_path)
204 logging.debug('VM image path: %s', self.image_path)
205
Achuith Bhandarkar2a39adf2017-10-30 10:24:45 +0100206 def Run(self):
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700207 """Performs an action, one of start, stop, or run a command in the VM.
208
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700209 Returns:
210 cmd output.
211 """
212
Achuith Bhandarkar2a39adf2017-10-30 10:24:45 +0100213 if not self.start and not self.stop and not self.cmd:
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700214 raise VMError('Must specify one of start, stop, or cmd.')
Achuith Bhandarkar2a39adf2017-10-30 10:24:45 +0100215 if self.start:
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700216 self.Start()
Achuith Bhandarkar2a39adf2017-10-30 10:24:45 +0100217 if self.cmd:
218 return self.RemoteCommand(self.cmd)
219 if self.stop:
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700220 self.Stop()
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700221
222 def Start(self):
223 """Start the VM."""
224
225 self.Stop()
226
Achuith Bhandarkar022d69c2016-10-05 14:28:14 -0700227 logging.debug('Start VM')
Achuith Bhandarkar1297dcf2017-11-21 12:03:48 -0800228 self._SetQemuPath()
229 self._SetVMImagePath()
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700230
231 self._CleanupFiles(recreate=True)
Mike Frysinger97080242017-09-13 01:58:45 -0400232 # Make sure we can read these files later on by creating them as ourselves.
233 osutils.Touch(self.kvm_serial)
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700234 for pipe in [self.kvm_pipe_in, self.kvm_pipe_out]:
235 os.mkfifo(pipe, 0600)
Mike Frysinger97080242017-09-13 01:58:45 -0400236 osutils.Touch(self.pidfile)
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700237
Achuith Bhandarkar1297dcf2017-11-21 12:03:48 -0800238 qemu_args = [self.qemu_path]
239 if self.qemu_bios_path:
240 if not os.path.isdir(self.qemu_bios_path):
241 raise VMError('Invalid QEMU bios path: %s' % self.qemu_bios_path)
242 qemu_args += ['-L', self.qemu_bios_path]
Achuith Bhandarkar22bfedf2017-11-08 11:59:38 +0100243
Achuith Bhandarkar41259652017-11-14 10:31:02 -0800244 qemu_args += [
245 '-m', '2G', '-smp', '4', '-vga', 'virtio', '-daemonize',
Nicolas Norvez02525112017-11-30 16:29:23 -0800246 '-usbdevice', 'tablet',
Achuith Bhandarkar41259652017-11-14 10:31:02 -0800247 '-pidfile', self.pidfile,
248 '-chardev', 'pipe,id=control_pipe,path=%s' % self.kvm_monitor,
249 '-serial', 'file:%s' % self.kvm_serial,
250 '-mon', 'chardev=control_pipe',
251 # Qemu-vlans are used by qemu to separate out network traffic on the
252 # slirp network bridge. qemu forwards traffic on a slirp vlan to all
253 # ports conected on that vlan. By default, slirp ports are on vlan
254 # 0. We explicitly set a vlan here so that another qemu VM using
255 # slirp doesn't conflict with our network traffic.
256 '-net', 'nic,model=virtio,vlan=%d' % self.ssh_port,
257 '-net', 'user,hostfwd=tcp:127.0.0.1:%d-:22,vlan=%d'
258 % (self.ssh_port, self.ssh_port),
259 '-drive', 'file=%s,index=0,media=disk,cache=unsafe,format=raw'
260 % self.image_path,
261 ]
Achuith Bhandarkarf4950ba2016-10-11 15:40:07 -0700262 if self.enable_kvm:
Achuith Bhandarkar41259652017-11-14 10:31:02 -0800263 qemu_args.append('-enable-kvm')
Achuith Bhandarkarb891adb2016-10-24 18:43:22 -0700264 if not self.display:
Achuith Bhandarkar41259652017-11-14 10:31:02 -0800265 qemu_args.extend(['-display', 'none'])
266 logging.info(cros_build_lib.CmdToStr(qemu_args))
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700267 logging.info('Pid file: %s', self.pidfile)
268 if not self.dry_run:
Achuith Bhandarkar41259652017-11-14 10:31:02 -0800269 self._RunCommand(qemu_args)
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700270
271 def _GetVMPid(self):
272 """Get the pid of the VM.
273
274 Returns:
275 pid of the VM.
276 """
Achuith Bhandarkaree3163d2016-10-19 12:58:35 -0700277 if not os.path.exists(self.vm_dir):
278 logging.debug('%s not present.', self.vm_dir)
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700279 return 0
280
281 if not os.path.exists(self.pidfile):
Achuith Bhandarkarf4950ba2016-10-11 15:40:07 -0700282 logging.info('%s does not exist.', self.pidfile)
283 return 0
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700284
Mike Frysinger97080242017-09-13 01:58:45 -0400285 pid = osutils.ReadFile(self.pidfile).rstrip()
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700286 if not pid.isdigit():
Mike Frysinger97080242017-09-13 01:58:45 -0400287 # Ignore blank/empty files.
288 if pid:
289 logging.error('%s in %s is not a pid.', pid, self.pidfile)
Achuith Bhandarkarf4950ba2016-10-11 15:40:07 -0700290 return 0
291
Achuith Bhandarkar022d69c2016-10-05 14:28:14 -0700292 return int(pid)
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700293
294 def IsRunning(self):
295 """Returns True if there's a running VM.
296
297 Returns:
298 True if there's a running VM.
299 """
Achuith Bhandarkarf4950ba2016-10-11 15:40:07 -0700300 pid = self._GetVMPid()
Achuith Bhandarkar022d69c2016-10-05 14:28:14 -0700301 if not pid:
302 return False
303
304 # Make sure the process actually exists.
Mike Frysinger97080242017-09-13 01:58:45 -0400305 return os.path.isdir('/proc/%i' % pid)
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700306
307 def Stop(self):
308 """Stop the VM."""
Achuith Bhandarkar022d69c2016-10-05 14:28:14 -0700309 logging.debug('Stop VM')
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700310
311 pid = self._GetVMPid()
Achuith Bhandarkarf4950ba2016-10-11 15:40:07 -0700312 if pid:
313 logging.info('Killing %d.', pid)
314 if not self.dry_run:
Achuith Bhandarkaree3163d2016-10-19 12:58:35 -0700315 self._RunCommand(['kill', '-9', str(pid)], error_code_ok=True)
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700316
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700317 self._CleanupFiles(recreate=False)
318
Achuith Bhandarkar2f8352f2017-06-02 12:47:18 -0700319 def _WaitForProcs(self):
320 """Wait for expected processes to launch."""
321 class _TooFewPidsException(Exception):
322 """Exception for _GetRunningPids to throw."""
323
324 def _GetRunningPids(exe, numpids):
325 pids = self.remote.GetRunningPids(exe, full_path=False)
326 logging.info('%s pids: %s', exe, repr(pids))
327 if len(pids) < numpids:
328 raise _TooFewPidsException()
329
330 def _WaitForProc(exe, numpids):
331 try:
332 retry_util.RetryException(
Achuith Bhandarkar0e7b8502017-06-12 15:32:41 -0700333 exception=_TooFewPidsException,
Achuith Bhandarkar2f8352f2017-06-02 12:47:18 -0700334 max_retry=20,
335 functor=lambda: _GetRunningPids(exe, numpids),
336 sleep=2)
337 except _TooFewPidsException:
338 raise VMError('_WaitForProcs failed: timed out while waiting for '
339 '%d %s processes to start.' % (numpids, exe))
340
341 # We could also wait for session_manager, nacl_helper, etc, but chrome is
342 # the long pole. We expect the parent, 2 zygotes, gpu-process, renderer.
343 # This could potentially break with Mustash.
344 _WaitForProc('chrome', 5)
345
346 def WaitForBoot(self):
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700347 """Wait for the VM to boot up.
348
Achuith Bhandarkar2f8352f2017-06-02 12:47:18 -0700349 Wait for ssh connection to become active, and wait for all expected chrome
350 processes to be launched.
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700351 """
Achuith Bhandarkaree3163d2016-10-19 12:58:35 -0700352 if not os.path.exists(self.vm_dir):
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700353 self.Start()
354
Achuith Bhandarkar2f8352f2017-06-02 12:47:18 -0700355 try:
356 result = retry_util.RetryException(
Achuith Bhandarkar0e7b8502017-06-12 15:32:41 -0700357 exception=remote_access.SSHConnectionError,
Achuith Bhandarkar2f8352f2017-06-02 12:47:18 -0700358 max_retry=10,
359 functor=lambda: self.RemoteCommand(cmd=['echo']),
360 sleep=5)
361 except remote_access.SSHConnectionError:
362 raise VMError('WaitForBoot timed out trying to connect to VM.')
363
364 if result.returncode != 0:
365 raise VMError('WaitForBoot failed: %s.' % result.error)
366
Achuith Bhandarkar2a39adf2017-10-30 10:24:45 +0100367 # Chrome can take a while to start with software emulation.
368 if not self.enable_kvm:
369 self._WaitForProcs()
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700370
Achuith Bhandarkar2a39adf2017-10-30 10:24:45 +0100371 def RemoteCommand(self, cmd, **kwargs):
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700372 """Run a remote command in the VM.
373
374 Args:
Achuith Bhandarkar2a39adf2017-10-30 10:24:45 +0100375 cmd: command to run.
376 kwargs: additional args (see documentation for RemoteDevice.RunCommand).
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700377 """
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700378 if not self.dry_run:
Achuith Bhandarkar65d1a892017-05-08 14:13:12 -0700379 return self.remote.RunCommand(cmd, debug_level=logging.INFO,
380 combine_stdout_stderr=True,
381 log_output=True,
Achuith Bhandarkar2a39adf2017-10-30 10:24:45 +0100382 error_code_ok=True,
383 **kwargs)
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700384
Achuith Bhandarkar2a39adf2017-10-30 10:24:45 +0100385 @staticmethod
386 def _ParseArgs(argv):
387 """Parse a list of args.
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700388
Achuith Bhandarkar2a39adf2017-10-30 10:24:45 +0100389 Args:
390 argv: list of command line arguments.
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700391
Achuith Bhandarkar2a39adf2017-10-30 10:24:45 +0100392 Returns:
393 List of parsed opts.
394 """
395 parser = commandline.ArgumentParser(description=__doc__)
396 parser.add_argument('--start', action='store_true', default=False,
397 help='Start the VM.')
398 parser.add_argument('--stop', action='store_true', default=False,
399 help='Stop the VM.')
400 parser.add_argument('--image-path', type='path',
401 help='Path to VM image to launch with --start.')
402 parser.add_argument('--qemu-path', type='path',
403 help='Path of qemu binary to launch with --start.')
Achuith Bhandarkar41259652017-11-14 10:31:02 -0800404 parser.add_argument('--qemu-bios-path', type='path',
405 help='Path of directory with qemu bios files.')
Achuith Bhandarkar2a39adf2017-10-30 10:24:45 +0100406 parser.add_argument('--disable-kvm', dest='enable_kvm',
407 action='store_false', default=True,
408 help='Disable KVM, use software emulation.')
409 parser.add_argument('--no-display', dest='display',
410 action='store_false', default=True,
411 help='Do not display video output.')
412 parser.add_argument('--ssh-port', type=int, default=VM.SSH_PORT,
413 help='ssh port to communicate with VM.')
Achuith Bhandarkar1297dcf2017-11-21 12:03:48 -0800414 sdk_board_env = os.environ.get(cros_chrome_sdk.SDKFetcher.SDK_BOARD_ENV)
415 parser.add_argument('--board', default=sdk_board_env, help='Board to use.')
Achuith Bhandarkar2a39adf2017-10-30 10:24:45 +0100416 parser.add_argument('--dry-run', action='store_true', default=False,
417 help='dry run for debugging.')
418 parser.add_argument('--cmd', action='store_true', default=False,
419 help='Run a command in the VM.')
420 parser.add_argument('args', nargs=argparse.REMAINDER,
421 help='Command to run in the VM.')
422 return parser.parse_args(argv)
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700423
424def main(argv):
Achuith Bhandarkar2a39adf2017-10-30 10:24:45 +0100425 vm = VM(argv)
426 vm.Run()