[OE-core] [PATCH 5/5] runqemu: set default device and hostfwd for slirp

Nathan Rossi nathan at nathanrossi.com
Tue Nov 29 09:38:30 UTC 2016


On 29 November 2016 at 17:35, Robert Yang <liezhi.yang at windriver.com> wrote:
> There is no network rather than lo whitout set device, so set it by
> default. And set hostfwd: 2222 -> 22, 2323 -> 23
>
> [YOCTO #7887]
>
> Signed-off-by: Robert Yang <liezhi.yang at windriver.com>
> ---
>  meta/conf/machine/include/qemuboot-x86.inc |  1 -
>  meta/conf/machine/qemuarm64.conf           |  1 -
>  scripts/runqemu                            | 34 ++++++++++++++++++++++++++++--
>  3 files changed, 32 insertions(+), 4 deletions(-)
>
> diff --git a/meta/conf/machine/include/qemuboot-x86.inc b/meta/conf/machine/include/qemuboot-x86.inc
> index 0870294..06ac983 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"
>  QB_KERNEL_CMDLINE_APPEND = "vga=0 uvesafb.mode_option=640x480-32 oprofile.timer=1 uvesafb.task_timeout=-1"
>  # Add the 'virtio-rng-pci' device otherwise the guest may run out of entropy
>  QB_OPT_APPEND = "-vga vmware -show-cursor -usb -usbdevice tablet -device virtio-rng-pci"
> -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 df2010c..7a5570c 100644
> --- a/meta/conf/machine/qemuarm64.conf
> +++ b/meta/conf/machine/qemuarm64.conf
> @@ -18,7 +18,6 @@ QB_KERNEL_CMDLINE_APPEND = "console=ttyAMA0,38400"
>  # Add the 'virtio-rng-pci' device otherwise the guest may run out of entropy
>  QB_OPT_APPEND = "-show-cursor -device virtio-rng-pci -monitor null"
>  QB_TAP_OPT = "-netdev tap,id=net0,ifname=@TAP@,script=no,downscript=no -device virtio-net-device,netdev=net0,mac=@MAC@"
> -QB_SLIRP_OPT = "-netdev user,id=net0 -device virtio-net-device,netdev=net0"
>  QB_ROOTFS_OPT = "-drive id=disk0,file=@ROOTFS@,if=none,format=raw -device virtio-blk-device,drive=disk0"
>  QB_SERIAL_OPT = "-device virtio-serial-device -chardev null,id=virtcon -device virtconsole,chardev=virtcon"
>  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/scripts/runqemu b/scripts/runqemu
> index a10270c..7123b8f 100755
> --- a/scripts/runqemu
> +++ b/scripts/runqemu
> @@ -147,6 +147,19 @@ def get_first_file(cmds):
>                      return f
>      return ''
>
> +def check_free_port(host, port):
> +    """ Check whether the port is free or not """
> +    import socket
> +    from contextlib import closing
> +
> +    with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
> +        if sock.connect_ex((host, port)) == 0:
> +            # Port is open, so not free
> +            return False
> +        else:
> +            # Port is not open, so free
> +            return True
> +
>  class BaseConfig(object):
>      def __init__(self):
>          # Vars can be merged with .qemuboot.conf, use a dict to manage them.
> @@ -751,14 +764,31 @@ class BaseConfig(object):
>
>          self.nfs_running = True
>
> -
>      def setup_slirp(self):
>          """Setup user networking"""
>
>          if self.fstype == 'nfs':
>              self.setup_nfs()
>          self.kernel_cmdline_script += ' ip=dhcp'
> -        self.set('NETWORK_CMD', self.get('QB_SLIRP_OPT'))
> +        # Port mapping
> +        hostfwd = ",hostfwd=tcp::2222-:22,hostfwd=tcp::2323-:23"
> +        qb_slirp_opt_default = "-netdev user,id=net0%s -device virtio-net-pci,netdev=net0" % hostfwd
> +        qb_slirp_opt = self.get('QB_SLIRP_OPT') or qb_slirp_opt_default
> +        # Figure out the port
> +        ports = re.findall('hostfwd=[^-]*:([0-9]+)-[^,-]*', qb_slirp_opt)
> +        ports = [int(i) for i in ports]

Should runqemu not just always supply "-netdev user,id=net0..." itself
for slirp. And qemuboot should supply only the devices, e.g. the "-dev
virtio-net-pci,netdev=net0" or "-net nic,netdev=net0". This could even
allow for consolidation of the "QB_SLIRP_OPT" and "QB_TAP_OPT" into
"QB_NETWORK".

This way runqemu would not need to do any searching/substitution and
could just build the user netdev arg itself. This would allow for easy
extension of custom forwards (e.g gdbserver, etc), and also even
setting up the in qemu user/slirp 'tftp=<dir>' server.

> +        # Find a free port to avoid conflicts
> +        for p in ports[:]:
> +            p_new = p
> +            while not check_free_port('127.0.0.1', p_new):

Should this be 'localhost', since 127.0.0.1 will only check for ipv4 listeners.

Regards,
Nathan

> +                p_new += 1
> +                while p_new in ports:
> +                        p_new += 1
> +            if p != p_new:
> +                ports.append(p_new)
> +                qb_slirp_opt = re.sub(':%s-' % p, ':%s-' % p_new, qb_slirp_opt)
> +                logger.info("Port forward changed: %s -> %s" % (p, p_new))
> +        self.set('NETWORK_CMD', qb_slirp_opt)
>
>      def setup_tap(self):
>          """Setup tap"""
> --
> 2.9.0
>
> --
> _______________________________________________
> Openembedded-core mailing list
> Openembedded-core at lists.openembedded.org
> http://lists.openembedded.org/mailman/listinfo/openembedded-core



More information about the Openembedded-core mailing list