KVM & virsh console

Thu 27 October 2016

When using a default ubuntu install with KVM the virsh console tool doesnt work out the box. The following playbook & conf will show you how to get your VMs working with the virsh console cli tool.

You will get something like this when your try the virsh console command with a guest that doesnt have a serial tty setup.

virsh # console webserver
Connected to domain webserver
Escape character is ^]

It will just hang here and not accept any input. This has been tested on 16.04 & 14.04 VMs, cant confirm anything else...

ttyS0.conf

# ttyS0 - getty
#
# This service maintains a getty on ttyS0 from the point the system is
# started until it is shut down again.

start on stopped rc RUNLEVEL=[2345]
stop on runlevel [!2345]

respawn
exec /sbin/getty -L 38400 ttyS0 vt102

This config file copied into /etc/init/ give you a serial console. This should work on its own for a console. But a small change to /etc/default/grub will let you see the kernel boot.

GRUB_CMDLINE_LINUX="console=ttyS0,38400n8 console=tty0"

Once this edit is in place run update-grub.

Below is a ansible playbook that you can execute on your VMs and after a reboot virsh will let you use the console tool.

---
- hosts: xxxxx
  tasks:

  - name: copy ttyS0 to the VMs
    become: yes
    copy:
      src: files/ttyS0.conf
      dest: /etc/init/ttyS0.conf
      owner: root
      group: root
      mode: 0644
      force: no
      backup: yes

  - name: edit default grub
    become: yes
    lineinfile:
      dest: /etc/default/grub
      regexp: ^GRUB_CMDLINE_LINUX=
      line: GRUB_CMDLINE_LINUX="console=ttyS0,38400n8 console=tty0"
      backup: yes
    notify:
      - update-grub

  handlers:

    - name: update-grub
      become: yes
      shell: "update-grub"

Make sure the conf file is matching your src in the copy task and fire away with the playbook.

For those not familar with ansible.

This will take the following steps to setup console access to your VM:

Run this with caution as it will update your grub config after making the changes.

blog comments powered by Disqus