Tips on LXC in Ubuntu natty

I’m going to post a few entries on using lxc in natty. This first one will just show some basic usage, with piggybacking on libvirt’s bridge. The next two will show how to use your own bridge.

First, I think it’s generally a good idea to use apt-cacher-ng so as to locally mirror packages and save on your network bandwith. I always install the package and convert both my host and guests to use it. Install the package with

apt-get install apt-cacher-ng

Configure the host to use it by editing /etc/apt/sources.list

   cp /etc/apt/sources.list /etc/apt/sources.list.nocache
   sed -i 's/http:\/\//localhost:3142\//' /etc/apt/sources.list

and configure lxc to use it by editing /etc/default/lxc:

   # find the device we're using for default route
   dev=`netstat -nr | grep '^0.0.0.0' | awk '{ print $8 }'`
   # and find that device's ip address
   ip=`ifconfig ${dev} | grep 'inet addr' | awk -F: '{ print $2 '} | awk '{ print $1 '}`
   # you don't need to do the above if you already know your ip address :)
   echo 'MIRROR="http://${ip}:3142/us.archive.ubuntu.com/ubuntu"' >> /etc/default/lxc

Next, you need to mount the cgroup filesystem. I’ll need to do a separate post on more of this, but for now just add cgroup to /etc/fstab:

  cat >> /etc/fstab << EOF
  cgroup	/sys/fs/cgroup	cgroup	defaults	0	0
  EOF

Now each reboot will cause it to be mounted, but since we don’t want to reboot, just do

mount -a

You can now install lxc and debootstrap (used by the lxc templates) by doing:

apt-get install lxc debootstrap

Remember in this post we’re assuming that libvirt is installed, so we can use the following lxc.conf to piggyback on libvirt’s bridge:

  cat >> /etc/lxc.conf << EOF
  lxc.network.type=veth
  lxc.network.link=virbr0
  lxc.network.flags=up
  EOF

Then create your first container, called natty1:

  lxc-create -f /etc/lxc.conf -t natty -n natty1

and start it up

  lxc-start -n natty1
Advertisements
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

2 Responses to Tips on LXC in Ubuntu natty

  1. Marcus says:

    >>echo ‘MIRROR=”http://${ip}:3142/us.archive.ubuntu.com/ubuntu”‘ >> /etc/apt/sources.list

    Good idea if you want to kill your sources list 😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s