runqemu 51 KB

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