Btrfs: resize, defragment, compress

cool_penguin_smallThe latest most advertised and advanced filesystem from Linuxland is btrfs. It is an extremely flexible filesystem and included as default in many major distros. There are valid reasons behind that. You can not only do many exciting things with btrfs offline (like convert an ext3/4 volume to btrfs) but even when it is online (mounted). We will discuss 3 such features in this article.

Resize

Let’s say you have mounted a btrfs filesystem to /mnt/volume. Here are three common ways to resize it.

//Resize volume to 25GB
# btrfs filesystem resize 25G /mnt/volume
//Shrink volume by 5GB
# btrfs filesystem resize -5G /mnt/volume
//Expand volume by 5GB
# btrfs filesystem resize +5G /mnt/volume
Defragment

You can defragment a single file on a btrfs filesystem! Let’s see.

# btrfs filesystem defragment /

Guess what? The above command just defragments the metadata of the / directory of the volume and not the whole volume. To defragment the volume, use

//defragment all file data recursively, verbose
# btrfs filesystem defragment -r -v /
//defragment all file metadata
# find / -xdev -type d -print -exec btrfs filesystem defragment '{}'

Use the autodefrag option in /etc/fstab to defragment the volume automatically.

Compress

Use the following options with mount or add in /etc/fstab:

//use LZO compression (better compression)
compress=lzo
//use zlib compression (faster)
compress=zlib

Only the files created or modified post mount will be compressed the above way. To re-compress everything on the volume:

# btrfs filesystem defragment -r -v -clzo /
OR
# btrfs filesystem defragment -r -v -czlib /

or you can compress a single file without the -r option as well.

2 thoughts on “Btrfs: resize, defragment, compress”

Leave a Reply

Your email address will not be published. Required fields are marked *