How to mount a (additional) block device into a LXC container

Written by - 0 comments

Published on - last updated on June 13th 2023 - Listed in LXC Linux

LXC containers start (by default) with a single filesystem, called the "rootfs". This can be seen in the LXC container's config file:

root@lxchost:~# grep rootfs /var/lib/lxc/db02/config
lxc.rootfs.path = lvm:/dev/vglxc/db02

In this case, the rootfs is located on a dedicated logical volume. It could also just be a local path (which is the default in LXC, unless otherwise overwritten with the -B parameter during lxc-create launch).

A dedicated fstab for each container

To mount additional block devices, such as another Logical Volume, into the LXC container, a dedicated fstab file can be created for this container db02:

root@lxchost:~# cat /var/lib/lxc/db02/fstab
/dev/vglxc/db02backup backup ext4 defaults 0 0

The content of the container's LXC fstab file looks very similar to the host's /etc/fstab, with one major difference: The mount path (inside the container) is added without a leading slash; it's just backup instead of /backup. The reason is that the target mount directory is a relative path and is automatically appended to the container's rootfs in its own namespace.

Inside the container's main configuration file (config), refer to the created fstab file with the lxc.mount.fstab option:

root@lxchost:~# grep mount /var/lib/lxc/db02/config
lxc.mount.fstab = /var/lib/lxc/db02/fstab

The additional mount does not automatically show up inside the container, you need to restart the container (using lxc-stop, lxc-start) on the host.

Error when mount fails during lxc start

Of course the target mount point must exist inside the container's rootfs, or you will run into an error like this when starting the LXC container:

root@lxchost:~# lxc-start -n db02 -F
lxc-start: db02: conf.c: mount_entry: 2410 No such file or directory - Failed to mount "/dev/vglxc/db02backup" on "/usr/lib/x86_64-linux-gnu/lxc/backup"
lxc-start: db02: conf.c: setup_mount_fstab: 2786 Failed to set up mount entries
lxc-start: db02: conf.c: lxc_setup: 4370 Failed to setup mounts
lxc-start: db02: start.c: do_start: 1275 Failed to setup container "db02"
lxc-start: db02: sync.c: sync_wait: 34 An error occurred in another process (expected sequence number 3)
lxc-start: db02: start.c: __lxc_start: 2074 Failed to spawn container "db02"
lxc-start: db02: tools/lxc_start.c: main: 306 The container failed to start
lxc-start: db02: tools/lxc_start.c: main: 311 Additional information can be obtained by setting the --logfile and --logpriority options

Can also be used as "shared drive"

The added block device can be added to multiple containers at the same time, there's no limit which restricts the mount to one container. This way it can be used to exchange data between two containers for example.

root@lxchost:~# lxc-attach -n db01
root@db01:~# ll /backup/
total 2005052
drwx------ 2 root root      16384 Jun 12 13:50 lost+found
-rw-r--r-- 1 root root 2053151578 Jun 12 14:10 data.sql.gz
root@db01:~# touch /backup/test
root@db01:~# exit

root@lxchost:~# lxc-attach -n db02
root@db02:~# ll /backup/
total 2005052
drwx------ 2 root root      16384 Jun 12 13:50 lost+found
-rw-r--r-- 1 root root 2053151578 Jun 12 14:10 data.sql.gz
-rw-r--r-- 1 root root          0 Jun 13 06:59 test

In the above example I created a "test" file inside the mounted /backup and then left the container db01. Then I entered the container db02, which also has the same logical volume mounted on /backup. And we can find our "test" file there, too.

Attention on userspace / netdev file systems

The LXC fstab method works perfectly fine for local block devices seen on the LXC host, such as physical disks or partitions, logical volumes and similar.

However when using special types of file systems, which are mounted in User Space (and not Kernel Space), such as Netdev file systems, this often won't work with a container fstab. GlusterFS is such an example which requires to be mounted in user space. Refer to another article how to mount a GlusterFS volume into a LXC container.

Add a comment

Show form to leave a comment

Comments (newest first)

No comments yet.