4. Filesystems
In this section we will try an example with a CoW (Copy-on-Write) filesystem. CoW filesystems have the nice property that it is possible to "clone" files instantly, by having the new file refer to the old blocks, and copying (possibly) changed blocks. This both saves time and space, and can be very beneficial in a lot of situations (for example when working with big files). In Linux this type of copy is called "reflink". We will see how to use it on the XFS filesystem.
1. Create a virtual block device
Linux supports a special block device called the loop device, which maps a normal file onto a virtual block device. This allows for the file to be used as a "virtual file system".
Loop devices are not available on a container, so we need a real machine or a VM, in order to try this example.
-
Create a file of size 1G:
fallocate -l 1G disk.img
ls -hl disk.img
du -hs disk.img
-
Create a loop device with this file:
sudo losetup -f disk.img
The option
-f
finds an unused loop device. -
Find the name of the loop device that was created:
losetup -a
losetup -a | grep disk.img
lodevice=$(losetup -a | grep disk.img | cut -d: -f1)
2. Create an XFS filesystem
-
Make sure that the package
xfsprogs
is installed:sudo apt install xfsprogs
-
Create an XFS filesystem on the image file:
mkfs.xfs -m reflink=1 -L test disk.img
The metadata
-m reflink=1
tells the command to enable reflinks, and-L test
sets the label of the filesystem. -
Create a directory:
mkdir mnt
-
Mount the loop device on it:
# sudo mount /dev/loop0 mnt
sudo mount $lodevice mntmount | grep mnt
-
Check the usage of the filesystem:
df -h mnt/
Notice that only 40M are used from it.
3. Copy files with '--reflink'
-
Create for testing a file of size 100 MB (with random data):
cd mnt/
sudo chown $(whoami): .dd if=/dev/urandom of=test bs=1M count=100
-
Check that now there are 140M of disk space used:
df -h .
-
Create a copy of the file (with reflinks enabled):
cp -v --reflink=always test test1
-
Check the size of each file:
ls -hsl
Each of them is 100M and in total there are 200M of data.
-
However if we check the disk usage we will see that both of them still take on disk the same amount of space as before 140M:
df -h .
This shows the space-saving feature of reflinks. If the file was big enough, we would have noticed as well that the reflink copy takes no time at all, it is done instantly.
4. Clean up
-
Unmount and delete the test directory
mnt/
:cd ..
sudo umount mnt/
rmdir mnt/ -
Delete the loop device:
losetup -a
# sudo losetup -d /dev/loop0
sudo losetup -d $lodevice -
Remove the file that was used to create the loop device:
rm disk.img