Create an LXC Application Container using libvirt

LXC application containers are good tools to isolate single applications. Instead of directly creating them using lxc-create you can also create them with libvirt / virsh.

If you have dnsmasq installed and running globally you will get into trouble running net-start default. So follow the advice on http://mytipsandtricson.blogspot.com/2010/12/kvm-failed-to-start-network-default-in.html.
Now let's create the virtual network for virsh (libvirt):

sudo virsh --connect lxc:/// net-start default

If you want to know how the default network is configured, then run sudo virsh net-dumpxml default.

Now create the xml configuration file for the application container, let's say application-test.xml:

<domain type='lxc'>
  <name>vm1</name>
  <memory>500000</memory>
  <os>
    <type>exe</type>
    <init>/bin/bash</init>
  </os>
  <vcpu>1</vcpu>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>destroy</on_crash>
  <devices>
    <emulator>/usr/lib/libvirt/libvirt_lxc</emulator>
    <interface type='network'>
      <source network='default'/>
    </interface>
    <console type='pty' />
  </devices>
</domain>

(Make sure you adjust emulator to the path where libvirt_lxc is on your system!)

Now you can define this linux container (LXC) using libvirt like this:

virsh --connect lxc:/// define application-test.xml

And start it:

virsh --connect lxc:/// start vm1

If you want to connect to the new container on the terminal, run:

virsh --connect lxc:/// console vm1

resources

Comments