Passa al contenuto principale

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".

informazioni

Loop devices are not available on a container, so we need a real machine or a VM, in order to try this example.

  1. Create a file of size 1G:

    fallocate -l 1G disk.img
    ls -hl disk.img
    du -hs disk.img
  2. Create a loop device with this file:

    sudo losetup -f disk.img

    The option -f finds an unused loop device.

  3. 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

  1. Make sure that the package xfsprogs is installed:

    sudo apt install xfsprogs
  2. 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.

  3. Create a directory:

    mkdir mnt
  4. Mount the loop device on it:

    # sudo mount /dev/loop0 mnt
    sudo mount $lodevice mnt
    mount | grep mnt
  5. Check the usage of the filesystem:

    df -h mnt/

    Notice that only 40M are used from it.

  1. 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
  2. Check that now there are 140M of disk space used:

    df -h .
  3. Create a copy of the file (with reflinks enabled):

    cp -v --reflink=always test test1
  4. Check the size of each file:

    ls -hsl

    Each of them is 100M and in total there are 200M of data.

  5. 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

  1. Unmount and delete the test directory mnt/:

    cd ..
    sudo umount mnt/
    rmdir mnt/
  2. Delete the loop device:

    losetup -a
    # sudo losetup -d /dev/loop0
    sudo losetup -d $lodevice
  3. Remove the file that was used to create the loop device:

    rm disk.img
Loading asciinema cast...