blob: 2adeaac12fcfe1c14fb0f77b974fa05ab7227c0c [file] [log] [blame]
Achuith Bhandarkard8d19292016-05-03 14:32:58 -07001# 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 Frysinger666566c2016-09-21 00:00:21 -04005"""Script for VM Management."""
Achuith Bhandarkard8d19292016-05-03 14:32:58 -07006
7from __future__ import print_function
8
9import os
10import time
11
12from chromite.lib import commandline
13from chromite.lib import cros_build_lib
14from chromite.lib import cros_logging as logging
Achuith Bhandarkarf4950ba2016-10-11 15:40:07 -070015from chromite.lib import osutils
Achuith Bhandarkard8d19292016-05-03 14:32:58 -070016from chromite.lib import remote_access
17
18
19class VMError(Exception):
20 """Exception for VM failures."""
21
22 def __init__(self, message):
23 super(VMError, self).__init__()
24 logging.error(message)
25
26
27class VM(object):
28 """Class for managing a VM."""
29
30 SSH_PORT = 9222
Achuith Bhandarkard8d19292016-05-03 14:32:58 -070031
32
Achuith Bhandarkarf4950ba2016-10-11 15:40:07 -070033 def __init__(self, image_path=None, qemu_path=None, enable_kvm=True,
34 ssh_port=SSH_PORT, dry_run=False):
Achuith Bhandarkard8d19292016-05-03 14:32:58 -070035 """Initialize VM.
36
37 Args:
Achuith Bhandarkard8d19292016-05-03 14:32:58 -070038 image_path: path of vm image.
Achuith Bhandarkarf4950ba2016-10-11 15:40:07 -070039 qemu_path: path to qemu binary.
40 enable_kvm: enable kvm (kernel support for virtualization).
Achuith Bhandarkard8d19292016-05-03 14:32:58 -070041 ssh_port: ssh port to use.
42 dry_run: disable VM commands.
43 """
44
Achuith Bhandarkarf4950ba2016-10-11 15:40:07 -070045 self.qemu_path = qemu_path
46 self.enable_kvm = enable_kvm
Achuith Bhandarkaree3163d2016-10-19 12:58:35 -070047 # Software emulation doesn't need sudo access.
48 self.use_sudo = enable_kvm
Achuith Bhandarkard8d19292016-05-03 14:32:58 -070049 self.image_path = image_path
50 self.ssh_port = ssh_port
51 self.dry_run = dry_run
52
Achuith Bhandarkaree3163d2016-10-19 12:58:35 -070053 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 Bhandarkard8d19292016-05-03 14:32:58 -070065 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 Bhandarkaree3163d2016-10-19 12:58:35 -070072
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 Bhandarkard8d19292016-05-03 14:32:58 -070082
83 Args:
Achuith Bhandarkaree3163d2016-10-19 12:58:35 -070084 recreate: recreate vm_dir.
Achuith Bhandarkard8d19292016-05-03 14:32:58 -070085 """
Achuith Bhandarkaree3163d2016-10-19 12:58:35 -070086 self._RunCommand(['rm', '-rf', self.vm_dir])
Achuith Bhandarkard8d19292016-05-03 14:32:58 -070087 if recreate:
Achuith Bhandarkaree3163d2016-10-19 12:58:35 -070088 self._RunCommand(['mkdir', self.vm_dir])
89 self._RunCommand(['chmod', '777', self.vm_dir])
Achuith Bhandarkard8d19292016-05-03 14:32:58 -070090
Achuith Bhandarkard8d19292016-05-03 14:32:58 -070091 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 Bhandarkar022d69c2016-10-05 14:28:14 -0700117 logging.debug('Start VM')
Achuith Bhandarkarf4950ba2016-10-11 15:40:07 -0700118 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 Bhandarkard8d19292016-05-03 14:32:58 -0700123
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 Bhandarkarf4950ba2016-10-11 15:40:07 -0700135 args = [self.qemu_path, '-m', '2G', '-smp', '4', '-vga', 'cirrus',
136 '-daemonize',
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700137 '-pidfile', self.pidfile,
138 '-chardev', 'pipe,id=control_pipe,path=%s' % self.kvm_monitor,
139 '-serial', 'file:%s' % self.kvm_serial,
Achuith Bhandarkarf4950ba2016-10-11 15:40:07 -0700140 '-mon', 'chardev=control_pipe',
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700141 '-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 Bhandarkarf4950ba2016-10-11 15:40:07 -0700145 if self.enable_kvm:
146 args.append('-enable-kvm')
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700147 logging.info(' '.join(args))
148 logging.info('Pid file: %s', self.pidfile)
149 if not self.dry_run:
Achuith Bhandarkaree3163d2016-10-19 12:58:35 -0700150 self._RunCommand(args)
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700151
152 def _GetVMPid(self):
153 """Get the pid of the VM.
154
155 Returns:
156 pid of the VM.
157 """
Achuith Bhandarkaree3163d2016-10-19 12:58:35 -0700158 if not os.path.exists(self.vm_dir):
159 logging.debug('%s not present.', self.vm_dir)
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700160 return 0
161
162 if not os.path.exists(self.pidfile):
Achuith Bhandarkarf4950ba2016-10-11 15:40:07 -0700163 logging.info('%s does not exist.', self.pidfile)
164 return 0
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700165
Achuith Bhandarkaree3163d2016-10-19 12:58:35 -0700166 pid = self._RunCommand(['cat', self.pidfile],
167 redirect_stdout=True).output.rstrip()
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700168 if not pid.isdigit():
Achuith Bhandarkarf4950ba2016-10-11 15:40:07 -0700169 logging.error('%s in %s is not a pid.', pid, self.pidfile)
170 return 0
171
Achuith Bhandarkar022d69c2016-10-05 14:28:14 -0700172 return int(pid)
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700173
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 Bhandarkarf4950ba2016-10-11 15:40:07 -0700180 pid = self._GetVMPid()
Achuith Bhandarkar022d69c2016-10-05 14:28:14 -0700181 if not pid:
182 return False
183
184 # Make sure the process actually exists.
Achuith Bhandarkaree3163d2016-10-19 12:58:35 -0700185 res = self._RunCommand(['kill', '-0', str(pid)], error_code_ok=True)
Achuith Bhandarkar022d69c2016-10-05 14:28:14 -0700186 return res.returncode == 0
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700187
188 def Stop(self):
189 """Stop the VM."""
Achuith Bhandarkar022d69c2016-10-05 14:28:14 -0700190 logging.debug('Stop VM')
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700191
192 pid = self._GetVMPid()
Achuith Bhandarkarf4950ba2016-10-11 15:40:07 -0700193 if pid:
194 logging.info('Killing %d.', pid)
195 if not self.dry_run:
Achuith Bhandarkaree3163d2016-10-19 12:58:35 -0700196 self._RunCommand(['kill', '-9', str(pid)], error_code_ok=True)
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700197
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700198 self._CleanupFiles(recreate=False)
199
Achuith Bhandarkarf4950ba2016-10-11 15:40:07 -0700200 def WaitForBoot(self, timeout=120, poll_interval=0.1):
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700201 """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 Bhandarkaree3163d2016-10-19 12:58:35 -0700209 if not os.path.exists(self.vm_dir):
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700210 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 Bhandarkard8d19292016-05-03 14:32:58 -0700229 """
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
246def 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 Bhandarkarf4950ba2016-10-11 15:40:07 -0700256 parser.add_argument('--start', action='store_true', default=False,
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700257 help='Start the VM.')
Achuith Bhandarkarf4950ba2016-10-11 15:40:07 -0700258 parser.add_argument('--stop', action='store_true', default=False,
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700259 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 Bhandarkarf4950ba2016-10-11 15:40:07 -0700263 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 Bhandarkard8d19292016-05-03 14:32:58 -0700267 parser.add_argument('--ssh-port', type=int, default=VM.SSH_PORT,
268 help='ssh port to communicate with VM.')
Achuith Bhandarkarf4950ba2016-10-11 15:40:07 -0700269 parser.add_argument('--dry-run', action='store_true', default=False,
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700270 help='dry run for debugging.')
271 return parser.parse_args(argv)
272
273
274def main(argv):
275 args = ParseCommandLine(argv)
Achuith Bhandarkarf4950ba2016-10-11 15:40:07 -0700276 vm = VM(image_path=args.image_path,
277 qemu_path=args.qemu_path, enable_kvm=not args.disable_kvm,
Achuith Bhandarkard8d19292016-05-03 14:32:58 -0700278 ssh_port=args.ssh_port, dry_run=args.dry_run)
279 vm.PerformAction(start=args.start, stop=args.stop, cmd=args.cmd)