Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 1 | # Copyright 2016 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Mike Frysinger | 666566c | 2016-09-21 00:00:21 -0400 | [diff] [blame] | 5 | """Script for VM Management.""" |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 6 | |
| 7 | from __future__ import print_function |
| 8 | |
| 9 | import os |
| 10 | import time |
| 11 | |
| 12 | from chromite.lib import commandline |
| 13 | from chromite.lib import cros_build_lib |
| 14 | from chromite.lib import cros_logging as logging |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 15 | from chromite.lib import osutils |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 16 | from chromite.lib import remote_access |
| 17 | |
| 18 | |
| 19 | class VMError(Exception): |
| 20 | """Exception for VM failures.""" |
| 21 | |
| 22 | def __init__(self, message): |
| 23 | super(VMError, self).__init__() |
| 24 | logging.error(message) |
| 25 | |
| 26 | |
| 27 | class VM(object): |
| 28 | """Class for managing a VM.""" |
| 29 | |
| 30 | SSH_PORT = 9222 |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 31 | |
| 32 | |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 33 | def __init__(self, image_path=None, qemu_path=None, enable_kvm=True, |
| 34 | ssh_port=SSH_PORT, dry_run=False): |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 35 | """Initialize VM. |
| 36 | |
| 37 | Args: |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 38 | image_path: path of vm image. |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 39 | qemu_path: path to qemu binary. |
| 40 | enable_kvm: enable kvm (kernel support for virtualization). |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 41 | ssh_port: ssh port to use. |
| 42 | dry_run: disable VM commands. |
| 43 | """ |
| 44 | |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 45 | self.qemu_path = qemu_path |
| 46 | self.enable_kvm = enable_kvm |
Achuith Bhandarkar | ee3163d | 2016-10-19 12:58:35 -0700 | [diff] [blame] | 47 | # Software emulation doesn't need sudo access. |
| 48 | self.use_sudo = enable_kvm |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 49 | self.image_path = image_path |
| 50 | self.ssh_port = ssh_port |
| 51 | self.dry_run = dry_run |
| 52 | |
Achuith Bhandarkar | ee3163d | 2016-10-19 12:58:35 -0700 | [diff] [blame] | 53 | self.vm_dir = os.path.join(osutils.GetGlobalTempDir(), 'cros_vm') |
| 54 | if os.path.exists(self.vm_dir): |
| 55 | # For security, ensure that vm_dir is not a symlink, and is owned by us or |
| 56 | # by root. |
| 57 | assert not os.path.islink(self.vm_dir), \ |
| 58 | 'VM state dir is misconfigured; please recreate: %s' % self.vm_dir |
| 59 | st_uid = os.stat(self.vm_dir).st_uid |
| 60 | assert st_uid == 0 or st_uid == os.getuid(), \ |
| 61 | 'VM state dir is misconfigured; please recreate: %s' % self.vm_dir |
| 62 | |
| 63 | self.pidfile = os.path.join(self.vm_dir, 'kvm.pid') |
| 64 | self.kvm_monitor = os.path.join(self.vm_dir, 'kvm.monitor') |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 65 | self.kvm_pipe_in = '%s.in' % self.kvm_monitor # to KVM |
| 66 | self.kvm_pipe_out = '%s.out' % self.kvm_monitor # from KVM |
| 67 | self.kvm_serial = '%s.serial' % self.kvm_monitor |
| 68 | |
| 69 | # TODO(achuith): support nographics, snapshot, mem_path, usb_passthrough, |
| 70 | # moblab, etc. |
| 71 | |
Achuith Bhandarkar | ee3163d | 2016-10-19 12:58:35 -0700 | [diff] [blame] | 72 | |
| 73 | def _RunCommand(self, *args, **kwargs): |
| 74 | """Use SudoRunCommand or RunCommand as necessary.""" |
| 75 | if self.use_sudo: |
| 76 | return cros_build_lib.SudoRunCommand(*args, **kwargs) |
| 77 | else: |
| 78 | return cros_build_lib.RunCommand(*args, **kwargs) |
| 79 | |
| 80 | def _CleanupFiles(self, recreate): |
| 81 | """Cleanup vm_dir. |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 82 | |
| 83 | Args: |
Achuith Bhandarkar | ee3163d | 2016-10-19 12:58:35 -0700 | [diff] [blame] | 84 | recreate: recreate vm_dir. |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 85 | """ |
Achuith Bhandarkar | ee3163d | 2016-10-19 12:58:35 -0700 | [diff] [blame] | 86 | self._RunCommand(['rm', '-rf', self.vm_dir]) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 87 | if recreate: |
Achuith Bhandarkar | ee3163d | 2016-10-19 12:58:35 -0700 | [diff] [blame] | 88 | self._RunCommand(['mkdir', self.vm_dir]) |
| 89 | self._RunCommand(['chmod', '777', self.vm_dir]) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 90 | |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 91 | def PerformAction(self, start=False, stop=False, cmd=None): |
| 92 | """Performs an action, one of start, stop, or run a command in the VM. |
| 93 | |
| 94 | Args: |
| 95 | start: start the VM. |
| 96 | stop: stop the VM. |
| 97 | cmd: list or scalar command to run in the VM. |
| 98 | |
| 99 | Returns: |
| 100 | cmd output. |
| 101 | """ |
| 102 | |
| 103 | if not start and not stop and not cmd: |
| 104 | raise VMError('Must specify one of start, stop, or cmd.') |
| 105 | if start: |
| 106 | self.Start() |
| 107 | if stop: |
| 108 | self.Stop() |
| 109 | if cmd: |
| 110 | return self.RemoteCommand(cmd.split()) |
| 111 | |
| 112 | def Start(self): |
| 113 | """Start the VM.""" |
| 114 | |
| 115 | self.Stop() |
| 116 | |
Achuith Bhandarkar | 022d69c | 2016-10-05 14:28:14 -0700 | [diff] [blame] | 117 | logging.debug('Start VM') |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 118 | if not self.qemu_path: |
| 119 | self.qemu_path = osutils.Which('qemu-system-x86_64') |
| 120 | if not self.qemu_path: |
| 121 | raise VMError('qemu not found.') |
| 122 | logging.debug('qemu path=%s', self.qemu_path) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 123 | |
| 124 | if not self.image_path: |
| 125 | self.image_path = os.environ.get('VM_IMAGE_PATH', '') |
| 126 | logging.debug('vm image path=%s', self.image_path) |
| 127 | if not self.image_path or not os.path.exists(self.image_path): |
| 128 | raise VMError('VM image path %s does not exist.' % self.image_path) |
| 129 | |
| 130 | self._CleanupFiles(recreate=True) |
| 131 | open(self.kvm_serial, 'w') |
| 132 | for pipe in [self.kvm_pipe_in, self.kvm_pipe_out]: |
| 133 | os.mkfifo(pipe, 0600) |
| 134 | |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 135 | args = [self.qemu_path, '-m', '2G', '-smp', '4', '-vga', 'cirrus', |
| 136 | '-daemonize', |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 137 | '-pidfile', self.pidfile, |
| 138 | '-chardev', 'pipe,id=control_pipe,path=%s' % self.kvm_monitor, |
| 139 | '-serial', 'file:%s' % self.kvm_serial, |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 140 | '-mon', 'chardev=control_pipe', |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 141 | '-net', 'nic,model=virtio', |
| 142 | '-net', 'user,hostfwd=tcp::%d-:22' % self.ssh_port, |
| 143 | '-drive', 'file=%s,index=0,media=disk,cache=unsafe' |
| 144 | % self.image_path] |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 145 | if self.enable_kvm: |
| 146 | args.append('-enable-kvm') |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 147 | logging.info(' '.join(args)) |
| 148 | logging.info('Pid file: %s', self.pidfile) |
| 149 | if not self.dry_run: |
Achuith Bhandarkar | ee3163d | 2016-10-19 12:58:35 -0700 | [diff] [blame] | 150 | self._RunCommand(args) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 151 | |
| 152 | def _GetVMPid(self): |
| 153 | """Get the pid of the VM. |
| 154 | |
| 155 | Returns: |
| 156 | pid of the VM. |
| 157 | """ |
Achuith Bhandarkar | ee3163d | 2016-10-19 12:58:35 -0700 | [diff] [blame] | 158 | if not os.path.exists(self.vm_dir): |
| 159 | logging.debug('%s not present.', self.vm_dir) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 160 | return 0 |
| 161 | |
| 162 | if not os.path.exists(self.pidfile): |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 163 | logging.info('%s does not exist.', self.pidfile) |
| 164 | return 0 |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 165 | |
Achuith Bhandarkar | ee3163d | 2016-10-19 12:58:35 -0700 | [diff] [blame] | 166 | pid = self._RunCommand(['cat', self.pidfile], |
| 167 | redirect_stdout=True).output.rstrip() |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 168 | if not pid.isdigit(): |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 169 | logging.error('%s in %s is not a pid.', pid, self.pidfile) |
| 170 | return 0 |
| 171 | |
Achuith Bhandarkar | 022d69c | 2016-10-05 14:28:14 -0700 | [diff] [blame] | 172 | return int(pid) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 173 | |
| 174 | def IsRunning(self): |
| 175 | """Returns True if there's a running VM. |
| 176 | |
| 177 | Returns: |
| 178 | True if there's a running VM. |
| 179 | """ |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 180 | pid = self._GetVMPid() |
Achuith Bhandarkar | 022d69c | 2016-10-05 14:28:14 -0700 | [diff] [blame] | 181 | if not pid: |
| 182 | return False |
| 183 | |
| 184 | # Make sure the process actually exists. |
Achuith Bhandarkar | ee3163d | 2016-10-19 12:58:35 -0700 | [diff] [blame] | 185 | res = self._RunCommand(['kill', '-0', str(pid)], error_code_ok=True) |
Achuith Bhandarkar | 022d69c | 2016-10-05 14:28:14 -0700 | [diff] [blame] | 186 | return res.returncode == 0 |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 187 | |
| 188 | def Stop(self): |
| 189 | """Stop the VM.""" |
Achuith Bhandarkar | 022d69c | 2016-10-05 14:28:14 -0700 | [diff] [blame] | 190 | logging.debug('Stop VM') |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 191 | |
| 192 | pid = self._GetVMPid() |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 193 | if pid: |
| 194 | logging.info('Killing %d.', pid) |
| 195 | if not self.dry_run: |
Achuith Bhandarkar | ee3163d | 2016-10-19 12:58:35 -0700 | [diff] [blame] | 196 | self._RunCommand(['kill', '-9', str(pid)], error_code_ok=True) |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 197 | |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 198 | self._CleanupFiles(recreate=False) |
| 199 | |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 200 | def WaitForBoot(self, timeout=120, poll_interval=0.1): |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 201 | """Wait for the VM to boot up. |
| 202 | |
| 203 | If there is no VM running, start one. |
| 204 | |
| 205 | Args: |
| 206 | timeout: maxiumum time to wait before raising an exception. |
| 207 | poll_interval: interval between checks. |
| 208 | """ |
Achuith Bhandarkar | ee3163d | 2016-10-19 12:58:35 -0700 | [diff] [blame] | 209 | if not os.path.exists(self.vm_dir): |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 210 | self.Start() |
| 211 | |
| 212 | start_time = time.time() |
| 213 | while time.time() - start_time < timeout: |
| 214 | result = self.RemoteCommand(cmd=['echo']) |
| 215 | if result.returncode == 255: |
| 216 | time.sleep(poll_interval) |
| 217 | continue |
| 218 | elif result.returncode == 0: |
| 219 | return |
| 220 | else: |
| 221 | break |
| 222 | raise VMError('WaitForBoot failed') |
| 223 | |
| 224 | def RemoteCommand(self, cmd): |
| 225 | """Run a remote command in the VM. |
| 226 | |
| 227 | Args: |
| 228 | cmd: command to run, of list type. |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 229 | """ |
| 230 | if not isinstance(cmd, list): |
| 231 | raise VMError('cmd must be a list.') |
| 232 | |
| 233 | args = ['ssh', '-o', 'UserKnownHostsFile=/dev/null', |
| 234 | '-o', 'StrictHostKeyChecking=no', |
| 235 | '-i', remote_access.TEST_PRIVATE_KEY, |
| 236 | '-p', str(self.ssh_port), 'root@localhost'] |
| 237 | args.extend(cmd) |
| 238 | |
| 239 | if not self.dry_run: |
| 240 | return cros_build_lib.RunCommand(args, redirect_stdout=True, |
| 241 | combine_stdout_stderr=True, |
| 242 | log_output=True, |
| 243 | error_code_ok=True) |
| 244 | |
| 245 | |
| 246 | def ParseCommandLine(argv): |
| 247 | """Parse the command line. |
| 248 | |
| 249 | Args: |
| 250 | argv: Command arguments. |
| 251 | |
| 252 | Returns: |
| 253 | List of parsed args. |
| 254 | """ |
| 255 | parser = commandline.ArgumentParser(description=__doc__) |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 256 | parser.add_argument('--start', action='store_true', default=False, |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 257 | help='Start the VM.') |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 258 | parser.add_argument('--stop', action='store_true', default=False, |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 259 | help='Stop the VM.') |
| 260 | parser.add_argument('--cmd', help='Run this command in the VM.') |
| 261 | parser.add_argument('--image-path', type='path', |
| 262 | help='Path to VM image to launch with --start.') |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 263 | parser.add_argument('--qemu-path', type='path', |
| 264 | help='Path of qemu binary to launch with --start.') |
| 265 | parser.add_argument('--disable-kvm', action='store_true', default=False, |
| 266 | help='Disable KVM, use software emulation.') |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 267 | parser.add_argument('--ssh-port', type=int, default=VM.SSH_PORT, |
| 268 | help='ssh port to communicate with VM.') |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 269 | parser.add_argument('--dry-run', action='store_true', default=False, |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 270 | help='dry run for debugging.') |
| 271 | return parser.parse_args(argv) |
| 272 | |
| 273 | |
| 274 | def main(argv): |
| 275 | args = ParseCommandLine(argv) |
Achuith Bhandarkar | f4950ba | 2016-10-11 15:40:07 -0700 | [diff] [blame] | 276 | vm = VM(image_path=args.image_path, |
| 277 | qemu_path=args.qemu_path, enable_kvm=not args.disable_kvm, |
Achuith Bhandarkar | d8d1929 | 2016-05-03 14:32:58 -0700 | [diff] [blame] | 278 | ssh_port=args.ssh_port, dry_run=args.dry_run) |
| 279 | vm.PerformAction(start=args.start, stop=args.stop, cmd=args.cmd) |