Btrfs – Linux Hint https://linuxhint.com Exploring and Master Linux Ecosystem Sun, 31 Jan 2021 18:40:31 +0000 en-US hourly 1 https://wordpress.org/?v=5.6.2 Useful Mount Options of the Btrfs Filesystem https://linuxhint.com/btrfs-filesystem-mount-options/ Sat, 30 Jan 2021 18:26:58 +0000 https://linuxhint.com/?p=88304

Like any other filesystems, the Btrfs filesystem also has a lot of mount options that you can use to configure the Btrfs filesystem’s behavior while mounting the filesystem.

This article will show you how to mount a Btrfs filesystem with your desired mount options. I will explain some of the useful Btrfs mount options as well. So, let’s get started.

Abbreviations

ACL – Access Control List
RAID – Redundant Array of Independent/Inexpensive Disks
UUID – Universally Unique Identifier

Where to Put Btrfs Mount Options

You can mount a Btrfs filesystem using the mount command-line program or the /etc/fstab file at boot time. You can configure the behavior of the Btrfs filesystem using mount options. In this section, I am going to show you how to mount a Btrfs filesystem using different mount options:

  1. from the command-line.
  2. using the /etc/fstab

From the command-line, you can mount a Btrfs filesystem (created on the sdb storage device) on the /data directory with the mount options option1, option2, option3, etc. as follows:

$ sudo mount -o option1,option2,option3,… /dev/sdb /data

To mount the same Btrfs filesystem at boot time using the /etc/fstab file, you need to find the UUID of the Btrfs filesystem.

You can find the UUID of the Btrfs filesystem with the following command:

$ sudo blkid --match-token TYPE=btrfs

As you can see, the UUID of the Btrfs filesystem created on the sdb storage device is c69a889a-8fd2-4571-bd97-a3c2e4543b6b.

Open the /etc/fstab file with the following command:

$ sudo nano /etc/fstab

To automatically mount the Btrfs filesystem that has the UUID c69a889a-8fd2-4571-bd97-a3c2e4543b6b on the /data directory with the mount options option1,option2,option3, etc., add the following line at the end of the /etc/fstab file.

UUID=c69a889a-8fd2-4571-bd97-a3c2e4543b6b            /data    btrfs     option1,option2,option3,…        0          0

Once you’re done, press <Ctrl> + X followed by Y and <Enter> to save the /etc/fstab file.

Your Btrfs filesystem should be mounted with your desired mount options.

Important Btrfs Mount Options

In this section, I am going to explain some of the important Btrfs mount options. So, let’s get started.

The most important Btrfs mount options are:

1. acl and noacl
ACL manages user and group permissions for the files/directories of the Btrfs filesystem.

The acl Btrfs mount option enables ACL. To disable ACL, you can use the noacl mount option.

By default, ACL is enabled. So, the Btrfs filesystem uses the acl mount option by default.

2. autodefrag and noautodefrag
Defragmenting a Btrfs filesystem will improve the filesystem’s performance by reducing data fragmentation.

The autodefrag mount option enables automatic defragmentation of the Btrfs filesystem.

The noautodefrag mount option disables automatic defragmentation of the Btrfs filesystem.

By default, automatic defragmentation is disabled. So, the Btrfs filesystem uses the noautodefrag mount option by default.

3. compress and compress-force
Controls the filesystem-level data compression of the Btrfs filesystem.

The compress option compresses only the files that are worth compressing (if compressing the file saves disk space).

The compress-force option compresses every file of the Btrfs filesystem even if compressing the file increases its size.

The Btrfs filesystem support many compression algorithms and each of the compression algorithm has different levels of compression.

The Btrfs supported compression algorithms are: lzo, zlib (level 1 to 9), and zstd (level 1 to 15).

You can specify what compression algorithm to use for the Btrfs filesystem with one of the following mount options:

  • compress=algorithm:level
  • compress-force=algorithm:level

For more information, check my article How to Enable Btrfs Filesystem Compression.

4. subvol and subvolid
These mount options are used to separately mount a specific subvolume of a Btrfs filesystem.

The subvol mount option is used to mount the subvolume of a Btrfs filesystem using its relative path.

The subvolid mount option is used to mount the subvolume of a Btrfs filesystem using the ID of the subvolume.

For more information, check my article How to Create and Mount Btrfs Subvolumes.

5. device
The device mount option is used in multi-device Btrfs filesystem or Btrfs RAID.

In some cases, the operating system may fail to detect the storage devices used in a multi-device Btrfs filesystem or Btrfs RAID. In such cases, you can use the device mount option to specify the devices that you want to use for the Btrfs multi-device filesystem or RAID.

You can use the device mount option multiple times to load different storage devices for the Btrfs multi-device filesystem or RAID.

You can use the device name (i.e., sdb, sdc) or UUID, UUID_SUB, or PARTUUID of the storage device with the device mount option to identify the storage device.

For example,

  • device=/dev/sdb
  • device=/dev/sdb,device=/dev/sdc
  • device=UUID_SUB=490a263d-eb9a-4558-931e-998d4d080c5d
  • device=UUID_SUB=490a263d-eb9a-4558-931e-998d4d080c5d,device=UUID_SUB=f7ce4875-0874-436a-b47d-3edef66d3424

6. degraded
The degraded mount option allows a Btrfs RAID to be mounted with fewer storage devices than the RAID profile requires.

For example, the raid1 profile requires 2 storage devices to be present. If one of the storage devices is not available in any case, you use the degraded mount option to mount the RAID even though 1 out of 2 storage devices is available.

7. commit
The commit mount option is used to set the interval (in seconds) within which the data will be written to the storage device.

The default is set to 30 seconds.

To set the commit interval to 15 seconds, you can use the mount option commit=15 (let’s say).

8. ssd and nossd
The ssd mount option tells the Btrfs filesystem that the filesystem is using an SSD storage device, and the Btrfs filesystem does the necessary SSD optimization.

The nossd mount option disables SSD optimization.

The Btrfs filesystem automatically detects whether an SSD is used for the Btrfs filesystem. If an SSD is used, the ssd mount option is enabled. Otherwise, the nossd mount option is enabled.

9. ssd_spread and nossd_spread
The ssd_spread mount option tries to allocate big continuous chunks of unused space from the SSD. This feature improves the performance of low-end (cheap) SSDs.

The nossd_spread mount option disables the ssd_spread feature.

The Btrfs filesystem automatically detects whether an SSD is used for the Btrfs filesystem. If an SSD is used, the ssd_spread mount option is enabled. Otherwise, the nossd_spread mount option is enabled.

10. discard and nodiscard
If you’re using an SSD that supports asynchronous queued TRIM (SATA rev3.1), then the discard mount option will enable the discarding of freed file blocks. This will improve the performance of the SSD.

If the SSD does not support asynchronous queued TRIM, then the discard mount option will degrade the SSD’s performance. In that case, the nodiscard mount option should be used.

By default, the nodiscard mount option is used.

11. norecovery
If the norecovery mount option is used, the Btrfs filesystem will not try to perform the data recovery operation at mount time.

12. usebackuproot and nousebackuproot
If the usebackuproot mount option is used, the Btrfs filesystem will try to recover any bad/corrupted tree root at mount time. The Btrfs filesystem may store multiple tree roots in the filesystem. The usebackuproot mount option will scan for a good tree root and use the first good one it finds.

The nousebackuproot mount option will not check or recover bad/corrupted tree roots at mount time. This is the default behavior of the Btrfs filesystem.

13. space_cache, space_cache=version, nospace_cache, and clear_cache
The space_cache mount option is used to control the free space cache. Free space cache is used to improve the performance of reading the block group free space of the Btrfs filesystem into memory (RAM).

The Btrfs filesystem supports 2 versions of the free space cache: v1 (default) and v2

The v2 free space caching mechanism improves the performance of big filesystems (multi terabytes in size).

You can use the mount option space_cache=v1 to set the v1 of the free space cache and the mount option space_cache=v2 to set the v2 of the free space cache.

The clear_cache mount option is used to clear the free space cache.

When the v2 free space cache is created, the cache must be cleared to create a v1 free space cache.

So, to use the v1 free space cache after the v2 free space cache is created, the clear_cache and space_cache=v1 mount options must be combined: clear_cache,space_cache=v1

The nospace_cache mount option is used to disable free space caching.

To disable the free space caching after the v1 or v2 cache is created, the nospace_cache and clear_cache mount option must be combined: clear_cache,nosapce_cache

14. skip_balance
By default, interrupted/paused balance operation of a multi-device Btrfs filesystem or Btrfs RAID will be automatically resumed once the Btrfs filesystem is mounted. To disable automatic resuming of interrupted/paused balance operation on a multi-device Btrfs filesystem or Btrfs RAID, you can use the skip_balance mount option.

15. datacow and nodatacow
The datacow mount option enables the Copy-on-Write (CoW) feature of the Btrfs filesystem. It is the default behavior.

If you want to disable the Copy-on-Write (CoW) feature of the Btrfs filesystem for the newly created files, mount the Btrfs filesystem with the nodatacow mount option.

16. datasum and nodatasum
The datasum mount option enables data checksumming for newly created files of the Btrfs filesystem. This is the default behavior.

If you don’t want the Btrfs filesystem to checksum the data for newly created files, mount the Btrfs filesystem with the nodatasum mount option.

Conclusion

This article has shown you how to mount a Btrfs filesystem with your desired mount options. I have explained some of the useful Btrfs mount options as well.

References

[1] The Btrfs Mount Options Manpage – man 5 btrfs

]]>
How to Encrypt a Btrfs Filesystem? https://linuxhint.com/encrypt-a-btrfs-filesystem/ Sat, 30 Jan 2021 16:31:52 +0000 https://linuxhint.com/?p=88201

The Btrfs filesystem-level encryption feature is still not available. But you can use a 3rd party encryption tool like dm-crypt to encrypt the entire storage devices of your Btrfs filesystem.

In this article, I am going to show you how to encrypt the storage devices added to a Btrfs filesystem with dm-crypt. So, let’s get started.

Abbreviations

  • LUKS – Linux Unified Key Setup
  • HDD – Hard Disk Drive
  • SSD – Solid-State Drive

Prerequisites

To follow this article:

  • You must be running either Fedora 33 Workstation or Ubuntu 20.04 LTS Linux distribution on your computer.
  • You must have a free HDD/SSD on your computer.

As you can see, I have an HDD sdb on my Ubuntu 20.04 LTS machine. I will encrypt it and format it with the Btrfs filesystem.

$ sudo lsblk -e7

Installing Required Packages on Ubuntu 20.04 LTS

To encrypt storage devices and format them with the Btrfs filesystem, you need to have the btrfs-progs and cryptsetup packages installed on your Ubuntu 20.04 LTS machine. Luckily, these packages are available in the official package repository of Ubuntu 20.04 LTS.

First, update the APT package repository cache with the following command:

$ sudo apt update


To install btrfs-progs and cryptsetup, run the following command:

$ sudo apt install btrfs-progs cryptsetup --install-suggests


To confirm the installation, press Y and then press <Enter>.


The btrfs-progs and cryptsetup packages and their dependencies are being installed.


The btrfs-progs and cryptsetup packages should be installed at this point.

Installing Required Packages on Fedora 33

To encrypt storage devices and format them with the Btrfs filesystem, you need to have the btrfs-progs and cryptsetup packages installed on your Fedora 33 Workstation machine. Luckily, these packages are available in the official package repository of Fedora 33 Workstation.

First, update the DNF package repository cache with the following command:

$ sudo dnf makecache


To install btrfs-progs and cryptsetup, run the following command:

$ sudo dnf install btrfs-progs cryptsetup -y


Fedora 33 Workstation uses the Btrfs filesystem by default. So, it’s more likely that you will have these packages installed already, as you can see in the screenshot below. If for some reason, they are not installed, they will be installed.

Generating an Encryption Key

Before you can encrypt your storage devices with cryptsetup, you need to generate a 64 bytes long random key.

You can generate your encryption key and store it in the /etc/cryptkey file with the following command:

$ sudo dd if=/dev/urandom of=/etc/cryptkey bs=64 count=1


A new encryption key should be generated and stored in the /etc/cryptkey file.


The encryption key file /etc/cryptkey can be read by everyone by default, as you can see in the screenshot below. This is a security risk. We want only the root user to be able to read/write to the /etc/cryptkey file.

$ ls -lh /etc/cryptkey


To allow only the root user to read/write to the /etc/cryptkey file, change the file permissions as follows:

$ sudo chmod -v 600 /etc/cryptkey


As you can see, only the root user has read/write (rw) permission to the /etc/cryptkey file. So, no one else can see what’s in the /etc/cryptkey file.

$ ls -lh /etc/cryptkey

Encrypting the Storage Devices with dm-crypt

Now that you have generated an encryption key, you can encrypt your storage device. let’s say,  sdb, with the LUKS v2 (version 2) disk encryption technology as follows:

$ sudo cryptsetup -v --type luks2 luksFormat /dev/sdb /etc/cryptkey

cryptsetup will prompt you to confirm the encryption operation.

NOTE: All the data of your HDD/SSD should be removed. So, make sure to move all of your important data before you attempt to encrypt your HDD/SSD.


To confirm the disk encryption operation, type in YES (in uppercase) and press <Enter>. It may take a while to complete.


At this point, the storage device /dev/sdb should be encrypted with the encryption key /etc/cryptkey.

Opening Encrypted Storage Devices

Once you’ve encrypted a storage device with cryptsetup, you need to open it with the cryptsetup tool to be able to use it.

You can open the encrypted storage device sdb and map it to your computer as a data storage device as follows:

$ sudo cryptsetup open --key-file=/etc/cryptkey --type luks2 /dev/sdb data


Now, the decrypted storage device will be available in the path /dev/mapper/data. You have to create your desired filesystem in the /dev/mapper/data device and mount the /dev/mapper/data device instead of /dev/sdb from now on.

Creating Btrfs Filesystem on Encrypted Devices:

To create a Btrfs filesystem on the decrypted storage device /dev/mapper/data with the label data, run the following command:

$ sudo mkfs.btrfs -L data /dev/mapper/data


A Btrfs filesystem should be created on the /dev/mapper/data storage device, which is decrypted from the storage device /dev/sdb (encrypted with LUKS 2).

Mounting Encrypted Btrfs Filesystem

You can mount the Btrfs filesystem you have created earlier as well.

Let’s say, you want to mount the Btrfs filesystem you’ve created earlier in the /data directory.

So, create the /data directory as follows:

$ sudo mkdir -v /data


To mount the Btrfs filesystem created on the /dev/mapper/data storage device in the /data directory, run the following command:

$ sudo mount /dev/mapper/data /data


As you can see, the Btrfs filesystem created on the encrypted storage device sdb is mounted in the /data directory.

$ sudo btrfs filesystem show /data

Automatically Mounting Encrypted Btrfs Filesystem at Boot-Time

You can mount the encrypted Btrfs filesystem at boot time as well.

To mount the encrypted Btrfs filesystem at boot time, you need to:

  • decrypt the storage device /dev/sdb at boot time using the /etc/cryptkey encryption key file
  • mount the decrypted storage device /dev/mapper/data to the /data directory

First, find the UUID of the sdb encrypted storage device with the following command:

$ sudo blkid /dev/sdb


As you can see, the UUID of the sdb encrypted storage device is 1c66b0de-b2a3-4d28-81c5-81950434f972. It will be different for you. So, make sure to change it with yours from now on.


To automatically decrypt the sdb storage device at boot time, you have to add an entry for it on the /etc/crypttab file.

Open the /etc/crypttab file with the nano text editor as follows:

$ sudo nano /etc/crypttab


Add the following line at the end of the /etc/crypttab file if you’re using an HDD.

data UUID=1c66b0de-b2a3-4d28-81c5-81950434f972 /etc/cryptkey luks,noearly

Add the following line at the end of the /etc/crypttab file if you’re using an SSD.

data UUID=1c66b0de-b2a3-4d28-81c5-81950434f972 /etc/cryptkey luks,noearly,discard

Once you’re done, press <Ctrl> + X, followed by Y, and <Enter> to save the /etc/crypttab file.


Now, find the UUID of the decrypted /dev/mapper/data storage device with the following command:

$ sudo blkid /dev/mapper/data


As you can see, the UUID of the /dev/mapper/data decrypted storage device is dafd9d61-bdc9-446a-8b0c-aa209bfab98d. It will be different for you. So, make sure to change it with yours from now on.


To automatically mount the decrypted storage device /dev/mapper/data in the /data directory at boot time, you have to add an entry for it on the /etc/fstab file.

Open the /etc/fstab file with the nano text editor as follows:

$ sudo nano /etc/fstab


Now, add the following line at the end of the /etc/fstab file:

UUID=dafd9d61-bdc9-446a-8b0c-aa209bfab98d /data btrfs defaults 0 0

Once you’re done, press <Ctrl> + X, followed by Y, and <Enter> to save the /etc/fstab file.


Finally, reboot your computer for the changes to take effect.

$ sudo reboot


The encrypted storage device sdb is decrypted into a data storage device, and the data storage device is mounted in the /data directory.

$ sudo lsblk -e7


As you can see, the Btrfs filesystem, which was created on the decrypted /dev/mapper/data storage device is mounted in the /data directory.

$ sudo btrfs filesystem show /data

Conclusion

In this article, I have shown you how to encrypt a storage device using the LUKS 2 encryption technology with cryptsetup. You also learn how to decrypt the encrypted storage device and format it with the Btrfs filesystem as well. As well as how to automatically decrypt the encrypted storage device and mount it at boot time. This article should help you get started with Btrfs filesystem encryption.

]]>
How to Use Btrfs Balance? https://linuxhint.com/how-to-use-btrfs-balance/ Sun, 24 Jan 2021 15:22:36 +0000 https://linuxhint.com/?p=87342 The Btrfs filesystem has built-in multi-device support, so you can create different levels of RAID using it.

Once you’ve created a Btrfs RAID, you can add more storage devices to the RAID to expand the RAID. But, once you have added more storage devices to the RAID, Btrfs won’t spread the existing data/metadata/system-data to the new storage devices automatically. So, you may not get the desired throughput (read/write speed) out of the RAID, and it may not be able to populate the new storage devices with the required redundant data. So, the RAID array may fail to survive the desired number of drive failures.

To solve these problems, the Btrfs filesystem provides a built-in balancing tool. The Btrfs balance utility will spread the data/metadata/system-data of the existing storage devices of the RAID to the newly added storage devices.

In this article, I am going to show you how to use the Btrfs balance utility to spread the data/metadata/system-data of the existing storage devices of the RAID to the newly added storage devices. So, let’s get started!

Abbreviations

RAID – Redundant Array of Inexpensive/Independent Disks
MB – Megabyte
GB – Gigabyte

Prerequisites

To follow this article, you need to have a working Btrfs RAID or multi-device setup.

I have created a Btrfs RAID in RAID-0 configuration using 4 storage devices sdb, sdc, sdd, and sde.

As you can see, the Btrfs filesystem allocated 1 GB of disk space for data1 256 MB of disk space for metadata2, and 4 MB of disk space for system-data3 from each of the storage devices in the RAID.

About 18.75 GB out of 20 GB is still unallocated4 from each of the storage devices of the RAID.

$ sudo btrfs filesystem usage /data

Writing a Script to Generate Random Files

To show you how the Btrfs balance utility works, we need to generate some random files to fill up the Btrfs filesystem. Let’s create a shell script that does just that.

Create a new shell script genfiles.sh in the /usr/local/bin/ directory as follows:

$ sudo nano /usr/local/bin/genfiles.sh

Type in the following lines of codes in the genfiles.sh shell script.

#!/bin/bash
while true
do
    FILENAME=$(uuidgen)
    echo "[Creating] $FILENAME"
    dd if=/dev/random of=$FILENAME bs=1M count=256 status=progress
    echo "[Created] $FILENAME"
done

Once you’re done, press <Ctrl> + X followed by Y and <Enter> to save the genfiles.sh shell script.

The genfiles.sh shell script runs an infinite while loop.

while true
do
    # other codes
done

The following line generates a UUID using the uuidgen command and stores the UUID in the FILENAME variable.

The following line prints a message on the console before the file FILENAME is generated.

The following line generates a new random file FILENAME using the dd command. The file will be 256 MB in size.

The following line prints a message on the console after the file FILENAME is generated.

Add execute permission to the genfiles.sh shell script as follows:

$ sudo chmod +x /usr/local/bin/genfiles.sh

The genfiles.sh shell script should now be accessible as any other commands.

$ which genfiles.sh

Generating Random Files in the Btrfs Filesystem

We want to generate random files in the Btrfs RAID. Let’s say, the Btrfs RAID is mounted on the /data directory.

Navigate to the /data directory where the Btrfs RAID is mounted as follows:

$ cd /data

As you can see, there are no files available in my Btrfs RAID at the moment.

$ ls -lh

To generate some random files in the current working directory (/data directory in this case), run the genfiles.sh shell script as follows:

$ sudo genfiles.sh

The genfiles.sh shell script should start generating random files in the /data directory.

The genfiles.sh script is generating random files. Let the script run for a couple of minutes, so it fills up about 2-3 GB of disk space of the Btrfs RAID.

When you want to stop the genfiles.sh shell script, press <Ctrl> + C.

As you can see, some random files are generated in the Btrfs RAID.

$ ls -lh


As you can see, the Btrfs RAID allocated 2 GB from each of the storage devices added to the RAID. Previously the Btrfs RAID allocated 1 GB from each of the storage devices added to the RAID.

The unallocated disk space has been reduced from 18.75 GB to 17.75 GB in all the storage devices of the RAID.

$ sudo btrfs filesystem usage /data

Adding Another Storage Device to the Btrfs RAID

To show you how to balance a Btrfs RAID after adding a new storage device, you have to add a new storage device to it.

I have added a new HDD sdf to my computer, which I want to add to the Btrfs RAID mounted on the /data directory. Let’s see how to do it.

$ sudo lsblk -e7

Navigate to a different directory (i.e., HOME directory) from the /data directory as follows:

$ cd

To add the storage device sdf to the Btrfs RAID mounted on the /data directory, run the following command:

$ sudo btrfs device add /dev/sdf /data

As you can see, the storage device sdf is added to the Btrfs RAID. The RAID size has increased from 80 GB to 100 GB.

$ sudo btrfs filesystem usage /data

Balancing the Btrfs RAID

As you can see, the newly added storage device (sdf) of the RAID (mounted on the /data directory) has 20 GB unallocated, and the other storage devices (sdb, sdc, sdd, sde, etc.) have 17.75 GB unallocated.

$ sudo btrfs filesystem usage /data

The data1, metadata2, and system-data3 are only available on the existing storage devices of the RAID, not the newly added storage device.

To spread out the data, metadata, and system-data on all the storage devices of the RAID (including the newly added storage device) mounted on the /data directory, run the following command:

$ sudo btrfs balance start --full-balance /data

It may take a while to spread out the data, metadata, and system-data on all the storage devices of the RAID ifit contains a lot of data.

Once the storage devices of the RAID are properly balanced, you should see the following message.

As you can see, after the balance operation is completed, the newly added storage device has an equal amount of unallocated disk space as the other storage devices of the RAID.

After the balance operation, an equal amount of disk space as the other storage devices of the RAID is allocated for the data, metadata, and system-data from the newly added storage device (sdf) of the RAID.

Conclusion

In this article, I have discussed the purpose of the Btrfs balance utility, as well as how to balance a Btrfs RAID or multi-device filesystem after adding new storage devices to the RAID or multi-device filesystem.

]]>
How to Use Btrfs Scrub? https://linuxhint.com/how-to-use-btrfs-scrub/ Sun, 24 Jan 2021 15:07:21 +0000 https://linuxhint.com/?p=87408 The Btrfs filesystem is a multi-device filesystem that has built-in support for RAID. In a multi-device Btrfs filesystem or RAID, the data/metadata blocks may be stored in one or more storage devices. The Btrfs scrub tool will read all the data/metadata blocks from all the storage devices added to a Btrfs filesystem or RAID and find all the corrupted data/metadata blocks. Once the corrupted data/metadata blocks are found, the Btrfs scrub tool will automatically repair those corrupted data/metadata blocks if possible.

In a multi-device Btrfs filesystem or Btrfs RAID, depending on the filesystem configuration, there may be multiple copies of the data/metadata blocks stored in different locations of the storage devices added to the Btrfs filesystem. When the Btrfs scrub tool finds a corrupted data/metadata block, it searches all the storage devices added to the Btrfs filesystem for duplicate copies of that data/metadata block. Once a duplicate copy of that data/metadata block is found, the corrupted data/metadata block is overwritten with the correct data/metadata block. This is how the Btrfs scrub tool repairs corrupted data/metadata blocks in a multi-device Btrfs filesystem or Btrfs RAID.

In this article, I am going to show you how to use the Btrfs scrub tool to find and repair corrupted data/metadata blocks in a multi-device Btrfs filesystem or Btrfs RAID. So, let’s get started.

Abbreviations

RAID – Redundant Array of Inexpensive/Independent Disks
GB – Gigabyte

Prerequisites

To follow this article, you need to have a working multi-device Btrfs filesystem or a Btrfs RAID.

I have created a Btrfs RAID in RAID-1 configuration (mounted on the /data directory) using 4 storage devices sdb, sdc, sdd, and sde, as you can see in the screenshot below. I will be using this Btrfs RAID for the Btrfs scrub demonstration in this article.

$ sudo btrfs filesystem usage /data

If you need any assistance on installing the Btrfs filesystem on Ubuntu, check my article Install and Use Btrfs on Ubuntu 20.04 LTS.

If you need any assistance on installing the Btrfs filesystem on Fedora, check my article Install and Use Btrfs on Fedora 33.

If you need any assistance in creating a Btrfs RAID, check my article How to Setup Btrfs RAID.

Generating Dummy Files on the Btrfs Filesystem

To show you how the Btrfs scrub tool works, we need to generate some random files to fill up the Btrfs filesystem. Let’s create a shell script that does just that.

Create a new shell script genfiles.sh in the /usr/local/bin/ directory as follows:

$ sudo nano /usr/local/bin/genfiles.sh

Type in the following lines of codes in the genfiles.sh shell script.

#!/bin/bash
while true
do
    FILENAME=$(uuidgen)
    echo "[Creating] $FILENAME"
    dd if=/dev/random of=$FILENAME bs=1M count=256 status=progress
    echo "[Created] $FILENAME"
done

Once you’re done, press <Ctrl> + X followed by Y and <Enter> to save the genfiles.sh shell script.

The genfiles.sh shell script runs an infinite while loop.

while true
do
    # other codes
done

The following line generates a UUID using the uuidgen command and stores the UUID in the FILENAME variable.

The following line prints a message on the console before the file FILENAME is generated.

The following line generates a new random file FILENAME using the dd command. The file will be 256 MB in size.

The following line prints a message on the console after the file FILENAME is generated.

Add execute permission to the genfiles.sh shell script as follows:

s

The genfiles.sh shell script should now be accessible as any other commands.

$ which genfiles.sh

Let’s generate some random files in the Btrfs RAID mounted in the /data directory.

Navigate to the /data directory where the Btrfs RAID is mounted as follows:

$ cd /data

As you can see, there are no files available in my Btrfs RAID at the moment.

$ ls -lh

To generate some random files in the current working directory (/data directory in this case), run the genfiles.sh shell script as follows:

$ sudo genfiles.sh

The genfiles.sh shell script should start generating random files in the /data directory.

The genfiles.sh script is generating random files. Let the script run for a couple of minutes, so it fills up about 2-3 GB of disk space of the Btrfs RAID.

When you want to stop the genfiles.sh shell script, press <Ctrl> + C.

As you can see, some random files are generated in the Btrfs RAID.

$ ls -lh

I have generated about 13 GB of random files in the Btrfs RAID mounted in the /data directory, as you can see in the screenshot below.

$ sudo du -sh /data

Working with the Btrfs Scrub Tool

In this section, I am going to show you how to use the Btrfs scrub tool. Let’s get started.

You can start the scrub process on the Btrfs filesystem mounted on the /data directory with the following command:

$ sudo btrfs scrub start /data

A Btrfs scrub process should be started on the Btrfs filesystem mounted on the /data directory.

You can see the status of the Btrfs scrub process running on the Btrfs filesystem mounted on the /data directory as follows:

$ sudo btrfs scrub status /data

As you can see, the Btrfs scrub process is still running.

Scrubbing a Btrfs filesystem or Btrfs RAID that has a lot of files will take a long time to complete.

Once the Btrfs scrub process is complete, the status should be changed to finished, as you can see in the screenshot below.

$ sudo btrfs scrub status /data

You can also see the Btrfs scrub status for each of the storage devices added to the Btrfs filesystem (mounted in the /data directory) separately as follows:

$ sudo btrfs scrub status -d /data

I have told you that the Btrfs scrub process takes a long time to complete on a big Btrfs filesystem. One big advantage of the Btrfs scrub tool is that its process can be paused and resumed at any time.

Let’s see how to pause and resume a Btrfs scrub process.

First, start a new Btrfs scrub process on the Btrfs filesystem mounted in the /data directory as follows:

$ sudo btrfs scrub start /data

To cancel or pause the Btrfs scrub process that is currently running on the Btrfs filesystem mounted on the /data directory, run the following command:

$ sudo btrfs scrub cancel /data

The running Btrfs scrub process should be canceled or paused.

As you can see, the Btrfs scrub status is aborted. So, the Btrfs scrub process is not running anymore.

$ sudo btrfs scrub status /data

To resume the Btrfs scrub process that you’ve canceled or paused, run the following command:

$ sudo btrfs scrub resume /data

The Btrfs scrub process should be resumed.

As you can see, the Btrfs scrub status is now running. So, the Btrfs scrub process is resumed.

$ sudo btrfs scrub status /data

After the Btrfs scrub process is complete, the Btrfs scrub status should be changed to finished.

$ sudo btrfs scrub status /data

Conclusion

In this article, I have shown you how to work with the Btrfs scrub tool to find and fix corrupted data/metadata blocks of a Btrfs multi-device filesystem or RAID. I have shown you how to cancel/pause and resume a Btrfs scrub process once it’s started as well.

]]>
How to Set Up Btrfs RAID https://linuxhint.com/set-up-btrfs-raid/ Mon, 18 Jan 2021 16:09:35 +0000 https://linuxhint.com/?p=86014 Btrfs is a modern Copy-on-Write (CoW) filesystem with built-in RAID support. So, you do not need any third-party tools to create software RAIDs on a Btrfs filesystem.

The Btrfs filesystem keeps the filesystem metadata and data separately. You can use different RAID levels for the data and metadata at the same time. This is a major advantage of the Btrfs filesystem.

This article shows you how to set up Btrfs RAIDs in the RAID-0, RAID-1, RAID-1C3, RAID-1C4, RAID-10, RAID-5, and RAID-6 configurations.

Abbreviations

  • Btrfs – B-tree Filesystem
  • RAID – Redundant Array of Inexpensive Disks/Redundant Array of Independent Disks
  • GB – Gigabyte
  • TB – Terabyte
  • HDD – Hard Disk Drive
  • SSD – Solid-State Drive

Prerequisites

To try out the examples included in this article:

  • You must have the Btrfs filesystem installed on your computer.
  • You will need at least four same-capacity HDDs/SSDs to try out the different RAID configurations.

In my Ubuntu machine, I have added four HDDs (sdb, sdc, sdd, sde). Each of them is 20 GB in size.

$ sudo lsblk -e7

Note: Your HDDs/SSDs may have different names than mine. So, be sure to replace them with yours from now on.


For assistance with installing the Btrfs filesystem in Ubuntu, check out the article Install and Use Btrfs on Ubuntu 20.04 LTS.

For assistance with installing the Btrfs filesystem in Fedora, check out the article Install and Use Btrfs on Fedora 33.

Btrfs Profiles

A Btrfs profile is used to tell the Btrfs filesystem how many copies of the data/metadata to keep and what RAID levels to use for the data/metadata. The Btrfs filesystem contains many profiles. Understanding them will help you to configure a Btrfs RAID just the way you want.

The available Btrfs profiles are as follows:

single: If the single profile is used for the data/metadata, only one copy of the data/metadata will be stored in the filesystem, even if you add multiple storage devices to the filesystem. So, 100% of the disk space of each of the storage devices added to the filesystem can be utilized.

dup: If the dup profile is used for the data/metadata, each of the storage devices added to the filesystem will keep two copies of the data/metadata. So, 50% of the disk space of each of the storage devices added to the filesystem can be utilized.

raid0: In the raid0 profile, the data/metadata will be split evenly across all the storage devices added to the filesystem. In this setup, there will be no redundant (duplicate) data/metadata. So, 100% of the disk space of each of the storage devices added to the filesystem can be used. If in any case one of the storage devices fails, the entire filesystem will be corrupted. You will need at least two storage devices to set up the Btrfs filesystem in the raid0 profile.

raid1: In the raid1 profile, two copies of the data/metadata will be stored in the storage devices added to the filesystem. In this setup, the RAID array can survive one drive failure. But, you can use only 50% of the total disk space. You will need at least two storage devices to set up the Btrfs filesystem in the raid1 profile.

raid1c3: In the raid1c3 profile, three copies of the data/metadata will be stored in the storage devices added to the filesystem. In this setup, the RAID array can survive two drive failures, but you can use only 33% of the total disk space. You will need at least three storage devices to set up the Btrfs filesystem in the raid1c3 profile.

raid1c4: In the raid1c4 profile, four copies of the data/metadata will be stored in the storage devices added to the filesystem. In this setup, the RAID array can survive three drive failures, but you can use only 25% of the total disk space. You will need at least four storage devices to set up the Btrfs filesystem in the raid1c4 profile.

raid10: In the raid10 profile, two copies of the data/metadata will be stored in the storage devices added to the filesystem, as in the raid1 profile. Also, the data/metadata will be split across the storage devices, as in the raid0 profile.

The raid10 profile is a hybrid of the raid1 and raid0 profiles. Some of the storage devices form raid1 arrays and some of these raid1 arrays are used to form a raid0 array. In a raid10 setup, the filesystem can survive a single drive failure in each of the raid1 arrays.

You can use 50% of the total disk space in the raid10 configuration. You will need at least four storage devices to set up the Btrfs filesystem in the raid10 profile.

raid5: In the raid5 profile, one copy of the data/metadata will be split across the storage devices. A single parity will be calculated and distributed among the storage devices of the RAID array.

In a raid5 configuration, the filesystem can survive a single drive failure. If a drive fails, you can add a new drive to the filesystem and the lost data will be calculated from the distributed parity of the running drives.

You can use 100x(N-1)/N % of the total disk spaces in the raid5 configuration. Here, N is the number of storage devices added to the filesystem. You will need at least three storage devices to set up the Btrfs filesystem in the raid5 profile.

raid6: In the raid6 profile, one copy of the data/metadata will be split across the storage devices. Two parities will be calculated and distributed among the storage devices of the RAID array.

In a raid6 configuration, the filesystem can survive two drive failures at once. If a drive fails, you can add a new drive to the filesystem, and the lost data will be calculated from the two distributed parities of the running drives.

You can use 100x(N-2)/N % of the total disk space in the raid6 configuration. Here, N is the number of storage devices added to the filesystem. You will need at least four storage devices to set up the Btrfs filesystem in the raid6 profile.

Creating a Mount Point

You need to create a directory to mount the Btrfs filesystem that you will create in the next sections of this article.

To create the directory/mount point /data, run the following command:

$ sudo mkdir -v /data

Setting Up RAID-0

In this section, you will learn how to set up a Btrfs RAID in the RAID-0 configuration using four HDDs (sdb, sdc, sdd, and sde). The HDDs are 20 GB in size.

$ sudo lsblk -e7

To create a Btrfs RAID in the RAID-0 configuration using four HDDs (sdb, sdc, sdd, and sde) run the following command:

$ sudo mkfs.btrfs -L data -d raid0 -m raid0 -f /dev/sdb /dev/sdc /dev/sdd /dev/sde

Here,

  • The –L option is used to set the filesystem label data.
  • The –d option is used to set the RAID profile raid0 for the filesystem data.
  • The –m option is used to set the RAID profile raid0 for the filesystem metadata.
  • The –f option is used to force the creation of the Btrfs filesystem, even if any of the HDDs have an existing filesystem.

The Btrfs filesystem data in the RAID-0 configuration should now be created, as you can see in the screenshot below.

You can mount the Btrfs RAID using any HDD/SSD you used to create the RAID.

For example, I used the HDDs sdb, sdc, sdd, and sde to create the Btrfs RAID in the RAID-0 configuration.

So, I can mount the Btrfs filesystem data in the /data directory using the HDD sdb, as follows:

$ sudo mount /dev/sdb /data

As you can see, the Btrfs RAID is mounted in the /data directory.

$ sudo df -h /data

To find the filesystem usage information of the data Btrfs filesystem mounted in the /data directory, run the following command:

$ sudo btrfs filesystem usage /data

As you can see,

The RAID size (Device size) is 80 GB (4×20 GB per HDD).

About 78.98 GB (Free (estimated)) of 80 GB of disk space can be used in the RAID-0 configuration.

Only one copy of the data (Data ratio) and one copy of the metadata (Metadata ratio) will be stored in the Btrfs filesystem in the RAID-0 configuration.

As the Btrfs RAID is working, you can unmount it from the /data directory, as follows:

$ sudo umount /data

Setting Up RAID-1

In this section, you will learn how to set up a Btrfs RAID in the RAID-1 configuration using four HDDs (sdb, sdc, sdd, and sde). The HDDs are 20 GB in size.

$ sudo lsblk -e7

To create a Btrfs RAID in the RAID-1 configuration using four HDDs (sdb, sdc, sdd, and sde), run the following command:

$ sudo mkfs.btrfs -L data -d raid1 -m raid1 -f /dev/sdb /dev/sdc /dev/sdd /dev/sde

Here,

  • The –L option is used to set the filesystem label data.
  • The –d option is used to set the RAID profile raid1 for the filesystem data.
  • The –m option is used to set the RAID profile raid1 for the filesystem metadata.
  • The –f option is used to force the creation of the Btrfs filesystem, even if any of the HDDs have an existing filesystem.

The Btrfs filesystem data in the RAID-1 configuration should now be created, as you can see in the screenshot below.

You can mount the Btrfs RAID using any HDD/SSD you used to create the RAID.

For example, I used the HDDs sdb, sdc, sdd, and sde to create the Btrfs RAID in the RAID-1 configuration.

I can mount the Btrfs filesystem data in the /data directory using the HDD sdb, as follows:

$ sudo mount /dev/sdb /data

As you can see, the Btrfs RAID is mounted in the /data directory.

$ sudo df -h /data

To find the filesystem usage information of the data Btrfs filesystem mounted in the /data directory, run the following command:

$ sudo btrfs filesystem usage /data

As you can see,

The RAID size (Device size) is 80 GB (4×20 GB per HDD).

About 38.99 GB (Free (estimated)) of 80 GB of disk space can be used in the RAID-1 configuration.

In the RAID-1 configuration, two copies of the data (Data ratio) and two copies of the metadata (Metadata ratio) will be stored in the Btrfs filesystem.

As the Btrfs RAID is working, you can unmount it from the /data directory, as follows:

$ sudo umount /data

Setting Up RAID-1C3

In this section, you will learn how to set up a Btrfs RAID in the RAID-1C3 configuration using four HDDs (sdb, sdc, sdd, and sde). The HDDs are 20 GB in size

$ sudo lsblk -e7

To create a Btrfs RAID in the RAID-1C3 configuration using the four HDDs sdb, sdc, sdd, and sde, run the following command:

$ sudo mkfs.btrfs -L data -d raid1c3 -m raid1c3 -f /dev/sdb /dev/sdc /dev/sdd /dev/sde

Here,

  • The –L option is used to set the filesystem label data.
  • The –d option is used to set the RAID profile raid1c3 for the filesystem data.
  • The –m option is used to set the RAID profile raid1c3 for the filesystem metadata.
  • The –f option is used to force the creation of the Btrfs filesystem, even if any of the HDDs have an existing filesystem.

The Btrfs filesystem data in the RAID-1C3 configuration should now be created, as you can see in the screenshot below.

You can mount the Btrfs RAID using any HDD/SSD you used to create the RAID.

For example, I used the HDDs sdb, sdc, sdd, and sde to create the Btrfs RAID in the RAID-1C3 configuration.

So, I can mount the Btrfs filesystem data in the /data directory using the HDD sdb, as follows:

$ sudo mount /dev/sdb /data

As you can see, the Btrfs RAID is mounted in the /data directory.

$ sudo df -h /data

To find the filesystem usage information of the data Btrfs filesystem mounted in the /data directory, run the following command:

$ sudo btrfs filesystem usage /data

As you can see,

The RAID size (Device size) is 80 GB (4×20 GB per HDD).

About 25.66 GB (Free (estimated)) of 80 GB of disk space can be used in the RAID-1C3 configuration.

In the RAID-1C3 configuration, three copies of the data (Data ratio) and three copies of the metadata (Metadata ratio) will be stored in the Btrfs filesystem.

As the Btrfs RAID is working, you can unmount it from the /data directory, as follows:

$ sudo umount /data

Setting Up RAID-1C4

In this section, you will learn how to set up a Btrfs RAID in the RAID-1C4 configuration using the four HDDs sdb, sdc, sdd, and sde. The HDDs are 20 GB in size.

$ sudo lsblk -e7

To create a Btrfs RAID in the RAID-1C4 configuration using the four HDDs sdb, sdc, sdd, and sde, run the following command:

$ sudo mkfs.btrfs -L data -d raid1c4 -m raid1c4 -f /dev/sdb /dev/sdc /dev/sdd /dev/sde

Here,

  • The –L option is used to set the filesystem label data.
  • The –d option is used to set the RAID profile raid1c4 for the filesystem data.
  • The –m option is used to set the RAID profile raid1c4 for the filesystem metadata.
  • The –f option is used to force the creation of the Btrfs filesystem, even if any of the HDDs have an existing filesystem.

The Btrfs filesystem data in the RAID-1C4 configuration should now be created, as you can see in the screenshot below.

You can mount the Btrfs RAID using any HDD/SSD you used to create the RAID.

For example, I used the HDDs sdb, sdc, sdd, and sde to create the Btrfs RAID in the RAID-1C4 configuration.

So, I can mount the Btrfs filesystem data in the /data directory using the HDD sdb, as follows:

$ sudo mount /dev/sdb /data

As you can see, the Btrfs RAID is mounted in the /data

$ sudo df -h /data

To find the filesystem usage information of the data Btrfs filesystem mounted in the /data

$ sudo btrfs filesystem usage /data

As you can see,

The RAID size (Device size) is 80 GB (4×20 GB per HDD).

About 18.99 GB (Free (estimated)) of 80 GB of disk space can be used in the RAID-1C4 configuration.

In the RAID-1C4 configuration, four copies of the data (Data ratio) and four copies of the metadata (Metadata ratio) will be stored in the Btrfs filesystem.

As the Btrfs RAID is working, you can unmount it from the /data directory, as follows:

$ sudo umount /data

Setting Up RAID-10

In this section, you will learn how to set up a Btrfs RAID in the RAID-10 configuration using the four HDDs sdb, sdc, sdd, and sde. The HDDs are 20 GB in size.

$ sudo lsblk -e7

To create a Btrfs RAID in the RAID-10 configuration using the four HDDs sdb, sdc, sdd, and sde, run the following command:

$ sudo mkfs.btrfs -L data -d raid10 -m raid10 -f /dev/sdb /dev/sdc /dev/sdd /dev/sde

Here,

  • The –L option is used to set the filesystem label data.
  • The –d option is used to set the RAID profile raid10 for the filesystem data.
  • The –m option is used to set the RAID profile raid10 for the filesystem metadata.
  • The –f option is used to force the creation of the Btrfs filesystem, even if any of the HDDs have an existing filesystem.

The Btrfs filesystem data in the RAID-10 configuration should now be created, as you can see in the screenshot below.

You can mount the Btrfs RAID using any HDD/SSD you used to create the RAID.

For example, I used the HDDs sdb, sdc, sdd, and sde to create the Btrfs RAID in the RAID-10 configuration.

So, I can mount the Btrfs filesystem data in the /data directory using the HDD sdb, as follows:

$ sudo mount /dev/sdb /data

As you can see, the Btrfs RAID is mounted in the /data directory.

$ sudo df -h /data

To find the filesystem usage information of the data Btrfs filesystem mounted in the /data directory, run the following command:

$ sudo btrfs filesystem usage /data

As you can see,

The RAID size (Device size) is 80 GB (4×20 GB per HDD).

About 39.48 GB (Free (estimated)) of 80 GB of disk space can be used in the RAID-10 configuration.

In the RAID-10 configuration, two copies of the data (Data ratio) and two copies of the metadata (Metadata ratio) will be stored in the Btrfs filesystem.

As the Btrfs RAID is working, you can unmount it from the /data directory, as follows:

$ sudo umount /data

Setting Up RAID-5

In this section, you will learn how to set up a Btrfs RAID in the RAID-5 configuration using the four HDDs sdb, sdc, sdd, and sde. The HDDs are 20 GB in size.

$ sudo lsblk -e7

To create a Btrfs RAID in the RAID-5 configuration using the four HDDs sdb, sdc, sdd, and sde, run the following command:

$ sudo mkfs.btrfs -L data -d raid5 -m raid5 -f /dev/sdb /dev/sdc /dev/sdd /dev/sde

Here,

  • The –L option is used to set the filesystem label data.
  • The –d option is used to set the RAID profile raid5 for the filesystem data.
  • The –m option is used to set the RAID profile raid5 for the filesystem metadata.
  • The –f option is used to force the creation of the Btrfs filesystem, even if any of the HDDs have an existing filesystem.

The Btrfs filesystem data in the RAID-5 configuration should now be created, as you can see in the screenshot below.

You can mount the Btrfs RAID using any HDD/SSD you used to create the RAID.

For example, I used the HDDs sdb, sdc, sdd, and sde to create the Btrfs RAID in the RAID-5 configuration.

So, I can mount the Btrfs filesystem data in the /data directory using the HDD sdb, as follows:

$ sudo mount /dev/sdb /data

As you can see, the Btrfs RAID is mounted in the /data directory.

$ sudo df -h /data

To find the filesystem usage information of the data Btrfs filesystem mounted in the /data directory, run the following command:

$ sudo btrfs filesystem usage /data

As you can see,

The RAID size (Device size) is 80 GB (4×20 GB per HDD).

About 59.24 GB (Free (estimated)) of 80 GB of disk space can be used in the RAID-5 configuration.

In the RAID-5 configuration, 1.33 copies of the data (Data ratio) and 1.33 copies of the metadata (Metadata ratio) will be stored in the Btrfs filesystem.

As the Btrfs RAID is working, you can unmount it from the /data directory, as follows:

$ sudo umount /data

Setting Up RAID-6

In this section, you will learn how to set up a Btrfs RAID in the RAID-6 configuration using the four HDDs sdb, sdc, sdd, and sde. The HDDs are 20 GB in size.

$ sudo lsblk -e7

To create a Btrfs RAID in the RAID-6 configuration using the four HDDs sdb, sdc, sdd, and sde, run the following command:

$ sudo mkfs.btrfs -L data -d raid6 -m raid6 -f /dev/sdb /dev/sdc /dev/sdd /dev/sde

Here,

  • The –L option is used to set the filesystem label data.
  • The –d option is used to set the RAID profile raid6 for the filesystem data.
  • The –m option is used to set the RAID profile raid6 for the filesystem metadata.
  • The –f option is used to force the creation of the Btrfs filesystem, even if any of the HDDs have an existing filesystem.

The Btrfs filesystem data in the RAID-6 configuration should now be created, as you can see in the screenshot below.

You can mount the Btrfs RAID using any HDD/SSD you used to create the RAID.

For example, I used the HDDs sdb, sdc, sdd, and sde to create the Btrfs RAID in the RAID-6 configuration.

So, I can mount the Btrfs filesystem data in the /data directory using the HDD sdb, as follows:

$ sudo mount /dev/sdb /data

As you can see, the Btrfs RAID is mounted in the /data directory.

$ sudo df -h /data

To find the filesystem usage information of the data Btrfs filesystem mounted in the /data directory, run the following command:

$ sudo btrfs filesystem usage /data

As you can see,

The RAID size (Device size) is 80 GB (4×20 GB per HDD).

About 39.48 GB (Free (estimated)) of 80 GB of disk space can be used in the RAID-6 configuration.

In the RAID-6 configuration, two copies of the data (Data ratio) and two copies of the metadata (Metadata ratio) will be stored in the Btrfs filesystem.

As the Btrfs RAID is working, you can unmount it from the /data directory, as follows:

$ sudo umount /data

Problems with Btrfs RAID-5 and RAID-6

The built-in Btrfs RAID-5 and RAID-6 configurations are still experimental. These configurations are very unstable and you should not use them in production.

To prevent data corruption, the Ubuntu operating system did not implement RAID-5 and RAID-6 for the Btrfs filesystem. So, you will not be able to create a Btrfs RAID in the RAID-5 and RAID-6 configurations using the built-in RAID feature of the Btrfs filesystem on Ubuntu. That is why I have shown you how to create a Btrfs RAID in the RAID-5 and RAID-6 configurations in Fedora 33, instead of Ubuntu 20.04 LTS.

Mounting a Btrfs RAID Automatically on Boot

To mount a Btrfs RAID automatically at boot time using the /etc/fstab file, you will need to know the UUID of the Btrfs filesystem.

You can find the UUID of a Btrfs filesystem with the following command:

$ sudo blkid --match-token TYPE=btrfs

As you can see, the UUID of the storage devices that are added to the Btrfs filesystem for configuring the RAID is the same.

In my case, it is c69a889a-8fd2-4571-bd97-a3c2e4543b6b. It will be different for you. So, be sure to replace this UUID with yours from now on.

Now, open the /etc/fstab file with the nano text editor, as follows:

$ sudo nano /etc/fstab

Add the following line to the end of the /etc/fstab file.

UUID=<strong>c69a889a-8fd2-4571-bd97-a3c2e4543b6b</strong>  /data   btrfs   defaults    0   0

Once you are finished, press <Ctrl> + X followed by Y and <Enter> to save the /etc/fstab file.

For the changes to take effect, restart your computer, as follows:

$ sudo reboot

As you can see, the Btrfs RAID is correctly mounted in the /data directory.

$ df -h /data

As you can see, the Btrfs RAID mounted in the /data directory is working just fine.

$ sudo btrfs filesystem usage /data

Conclusion

This article explained various Btrfs RAID profiles in detail. The article showed you how to set up a Btrfs RAID in the RAID-0, RAID-1, RAID-1C3, RAID-1C4, RAID-10, RAID-5, and RAID-6 configurations. You also learned about some of the problems with the Btrfs RAID-5 and RAID-6 configurations, as well as how to mount the Btrfs RAID automatically at boot time.

References

]]>
How to Defragment a Btrfs Filesystem https://linuxhint.com/defragment-btrfs-filesystem/ Fri, 15 Jan 2021 20:40:50 +0000 https://linuxhint.com/?p=85810

Btrfs is an extent-based Copy-on-Write (CoW) filesystem. Large files are stored in multiple data extents; and when these large files are modified, the extents to be modified are copied to new, empty extents in another location of the storage device and are modified in the new location. Then, the extents of the large files are re-linked to point to the updated extents. The old extents are never removed instantly.

This is how the Copy-on-Write (CoW) feature of the Btrfs filesystem causes fragmentation. Fragmentation means that the data extents of large files are scattered around the entire storage device. They are not instantaneous. So, the performance (read/write speed) of the filesystem may be reduced.

To solve this problem, it is necessary to defragment the Btrfs filesystem every once in a while. This article shows you how to defragment the Btrfs filesystem.

Abbreviations

The abbreviations(short forms) used in this article are as follows:

  • CoW – Copy-on-Write
  • Btrfs – B-tree Filesystem
  • HDD – Hard Disk Drive
  • SSD – Solid-State Drive
  • GB – Gigabyte
  • VM – Virtual Machine

Prerequisites

To try out the examples included in this article:

  • You must have the Btrfs filesystem installed on your computer.
  • You must have a spare HDD/SSD (of any size) or at least 1 free HDD/SSD partition (of any size).

I have a 20 GB HDD sdb on my Ubuntu machine. I will create a Btrfs filesystem on the HDD sdb.

$ sudo lsblk -e7

Note: Your HDD/SSD will likely have a different name than mine, and so will the partitions. So, be sure to replace them with yours from now on.

You can create a Btrfs filesystem on your HDD/SSD (without partitioning) if you have a spare HDD/SSD. You can also create a partition on your HDD/SSD and create a Btrfs filesystem there.

For assistance with installing the Btrfs filesystem in Ubuntu, check out the article Install and Use Btrfs on Ubuntu 20.04 LTS.

For assistance with installing the Btrfs filesystem in Fedora, check out the article Install and Use Btrfs on Fedora 33.

Creating a Btrfs Filesystem

You can create a Btrfs filesystem on your HDD/SSD (unpartitioned) or on your HDD/SSD partition.

To create a Btrfs filesystem on the sdb HDD (entire HDD, no partitions) and give it the filesystem label data, run the following command:

$ sudo mkfs.btrfs -L data /dev/sdb

A Btrfs filesystem should now be created on the sdb HDD.

Create the directory /data to mount the Btrfs filesystem you have just created with the following command:

$ sudo mkdir -v /data

To mount the Btrfs filesystem created on the sdb HDD on the /data directory, run the following command:

$ sudo mount /dev/sdb /data

The Btrfs filesystem should now be mounted, as you can see in the screenshot below:

$ df -h /data

Defragmenting a Btrfs Filesystem Manually

As you can see in the screenshot below, I have copied two files on the Btrfs filesystem mounted on the /data directory to demonstrate the process of Btrfs filesystem defragmentation:

$ ls -lh /data

You can defragment a single file or an entire subvolume/directory recursively.

To defragment the single file /data/ubuntu-20.04.1-live-server-amd64.iso, we will run the following command:

$ sudo btrfs filesystem defragment -vf /data/ubuntu-20.04.1-live-server-amd64.iso

The file /data/ubuntu-20.04.1-live-server-amd64.iso should be defragmented.

To defragment every file or directory of the /data directory recursively, run the following command:

$ sudo btrfs filesystem defragment -rvf /data

As you can see, all the files of the /data directory are defragmented.

In the same way, if you had the subvolume /data/osimages, then you could defragment all the files of the /data/osimages subvolume recursively with the following command:

$ sudo btrfs filesystem defragment -rvf /data/osimages

Compressing a Btrfs Filesystem While Defragmenting

The Btrfs filesystem allows you to compress files while you defragment them.

To defragment all the files in the /data directory and compress them with the ZLIB compression algorithm at the same time, run the defragment command with the -czlib option, as follows:

$ sudo btrfs filesystem defragment -rvf -czlib /data

To defragment all the files in the /data directory and compress them with the ZSTD compression algorithm at the same time, run the defragment command with the -czstd option, as follows:

$ sudo btrfs filesystem defragment -rvf -czstd /data

To defragment all the files in the /data directory and compress them with the LZO compression algorithm at the same time, run the defragment command with the -clzo option, as follows:

$ sudo btrfs filesystem defragment -rvf -clzo /data

The files in the /data directory should be defragmented and compressed at the same time.

In the same way, you can defragment and compress the files of a Btrfs subvolume, as well.

Defragmenting a Btrfs Filesystem Automatically

You can enable automatic defragmentation on your Btrfs filesystem at mount time. This feature of the Btrfs filesystem will defragment all the files of your Btrfs filesystem automatically.

To mount the Btrfs filesystem created on the sdb HDD in the /data directory with automatic defragmentation enabled at boot time, you must add an entry for the Btrfs filesystem in the /etc/fstab file.

First, find the UUID of the Btrfs filesystem created on the sdb HDD, as follows:

$ sudo blkid /dev/sdb

As you can see, the UUID of the Btrfs filesystem created on the sdb HDD is 60afc092-e0fa-4b65-81fd-5dfd7dd884de.

It will be different for you. So, be sure to replace it with yours from now on.

Open the /etc/fstab file with the nano text editor, as follows:

$ sudo nano /etc/fstab

Add the following line to the end of the /etc/fstab file:

UUID=60afc092-e0fa-4b65-81fd-5dfd7dd884de /data     btrfs      autodefrag         0              0

Once you are done, press <Ctrl> + X followed by Y and <Enter> to save the /etc/fstab file.

For the changes to take effect, reboot your computer with the following command:

$ sudo reboot

As you can see, the Btrfs filesystem created on the sdb HDD is mounted on the /data directory with auto defragmentation enabled.

Problems with Defragmenting a Btrfs Filesystem

Though it may seem that defragmentation improves filesystem performance, there are some problems with defragmenting a Btrfs filesystem.

As Btrfs is a Copy-on-Write (CoW) filesystem, to understand the problems with Btrfs filesystem defragmentation, you must understand the Copy-on-Write feature of the Btrfs filesystem.

Suppose, you have a large file (file1) that uses 100 extents (you can think of extents as file blocks) of a Btrfs filesystem. If you create another copy of that large file (file2) in the same Btrfs filesystem, you will see that no additional disk space is used. That is because the files are identical, and the 100 extents of each file are the same. So, the Btrfs filesystem uses the same extents for both files.

Figure 1: file1 and file2 are identical and sharing the same Btrfs filesystem extents to save disk space

Now, say, you have modified one of the copies of the large file (file2). The modification needs to change 10 of the 100 extents. The Btrfs filesystem will copy the required 10 extents in another unused location (say, e101e110) of the filesystem and change them there. Once the changes are written to the disk, the Btrfs filesystem will re-link the extents so that the changes are reflected in the large file. The process is illustrated in the figure below:

Figure 2: 10 extents are changed in file2. So, the extents are re-linked in the Btrfs filesystem.

From figures 1 and 2, you can understand how Copy-on-Write (CoW) works and how the Btrfs filesystem uses Copy-on-Write (CoW) to save disk space.

Now that you know how the Copy-on-Write (CoW) feature of the Btrfs filesystem works, you will understand the problems with defragmenting a Btrfs filesystem.

  1. Defragmenting files move Btrfs data extents and attempt to align them, one after the other. So, the Copy-on-Write links between the copies of the file breaks. This will increase redundant data extents, as well as the disk usage of a Btrfs filesystem that was previously saved by sharing data extents between identical (or nearly identical) copies of the file.
  2. If a Btrfs subvolume has multiple snapshots, defragmenting the subvolume will break the Copy-on-Write links between the subvolume and the snapshots. This will increase disk usage of a Btrfs filesystem.
  3. If you are using the Btrfs filesystem for large databases or virtual machine images (for storing VM data/disks), defragmenting the filesystem will also negatively impact the performance of the filesystem.

Conclusion

In this article, you learned how to defragment a single file and the files in a directory/subvolume recursively of a Btrfs filesystem. You also learned how to enable automatic defragmentation on a Btrfs filesystem at mount time. Finally, the article discussed some of the problems with defragmenting a Btrfs filesystem.

]]>
How to Backup Btrfs Snapshots to External Drives https://linuxhint.com/back_up_btrfs_snapshots_external_drives/ Sun, 10 Jan 2021 14:18:48 +0000 https://linuxhint.com/?p=85110

By default, you can store the snapshots you take of your Btrfs subvolumes in the same Btrfs filesystem, but it is not possible to store the snapshots of one Btrfs filesystem directly to another Btrfs filesystem. However, the Btrfs filesystem provides you with the necessary tools to back up snapshots of one Btrfs filesystem to another Btrfs filesystem. This article shows you how to back up Btrfs snapshots to an external Btrfs filesystem on an external drive.

Prerequisites

To try out the examples included in this article, you must fulfill the following prerequisites:

  • Have the Btrfs filesystem installed on your computer.
  • Have a hard disk or SSD with at least 2 free partitions (of any size).

I have the 20 GB hard disk, sdb, on my Ubuntu machine. I have created two partitions, sdb1 and sdb2, on this hard disk.

$ sudo lsblk -e7

Note: Your hard disk or SSD will have a different name than mine, and so will the partitions. So, be sure to replace these names with yours from now on.


I will create Btrfs filesystems on the sdb1 and the sdb2 partitions. The snapshots created on the Btrfs filesystem (sdb1) will be backed up to the Btrfs filesystem created on the sdb2 partition. The Btrfs filesystem created on the sdb2 partition will act as the external drive. You may use a USB thumb drive or an external hard drive, as well; just be sure to format it with the Btrfs filesystem.

For assistance with installing the Btrfs filesystem in Ubuntu, check out my article Install and Use Btrfs on Ubuntu 20.04 LTS.

For assistance with installing the Btrfs filesystem in Fedora, check out my article Install and Use Btrfs on Fedora 33.

Creating Required Btrfs Filesystems

I will format both the sdb1 and sdb2 partitions as Btrfs. I will use the sdb1 partition for storing the data and Btrfs snapshots. I will use the sdb2 partition for backing up the snapshots of the Btrfs filesystem created on the sdb1 partition.

To create a Btrfs filesystem on the sdb1 partition and give it the filesystem label data, run the following command:

$ sudo mkfs.btrfs -L data /dev/sdb1

A Btrfs filesystem should now be created on the sdb1 partition.

To create a Btrfs filesystem on the sdb2 partition and give it the filesystem label snapshots, run the following command:

$ sudo mkfs.btrfs -L snapshots /dev/sdb2

A Btrfs filesystem should now be created on the sdb2 partition.

Create the directories /data and /snapshots for mounting the sdb1 and sdb2 partitions, respectively, as follows:

$ sudo mkdir -v /{data,snapshots}

Mount the Btrfs filesystem you have created on the sdb1 partition on the /data directory, as follows:

$ sudo mount /dev/sdb1 /data

In the same way, mount the Btrfs filesystem you have created on the sdb2 partition on the /snapshots directory, as follows:

$ sudo mount /dev/sdb2 /snapshots

As you can see in the screenshot below, both the Btrfs filesystems (sdb1 and sdb2 partitions) have been mounted correctly.

$ df -h -t btrfs

Taking Snapshots of a Btrfs Filesystem

In this section, we will create the dummy project web1 on the /data/projects/web1 Btrfs subvolume. We will take a snapshot of that subvolume in this section, as well as some other snapshots in later sections of this article.

First, create the new directory /data/projects, as follows:

$ sudo mkdir -v /data/projects

Next, create the new subvolume web1 in the /data/projects directory, as follows:

$ sudo btrfs subvolume create /data/projects/web1

Finally, create the new file index.html in the /data/projects/web1 subvolume with the nano text editor, as follows:

$ sudo nano /data/projects/web1/index.html

Type in the following lines of code in the index.html file:

<!DOCTYPE html>

<html>

<head>

        <title>Demo Website</title>

        <link rel="stylesheet" href="style.css"/>

</head>

<body>

        <h1>Hello World</h1>

</body>

</html>


Once you are done, press <Ctrl> + X followed by Y and <Enter> to save the index.html file.

In the same way, create the new file style.css in the /data/projects/web1 subvolume as follows:

$ sudo nano /data/projects/web1/style.css


Type the following lines of code in the style.css file:

h1 {

        color: green;

}

Once you are done, press <Ctrl> + X followed by Y and <Enter> to save the style.css file.

Now, the /data/projects/web1 subvolume contains the index.html and style.css file.

$ ls -lh /data/projects/web1

We will keep all the snapshots of this Btrfs filesystem in the /data/.snapshots directory.

First, create the /data/.snapshots directory with the following command:

$ sudo mkdir -v /data/.snapshots

Next, create the read-only snapshot /data/.snapshots/web1-2020-12-30 of the /data/projects/web1 subvolume with the following command:

$ sudo btrfs subvolume snapshot -r /data/projects/web1 /data/.snapshots/web1-2020-12-30

As you can see, the new snapshot /data/.snapshots/web1-2020-12-30 has been created.

$ sudo btrfs subvolume list /data

Backing up Snapshots to External Drive

To back up the snapshot /data/.snapshots/web1-2020-12-30 to another Btrfs filesystem (external drive sdb2, in this case) mounted on the /snapshots directory, run the following command:

$ sudo btrfs send /data/.snapshots/web1-2020-12-30 | sudo btrfs receive /snapshots

The snapshot /data/.snapshots/web1-2020-12-30 should be backed up to the external Btrfs filesystem (sdb2) mounted on the /snapshots directory.

As you can see, the new subvolume web1-2020-12-30 has been created on the external Btrfs filesystem.

$ sudo btrfs subvolume list /snapshots

The snapshot web1-2020-12-30 should have the same files/directories as the /data/.snapshots/web1-2020-12-30 snapshot.

$ tree -a /snapshots

You can obtain more information about the backed-up snapshot /snapshosts/web1-2020-12-30 as follows:

$ sudo btrfs subvolume show /snapshots/web1-2020-12-30

Incremental Back-up of Snapshots to External Drive

If there are a lot of files in the snapshots to back up to an external drive, then incremental backups will help you speed up the back-up operation. In this case, Btrfs will only update the files that have changed since the last snapshot and copy new files that were not available in the last snapshot.

In this section, I will show you how to perform incremental back-ups of Btrfs snapshots to external Btrfs filesystems.

First, open the index.html file from the /data/projects/web1 subvolume, as follows:

$ sudo nano /data/projects/web1/index.html

Make any changes you want to the index.html file. Once you are done, press <Ctrl> + X followed by Y and <Enter> to save the index.html file.

Take a new read-only snapshot of the /data/projects/web1 subvolume, as follows:

$ sudo btrfs subvolume snapshot -r /data/projects/web1 /data/.snapshots/web1-2020-12-31

As you can see, the new snapshot /data/.snapshots/web1-2020-12-31 of the /data/projects/web1 subvolume has been created.

$ sudo btrfs subvolume list /data

Now, we are ready to take an incremental backup.

To take an incremental backup, you will need a common snapshot of both the source and the destination (external drive) Btrfs filesystems. The common snapshot is usually the latest snapshot of a Btrfs subvolume. When you take a new snapshot on the source Btrfs filesystem, the new snapshot is compared with the latest snapshot (available on both the source and the destination Btrfs filesystem) of the source Btrfs filesystem. Btrfs will calculate the difference and send only the required data to the destination Btrfs filesystem (the external drive).

For example, to take an incremental backup of the /data/.snapshots/web1-2020-12-31 snapshot, you must specify the parent snapshot (the latest snapshot available on both the source and destination Btrfs filesystems), /data/.snapshots/web1-2020-12-30, as well.

An incremental backup of the /data/.snapshots/web1-2020-12-31 snapshot can be taken to an external Btrfs filesystem, as follows:

$ sudo btrfs send -p /data/.snapshots/web1-2020-12-30 /data/.snapshots/web1-2020-12-31 | sudo btrfs receive /snapshots

An incremental backup of the /data/.snapshots/web1-2020-12-31 snapshot should be taken.

As you can see, the web1-2020-12-31 snapshot has been backed up to the external Btrfs filesystem mounted on the /snapshots directory.

$ sudo btrfs subvolume list /snapshots

As you can see in the screenshot below, the changes you have made to the index.html file are available in the web1-2020-12-31 snapshot that has been backed up to the external Btrfs filesystem.

$ cat /snapshots/web1-2020-12-31/index.html

In the same way, you may take as many incremental backups of your snapshots as you want.

I will show you how to do an incremental backup one more time. I will not take the time to explain it again. Instead, I will just show you the process for clarity.

Open the index.html file from the /data/projects/web1 subvolume, as follows:

$ sudo nano /data/projects/web1/index.html

Make any changes you want to the index.html file. Once you are done, press <Ctrl> + X followed by Y and <Enter> to save the index.html file.

Take a new read-only snapshot of the /data/projects/web1 subvolume, as follows:

$ sudo btrfs subvolume snapshot -r /data/projects/web1 /data/.snapshots/web1-2020-12-31_2

Take an incremental backup of the /data/.snapshots/web1-2020-12-31_2 snapshot to an external Btrfs filesystem, as follows:

$ sudo btrfs send -p /data/.snapshots/web1-2020-12-31 /data/.snapshots/web1-2020-12-31_2 | sudo btrfs receive /snapshots

Note: Now, the parent snapshot to which the /data/.snapshots/web1-2020-12-31_2 snapshot will be compared is /data/.snapshots/web1-2020-12-31.

As you can see, the web1-2020-12-31_2 snapshot has been backed up to the external Btrfs filesystem mounted on the /snapshots directory.

$ sudo btrfs subvolume list /snapshots

As you can see in the screenshot below, the recent changes made to the index.html file are available on the web1-2020-12-31_2 snapshot backed up to the external Btrfs filesystem.

$ cat /snapshots/web1-2020-12-31_2/index.html

Keeping Things Clean

If you back up your Btrfs snapshots frequently, you will end up with a lot of snapshots, and it may become difficult to manage them. Luckily, you can remove any snapshot from the Btrfs filesystem.

If you are using a large enough external drive for keeping backups of the Btrfs snapshots, then you can keep a few snapshots on your Btrfs filesystem and back up all the snapshots on your external drive.

If you are using a smaller external drive, then you can selectively keep only the most important snapshots backed up on the external drive.

To perform backups of your Btrfs snapshots, you need to keep at least the latest snapshot on both the source (/data/.snapshots) and the destination (/snapshots – external drive) Btrfs filesystems. So, feel free to remove any snapshots other than the latest snapshot on both ends.

For example, in this case, the latest snapshot is web1-2020-12-31_2. So, to perform incremental backups, this snapshot must be kept on the source and the destination (external drive) Btrfs filesystems.

Suppose, you want to remove the /data/.snapshots/web1-2020-12-30 snapshot.

To do this, run the following command:

$ sudo btrfs subvolume delete /data/.snapshots/web1-2020-12-30

The Btrfs snapshot /data/.snapshots/web1-2020-12-30 should now be removed.

In the same way, you can remove the /data/.snapshots/web1-2020-12-31 snapshot, as follows:

$ sudo btrfs subvolume delete /data/.snapshots/web1-2020-12-31

Now, only the latest snapshot, /data/.snapshots/web1-2020-12-31_2, is available on the Btrfs filesystem, mounted on the /data directory. The other snapshots are backed up on the external drive, mounted on the /snapshots directory.

$ sudo btrfs subvolume list /data

$ sudo btrfs subvolume list /snapshots

Restoring Snapshots from External Drive

If you have backed up your snapshots on the external drive, you can restore them at any time from the external drive.

For example, I have removed the web1-2020-12-30 snapshot from my Btrfs filesystem, mounted on the /data directory. But, this snapshot is backed up on the external drive, mounted on the /snapshots directory. Let us restore this snapshot.

$ sudo btrfs subvolume list /snapshots

To restore the web1-2020-12-30 snapshot from the external drive, run the following command:

$ sudo btrfs send /snapshots/web1-2020-12-30 | sudo btrfs receive /data/.snapshots

The snapshot web1-2020-12-30 should be restored on the Btrfs filesystem mounted on the /data directory.

As you can see, the web1-2020-12-30 snapshot is restored on the Btrfs filesystem mounted on the /data directory.

$ sudo btrfs subvolume list /data

And, as you can see, the contents of the index.html file from the web1-2020-12-30 snapshot. This is the first version of the index.html file from before.

$ cat /data/.snapshots/web1-2020-12-30/index.html

Conclusion

In this article, you learned how to back up snapshots of your Btrfs filesystem to an external drive. You also learned how to take incremental backups of your Btrfs snapshots to an external drive. Finally, you learned how to remove existing snapshots from a Brtfs filesystem and restore snapshots from the external drive, as well.

]]>
How to Use Btrfs Snapshots https://linuxhint.com/use-btrfs-snapshots/ Sat, 09 Jan 2021 11:12:01 +0000 https://linuxhint.com/?p=85021 The Btrfs filesystem has built-in filesystem-level snapshot support. You can create a subvolume in your Btrfs filesystem and take snapshots of the files/directories in that subvolume. Taking a snapshot of a subvolume will save the state of the files/directories in that subvolume. You can recover any files/directories of the subvolume from the snapshot in case you need it.

The snapshot feature of the Btrfs filesystem uses the Copy-on-Write (CoW) principle. So, it does not take much disk space, and you can take snapshots of a subvolume instantly.

The Btrfs filesystem supports 2 types of snapshots.

  1. Writable snapshots: If you take a writable snapshot, you can modify that snapshot’s files/directories later. This is the default snapshot type of the Btrfs filesystem.
  2. Read-only snapshots: If you take a read-only snapshot, you can’t modify that snapshot’s files/directories later.

This article will show you how to take writable and read-only snapshots of your Btrfs filesystem subvolumes. I will also show you how to update a writable snapshot and recover files from a snapshot. I will show you how to remove a snapshot as well. So, let’s get started.

Prerequisites

To try out the examples of this article,

  • You must have the Btrfs filesystem installed on your computer.
  • You need to have a hard disk or SSD with at least 1 free partition (of any size).

I have a 20 GB hard disk sdb on my Ubuntu machine. I have created 2 partitions sdb1 and sdb2 on this hard disk. I will use the partition sdb1 in this article.

$ sudo lsblk -e7

Your hard disk or SSD may have a different name than mine, so will the partitions. So, make sure to replace them with yours from now on.

If you need any assistance on installing the Btrfs filesystem on Ubuntu, check my article Install and Use Btrfs on Ubuntu 20.04 LTS.

If you need any assistance on installing the Btrfs filesystem on Fedora, check my article Install and Use Btrfs on Fedora 33.

Creating a Btrfs Filesystem

To experiment with Btrfs subvolumes, you need to create a Btrfs filesystem.

To create a Btrfs filesystem with the label data on the sdb1 partition, run the following command:

$ sudo mkfs.btrfs -L data /dev/sdb1

A Btrfs filesystem should be created.

Create a directory /data with the following command:

$ sudo mkdir -v /data

To mount the Btrfs filesystem created on the sdb1 partition in the /data directory, run the following command:

$ sudo mount /dev/sdb1 /data

The Btrfs filesystem should be mounted as you can see in the screenshot below.

$ df -h /data

Preparing the Btrfs Filesystem for Snapshots

In Btrfs, you can take snapshots of Btrfs subvolumes only. The main root of a Btrfs filesystem is also a subvolume. So, you can take the backup of the entire Btrfs filesystem as well as specific subvolumes.

This section will create a Btrfs subvolume /data/projects/web1 and create the necessary files for the next sections of this article below. I will also create a directory where you can keep your snapshots. In the next sections, I will show you how to take snapshots (writable and read-only), update a writable snapshot, and recover files from the snapshot. So, let’s get started.

First, create a new directory /data/projects as follows:

$ sudo mkdir -v /data/projects

Create a new subvolume web1 in the /data/projects directory as follows:

$ sudo btrfs subvolume create /data/projects/web1

Create a new file index.html in the /data/projects/web1 subvolume as follows:

$ sudo nano /data/projects/web1/index.html

Type in the following lines of codes in the index.html file.

<!DOCTYPE html>
<html>
<head>
    <title>Demo Website</title>
    <link rel="stylesheet" href="style.css"/>
</head>
<body>
    <h1>Hello World 4</h1>
</body>
</html>

Once you’re done, press <Ctrl> + X followed by Y and <Enter> to save the index.html file.

Create a new file style.css in the /data/projects/web1 subvolume as follows:

$ sudo nano /data/projects/web1/style.css

Type in the following lines of codes in the style.css file.

h1 {
color: green;
}

Once you’re done, press <Ctrl> + X followed by Y and <Enter> to save the style.css file.

Now, the /data/projects/web1 subvolume has the index.html and style.css file.

$ ls -lh /data/projects/web1

I want to keep all the snapshots of this Btrfs filesystem in the /data/.snapshots directory.

Create the /data/.snapshots directory with the following command:

$ sudo mkdir -v /data/.snapshots

Taking Snapshots of a Subvolume

To take a snapshot of the /data/projects/web1 subvolume into the /data/.snapshots/web1-2020-12-25 directory (will be created automatically), run the following command:

$ sudo btrfs subvolume snapshot /data/projects/web1 /data/.snapshots/web1-2020-12-25

A snapshot of the /data/projects/web1 directory should be created on the /data/.snapshots/web1-2020-12-25 directory.

As you can see in the screenshot below, a new subvolume .snapshots/web1-2020-12-25 is created. A snapshot is actually a subvolume.

$ sudo btrfs subvolume list /data

You can see more information about the snapshot you’ve created in the /data/.snapshots/web1-2020-12-25 directory as follows:

$ sudo btrfs subvolume show /data/.snapshots/web1-2020-12-25

As you can see, all the files that are in the /data/projects/web1 subvolume are in the /data/.snapshots/web1-2020-12-25 snapshot.

$ tree -a /data

Recovering Files from Snapshots

In this section, I am going to show you how to recover files from the Btrfs snapshots.

First, I am going to show you how to recover a single file from the snapshot.

Open the /data/projects/web1/index.html file with the nano text editor as follows:

$ sudo nano /data/projects/web1/index.html

Make any changes you want.

Once you’re done, press <Ctrl> + X followed by Y and <Enter> to save the file.

As you can see, the main index.html file is different from the index.html file in the snapshot.

$ cat /data/projects/web1/index.html
$ cat /data/.snapshots/web1-2020-12-25/index.html

We have made the changes to the main index.html file are unwanted, and we want to recover the index.html file from the snapshot.

You can restore the index.html file from the snapshot as follows:

$ sudo cp -v /data/.snapshots/web1-2020-12-25/index.html /data/projects/web1/index.html

As you can see, the index.html file is restored from the snapshot.

$ cat /data/projects/web1/index.html
$ cat /data/.snapshots/web1-2020-12-25/index.html

Now, let’s see how to recover all the files/directories from the snapshot.

Remove all the files from the /data/projects/web1 snapshot as follows:

$ sudo rm -rv /data/projects/web1/*

To recover all the files/directories from the snapshot, run the following command:

$ sudo rsync -avz /data/.snapshots/web1-2020-12-25/ /data/projects/web1/

As you can see, the files/directories are restored from the snapshot.

$ ls -lh /data/projects/web1

Finally, let’s see how to recover files/directories from the snapshot in mirror mode. In mirror mode, the subvolume’s files/directories will be the same as in the snapshot. If there are any files/directories in the subvolume that are not available in the snapshot, they will be removed.

Let’s create a new file in the subvolume to differentiate the file tree from the snapshot.

Create a README.txt file in the /data/projects/web1 subvolume as follows:

$ echo "hello world 5" | sudo tee /data/projects/web1/README.txt

As you can see, the file tree of the /data/projects/web1 subvolume is different from the /data/.snapshots/web1-2020-12-25 snapshot.

$ tree -a /data

To restore the files/directories from the /data/.snapshots/web1-2020-12-25 snapshot to the /data/projects/web1 subvolume in mirror mode, run the following command:

$ sudo rsync -avz --delete /data/.snapshots/web1-2020-12-25/ /data/projects/web1/

All the files/directories of the /data/projects/web1 subvolume should be restored (in mirror mode) from the /data/.snapshots/web1-2020-12-25 snapshot.

The file tree of the /data/projects/web1 subvolume and the /data/.snapshots/web1-2020-12-25 snapshot should be the same.

As you can see, the index.html file and style.css file contents are the same in the /data/projects/web1 subvolume and the /data/.snapshots/web1-2020-12-25 snapshot.

Contents of the index.html and style.css file in the /data/projects/web1 subvolume.

$ cat /data/projects/web1/index.html
$ cat /data/projects/web1/style.css

Contents of the index.html and style.css file in the /data/.snapshots/web1-2020-12-25 snapshot.

$ cat /data/projects/web1/index.html
$ cat /data/projects/web1/style.css

Updating a Snapshot

By default, the Btrfs filesystem takes writable snapshots. A Btrfs snapshot is just like a subvolume. So, you can modify/update the files/directories of a writable snapshot.

Let’s update the index.html file in the /data/projects/web1 subvolume.

First, open the index.html file from the /data/projects/web1 subvolume with the nano text editor as follows:

$ sudo nano /data/projects/web1/index.html

Make any changes you want. Once you’re done, press <Ctrl> + X followed by Y and <Enter> to save the index.html file.

As you can see, the index.html file of the /data/projects/web1 subvolume is different from the /data/.snapshots/web1-2020-12-25 snapshot.

$ cat /data/projects/web1/index.html
$ cat /data/.snapshots/web1-2020-12-25/index.html

You want to keep the index.html file of the /data/projects/web1 subvolume.

To update the index.html file in the /data/.snapshots/web1-2020-12-25 snapshot, run the following command:

$ sudo cp -v /data/projects/web1/index.html /data/.snapshots/web1-2020-12-25/index.html

As you can see, the index.html file of the /data/.snapshots/web1-2020-12-25 snapshot is updated.

Updating a snapshot is as easy as copying new files to the snapshot.

Taking Read-Only Snapshots of a Subvolume

At times, you don’t want the snapshots you’ve taken to be updated in any way. In that case, you can create read-only snapshots.

For example, to create a read-only snapshot /data/.snapshots/web1-2020-12-26 of the /data/projects/web1 subvolume, run the following command:

$ sudo btrfs subvolume snapshot -r /data/projects/web1 /data/.snapshots/web1-2020-12-26

As you can see, a new subvolume .snapshots/web1-2020-12-26 is created.

$ sudo btrfs subvolume list /data

As you can see, the snapshot /data/.snapshots/web1-2020-12-26 is read-only.

$ sudo btrfs subvolume show /data/.snapshots/web1-2020-12-26

Let’s update the index.html file from the /data/projects/web1 subvolume.

To do that, open the index.html file from the /data/projects/web1 subvolume with the nano text editor as follows:

$ sudo nano /data/projects/web1/index.html

Make any changes you want. Once you’re done, press <Ctrl> + X followed by Y and <Enter> to save the changes.

As you can see, the index.html in the /data/projects/web1 subvolume is different from the /data/.snapshots/web1-2020-12-26 snapshot.

$ cat /data/projects/web1/index.html
$ cat /data/.snapshots/web1-2020-12-26/index.html

Let’s try to update the index.html file in the /data/.snapshots/web1-2020-12-26 snapshot.

$ sudo cp -v /data/projects/web1/index.html /data/.snapshots/web1-2020-12-26/index.html

As you can see, you can’t update the index.html file of the /data/.snapshots/web1-2020-12-26 snapshot because the snapshot is read-only.

Removing a Snapshot

I have told you earlier that a Btrfs snapshot is like a subvolume. So, you can remove a Btrfs snapshot just like you remove a Btrfs subvolume. Same command.

This is how the file tree of the Btrfs filesystem mounted on the /data directory looks like at the moment.

$ tree -a /data

Let’s remove the .snapshots/web1-2020-12-25 snapshot.

$ sudo btrfs subvolume list /data

To remove the /data/.snapshots/web1-2020-12-25 snapshot, run the following command:

$ sudo btrfs subvolume delete /data/.snapshots/web1-2020-12-25

As you can see, the snapshot .snapshots/web1-2020-12-25 is no more.

$ sudo btrfs subvolume list /data

As you can see, the files/directories of the /data/.snapshots/web1-2020-12-25 snapshot is removed as well.

$ tree -a /data

Conclusion

This article has shown you how to take writable and read-only snapshots of your Btrfs filesystem subvolumes. I have also shown you how to update a writable snapshot and recover files from a snapshot. I have shown you how to remove a Btrfs snapshot as well. This article should help you get started with the Btrfs snapshot feature.

]]>
How to Create and Mount Btrfs Subvolumes https://linuxhint.com/create-mount-btrfs-subvolumes/ Fri, 08 Jan 2021 20:50:48 +0000 https://linuxhint.com/?p=84971 A Btrfs subvolume works just like a directory, but it has its own file tree. So, you can mount Btrfs subvolumes separately as they have their own file tree. You also need to create subvolumes to take snapshots of your important data.

This article will show you how to create and delete Btrfs subvolumes, mount Btrfs subvolumes, and automatically mount Btrfs subvolumes using the /etc/fstab file. So, let’s get started.

Prerequisites

To try out the examples of this article,

  • You must have the Btrfs filesystem installed on your computer.
  • You need to have a hard disk or SSD with at least 1 free partition (of any size).

I have a 20 GB hard disk sdb on my Ubuntu machine. I have created 2 partitions sdb1 and sdb2 on this hard disk. I will use the partition sdb1 in this article.

$ sudo lsblk -e7

Your hard disk or SSD may have a different name than mine, so will the partitions. So, make sure to replace them with yours from now on.

If you need any assistance on installing the Btrfs filesystem on Ubuntu, check my article Install and Use Btrfs on Ubuntu 20.04 LTS.

If you need any assistance on installing the Btrfs filesystem on Fedora, check my article Install and Use Btrfs on Fedora 33.

Creating a Btrfs Filesystem

To experiment with Btrfs subvolumes, you need to create a Btrfs filesystem.

To create a Btrfs filesystem with the label data on the sdb1 partition, run the following command:

$ sudo mkfs.btrfs -L data /dev/sdb1

A Btrfs filesystem should be created.

Create a directory /data with the following command:

$ sudo mkdir -v /data

To mount the Btrfs filesystem created on the sdb1 partition in the /data directory, run the following command:

$ sudo mount /dev/sdb1 /data

The Btrfs filesystem should be mounted as you can see in the screenshot below.

$ df -h /data

Creating Btrfs Subvolumes

A Btrfs subvolume is just like a directory in your Btrfs filesystem. So, you need to specify a directory path to create a Btrfs subvolume in that directory path. The path must point to a Btrfs filesystem where you want to create the subvolume.

For example, to create a Btrfs subvolume in the path /data/photos (the Btrfs filesystem is mounted in the /data directory), run the following command:

$ sudo btrfs subvolume create /data/photos

A Btrfs subvolume /data/photos should be created.

Let’s create some more Btrfs subvolumes.

Create a Btrfs subvolume /data/videos with the following command:

$ sudo btrfs subvolume create /data/videos

Create a Btrfs subvolume /data/documents with the following command:

$ sudo btrfs subvolume create /data/documents

Create a Btrfs subvolume /data/projects with the following command:

$ sudo btrfs subvolume create /data/projects

As you can see, a new directory is automatically created for each of the subvolumes.

You can list all the subvolumes of your Btrfs filesystem (mounted on the /data directory) as follows:

$ sudo btrfs subvolume list /data

As you can see, all the subvolumes we have created are listed.

You can find a lot of information about a Btrfs subvolume (let’s say /data/projects) like the subvolume name, the subvolume UUID, the subvolume ID etc. as follows:

$ sudo btrfs subvolume show /data/projects

Let’s create some dummy files in each of the Btrfs subvolumes. Once we mount the Btrfs subvolumes separately, the files in each of the subvolumes should be there.

To create some dummy files in the /data/projects subvolume, run the following command:

$ sudo touch /data/projects/file{1..3}

To create some dummy files in the /data/photos subvolume, run the following command:

$ sudo touch /data/photos/file{4..6}

To create some dummy files in the /data/videos subvolume, run the following command:

$ sudo touch /data/videos/file{7..8}

To create some dummy files in the /data/documents subvolume, run the following command:

$ sudo touch /data/documents/file{9..10}

Right now, this is how the Btrfs filesystem mounted on the /data directory looks like.

$ tree /data

Mounting Btrfs Subvolumes

To mount a Btrfs subvolume, you need to know either its name or its ID.

You can find the name or the ID of all the Btrfs subvolumes created on the Btrfs filesystem mounted on the /data directory as follows:

$ sudo btrfs subvolume list /data

Let’s mount the projects Btrfs subvolume. The projects Btrfs subvolume has the ID 261.

I will mount the Btrfs subvolume projects in the /tmp/projects directory to show you how to mount a Btrfs subvolume.

Create a directory /tmp/projects as follows:

$ sudo mkdir -v /tmp/projects

You can mount the projects Btrfs subvolume (which is available in the Btrfs filesystem created on the sdb1 partition) using its name projects in the /tmp/projects directory as follows:

$ sudo mount /dev/sdb1 -o subvol=projects /tmp/projects

The projects subvolume should be mounted on the /tmp/projects directory as you can see in the screenshot below.

$ sudo btrfs subvolume show /tmp/projects

You can also see that the Btrfs filesystem (the projects subvolume) is mounted on the /tmp/projects directory.

$ df -h -t btrfs

All the files you have created in the projects subvolume are also available in the /tmp/projects directory as you can see in the screenshot below.

$ tree /tmp/projects

Now, let’s see how to mount a Btrfs subvolume using its ID.

Before that, umount the projects subvolume from the /tmp/projects directory as follows:

$ sudo umount /tmp/projects

You can mount the projects Btrfs subvolume (which is available in the Btrfs filesystem created on the sdb1 partition) using its ID 261 in the /tmp/projects directory as follows:

$ sudo mount /dev/sdb1 -o subvolid=261 /tmp/projects

The projects subvolume should be mounted on the /tmp/projects directory as you can see in the screenshot below.

$ sudo btrfs subvolume show /tmp/projects

You can also see that the Btrfs filesystem (the projects subvolume) is mounted on the /tmp/projects directory.

$ df -h -t btrfs

All the files you have created in the projects subvolume are also available in the /tmp/projects directory as you can see in the screenshot below.

$ tree /tmp/projects

Removing Btrfs Subvolumes

In this section, I am going to show you how to remove a Btrfs subvolume.

Let’s create a Btrfs subvolume test on the Btrfs filesystem mounted on the /data directory as follows:

$ sudo btrfs subvolume create /data/test

As you can see, the test subvolume is created on the Btrfs filesystem mounted on the /data directory.

$ sudo btrfs subvolume list /data

To remove the test Btrfs subvolume, run the following command:

$ sudo btrfs subvolume delete /data/test

NOTE: If you delete a Btrfs subvolume, all the files/directories in that subvolume will also be removed.

As you can see, the Btrfs subvolume test is removed.

$ sudo btrfs subvolume list /data

Automatically Mount Brtfs Subvolumes at Boot Time

In this section, I will show you how to mount the Btrfs subvolumes of the Btrfs filesystem created on the sdb1 partition (mounted on /data directory now).

First, unmount the Btrfs filesystem, which is mounted on the /data directory as follows:

$ sudo umount /data

I want to mount the Btrfs subvolumes in their respective directories. Let’s create some directories where we can mount the Btrfs subvolumes.

To create the directories documents, projects, photos, and videos, run the following command:

$ sudo mkdir -pv /data/{documents,projects,photos,videos}

Find the UUID of the Btrfs filesystem on the sdb1 partition as follows:

$ sudo blkid  /dev/sdb1

As you can see, the UUID of the Btrfs filesystem is 0b56138b-6124-4ec4-a7a3-7c503516a65c.

Now, edit the /etc/fstab file with the nano text editor as follows:

$ sudo nano /etc/fstab

Type in the following lines in the /etc/fstab file:

# Mount the Btrfs subvolumes to their respective directories
UUID=0b56138b-6124-4ec4-a7a3-7c503516a65c   /data/projects   
btrfs   subvol=projects    0   0
UUID=0b56138b-6124-4ec4-a7a3-7c503516a65c   /data/documents  
btrfs   subvol=documents   0   0
UUID=0b56138b-6124-4ec4-a7a3-7c503516a65c   /data/photos     
btrfs   subvol=photos      0   0
UUID=0b56138b-6124-4ec4-a7a3-7c503516a65c   /data/videos     
btrfs   subvol=videos      0   0

NOTE: Make changes as required.

Once you’re done, press <Ctrl> + X followed by Y and <Enter> to save the /etc/fstab file.

For the changes to take effect, reboot your computer with the following command:

$ sudo reboot

Once your computer boots, the Btrfs subvolumes should be mounted on their respective directories as you can see in the screenshot below.

$ df -h -t btrfs

Conclusion

In this article, I have shown you how to create and delete Btrfs subvolumes, mount Btrfs subvolumes, and automatically mount Btrfs subvolumes using the /etc/fstab file. This article should help you get started with the subvolume feature of the Btrfs filesystem.

]]>
How to Save Disk Space using Btrfs Deduplication https://linuxhint.com/save-disk-space-btrfs-deduplication/ Tue, 29 Dec 2020 22:14:20 +0000 https://linuxhint.com/?p=83592 Deduplication is a software feature that is used to remove duplicate data blocks (redundant data blocks) from a filesystem to save disk spaces. The Btrfs filesystem is a modern Copy-on-Write (CoW) filesystem that supports deduplication.

If you need to keep a lot of redundant data (i.e., file backups, database) on your computer, then the Copy-on-Write (CoW) and deduplication feature of the Btrfs filesystem can save a huge amount of disk spaces.

In this article, I will show you how to save disk spaces using the Btrfs deduplication feature. So, let’s get started.

Prerequisites:

To try out the examples of this article,

  • You must have the Btrfs filesystem installed on your computer.
  • You need to have a hard disk or SSD with at least 1 free partition (of any size).

I have a 20 GB hard disk sdb on my Ubuntu machine. I have created 2 partitions sdb1 and sdb2, on this hard disk. I will use the partition sdb1 in this article.

$ sudo lsblk -e7

Your hard disk or SSD may have a different name than mine, so will the partitions. So, make sure to replace them with yours from now on.

If you need any assistance on installing the Btrfs filesystem on Ubuntu, check my article Install and Use Btrfs on Ubuntu 20.04 LTS.

If you need any assistance on installing the Btrfs filesystem on Fedora, check my article Install and Use Btrfs on Fedora 33.

Creating a Btrfs Filesystem:

To experiment with Btrfs filesystem-level data compression, you need to create a Btrfs filesystem.

To create a Btrfs filesystem with the label data on the sdb1 partition, run the following command:

$ sudo mkfs.btrfs -L data /dev/sdb1

Mount a Btrfs Filesystem:

Create a directory /data with the following command:

$ sudo mkdir -v /data

To mount the Btrfs filesystem created on the sdb1 partition on the /data directory, run the following command:

$ sudo mount /dev/sdb1 /data

The Btrfs filesystem should be mounted, as you can see in the screenshot below.

$ df -h /data

Installing Deduplication Tools on Ubuntu 20.04 LTS:

To deduplicate a Btrfs filesystem, you need to install the duperemove program on your computer.

If you’re using Ubuntu 20.04 LTS, then you can install duperemove from the official package repository of Ubuntu.

First, update the APT package repository cache with the following command:

$ sudo apt update

Install the duperemove package with the following command:

$ sudo apt install duperemove -y

The duperemove package should be installed.

Installing Deduplication Tools on Fedora 33:

To deduplicate a Btrfs filesystem, you need to install the duperemove program on your computer.

If you’re using Fedora 33, then you can install duperemove from the official package repository of Fedora.

First, update the DNF package repository cache with the following command:

$ sudo dnf makecache

Install the duperemove package with the following command:

$ sudo dnf install duperemove

To confirm the installation, press Y and then press <Enter>.

The duperemove package should be installed.

Testing Deduplication on a Btrfs Filesystem:

In this section, I am going to do a simple test to show you how the deduplication feature of the Btrfs filesystem removes redundant data from the filesystem and saves disk space.

As you can see,

  1. I have copied a file QGIS-OSGeo4W-3.14.0-1-Setup-x86_64.exe to the /data directory. The file is 407 MB in size.
  2. The file stored on the /data directory is 407 MB in size.
  3. Only the file consumed about 412 MB of disk space from the Btrfs filesystem mounted on the /data directory.

As you can see,

  1. I have copied the same file to the /data directory and renamed it to QGIS-OSGeo4W-3.14.0-1-Setup-x86_64.2.exe.
  2. The file stored on the /data directory is now 814 MB in size.
  3. The files consumed about 820 MB of disk space from the Btrfs filesystem mounted on the /data directory.

To perform the deduplication operation on the Btrfs filesystem mounted on the /data directory, run the following command:

$ sudo duperemove -dr /data

The redundant data blocks from the Btrfs filesystem mounted on the /data directory should be removed.

As you can see,

  1. I have the files QGIS-OSGeo4W-3.14.0-1-Setup-x86_64.exe and QGIS-OSGeo4W-3.14.0-1-Setup-x86_64.2.exe in /data directory.
  2. The file stored on the /data directory is now 814 MB in size.
  3. The files consumed about 412 MB of disk space from the Btrfs filesystem mounted on the /data directory.

The duperemove program removed redundant (duplicate) data blocks from the Btrfs filesystem mounted on the /data directory and saved a lot of disk spaces.

Automatically Mounting a Btrfs Filesystem on Boot:

To mount the Btrfs filesystem you have created, you need to know the UUID of the Btrfs filesystem.

You can find the UUID of the Btrfs filesystem mounted on the /data directory with the following command:

$ sudo btrfs filesystem show /data

As you can see, the UUID of the Btrfs filesystem that I want to mount at boot time is e39ac376-90dd-4c39-84d2-e77abb5e3059. It will be different for you. So, make sure to replace it with yours from now on.

Open the /etc/fstab file with the nano text editor as follows:

$ sudo nano /etc/fstab

Type in the following line at the end of the /etc/fstab file:

UUID=e39ac376-90dd-4c39-84d2-e77abb5e3059    /data    btrfs    defaults   0   0

NOTE: Replace the UUID of the Btrfs filesystem with yours. Also, change the mount option and compression algorithm as you like.

Once you’re done, press <Ctrl> + X followed by Y and <Enter> to save the /etc/fstab file.

For the changes to take effect, reboot your computer with the following command:

$ sudo reboot

Once your computer boots, the Btrfs filesystem should be mounted in the /data directory, as you can see in the screenshot below.

$ df -h /data

Automatically Perform Deduplication using Cron Job:

To remove redundant data from the Btrfs filesystem, you have to run the duperemove command every once in a while.

You can automatically run the duperemove command hourly, daily, weekly, monthly, yearly, or at boot time using a cron job.

First, find the full path of the duperemove command with the following command:

$ which duperemove

As you can see, the full path of the duperemove command is /usr/bin/duperemove. Remember the path as you will need it later.

To edit the crontab file, run the following command:

$ sudo crontab -e

Select a text editor you like and press <Enter>.

I will use the nano text editor. So, I will type in 1 and press <Enter>.

The crontab file should be opened.

To run the duperemove command on the /data directory every hour, add the following line at the end of the crontab file.

@hourly /usr/bin/duperemove -dr /data >> /var/log/duperemove.log

To run the duperemove command on the /data directory every day, add the following line at the end of the crontab file.

@daily /usr/bin/duperemove -dr /data >> /var/log/duperemove.log

To run the duperemove command on the /data directory every week, add the following line at the end of the crontab file.

@weekly /usr/bin/duperemove -dr /data >> /var/log/duperemove.log

To run the duperemove command on the /data directory every month, add the following line at the end of the crontab file.

@monthly /usr/bin/duperemove -dr /data >> /var/log/duperemove.log

To run the duperemove command on the /data directory every year, add the following line at the end of the crontab file.

@yearly /usr/bin/duperemove -dr /data >> /var/log/duperemove.log

To run the duperemove command on the /data directory at boot time, add the following line at the end of the crontab file.

@reboot /usr/bin/duperemove -dr /data >> /var/log/duperemove.log

NOTE: I will run the duperemove command at boot time in this article.

Once you’re done, press <Ctrl> + X followed by Y and <Enter> to save the crontab file.

A new cron job should be installed.

For the changes to take effect, reboot your computer with the following command:

$ sudo reboot

As the duperemove command runs in the background, the output of the command will be stored in the /var/log/duperemove.log file.

$ sudo ls -lh /var/log/duperemove*

As you can see, the /var/log/duperemove.log file contains the duperemove log data. It means the cron job is working just fine.

Conclusion:

In this article, I have shown you how to install the duperemove Brtfs deduplication tool on Ubuntu 20.04 LTS and Fedora 33. I have also shown you how to perform Btrfs deduplication using the duperemove tool and run the duperemove tool automatically using a cron job. ]]> Install and Use Btrfs on Fedora 33 https://linuxhint.com/install-and-use-btrfs-on-fedora33/ Mon, 28 Dec 2020 13:59:24 +0000 https://linuxhint.com/?p=83171 Btrfs (B-Tree Filesystem) is a modern copy-on-write (CoW) filesystem for Linux. It aims to implement many advanced filesystem features while focusing on fault tolerance, repair, and easy administration. The Btrfs filesystem is designed to support the requirement of high performance and high-capacity storage servers.

If you want to learn more about the Btrfs filesystem, check my article Introduction to Btrfs Filesystem.

In this article, I am going to show you how to install Btrfs on Fedora 33 and use it. So, let’s get started.

Installing Btrfs Filesystem

The Btrfs filesystem package is available in the official package repository of Fedora 33. So, you can easily install it on your Fedora 33 operating system.

First, update the DNF package manager cache with the following command:

$ sudo dnf makecache

To install the Btrfs filesystem on Fedora 33, run the following command:

$ sudo dnf install btrfs-progs -y

Fedora 33 uses the Btrfs filesystem by default. So, it should be installed on your Fedora 33 operating system already.

Partitioning the Disk

You don’t have to partition your HDD/SSD to create a Btrfs filesystem, you can just make it on your bare unpartitioned HDD/SSD. But you may want to partition your HDD/SSD before you format your HDD/SSD with the Btrfs filesystem.

You can list all the storage devices and partitions of your computer with the following command:

$ sudo lsblk

I have an HDD sdb on my computer, as you can see in the screenshot below. I will be partitioning the HDD sdb and formatting the partitions with the Btrfs filesystem for the demonstration in this article.

To partition the HDD sdb, I will use the cfdisk partitioning tool.

You can open the HDD sdb with the cfdisk partitioning tool as follows:

$ sudo cfdisk /dev/sdb

Select gpt and press <Enter>.

To create a new partition, select Free space, select [ New ], and press <Enter>.

Type in the size of the partition you want to create. I will create a 10 GB partition. So, I will type in 10G.

You can use the following symbols to create partitions of different sizes/units:

  • M – partition size in megabyte unit
  • G – partition size in gigabyte unit
  • T – partition size in terabyte unit
  • S –number of sectors you want to use for the partition

Once you’re done, press <Enter>.

A new partition (sdb1 in my case) should be created.

Let’s create another partition.

To do that, select the Free space, select [ New ], and press <Enter>.

Type in the size of the partition and press <Enter>.

A new partition (sdb2 in my case) should be created.

To write the changes to the disk, select [ Write ] and press <Enter>.

To confirm the changes, type in yes and press <Enter>.

The partition table should be saved to the disk.

To quit cfdisk program, select [ Quit ] and press <Enter>.

Formatting a Disk with Btrfs Filesystem

In this section, I am going to show you how to format a partition with the Btrfs filesystem.

I have created 2 partitions sdb1 and sdb2 in the earlier section of this article. I will format the partition sdb1 with the Btrfs filesystem for the demonstration.

$ sudo lsblk

To format the partition sdb1 with the Btrfs filesystem, run the following command:

$ sudo mkfs.btrfs -L data /dev/sdb1

NOTE: Here, the -L flag sets the label of the partition. In this case, the partition label is data.

The partition sdb1 should be formatted with the Btrfs filesystem.

Mounting a Btrfs Filesystem:

To mount a Btrfs filesystem, you need to create a directory (mount point) where you can mount the Btrfs filesystem.

To create a directory/mount point /data, run the following command:

$ sudo mkdir -v /data

Once the /data mount point is created, you can mount the sdb1 Btrfs filesystem on the /data mount point with the following command:

$ sudo mount /dev/sdb1 /data

The Btrfs partition sdb1 should be mounted in the /data mount point as you can see in the screenshot below.

$ df -h

Checking Btrfs Filesystem Usage Information

Checking the usage information of your Btrfs filesystem is very important. There are many ways to check the usage information of your Btrfs filesystem. Let’s see some of them.

You can use the following command to see the usage information of all the Btrfs filesystems on your computer:

$ sudo btrfs filesystem show

As you can see, the usage information of the fedora_localhost-live Btrfs filesystem (where the Fedora 33 operating system is installed) and the data Btrfs filesystem that we have created are listed.

You should find the following usage information here:

  • The label of each of the Btrfs filesystems on your computer.
  • The UUID of each of the Btrfs filesystems on your computer.
  • The total number of devices added to each of the Btrfs filesystems on your computer.
  • The disk usage information of each of the storage devices added to each of the Btrfs filesystems on your computer.

To find disk usage information about a specific Btrfs filesystem mounted on a specific directory path (/data let’s say), run the following command:

$ sudo btrfs filesystem usage /data

As you can see, a lot of disk usage information about the Btrfs partition mounted on the /data mount point is displayed.

On the top, you should find the total disk size of the Btrfs filesystem.

You should also find the amount of disk space the Btrfs filesystem has allocated (reserved for storing data) and the amount of disk space that is used from the allocated/reserved disk space.

You should also find the amount of disk space the Btrfs filesystem did not allocate (did not reserve for storing data) yet and the estimated amount of disk space (allocated and unallocated) that is still available for storing new data.

On the bottom, you should find the following information:

  • The total amount of disk space allocated for data and used for data from all the storage devices added to the Btrfs filesystem.
  • The amount of disk space allocated for data in each of the storage devices added to the Btrfs filesystem.
  • The total amount of disk space allocated and used for metadata from all the storage devices added to the Btrfs filesystem.
  • The amount of disk space allocated for metadata in each of the storage devices added to the Btrfs filesystem.
  • The total amount of disk space allocated and used for the Btrfs system data from all the storage devices added to the Btrfs filesystem.
  • The amount of disk space allocated for the Btrfs system data in each of the storage devices added to the Btrfs filesystem.
  • The amount of unallocated disk space in each of the storage devices added to the Btrfs filesystem.

On the bottom, you should also find:

  • The method (i.e., single, DUP) that is used to allocate disk space for the data, metadata, and system data.

Here:

  • For single-mode allocation, the Btrfs filesystem will keep only one instance of the allocation. There won’t be any duplicates.
  • For DUP mode allocation, the Btrfs filesystem will allocate the disk space in different parts of the filesystem for the same purpose. So, multiple copies (usually two) of the same data will be kept on the filesystem.
  • Usually, the data is allocated in a single mode. The metadata and the system data are allocated in DUP mode.
  • In single mode, notice that the Btrfs filesystem can use all the allocated disk space.
  • In DUP mode, notice that the Btrfs filesystem can use half the disk space from the total allocated disk space.

To see the summary of the disk space allocated and used for the data, metadata, and system of a Btrfs filesystem mounted in the /data directory, run the following command:

$ sudo btrfs filesystem df /data

You can also list the disk usage information of each of the files and directories of the Btrfs filesystem mounted on the /data directory as follows:

$ sudo btrfs filesystem du /data

In the end, the disk usage summary of all the files and directories of the /data btrfs filesystem should be displayed.

To only see the disk usage summary of the files and directories of the Btrfs filesystem mounted on the /data directory, run the following command:

$ sudo btrfs filesystem du -s /data

Adding More Storage Devices to a Btrfs Filesyste

If you need more disk space on your Btrfs filesystem, you can add more storage devices or partitions to the Btrfs filesystem to expand the disk space of the filesystem.

For example, to add the partition sdb2 on the Btrfs filesystem mounted on the /data directory, run the following command:

$ sudo btrfs device add /dev/sdb2 /data

As you can see, the new partition sdb2 is added to the Btrfs filesystem mounted on the /data directory.

$ sudo btrfs device usage /data

As you can see, the size of the Btrfs filesystem mounted on the /data directory has increased.

$ df -h

Mounting a Btrfs Filesystem at Boot Time:

Once you have set up a Btrfs filesystem, you don’t want to mount it manually every time you boot your computer, instead, you would want it to automatically do so. Let’s see how to do that.

First, find the UUID of the Btrfs filesystem mounted on the /data directory as follows:

$ sudo btrfs filesystem show /data

In my case, the UUID of the Btrfs filesystem is

7732d03-b934-4826-9e8f-d7de4971fb15.

It will be different for you. So, make sure to replace it with yours from now on.

Open the /etc/fstab file with the nano text editor as follows:

$ sudo nano /etc/fstab

At the end of the /etc/fstab file, type in the following line.

UUID=7732d03-b934-4826-9e8f-d7de4971fb15        /data   btrfs   defaults    0 0

Once you’re done, press <Ctrl> + X, followed by Y, and <Enter> to save the /etc/fstab file.

For the changes to take effect, reboot your computer with the following command:

$ sudo reboot

Once your computer boots, you should see that the Btrfs filesystem is correctly mounted in the /data directory at boot time, as you can see in the screenshot below.

$ df -h

Conclusion

In this article, I have shown you how to install and use the Btrfs filesystem on Fedora 33. This article should help you get started with the Btrfs filesystem on Fedora 33.

]]>
How to Enable Btrfs Filesystem Compression https://linuxhint.com/enable-btrfs-filesystem-compression/ Mon, 28 Dec 2020 13:44:20 +0000 https://linuxhint.com/?p=83431 The Btrfs filesystem supports filesystem-level data compression. It means that the filesystem data will be compressed automatically as new data is written to the filesystem. When you access the files stored in your Btrfs filesystem, those files’ data will be automatically decompressed.

This feature of the filesystem will save you a lot of disk space and will save you a lot of time that you would have spent compressing your files manually.

In this article, I am going to show you how to enable the Btrfs filesystem-level compression on a Btrfs filesystem. So, let’s get started.

Prerequisites:

To try out the examples of this article,

  • You must have the Btrfs filesystem installed on your computer.
  • You need to have a hard disk or SSD with at least 1 free partition (of any size).

I have a 20 GB hard disk sdb on my Ubuntu machine. I have created 2 partitions sdb1 and sdb2 on this hard disk. I will use the partition sdb1 in this article.

$ sudo lsblk -e7

Your hard disk or SSD may have a different name than mine, so will the partitions. So, make sure to replace them with yours from now on.

If you need any assistance installing the Btrfs filesystem on Ubuntu, check my article Install and Use Btrfs on Ubuntu 20.04 LTS.

If you need any assistance installing the Btrfs filesystem on Fedora, check my article Install and Use Btrfs on Fedora 33.

Btrfs Compression Algorithms:

At the time of this writing, the Btrfs filesystem supports the following compression algorithms:

i) LZO: LZO is a lossless real-time block compression algorithm. LZO divides the data into blocks and compresses/decompressed the data by blocks in real-time. It is the default compression algorithm of the Btrfs filesystem.

ii) ZLIB: ZLIB is a library used for data compression. It uses the DEFLATE data compression algorithm. The DEFLATE data compression algorithm is a combination of the LZ77 and Huffman coding algorithms. The Btrfs filesystem supports the ZLIB data compression algorithm.

You can also specify the level of compression you want. The level can be any number from 1 to 9. A higher level indicates a higher compression ratio. So, level 9 will save more disk space than level 1 (level 9 has a higher compression ratio than level 1). Unless you specify a ZLIB level of compression to use, the Btrfs filesystem will use the ZLIB compression level 3 by default.

ZSTD: ZSTD or Zstandard is a high-performance lossless data compression algorithm. It was developed at Facebook by Yann Collect. Its compression ratio is comparable to the DEFLATE algorithm that is used in ZLIB, but it’s faster. The Btrfs filesystem supports the ZSTD data compression algorithm.

You can also specify the level of compression you want. The level can be any number from 1 to 15. A higher level indicates a higher compression ratio. So, level 15 will save more disk space than level 1 (level 15 has a higher compression ratio than level 1). Unless you specify a ZSTD level of compression to use, the Btrfs filesystem will use the ZSTD compression level 3 by default.

Creating a Btrfs Filesystem:

To experiment with Btrfs filesystem-level data compression, you need to create a Btrfs filesystem.

To create a Btrfs filesystem with the label data on the sdb1 partition, run the following command:

$ sudo mkfs.btrfs -L data /dev/sdb1

Mount a Btrfs Filesystem with Compression Enabled:

To enable Btrfs filesystem-level compression, you have to mount the Btrfs filesystem you have created on the sdb1 partition with either the compress or compress-force mount option.

i) compress mount option: The compress mount option will simply enable Btrfs filesystem-level compression. The Btrfs filesystem will determine whether compression makes the file that is to be compressed bigger than the original (uncompressed) file size. If compression makes the file size any bigger than the original, then the Btrfs filesystem will not compress that file.

ii) compress-force mount option: Unlike the compress mount option, if the Btrfs filesystem is mounted using the compress-force mount option, then every file on the Btrfs filesystem will be compressed even when compression makes the file bigger.

Create a directory /data with the following command:

$ sudo mkdir -v /data

To enable LZO compression, mount the Btrfs filesystem that you’ve created earlier in the /data directory with the following command:

$ sudo mount -o compress=lzo /dev/sdb1 /data

To enable force LZO compression, mount the Btrfs filesystem that you’ve created earlier in the /data directory as follows:

$ sudo mount -o compress-force=lzo /dev/sdb1 /data

In the same way, you can mount the Btrfs filesystem in the /data directory as follows to enable ZLIB compression:

$ sudo mount -o compress=zlib /dev/sdb1 /data

To set a ZLIB compression level (let’s say, level 7), you can mount the Btrfs filesystem in the /data directory as follows:

$ sudo mount -o compress=zlib:7 /dev/sdb1 /data

To enable ZSTD compression, mount the Btrfs filesystem in the /data directory as follows:

$ sudo mount -o compress=zstd /dev/sdb1 /data

To set a ZSTD compression level (let’s say, level 10), you can mount the Btrfs filesystem in the /data directory as follows:

$ sudo mount -o compress=zstd:10 /dev/sdb1 /data

The Btrfs filesystem that you’ve created on the sdb1 partition should be mounted in the /data directory as you can see in the screenshot below.

$ df -h /data

Testing Btrfs Filesystem Compression:

To test whether the Btrfs filesystem compresses the files that are on the Btrfs filesystem, I will mount the Btrfs filesystem on the /data directory with the compress-force option. I will use the highest compression level of the ZSTD compression algorithm for the demonstration.

First, unmount the Btrfs filesystem that you may have mounted on the /data directory as follows:

$ sudo umount /data

Mount the Btrfs filesystem with the highest compression level (level 15) of the ZSTD compression algorithm in the /data directory as follows:

$ sudo mount -o compress-force=zstd:15 /dev/sdb1 /data

I have copied about 717 MB of data on the Btrfs filesystem mounted on the /data directory. As you can see, only 661 MB is disk space is used on the Btrfs filesystem even though the data stored in the filesystem is 717 MB in size. So, the Btrfs filesystem-level compression is working.

Mounting a Compression Enabled Btrfs Filesystem on Boot:

If you want to mount the Btrfs filesystem automatically at boot time with compression enabled (which you most likely do), then this section is for you.

First, find the UUID of the Btrfs filesystem which you want to enable compression and mount automatically at boot time as follows:

$ sudo btrfs filesystem show /data

As you can see, the UUID of the Btrfs filesystem is a8e75a9d-a6f6-4c6e-be41-c10bc1077aa2 in my case. It will be different for you. So, make sure to replace it with yours from now on.

Open the /etc/fstab file with the nano text editor as follows:

$ sudo nano /etc/fstab

Type in the following line at the end of the /etc/fstab file:

UUID=a8e75a9d-a6f6-4c6e-be41-c10bc1077aa2 /data btrfs compress=lzo 0 0

NOTE: Replace the UUID of the Btrfs filesystem with yours. Also, change the mount option and compression algorithm as you like.

Once you’re done, press <Ctrl> + X followed by Y and <Enter> to save the /etc/fstab file.

For the changes to take effect, reboot your computer with the following command:

$ sudo reboot

Once your computer boots, the Btrfs filesystem should be mounted in the /data directory as you can see in the screenshot below.

$ df -h /data

Conclusion:

In this article, I have discussed the compression algorithms supported by the Btrfs filesystem: LZO, ZLIB, and ZSTD. I have also shown you how to enable filesystem-level compression in a Btrfs filesystem.

]]>
Resize a Btrfs Filesystem https://linuxhint.com/resize_a_btrfs_filesystem/ Sun, 27 Dec 2020 17:02:27 +0000 https://linuxhint.com/?p=83067

The Btrfs filesystem can be resized online (when the filesystem is mounted), but if you want to resize a partition that is added to a Btrfs filesystem, you will have to do it offline (when the filesystem is not mounted). You can grow/expand or shrink a Btrfs filesystem online and grow/expand or shrink a Btrfs partition offline.

The Btrfs filesystem is a multi-device filesystem. If you have multiple devices added to your Btrfs filesystem, then you need to resize specific storage devices attached to the filesystem to resize the filesystem itself. Otherwise, you can directly resize the filesystem (as by default, the only attached storage device will be selected when you perform the resize operation).

In this article, I am going to show you how to grow/expand and shrink a Btrfs filesystem online and a Btrfs partition offline. I will also show how to resize a Btrfs filesystem that has multiple devices attached to it. So, let’s get started.

Prerequisites

To try out the examples of this article:

  • You must have the Btrfs filesystem installed on your computer.
  • You need to have a hard disk or SSD with at least 2 free partitions (of any size).

I have a 20 GB hard disk sdb on my Ubuntu machine. I have created 2 partitions, sdb1 and sdb2, on this hard disk. The partitions sdb1 and sdb2 are 10 GB in size.

$ sudo lsblk -e7


Your hard disk or SSD may have a different name than mine, so will the partitions. So, make sure to replace them with yours from now on.

If you need any assistance installing the Btrfs filesystem on Ubuntu, check my article Install and Use Btrfs on Ubuntu 20.04 LTS.

If you need any assistance installing the Btrfs filesystem on Fedora, check my article Install and Use Btrfs on Fedora 33.

Creating a Btrfs Filesystem

To experiment with resizing a Btrfs filesystem, we need to create a Btrfs filesystem. So, let’s create a Btrfs filesystem data on the partition sdb1.

To create a Btrfs filesystem with the label data on the sdb1 partition, run the following command:

$ sudo mkfs.btrfs -L data /dev/sdb1

Create a directory /data with the following command:

$ sudo mkdir -v /data


Mount the Btrfs partition sdb1 (that you have created earlier) on the /data directory with the following command:

$ sudo mount /dev/sdb1 /data


As you can see, the Btrfs filesystem data mounted on the /data directory has only one storage device (sdb1) added to it, and the ID of the storage device is 1.

$ sudo btrfs device usage /data


The size of the filesystem is 10 GB (Device size). Out of 10 GB of disk space, 9.48 GB is not used (Unallocated), 8 MB is allocated for storing data (Data, single), 512 MB is allocated for the filesystem metadata (Metadata, DUP), and 16 MB is allocated for system data (System, Dup).

The entire disk space of the partition sdb1 is on the Btrfs filesystem pool (can be used). So, 0 byte is outside of the filesystem pool ( Device slack).


The Btrfs filesystem mounted on the /data directory is 10 GB in size.

$ df -h /data

Resize a Btrfs Filesystem

You can resize the Btrfs filesystem data that you have created earlier and mounted it on the /data directory online (when it’s mounted).

For example, to shrink the Btrfs filesystem mounted on the /data directory, let’s say, by 1 GB, run the following command:

$ sudo btrfs filesystem resize -1G /data

As shown in the illustration, the Btrfs filesystem removed 1 GB of disk space from the filesystem pool. You can use the slack space (Device slack) to grow/expand the Btrfs filesystem later.

$ sudo btrfs device usage /data


Based on the image below, you can see that the Brtfs filesystem mounted on the /data directory is 9 GB in size. It was previously 10 GB.

$ df -h /data


To grow/expand the Btrfs filesystem mounted on the /data directory, let’s say, by 256 MB, run the following command:

$ sudo btrfs filesystem resize +256M /data


You can see from the picture below that 256 MB of disk space is removed from the Device slack and added to the Btrfs filesystem pool.

$ sudo btrfs device usage /data


As you can see, the Btrfs filesystem mounted on the /data directory is now 256 MB larger than before.

$ df -h /data


To grow/expand the Btrfs filesystem mounted on the /data directory to the maximum available disk space (in Device slack), run the following command:

$ sudo btrfs filesystem resize max /data


The illustration below shows that all the available disk space from the Device slack is added to the Btrfs filesystem pool. So, the Device slack is now 0 byte in size.

$ sudo btrfs device usage /data


The Btrfs filesystem mounted on the /data directory is now 10 GB in size.

$ df -h /data

Resize a Btrfs Partition

You can resize a partition that is added to a Btrfs filesystem offline (when the Btrfs filesystem is not mounted).

WARNING: Be careful when you resize a partition that is added to a Btrfs filesystem as you may lose important data from the partition. Always take a backup before resizing.

As you can see, the Btrfs filesystem we have created in this article has one disk partition (sdb1) added to it. The partition is 10 GB in size.

$ sudo btrfs device usage /data


As shown in the image below,the size of the partition sdb1 is 10 GB.

$ df -h /data


Before you resize the partition, unmount the Btrfs filesystem from the /data directory with the following command:

$ sudo umount /data


The name of the disk that contains the partition sdb1 is sdb.

$ sudo lsblk -e7


Open the disk sdb with a disk partitioning program like fdisk as follows:

$ sudo fdisk /dev/sdb

Type in p and press <Enter> to list all the existing partitions of the storage device sdb.

As you can see below, I have two partitions, sdb1 and sdb2, in the disk sdb. Let’s resize the first partition (sdb1).


To resize a partition, you have to remove the partition, then add it again. So, you have to remember the start sector number of the partition.

For example, the start sector number of the first partition, sdb1, is 2048, as you can see in the screenshot below.


To remove a partition, type in d and press <Enter>.


To remove the first partition (sdb1), type in 1, and press <Enter>. The partition sdb1 should be removed.


To recreate the same partition, type in n and press <Enter>.


Type in 1 as the partition number and press <Enter>.


Type in 2048 as the first sector number and press <Enter>.


I want to demonstrate the process of shrinking the partition. So, I am going to create a smaller partition than before.

Type in +9G (to create a 9 GB partition) and press <Enter>.


We would want to keep the partition signature, so type in N and press <Enter>.


The partition should be created.


To save the changes, type in w and press <Enter>.


Now, mount the Btrfs filesystem on the /data directory as follows:

$ sudo mount /dev/sdb1 /data


Resize the Btrfs filesystem that is mounted on the /data directory for the changes to take effect.

$ sudo btrfs filesystem resize max /data


You can see from the image below that the size of the sdb1 partition that is added to the Btrfs filesystem is reduced to 9 GB (from 10 GB).


You can confirm the partition size change with the df command as well.

$ df -h /data


We can grow/expand a partition that is added to the Btrfs filesystem the same way. Let’s see how to do that.

Unmount the Btrfs filesystem that is mounted on the /data directory as follows:

$ sudo umount /data


Open the disk sdb with a disk partitioning program like fdisk as follows:

$ sudo fdisk /dev/sdb


Now, the first partition sdb1 is 9 GB in size.


The start sector number of the first partition, sdb1, is 2048.


To remove the first partition, type in d and press <Enter>.


Type in 1 as the partition number and press <Enter>. The first partition sdb1 should be removed.


To recreate the first partition sdb1, type in n and press <Enter>.


Type in 1 as the partition number and press <Enter>.


Type in 2048 as the first sector number and press <Enter>.


I will increase the partition size by 500 MB. So, the new partition size should be 9.5 GB.

Type in +9.5G and press <Enter>.


As we would want to keep the filesystem signature, let’s type in N and press <Enter>.


The first partition, sdb1, should be recreated, and its size increased.


To save the changes, type in w and press <Enter>.


Mount the Btrfs partition sdb1 to the /data directory as follows:

$ sudo mount /dev/sdb1 /data


Resize the Btrfs filesystem that is mounted on the /data directory for the changes to take effect.

$ sudo btrfs filesystem resize max /data


As you can see, the partition (sdb1) size has increased to 9.5 GB (from 9 GB).

$ sudo btrfs device usage /data


You can confirm the partition size with the df command as well.

$ df -h /data

Resize a Multi-device Btrfs Filesystem

Btrfs is a multi-device filesystem. It means you can add multiple storage devices or partitions to a single Btrfs filesystem. In this section, I am going to show you how to resize a Btrfs filesystem that has multiple storage devices or partitions added to it. So, let’s get started.

Right now, the Btrfs filesystem that is mounted on the /data directory is 10 GB in size.

$ df -h /data


The partition sdb1 (which has the ID 1) is the only partition added to the Btrfs filesystem.

$ sudo btrfs device usage /data


You can add another partition (let’s say, sdb2) to the Btrfs filesystem, which is mounted on the /data directory with the following command:

$ sudo btrfs device add /dev/sdb2 /data


The newly added partition, sdb2, of the Btrfs filesystem, which is mounted on the /data directory has the ID 2, as you can see in the screenshot below.

$ sudo btrfs device usage /data


As you can see, the size of the Btrfs filesystem, which is mounted on the /data partition, has increased. The disk space of the sdb2 partition is added to the Btrfs filesystem.

$ df -h /data


To resize a Btrfs filesystem that has multiple storage devices added to it, you have to specify which partition of the Btrfs filesystem you want to resize. To specify the partition to resize in a Btrfs filesystem, you have to use the partition ID.

$ sudo btrfs device usage /data


For example, to shrink the partition with the ID 1 by 2 GB of the Btrfs filesystem mounted on the /data directory, you can run the following command:

$ sudo btrfs filesystem resize 1:-2G /data


The 2 GB of disk space is removed from the partition sdb1 of the Btrfs filesystem mounted on the /data directory.

$ sudo btrfs device usage /data


As you can see in the illustration, the Btrfs filesystem is resized (shrank) to 18 GB from 20 GB.

$ df -h /data


In the same way, you can shrink the Btrfs filesystem partition sdb2 using the partition ID 2.

$ sudo btrfs device usage /data


To shrink the partition with the ID 2 by 1 GB of the Btrfs filesystem mounted on the /data directory, you can run the following command:

$ sudo btrfs filesystem resize 2:-1G /data


You can see that 1 GB of disk space is removed from the partition sdb2 of the Btrfs filesystem mounted on the /data directory.

$ sudo btrfs device usage /data


The Btrfs filesystem is resized (shrank) to 17 GB from 18 GB, as shown in the image below.

$ df -h /data


To expand the partition with the ID 1 by 1 GB of the Btrfs filesystem mounted on the /data directory, you can run the following command:

$ sudo btrfs filesystem resize 1:+1G /data


As you can see, 1 GB of disk space from the partition sdb1 is added to the Btrfs filesystem pool.

$ sudo btrfs device usage /data


Now, the Btrfs filesystem is resized (expanded) to 18 GB from 17 GB.

$ df -h /data


To expand the partition with the ID 2 by 1 GB of the Btrfs filesystem mounted on the /data directory, you can run the following command:

$ sudo btrfs filesystem resize 2:+1G /data


You can see that 1 GB of disk space from the partition sdb2 is added to the Btrfs filesystem pool.

$ sudo btrfs device usage /data


The Btrfs filesystem is now resized (expanded) to 19 GB from 18 GB.

$ df -h /data

Conclusion

In this article, I have shown you how to resize a Btrfs filesystem and the partitions added to a Btrfs filesystem. As well as how you can shrink or grow/expand a Btrfs filesystem and the partitions added to a Btrfs filesystem.

]]>
The Comparison of Btrfs vs Ext4 Filesystems https://linuxhint.com/btrfs-vs-ext4-filesystems-comparison/ Fri, 25 Dec 2020 06:47:02 +0000 https://linuxhint.com/?p=82107 There are many filesystems out there for Linux. The most common ones are Ext4, Btrfs, XFS, ZFS, and so on. Each of the filesystems has its use cases, pros, and cons. You may have a hard time deciding which filesystem to use.

In this article, I will compare the Ext4 and the Btrfs filesystem. So, if you’re having a hard time deciding whether to use the Ext4 filesystem or the Btrfs filesystem, then

Introduction to the Ext4 and the Btrfs Filesystems:

Ext4 Filesystem: Ext4 is the fourth version of the Ext (Extended) filesystem. It is a successor to the Ext3 filesystem. The first version of the Ext filesystem was release in 1992 for the Minix operating system. It was later ported on Linux operating systems. The Ext4 filesystem was released in 2008. Ext4 is a journaled filesystem.

Btrfs Filesystem: Btrfs or the B-Tree filesystem is a modern Copy-on-Write (CoW) filesystem. It is new compared to the Ext filesystem. It was designed for the Linux operating systems at Oracle Corporation in 2007. In November 2013, the Btrfs filesystem was declared stable for the Linux kernel.

Feature Comparisons of the Ext4 and Btrfs Filesystems:

The Ext4 and Btrfs filesystem was designed to solve different types of problems. So, the design goal of the Ext4 filesystem was different than the Btrfs filesystem. Still, they are filesystems. They do have some similarities that we can compare.

i. Maximum Partition Size: The Ext4 filesystem supports partition sizes up to 1 EiB.

The Btrfs filesystem supports partition sizes up to 16 EiB.

ii. Maximum File Size: The Ext4 filesystem supports file sizes up to 16 TiB (for standard 4 KiB block size).

The Btrfs filesystem supports file sizes up to 16 EiB.

iii. Maximum Filename Length: The Ext4 filesystem supports up to 255 characters (255 bytes) long file names.

The Btrfs filesystem also supports up to 255 characters (255 bytes) long file names.

iv. Allowed Characters in Directory and Filenames: The Ext4 filesystem allows any characters except the / and NULL (\0) characters in directory and file names.

NOTE: You can’t create a file or directory with the name. and .. in either the Ext4 or the Btrfs filesystem.

v. Maximum Path Length: The Ext4 filesystem does not have any limits to the length of the path of a file or directory. So, you can create very very deep directory structures and keep your files there.

The same is true for the Btrfs filesystem.

vi. Max Number of Files: You can create at max 232 (= 4,294,967,296 ~= 4 billion) files in an Ext4 filesystem.

You can create at max 264 (= 18,446,744,073,709,551,616 ~= 18 quintillion) files in a Btrfs filesystem.

vii. inode Allocation Method: An inode is a filesystem data structure that is used to describe a file or a directory. So, a directory or a file requires 1 inode. 2 directories or 2 files will require 2 inodes.

In the Ext4 filesystem, you define the number of inodes the filesystem can supports while you create the filesystem. You can’t change it after the filesystem is created. If you create too many small files, you may have free disk space left on your filesystem, but you won’t be able to create new files/directories unless you have free inodes. This is a big limitation of the Ext4 filesystem.

In the Btrfs filesystem, the inode allocation is flexible. The filesystem can add as many inodes as needed. So, you will never run out of inodes.

viii. Checksum/ECC Support: The Ext4 filesystem does not keep checksum of the data stored on the filesystem.

The Btrfs filesystem keeps crc32c checksum of the data stored on the filesystem. So, in case of any data corruption, the Btrfs filesystem can detect it and recover the corrupted file.

ix. Journal and Copy-on-Write Support: The Ext4 filesystem is a journaling filesystem. It does not have any Copy-on-Write (CoW) support.

The Btrfs filesystem is a Copy-on-Write (CoW) filesystem, and it does not have any journal support.

x. Filesystem Snapshot: The Ext4 filesystem can’t take snapshots of the filesystem.

The Btrfs filesystem can take snapshots. You can take read-only snapshots and writable snapshots.

NOTE: Filesystem snapshot is an important feature. Using this feature, you can take a snapshot of your filesystem before trying out anything risky. If things do not go as planned, you can go back to an early state where everything worked. This is a built-in feature of the Btrfs filesystem. You don’t need any 3rd-party tools/software to do that on a Btrfs filesystem.

xi. Filesystem-level Encryption: The Ext4 filesystem has experimental support for filesystem-level encryption.

The Btrfs filesystem does not have any support for filesystem-level encryption.

xii. Filesystem-level Deduplication: The Ext4 filesystem does not have deduplication support.

The Btrfs filesystem supports deduplication on the filesystem-level. You don’t need any 3rd-party tools/software for that.

NOTE: Depulication is a technique to eliminate/remove duplicate copies of data from the filesystem and keep only one copy of data (unique data) on the filesystem. This technique is used to save disk spaces.

xiii. Multiple Devices Support: The Btrfs filesystem supports multiple devices and has built-in RAID support. The Btrfs filesystem has a built-in logical volume manager (LVM) that is used to add multiple storage devices or partitions to a single Btrfs filesystem. A single Btrfs filesystem can span over multiple disks and partitions.

The Ext4 filesystem does not support multiple devices. You can’t span a single Ext4 filesystem over multiple disks or partitions. To combine multiple storage devices and partitions in an Ext4 filesystem, you have to use 3rd-party logical volume managers like LVM 2. To set up RAID, you have to use 3rd-party tools like DM-RAID or MDADM.

xiv. Filesystem-level Compression: The Ext4 filesystem does not have built-in filesystem-level compression support.

The Btrfs filesystem has built-in filesystem-level compression support. It can compress a single directory or a single file or the entire filesystem to save disk space.

xv. Offline Filesystem Resize Capabilities: The Ext4 filesystem has support for offline filesystem growing (increase filesystem size) and shrinking (decrease filesystem size).

The Btrfs filesystem also supports offline filesystem growing and shrinking.

xvi. Online Filesystem Resize Capabilities: The Ext4 filesystem has support for online growing (increase filesystem size when mounted). But it has no support for online filesystem shrinking (decrease filesystem size when mounted).

You can grow (increase filesystem size) and shrink (decrease filesystem size) Btrfs filesystems online (when mounted).

xvii. Sparse files: Sparse file feature save disk space when small files (smaller than the block size) are stored on the filesystem. The Ext4 and the Btrfs filesystem supports sparse files.

xviii. Block sub-allocation: The Ext4 filesystem does not support block sub-allocation.

The Btrfs filesystem supports block sub-allocation.

NOTE: When a filesystem stores large files in a filesystem, the large file is broken into blocks, and the blocks are stored in the filesystem. The last block of the file does not occupy the entire block. This last block is called the tail block. In the same way, when a lot of small files are stored, they do not occupy the entire block. So, a lot of disk space is wasted. Block sub-allocation is a method to store parts of another file block to the tail block (the last block of another file that did not occupy the entire block) and save disk spaces.

xix. Tail packing: The Ext4 filesystem does not support tail packing.

The Btrfs filesystem supports tail packing.

NOTE: Tail packing is a part of block sub-allocation. As I have already discussed, small files do not occupy an entire file block. So, to efficiently store small files (i.e. program source codes) in the filesystem, the tail block of a small file is used to store other small files. Tail packing improves the filesystem performance and saves a lot of disk space in a filesystem where lots of small files (i.e. program source codes) are stored.

xx. Extent-based Filesystem: Both the Ext4 and the Btrfs filesystems are extent-based filesystems.

NOTE: An extent is a contiguous area of the storage device which is reserved for a file in a filesystem. Extent-based filesystems store large files in a contiguous storage area. This improves filesystem performance and increases storage efficiency.

xxi. Variable file block size: The Ext4 filesystem supports fixed block size. The block size is set before the filesystem is created. Once the filesystem is created, you can’t change the block size.

The Btrfs filesystem supports variable block size. The filesystem can determine the best possible block size to store a file on the filesystem based on the size of the file. This feature can save a lot of disk space.

xxii. Allocate-on-flush: Both the Ext4 and the Btrfs filesystem supports allocate-on-flush.

NOTE: The filesystem allocates some buffer space in the memory of the computer. When there are disk write requests, the filesystem does not write the data blocks directly on the storage device. Instead, the filesystem stores the data blocks in the buffer memory. When the buffer memory is full, the filesystem writes all the pending data blocks to the storage device at once. This reduces CPU usage, speeds up disk writes and reduces disk fragmentation.

xxiii. TRIM support: Both the Ext4 and the Btrfs filesystem support TRIM. It is a very important feature for SSD storage devices.

NOTE: When you remove a file from an SSD, the TRIM command notifies the SSD storage device of the pages (file blocks) that are no longer needed. The SSD erases the unnecessary pages (file blocks) from the flash storage and prepares the pages (file blocks) for storing new data. Without TRIM support, the SSD write speed would get slower as the SSD is filled with new data.

Advantages of Ext4 over Btrfs:

The Ext4 filesystem is a very old filesystem. It has been used on the Linux operating system for a long, long time. Because of that, the Ext4 filesystem is very stable. The Ext4 filesystem is still the default filesystem in many popular Linux distributions (i.e. Ubuntu/Debian). If you need to store some data as an ordinary Linux user, you can keep your eyes closed and use the Ext4 filesystem. The Ext4 filesystem has journaling support. So, your files should be safe even when there’s a power failure. It’s a good filesystem for everyday use.

Advantages of Btrfs over Ext4:

The Btrfs filesystem is a modern Copy-on-Write (CoW) filesystem that was designed for high-capacity and high-performance storage servers. So, it has a lot of advanced features that the Ext4 filesystem does not have. The Ext4 filesystem was designed to be a simple local filesystem.

The main features of the Btrfs filesystem that are useful to everyday Linux users are:

  1. Built-in Filesystem-level snapshots.
  2. Multiple device support.
  3. Built-in RAID support.
  4. Flexible inode allocation.
  5. Optimizations for storing smaller files (sparse files, block sub-allocation, tail packing, variable block size).
  6. Built-in filesystem-level compression support.

These are the filesystem features for which you may choose to use the Btrfs filesystem over the Ext4 filesystem.

Conclusion:

In this article, I have compared the Btrfs and the Ext4 filesystems. I have compared the main features of the Btrfs and Ext4 filesystem. This article should help you decide between the Btrfs and the Ext4 filesystem.

References:

  1. ext4 – Wikipedia – https://en.wikipedia.org/wiki/Ext4
  2. Btrfs – Wikipedia – https://en.wikipedia.org/wiki/Btrfs
  3. kernel/git/torvalds/linux.git – Linux kernel source tree – https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=4204617d142c0887e45fda2562cb5c58097b918e
  4. Comparison of Filesystems – Wikipedia – https://en.wikipedia.org/wiki/Comparison_of_file_systems
  5. Data deduplication – Wikipedia – https://en.wikipedia.org/wiki/Data_deduplication
  6. Sparse file – Wikipedia – https://en.wikipedia.org/wiki/Sparse_file
  7. Block suballocation – Wikipedia – https://en.wikipedia.org/wiki/Block_suballocation
  8. Extent (file systems) – Wikipedia – https://en.wikipedia.org/wiki/Extent_(file_systems)
  9. Allocate-on-flush – Wikipedia – https://en.wikipedia.org/wiki/Allocate-on-flush
  10. Trim (computing) – Wikipedia – https://en.wikipedia.org/wiki/Trim_(computing)
]]>
Comparison Between Btrfs and XFS Filesystems https://linuxhint.com/comparison-between-btrfs-and-xfs-filesystems/ Thu, 24 Dec 2020 19:44:38 +0000 https://linuxhint.com/?p=82941 There are many filesystems available for use with Linux. The most common Linux filesystems include Ext4, Btrfs, XFS, and ZFS. Every filesystem has its unique use cases, pros, and cons. Due to the variety of options available, you may have a hard time deciding which filesystem to use.To help you with your choice, this article compares the XFS and the Btrfs filesystems. If you are having a hard time deciding whether to use the XFS filesystem or the Btrfs filesystem, then this article should help. Let us begin!

Introduction to XFS and Btrfs Filesystems

XFS Filesystem: XFS is a high-performance 64-bit journaling filesystem. It was originally developed by Silicon Graphics, Inc. in 1993 for the IRIX operating system and was later ported to the Linux kernel in 2001.

Btrfs Filesystem: Btrfs or the B-Tree filesystem is a modern Copy-on-Write (CoW) filesystem. It is new compared to the Ext filesystem. Btrfs was originally designed for the Linux operating systems by the Oracle Corporation in 2007. In November 2013, the Btrfs filesystem was declared stable for the Linux kernel.

Feature Comparison

The XFS and Btrfs filesystems were designed to solve different types of problems. Though the design goal of the XFS filesystem was different than that of the Btrfs filesystem, because they are both filesystems, we may compare them in depth.

  • Maximum Partition Size: The XFS filesystem supports partition sizes of up to 1 byte less than 8 EiB (8 EiB – 1 byte).
  • The Btrfs filesystem supports partition sizes of up to 16 EiB.
  • Maximum File Size: The XFS filesystem supports file sizes of up to 1 byte and less than 8 EiB (8 EiB – 1 byte).
  • The Btrfs filesystem supports file sizes of up to 16 EiB.
  • Maximum Filename Length: The XFS filesystem supports filenames up to 255 characters (255 bytes) in length.
  • The Btrfs filesystem also supports filenames up to 255 characters (255 bytes) in length.
  • Allowed Characters in Directory and Filenames: The XFS filesystem allows any characters except the / and NULL (\0) characters in directory and file names.
  • The Btrfs filesystem also allows any characters except the / and NULL (\0)characters in directory and file names.

NOTE: You cannot create a file or directory with the name . and .. in either of the XFS or Btrfs filesystems.

  • Maximum Path Length: The XFS filesystem does not have any limits to the length of the path of a file or directory. So, you can create deep directory structures and keep your files in these structures.
  • The same is true for the Btrfs filesystem.
  • Max Number of Files: You can create a maximum of 264 (= 18,446,744,073,709,551,616 ~= 18 quintillion) files in an XFS filesystem.
  • The same is true for the Btrfs filesystem.
  • Inode Allocation Method: An inode is a filesystem data structure used to describe a file or a directory. So, a single directory or file requires one inode, two directories or files will require two inodes, and so on.
  • In the Ext4 filesystem, you define the number of inodes the filesystem can support when creating the filesystem. You cannot change this after the filesystem has been created. If you create too many small files, you may have free disk space left on your filesystem, but you will not be able to create new files/directories unless you have free inodes. This is a major limitation to the Ext4 filesystem.
  • Unlike the Ext4 filesystem, inode allocation is flexible in the XFS filesystem. So, the filesystem can add as many inodes as needed and you will never run out of inodes.
  • The above is also true for the Btrfs filesystem.
  • Checksum/ECC Support: The Btrfs filesystem keeps crc32c checksum of the data and metadata stored in the filesystem. So, in the case of data corruption, the Btrfs filesystem can detect the corruption and recover the corrupted files or metadata.
  • The XFS filesystem only keeps the CRC32 checksum of the metadata. It does not keep a checksum of the data stored in the filesystem, unlike the Btrfs filesystem.
  • Journal and Copy-on-Write Support: The XFS filesystem is a journaling filesystem. It does not have Copy-on-Write (CoW) support.
  • The Btrfs filesystem is a Copy-on-Write (CoW) filesystem and it does not have journal support.
  • Filesystem Snapshot: The XFS filesystem cannot take snapshots of the filesystem.
  • The Btrfs filesystem can take snapshots of the filesystem. With Btrfs, you may take read-only snapshots and writable snapshots of the filesystem.

NOTE: The filesystem snapshot is an important feature. You may take a snapshot of your filesystem using this feature before attempting any risky actions. If things do not go as planned, a snapshot allows you to go back to an earlier state in which everything in the system worked. This is a built-in feature of the Btrfs filesystem. You do not need any third-party tools or software to generate a snapshot of a Btrfs filesystem.

  • Filesystem-level Encryption: The Btrfs filesystem does not support filesystem-level encryption.
  • The same is true for the XFS filesystem.
  • Filesystem-level Deduplication: The Btrfs filesystem supports deduplication at the filesystem level. You do not need any third-party tools or software to use this feature.
  • The XFS filesystem also has deduplication support, but the deduplication feature of the XFS filesystem is still experimental.

NOTE: Depulication is a technique for eliminating duplicate copies of data from the filesystem and keeping only one copy of the data (unique data) in the filesystem. This technique is used to save disk space.

  • Multiple Devices Support: The Btrfs filesystem supports multiple devices and includes built-in RAID support. The Btrfs filesystem has a built-in logical volume manager (LVM) for adding multiple storage devices or partitions to a single Btrfs filesystem. A single Btrfs filesystem can span over multiple disks and partitions.
  • The XFS filesystem does not support multiple devices, meaning that you cannot span a single XFS filesystem over multiple disks or partitions. To combine multiple storage devices and partitions in an XFS filesystem, you must use third-party logical volume managers, such as LVM 2. To set up RAID, you must use third-party tools such as dm-raid or mdadm.
  • The XFS filesystem was designed to execute I/O (input/output) operations in parallel. If you span the XFS filesystem over multiple devices using LVM 2 or a different logical volume manager, the filesystem performance will be increased.
  • Filesystem-level Compression: The XFS filesystem does not include built-in filesystem-level compression support.
  • The Btrfs filesystem includes built-in filesystem-level compression support. This feature allows you to compress a single directory, a single file, or the entire filesystem to save disk space.
  • Offline Filesystem Resize Capabilities: You cannot grow (increase filesystem size) or shrink (decrease filesystem size) an XFS filesystem while the filesystem is not mounted.
  • You can grow (increase filesystem size) or shrink (decrease filesystem size) a Btrfs filesystem while the filesystem is not mounted.
  • Online Filesystem Resize Capabilities: You can grow (increase filesystem size) an XFS filesystem while the filesystem is mounted, but you cannot shrink (decrease filesystem size) an XFS filesystem while the filesystem is mounted.
  • You can grow (increase filesystem size) or shrink (decrease filesystem size) a Btrfs filesystem while the filesystem is mounted.
  • Sparse files: The sparse file feature saves disk space when small files (smaller than the block size) are stored on the filesystem. The XFS and the Btrfs filesystems both support sparse files.
  • Block sub-allocation: The Btrfs filesystem supports block sub-allocation.
  • The XFS filesystem does not support block sub-allocation.

NOTE: When a filesystem stores large files in a filesystem, the large file is broken into blocks, and the blocks are stored in the filesystem. The last block of the file, called the tail block, does not occupy the entire block. When many small files are stored, they do not occupy the entire block, and a lot of disk space is wasted. Block sub-allocation allows you to store parts of another file block in the tail block (the last block of another file that did not occupy the entire block) to save disk space.

  • Tail packing: The Btrfs filesystem supports tail packing.
  • The XFS filesystem does not support tail packing.

NOTE: Tail packing is a part of block sub-allocation. As previously discussed, small files do not occupy an entire file block. To efficiently store small files (e.g., program source codes) in the filesystem, the tail block of a small file is used to store other small files. Tail packing improves filesystem performance and saves disk space in filesystems in which many small files (e.g., program source codes) are stored.

  • Extent-based Filesystem: Both the XFS and Btrfs filesystems are extent-based filesystems.

NOTE: An extent is a contiguous area of the storage device reserved for a file in a filesystem. Extent-based filesystems store large files in a contiguous storage area. This improves filesystem performance and increases storage efficiency.

  • Variable file block size: The block size is set before the filesystem is created. Once the filesystem is created, you cannot change the block size.
  • The XFS filesystem supports fixed block size.
  • The Btrfs filesystem supports variable block size. The filesystem can determine the best possible block size to store a file on the filesystem based on the size of the file. This feature can save a lot of disk space.
  • Allocate-on-flush: Both the XFS and Btrfs filesystems support allocate-on-flush.

NOTE: The filesystem allocates some buffer space in the system memory. When there are disk write requests, the filesystem does not write the data blocks directly on the storage device. Instead, the filesystem stores the data blocks in the buffer memory. When the buffer memory is full, the filesystem writes all the pending data blocks to the storage device at once. This reduces CPU usage, speeds up disk writes, and reduces disk fragmentation.

  • TRIM support: Both the XFS and Btrfs filesystems support TRIM, which is a very important feature for SSD storage devices.

NOTE: When you remove a file from an SSD, the TRIM command notifies the SSD storage device of the pages (file blocks) that are no longer needed. The SSD erases the unnecessary pages (file blocks) from the flash storage and prepares the pages (file blocks) for storing new data. Without TRIM support, the SSD write speed would become progressively slower as the SSD fills with new data.

Advantages of XFS over Btrfs

XFS is a stable 64-bit journaling filesystem for high-capacity storage devices.

You may use the XFS filesystem for the following reasons:

  • Parallel I/O (Input/Output) Support

The XFS filesystem supports parallel I/O and can provide multiple data streams for files due to its design.

  • Large partition support

The XFS filesystem supports partition sizes of up to 8 EiB (up to 8 EiB – 1 byte).

  • Large file support

The XFS filesystem supports file sizes of up to 8 EiB (up to 8 EiB – 1 byte).

  • Journaling Support

Journaling ensures data consistency in the filesystem in the event of a power outage or system crash. In the event of a power outage or system crash, the data stored in the journal will be recovered and applied to the filesystem.

  • Direct I/O

This is an important feature of the XFS filesystem. It is essential for applications that require high read/write speed to storage devices. Direct I/O allows storage devices direct access to the data buffer using DMA (Direct Memory Access) so that the full I/O bandwidth of the storage device can be utilized.

  • Guaranteed-rate I/O

The XFS filesystem can reserve the bandwidth of the storage device for certain applications. This feature is ideal for real-time applications (e.g., video streaming).

Disadvantages of the XFS Filesystem

There are some disadvantages to the XFS filesystem.

Disadvantages of the XFS filesystem include the following:

  • No Built-in LVM Support

Compared to the Btrfs filesystem, the XFS filesystem does not have a built-in logical volume manager. So, you will have to use LVM 2 for logical volume management.

  • No Built-in RAID Support

Compared to the Btrfs filesystem, the XFS filesystem does not have built-in RAID support. So, you will have to use dm-raid or mdadm to configure RAID.

  • No Snapshot Support

The XFS filesystem does not have a filesystem snapshot feature, unlike the Btrfs filesystem.

  • Journaling Cannot Be Disabled

As with some other journaling filesystems, you cannot disable the journaling feature of the XFS filesystem. Journaling is not good for USB flash drives. If you use the XFS filesystem on a USB flash drive, the lifetime of the USB flash disk will be reduced due to the journaling overload.

Advantages of Btrfs over XFS

The Btrfs filesystem is a modern Copy-on-Write (CoW) filesystem designed for high-capacity and high-performance storage servers. XFS is also a high-performance 64-bit journaling filesystem that is also capable of parallel I/O operations. The XFS filesystem contains many important features, including Direct I/O, Guaranteed-rate I/O, and more. Compared to the XFS filesystem, however, the Btrfs filesystem has many advantages.

The advantages of the Btrfs filesystem over the XFS filesystem include the following:

i) Built-in Filesystem-level snapshots.

ii) Multiple device support.

iii) Built-in RAID support.

iv) Flexible inode allocation.

v) Optimizations for storing smaller files (sparse files, block sub-allocation, tail packing, variable block size).

vi) Built-in filesystem-level compression support.

These are the filesystem features that may cause you to choose the Btrfs filesystem over the XFS filesystem.

Conclusion

This article compared the Btrfs and XFS filesystems, including a comparison of the most important features of each filesystem. This article should help you to decide between the Btrfs and XFS filesystems. Choose whichever system works best for you, according to your unique needs and preferences.

References:

  1. XFS – Wikipedia – https://en.wikipedia.org/wiki/XFS
  2. Comparison of file systems – Wikipedia – https://en.wikipedia.org/wiki/Comparison_of_file_systems
  3. XFS – ArchWiki – https://wiki.archlinux.org/index.php/XFS
]]>
Install and Use Btrfs on Ubuntu 20.04 LTS https://linuxhint.com/install-and-use-btrfs-on-ubuntu-lts/ Sat, 19 Dec 2020 03:34:14 +0000 https://linuxhint.com/?p=81944 Btrfs (B-Tree Filesystem) is a modern copy-on-write (CoW) filesystem for Linux. It aims to implement many advanced filesystem features while focusing on fault tolerance, repair, and easy administration. The Btrfs filesystem is designed to support the requirement of high performance and high-capacity storage servers.

If you want to learn more about the Btrfs filesystem, check my article Introduction to Btrfs Filesystem.
In this article, I am going to show you how to install and use Btrfs on Ubuntu 20.04 LTS. So, let’s get started.

Installing Btrfs Filesystem

The Btrfs filesystem package is available in the official package repository of Ubuntu 20.04 LTS, so you can easily install it from there.

First, update the APT package repository cache with the following command:

$ sudo apt update


To install the Btrfs filesystem on Ubuntu 20.04 LTS, run the following command:

$ sudo apt install btrfs-progs -y


The Btrfs filesystem should be installed.

Partitioning the Disk

You don’t have to partition your HDD/SSD to create a Btrfs filesystem; you can create it on your bare unpartitioned HDD/SSD. But you may want to partition your HDD/SSD before you format your HDD/SSD with the Btrfs filesystem.
You can list all the storage devices and partitions of your computer with the following command:

$ sudo lsblk -e7


I have an HDD sdb on my computer, as you can see in the screenshot below. I will be partitioning the HDD sdb and formatting the partitions with the Btrfs filesystem for the demonstration in this article.


To partition the HDD sdb, I will use the cfdisk partitioning tool.
You can open the HDD sdb with the cfdisk partitioning tool as follows:

$ sudo cfdisk /dev/sdb


Select gpt and press <Enter>.


To create a new partition, select Free space, then select [ New ], and press <Enter>.


Type in the size of the partition you want to create. I will create a 10 GB partition. So, I will type in 10G.

You can use the following symbols to create partitions of different sizes/units:
M – partition size in megabyte unit
G – partition size in gigabyte unit
T – partition size in terabyte unit
S –number of sectors you want to use for the partition
Once you’re done, press <Enter>.


A new partition (sdb1 in my case) should be created.


Let’s create another partition.
To do that, select the Free space, then select [ New ], and press <Enter>.


Type in the size of the partition, and press <Enter>.


A new partition (sdb2 in my case) should be created.


To write the changes to the disk, select [ Write ] and press <Enter>.


To confirm the changes, type in yes and press <Enter>.

The partition table should be saved to the disk.

To quit cfdisk program, select [ Quit ] and press <Enter>.

Formatting a Disk with Btrfs Filesystem

In this section, I am going to show you how to format a partition with the Btrfs filesystem.
I have created 2 partitions, sdb1 and sdb2, in the earlier section of this article. I will format the partition sdb1 with the Btrfs filesystem for the demonstration.

$ sudo lsblk -e7

To format the partition sdb1 with the Btrfs filesystem, run the following command:

$ sudo mkfs.btrfs -L data /dev/sdb1

NOTE: Here, the -L flag sets the label of the partition. In this case, the partition label is data.

The partition sdb1 should be formatted with the Btrfs filesystem.

Mounting a Btrfs Filesystem

To mount a Btrfs filesystem, you need to create a directory (mount point) where you can mount the Btrfs filesystem.

To create a directory/mount point /data, run the following command:

$ sudo mkdir -v /data

Once the /data mount point is created, you can mount the sdb1 Btrfs filesystem on the /data mount point with the following command:

$ sudo mount /dev/sdb1 /data

The Btrfs partition sdb1 should be mounted in the /data mount point, as you can see in the screenshot below.

$ sudo lsblk -e7

Checking Btrfs Filesystem Usage Information

Checking the usage information of your Btrfs filesystem is very important, and there are many ways to check it. Let’s see some of them.

You can use the following command to see the usage summary of all the Btrfs filesystems on your computer:

$ sudo btrfs filesystem show

The usage summary of all the Btrfs filesystems on your computer should be displayed.
You should find the following usage information here:

  • The label of each of the Btrfs filesystems on your computer.
  • The UUID of each of the Btrfs filesystems on your computer.
  • The total number of devices added to each of the Btrfs filesystems on your computer.
  • The disk usage information of each of the storage devices added to each of the Btrfs filesystems on your computer.

To find disk usage information about a specific Btrfs filesystem mounted on a specific directory path (e.g., /data), run the following command:

$ sudo btrfs filesystem usage /data

As you can see, a lot of disk usage information about the Btrfs partition mounted on the /data mount point is displayed.

On the top, you should find the total disk size of the Btrfs filesystem.

You should also find the amount of disk space the Btrfs filesystem has allocated (reserved for storing data) and the amount of disk space that is used from the allocated/reserved disk space.

You should also find the amount of disk space the Btrfs filesystem did not allocate (did not reserve for storing data) yet and the estimated amount of disk space (allocated and unallocated) that is still available for storing new data.

On the bottom, you should find the following information:

  • The total amount of disk space allocated and used for data from all the storage devices added to the Btrfs filesystem.
  • The amount of disk space allocated for data in each of the storage devices added to the Btrfs filesystem.
  • The total amount of disk space allocated and used for metadata from all the storage devices added to the Btrfs filesystem.
  • The amount of disk space allocated for metadata in each of the storage devices added to the Btrfs filesystem.
  • The total amount of disk space allocated and used for the Btrfs system data from all the storage devices added to the Btrfs filesystem.
  • The amount of disk space allocated for the Btrfs system data in each of the storage devices added to the Btrfs filesystem.
  • The amount of unallocated disk space in each of the storage devices added to the Btrfs filesystem.

On the bottom, you should also find the method (i.e., single, DUP) that is used to allocate disk space for the data, metadata, and system data:

  • For single-mode allocation, the Btrfs filesystem will keep only one instance of the allocation. There won’t be any duplicates.
  • For DUP mode allocation, the Btrfs filesystem will allocate the disk space in different parts of the filesystem for the same purpose. So, multiple copies (usually two) of the same data will be kept on the filesystem.
  • Usually, the data is allocated in the single mode. The metadata and the system data are allocated in DUP mode.
  • In single mode, notice that the Btrfs filesystem can use all the allocated disk space.
  • In DUP mode, notice that the Btrfs filesystem can use half the disk space from the total allocated disk space.

To see the summary of the disk space allocated and used for the data, metadata, and system of a Btrfs filesystem mounted in the /data directory, run the following command:

$ sudo btrfs filesystem df /data

You can also list the disk usage information of each of the files and directories of the Btrfs filesystem mounted on the /data directory as follows:

$ sudo btrfs filesystem du /data

In the end, the disk usage summary of all the files and directories of the /data btrfs filesystem should be displayed.

To only see the disk usage summary of the files and directories of the Btrfs filesystem mounted on the /data directory, run the following command:

$ sudo btrfs filesystem du -s /data

Adding More Storage Devices to a Btrfs Filesystem

If you need more disk space on your Btrfs filesystem, you can add more storage devices or partitions to the Btrfs filesystem to expand the disk space of the filesystem.

For example, to add the partition sdb2 on the Btrfs filesystem mounted on the /data directory, run the following command:

$ sudo btrfs device add /dev/sdb2 /data

As you can see, the new partition sdb2 is added to the Btrfs filesystem mounted on the /data directory.

$ sudo btrfs device usage /data

As you can see, the size of the Btrfs filesystem mounted on the /data directory has increased.

$ df -h -x squashfs -x tmpfs -x devtmpfs

Mounting a Btrfs Filesystem at Boot Time

Once you have set up a Btrfs filesystem, you don’t want to mount it manually every time you boot your computer, rather mount it in automatically. Let’s see how to do that.

First, find the UUID of the Btrfs filesystem mounted on the /data directory as follows:

$ sudo btrfs filesystem show /data

In my case, the UUID of the Btrfs filesystem is c9333c36-f8bf-4825-9706-8b5f28cb4e34. It will be different for you. So, make sure to replace it with yours from now on.

Open the /etc/fstab file with the nano text editor as follows:

$ sudo nano /etc/fstab

At the end of the /etc/fstab file, type in the following line:

UUID=c9333c36-f8bf-4825-9706-8b5f28cb4e34       /data   btrfs   defaults    0 0

Once you’re done, press <Ctrl> + X, followed by Y, and then <Enter> to save the /etc/fstab file.

For the changes to take effect, reboot your computer with the following command:

$ sudo reboot

Once your computer boots, you should see that the Btrfs filesystem is correctly mounted in the /data directory at boot time, as you can see in the screenshot below.

$ df -h -x squashfs -x tmpfs -x devtmpfs

Conclusion

In this article, I have shown you how to install and use the Btrfs filesystem on Ubuntu 20.04 LTS. This information imparted here should help you get started with the Btrfs filesystem on Ubuntu 20.04 LTS.

]]>
Introduction to Btrfs Filesystem https://linuxhint.com/btrfs-filesystem-beginner-guide/ Sat, 12 Dec 2020 08:12:46 +0000 https://linuxhint.com/?p=81041 Btrfs (B-Tree Filesystem) is a modern copy-on-write (CoW) filesystem for Linux. Btrfs aims to implement many advanced filesystem features while focusing on fault tolerance, repair, and easy administration. The btrfs filesystem is designed to support the requirement of high performance and large storage servers. It is suitable for petabyte-scale data centers as well as cellular smartphones.In this article, I am going to discuss the Btrfs filesystem and its features. So, let’s get started.

Copy on Write – CoW Filesystem:

Btrfs is a copy-on-write (CoW) filesystem. In a CoW filesystem, when you try to modify data on the filesystem, the filesystem copies the data, modifies the data, and then writes the modified data back to a different free location of the filesystem.

The main advantage of the Copy-on-Write (CoW) filesystem is that the data extent it wants to modify is copied to a different location, modified, and stored in a different extent of the filesystem. The original data extent is not modified. So, the btrfs filesystem can eliminate the risk of data corruption or partial update in case of power failure during data modification as the original data is kept unchanged.

The main disadvantage of the Copy-on-Write (CoW) filesystem is that big files tend to get fragmented as they are modified. So, defragmentation is required once in a while. Luckily, the btrfs filesystem supports online defragmentation. So, you don’t have to unmount the filesystem to defragment a btrfs filesystem.

Main Features of Btrfs Filesystem:

The main features of the Btrfs filesystem are:

i) Extent based file storage: In an extent based filesystem, the storage unit is called an extent. An extent is a contiguous area of storage that is reserved for a file. One file requires one extent, no matter how small the file is. For larger files (file size larger than the extent size), multiple extents will be required. For larger files, metadata will be used to keep track of the extents the file are using. In the Btrfs filesystem, the metadata is significantly smaller in size. Smaller metadata improves storage efficiency and the performance of the filesystem.

ii) Huge file size support: In a Btrfs filesystem, a single file can be about 264 bytes or 16 EiB (exbibytes) in size. No matter how big your file becomes, Btrfs can support it.

iii) Space-efficient packaging of small files: Normally, no matter how small a file is, it will require one block or one extent to store the file. This wastes a lot of disk space. To solve this problem, the Btrfs filesystem embeds smaller files in the metadata to store smaller files efficiently.

iv) Space-efficient indexed directories: The btrfs filesystem directories are indexed in two different ways. For filename lookup, key-based indexing is used. To reference data, inode-based key indexing is used. Two-level indexing improves directory/file lookup performance and reduces storage requirements for the indexes.

v) Dynamic inode allocation: You need 1 inode to reference 1 file. Many filesystems (i.e., Ext4) have a fixed number of inodes. So, if you create too many small files, you may have a lot of space left on your disk, but you won’t be able to create any new files. You also can’t increase the maximum number of inodes once the filesystem is created.

Btrfs solves this problem by allocating inodes dynamically as they are required. So, you can create as many files as you want as long as you have free disk space.

vi) Writable snapshots and read-only snapshots: The Btrfs filesystem supports snapshots. You can take a snapshot of the current filesystem, which you can use to restore your data if you have accidentally removed some files or corrupted some data.

By default, the btrfs snapshots are read-only. Once you’ve taken a read-only snapshot, you can’t change any files/directories in that snapshot. In any case, if you want to change any files/directories after you have taken a snapshot of your existing Btrfs filesystem, you can change the read-only snapshot to a writable snapshot and modify any files/directories in that snapshot.

vii) Subvolumes: A Btrfs filesystem can have many subvolumes. A subvolume is a named binary tree (B-tree) (or internal/logical filesystem root) of the existing filesystem root tree (main) of the btrfs filesystem. A subvolume is not a block device of its own. But, you can mount Btrfs subvolumes individually. You can think of subvolumes as namespaces.

viii) Subvolume aware quota support: You can allocate quotas for subvolumes as well. Once the quota is exceeded, you won’t be able to add any new data to the subvolume. You won’t need any separate programs to create Btrfs subvolume quotas.

ix) Checksums on data and metadata: To avoid data corruption, Btrfs uses crc32c checksum algorithms for the data and the filesystem’s metadata by default. The checksums are stored in the filesystem to automatically check for filesystem errors and data corruptions in the background.

Btrfs has support for many other checksum algorithms: xxhash, sha256, and blake2b.

x) Compression: Btrfs filesystem supports transparent file compression. The compression and decompression of the files in a btrfs filesystem are done in the background automatically.

Btrfs supports 3 compression algorithms: ZLIB, LZO, and ZSTD.

ZLIB is the default compression method of the btrfs filesystem.

xi) Integrated multiple device support: Btrfs filesystems have built-in logical volume manager (LVM) support. You can add multiple storage devices in a single btrfs filesystem. You can also configure RAID arrays on the btrfs filesystem without needing any extra piece of software.

Btrfs filesystem supports data striping, data mirroring, data striping+mirroring, and single and dual parity implementations.

Data striping: If you have added multiple storage devices in the same btrfs filesystem, btrfs can store the same file on different physical devices/partitions. This is called data striping. Data striping improves the read/write performance of the filesystem. RAID-0 uses the data striping feature extensively.

Data mirroring: If you have added multiple storage devices in the same btrfs filesystem, all the data written to one storage device will be written to all the other storage devices. This is called data mirroring. RAID-1 uses the data mirroring feature extensively.

Data striping+single parity: RAID-5 uses data striping and single distributed parity. If you have added multiple storage devices in a btrfs filesystem, then RAID-5 will strip the data on multiple storage devices and calculate and store parity blocks across the storage devices. RAID-5 can sustain a single drive failure.

Data striping+double parity: RAID-6 uses data striping and double distributed parity. If you have added multiple storage devices in a btrfs filesystem, then RAID-6 will strip the data on multiple storage devices and calculate and store double parity blocks across the storage devices. RAID-6 can sustain two drive failures. Other than that, it is the same as RAID-5 (data striping+single parity).

Data striping+mirroring: RAID-10 uses data striping and data mirroring at the same time. RAID-10 requires an even number of storage devices of the same size to be added to a single btrfs filesystem. The minimum number of storage devices you can add on a RAID-10 btrfs filesystem is 4. Half the storage device will be used for data striping, and the other half be used for mirroring the data of the first half of the storage devices (where data is striped).

xii) SSD awareness and optimizations: The btrfs filesystem is SSD aware and has some SSD optimization features. The btrfs filesystem also has TRIM/Discard support for SSD storage devices.

The TRIM feature can detect and mark data extents that are no longer used. Once the extends are marked, the btrfs filesystem can wipe them automatically so that the other files can use these data extents.

The Discard feature will remove all the data extends of the SSD. If you want to sell your SSD, this feature may come in handy.

xiii) Efficient incremental backup: Btrfs supports incremental backup. The first time you back up a btrfs filesystem, it takes a snapshot of the current filesystem. Then, any subsequent backups will be compared with the first snapshot, and only the changes will be stored on the disk. So, any subsequent backups will take less disk space, and backups will be faster.

xiv) Background scrub: It is a Btrfs filesystem process used to find and fix errors on the files that have redundant copies (multiple copies) stored in the Btrfs filesystem.

xv) Online filesystem defragmentation: I have explained earlier how the Btrfs Copy-on-Write filesystem works. Larges files are stored in multiple extents of the Btrfs filesystem. As you modify large files, the extents that are to be modified are copied to different free extents of the filesystem and modified there. So, the unmodified data extents are also kept in case it is required for filesystem recovery. This causes fragmentation (the data extents of a large file will not be continuous and will be scattered around the entire storage device) on the filesystem as large files are modified. Too much fragmentation negatively impacts the filesystem (makes the filesystem read/write operation slower).

To solve this problem, the btrfs filesystem supports online filesystem defragmentation. With online defragmentation, you don’t have to unmount the filesystem to defragment the filesystem. You can keep the filesystem up and running and still defragment it. Defragmentation will move file extents around the filesystem to keep the extents of the same large file as continuous as possible. Defragmentation improves filesystem performance.

xvi) Offline filesystem check: The Btrfs filesystem has many built-in tools that you can use to check for filesystem errors and fix them. You can also fix a broken Btrfs filesystem (that can’t be mounted) with these tools.

xvii) In-place conversion of existing Ext2/3/4 and ReiserFS filesystems: The Btrfs filesystem has a built-in utility btrfs-convert, which you can use to convert an existing Ext2/3/4 and ReiserFS filesystems to a Btrfs filesystem.

The Btrfs filesystem conversion program reads the metadata of an existing Ext2/3/4 ( or ReiserFS) filesystem, creates Btrfs metadata, and stores them on the filesystem. The filesystem keeps both the Btrfs and the Ext2/3/4 (or ReiserFS) metadata. The Btrfs filesystem points to the same file blocks used by the Ext2/3/4 (or ReiserFS) filesystem files. The existing filesystem and data blocks are kept untouched as Btrfs is a Copy-on-Write (CoW) filesystem. When a file is modified, the Btrfs filesystem copies the original data blocks to new free extents and modifies them there.

xviii) Seed devices: The Btrfs filesystem supports seed devices. You can create a read-only filesystem and use it as a template (seed device) to create other Btrfs filesystems. The benefit of doing that is that only the modified data will be written to the new filesystem. The original data (on the seed devices) will be kept as it is. This feature can be used to save a lot of disk space and data redundancy.

xix) Send/receive subvolume changes: The btrfs filesystem can send/receive subvolume changes. The Btrfs filesystem can send the incremental changes of a subvolume to another Btrfs filesystem (can also reside in another computer) that can receive the subvolume changes. This feature is used to take incremental backups of the Btrfs filesystem either locally or remotely. This method is faster and more efficient than rsync.

xx) Batch/Out of band deduplication: The Btrfs filesystem supports batch or out-of-band deduplication. The duplication takes place after a file is written to the filesystem. The Btrfs filesystem actively scans the entire filesystem for identical extents and keeps only one copy of each extent (removes redundant/duplicate extents). The same copy-on-write (CoW) principle is used for this task. Deduplication saves a lot of disk spaces.

xxi) Swapfile support: If you’re using Linux Kernel 5.0 or newer, you can create swapfiles on the Btrfs filesystem.

There are some limitations of Swapfile in a Btrfs filesystem:

– The swapfile must be allocated as NoCoW (not copy-on-write)

– The swapfile must not have any compression enabled.

Stability of Btrfs Filesystem:

The Btrfs filesystem is actively developed by the Btrfs team. Most of the features of the filesystem are stable at the time of this writing. Some of the advanced features are not yet stable enough for a production environment. The Btrfs team is working hard to solve these stability issues.

If you want to use the Btrfs filesystem on your production server, check the official Status – btrfs Wiki page to find out whether the filesystem features you need are stable enough for you or not. Also, make sure to run some tests before the final deployment of your Btrfs filesystem, and do remember to keep backups of your important data. Keeping backup is always important for production environments.

Future Replacement of Ext4 Filesystem:

Btrfs filesystem is being developed rapidly. The Btrfs development team also cares about the stability of the filesystem. So, they try their best to make it as stable as possible while developing the btrfs filesystem. Once the btrfs filesystem is fully developed, and all the features are stable enough, it may replace the Ext4 filesystem.

References:

[1] btrfs Wiki – https://btrfs.wiki.kernel.org/index.php/Main_Page
[2] BTRFS – The Kernel Tree Documentation – https://www.kernel.org/doc/html/latest/filesystems/btrfs.html
[3] BTRFS – Glossary – https://btrfs.wiki.kernel.org/index.php/Glossary
[4] Features of the “Btrfs” Filesystem – https://www.thegeekdiary.com/features-of-the-btrfs-filesystem/
[5] Comparison of Filesystems – https://en.wikipedia.org/wiki/Comparison_of_file_systems
[6] Btrfs design – btrfs Wiki – https://btrfs.wiki.kernel.org/index.php/Btrfs_design
[7] perhaps running out of inodes could be taken “more seriously”? – https://lwn.net/Articles/724522/
[8] Making a Btrfs read-only snapshots writable – https://markandruth.co.uk/2016/12/29/making-a-btrfs-read-only-snapshot-writable
[9] Data striping – https://en.wikipedia.org/wiki/Data_striping
[10] FAQ – btrfs wiki – https://btrfs.wiki.kernel.org/index.php/FAQ
[11] Standard RAID levels – https://en.wikipedia.org/wiki/Standard_RAID_levels
[12] Trim (computing) – https://en.wikipedia.org/wiki/Trim_(computing)
[13] Solid state drive – ArchWiki – https://wiki.archlinux.org/index.php/Solid_state_drive#TRIM
[14] Btrfsck – btrfs Wiki – https://btrfs.wiki.kernel.org/index.php/Btrfsck
[15] Conversion from Ext3/4 and ReiserFS – btrfs Wiki – https://btrfs.wiki.kernel.org/index.php/Conversion_from_Ext3
[16] Incremental Backup – btrfs Wiki – https://btrfs.wiki.kernel.org/index.php/Incremental_Backup
[17] Deduplication – btrfs Wiki – https://btrfs.wiki.kernel.org/index.php/Deduplication
[18] Status – btrfs Wiki – https://btrfs.wiki.kernel.org/index.php/Status ]]> Btrfs vs OpenZFS https://linuxhint.com/btrfs_vs_openzfs/ Thu, 27 Dec 2018 02:39:23 +0000 https://linuxhint.com/?p=34470 Btrfs or B-tree file system is the newest competitor against OpenZFS, arguably the most resilient file system out there. Both the file systems share some commonalities such as having checksum on data blocks, transaction groups and copy-on-write mechanism, making them both target the user groups. So what’s the difference and which one should you use?

1. Copy-on-Write (COW) Mechanism

Both the file systems use copy-on-write mechanism. This means that, if you are trying to modify a file, neither of the file systems will try to overwrite the existing data on the disk with the newer data. Instead, the newer data is written elsewhere and once the write operation is complete, the file system simply points to the newer data blocks and the old blocks get recycled over time. This mechanism allows both the file systems to have features like snapshots and cloning.

COW also prevents edge cases like partial writes,which can happen due to kernel panic or power failure and potentially corrupt your entire entire file system. With COW in place, a write has either happened or not happened, there’s no in between.

2. Pooling and RAID

Both the file systems intend on eliminating the need of a volume manager, raid and other abstractions that sit between the file system and the disks. This is more robust and reliable than having a hardware RAID controller, simply because it eliminates a single point of failure — The RAID controller itself.

OpenZFS offers a stable, reliable and user-friendly RAID mechanism. You can mirror between drives, use RAIDZ1 which spreads your data across 3 or more disk with one parity block. So it can withstand upton 1 disk’s failure per Vdev. Similarly, RAIDZ2 can use 4 or more disks and withstand upto 2 disks failing and similarly we have RAIDZ3.

Btrfs too has these features implemented, the difference is simply that it calls them RAID, instead of RAIDZ and so on. Some more complicated RAID array setups like RAID56 are buggy and not fit for use, at the time of this writing.

3. Licensing

One of the reasons OpenZFS came so late on the GNU/Linux ecosystem is because of its license incompatibility with GNU GPL. Without getting into too much details, Btrfs is under GPL which allows users to take source code and modify it, but the modifications should also be published under GPL and stay open source.

OpenZFS on the other hand, is licensed under CDDL which is much more permissive and allows users to modify and distribute code with a greater degree of freedom.

4. Communities and Companies Behind Them

OpenZFS has a massive community behind it. FreeBSD community, Illumos community and many other open source projects rely on OpenZFS and thus contribute back to the file system. It has grown several fold in terms of code base, user base, features and flexibility ever since its inception. Companies like Delphix, iXsystems, Joyent and many more rely on it and have their developers work on because it is a core component of their business. Many more organizations might be using OpenZFS without our knowledge, thanks to the CDDL license, they don’t have to come forth and say out-right that they use it.

Btrfs had Red Hat as one of the main steward of its community. However, that recieved a major blow a while back when Red Hat deprecated the filesystem this means you won’t be seeing it in any future RHEL and the company won’t provide commercial support for it out-of-the-box. SUSE, however, has gone so far as to make it their default and their is still a thriving community behind the file system with contributions from Facebook, Intel and other 800 pound gorillas of the Silicon Valley.

5. Reliability

ZFS was designed to be reliable right from the beginning. People have zpools dating back to the early 2000s that are still usable and guaranteed to not return erroneous data silently. Yes, there has been a few snafus with files disappearing on for OpenZFS on Linux but given its long history the track record has been surprising clean.

Btrfs, on the other hand, has had issues right from the beginning. With buggy interfaces to straight up data loss and file corruption. Even now, it is bit of a laughing stock in the community. Make of that what you will.

6. Supported OSes

Btrfs has had its origin has a file system for Linux while ZFS was conceived inside Sun, for Solaris OS. However, OpenZFS has long since been ported to FreeBSD, Apple’s OS X, open source derivatives of Solaris. It’s support for Linux came a little later than one would have predicted, but it is here and corporations rely on it. A project for making it run on Microsoft Windows is also making quite a bit of progress, although it is not quite there yet.

Conclusion: A Note on Monocultures

All of this talk may convince you to use OpenZFS to keep your data safe, and that is not a bad course of action. It is objectively better than Btrfs in terms of features, reliability, community and much more. However, in the long run this might not be good for the open source community, in general.

In a post titled similar to this one, the author talks about the dangerous of monocultures. I encourage you to go through this post. The gist of it is this — Options are important. One of the greatest strength of Open Source software (and software, in general)is that we have multiple options to adopt. There’s Apache and then there’s Nginx, there are BSDs and Linux, there is OpenSSL and there is LibreSSL.

If there is a fatal flaw in any of these key technologies, the world will not stop spinning. But with the prevalence of OpenZFS, the storage technology has turned into something of a monoculture. So, I would very much like for the developers and system programmers who are reading this, to adopt not OpenZFS but projects like Btrfs and HAMMER.

]]>