Configuring for KVM


On Linux, it is very easy to use the local provider with KVM-based virtual machines. However, as the local provider is typically used with LXC containers users wishing to use KVM instead are urged to first read the more general writeup on Installing and configuring Juju for LXC (Linux).

Begin by adding the Juju stable release PPA:

sudo apt-add-repository ppa:juju/stable
sudo apt-get update

Now install the local provider and the KVM/libvirt software, including helper tools:

sudo apt-get install juju-local qemu-kvm libvirt-bin bridge-utils virt-manager qemu-system uvtool-libvirt uvtool


Start by generating a generic configuration file for Juju and then switching to the local provider:

juju generate-config
juju switch local

This will generate a file, environments.yaml (if it doesn't already exist), which will live, on Ubuntu, in your ~/.juju/ directory (and will create the directory if it doesn't already exist).

Note: If you have an existing configuration, you can use juju generate-config --show to output the new config file, then copy and paste relevant areas in a text editor etc.

The key ingredient necessary to use KVM virtual machines is container: kvm. An example working configuration for using them with the local provider is:

    type: local
    container: kvm

Note that the name of this provider has been changed from "local" to "kvm". Recall that this label is arbitrary and therefore useful for description purposes.


Proceed to bootstrap your environment:

juju bootstrap


Verify the setup by adding a machine:

juju add-machine --constraints "root-disk=6G mem=2G"

A KVM guest should appear when using the libvirt client:

virsh list --all
 Id    Name                           State
 1     ubuntu-local-machine-1         running

And juju status should give something similar to:

    agent-state: started
    instance-id: ubuntu-local-machine-1
    series: trusty
    hardware: arch=amd64 cpu-cores=1 mem=2048M root-disk=6144M

LXC Containers within a KVM Guest

You can go further and use the KVM guest as a hosting system for LXC containers. This is achieved in the manner in which Juju commands are invoked; no extra Juju configuration is required. What is required, however, is the creation of a network bridge on the KVM guest (LXC host) in order for the containers to have access to the external network (that of the LXC host, or KVM guest).

Although not required, as covered in Installing and configuring Juju for LXC (Linux), it is recommended to use LXC cloning to speed up the creation of LXC containers.
Unfortunately, lxc-clone cannot be specified during run-time with juju set-env.

KVM Guest Network Bridge

Connect to the KVM guest, assuming a Juju machine # of '1' (based on the above output for juju status):

juju ssh 1

Create a simple bridge ('lxcbr0') by editing the primary interface network ('eth0' here, and using DHCP) configuration so it looks like:

auto lxcbr0
 iface lxcbr0 inet dhcp
 bridge_ports eth0

Reboot the KVM guest to ensure the bridge comes up reliably.

Managing KVM-based LXC containers

To add two (to test connectivity) LXC containers to the KVM guest (machine #1):

juju add-machine lxc:1
juju add-machine lxc:1

The output to juju status should now look like:

    agent-state: started
    instance-id: ubuntu-local-machine-1
    series: trusty
        agent-state: started
        instance-id: ubuntu-local-machine-1-lxc-0
        series: trusty
        hardware: arch=amd64
        agent-state: started
        instance-id: ubuntu-local-machine-1-lxc-1
        series: trusty
        hardware: arch=amd64
    hardware: arch=amd64 cpu-cores=1 mem=2048M root-disk=6144M

And from the KVM guest:

sudo lxc-ls --fancy
NAME                          STATE    IPV4        IPV6  AUTOSTART
juju-trusty-lxc-template      STOPPED  -           -     NO 
ubuntu-local-machine-1-lxc-0  RUNNING   -     YES
ubuntu-local-machine-1-lxc-1  RUNNING  -     YES

Note: The template container is used for cloning.

To remove one of these containers (1/lxc/0 here):

juju destroy-machine --force 1/lxc/0

© 2018 Canonical Ltd. Ubuntu and Canonical are registered trademarks of Canonical Ltd.