diff options
-rw-r--r-- | meta/classes/qemuboot.bbclass | 11 | ||||
-rw-r--r-- | meta/conf/machine/include/qemuboot-x86.inc | 1 | ||||
-rw-r--r-- | meta/conf/machine/qemuarm64.conf | 4 | ||||
-rw-r--r-- | meta/conf/machine/qemuppc.conf | 2 | ||||
-rwxr-xr-x | scripts/runqemu | 62 |
5 files changed, 66 insertions, 14 deletions
diff --git a/meta/classes/qemuboot.bbclass b/meta/classes/qemuboot.bbclass index a181fa2213..be5d7a4c8f 100644 --- a/meta/classes/qemuboot.bbclass +++ b/meta/classes/qemuboot.bbclass | |||
@@ -18,11 +18,15 @@ | |||
18 | # QB_AUDIO_OPT: qemu audio option, e.g., "-soundhw ac97,es1370", used | 18 | # QB_AUDIO_OPT: qemu audio option, e.g., "-soundhw ac97,es1370", used |
19 | # when QB_AUDIO_DRV is set. | 19 | # when QB_AUDIO_DRV is set. |
20 | # QB_KERNEL_ROOT: kernel's root, e.g., /dev/vda | 20 | # QB_KERNEL_ROOT: kernel's root, e.g., /dev/vda |
21 | # QB_NETWORK_DEVICE: network device, e.g., "-device virtio-net-pci,netdev=net0,mac=@MAC@", | ||
22 | # it needs work with QB_TAP_OPT and QB_SLIRP_OPT. | ||
23 | # Note, runqemu will replace @MAC@ with a predefined mac, you can set | ||
24 | # a custom one, but that may cause conflicts when multiple qemus are | ||
25 | # running on the same host. | ||
21 | # QB_TAP_OPT: netowrk option for 'tap' mode, e.g., | 26 | # QB_TAP_OPT: netowrk option for 'tap' mode, e.g., |
22 | # "-netdev tap,id=net0,ifname=@TAP@,script=no,downscript=no -device virtio-net-device,netdev=net0" | 27 | # "-netdev tap,id=net0,ifname=@TAP@,script=no,downscript=no" |
23 | # Note, runqemu will replace "@TAP@" with the one which is used, such as tap0, tap1 ... | 28 | # Note, runqemu will replace "@TAP@" with the one which is used, such as tap0, tap1 ... |
24 | # QB_SLIRP_OPT: network option for SLIRP mode, e.g., | 29 | # QB_SLIRP_OPT: network option for SLIRP mode, e.g., -netdev user,id=net0" |
25 | # "-netdev user,id=net0 -device virtio-net-device,netdev=net0" | ||
26 | # QB_ROOTFS_OPT: used as rootfs, e.g., | 30 | # QB_ROOTFS_OPT: used as rootfs, e.g., |
27 | # "-drive id=disk0,file=@ROOTFS@,if=none,format=raw -device virtio-blk-device,drive=disk0" | 31 | # "-drive id=disk0,file=@ROOTFS@,if=none,format=raw -device virtio-blk-device,drive=disk0" |
28 | # Note, runqemu will replace "@ROOTFS@" with the one which is used, such as core-image-minimal-qemuarm64.ext4. | 32 | # Note, runqemu will replace "@ROOTFS@" with the one which is used, such as core-image-minimal-qemuarm64.ext4. |
@@ -40,6 +44,7 @@ QB_SERIAL_OPT ?= "-serial mon:stdio -serial null" | |||
40 | QB_DEFAULT_KERNEL ?= "${KERNEL_IMAGETYPE}" | 44 | QB_DEFAULT_KERNEL ?= "${KERNEL_IMAGETYPE}" |
41 | QB_DEFAULT_FSTYPE ?= "ext4" | 45 | QB_DEFAULT_FSTYPE ?= "ext4" |
42 | QB_OPT_APPEND ?= "-show-cursor" | 46 | QB_OPT_APPEND ?= "-show-cursor" |
47 | QB_NETWORK_DEVICE ?= "-device virtio-net-pci,netdev=net0,mac=@MAC@" | ||
43 | 48 | ||
44 | # Create qemuboot.conf | 49 | # Create qemuboot.conf |
45 | addtask do_write_qemuboot_conf after do_rootfs before do_image | 50 | addtask do_write_qemuboot_conf after do_rootfs before do_image |
diff --git a/meta/conf/machine/include/qemuboot-x86.inc b/meta/conf/machine/include/qemuboot-x86.inc index 08702948e4..06ac983d4d 100644 --- a/meta/conf/machine/include/qemuboot-x86.inc +++ b/meta/conf/machine/include/qemuboot-x86.inc | |||
@@ -13,4 +13,3 @@ QB_AUDIO_OPT = "-soundhw ac97,es1370" | |||
13 | QB_KERNEL_CMDLINE_APPEND = "vga=0 uvesafb.mode_option=640x480-32 oprofile.timer=1 uvesafb.task_timeout=-1" | 13 | QB_KERNEL_CMDLINE_APPEND = "vga=0 uvesafb.mode_option=640x480-32 oprofile.timer=1 uvesafb.task_timeout=-1" |
14 | # Add the 'virtio-rng-pci' device otherwise the guest may run out of entropy | 14 | # Add the 'virtio-rng-pci' device otherwise the guest may run out of entropy |
15 | QB_OPT_APPEND = "-vga vmware -show-cursor -usb -usbdevice tablet -device virtio-rng-pci" | 15 | QB_OPT_APPEND = "-vga vmware -show-cursor -usb -usbdevice tablet -device virtio-rng-pci" |
16 | QB_SLIRP_OPT = "-net nic,model=e1000 -net user,hostfwd=tcp::2222-:22" | ||
diff --git a/meta/conf/machine/qemuarm64.conf b/meta/conf/machine/qemuarm64.conf index e70538aac6..242889ac8a 100644 --- a/meta/conf/machine/qemuarm64.conf +++ b/meta/conf/machine/qemuarm64.conf | |||
@@ -17,8 +17,8 @@ QB_CPU = "-cpu cortex-a57" | |||
17 | QB_KERNEL_CMDLINE_APPEND = "console=ttyAMA0,38400" | 17 | QB_KERNEL_CMDLINE_APPEND = "console=ttyAMA0,38400" |
18 | # Add the 'virtio-rng-pci' device otherwise the guest may run out of entropy | 18 | # Add the 'virtio-rng-pci' device otherwise the guest may run out of entropy |
19 | QB_OPT_APPEND = "-show-cursor -device virtio-rng-pci -monitor null" | 19 | QB_OPT_APPEND = "-show-cursor -device virtio-rng-pci -monitor null" |
20 | QB_TAP_OPT = "-netdev tap,id=net0,ifname=@TAP@,script=no,downscript=no -device virtio-net-device,netdev=net0,mac=@MAC@" | 20 | QB_TAP_OPT = "-netdev tap,id=net0,ifname=@TAP@,script=no,downscript=no" |
21 | QB_SLIRP_OPT = "-netdev user,id=net0 -device virtio-net-device,netdev=net0" | 21 | QB_NETWORK_DEVICE = "-device virtio-net-device,netdev=net0,mac=@MAC@" |
22 | QB_ROOTFS_OPT = "-drive id=disk0,file=@ROOTFS@,if=none,format=raw -device virtio-blk-device,drive=disk0" | 22 | QB_ROOTFS_OPT = "-drive id=disk0,file=@ROOTFS@,if=none,format=raw -device virtio-blk-device,drive=disk0" |
23 | QB_SERIAL_OPT = "-device virtio-serial-device -chardev null,id=virtcon -device virtconsole,chardev=virtcon" | 23 | QB_SERIAL_OPT = "-device virtio-serial-device -chardev null,id=virtcon -device virtconsole,chardev=virtcon" |
24 | QB_TCPSERIAL_OPT = " -device virtio-serial-device -chardev socket,id=virtcon,port=@PORT@,host=127.0.0.1 -device virtconsole,chardev=virtcon" | 24 | QB_TCPSERIAL_OPT = " -device virtio-serial-device -chardev socket,id=virtcon,port=@PORT@,host=127.0.0.1 -device virtconsole,chardev=virtcon" |
diff --git a/meta/conf/machine/qemuppc.conf b/meta/conf/machine/qemuppc.conf index 9d174bc439..a9ef64b0af 100644 --- a/meta/conf/machine/qemuppc.conf +++ b/meta/conf/machine/qemuppc.conf | |||
@@ -18,4 +18,4 @@ QB_CPU = "-cpu G4" | |||
18 | QB_KERNEL_CMDLINE_APPEND = "console=tty console=ttyS0" | 18 | QB_KERNEL_CMDLINE_APPEND = "console=tty console=ttyS0" |
19 | # Add the 'virtio-rng-pci' device otherwise the guest may run out of entropy | 19 | # Add the 'virtio-rng-pci' device otherwise the guest may run out of entropy |
20 | QB_OPT_APPEND = "-show-cursor -usb -usbdevice tablet -device virtio-rng-pci" | 20 | QB_OPT_APPEND = "-show-cursor -usb -usbdevice tablet -device virtio-rng-pci" |
21 | QB_TAP_OPT = "-netdev tap,id=net0,ifname=@TAP@,script=no,downscript=no -device virtio-net-pci,netdev=net0,mac=@MAC@" | 21 | QB_TAP_OPT = "-netdev tap,id=net0,ifname=@TAP@,script=no,downscript=no" |
diff --git a/scripts/runqemu b/scripts/runqemu index b176e20845..30e5346356 100755 --- a/scripts/runqemu +++ b/scripts/runqemu | |||
@@ -84,7 +84,7 @@ of the following environment variables (in any order): | |||
84 | Examples: | 84 | Examples: |
85 | runqemu qemuarm | 85 | runqemu qemuarm |
86 | runqemu tmp/deploy/images/qemuarm | 86 | runqemu tmp/deploy/images/qemuarm |
87 | runqemu tmp/deploy/images/qemux86/.qemuboot.conf | 87 | runqemu tmp/deploy/images/qemux86/<qemuboot.conf> |
88 | runqemu qemux86-64 core-image-sato ext4 | 88 | runqemu qemux86-64 core-image-sato ext4 |
89 | runqemu qemux86-64 wic-image-minimal wic | 89 | runqemu qemux86-64 wic-image-minimal wic |
90 | runqemu path/to/bzImage-qemux86.bin path/to/nfsrootdir/ serial | 90 | runqemu path/to/bzImage-qemux86.bin path/to/nfsrootdir/ serial |
@@ -147,6 +147,19 @@ def get_first_file(cmds): | |||
147 | return f | 147 | return f |
148 | return '' | 148 | return '' |
149 | 149 | ||
150 | def check_free_port(host, port): | ||
151 | """ Check whether the port is free or not """ | ||
152 | import socket | ||
153 | from contextlib import closing | ||
154 | |||
155 | with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock: | ||
156 | if sock.connect_ex((host, port)) == 0: | ||
157 | # Port is open, so not free | ||
158 | return False | ||
159 | else: | ||
160 | # Port is not open, so free | ||
161 | return True | ||
162 | |||
150 | class BaseConfig(object): | 163 | class BaseConfig(object): |
151 | def __init__(self): | 164 | def __init__(self): |
152 | # Vars can be merged with .qemuboot.conf, use a dict to manage them. | 165 | # Vars can be merged with .qemuboot.conf, use a dict to manage them. |
@@ -186,6 +199,15 @@ class BaseConfig(object): | |||
186 | self.snapshot = False | 199 | self.snapshot = False |
187 | self.fstypes = ('ext2', 'ext3', 'ext4', 'jffs2', 'nfs', 'btrfs', 'cpio.gz', 'cpio', 'ramfs') | 200 | self.fstypes = ('ext2', 'ext3', 'ext4', 'jffs2', 'nfs', 'btrfs', 'cpio.gz', 'cpio', 'ramfs') |
188 | self.vmtypes = ('hddimg', 'hdddirect', 'wic', 'vmdk', 'qcow2', 'vdi', 'iso') | 201 | self.vmtypes = ('hddimg', 'hdddirect', 'wic', 'vmdk', 'qcow2', 'vdi', 'iso') |
202 | self.network_device = "-device e1000,netdev=net0,mac=@MAC@" | ||
203 | # Use different mac section for tap and slirp to avoid | ||
204 | # conflicts, e.g., when one is running with tap, the other is | ||
205 | # running with slirp. | ||
206 | # The last section is dynamic, which is for avoiding conflicts, | ||
207 | # when multiple qemus are running, e.g., when multiple tap or | ||
208 | # slirp qemus are running. | ||
209 | self.mac_tap = "52:54:00:12:34:" | ||
210 | self.mac_slirp = "52:54:00:12:35:" | ||
189 | 211 | ||
190 | def acquire_lock(self): | 212 | def acquire_lock(self): |
191 | logger.info("Acquiring lockfile %s..." % self.lock) | 213 | logger.info("Acquiring lockfile %s..." % self.lock) |
@@ -757,14 +779,39 @@ class BaseConfig(object): | |||
757 | 779 | ||
758 | self.nfs_running = True | 780 | self.nfs_running = True |
759 | 781 | ||
760 | |||
761 | def setup_slirp(self): | 782 | def setup_slirp(self): |
762 | """Setup user networking""" | 783 | """Setup user networking""" |
763 | 784 | ||
764 | if self.fstype == 'nfs': | 785 | if self.fstype == 'nfs': |
765 | self.setup_nfs() | 786 | self.setup_nfs() |
766 | self.kernel_cmdline_script += ' ip=dhcp' | 787 | self.kernel_cmdline_script += ' ip=dhcp' |
767 | self.set('NETWORK_CMD', self.get('QB_SLIRP_OPT')) | 788 | # Port mapping |
789 | hostfwd = ",hostfwd=tcp::2222-:22,hostfwd=tcp::2323-:23" | ||
790 | qb_slirp_opt_default = "-netdev user,id=net0%s" % hostfwd | ||
791 | qb_slirp_opt = self.get('QB_SLIRP_OPT') or qb_slirp_opt_default | ||
792 | # Figure out the port | ||
793 | ports = re.findall('hostfwd=[^-]*:([0-9]+)-[^,-]*', qb_slirp_opt) | ||
794 | ports = [int(i) for i in ports] | ||
795 | mac = 2 | ||
796 | # Find a free port to avoid conflicts | ||
797 | for p in ports[:]: | ||
798 | p_new = p | ||
799 | while not check_free_port('localhost', p_new): | ||
800 | p_new += 1 | ||
801 | mac += 1 | ||
802 | while p_new in ports: | ||
803 | p_new += 1 | ||
804 | mac += 1 | ||
805 | if p != p_new: | ||
806 | ports.append(p_new) | ||
807 | qb_slirp_opt = re.sub(':%s-' % p, ':%s-' % p_new, qb_slirp_opt) | ||
808 | logger.info("Port forward changed: %s -> %s" % (p, p_new)) | ||
809 | mac = "%s%02x" % (self.mac_slirp, mac) | ||
810 | self.set('NETWORK_CMD', '%s %s' % (self.network_device.replace('@MAC@', mac), qb_slirp_opt)) | ||
811 | # Print out port foward | ||
812 | hostfwd = re.findall('(hostfwd=[^,]*)', qb_slirp_opt) | ||
813 | if hostfwd: | ||
814 | logger.info('Port forward: %s' % ' '.join(hostfwd)) | ||
768 | 815 | ||
769 | def setup_tap(self): | 816 | def setup_tap(self): |
770 | """Setup tap""" | 817 | """Setup tap""" |
@@ -834,21 +881,22 @@ class BaseConfig(object): | |||
834 | if self.fstype == 'nfs': | 881 | if self.fstype == 'nfs': |
835 | self.setup_nfs() | 882 | self.setup_nfs() |
836 | self.kernel_cmdline_script += " ip=192.168.7.%s::192.168.7.%s:255.255.255.0" % (client, gateway) | 883 | self.kernel_cmdline_script += " ip=192.168.7.%s::192.168.7.%s:255.255.255.0" % (client, gateway) |
837 | mac = "52:54:00:12:34:%02x" % client | 884 | mac = "%s%02x" % (self.mac_tap, client) |
838 | qb_tap_opt = self.get('QB_TAP_OPT') | 885 | qb_tap_opt = self.get('QB_TAP_OPT') |
839 | if qb_tap_opt: | 886 | if qb_tap_opt: |
840 | qemu_tap_opt = qb_tap_opt.replace('@TAP@', tap).replace('@MAC@', mac) | 887 | qemu_tap_opt = qb_tap_opt.replace('@TAP@', tap) |
841 | else: | 888 | else: |
842 | qemu_tap_opt = "-device virtio-net-pci,netdev=net0,mac=%s -netdev tap,id=net0,ifname=%s,script=no,downscript=no" % (mac, self.tap) | 889 | qemu_tap_opt = "-netdev tap,id=net0,ifname=%s,script=no,downscript=no" % (self.tap) |
843 | 890 | ||
844 | if self.vhost_enabled: | 891 | if self.vhost_enabled: |
845 | qemu_tap_opt += ',vhost=on' | 892 | qemu_tap_opt += ',vhost=on' |
846 | 893 | ||
847 | self.set('NETWORK_CMD', qemu_tap_opt) | 894 | self.set('NETWORK_CMD', '%s %s' % (self.network_device.replace('@MAC@', mac), qemu_tap_opt)) |
848 | 895 | ||
849 | def setup_network(self): | 896 | def setup_network(self): |
850 | cmd = "stty -g" | 897 | cmd = "stty -g" |
851 | self.saved_stty = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8') | 898 | self.saved_stty = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8') |
899 | self.network_device = self.get('QB_NETWORK_DEVICE') or self.network_device | ||
852 | if self.slirp_enabled: | 900 | if self.slirp_enabled: |
853 | self.setup_slirp() | 901 | self.setup_slirp() |
854 | else: | 902 | else: |