blob: 295c8b1b605ec6531ba1afad5e7498d531e8a949 [file] [log] [blame]
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001#!/usr/bin/env python3
2
Patrick Williamsc124f4f2015-09-15 14:41:29 -05003# Handle running OE images standalone with QEMU
4#
5# Copyright (C) 2006-2011 Linux Foundation
Patrick Williamsc0f7c042017-02-23 20:41:17 -06006# Copyright (c) 2016 Wind River Systems, Inc.
Patrick Williamsc124f4f2015-09-15 14:41:29 -05007#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License version 2 as
10# published by the Free Software Foundation.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License along
18# with this program; if not, write to the Free Software Foundation, Inc.,
19# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
Patrick Williamsc0f7c042017-02-23 20:41:17 -060021import os
22import sys
23import logging
24import subprocess
25import re
26import fcntl
27import shutil
28import glob
29import configparser
Brad Bishop004d4992018-10-02 23:54:45 +020030import signal
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050031
Brad Bishopd7bf8c12018-02-25 22:55:05 -050032class RunQemuError(Exception):
33 """Custom exception to raise on known errors."""
34 pass
35
36class OEPathError(RunQemuError):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060037 """Custom Exception to give better guidance on missing binaries"""
38 def __init__(self, message):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050039 super().__init__("In order for this script to dynamically infer paths\n \
Patrick Williamsc0f7c042017-02-23 20:41:17 -060040kernels or filesystem images, you either need bitbake in your PATH\n \
41or to source oe-init-build-env before running this script.\n\n \
42Dynamic path inference can be avoided by passing a *.qemuboot.conf to\n \
Brad Bishopd7bf8c12018-02-25 22:55:05 -050043runqemu, i.e. `runqemu /path/to/my-image-name.qemuboot.conf`\n\n %s" % message)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060044
45
46def create_logger():
47 logger = logging.getLogger('runqemu')
48 logger.setLevel(logging.INFO)
49
50 # create console handler and set level to debug
51 ch = logging.StreamHandler()
Brad Bishopd7bf8c12018-02-25 22:55:05 -050052 ch.setLevel(logging.DEBUG)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060053
54 # create formatter
55 formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
56
57 # add formatter to ch
58 ch.setFormatter(formatter)
59
60 # add ch to logger
61 logger.addHandler(ch)
62
63 return logger
64
65logger = create_logger()
66
67def print_usage():
68 print("""
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050069Usage: you can run this script with any valid combination
70of the following environment variables (in any order):
71 KERNEL - the kernel image file to use
72 ROOTFS - the rootfs image file or nfsroot directory to use
Brad Bishop316dfdd2018-06-25 12:45:53 -040073 DEVICE_TREE - the device tree blob to use
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050074 MACHINE - the machine name (optional, autodetected from KERNEL filename if unspecified)
75 Simplified QEMU command-line options can be passed with:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060076 nographic - disable video console
77 serial - enable a serial console on /dev/ttyS0
78 slirp - enable user networking, no root privileges is required
79 kvm - enable KVM when running x86/x86_64 (VT-capable CPU required)
80 kvm-vhost - enable KVM with vhost when running x86/x86_64 (VT-capable CPU required)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050081 publicvnc - enable a VNC server open to all hosts
Patrick Williamsc0f7c042017-02-23 20:41:17 -060082 audio - enable audio
Brad Bishop6e60e8b2018-02-01 10:27:11 -050083 [*/]ovmf* - OVMF firmware file or base name for booting with UEFI
Patrick Williamsc0f7c042017-02-23 20:41:17 -060084 tcpserial=<port> - specify tcp serial port number
85 biosdir=<dir> - specify custom bios dir
86 biosfilename=<filename> - specify bios filename
87 qemuparams=<xyz> - specify custom parameters to QEMU
88 bootparams=<xyz> - specify custom kernel parameters during boot
Brad Bishop6e60e8b2018-02-01 10:27:11 -050089 help, -h, --help: print this text
Brad Bishopd7bf8c12018-02-25 22:55:05 -050090 -d, --debug: Enable debug output
91 -q, --quite: Hide most output except error messages
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050092
93Examples:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050094 runqemu
Patrick Williamsc0f7c042017-02-23 20:41:17 -060095 runqemu qemuarm
96 runqemu tmp/deploy/images/qemuarm
Brad Bishop6e60e8b2018-02-01 10:27:11 -050097 runqemu tmp/deploy/images/qemux86/<qemuboot.conf>
Patrick Williamsc0f7c042017-02-23 20:41:17 -060098 runqemu qemux86-64 core-image-sato ext4
99 runqemu qemux86-64 wic-image-minimal wic
100 runqemu path/to/bzImage-qemux86.bin path/to/nfsrootdir/ serial
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500101 runqemu qemux86 iso/hddimg/wic.vmdk/wic.qcow2/wic.vdi/ramfs/cpio.gz...
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600102 runqemu qemux86 qemuparams="-m 256"
103 runqemu qemux86 bootparams="psplash=false"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600104 runqemu path/to/<image>-<machine>.wic
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500105 runqemu path/to/<image>-<machine>.wic.vmdk
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600106""")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600108def check_tun():
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500109 """Check /dev/net/tun"""
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600110 dev_tun = '/dev/net/tun'
111 if not os.path.exists(dev_tun):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500112 raise RunQemuError("TUN control device %s is unavailable; you may need to enable TUN (e.g. sudo modprobe tun)" % dev_tun)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500113
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600114 if not os.access(dev_tun, os.W_OK):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500115 raise RunQemuError("TUN control device %s is not writable, please fix (e.g. sudo chmod 666 %s)" % (dev_tun, dev_tun))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500116
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600117def check_libgl(qemu_bin):
118 cmd = 'ldd %s' % qemu_bin
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500119 logger.debug('Running %s...' % cmd)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600120 need_gl = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8')
121 if re.search('libGLU', need_gl):
122 # We can't run without a libGL.so
123 libgl = False
124 check_files = (('/usr/lib/libGL.so', '/usr/lib/libGLU.so'), \
125 ('/usr/lib64/libGL.so', '/usr/lib64/libGLU.so'), \
126 ('/usr/lib/*-linux-gnu/libGL.so', '/usr/lib/*-linux-gnu/libGLU.so'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500127
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600128 for (f1, f2) in check_files:
129 if re.search('\*', f1):
130 for g1 in glob.glob(f1):
131 if libgl:
132 break
133 if os.path.exists(g1):
134 for g2 in glob.glob(f2):
135 if os.path.exists(g2):
136 libgl = True
137 break
138 if libgl:
139 break
140 else:
141 if os.path.exists(f1) and os.path.exists(f2):
142 libgl = True
143 break
144 if not libgl:
145 logger.error("You need libGL.so and libGLU.so to exist in your library path to run the QEMU emulator.")
146 logger.error("Ubuntu package names are: libgl1-mesa-dev and libglu1-mesa-dev.")
147 logger.error("Fedora package names are: mesa-libGL-devel mesa-libGLU-devel.")
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500148 raise RunQemuError('%s requires libGLU, but not found' % qemu_bin)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500149
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600150def get_first_file(cmds):
151 """Return first file found in wildcard cmds"""
152 for cmd in cmds:
153 all_files = glob.glob(cmd)
154 if all_files:
155 for f in all_files:
156 if not os.path.isdir(f):
157 return f
158 return ''
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500159
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500160def check_free_port(host, port):
161 """ Check whether the port is free or not """
162 import socket
163 from contextlib import closing
164
165 with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
166 if sock.connect_ex((host, port)) == 0:
167 # Port is open, so not free
168 return False
169 else:
170 # Port is not open, so free
171 return True
172
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600173class BaseConfig(object):
174 def __init__(self):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500175 # The self.d saved vars from self.set(), part of them are from qemuboot.conf
176 self.d = {'QB_KERNEL_ROOT': '/dev/vda'}
177
178 # Supported env vars, add it here if a var can be got from env,
179 # and don't use os.getenv in the code.
180 self.env_vars = ('MACHINE',
181 'ROOTFS',
182 'KERNEL',
Brad Bishop316dfdd2018-06-25 12:45:53 -0400183 'DEVICE_TREE',
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500184 'DEPLOY_DIR_IMAGE',
185 'OE_TMPDIR',
186 'OECORE_NATIVE_SYSROOT',
187 )
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500188
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600189 self.qemu_opt = ''
190 self.qemu_opt_script = ''
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600191 self.clean_nfs_dir = False
192 self.nfs_server = ''
193 self.rootfs = ''
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500194 # File name(s) of a OVMF firmware file or variable store,
195 # to be added with -drive if=pflash.
196 # Found in the same places as the rootfs, with or without one of
197 # these suffices: qcow2, bin.
198 # Setting one also adds "-vga std" because that is all that
199 # OVMF supports.
200 self.ovmf_bios = []
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600201 self.qemuboot = ''
202 self.qbconfload = False
203 self.kernel = ''
204 self.kernel_cmdline = ''
205 self.kernel_cmdline_script = ''
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500206 self.bootparams = ''
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600207 self.dtb = ''
208 self.fstype = ''
209 self.kvm_enabled = False
210 self.vhost_enabled = False
211 self.slirp_enabled = False
212 self.nfs_instance = 0
213 self.nfs_running = False
214 self.serialstdio = False
215 self.cleantap = False
216 self.saved_stty = ''
217 self.audio_enabled = False
218 self.tcpserial_portnum = ''
219 self.custombiosdir = ''
220 self.lock = ''
Brad Bishopf86d0552018-12-04 14:18:15 -0800221 self.lock_descriptor = None
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600222 self.bitbake_e = ''
223 self.snapshot = False
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500224 self.fstypes = ('ext2', 'ext3', 'ext4', 'jffs2', 'nfs', 'btrfs',
225 'cpio.gz', 'cpio', 'ramfs', 'tar.bz2', 'tar.gz')
226 self.vmtypes = ('hddimg', 'hdddirect', 'wic', 'wic.vmdk',
227 'wic.qcow2', 'wic.vdi', 'iso')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500228 self.network_device = "-device e1000,netdev=net0,mac=@MAC@"
229 # Use different mac section for tap and slirp to avoid
230 # conflicts, e.g., when one is running with tap, the other is
231 # running with slirp.
232 # The last section is dynamic, which is for avoiding conflicts,
233 # when multiple qemus are running, e.g., when multiple tap or
234 # slirp qemus are running.
235 self.mac_tap = "52:54:00:12:34:"
236 self.mac_slirp = "52:54:00:12:35:"
Brad Bishop004d4992018-10-02 23:54:45 +0200237 # pid of the actual qemu process
238 self.qemupid = None
239 # avoid cleanup twice
240 self.cleaned = False
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500241
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500242 def acquire_lock(self, error=True):
243 logger.debug("Acquiring lockfile %s..." % self.lock)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600244 try:
245 self.lock_descriptor = open(self.lock, 'w')
246 fcntl.flock(self.lock_descriptor, fcntl.LOCK_EX|fcntl.LOCK_NB)
247 except Exception as e:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500248 msg = "Acquiring lockfile %s failed: %s" % (self.lock, e)
249 if error:
250 logger.error(msg)
251 else:
252 logger.info(msg)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600253 if self.lock_descriptor:
254 self.lock_descriptor.close()
Brad Bishopf86d0552018-12-04 14:18:15 -0800255 self.lock_descriptor = None
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600256 return False
257 return True
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500258
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600259 def release_lock(self):
Brad Bishopf86d0552018-12-04 14:18:15 -0800260 if self.lock_descriptor:
261 logger.debug("Releasing lockfile for tap device '%s'" % self.tap)
262 fcntl.flock(self.lock_descriptor, fcntl.LOCK_UN)
263 self.lock_descriptor.close()
264 os.remove(self.lock)
265 self.lock_descriptor = None
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500266
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600267 def get(self, key):
268 if key in self.d:
269 return self.d.get(key)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500270 elif os.getenv(key):
271 return os.getenv(key)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600272 else:
273 return ''
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500274
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600275 def set(self, key, value):
276 self.d[key] = value
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500277
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600278 def is_deploy_dir_image(self, p):
279 if os.path.isdir(p):
280 if not re.search('.qemuboot.conf$', '\n'.join(os.listdir(p)), re.M):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500281 logger.debug("Can't find required *.qemuboot.conf in %s" % p)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600282 return False
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500283 if not any(map(lambda name: '-image-' in name, os.listdir(p))):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500284 logger.debug("Can't find *-image-* in %s" % p)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600285 return False
286 return True
287 else:
288 return False
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500289
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600290 def check_arg_fstype(self, fst):
291 """Check and set FSTYPE"""
292 if fst not in self.fstypes + self.vmtypes:
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800293 logger.warning("Maybe unsupported FSTYPE: %s" % fst)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600294 if not self.fstype or self.fstype == fst:
295 if fst == 'ramfs':
296 fst = 'cpio.gz'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500297 if fst in ('tar.bz2', 'tar.gz'):
298 fst = 'nfs'
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600299 self.fstype = fst
300 else:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500301 raise RunQemuError("Conflicting: FSTYPE %s and %s" % (self.fstype, fst))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500302
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600303 def set_machine_deploy_dir(self, machine, deploy_dir_image):
304 """Set MACHINE and DEPLOY_DIR_IMAGE"""
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500305 logger.debug('MACHINE: %s' % machine)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600306 self.set("MACHINE", machine)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500307 logger.debug('DEPLOY_DIR_IMAGE: %s' % deploy_dir_image)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600308 self.set("DEPLOY_DIR_IMAGE", deploy_dir_image)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500309
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600310 def check_arg_nfs(self, p):
311 if os.path.isdir(p):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500312 self.rootfs = p
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600313 else:
314 m = re.match('(.*):(.*)', p)
315 self.nfs_server = m.group(1)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500316 self.rootfs = m.group(2)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600317 self.check_arg_fstype('nfs')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500318
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600319 def check_arg_path(self, p):
320 """
321 - Check whether it is <image>.qemuboot.conf or contains <image>.qemuboot.conf
322 - Check whether is a kernel file
323 - Check whether is a image file
324 - Check whether it is a nfs dir
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500325 - Check whether it is a OVMF flash file
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600326 """
327 if p.endswith('.qemuboot.conf'):
328 self.qemuboot = p
329 self.qbconfload = True
330 elif re.search('\.bin$', p) or re.search('bzImage', p) or \
331 re.search('zImage', p) or re.search('vmlinux', p) or \
332 re.search('fitImage', p) or re.search('uImage', p):
333 self.kernel = p
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500334 elif os.path.exists(p) and (not os.path.isdir(p)) and '-image-' in os.path.basename(p):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600335 self.rootfs = p
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500336 # Check filename against self.fstypes can hanlde <file>.cpio.gz,
337 # otherwise, its type would be "gz", which is incorrect.
338 fst = ""
339 for t in self.fstypes:
340 if p.endswith(t):
341 fst = t
342 break
343 if not fst:
344 m = re.search('.*\.(.*)$', self.rootfs)
345 if m:
346 fst = m.group(1)
347 if fst:
348 self.check_arg_fstype(fst)
349 qb = re.sub('\.' + fst + "$", '', self.rootfs)
350 qb = '%s%s' % (re.sub('\.rootfs$', '', qb), '.qemuboot.conf')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600351 if os.path.exists(qb):
352 self.qemuboot = qb
353 self.qbconfload = True
354 else:
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800355 logger.warning("%s doesn't exist" % qb)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600356 else:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500357 raise RunQemuError("Can't find FSTYPE from: %s" % p)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500358
359 elif os.path.isdir(p) or re.search(':', p) and re.search('/', p):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600360 if self.is_deploy_dir_image(p):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500361 logger.debug('DEPLOY_DIR_IMAGE: %s' % p)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600362 self.set("DEPLOY_DIR_IMAGE", p)
363 else:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500364 logger.debug("Assuming %s is an nfs rootfs" % p)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600365 self.check_arg_nfs(p)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500366 elif os.path.basename(p).startswith('ovmf'):
367 self.ovmf_bios.append(p)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600368 else:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500369 raise RunQemuError("Unknown path arg %s" % p)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500370
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600371 def check_arg_machine(self, arg):
372 """Check whether it is a machine"""
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500373 if self.get('MACHINE') == arg:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600374 return
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500375 elif self.get('MACHINE') and self.get('MACHINE') != arg:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500376 raise RunQemuError("Maybe conflicted MACHINE: %s vs %s" % (self.get('MACHINE'), arg))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500377 elif re.search('/', arg):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500378 raise RunQemuError("Unknown arg: %s" % arg)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500379
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500380 logger.debug('Assuming MACHINE = %s' % arg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500381
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600382 # if we're running under testimage, or similarly as a child
383 # of an existing bitbake invocation, we can't invoke bitbake
384 # to validate the MACHINE setting and must assume it's correct...
385 # FIXME: testimage.bbclass exports these two variables into env,
386 # are there other scenarios in which we need to support being
387 # invoked by bitbake?
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500388 deploy = self.get('DEPLOY_DIR_IMAGE')
389 bbchild = deploy and self.get('OE_TMPDIR')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600390 if bbchild:
391 self.set_machine_deploy_dir(arg, deploy)
392 return
393 # also check whether we're running under a sourced toolchain
394 # environment file
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500395 if self.get('OECORE_NATIVE_SYSROOT'):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600396 self.set("MACHINE", arg)
397 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500398
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600399 cmd = 'MACHINE=%s bitbake -e' % arg
400 logger.info('Running %s...' % cmd)
401 self.bitbake_e = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8')
402 # bitbake -e doesn't report invalid MACHINE as an error, so
403 # let's check DEPLOY_DIR_IMAGE to make sure that it is a valid
404 # MACHINE.
405 s = re.search('^DEPLOY_DIR_IMAGE="(.*)"', self.bitbake_e, re.M)
406 if s:
407 deploy_dir_image = s.group(1)
408 else:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500409 raise RunQemuError("bitbake -e %s" % self.bitbake_e)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600410 if self.is_deploy_dir_image(deploy_dir_image):
411 self.set_machine_deploy_dir(arg, deploy_dir_image)
412 else:
413 logger.error("%s not a directory valid DEPLOY_DIR_IMAGE" % deploy_dir_image)
414 self.set("MACHINE", arg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500415
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600416 def check_args(self):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500417 for debug in ("-d", "--debug"):
418 if debug in sys.argv:
419 logger.setLevel(logging.DEBUG)
420 sys.argv.remove(debug)
421
422 for quiet in ("-q", "--quiet"):
423 if quiet in sys.argv:
424 logger.setLevel(logging.ERROR)
425 sys.argv.remove(quiet)
426
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600427 unknown_arg = ""
428 for arg in sys.argv[1:]:
429 if arg in self.fstypes + self.vmtypes:
430 self.check_arg_fstype(arg)
431 elif arg == 'nographic':
432 self.qemu_opt_script += ' -nographic'
433 self.kernel_cmdline_script += ' console=ttyS0'
434 elif arg == 'serial':
435 self.kernel_cmdline_script += ' console=ttyS0'
436 self.serialstdio = True
437 elif arg == 'audio':
438 logger.info("Enabling audio in qemu")
439 logger.info("Please install sound drivers in linux host")
440 self.audio_enabled = True
441 elif arg == 'kvm':
442 self.kvm_enabled = True
443 elif arg == 'kvm-vhost':
444 self.vhost_enabled = True
445 elif arg == 'slirp':
446 self.slirp_enabled = True
447 elif arg == 'snapshot':
448 self.snapshot = True
449 elif arg == 'publicvnc':
450 self.qemu_opt_script += ' -vnc :0'
451 elif arg.startswith('tcpserial='):
452 self.tcpserial_portnum = arg[len('tcpserial='):]
453 elif arg.startswith('biosdir='):
454 self.custombiosdir = arg[len('biosdir='):]
455 elif arg.startswith('biosfilename='):
456 self.qemu_opt_script += ' -bios %s' % arg[len('biosfilename='):]
457 elif arg.startswith('qemuparams='):
458 self.qemu_opt_script += ' %s' % arg[len('qemuparams='):]
459 elif arg.startswith('bootparams='):
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500460 self.bootparams = arg[len('bootparams='):]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600461 elif os.path.exists(arg) or (re.search(':', arg) and re.search('/', arg)):
462 self.check_arg_path(os.path.abspath(arg))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500463 elif re.search(r'-image-|-image$', arg):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600464 # Lazy rootfs
465 self.rootfs = arg
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500466 elif arg.startswith('ovmf'):
467 self.ovmf_bios.append(arg)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600468 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500469 # At last, assume it is the MACHINE
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600470 if (not unknown_arg) or unknown_arg == arg:
471 unknown_arg = arg
472 else:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500473 raise RunQemuError("Can't handle two unknown args: %s %s\n"
474 "Try 'runqemu help' on how to use it" % \
475 (unknown_arg, arg))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600476 # Check to make sure it is a valid machine
477 if unknown_arg:
478 if self.get('MACHINE') == unknown_arg:
479 return
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500480 if self.get('DEPLOY_DIR_IMAGE'):
481 machine = os.path.basename(self.get('DEPLOY_DIR_IMAGE'))
482 if unknown_arg == machine:
483 self.set("MACHINE", machine)
484 return
485
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600486 self.check_arg_machine(unknown_arg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500487
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500488 if not (self.get('DEPLOY_DIR_IMAGE') or self.qbconfload):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500489 self.load_bitbake_env()
490 s = re.search('^DEPLOY_DIR_IMAGE="(.*)"', self.bitbake_e, re.M)
491 if s:
492 self.set("DEPLOY_DIR_IMAGE", s.group(1))
493
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600494 def check_kvm(self):
495 """Check kvm and kvm-host"""
496 if not (self.kvm_enabled or self.vhost_enabled):
497 self.qemu_opt_script += ' %s %s' % (self.get('QB_MACHINE'), self.get('QB_CPU'))
498 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500499
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600500 if not self.get('QB_CPU_KVM'):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500501 raise RunQemuError("QB_CPU_KVM is NULL, this board doesn't support kvm")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500502
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600503 self.qemu_opt_script += ' %s %s' % (self.get('QB_MACHINE'), self.get('QB_CPU_KVM'))
504 yocto_kvm_wiki = "https://wiki.yoctoproject.org/wiki/How_to_enable_KVM_for_Poky_qemu"
505 yocto_paravirt_kvm_wiki = "https://wiki.yoctoproject.org/wiki/Running_an_x86_Yocto_Linux_image_under_QEMU_KVM"
506 dev_kvm = '/dev/kvm'
507 dev_vhost = '/dev/vhost-net'
508 with open('/proc/cpuinfo', 'r') as f:
509 kvm_cap = re.search('vmx|svm', "".join(f.readlines()))
510 if not kvm_cap:
511 logger.error("You are trying to enable KVM on a cpu without VT support.")
512 logger.error("Remove kvm from the command-line, or refer:")
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500513 raise RunQemuError(yocto_kvm_wiki)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500514
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600515 if not os.path.exists(dev_kvm):
516 logger.error("Missing KVM device. Have you inserted kvm modules?")
517 logger.error("For further help see:")
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500518 raise RunQemuError(yocto_kvm_wiki)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500519
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600520 if os.access(dev_kvm, os.W_OK|os.R_OK):
521 self.qemu_opt_script += ' -enable-kvm'
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500522 if self.get('MACHINE') == "qemux86":
523 # Workaround for broken APIC window on pre 4.15 host kernels which causes boot hangs
524 # See YOCTO #12301
525 # On 64 bit we use x2apic
526 self.kernel_cmdline_script += " clocksource=kvm-clock hpet=disable noapic nolapic"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600527 else:
528 logger.error("You have no read or write permission on /dev/kvm.")
529 logger.error("Please change the ownership of this file as described at:")
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500530 raise RunQemuError(yocto_kvm_wiki)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500531
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600532 if self.vhost_enabled:
533 if not os.path.exists(dev_vhost):
534 logger.error("Missing virtio net device. Have you inserted vhost-net module?")
535 logger.error("For further help see:")
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500536 raise RunQemuError(yocto_paravirt_kvm_wiki)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500537
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600538 if not os.access(dev_kvm, os.W_OK|os.R_OK):
539 logger.error("You have no read or write permission on /dev/vhost-net.")
540 logger.error("Please change the ownership of this file as described at:")
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500541 raise RunQemuError(yocto_kvm_wiki)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500542
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600543 def check_fstype(self):
544 """Check and setup FSTYPE"""
545 if not self.fstype:
546 fstype = self.get('QB_DEFAULT_FSTYPE')
547 if fstype:
548 self.fstype = fstype
549 else:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500550 raise RunQemuError("FSTYPE is NULL!")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500551
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600552 def check_rootfs(self):
553 """Check and set rootfs"""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500554
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500555 if self.fstype == "none":
556 return
557
558 if self.get('ROOTFS'):
559 if not self.rootfs:
560 self.rootfs = self.get('ROOTFS')
561 elif self.get('ROOTFS') != self.rootfs:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500562 raise RunQemuError("Maybe conflicted ROOTFS: %s vs %s" % (self.get('ROOTFS'), self.rootfs))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500563
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600564 if self.fstype == 'nfs':
565 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500566
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600567 if self.rootfs and not os.path.exists(self.rootfs):
568 # Lazy rootfs
569 self.rootfs = "%s/%s-%s.%s" % (self.get('DEPLOY_DIR_IMAGE'),
570 self.rootfs, self.get('MACHINE'),
571 self.fstype)
572 elif not self.rootfs:
573 cmd_name = '%s/%s*.%s' % (self.get('DEPLOY_DIR_IMAGE'), self.get('IMAGE_NAME'), self.fstype)
574 cmd_link = '%s/%s*.%s' % (self.get('DEPLOY_DIR_IMAGE'), self.get('IMAGE_LINK_NAME'), self.fstype)
575 cmds = (cmd_name, cmd_link)
576 self.rootfs = get_first_file(cmds)
577 if not self.rootfs:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500578 raise RunQemuError("Failed to find rootfs: %s or %s" % cmds)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500579
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600580 if not os.path.exists(self.rootfs):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500581 raise RunQemuError("Can't find rootfs: %s" % self.rootfs)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500582
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500583 def check_ovmf(self):
584 """Check and set full path for OVMF firmware and variable file(s)."""
585
586 for index, ovmf in enumerate(self.ovmf_bios):
587 if os.path.exists(ovmf):
588 continue
589 for suffix in ('qcow2', 'bin'):
590 path = '%s/%s.%s' % (self.get('DEPLOY_DIR_IMAGE'), ovmf, suffix)
591 if os.path.exists(path):
592 self.ovmf_bios[index] = path
593 break
594 else:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500595 raise RunQemuError("Can't find OVMF firmware: %s" % ovmf)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500596
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600597 def check_kernel(self):
Brad Bishop316dfdd2018-06-25 12:45:53 -0400598 """Check and set kernel"""
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600599 # The vm image doesn't need a kernel
600 if self.fstype in self.vmtypes:
601 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500602
Brad Bishop316dfdd2018-06-25 12:45:53 -0400603 # See if the user supplied a KERNEL option
604 if self.get('KERNEL'):
605 self.kernel = self.get('KERNEL')
606
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500607 # QB_DEFAULT_KERNEL is always a full file path
608 kernel_name = os.path.basename(self.get('QB_DEFAULT_KERNEL'))
609
610 # The user didn't want a kernel to be loaded
Brad Bishop316dfdd2018-06-25 12:45:53 -0400611 if kernel_name == "none" and not self.kernel:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500612 return
613
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600614 deploy_dir_image = self.get('DEPLOY_DIR_IMAGE')
615 if not self.kernel:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500616 kernel_match_name = "%s/%s" % (deploy_dir_image, kernel_name)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600617 kernel_match_link = "%s/%s" % (deploy_dir_image, self.get('KERNEL_IMAGETYPE'))
618 kernel_startswith = "%s/%s*" % (deploy_dir_image, self.get('KERNEL_IMAGETYPE'))
619 cmds = (kernel_match_name, kernel_match_link, kernel_startswith)
620 self.kernel = get_first_file(cmds)
621 if not self.kernel:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500622 raise RunQemuError('KERNEL not found: %s, %s or %s' % cmds)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500623
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600624 if not os.path.exists(self.kernel):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500625 raise RunQemuError("KERNEL %s not found" % self.kernel)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500626
Brad Bishop316dfdd2018-06-25 12:45:53 -0400627 def check_dtb(self):
628 """Check and set dtb"""
629 # Did the user specify a device tree?
630 if self.get('DEVICE_TREE'):
631 self.dtb = self.get('DEVICE_TREE')
632 if not os.path.exists(self.dtb):
633 raise RunQemuError('Specified DTB not found: %s' % self.dtb)
634 return
635
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600636 dtb = self.get('QB_DTB')
637 if dtb:
Brad Bishop316dfdd2018-06-25 12:45:53 -0400638 deploy_dir_image = self.get('DEPLOY_DIR_IMAGE')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600639 cmd_match = "%s/%s" % (deploy_dir_image, dtb)
640 cmd_startswith = "%s/%s*" % (deploy_dir_image, dtb)
641 cmd_wild = "%s/*.dtb" % deploy_dir_image
642 cmds = (cmd_match, cmd_startswith, cmd_wild)
643 self.dtb = get_first_file(cmds)
644 if not os.path.exists(self.dtb):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500645 raise RunQemuError('DTB not found: %s, %s or %s' % cmds)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500646
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600647 def check_biosdir(self):
648 """Check custombiosdir"""
649 if not self.custombiosdir:
650 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500651
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600652 biosdir = ""
653 biosdir_native = "%s/%s" % (self.get('STAGING_DIR_NATIVE'), self.custombiosdir)
654 biosdir_host = "%s/%s" % (self.get('STAGING_DIR_HOST'), self.custombiosdir)
655 for i in (self.custombiosdir, biosdir_native, biosdir_host):
656 if os.path.isdir(i):
657 biosdir = i
658 break
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500659
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600660 if biosdir:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500661 logger.debug("Assuming biosdir is: %s" % biosdir)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600662 self.qemu_opt_script += ' -L %s' % biosdir
663 else:
664 logger.error("Custom BIOS directory not found. Tried: %s, %s, and %s" % (self.custombiosdir, biosdir_native, biosdir_host))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500665 raise RunQemuError("Invalid custombiosdir: %s" % self.custombiosdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500666
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600667 def check_mem(self):
668 s = re.search('-m +([0-9]+)', self.qemu_opt_script)
669 if s:
670 self.set('QB_MEM', '-m %s' % s.group(1))
671 elif not self.get('QB_MEM'):
672 logger.info('QB_MEM is not set, use 512M by default')
673 self.set('QB_MEM', '-m 512')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500674
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800675 mach = self.get('MACHINE')
676 if not mach.startswith('qemumips'):
677 self.kernel_cmdline_script += ' mem=%s' % self.get('QB_MEM').replace('-m','').strip() + 'M'
678
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600679 self.qemu_opt_script += ' %s' % self.get('QB_MEM')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500680
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600681 def check_tcpserial(self):
682 if self.tcpserial_portnum:
683 if self.get('QB_TCPSERIAL_OPT'):
684 self.qemu_opt_script += ' ' + self.get('QB_TCPSERIAL_OPT').replace('@PORT@', self.tcpserial_portnum)
685 else:
686 self.qemu_opt_script += ' -serial tcp:127.0.0.1:%s' % self.tcpserial_portnum
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500687
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600688 def check_and_set(self):
689 """Check configs sanity and set when needed"""
690 self.validate_paths()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500691 if not self.slirp_enabled:
692 check_tun()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600693 # Check audio
694 if self.audio_enabled:
695 if not self.get('QB_AUDIO_DRV'):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500696 raise RunQemuError("QB_AUDIO_DRV is NULL, this board doesn't support audio")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600697 if not self.get('QB_AUDIO_OPT'):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800698 logger.warning('QB_AUDIO_OPT is NULL, you may need define it to make audio work')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600699 else:
700 self.qemu_opt_script += ' %s' % self.get('QB_AUDIO_OPT')
701 os.putenv('QEMU_AUDIO_DRV', self.get('QB_AUDIO_DRV'))
702 else:
703 os.putenv('QEMU_AUDIO_DRV', 'none')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500704
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600705 self.check_kvm()
706 self.check_fstype()
707 self.check_rootfs()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500708 self.check_ovmf()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600709 self.check_kernel()
Brad Bishop316dfdd2018-06-25 12:45:53 -0400710 self.check_dtb()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600711 self.check_biosdir()
712 self.check_mem()
713 self.check_tcpserial()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500714
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600715 def read_qemuboot(self):
716 if not self.qemuboot:
717 if self.get('DEPLOY_DIR_IMAGE'):
718 deploy_dir_image = self.get('DEPLOY_DIR_IMAGE')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600719 else:
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800720 logger.warning("Can't find qemuboot conf file, DEPLOY_DIR_IMAGE is NULL!")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600721 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500722
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600723 if self.rootfs and not os.path.exists(self.rootfs):
724 # Lazy rootfs
725 machine = self.get('MACHINE')
726 if not machine:
727 machine = os.path.basename(deploy_dir_image)
728 self.qemuboot = "%s/%s-%s.qemuboot.conf" % (deploy_dir_image,
729 self.rootfs, machine)
730 else:
731 cmd = 'ls -t %s/*.qemuboot.conf' % deploy_dir_image
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500732 logger.debug('Running %s...' % cmd)
733 try:
734 qbs = subprocess.check_output(cmd, shell=True).decode('utf-8')
735 except subprocess.CalledProcessError as err:
736 raise RunQemuError(err)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600737 if qbs:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500738 for qb in qbs.split():
739 # Don't use initramfs when other choices unless fstype is ramfs
740 if '-initramfs-' in os.path.basename(qb) and self.fstype != 'cpio.gz':
741 continue
742 self.qemuboot = qb
743 break
744 if not self.qemuboot:
745 # Use the first one when no choice
746 self.qemuboot = qbs.split()[0]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600747 self.qbconfload = True
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500748
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600749 if not self.qemuboot:
750 # If we haven't found a .qemuboot.conf at this point it probably
751 # doesn't exist, continue without
752 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500753
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600754 if not os.path.exists(self.qemuboot):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500755 raise RunQemuError("Failed to find %s (wrong image name or BSP does not support running under qemu?)." % self.qemuboot)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500756
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500757 logger.debug('CONFFILE: %s' % self.qemuboot)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500758
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600759 cf = configparser.ConfigParser()
760 cf.read(self.qemuboot)
761 for k, v in cf.items('config_bsp'):
762 k_upper = k.upper()
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500763 if v.startswith("../"):
764 v = os.path.abspath(os.path.dirname(self.qemuboot) + "/" + v)
765 elif v == ".":
766 v = os.path.dirname(self.qemuboot)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600767 self.set(k_upper, v)
768
769 def validate_paths(self):
770 """Ensure all relevant path variables are set"""
771 # When we're started with a *.qemuboot.conf arg assume that image
772 # artefacts are relative to that file, rather than in whatever
773 # directory DEPLOY_DIR_IMAGE in the conf file points to.
774 if self.qbconfload:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500775 imgdir = os.path.realpath(os.path.dirname(self.qemuboot))
776 if imgdir != os.path.realpath(self.get('DEPLOY_DIR_IMAGE')):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600777 logger.info('Setting DEPLOY_DIR_IMAGE to folder containing %s (%s)' % (self.qemuboot, imgdir))
778 self.set('DEPLOY_DIR_IMAGE', imgdir)
779
780 # If the STAGING_*_NATIVE directories from the config file don't exist
781 # and we're in a sourced OE build directory try to extract the paths
782 # from `bitbake -e`
783 havenative = os.path.exists(self.get('STAGING_DIR_NATIVE')) and \
784 os.path.exists(self.get('STAGING_BINDIR_NATIVE'))
785
786 if not havenative:
787 if not self.bitbake_e:
788 self.load_bitbake_env()
789
790 if self.bitbake_e:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500791 native_vars = ['STAGING_DIR_NATIVE']
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600792 for nv in native_vars:
793 s = re.search('^%s="(.*)"' % nv, self.bitbake_e, re.M)
794 if s and s.group(1) != self.get(nv):
795 logger.info('Overriding conf file setting of %s to %s from Bitbake environment' % (nv, s.group(1)))
796 self.set(nv, s.group(1))
797 else:
798 # when we're invoked from a running bitbake instance we won't
799 # be able to call `bitbake -e`, then try:
800 # - get OE_TMPDIR from environment and guess paths based on it
801 # - get OECORE_NATIVE_SYSROOT from environment (for sdk)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500802 tmpdir = self.get('OE_TMPDIR')
803 oecore_native_sysroot = self.get('OECORE_NATIVE_SYSROOT')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600804 if tmpdir:
805 logger.info('Setting STAGING_DIR_NATIVE and STAGING_BINDIR_NATIVE relative to OE_TMPDIR (%s)' % tmpdir)
806 hostos, _, _, _, machine = os.uname()
807 buildsys = '%s-%s' % (machine, hostos.lower())
808 staging_dir_native = '%s/sysroots/%s' % (tmpdir, buildsys)
809 self.set('STAGING_DIR_NATIVE', staging_dir_native)
810 elif oecore_native_sysroot:
811 logger.info('Setting STAGING_DIR_NATIVE to OECORE_NATIVE_SYSROOT (%s)' % oecore_native_sysroot)
812 self.set('STAGING_DIR_NATIVE', oecore_native_sysroot)
813 if self.get('STAGING_DIR_NATIVE'):
814 # we have to assume that STAGING_BINDIR_NATIVE is at usr/bin
815 staging_bindir_native = '%s/usr/bin' % self.get('STAGING_DIR_NATIVE')
816 logger.info('Setting STAGING_BINDIR_NATIVE to %s' % staging_bindir_native)
817 self.set('STAGING_BINDIR_NATIVE', '%s/usr/bin' % self.get('STAGING_DIR_NATIVE'))
818
819 def print_config(self):
820 logger.info('Continuing with the following parameters:\n')
821 if not self.fstype in self.vmtypes:
822 print('KERNEL: [%s]' % self.kernel)
823 if self.dtb:
824 print('DTB: [%s]' % self.dtb)
825 print('MACHINE: [%s]' % self.get('MACHINE'))
826 print('FSTYPE: [%s]' % self.fstype)
827 if self.fstype == 'nfs':
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500828 print('NFS_DIR: [%s]' % self.rootfs)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600829 else:
830 print('ROOTFS: [%s]' % self.rootfs)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500831 if self.ovmf_bios:
832 print('OVMF: %s' % self.ovmf_bios)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600833 print('CONFFILE: [%s]' % self.qemuboot)
834 print('')
835
836 def setup_nfs(self):
837 if not self.nfs_server:
838 if self.slirp_enabled:
839 self.nfs_server = '10.0.2.2'
840 else:
841 self.nfs_server = '192.168.7.1'
842
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500843 # Figure out a new nfs_instance to allow multiple qemus running.
844 # CentOS 7.1's ps doesn't print full command line without "ww"
845 # when invoke by subprocess.Popen().
846 cmd = "ps auxww"
847 ps = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8')
848 pattern = '/bin/unfsd .* -i .*\.pid -e .*/exports([0-9]+) '
849 all_instances = re.findall(pattern, ps, re.M)
850 if all_instances:
851 all_instances.sort(key=int)
852 self.nfs_instance = int(all_instances.pop()) + 1
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600853
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500854 nfsd_port = 3049 + 2 * self.nfs_instance
855 mountd_port = 3048 + 2 * self.nfs_instance
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600856
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500857 # Export vars for runqemu-export-rootfs
858 export_dict = {
859 'NFS_INSTANCE': self.nfs_instance,
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500860 'NFSD_PORT': nfsd_port,
861 'MOUNTD_PORT': mountd_port,
862 }
863 for k, v in export_dict.items():
864 # Use '%s' since they are integers
865 os.putenv(k, '%s' % v)
866
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500867 self.unfs_opts="nfsvers=3,port=%s,udp,mountport=%s" % (nfsd_port, mountd_port)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600868
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500869 # Extract .tar.bz2 or .tar.bz if no nfs dir
870 if not (self.rootfs and os.path.isdir(self.rootfs)):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600871 src_prefix = '%s/%s' % (self.get('DEPLOY_DIR_IMAGE'), self.get('IMAGE_LINK_NAME'))
872 dest = "%s-nfsroot" % src_prefix
873 if os.path.exists('%s.pseudo_state' % dest):
874 logger.info('Use %s as NFS_DIR' % dest)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500875 self.rootfs = dest
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600876 else:
877 src = ""
878 src1 = '%s.tar.bz2' % src_prefix
879 src2 = '%s.tar.gz' % src_prefix
880 if os.path.exists(src1):
881 src = src1
882 elif os.path.exists(src2):
883 src = src2
884 if not src:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500885 raise RunQemuError("No NFS_DIR is set, and can't find %s or %s to extract" % (src1, src2))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600886 logger.info('NFS_DIR not found, extracting %s to %s' % (src, dest))
887 cmd = 'runqemu-extract-sdk %s %s' % (src, dest)
888 logger.info('Running %s...' % cmd)
889 if subprocess.call(cmd, shell=True) != 0:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500890 raise RunQemuError('Failed to run %s' % cmd)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600891 self.clean_nfs_dir = True
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500892 self.rootfs = dest
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600893
894 # Start the userspace NFS server
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500895 cmd = 'runqemu-export-rootfs start %s' % self.rootfs
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600896 logger.info('Running %s...' % cmd)
897 if subprocess.call(cmd, shell=True) != 0:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500898 raise RunQemuError('Failed to run %s' % cmd)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600899
900 self.nfs_running = True
901
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600902 def setup_slirp(self):
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500903 """Setup user networking"""
904
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600905 if self.fstype == 'nfs':
906 self.setup_nfs()
907 self.kernel_cmdline_script += ' ip=dhcp'
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500908 # Port mapping
909 hostfwd = ",hostfwd=tcp::2222-:22,hostfwd=tcp::2323-:23"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500910 qb_slirp_opt_default = "-netdev user,id=net0%s,tftp=%s" % (hostfwd, self.get('DEPLOY_DIR_IMAGE'))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500911 qb_slirp_opt = self.get('QB_SLIRP_OPT') or qb_slirp_opt_default
912 # Figure out the port
913 ports = re.findall('hostfwd=[^-]*:([0-9]+)-[^,-]*', qb_slirp_opt)
914 ports = [int(i) for i in ports]
915 mac = 2
916 # Find a free port to avoid conflicts
917 for p in ports[:]:
918 p_new = p
919 while not check_free_port('localhost', p_new):
920 p_new += 1
921 mac += 1
922 while p_new in ports:
923 p_new += 1
924 mac += 1
925 if p != p_new:
926 ports.append(p_new)
927 qb_slirp_opt = re.sub(':%s-' % p, ':%s-' % p_new, qb_slirp_opt)
928 logger.info("Port forward changed: %s -> %s" % (p, p_new))
929 mac = "%s%02x" % (self.mac_slirp, mac)
930 self.set('NETWORK_CMD', '%s %s' % (self.network_device.replace('@MAC@', mac), qb_slirp_opt))
931 # Print out port foward
932 hostfwd = re.findall('(hostfwd=[^,]*)', qb_slirp_opt)
933 if hostfwd:
934 logger.info('Port forward: %s' % ' '.join(hostfwd))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600935
936 def setup_tap(self):
937 """Setup tap"""
938
939 # This file is created when runqemu-gen-tapdevs creates a bank of tap
940 # devices, indicating that the user should not bring up new ones using
941 # sudo.
942 nosudo_flag = '/etc/runqemu-nosudo'
943 self.qemuifup = shutil.which('runqemu-ifup')
944 self.qemuifdown = shutil.which('runqemu-ifdown')
945 ip = shutil.which('ip')
946 lockdir = "/tmp/qemu-tap-locks"
947
948 if not (self.qemuifup and self.qemuifdown and ip):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500949 logger.error("runqemu-ifup: %s" % self.qemuifup)
950 logger.error("runqemu-ifdown: %s" % self.qemuifdown)
951 logger.error("ip: %s" % ip)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600952 raise OEPathError("runqemu-ifup, runqemu-ifdown or ip not found")
953
954 if not os.path.exists(lockdir):
955 # There might be a race issue when multi runqemu processess are
956 # running at the same time.
957 try:
958 os.mkdir(lockdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500959 os.chmod(lockdir, 0o777)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600960 except FileExistsError:
961 pass
962
963 cmd = '%s link' % ip
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500964 logger.debug('Running %s...' % cmd)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600965 ip_link = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8')
966 # Matches line like: 6: tap0: <foo>
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500967 possibles = re.findall('^[0-9]+: +(tap[0-9]+): <.*', ip_link, re.M)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600968 tap = ""
969 for p in possibles:
970 lockfile = os.path.join(lockdir, p)
971 if os.path.exists('%s.skip' % lockfile):
972 logger.info('Found %s.skip, skipping %s' % (lockfile, p))
973 continue
974 self.lock = lockfile + '.lock'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500975 if self.acquire_lock(error=False):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600976 tap = p
977 logger.info("Using preconfigured tap device %s" % tap)
978 logger.info("If this is not intended, touch %s.skip to make runqemu skip %s." %(lockfile, tap))
979 break
980
981 if not tap:
982 if os.path.exists(nosudo_flag):
983 logger.error("Error: There are no available tap devices to use for networking,")
984 logger.error("and I see %s exists, so I am not going to try creating" % nosudo_flag)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500985 raise RunQemuError("a new one with sudo.")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600986
987 gid = os.getgid()
988 uid = os.getuid()
989 logger.info("Setting up tap interface under sudo")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500990 cmd = 'sudo %s %s %s %s' % (self.qemuifup, uid, gid, self.bindir_native)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600991 tap = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8').rstrip('\n')
992 lockfile = os.path.join(lockdir, tap)
993 self.lock = lockfile + '.lock'
994 self.acquire_lock()
995 self.cleantap = True
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500996 logger.debug('Created tap: %s' % tap)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600997
998 if not tap:
999 logger.error("Failed to setup tap device. Run runqemu-gen-tapdevs to manually create.")
1000 return 1
1001 self.tap = tap
Brad Bishop37a0e4d2017-12-04 01:01:44 -05001002 tapnum = int(tap[3:])
1003 gateway = tapnum * 2 + 1
1004 client = gateway + 1
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001005 if self.fstype == 'nfs':
1006 self.setup_nfs()
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001007 netconf = "192.168.7.%s::192.168.7.%s:255.255.255.0" % (client, gateway)
1008 logger.info("Network configuration: %s", netconf)
1009 self.kernel_cmdline_script += " ip=%s" % netconf
1010 mac = "%s%02x" % (self.mac_tap, client)
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001011 qb_tap_opt = self.get('QB_TAP_OPT')
1012 if qb_tap_opt:
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001013 qemu_tap_opt = qb_tap_opt.replace('@TAP@', tap)
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001014 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001015 qemu_tap_opt = "-netdev tap,id=net0,ifname=%s,script=no,downscript=no" % (self.tap)
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001016
1017 if self.vhost_enabled:
1018 qemu_tap_opt += ',vhost=on'
1019
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001020 self.set('NETWORK_CMD', '%s %s' % (self.network_device.replace('@MAC@', mac), qemu_tap_opt))
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001021
1022 def setup_network(self):
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001023 if self.get('QB_NET') == 'none':
1024 return
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001025 if sys.stdin.isatty():
1026 self.saved_stty = subprocess.check_output("stty -g", shell=True).decode('utf-8')
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001027 self.network_device = self.get('QB_NETWORK_DEVICE') or self.network_device
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001028 if self.slirp_enabled:
1029 self.setup_slirp()
1030 else:
1031 self.setup_tap()
1032
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001033 def setup_rootfs(self):
1034 if self.get('QB_ROOTFS') == 'none':
1035 return
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001036 if 'wic.' in self.fstype:
1037 self.fstype = self.fstype[4:]
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001038 rootfs_format = self.fstype if self.fstype in ('vmdk', 'qcow2', 'vdi') else 'raw'
1039
1040 qb_rootfs_opt = self.get('QB_ROOTFS_OPT')
1041 if qb_rootfs_opt:
1042 self.rootfs_options = qb_rootfs_opt.replace('@ROOTFS@', self.rootfs)
1043 else:
1044 self.rootfs_options = '-drive file=%s,if=virtio,format=%s' % (self.rootfs, rootfs_format)
1045
1046 if self.fstype in ('cpio.gz', 'cpio'):
1047 self.kernel_cmdline = 'root=/dev/ram0 rw debugshell'
1048 self.rootfs_options = '-initrd %s' % self.rootfs
1049 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001050 vm_drive = ''
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001051 if self.fstype in self.vmtypes:
1052 if self.fstype == 'iso':
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001053 vm_drive = '-drive file=%s,if=virtio,media=cdrom' % self.rootfs
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001054 elif self.get('QB_DRIVE_TYPE'):
1055 drive_type = self.get('QB_DRIVE_TYPE')
1056 if drive_type.startswith("/dev/sd"):
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001057 logger.info('Using scsi drive')
1058 vm_drive = '-drive if=none,id=hd,file=%s,format=%s -device virtio-scsi-pci,id=scsi -device scsi-hd,drive=hd' \
1059 % (self.rootfs, rootfs_format)
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001060 elif drive_type.startswith("/dev/hd"):
Brad Bishop37a0e4d2017-12-04 01:01:44 -05001061 logger.info('Using ide drive')
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001062 vm_drive = "-drive file=%s,format=%s" % (self.rootfs, rootfs_format)
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001063 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001064 # virtio might have been selected explicitly (just use it), or
1065 # is used as fallback (then warn about that).
1066 if not drive_type.startswith("/dev/vd"):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001067 logger.warning("Unknown QB_DRIVE_TYPE: %s" % drive_type)
1068 logger.warning("Failed to figure out drive type, consider define or fix QB_DRIVE_TYPE")
1069 logger.warning('Trying to use virtio block drive')
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001070 vm_drive = '-drive if=virtio,file=%s,format=%s' % (self.rootfs, rootfs_format)
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001071
1072 # All branches above set vm_drive.
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001073 self.rootfs_options = '%s -no-reboot' % vm_drive
1074 self.kernel_cmdline = 'root=%s rw highres=off' % (self.get('QB_KERNEL_ROOT'))
1075
1076 if self.fstype == 'nfs':
1077 self.rootfs_options = ''
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001078 k_root = '/dev/nfs nfsroot=%s:%s,%s' % (self.nfs_server, os.path.abspath(self.rootfs), self.unfs_opts)
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001079 self.kernel_cmdline = 'root=%s rw highres=off' % k_root
1080
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001081 if self.fstype == 'none':
1082 self.rootfs_options = ''
1083
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001084 self.set('ROOTFS_OPTIONS', self.rootfs_options)
1085
1086 def guess_qb_system(self):
1087 """attempt to determine the appropriate qemu-system binary"""
1088 mach = self.get('MACHINE')
1089 if not mach:
1090 search = '.*(qemux86-64|qemux86|qemuarm64|qemuarm|qemumips64|qemumips64el|qemumipsel|qemumips|qemuppc).*'
1091 if self.rootfs:
1092 match = re.match(search, self.rootfs)
1093 if match:
1094 mach = match.group(1)
1095 elif self.kernel:
1096 match = re.match(search, self.kernel)
1097 if match:
1098 mach = match.group(1)
1099
1100 if not mach:
1101 return None
1102
1103 if mach == 'qemuarm':
1104 qbsys = 'arm'
1105 elif mach == 'qemuarm64':
1106 qbsys = 'aarch64'
1107 elif mach == 'qemux86':
1108 qbsys = 'i386'
1109 elif mach == 'qemux86-64':
1110 qbsys = 'x86_64'
1111 elif mach == 'qemuppc':
1112 qbsys = 'ppc'
1113 elif mach == 'qemumips':
1114 qbsys = 'mips'
1115 elif mach == 'qemumips64':
1116 qbsys = 'mips64'
1117 elif mach == 'qemumipsel':
1118 qbsys = 'mipsel'
1119 elif mach == 'qemumips64el':
1120 qbsys = 'mips64el'
Brad Bishop316dfdd2018-06-25 12:45:53 -04001121 elif mach == 'qemuriscv64':
1122 qbsys = 'riscv64'
1123 elif mach == 'qemuriscv32':
1124 qbsys = 'riscv32'
Brad Bishop004d4992018-10-02 23:54:45 +02001125 else:
1126 logger.error("Unable to determine QEMU PC System emulator for %s machine." % mach)
1127 logger.error("As %s is not among valid QEMU machines such as," % mach)
1128 logger.error("qemux86-64, qemux86, qemuarm64, qemuarm, qemumips64, qemumips64el, qemumipsel, qemumips, qemuppc")
1129 raise RunQemuError("Set qb_system_name with suitable QEMU PC System emulator in .*qemuboot.conf.")
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001130
1131 return 'qemu-system-%s' % qbsys
1132
1133 def setup_final(self):
1134 qemu_system = self.get('QB_SYSTEM_NAME')
1135 if not qemu_system:
1136 qemu_system = self.guess_qb_system()
1137 if not qemu_system:
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001138 raise RunQemuError("Failed to boot, QB_SYSTEM_NAME is NULL!")
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001139
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001140 qemu_bin = '%s/%s' % (self.bindir_native, qemu_system)
1141
1142 # It is possible to have qemu-native in ASSUME_PROVIDED, and it won't
1143 # find QEMU in sysroot, it needs to use host's qemu.
1144 if not os.path.exists(qemu_bin):
1145 logger.info("QEMU binary not found in %s, trying host's QEMU" % qemu_bin)
1146 for path in (os.environ['PATH'] or '').split(':'):
1147 qemu_bin_tmp = os.path.join(path, qemu_system)
1148 logger.info("Trying: %s" % qemu_bin_tmp)
1149 if os.path.exists(qemu_bin_tmp):
1150 qemu_bin = qemu_bin_tmp
1151 if not os.path.isabs(qemu_bin):
1152 qemu_bin = os.path.abspath(qemu_bin)
1153 logger.info("Using host's QEMU: %s" % qemu_bin)
1154 break
1155
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001156 if not os.access(qemu_bin, os.X_OK):
1157 raise OEPathError("No QEMU binary '%s' could be found" % qemu_bin)
1158
1159 check_libgl(qemu_bin)
1160
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001161 self.qemu_opt = "%s %s %s %s" % (qemu_bin, self.get('NETWORK_CMD'), self.get('ROOTFS_OPTIONS'), self.get('QB_OPT_APPEND'))
1162
1163 for ovmf in self.ovmf_bios:
1164 format = ovmf.rsplit('.', 1)[-1]
1165 self.qemu_opt += ' -drive if=pflash,format=%s,file=%s' % (format, ovmf)
1166 if self.ovmf_bios:
1167 # OVMF only supports normal VGA, i.e. we need to override a -vga vmware
1168 # that gets added for example for normal qemux86.
1169 self.qemu_opt += ' -vga std'
1170
1171 self.qemu_opt += ' ' + self.qemu_opt_script
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001172
1173 if self.snapshot:
1174 self.qemu_opt += " -snapshot"
1175
1176 if self.serialstdio:
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001177 if sys.stdin.isatty():
1178 subprocess.check_call("stty intr ^]", shell=True)
1179 logger.info("Interrupt character is '^]'")
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001180
1181 first_serial = ""
1182 if not re.search("-nographic", self.qemu_opt):
1183 first_serial = "-serial mon:vc"
1184 # We always want a ttyS1. Since qemu by default adds a serial
1185 # port when nodefaults is not specified, it seems that all that
1186 # would be needed is to make sure a "-serial" is there. However,
1187 # it appears that when "-serial" is specified, it ignores the
1188 # default serial port that is normally added. So here we make
1189 # sure to add two -serial if there are none. And only one if
1190 # there is one -serial already.
1191 serial_num = len(re.findall("-serial", self.qemu_opt))
1192 if serial_num == 0:
1193 self.qemu_opt += " %s %s" % (first_serial, self.get("QB_SERIAL_OPT"))
1194 elif serial_num == 1:
1195 self.qemu_opt += " %s" % self.get("QB_SERIAL_OPT")
1196
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001197 # We always wants ttyS0 and ttyS1 in qemu machines (see SERIAL_CONSOLES),
1198 # if not serial or serialtcp options was specified only ttyS0 is created
1199 # and sysvinit shows an error trying to enable ttyS1:
1200 # INIT: Id "S1" respawning too fast: disabled for 5 minutes
1201 serial_num = len(re.findall("-serial", self.qemu_opt))
1202 if serial_num == 0:
1203 if re.search("-nographic", self.qemu_opt):
1204 self.qemu_opt += " -serial mon:stdio -serial null"
1205 else:
1206 self.qemu_opt += " -serial mon:vc -serial null"
1207
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001208 def start_qemu(self):
Brad Bishop004d4992018-10-02 23:54:45 +02001209 import shlex
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001210 if self.kernel:
Brad Bishop37a0e4d2017-12-04 01:01:44 -05001211 kernel_opts = "-kernel %s -append '%s %s %s %s'" % (self.kernel, self.kernel_cmdline,
1212 self.kernel_cmdline_script, self.get('QB_KERNEL_CMDLINE_APPEND'),
1213 self.bootparams)
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001214 if self.dtb:
1215 kernel_opts += " -dtb %s" % self.dtb
1216 else:
1217 kernel_opts = ""
1218 cmd = "%s %s" % (self.qemu_opt, kernel_opts)
Brad Bishop004d4992018-10-02 23:54:45 +02001219 cmds = shlex.split(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001220 logger.info('Running %s\n' % cmd)
Brad Bishopf86d0552018-12-04 14:18:15 -08001221 pass_fds = []
1222 if self.lock_descriptor:
1223 pass_fds = [self.lock_descriptor.fileno()]
1224 process = subprocess.Popen(cmds, stderr=subprocess.PIPE, pass_fds=pass_fds)
Brad Bishop004d4992018-10-02 23:54:45 +02001225 self.qemupid = process.pid
1226 retcode = process.wait()
1227 if retcode:
1228 if retcode == -signal.SIGTERM:
1229 logger.info("Qemu terminated by SIGTERM")
1230 else:
1231 logger.error("Failed to run qemu: %s", process.stderr.read().decode())
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001232
1233 def cleanup(self):
Brad Bishop004d4992018-10-02 23:54:45 +02001234 if self.cleaned:
1235 return
1236
1237 # avoid dealing with SIGTERM when cleanup function is running
1238 signal.signal(signal.SIGTERM, signal.SIG_IGN)
1239
1240 logger.info("Cleaning up")
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001241 if self.cleantap:
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001242 cmd = 'sudo %s %s %s' % (self.qemuifdown, self.tap, self.bindir_native)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001243 logger.debug('Running %s' % cmd)
1244 subprocess.check_call(cmd, shell=True)
Brad Bishopf86d0552018-12-04 14:18:15 -08001245 self.release_lock()
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001246
1247 if self.nfs_running:
1248 logger.info("Shutting down the userspace NFS server...")
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001249 cmd = "runqemu-export-rootfs stop %s" % self.rootfs
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001250 logger.debug('Running %s' % cmd)
1251 subprocess.check_call(cmd, shell=True)
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001252
1253 if self.saved_stty:
1254 cmd = "stty %s" % self.saved_stty
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001255 subprocess.check_call(cmd, shell=True)
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001256
1257 if self.clean_nfs_dir:
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001258 logger.info('Removing %s' % self.rootfs)
1259 shutil.rmtree(self.rootfs)
1260 shutil.rmtree('%s.pseudo_state' % self.rootfs)
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001261
Brad Bishop004d4992018-10-02 23:54:45 +02001262 self.cleaned = True
1263
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001264 def load_bitbake_env(self, mach=None):
1265 if self.bitbake_e:
1266 return
1267
1268 bitbake = shutil.which('bitbake')
1269 if not bitbake:
1270 return
1271
1272 if not mach:
1273 mach = self.get('MACHINE')
1274
1275 if mach:
1276 cmd = 'MACHINE=%s bitbake -e' % mach
1277 else:
1278 cmd = 'bitbake -e'
1279
1280 logger.info('Running %s...' % cmd)
1281 try:
1282 self.bitbake_e = subprocess.check_output(cmd, shell=True).decode('utf-8')
1283 except subprocess.CalledProcessError as err:
1284 self.bitbake_e = ''
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001285 logger.warning("Couldn't run 'bitbake -e' to gather environment information:\n%s" % err.output.decode('utf-8'))
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001286
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001287 def validate_combos(self):
1288 if (self.fstype in self.vmtypes) and self.kernel:
1289 raise RunQemuError("%s doesn't need kernel %s!" % (self.fstype, self.kernel))
1290
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001291 @property
1292 def bindir_native(self):
1293 result = self.get('STAGING_BINDIR_NATIVE')
1294 if result and os.path.exists(result):
1295 return result
1296
1297 cmd = 'bitbake qemu-helper-native -e'
1298 logger.info('Running %s...' % cmd)
1299 out = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
1300 out = out.stdout.read().decode('utf-8')
1301
1302 match = re.search('^STAGING_BINDIR_NATIVE="(.*)"', out, re.M)
1303 if match:
1304 result = match.group(1)
1305 if os.path.exists(result):
1306 self.set('STAGING_BINDIR_NATIVE', result)
1307 return result
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001308 raise RunQemuError("Native sysroot directory %s doesn't exist" % result)
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001309 else:
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001310 raise RunQemuError("Can't find STAGING_BINDIR_NATIVE in '%s' output" % cmd)
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001311
1312
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001313def main():
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001314 if "help" in sys.argv or '-h' in sys.argv or '--help' in sys.argv:
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001315 print_usage()
1316 return 0
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001317 try:
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001318 config = BaseConfig()
Brad Bishop004d4992018-10-02 23:54:45 +02001319
1320 def sigterm_handler(signum, frame):
1321 logger.info("SIGTERM received")
1322 os.kill(config.qemupid, signal.SIGTERM)
1323 config.cleanup()
Brad Bishopf86d0552018-12-04 14:18:15 -08001324 subprocess.check_call(["tput", "smam"])
Brad Bishop004d4992018-10-02 23:54:45 +02001325 signal.signal(signal.SIGTERM, sigterm_handler)
1326
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001327 config.check_args()
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001328 config.read_qemuboot()
1329 config.check_and_set()
1330 # Check whether the combos is valid or not
1331 config.validate_combos()
1332 config.print_config()
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001333 config.setup_network()
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001334 config.setup_rootfs()
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001335 config.setup_final()
1336 config.start_qemu()
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001337 except RunQemuError as err:
1338 logger.error(err)
1339 return 1
1340 except Exception as err:
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001341 import traceback
1342 traceback.print_exc()
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001343 return 1
1344 finally:
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001345 config.cleanup()
Brad Bishopf86d0552018-12-04 14:18:15 -08001346 subprocess.check_call(["tput", "smam"])
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001347
1348if __name__ == "__main__":
1349 sys.exit(main())