6

I have a serial console working for a centos7 guest without graphics, which I access with virsh console vm. The guest has the appropriate console=ttyS0,115200n8 kernel command line parameter for it.

Is it possible to configure additional consoles, so that I can say virsh console vm --devname vc1 and get a login prompt?

Instinctively, I was thinking of connecting somehow to the character devices of the guest's first 6 virtual consoles; I've looked into libvirt domain format and virtio-serial as it seemed I should go in that direction, but couldn't get it to work.

Background: We had network issues which took a significant amount of time to fix, during which we needed one team member to work on network issues and the other to continue his work on the VM uninterrupted, thus the need for multiple consoles under no networking.

I am aware that having

<graphics type='vnc' port='5900' autoport='yes' listen='127.0.0.1'>
  <listen type='address' address='127.0.0.1'/>
</graphics>

enables VNC access with 6 virtual terminals, I was simply wondering if it is possible to have such 6 virtual terminals via the virsh console <domain> --device <device> syntax in any reasonable way, simply because virsh console is far more convenient.

Software:

# cat /etc/fedora-release # host
Fedora release 24 (Twenty Four)
# virsh --version
1.3.3.3
# qemu-system-x86_64 --version
QEMU emulator version 2.6.2 (qemu-2.6.2-8.fc24), Copyright (c) 2003-2008 Fabrice Bellard

# cat /etc/centos-release # guest
CentOS Linux release 7.3.1611 (Core)
Irfy
  • 215
  • 2
  • 7
  • have you considered `screen` or `tmux` in the VM? – cas Aug 03 '17 at 10:09
  • I've added some background info that should explain why I need it the way I described it: no network, one serial console, two guys. – Irfy Aug 03 '17 at 19:14

3 Answers3

4

I frequently use multiple "consoles" on my VMs - one for an interactive console showing the boot-up and ending with a login prompt, and another to log all of that to a text file (usually /var/lib/libvirt/consoles/<domain>.log)

I don't know if you can have multiple interactive "consoles" in a VM, but you can add as many serial ports as you like, and then run getty on them in the VM for the login prompt.

These serial ports in the VM can be connected to, e.g., a file, or a socket, or a TCP port on the host that speaks telnet protocol. Easiest to work with is probably a telnet port.

e.g. to add a serial ttyS1 serial port which can be accessed via telnet, save the following XML fragment to /tmp/serial1.xml:

<serial type='tcp'>
  <source mode='bind' host='127.0.0.1' service='4555' tls='no'/>
  <protocol type='telnet'/>
  <target port='1'/>
  <alias name='serial1'/>
</serial>

Then run virsh attach-device --config <domain> /tmp/serial1.xml.

That will add a serial port device to the VM, which will be activated the next time the VM restarts. (There may be some way to add it as a hot-pluggable USB device rather than a non-USB serial port, and avoid the need to restart the VM. I've never cared enough to find out).

After the VM has rebooted, run a getty on the port. e.g. with sysvinit, edit /etc/inittab and run telinit q.

With systemd:

systemctl enable [email protected]
systemctl start [email protected]

To connect to the VM's serial port from the KVM host, run telnet 127.0.0.1 4555.

You can create as many serial ports as you like, each listening on a different port. Just change the tcp port number (service=), target port, and alias name in the XML fragment.

If you need to access it from another machine, you can make it listen on a different IP address (although you probably want tls='yes' in that case, and use a tls-enabled telnet client to connect, which will require setting up a certificate for qemu to use).

For example, I added two serial ports to a Debian Stretch VM:

First, ttyS1 on localhost:4555

$ telnet localhost 4555
Trying 127.0.0.1...
Connected to localhost.mydomain.
Escape character is '^]'.

Debian GNU/Linux 9 stretch ttyS1

stretch login: 
telnet> quit
Connection closed.

Then ttyS2 on localhost:4556

$ telnet localhost 4556
Trying 127.0.0.1...
Connected to localhost.mydomain.
Escape character is '^]'.

Debian GNU/Linux 9 stretch ttyS2

stretch login: 
telnet> quit
Connection closed.
cas
  • 1
  • 7
  • 119
  • 185
  • The project where we needed this was cancelled and I never got to test this out. But it seems promising, and reasonable, so I'm tentatively accepting it as the answer. – Irfy Sep 23 '18 at 18:01
3

I stumbled upon this thread for similar reasons described by OP. There is no need for domain-wide alias(es) in the ua- form described project documentation.

Simply add one or more consoles as <target type='virtio' port='N'/>. Then access the hvc0, hvc1, ... consoles with

virsh console VMNAME consoleN

N counts from 1. Guest-side getty setup is left as an exercise for the reader.

AdminBee
  • 21,637
  • 21
  • 47
  • 71
Kalts
  • 31
  • 2
2

I just found this thread while trying to figure out something similar to this. I managed to find a way to do exactly what the original post was asking for. Adding extra consoles to a guest and connecting to them using virsh console.
This works best with guests that have virtio capability (modern linux distros do) and have it enabled.

First create an xml for the console device to attach. Should look like this:

<console type='pty'>
  <target type='virtio' port='1'/>
  <alias name='ua-hvc0'/>
</console>

Virtio provides 8 virtual consoles within the guest as /dev/hvc{0..7} which should correspond to port 1 to 8. The alias has to start with ua- otherwise libvirt ignores it and has to be unique. You can set it to something else if you like.

Next you attach the device to the guest with virsh attach-device <domain> /path/to/xml
To be able to log in on the console you have to enable getty on the corresponding hvc. With systemd this would look like systemctl enable --now getty@hvc0 for example.

After this you should be able to connect from the host with virsh console <domain> ua-hvc0 or whatever alias you chose.

I haven't tested without virtio but the process should be similar. instead of a console device you add a serial device like:

<serial type='pty'>
  <target port='1'/>
  <alias name='ua-ttyS1'/>
</serial>

This should add serial port 1 which should be the guests /dev/ttyS1. Enable getty on ttyS1 (systemct enable getty@ttyS1 on guest) and connect with virsh console <domain> ua-ttyS1

Vasi Márk
  • 33
  • 3