LXC attach extra block device to container on CentOS/RHEL 7.x

You might ask why do I need an extra block device?
Most of the time you do not need it but special cases might occur and I’ll give an example: we want to test GlusterFS with distributed/replicated bricks using plain block devices inside containers.

1st make sure you have installed lxc
yum install lxc lxc-templates

Setup the container
lxc-create --template=centos --name=gluster01 --bdev=lvm --vgname=cached_raid1 --fssize=4G

Create 2 new extra block devices
lvcreate -V 10G --thin -n gluster01_brick1 raid_10/thin_pool_secondary
lvcreate -V 10G --thin -n gluster01_brick2 raid_10/thin_pool_secondary

Grab minor and major of the new block device
ls -al /dev/mapper/raid_10-gluster01_brick1
ls -al /dev/mapper/raid_10-gluster01_brick2

Configure the container with the new block device
vi /var/lib/lxc/gluster01/config

add

# Mounting extra disk
lxc.aa_profile = lxc-container-default-with-mounting
lxc.cgroup.devices.allow = b 253:37 rwm # 1st brick
lxc.cgroup.devices.allow = b 253:38 rwm # 2nd brick
lxc.autodev = 1
lxc.hook.autodev = /var/lib/lxc/gluster01/autodev_block.sh

Create /var/lib/lxc/gluster01/autodev_block.sh
with the following content

#!/bin/bash
cd ${LXC_ROOTFS_MOUNT}/dev
mknod -m 666 sdb b 253 37
mknod -m 666 sdc b 253 38

then make it executable

chmod +x /var/lib/lxc/gluster01/autodev_block.sh

Start the container and check everything
lxc-start --name=gluster01

and run inside the container:

fdisk -l

You should see 2 disks of 10GB.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s