runqemu 69 KB

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