Linux: Mounting a loopback ext4/xfs filesystem to isolate or enforce storage limits

The physical partitions and filesystem formats on your host are configured for your main workload, but if you want an application to use a specific filesystem (xfs, ext4, zfs) and size capacity without reconfiguration at the physical level then you can consider a loopback image.

For example, if we create a 100Mb disk file named “100Mb.img”, it can be formatted as an xfs filesystem and mounted as a loopback device and exposed as the directory “/data/myxfs”.  An application/user would have no idea their work within that directory was actually modifying the disk file “100Mb.img”.

This can be useful in a container system like Docker, where upper capacity will be limited by the size of the loopback disk volume.

Below are instructions for creating an ext4 and xfs loopback device.

Mounting an ext4 loopback device

Below are instructions for creating a 32Mb disk image with a ext4 filesystem, mounted at “/data/volumes/ext32m”.  The file is sparse, which means it can grow up to 32M, but does not take up that room until actually used.

# make directory where disk images and mounts will occur
sudo mkdir -p /data/volumes && cd $_

# create sparse file using seek, 32Mb max size
sudo dd if=/dev/zero of=ext4.32M bs=1 count=0 seek=32M

# check true and apparent size
ls -lhs ext4.32M

# format in ext4 format
sudo mkfs -t ext4 -q ext4.32M

# create directory where mount will occur
sudo mkdir -p /data/volumes/ext32m

# mount as loopback device
sudo mount -o loop,rw ext4.32M /data/volumes/ext32m

# now has entry in loopback list, auto-assigned
sudo losetup -a | grep ext4.32M

# check at what size write failure occurs
for i in $(seq 20 35); do sudo dd if=/dev/zero of=ext32m/test.txt bs=1M count=$i 2>/dev/null; if [ $? -ne 0 ]; then echo "filesize of $i Mb failed, capacity reached!"; else echo "filesize of $i Mb was ok"; fi; done

# remove content file
sudo rm ext32m/test.txt

# unmount
sudo umount /data/volumes/ext32m
# check that entry has been cleared from loopback list
sudo losetup -a

# delete disk image completely
sudo rm ext4.32M

The minimum size for ext4 is ~ 2M

Mounting an xfs loopback device

Below are instructions for creating a 32Mb disk image with an xfs filesystem, mounted at “/data/volumes/xfs32m”.  The file is sparse, which means it can grow up to 32M, but does not take up that room until actually used.

# make directory where disk images and mounts will occur
sudo mkdir -p /data/volumes && cd $_

# create sparse file using seek, 32Mb max size
sudo dd if=/dev/zero of=xfs.32M bs=1 count=0 seek=32M

# check true and apparent size
ls -lhs xfs.32M

# format in xfs format
sudo mkfs -t xfs -q xfs.32M

# create directory where mount will occur
sudo mkdir -p /data/volumes/xfs32m

# mount as loopback device with project quota enabled
sudo mount -o loop,rw xfs.32M -o pquota /data/volumes/xfs32m
# check entry in loopback list, auto-assigned
sudo losetup -a | grep xfs.32M

# check at what size write failure occurs
for i in $(seq 20 35); do sudo dd if=/dev/zero of=xfs32m/test.txt bs=1M count=$i 2>/dev/null; if [ $? -ne 0 ]; then echo "filesize of $i Mb failed, capacity reached!"; else echo "filesize of $i Mb was ok"; fi; done

# remove content file
sudo rm xfs32m/test.txt

# unmount
sudo umount /data/volumes/xfs32m
# check that entry has been cleared from loopback list
sudo losetup -a

# delete disk image completely
sudo rm xfs.32M

The minimum size for xfs is 4096 blocks ~20M

Take note that with xfs you can also have user/group/project quotas within this single filesystem.  Read my article “Using xfs project quotas to limit capacity within a subdirectory” for more details.

 

REFERENCES

thegeekdiary, virtual loopback block device

thegeekdiary, quotas on xfs

linuxtechi, good explanation of xfs quotas

held.org.il, examples of project based quotas, /etc/projects and /etc/projid files

scriptthe.net, hard quotas on directory

stackoverflow, setting GRUB_CMDLINE_LINUX_DEFAULT to support xfs quota

xfs_quota man page, shows how to init configuration without config files

invent.life, create loopback device with ext4

man mount page

 

NOTES

Alternate way of creating large files without dd (non-sparse)

fallocate -l 100M 100m.pdf
fallocate -l 1G 1g.pdf