runqemu 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  1. #!/usr/bin/env python3
  2. # Handle running OE images standalone with QEMU
  3. #
  4. # Copyright (C) 2006-2011 Linux Foundation
  5. # Copyright (c) 2016 Wind River Systems, Inc.
  6. #
  7. # SPDX-License-Identifier: GPL-2.0-only
  8. #
  9. import os
  10. import sys
  11. import logging
  12. import subprocess
  13. import re
  14. import fcntl
  15. import shutil
  16. import glob
  17. import configparser
  18. import signal
  19. class RunQemuError(Exception):
  20. """Custom exception to raise on known errors."""
  21. pass
  22. class OEPathError(RunQemuError):
  23. """Custom Exception to give better guidance on missing binaries"""
  24. def __init__(self, message):
  25. super().__init__("In order for this script to dynamically infer paths\n \
  26. kernels or filesystem images, you either need bitbake in your PATH\n \
  27. or to source oe-init-build-env before running this script.\n\n \
  28. Dynamic path inference can be avoided by passing a *.qemuboot.conf to\n \
  29. runqemu, i.e. `runqemu /path/to/my-image-name.qemuboot.conf`\n\n %s" % message)
  30. def create_logger():
  31. logger = logging.getLogger('runqemu')
  32. logger.setLevel(logging.INFO)
  33. # create console handler and set level to debug
  34. ch = logging.StreamHandler()
  35. ch.setLevel(logging.DEBUG)
  36. # create formatter
  37. formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
  38. # add formatter to ch
  39. ch.setFormatter(formatter)
  40. # add ch to logger
  41. logger.addHandler(ch)
  42. return logger
  43. logger = create_logger()
  44. def print_usage():
  45. print("""
  46. Usage: you can run this script with any valid combination
  47. of the following environment variables (in any order):
  48. KERNEL - the kernel image file to use
  49. ROOTFS - the rootfs image file or nfsroot directory to use
  50. DEVICE_TREE - the device tree blob to use
  51. MACHINE - the machine name (optional, autodetected from KERNEL filename if unspecified)
  52. Simplified QEMU command-line options can be passed with:
  53. nographic - disable video console
  54. sdl - choose the SDL frontend instead of the Gtk+ default
  55. gtk-gl - enable virgl-based GL acceleration using Gtk+ frontend
  56. gtk-gl-es - enable virgl-based GL acceleration, using OpenGL ES and Gtk+ frontend
  57. egl-headless - enable headless EGL output; use vnc or spice to see it
  58. serial - enable a serial console on /dev/ttyS0
  59. serialstdio - enable a serial console on the console (regardless of graphics mode)
  60. slirp - enable user networking, no root privileges is required
  61. kvm - enable KVM when running x86/x86_64 (VT-capable CPU required)
  62. kvm-vhost - enable KVM with vhost when running x86/x86_64 (VT-capable CPU required)
  63. publicvnc - enable a VNC server open to all hosts
  64. audio - enable audio
  65. [*/]ovmf* - OVMF firmware file or base name for booting with UEFI
  66. tcpserial=<port> - specify tcp serial port number
  67. biosdir=<dir> - specify custom bios dir
  68. biosfilename=<filename> - specify bios filename
  69. qemuparams=<xyz> - specify custom parameters to QEMU
  70. bootparams=<xyz> - specify custom kernel parameters during boot
  71. help, -h, --help: print this text
  72. -d, --debug: Enable debug output
  73. -q, --quite: Hide most output except error messages
  74. Examples:
  75. runqemu
  76. runqemu qemuarm
  77. runqemu tmp/deploy/images/qemuarm
  78. runqemu tmp/deploy/images/qemux86/<qemuboot.conf>
  79. runqemu qemux86-64 core-image-sato ext4
  80. runqemu qemux86-64 wic-image-minimal wic
  81. runqemu path/to/bzImage-qemux86.bin path/to/nfsrootdir/ serial
  82. runqemu qemux86 iso/hddimg/wic.vmdk/wic.qcow2/wic.vdi/ramfs/cpio.gz...
  83. runqemu qemux86 qemuparams="-m 256"
  84. runqemu qemux86 bootparams="psplash=false"
  85. runqemu path/to/<image>-<machine>.wic
  86. runqemu path/to/<image>-<machine>.wic.vmdk
  87. """)
  88. def check_tun():
  89. """Check /dev/net/tun"""
  90. dev_tun = '/dev/net/tun'
  91. if not os.path.exists(dev_tun):
  92. raise RunQemuError("TUN control device %s is unavailable; you may need to enable TUN (e.g. sudo modprobe tun)" % dev_tun)
  93. if not os.access(dev_tun, os.W_OK):
  94. raise RunQemuError("TUN control device %s is not writable, please fix (e.g. sudo chmod 666 %s)" % (dev_tun, dev_tun))
  95. def get_first_file(cmds):
  96. """Return first file found in wildcard cmds"""
  97. for cmd in cmds:
  98. all_files = glob.glob(cmd)
  99. if all_files:
  100. for f in all_files:
  101. if not os.path.isdir(f):
  102. return f
  103. return ''
  104. def check_free_port(host, port):
  105. """ Check whether the port is free or not """
  106. import socket
  107. from contextlib import closing
  108. with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
  109. if sock.connect_ex((host, port)) == 0:
  110. # Port is open, so not free
  111. return False
  112. else:
  113. # Port is not open, so free
  114. return True
  115. class BaseConfig(object):
  116. def __init__(self):
  117. # The self.d saved vars from self.set(), part of them are from qemuboot.conf
  118. self.d = {'QB_KERNEL_ROOT': '/dev/vda'}
  119. # Supported env vars, add it here if a var can be got from env,
  120. # and don't use os.getenv in the code.
  121. self.env_vars = ('MACHINE',
  122. 'ROOTFS',
  123. 'KERNEL',
  124. 'DEVICE_TREE',
  125. 'DEPLOY_DIR_IMAGE',
  126. 'OE_TMPDIR',
  127. 'OECORE_NATIVE_SYSROOT',
  128. )
  129. self.qemu_opt = ''
  130. self.qemu_opt_script = ''
  131. self.qemuparams = ''
  132. self.clean_nfs_dir = False
  133. self.nfs_server = ''
  134. self.rootfs = ''
  135. # File name(s) of a OVMF firmware file or variable store,
  136. # to be added with -drive if=pflash.
  137. # Found in the same places as the rootfs, with or without one of
  138. # these suffices: qcow2, bin.
  139. # Setting one also adds "-vga std" because that is all that
  140. # OVMF supports.
  141. self.ovmf_bios = []
  142. self.qemuboot = ''
  143. self.qbconfload = False
  144. self.kernel = ''
  145. self.kernel_cmdline = ''
  146. self.kernel_cmdline_script = ''
  147. self.bootparams = ''
  148. self.dtb = ''
  149. self.fstype = ''
  150. self.kvm_enabled = False
  151. self.vhost_enabled = False
  152. self.slirp_enabled = False
  153. self.nfs_instance = 0
  154. self.nfs_running = False
  155. self.serialconsole = False
  156. self.serialstdio = False
  157. self.cleantap = False
  158. self.saved_stty = ''
  159. self.audio_enabled = False
  160. self.tcpserial_portnum = ''
  161. self.custombiosdir = ''
  162. self.lock = ''
  163. self.lock_descriptor = None
  164. self.bitbake_e = ''
  165. self.snapshot = False
  166. self.fstypes = ('ext2', 'ext3', 'ext4', 'jffs2', 'nfs', 'btrfs',
  167. 'cpio.gz', 'cpio', 'ramfs', 'tar.bz2', 'tar.gz')
  168. self.vmtypes = ('hddimg', 'hdddirect', 'wic', 'wic.vmdk',
  169. 'wic.qcow2', 'wic.vdi', 'iso')
  170. self.network_device = "-device e1000,netdev=net0,mac=@MAC@"
  171. # Use different mac section for tap and slirp to avoid
  172. # conflicts, e.g., when one is running with tap, the other is
  173. # running with slirp.
  174. # The last section is dynamic, which is for avoiding conflicts,
  175. # when multiple qemus are running, e.g., when multiple tap or
  176. # slirp qemus are running.
  177. self.mac_tap = "52:54:00:12:34:"
  178. self.mac_slirp = "52:54:00:12:35:"
  179. # pid of the actual qemu process
  180. self.qemupid = None
  181. # avoid cleanup twice
  182. self.cleaned = False
  183. def acquire_lock(self, error=True):
  184. logger.debug("Acquiring lockfile %s..." % self.lock)
  185. try:
  186. self.lock_descriptor = open(self.lock, 'w')
  187. fcntl.flock(self.lock_descriptor, fcntl.LOCK_EX|fcntl.LOCK_NB)
  188. except Exception as e:
  189. msg = "Acquiring lockfile %s failed: %s" % (self.lock, e)
  190. if error:
  191. logger.error(msg)
  192. else:
  193. logger.info(msg)
  194. if self.lock_descriptor:
  195. self.lock_descriptor.close()
  196. self.lock_descriptor = None
  197. return False
  198. return True
  199. def release_lock(self):
  200. if self.lock_descriptor:
  201. logger.debug("Releasing lockfile for tap device '%s'" % self.tap)
  202. fcntl.flock(self.lock_descriptor, fcntl.LOCK_UN)
  203. self.lock_descriptor.close()
  204. os.remove(self.lock)
  205. self.lock_descriptor = None
  206. def get(self, key):
  207. if key in self.d:
  208. return self.d.get(key)
  209. elif os.getenv(key):
  210. return os.getenv(key)
  211. else:
  212. return ''
  213. def set(self, key, value):
  214. self.d[key] = value
  215. def is_deploy_dir_image(self, p):
  216. if os.path.isdir(p):
  217. if not re.search('.qemuboot.conf$', '\n'.join(os.listdir(p)), re.M):
  218. logger.debug("Can't find required *.qemuboot.conf in %s" % p)
  219. return False
  220. if not any(map(lambda name: '-image-' in name, os.listdir(p))):
  221. logger.debug("Can't find *-image-* in %s" % p)
  222. return False
  223. return True
  224. else:
  225. return False
  226. def check_arg_fstype(self, fst):
  227. """Check and set FSTYPE"""
  228. if fst not in self.fstypes + self.vmtypes:
  229. logger.warning("Maybe unsupported FSTYPE: %s" % fst)
  230. if not self.fstype or self.fstype == fst:
  231. if fst == 'ramfs':
  232. fst = 'cpio.gz'
  233. if fst in ('tar.bz2', 'tar.gz'):
  234. fst = 'nfs'
  235. self.fstype = fst
  236. else:
  237. raise RunQemuError("Conflicting: FSTYPE %s and %s" % (self.fstype, fst))
  238. def set_machine_deploy_dir(self, machine, deploy_dir_image):
  239. """Set MACHINE and DEPLOY_DIR_IMAGE"""
  240. logger.debug('MACHINE: %s' % machine)
  241. self.set("MACHINE", machine)
  242. logger.debug('DEPLOY_DIR_IMAGE: %s' % deploy_dir_image)
  243. self.set("DEPLOY_DIR_IMAGE", deploy_dir_image)
  244. def check_arg_nfs(self, p):
  245. if os.path.isdir(p):
  246. self.rootfs = p
  247. else:
  248. m = re.match('(.*):(.*)', p)
  249. self.nfs_server = m.group(1)
  250. self.rootfs = m.group(2)
  251. self.check_arg_fstype('nfs')
  252. def check_arg_path(self, p):
  253. """
  254. - Check whether it is <image>.qemuboot.conf or contains <image>.qemuboot.conf
  255. - Check whether is a kernel file
  256. - Check whether is a image file
  257. - Check whether it is a nfs dir
  258. - Check whether it is a OVMF flash file
  259. """
  260. if p.endswith('.qemuboot.conf'):
  261. self.qemuboot = p
  262. self.qbconfload = True
  263. elif re.search('\.bin$', p) or re.search('bzImage', p) or \
  264. re.search('zImage', p) or re.search('vmlinux', p) or \
  265. re.search('fitImage', p) or re.search('uImage', p):
  266. self.kernel = p
  267. elif os.path.exists(p) and (not os.path.isdir(p)) and '-image-' in os.path.basename(p):
  268. self.rootfs = p
  269. # Check filename against self.fstypes can hanlde <file>.cpio.gz,
  270. # otherwise, its type would be "gz", which is incorrect.
  271. fst = ""
  272. for t in self.fstypes:
  273. if p.endswith(t):
  274. fst = t
  275. break
  276. if not fst:
  277. m = re.search('.*\.(.*)$', self.rootfs)
  278. if m:
  279. fst = m.group(1)
  280. if fst:
  281. self.check_arg_fstype(fst)
  282. qb = re.sub('\.' + fst + "$", '', self.rootfs)
  283. qb = '%s%s' % (re.sub('\.rootfs$', '', qb), '.qemuboot.conf')
  284. if os.path.exists(qb):
  285. self.qemuboot = qb
  286. self.qbconfload = True
  287. else:
  288. logger.warning("%s doesn't exist" % qb)
  289. else:
  290. raise RunQemuError("Can't find FSTYPE from: %s" % p)
  291. elif os.path.isdir(p) or re.search(':', p) and re.search('/', p):
  292. if self.is_deploy_dir_image(p):
  293. logger.debug('DEPLOY_DIR_IMAGE: %s' % p)
  294. self.set("DEPLOY_DIR_IMAGE", p)
  295. else:
  296. logger.debug("Assuming %s is an nfs rootfs" % p)
  297. self.check_arg_nfs(p)
  298. elif os.path.basename(p).startswith('ovmf'):
  299. self.ovmf_bios.append(p)
  300. else:
  301. raise RunQemuError("Unknown path arg %s" % p)
  302. def check_arg_machine(self, arg):
  303. """Check whether it is a machine"""
  304. if self.get('MACHINE') == arg:
  305. return
  306. elif self.get('MACHINE') and self.get('MACHINE') != arg:
  307. raise RunQemuError("Maybe conflicted MACHINE: %s vs %s" % (self.get('MACHINE'), arg))
  308. elif re.search('/', arg):
  309. raise RunQemuError("Unknown arg: %s" % arg)
  310. logger.debug('Assuming MACHINE = %s' % arg)
  311. # if we're running under testimage, or similarly as a child
  312. # of an existing bitbake invocation, we can't invoke bitbake
  313. # to validate the MACHINE setting and must assume it's correct...
  314. # FIXME: testimage.bbclass exports these two variables into env,
  315. # are there other scenarios in which we need to support being
  316. # invoked by bitbake?
  317. deploy = self.get('DEPLOY_DIR_IMAGE')
  318. bbchild = deploy and self.get('OE_TMPDIR')
  319. if bbchild:
  320. self.set_machine_deploy_dir(arg, deploy)
  321. return
  322. # also check whether we're running under a sourced toolchain
  323. # environment file
  324. if self.get('OECORE_NATIVE_SYSROOT'):
  325. self.set("MACHINE", arg)
  326. return
  327. cmd = 'MACHINE=%s bitbake -e' % arg
  328. logger.info('Running %s...' % cmd)
  329. self.bitbake_e = subprocess.check_output(cmd, shell=True).decode('utf-8')
  330. # bitbake -e doesn't report invalid MACHINE as an error, so
  331. # let's check DEPLOY_DIR_IMAGE to make sure that it is a valid
  332. # MACHINE.
  333. s = re.search('^DEPLOY_DIR_IMAGE="(.*)"', self.bitbake_e, re.M)
  334. if s:
  335. deploy_dir_image = s.group(1)
  336. else:
  337. raise RunQemuError("bitbake -e %s" % self.bitbake_e)
  338. if self.is_deploy_dir_image(deploy_dir_image):
  339. self.set_machine_deploy_dir(arg, deploy_dir_image)
  340. else:
  341. logger.error("%s not a directory valid DEPLOY_DIR_IMAGE" % deploy_dir_image)
  342. self.set("MACHINE", arg)
  343. def check_args(self):
  344. for debug in ("-d", "--debug"):
  345. if debug in sys.argv:
  346. logger.setLevel(logging.DEBUG)
  347. sys.argv.remove(debug)
  348. for quiet in ("-q", "--quiet"):
  349. if quiet in sys.argv:
  350. logger.setLevel(logging.ERROR)
  351. sys.argv.remove(quiet)
  352. unknown_arg = ""
  353. for arg in sys.argv[1:]:
  354. if arg in self.fstypes + self.vmtypes:
  355. self.check_arg_fstype(arg)
  356. elif arg == 'nographic':
  357. self.qemu_opt_script += ' -nographic'
  358. self.kernel_cmdline_script += ' console=ttyS0'
  359. elif arg == 'sdl':
  360. self.qemu_opt_script += ' -display sdl'
  361. elif arg == 'gtk-gl':
  362. self.qemu_opt_script += ' -vga virtio -display gtk,gl=on'
  363. elif arg == 'gtk-gl-es':
  364. self.qemu_opt_script += ' -vga virtio -display gtk,gl=es'
  365. elif arg == 'egl-headless':
  366. self.qemu_opt_script += ' -vga virtio -display egl-headless'
  367. # As runqemu can be run within bitbake (when using testimage, for example),
  368. # we need to ensure that we run host pkg-config, and that it does not
  369. # get mis-directed to native build paths set by bitbake.
  370. try:
  371. del os.environ['PKG_CONFIG_PATH']
  372. del os.environ['PKG_CONFIG_DIR']
  373. del os.environ['PKG_CONFIG_LIBDIR']
  374. except KeyError:
  375. pass
  376. try:
  377. dripath = subprocess.check_output("PATH=/bin:/usr/bin:$PATH pkg-config --variable=dridriverdir dri", shell=True)
  378. except subprocess.CalledProcessError as e:
  379. raise RunQemuError("Could not determine the path to dri drivers on the host via pkg-config.\nPlease install Mesa development files (particularly, dri.pc) on the host machine.")
  380. os.environ['LIBGL_DRIVERS_PATH'] = dripath.decode('utf-8').strip()
  381. elif arg == 'serial':
  382. self.kernel_cmdline_script += ' console=ttyS0'
  383. self.serialconsole = True
  384. elif arg == "serialstdio":
  385. self.kernel_cmdline_script += ' console=ttyS0'
  386. self.serialstdio = True
  387. elif arg == 'audio':
  388. logger.info("Enabling audio in qemu")
  389. logger.info("Please install sound drivers in linux host")
  390. self.audio_enabled = True
  391. elif arg == 'kvm':
  392. self.kvm_enabled = True
  393. elif arg == 'kvm-vhost':
  394. self.vhost_enabled = True
  395. elif arg == 'slirp':
  396. self.slirp_enabled = True
  397. elif arg == 'snapshot':
  398. self.snapshot = True
  399. elif arg == 'publicvnc':
  400. self.qemu_opt_script += ' -vnc :0'
  401. elif arg.startswith('tcpserial='):
  402. self.tcpserial_portnum = '%s' % arg[len('tcpserial='):]
  403. elif arg.startswith('biosdir='):
  404. self.custombiosdir = arg[len('biosdir='):]
  405. elif arg.startswith('biosfilename='):
  406. self.qemu_opt_script += ' -bios %s' % arg[len('biosfilename='):]
  407. elif arg.startswith('qemuparams='):
  408. self.qemuparams = ' %s' % arg[len('qemuparams='):]
  409. elif arg.startswith('bootparams='):
  410. self.bootparams = arg[len('bootparams='):]
  411. elif os.path.exists(arg) or (re.search(':', arg) and re.search('/', arg)):
  412. self.check_arg_path(os.path.abspath(arg))
  413. elif re.search(r'-image-|-image$', arg):
  414. # Lazy rootfs
  415. self.rootfs = arg
  416. elif arg.startswith('ovmf'):
  417. self.ovmf_bios.append(arg)
  418. else:
  419. # At last, assume it is the MACHINE
  420. if (not unknown_arg) or unknown_arg == arg:
  421. unknown_arg = arg
  422. else:
  423. raise RunQemuError("Can't handle two unknown args: %s %s\n"
  424. "Try 'runqemu help' on how to use it" % \
  425. (unknown_arg, arg))
  426. # Check to make sure it is a valid machine
  427. if unknown_arg and self.get('MACHINE') != unknown_arg:
  428. if self.get('DEPLOY_DIR_IMAGE'):
  429. machine = os.path.basename(self.get('DEPLOY_DIR_IMAGE'))
  430. if unknown_arg == machine:
  431. self.set("MACHINE", machine)
  432. self.check_arg_machine(unknown_arg)
  433. if not (self.get('DEPLOY_DIR_IMAGE') or self.qbconfload):
  434. self.load_bitbake_env()
  435. s = re.search('^DEPLOY_DIR_IMAGE="(.*)"', self.bitbake_e, re.M)
  436. if s:
  437. self.set("DEPLOY_DIR_IMAGE", s.group(1))
  438. def check_kvm(self):
  439. """Check kvm and kvm-host"""
  440. if not (self.kvm_enabled or self.vhost_enabled):
  441. self.qemu_opt_script += ' %s %s' % (self.get('QB_MACHINE'), self.get('QB_CPU'))
  442. return
  443. if not self.get('QB_CPU_KVM'):
  444. raise RunQemuError("QB_CPU_KVM is NULL, this board doesn't support kvm")
  445. self.qemu_opt_script += ' %s %s' % (self.get('QB_MACHINE'), self.get('QB_CPU_KVM'))
  446. yocto_kvm_wiki = "https://wiki.yoctoproject.org/wiki/How_to_enable_KVM_for_Poky_qemu"
  447. yocto_paravirt_kvm_wiki = "https://wiki.yoctoproject.org/wiki/Running_an_x86_Yocto_Linux_image_under_QEMU_KVM"
  448. dev_kvm = '/dev/kvm'
  449. dev_vhost = '/dev/vhost-net'
  450. if self.qemu_system.endswith(('i386', 'x86_64')):
  451. with open('/proc/cpuinfo', 'r') as f:
  452. kvm_cap = re.search('vmx|svm', "".join(f.readlines()))
  453. if not kvm_cap:
  454. logger.error("You are trying to enable KVM on a cpu without VT support.")
  455. logger.error("Remove kvm from the command-line, or refer:")
  456. raise RunQemuError(yocto_kvm_wiki)
  457. if not os.path.exists(dev_kvm):
  458. logger.error("Missing KVM device. Have you inserted kvm modules?")
  459. logger.error("For further help see:")
  460. raise RunQemuError(yocto_kvm_wiki)
  461. if os.access(dev_kvm, os.W_OK|os.R_OK):
  462. self.qemu_opt_script += ' -enable-kvm'
  463. if self.get('MACHINE') == "qemux86":
  464. # Workaround for broken APIC window on pre 4.15 host kernels which causes boot hangs
  465. # See YOCTO #12301
  466. # On 64 bit we use x2apic
  467. self.kernel_cmdline_script += " clocksource=kvm-clock hpet=disable noapic nolapic"
  468. else:
  469. logger.error("You have no read or write permission on /dev/kvm.")
  470. logger.error("Please change the ownership of this file as described at:")
  471. raise RunQemuError(yocto_kvm_wiki)
  472. if self.vhost_enabled:
  473. if not os.path.exists(dev_vhost):
  474. logger.error("Missing virtio net device. Have you inserted vhost-net module?")
  475. logger.error("For further help see:")
  476. raise RunQemuError(yocto_paravirt_kvm_wiki)
  477. if not os.access(dev_kvm, os.W_OK|os.R_OK):
  478. logger.error("You have no read or write permission on /dev/vhost-net.")
  479. logger.error("Please change the ownership of this file as described at:")
  480. raise RunQemuError(yocto_kvm_wiki)
  481. def check_fstype(self):
  482. """Check and setup FSTYPE"""
  483. if not self.fstype:
  484. fstype = self.get('QB_DEFAULT_FSTYPE')
  485. if fstype:
  486. self.fstype = fstype
  487. else:
  488. raise RunQemuError("FSTYPE is NULL!")
  489. def check_rootfs(self):
  490. """Check and set rootfs"""
  491. if self.fstype == "none":
  492. return
  493. if self.get('ROOTFS'):
  494. if not self.rootfs:
  495. self.rootfs = self.get('ROOTFS')
  496. elif self.get('ROOTFS') != self.rootfs:
  497. raise RunQemuError("Maybe conflicted ROOTFS: %s vs %s" % (self.get('ROOTFS'), self.rootfs))
  498. if self.fstype == 'nfs':
  499. return
  500. if self.rootfs and not os.path.exists(self.rootfs):
  501. # Lazy rootfs
  502. self.rootfs = "%s/%s-%s.%s" % (self.get('DEPLOY_DIR_IMAGE'),
  503. self.rootfs, self.get('MACHINE'),
  504. self.fstype)
  505. elif not self.rootfs:
  506. cmd_name = '%s/%s*.%s' % (self.get('DEPLOY_DIR_IMAGE'), self.get('IMAGE_NAME'), self.fstype)
  507. cmd_link = '%s/%s*.%s' % (self.get('DEPLOY_DIR_IMAGE'), self.get('IMAGE_LINK_NAME'), self.fstype)
  508. cmds = (cmd_name, cmd_link)
  509. self.rootfs = get_first_file(cmds)
  510. if not self.rootfs:
  511. raise RunQemuError("Failed to find rootfs: %s or %s" % cmds)
  512. if not os.path.exists(self.rootfs):
  513. raise RunQemuError("Can't find rootfs: %s" % self.rootfs)
  514. def check_ovmf(self):
  515. """Check and set full path for OVMF firmware and variable file(s)."""
  516. for index, ovmf in enumerate(self.ovmf_bios):
  517. if os.path.exists(ovmf):
  518. continue
  519. for suffix in ('qcow2', 'bin'):
  520. path = '%s/%s.%s' % (self.get('DEPLOY_DIR_IMAGE'), ovmf, suffix)
  521. if os.path.exists(path):
  522. self.ovmf_bios[index] = path
  523. break
  524. else:
  525. raise RunQemuError("Can't find OVMF firmware: %s" % ovmf)
  526. def check_kernel(self):
  527. """Check and set kernel"""
  528. # The vm image doesn't need a kernel
  529. if self.fstype in self.vmtypes:
  530. return
  531. # See if the user supplied a KERNEL option
  532. if self.get('KERNEL'):
  533. self.kernel = self.get('KERNEL')
  534. # QB_DEFAULT_KERNEL is always a full file path
  535. kernel_name = os.path.basename(self.get('QB_DEFAULT_KERNEL'))
  536. # The user didn't want a kernel to be loaded
  537. if kernel_name == "none" and not self.kernel:
  538. return
  539. deploy_dir_image = self.get('DEPLOY_DIR_IMAGE')
  540. if not self.kernel:
  541. kernel_match_name = "%s/%s" % (deploy_dir_image, kernel_name)
  542. kernel_match_link = "%s/%s" % (deploy_dir_image, self.get('KERNEL_IMAGETYPE'))
  543. kernel_startswith = "%s/%s*" % (deploy_dir_image, self.get('KERNEL_IMAGETYPE'))
  544. cmds = (kernel_match_name, kernel_match_link, kernel_startswith)
  545. self.kernel = get_first_file(cmds)
  546. if not self.kernel:
  547. raise RunQemuError('KERNEL not found: %s, %s or %s' % cmds)
  548. if not os.path.exists(self.kernel):
  549. raise RunQemuError("KERNEL %s not found" % self.kernel)
  550. def check_dtb(self):
  551. """Check and set dtb"""
  552. # Did the user specify a device tree?
  553. if self.get('DEVICE_TREE'):
  554. self.dtb = self.get('DEVICE_TREE')
  555. if not os.path.exists(self.dtb):
  556. raise RunQemuError('Specified DTB not found: %s' % self.dtb)
  557. return
  558. dtb = self.get('QB_DTB')
  559. if dtb:
  560. deploy_dir_image = self.get('DEPLOY_DIR_IMAGE')
  561. cmd_match = "%s/%s" % (deploy_dir_image, dtb)
  562. cmd_startswith = "%s/%s*" % (deploy_dir_image, dtb)
  563. cmd_wild = "%s/*.dtb" % deploy_dir_image
  564. cmds = (cmd_match, cmd_startswith, cmd_wild)
  565. self.dtb = get_first_file(cmds)
  566. if not os.path.exists(self.dtb):
  567. raise RunQemuError('DTB not found: %s, %s or %s' % cmds)
  568. def check_biosdir(self):
  569. """Check custombiosdir"""
  570. if not self.custombiosdir:
  571. return
  572. biosdir = ""
  573. biosdir_native = "%s/%s" % (self.get('STAGING_DIR_NATIVE'), self.custombiosdir)
  574. biosdir_host = "%s/%s" % (self.get('STAGING_DIR_HOST'), self.custombiosdir)
  575. for i in (self.custombiosdir, biosdir_native, biosdir_host):
  576. if os.path.isdir(i):
  577. biosdir = i
  578. break
  579. if biosdir:
  580. logger.debug("Assuming biosdir is: %s" % biosdir)
  581. self.qemu_opt_script += ' -L %s' % biosdir
  582. else:
  583. logger.error("Custom BIOS directory not found. Tried: %s, %s, and %s" % (self.custombiosdir, biosdir_native, biosdir_host))
  584. raise RunQemuError("Invalid custombiosdir: %s" % self.custombiosdir)
  585. def check_mem(self):
  586. """
  587. Both qemu and kernel needs memory settings, so check QB_MEM and set it
  588. for both.
  589. """
  590. s = re.search('-m +([0-9]+)', self.qemuparams)
  591. if s:
  592. self.set('QB_MEM', '-m %s' % s.group(1))
  593. elif not self.get('QB_MEM'):
  594. logger.info('QB_MEM is not set, use 512M by default')
  595. self.set('QB_MEM', '-m 512')
  596. # Check and remove M or m suffix
  597. qb_mem = self.get('QB_MEM')
  598. if qb_mem.endswith('M') or qb_mem.endswith('m'):
  599. qb_mem = qb_mem[:-1]
  600. # Add -m prefix it not present
  601. if not qb_mem.startswith('-m'):
  602. qb_mem = '-m %s' % qb_mem
  603. self.set('QB_MEM', qb_mem)
  604. mach = self.get('MACHINE')
  605. if not mach.startswith('qemumips'):
  606. self.kernel_cmdline_script += ' mem=%s' % self.get('QB_MEM').replace('-m','').strip() + 'M'
  607. self.qemu_opt_script += ' %s' % self.get('QB_MEM')
  608. def check_tcpserial(self):
  609. if self.tcpserial_portnum:
  610. ports = self.tcpserial_portnum.split(':')
  611. port = ports[0]
  612. if self.get('QB_TCPSERIAL_OPT'):
  613. self.qemu_opt_script += ' ' + self.get('QB_TCPSERIAL_OPT').replace('@PORT@', port)
  614. else:
  615. self.qemu_opt_script += ' -serial tcp:127.0.0.1:%s' % port
  616. if len(ports) > 1:
  617. for port in ports[1:]:
  618. self.qemu_opt_script += ' -serial tcp:127.0.0.1:%s' % port
  619. def check_and_set(self):
  620. """Check configs sanity and set when needed"""
  621. self.validate_paths()
  622. if not self.slirp_enabled:
  623. check_tun()
  624. # Check audio
  625. if self.audio_enabled:
  626. if not self.get('QB_AUDIO_DRV'):
  627. raise RunQemuError("QB_AUDIO_DRV is NULL, this board doesn't support audio")
  628. if not self.get('QB_AUDIO_OPT'):
  629. logger.warning('QB_AUDIO_OPT is NULL, you may need define it to make audio work')
  630. else:
  631. self.qemu_opt_script += ' %s' % self.get('QB_AUDIO_OPT')
  632. os.putenv('QEMU_AUDIO_DRV', self.get('QB_AUDIO_DRV'))
  633. else:
  634. os.putenv('QEMU_AUDIO_DRV', 'none')
  635. self.check_qemu_system()
  636. self.check_kvm()
  637. self.check_fstype()
  638. self.check_rootfs()
  639. self.check_ovmf()
  640. self.check_kernel()
  641. self.check_dtb()
  642. self.check_biosdir()
  643. self.check_mem()
  644. self.check_tcpserial()
  645. def read_qemuboot(self):
  646. if not self.qemuboot:
  647. if self.get('DEPLOY_DIR_IMAGE'):
  648. deploy_dir_image = self.get('DEPLOY_DIR_IMAGE')
  649. else:
  650. logger.warning("Can't find qemuboot conf file, DEPLOY_DIR_IMAGE is NULL!")
  651. return
  652. if self.rootfs and not os.path.exists(self.rootfs):
  653. # Lazy rootfs
  654. machine = self.get('MACHINE')
  655. if not machine:
  656. machine = os.path.basename(deploy_dir_image)
  657. self.qemuboot = "%s/%s-%s.qemuboot.conf" % (deploy_dir_image,
  658. self.rootfs, machine)
  659. else:
  660. cmd = 'ls -t %s/*.qemuboot.conf' % deploy_dir_image
  661. logger.debug('Running %s...' % cmd)
  662. try:
  663. qbs = subprocess.check_output(cmd, shell=True).decode('utf-8')
  664. except subprocess.CalledProcessError as err:
  665. raise RunQemuError(err)
  666. if qbs:
  667. for qb in qbs.split():
  668. # Don't use initramfs when other choices unless fstype is ramfs
  669. if '-initramfs-' in os.path.basename(qb) and self.fstype != 'cpio.gz':
  670. continue
  671. self.qemuboot = qb
  672. break
  673. if not self.qemuboot:
  674. # Use the first one when no choice
  675. self.qemuboot = qbs.split()[0]
  676. self.qbconfload = True
  677. if not self.qemuboot:
  678. # If we haven't found a .qemuboot.conf at this point it probably
  679. # doesn't exist, continue without
  680. return
  681. if not os.path.exists(self.qemuboot):
  682. raise RunQemuError("Failed to find %s (wrong image name or BSP does not support running under qemu?)." % self.qemuboot)
  683. logger.debug('CONFFILE: %s' % self.qemuboot)
  684. cf = configparser.ConfigParser()
  685. cf.read(self.qemuboot)
  686. for k, v in cf.items('config_bsp'):
  687. k_upper = k.upper()
  688. if v.startswith("../"):
  689. v = os.path.abspath(os.path.dirname(self.qemuboot) + "/" + v)
  690. elif v == ".":
  691. v = os.path.dirname(self.qemuboot)
  692. self.set(k_upper, v)
  693. def validate_paths(self):
  694. """Ensure all relevant path variables are set"""
  695. # When we're started with a *.qemuboot.conf arg assume that image
  696. # artefacts are relative to that file, rather than in whatever
  697. # directory DEPLOY_DIR_IMAGE in the conf file points to.
  698. if self.qbconfload:
  699. imgdir = os.path.realpath(os.path.dirname(self.qemuboot))
  700. if imgdir != os.path.realpath(self.get('DEPLOY_DIR_IMAGE')):
  701. logger.info('Setting DEPLOY_DIR_IMAGE to folder containing %s (%s)' % (self.qemuboot, imgdir))
  702. self.set('DEPLOY_DIR_IMAGE', imgdir)
  703. # If the STAGING_*_NATIVE directories from the config file don't exist
  704. # and we're in a sourced OE build directory try to extract the paths
  705. # from `bitbake -e`
  706. havenative = os.path.exists(self.get('STAGING_DIR_NATIVE')) and \
  707. os.path.exists(self.get('STAGING_BINDIR_NATIVE'))
  708. if not havenative:
  709. if not self.bitbake_e:
  710. self.load_bitbake_env()
  711. if self.bitbake_e:
  712. native_vars = ['STAGING_DIR_NATIVE']
  713. for nv in native_vars:
  714. s = re.search('^%s="(.*)"' % nv, self.bitbake_e, re.M)
  715. if s and s.group(1) != self.get(nv):
  716. logger.info('Overriding conf file setting of %s to %s from Bitbake environment' % (nv, s.group(1)))
  717. self.set(nv, s.group(1))
  718. else:
  719. # when we're invoked from a running bitbake instance we won't
  720. # be able to call `bitbake -e`, then try:
  721. # - get OE_TMPDIR from environment and guess paths based on it
  722. # - get OECORE_NATIVE_SYSROOT from environment (for sdk)
  723. tmpdir = self.get('OE_TMPDIR')
  724. oecore_native_sysroot = self.get('OECORE_NATIVE_SYSROOT')
  725. if tmpdir:
  726. logger.info('Setting STAGING_DIR_NATIVE and STAGING_BINDIR_NATIVE relative to OE_TMPDIR (%s)' % tmpdir)
  727. hostos, _, _, _, machine = os.uname()
  728. buildsys = '%s-%s' % (machine, hostos.lower())
  729. staging_dir_native = '%s/sysroots/%s' % (tmpdir, buildsys)
  730. self.set('STAGING_DIR_NATIVE', staging_dir_native)
  731. elif oecore_native_sysroot:
  732. logger.info('Setting STAGING_DIR_NATIVE to OECORE_NATIVE_SYSROOT (%s)' % oecore_native_sysroot)
  733. self.set('STAGING_DIR_NATIVE', oecore_native_sysroot)
  734. if self.get('STAGING_DIR_NATIVE'):
  735. # we have to assume that STAGING_BINDIR_NATIVE is at usr/bin
  736. staging_bindir_native = '%s/usr/bin' % self.get('STAGING_DIR_NATIVE')
  737. logger.info('Setting STAGING_BINDIR_NATIVE to %s' % staging_bindir_native)
  738. self.set('STAGING_BINDIR_NATIVE', '%s/usr/bin' % self.get('STAGING_DIR_NATIVE'))
  739. def print_config(self):
  740. logger.info('Continuing with the following parameters:\n')
  741. if not self.fstype in self.vmtypes:
  742. print('KERNEL: [%s]' % self.kernel)
  743. if self.dtb:
  744. print('DTB: [%s]' % self.dtb)
  745. print('MACHINE: [%s]' % self.get('MACHINE'))
  746. print('FSTYPE: [%s]' % self.fstype)
  747. if self.fstype == 'nfs':
  748. print('NFS_DIR: [%s]' % self.rootfs)
  749. else:
  750. print('ROOTFS: [%s]' % self.rootfs)
  751. if self.ovmf_bios:
  752. print('OVMF: %s' % self.ovmf_bios)
  753. print('CONFFILE: [%s]' % self.qemuboot)
  754. print('')
  755. def setup_nfs(self):
  756. if not self.nfs_server:
  757. if self.slirp_enabled:
  758. self.nfs_server = '10.0.2.2'
  759. else:
  760. self.nfs_server = '192.168.7.1'
  761. # Figure out a new nfs_instance to allow multiple qemus running.
  762. ps = subprocess.check_output(("ps", "auxww")).decode('utf-8')
  763. pattern = '/bin/unfsd .* -i .*\.pid -e .*/exports([0-9]+) '
  764. all_instances = re.findall(pattern, ps, re.M)
  765. if all_instances:
  766. all_instances.sort(key=int)
  767. self.nfs_instance = int(all_instances.pop()) + 1
  768. nfsd_port = 3049 + 2 * self.nfs_instance
  769. mountd_port = 3048 + 2 * self.nfs_instance
  770. # Export vars for runqemu-export-rootfs
  771. export_dict = {
  772. 'NFS_INSTANCE': self.nfs_instance,
  773. 'NFSD_PORT': nfsd_port,
  774. 'MOUNTD_PORT': mountd_port,
  775. }
  776. for k, v in export_dict.items():
  777. # Use '%s' since they are integers
  778. os.putenv(k, '%s' % v)
  779. self.unfs_opts="nfsvers=3,port=%s,udp,mountport=%s" % (nfsd_port, mountd_port)
  780. # Extract .tar.bz2 or .tar.bz if no nfs dir
  781. if not (self.rootfs and os.path.isdir(self.rootfs)):
  782. src_prefix = '%s/%s' % (self.get('DEPLOY_DIR_IMAGE'), self.get('IMAGE_LINK_NAME'))
  783. dest = "%s-nfsroot" % src_prefix
  784. if os.path.exists('%s.pseudo_state' % dest):
  785. logger.info('Use %s as NFS_DIR' % dest)
  786. self.rootfs = dest
  787. else:
  788. src = ""
  789. src1 = '%s.tar.bz2' % src_prefix
  790. src2 = '%s.tar.gz' % src_prefix
  791. if os.path.exists(src1):
  792. src = src1
  793. elif os.path.exists(src2):
  794. src = src2
  795. if not src:
  796. raise RunQemuError("No NFS_DIR is set, and can't find %s or %s to extract" % (src1, src2))
  797. logger.info('NFS_DIR not found, extracting %s to %s' % (src, dest))
  798. cmd = ('runqemu-extract-sdk', src, dest)
  799. logger.info('Running %s...' % str(cmd))
  800. if subprocess.call(cmd) != 0:
  801. raise RunQemuError('Failed to run %s' % cmd)
  802. self.clean_nfs_dir = True
  803. self.rootfs = dest
  804. # Start the userspace NFS server
  805. cmd = ('runqemu-export-rootfs', 'start', self.rootfs)
  806. logger.info('Running %s...' % str(cmd))
  807. if subprocess.call(cmd) != 0:
  808. raise RunQemuError('Failed to run %s' % cmd)
  809. self.nfs_running = True
  810. def setup_slirp(self):
  811. """Setup user networking"""
  812. if self.fstype == 'nfs':
  813. self.setup_nfs()
  814. self.kernel_cmdline_script += ' ip=dhcp'
  815. # Port mapping
  816. hostfwd = ",hostfwd=tcp::2222-:22,hostfwd=tcp::2323-:23"
  817. qb_slirp_opt_default = "-netdev user,id=net0%s,tftp=%s" % (hostfwd, self.get('DEPLOY_DIR_IMAGE'))
  818. qb_slirp_opt = self.get('QB_SLIRP_OPT') or qb_slirp_opt_default
  819. # Figure out the port
  820. ports = re.findall('hostfwd=[^-]*:([0-9]+)-[^,-]*', qb_slirp_opt)
  821. ports = [int(i) for i in ports]
  822. mac = 2
  823. # Find a free port to avoid conflicts
  824. for p in ports[:]:
  825. p_new = p
  826. while not check_free_port('localhost', p_new):
  827. p_new += 1
  828. mac += 1
  829. while p_new in ports:
  830. p_new += 1
  831. mac += 1
  832. if p != p_new:
  833. ports.append(p_new)
  834. qb_slirp_opt = re.sub(':%s-' % p, ':%s-' % p_new, qb_slirp_opt)
  835. logger.info("Port forward changed: %s -> %s" % (p, p_new))
  836. mac = "%s%02x" % (self.mac_slirp, mac)
  837. self.set('NETWORK_CMD', '%s %s' % (self.network_device.replace('@MAC@', mac), qb_slirp_opt))
  838. # Print out port foward
  839. hostfwd = re.findall('(hostfwd=[^,]*)', qb_slirp_opt)
  840. if hostfwd:
  841. logger.info('Port forward: %s' % ' '.join(hostfwd))
  842. def setup_tap(self):
  843. """Setup tap"""
  844. # This file is created when runqemu-gen-tapdevs creates a bank of tap
  845. # devices, indicating that the user should not bring up new ones using
  846. # sudo.
  847. nosudo_flag = '/etc/runqemu-nosudo'
  848. self.qemuifup = shutil.which('runqemu-ifup')
  849. self.qemuifdown = shutil.which('runqemu-ifdown')
  850. ip = shutil.which('ip')
  851. lockdir = "/tmp/qemu-tap-locks"
  852. if not (self.qemuifup and self.qemuifdown and ip):
  853. logger.error("runqemu-ifup: %s" % self.qemuifup)
  854. logger.error("runqemu-ifdown: %s" % self.qemuifdown)
  855. logger.error("ip: %s" % ip)
  856. raise OEPathError("runqemu-ifup, runqemu-ifdown or ip not found")
  857. if not os.path.exists(lockdir):
  858. # There might be a race issue when multi runqemu processess are
  859. # running at the same time.
  860. try:
  861. os.mkdir(lockdir)
  862. os.chmod(lockdir, 0o777)
  863. except FileExistsError:
  864. pass
  865. cmd = (ip, 'link')
  866. logger.debug('Running %s...' % str(cmd))
  867. ip_link = subprocess.check_output(cmd).decode('utf-8')
  868. # Matches line like: 6: tap0: <foo>
  869. possibles = re.findall('^[0-9]+: +(tap[0-9]+): <.*', ip_link, re.M)
  870. tap = ""
  871. for p in possibles:
  872. lockfile = os.path.join(lockdir, p)
  873. if os.path.exists('%s.skip' % lockfile):
  874. logger.info('Found %s.skip, skipping %s' % (lockfile, p))
  875. continue
  876. self.lock = lockfile + '.lock'
  877. if self.acquire_lock(error=False):
  878. tap = p
  879. logger.info("Using preconfigured tap device %s" % tap)
  880. logger.info("If this is not intended, touch %s.skip to make runqemu skip %s." %(lockfile, tap))
  881. break
  882. if not tap:
  883. if os.path.exists(nosudo_flag):
  884. logger.error("Error: There are no available tap devices to use for networking,")
  885. logger.error("and I see %s exists, so I am not going to try creating" % nosudo_flag)
  886. raise RunQemuError("a new one with sudo.")
  887. gid = os.getgid()
  888. uid = os.getuid()
  889. logger.info("Setting up tap interface under sudo")
  890. cmd = ('sudo', self.qemuifup, str(uid), str(gid), self.bindir_native)
  891. tap = subprocess.check_output(cmd).decode('utf-8').strip()
  892. lockfile = os.path.join(lockdir, tap)
  893. self.lock = lockfile + '.lock'
  894. self.acquire_lock()
  895. self.cleantap = True
  896. logger.debug('Created tap: %s' % tap)
  897. if not tap:
  898. logger.error("Failed to setup tap device. Run runqemu-gen-tapdevs to manually create.")
  899. return 1
  900. self.tap = tap
  901. tapnum = int(tap[3:])
  902. gateway = tapnum * 2 + 1
  903. client = gateway + 1
  904. if self.fstype == 'nfs':
  905. self.setup_nfs()
  906. netconf = "192.168.7.%s::192.168.7.%s:255.255.255.0" % (client, gateway)
  907. logger.info("Network configuration: %s", netconf)
  908. self.kernel_cmdline_script += " ip=%s" % netconf
  909. mac = "%s%02x" % (self.mac_tap, client)
  910. qb_tap_opt = self.get('QB_TAP_OPT')
  911. if qb_tap_opt:
  912. qemu_tap_opt = qb_tap_opt.replace('@TAP@', tap)
  913. else:
  914. qemu_tap_opt = "-netdev tap,id=net0,ifname=%s,script=no,downscript=no" % (self.tap)
  915. if self.vhost_enabled:
  916. qemu_tap_opt += ',vhost=on'
  917. self.set('NETWORK_CMD', '%s %s' % (self.network_device.replace('@MAC@', mac), qemu_tap_opt))
  918. def setup_network(self):
  919. if self.get('QB_NET') == 'none':
  920. return
  921. if sys.stdin.isatty():
  922. self.saved_stty = subprocess.check_output(("stty", "-g")).decode('utf-8').strip()
  923. self.network_device = self.get('QB_NETWORK_DEVICE') or self.network_device
  924. if self.slirp_enabled:
  925. self.setup_slirp()
  926. else:
  927. self.setup_tap()
  928. def setup_rootfs(self):
  929. if self.get('QB_ROOTFS') == 'none':
  930. return
  931. if 'wic.' in self.fstype:
  932. self.fstype = self.fstype[4:]
  933. rootfs_format = self.fstype if self.fstype in ('vmdk', 'qcow2', 'vdi') else 'raw'
  934. qb_rootfs_opt = self.get('QB_ROOTFS_OPT')
  935. if qb_rootfs_opt:
  936. self.rootfs_options = qb_rootfs_opt.replace('@ROOTFS@', self.rootfs)
  937. else:
  938. self.rootfs_options = '-drive file=%s,if=virtio,format=%s' % (self.rootfs, rootfs_format)
  939. if self.fstype in ('cpio.gz', 'cpio'):
  940. self.kernel_cmdline = 'root=/dev/ram0 rw debugshell'
  941. self.rootfs_options = '-initrd %s' % self.rootfs
  942. else:
  943. vm_drive = ''
  944. if self.fstype in self.vmtypes:
  945. if self.fstype == 'iso':
  946. vm_drive = '-drive file=%s,if=virtio,media=cdrom' % self.rootfs
  947. elif self.get('QB_DRIVE_TYPE'):
  948. drive_type = self.get('QB_DRIVE_TYPE')
  949. if drive_type.startswith("/dev/sd"):
  950. logger.info('Using scsi drive')
  951. vm_drive = '-drive if=none,id=hd,file=%s,format=%s -device virtio-scsi-pci,id=scsi -device scsi-hd,drive=hd' \
  952. % (self.rootfs, rootfs_format)
  953. elif drive_type.startswith("/dev/hd"):
  954. logger.info('Using ide drive')
  955. vm_drive = "-drive file=%s,format=%s" % (self.rootfs, rootfs_format)
  956. else:
  957. # virtio might have been selected explicitly (just use it), or
  958. # is used as fallback (then warn about that).
  959. if not drive_type.startswith("/dev/vd"):
  960. logger.warning("Unknown QB_DRIVE_TYPE: %s" % drive_type)
  961. logger.warning("Failed to figure out drive type, consider define or fix QB_DRIVE_TYPE")
  962. logger.warning('Trying to use virtio block drive')
  963. vm_drive = '-drive if=virtio,file=%s,format=%s' % (self.rootfs, rootfs_format)
  964. # All branches above set vm_drive.
  965. self.rootfs_options = '%s -no-reboot' % vm_drive
  966. self.kernel_cmdline = 'root=%s rw highres=off' % (self.get('QB_KERNEL_ROOT'))
  967. if self.fstype == 'nfs':
  968. self.rootfs_options = ''
  969. k_root = '/dev/nfs nfsroot=%s:%s,%s' % (self.nfs_server, os.path.abspath(self.rootfs), self.unfs_opts)
  970. self.kernel_cmdline = 'root=%s rw highres=off' % k_root
  971. if self.fstype == 'none':
  972. self.rootfs_options = ''
  973. self.set('ROOTFS_OPTIONS', self.rootfs_options)
  974. def guess_qb_system(self):
  975. """attempt to determine the appropriate qemu-system binary"""
  976. mach = self.get('MACHINE')
  977. if not mach:
  978. search = '.*(qemux86-64|qemux86|qemuarm64|qemuarm|qemumips64|qemumips64el|qemumipsel|qemumips|qemuppc).*'
  979. if self.rootfs:
  980. match = re.match(search, self.rootfs)
  981. if match:
  982. mach = match.group(1)
  983. elif self.kernel:
  984. match = re.match(search, self.kernel)
  985. if match:
  986. mach = match.group(1)
  987. if not mach:
  988. return None
  989. if mach == 'qemuarm':
  990. qbsys = 'arm'
  991. elif mach == 'qemuarm64':
  992. qbsys = 'aarch64'
  993. elif mach == 'qemux86':
  994. qbsys = 'i386'
  995. elif mach == 'qemux86-64':
  996. qbsys = 'x86_64'
  997. elif mach == 'qemuppc':
  998. qbsys = 'ppc'
  999. elif mach == 'qemumips':
  1000. qbsys = 'mips'
  1001. elif mach == 'qemumips64':
  1002. qbsys = 'mips64'
  1003. elif mach == 'qemumipsel':
  1004. qbsys = 'mipsel'
  1005. elif mach == 'qemumips64el':
  1006. qbsys = 'mips64el'
  1007. elif mach == 'qemuriscv64':
  1008. qbsys = 'riscv64'
  1009. elif mach == 'qemuriscv32':
  1010. qbsys = 'riscv32'
  1011. else:
  1012. logger.error("Unable to determine QEMU PC System emulator for %s machine." % mach)
  1013. logger.error("As %s is not among valid QEMU machines such as," % mach)
  1014. logger.error("qemux86-64, qemux86, qemuarm64, qemuarm, qemumips64, qemumips64el, qemumipsel, qemumips, qemuppc")
  1015. raise RunQemuError("Set qb_system_name with suitable QEMU PC System emulator in .*qemuboot.conf.")
  1016. return 'qemu-system-%s' % qbsys
  1017. def check_qemu_system(self):
  1018. qemu_system = self.get('QB_SYSTEM_NAME')
  1019. if not qemu_system:
  1020. qemu_system = self.guess_qb_system()
  1021. if not qemu_system:
  1022. raise RunQemuError("Failed to boot, QB_SYSTEM_NAME is NULL!")
  1023. self.qemu_system = qemu_system
  1024. def setup_final(self):
  1025. qemu_bin = os.path.join(self.bindir_native, self.qemu_system)
  1026. # It is possible to have qemu-native in ASSUME_PROVIDED, and it won't
  1027. # find QEMU in sysroot, it needs to use host's qemu.
  1028. if not os.path.exists(qemu_bin):
  1029. logger.info("QEMU binary not found in %s, trying host's QEMU" % qemu_bin)
  1030. for path in (os.environ['PATH'] or '').split(':'):
  1031. qemu_bin_tmp = os.path.join(path, self.qemu_system)
  1032. logger.info("Trying: %s" % qemu_bin_tmp)
  1033. if os.path.exists(qemu_bin_tmp):
  1034. qemu_bin = qemu_bin_tmp
  1035. if not os.path.isabs(qemu_bin):
  1036. qemu_bin = os.path.abspath(qemu_bin)
  1037. logger.info("Using host's QEMU: %s" % qemu_bin)
  1038. break
  1039. if not os.access(qemu_bin, os.X_OK):
  1040. raise OEPathError("No QEMU binary '%s' could be found" % qemu_bin)
  1041. self.qemu_opt = "%s %s %s %s" % (qemu_bin, self.get('NETWORK_CMD'), self.get('ROOTFS_OPTIONS'), self.get('QB_OPT_APPEND'))
  1042. for ovmf in self.ovmf_bios:
  1043. format = ovmf.rsplit('.', 1)[-1]
  1044. self.qemu_opt += ' -drive if=pflash,format=%s,file=%s' % (format, ovmf)
  1045. if self.ovmf_bios:
  1046. # OVMF only supports normal VGA, i.e. we need to override a -vga vmware
  1047. # that gets added for example for normal qemux86.
  1048. self.qemu_opt += ' -vga std'
  1049. self.qemu_opt += ' ' + self.qemu_opt_script
  1050. # Append qemuparams to override previous settings
  1051. if self.qemuparams:
  1052. self.qemu_opt += ' ' + self.qemuparams
  1053. if self.snapshot:
  1054. self.qemu_opt += " -snapshot"
  1055. if self.serialconsole:
  1056. if sys.stdin.isatty():
  1057. subprocess.check_call(("stty", "intr", "^]"))
  1058. logger.info("Interrupt character is '^]'")
  1059. first_serial = ""
  1060. if not re.search("-nographic", self.qemu_opt):
  1061. first_serial = "-serial mon:vc"
  1062. # We always want a ttyS1. Since qemu by default adds a serial
  1063. # port when nodefaults is not specified, it seems that all that
  1064. # would be needed is to make sure a "-serial" is there. However,
  1065. # it appears that when "-serial" is specified, it ignores the
  1066. # default serial port that is normally added. So here we make
  1067. # sure to add two -serial if there are none. And only one if
  1068. # there is one -serial already.
  1069. serial_num = len(re.findall("-serial", self.qemu_opt))
  1070. if serial_num == 0:
  1071. self.qemu_opt += " %s %s" % (first_serial, self.get("QB_SERIAL_OPT"))
  1072. elif serial_num == 1:
  1073. self.qemu_opt += " %s" % self.get("QB_SERIAL_OPT")
  1074. # We always wants ttyS0 and ttyS1 in qemu machines (see SERIAL_CONSOLES),
  1075. # if not serial or serialtcp options was specified only ttyS0 is created
  1076. # and sysvinit shows an error trying to enable ttyS1:
  1077. # INIT: Id "S1" respawning too fast: disabled for 5 minutes
  1078. serial_num = len(re.findall("-serial", self.qemu_opt))
  1079. if serial_num == 0:
  1080. if re.search("-nographic", self.qemu_opt) or self.serialstdio:
  1081. self.qemu_opt += " -serial mon:stdio -serial null"
  1082. else:
  1083. self.qemu_opt += " -serial mon:vc -serial null"
  1084. def start_qemu(self):
  1085. import shlex
  1086. if self.kernel:
  1087. kernel_opts = "-kernel %s -append '%s %s %s %s'" % (self.kernel, self.kernel_cmdline,
  1088. self.kernel_cmdline_script, self.get('QB_KERNEL_CMDLINE_APPEND'),
  1089. self.bootparams)
  1090. if self.dtb:
  1091. kernel_opts += " -dtb %s" % self.dtb
  1092. else:
  1093. kernel_opts = ""
  1094. cmd = "%s %s" % (self.qemu_opt, kernel_opts)
  1095. cmds = shlex.split(cmd)
  1096. logger.info('Running %s\n' % cmd)
  1097. pass_fds = []
  1098. if self.lock_descriptor:
  1099. pass_fds = [self.lock_descriptor.fileno()]
  1100. process = subprocess.Popen(cmds, stderr=subprocess.PIPE, pass_fds=pass_fds)
  1101. self.qemupid = process.pid
  1102. retcode = process.wait()
  1103. if retcode:
  1104. if retcode == -signal.SIGTERM:
  1105. logger.info("Qemu terminated by SIGTERM")
  1106. else:
  1107. logger.error("Failed to run qemu: %s", process.stderr.read().decode())
  1108. def cleanup(self):
  1109. if self.cleaned:
  1110. return
  1111. # avoid dealing with SIGTERM when cleanup function is running
  1112. signal.signal(signal.SIGTERM, signal.SIG_IGN)
  1113. logger.info("Cleaning up")
  1114. if self.cleantap:
  1115. cmd = ('sudo', self.qemuifdown, self.tap, self.bindir_native)
  1116. logger.debug('Running %s' % str(cmd))
  1117. subprocess.check_call(cmd)
  1118. self.release_lock()
  1119. if self.nfs_running:
  1120. logger.info("Shutting down the userspace NFS server...")
  1121. cmd = ("runqemu-export-rootfs", "stop", self.rootfs)
  1122. logger.debug('Running %s' % str(cmd))
  1123. subprocess.check_call(cmd)
  1124. if self.saved_stty:
  1125. subprocess.check_call(("stty", self.saved_stty))
  1126. if self.clean_nfs_dir:
  1127. logger.info('Removing %s' % self.rootfs)
  1128. shutil.rmtree(self.rootfs)
  1129. shutil.rmtree('%s.pseudo_state' % self.rootfs)
  1130. self.cleaned = True
  1131. def load_bitbake_env(self, mach=None):
  1132. if self.bitbake_e:
  1133. return
  1134. bitbake = shutil.which('bitbake')
  1135. if not bitbake:
  1136. return
  1137. if not mach:
  1138. mach = self.get('MACHINE')
  1139. if mach:
  1140. cmd = 'MACHINE=%s bitbake -e' % mach
  1141. else:
  1142. cmd = 'bitbake -e'
  1143. logger.info('Running %s...' % cmd)
  1144. try:
  1145. self.bitbake_e = subprocess.check_output(cmd, shell=True).decode('utf-8')
  1146. except subprocess.CalledProcessError as err:
  1147. self.bitbake_e = ''
  1148. logger.warning("Couldn't run 'bitbake -e' to gather environment information:\n%s" % err.output.decode('utf-8'))
  1149. def validate_combos(self):
  1150. if (self.fstype in self.vmtypes) and self.kernel:
  1151. raise RunQemuError("%s doesn't need kernel %s!" % (self.fstype, self.kernel))
  1152. @property
  1153. def bindir_native(self):
  1154. result = self.get('STAGING_BINDIR_NATIVE')
  1155. if result and os.path.exists(result):
  1156. return result
  1157. cmd = ('bitbake', 'qemu-helper-native', '-e')
  1158. logger.info('Running %s...' % str(cmd))
  1159. out = subprocess.check_output(cmd).decode('utf-8')
  1160. match = re.search('^STAGING_BINDIR_NATIVE="(.*)"', out, re.M)
  1161. if match:
  1162. result = match.group(1)
  1163. if os.path.exists(result):
  1164. self.set('STAGING_BINDIR_NATIVE', result)
  1165. return result
  1166. raise RunQemuError("Native sysroot directory %s doesn't exist" % result)
  1167. else:
  1168. raise RunQemuError("Can't find STAGING_BINDIR_NATIVE in '%s' output" % cmd)
  1169. def main():
  1170. if "help" in sys.argv or '-h' in sys.argv or '--help' in sys.argv:
  1171. print_usage()
  1172. return 0
  1173. try:
  1174. config = BaseConfig()
  1175. def sigterm_handler(signum, frame):
  1176. logger.info("SIGTERM received")
  1177. os.kill(config.qemupid, signal.SIGTERM)
  1178. config.cleanup()
  1179. # Deliberately ignore the return code of 'tput smam'.
  1180. subprocess.call(["tput", "smam"])
  1181. signal.signal(signal.SIGTERM, sigterm_handler)
  1182. config.check_args()
  1183. config.read_qemuboot()
  1184. config.check_and_set()
  1185. # Check whether the combos is valid or not
  1186. config.validate_combos()
  1187. config.print_config()
  1188. config.setup_network()
  1189. config.setup_rootfs()
  1190. config.setup_final()
  1191. config.start_qemu()
  1192. except RunQemuError as err:
  1193. logger.error(err)
  1194. return 1
  1195. except Exception as err:
  1196. import traceback
  1197. traceback.print_exc()
  1198. return 1
  1199. finally:
  1200. config.cleanup()
  1201. # Deliberately ignore the return code of 'tput smam'.
  1202. subprocess.call(["tput", "smam"])
  1203. if __name__ == "__main__":
  1204. sys.exit(main())