[libvirt] Serial connection between guests

Martin Kletzander mkletzan at redhat.com
Tue Jul 14 07:52:12 UTC 2015

On Fri, Jul 10, 2015 at 04:28:46PM -0300, Marcelo Ricardo Leitner wrote:
>(I'm not subscribed to the list, please keep me on Cc)
>I'm attempting to get a serial link between two guests, same hypervisor.
>The only practical way I could find is to add a serial port using a pty
>to a guest and then manually connecting to the serial (console in my
>case) of the other guest using socat in the hypervisor.
>Then it made me think.. we could have this implemented at libvirt level.
>We could have a serial port on which we choose pty, udp, tcp, etc, and
>also a serial port from another guest, so that libvirt would handle
>socat start/stop automatically as both guests come up/down. Maybe
>libvirt could even do something smarter than that, maybe it can avoid
>socat somehow.

You can have one domain with serial port that will listen on some
interface (unix socket, ip and port, whatever) and then have the
second one connect to it.  That could be done for example like this:

XML snippet of device for domain A:
  <serial type='file'>
    <source mode='bind' path='/tmp/tahi.sock'/>
    <target port='1'/>

XML snippet of device for domain B:
  <serial type='file'>
    <source mode='connect' path='/tmp/tahi.sock'/>
    <target port='1'/>

This way you need to make sure domain A is started when you are
starting domain B, so it can connect to the socket that was created by
domain A.  If you don't want to depend on the order of domains being
started, you can use for example socat for that:

XML snippet of device for domain A:
  <serial type='file'>
    <source mode='bind' path='/tmp/tahi-domA.sock'/>
    <target port='1'/>

XML snippet of device for domain B:
  <serial type='file'>
    <source mode='bind' path='/tmp/tahi-domB.sock'/>
    <target port='1'/>

And then run socat as:
  socat unix:/tmp/tahi-domA.sock unix:/tmp/tahi-domB.sock

For more details on how this works and what you can use, see the
documentation [1].  Beware that if it's the only serial port n the
guest, it will become a console, so you might want to add it instead
of modifying an existing one.

Also, the examples are done without any security labels in mind.  If
you use SELinux, for example, you'll probably need to adjust the
<seclabel/> for the sockets, see the docs [2] for details.


[1] https://libvirt.org/formatdomain.html#elementCharChannel
[2] https://libvirt.org/formatdomain.html#seclabel

>What do you think? My usage is for virtualizing TAHI:
>I need 2 ethernet links plus a serial one, which TAHI can't break while
>running the tests.
>I didn't think this regarding multi-platform & all.. just sharing the
>libvir-list mailing list
>libvir-list at redhat.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20150714/6af7de7e/attachment-0001.sig>

More information about the libvir-list mailing list