Easily making a blockdev available to a container

Often it would be nice to mount an existing (lvm) block device into a container. For instance, to emulate an Amazon ec2 environment, I’d like to have /dev/vdb or /dev/xvdb as a block device.

So I wrote a mount hook which will ‘insert’ a block device from the host into the container. Of course in Ubuntu containers are clamped down so that the container isn’t allowed to use this device. So I use this script to set a container up to use a particular block device.

For instance, if I have a pristine lvm-backed container called ‘quantal-amd64′, and I want to run a container which has a 500M block device available as /dev/xvdb, I would do:

# clone a new container
sudo lxc-clone -s -o quantal-amd64 -n q1
# create a LVM block device in the lxc VG
sudo lvcreate -L 500M -n q1-d1 lxc
# expose the block device to the container as /dev/xvdb
sudo lxc-enabledev.sh /dev/lxc/q1-d1 xvdb

Now when I start the container, I can format the device and mount it:

sudo mkfs.ext2 /dev/xvdb
sudo mount /dev/xvdb /mnt
echo "hello world" | sudo tee /mnt/ab

Of course I can also format the device on the host, and preserve the device between multiple containers.

If this turns out to be something many people want, we can add support for this into lxc itself. But for the moment this meets my needs, and uses only existing lxc features.

One note: when you delete the container, you’ll want to also delete the custom apparmor profile which this created.

About these ads
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

4 Responses to Easily making a blockdev available to a container

  1. Fernando says:

    Excelent news! This is very useful to test the “cloud” image type in lxc simulating disk attachments. Just one error i think in the line “sudo lxc-enabledev.sh /dev/lxc/q1-d1 xvdb” must say “sudo lxc-enabledev.sh name_of_the_container /dev/lxc/q1-d1 xvdb” i’m right?

    Thanks again!

  2. Stephen says:

    Did an easier way make it into lxc yet? Haven’t found anything anywhere else.

    Also is there a way to have the dev node available when the container tries to mount it? Currently if I use this method and assign the mount in the containers fstab, it’ll start but mount the rootfs instead of the block device.
    umounting and remounting things fixes things once it starts.

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