meta data for this page
  •  

This is an old revision of the document!


References

https://www.howtoforge.com/a-beginners-guide-to-btrfs
https://www.howtoforge.com/how-to-convert-an-ext3-ext4-root-file-system-to-btrfs-on-ubuntu-12.10
http://www.oracle.com/technetwork/articles/servers-storage-admin/advanced-btrfs-1734952.html

Compression

Create BTRFS filesystem on multiple block devices:

 mkfs.btrfs  /dev/loop0 /dev/loop1
btrfs filesystem show /dev/loop0
Label: none  uuid: e9a456cd-57d7-4355-a20a-77243d66722e
        Total devices 2 FS bytes used 28.00KiB
        devid    1 size 200.00MiB used 12.00MiB path /dev/loop0
        devid    2 size 200.00MiB used 0.00B path /dev/loop1

As you can see, every block belongs to FS can be used. Also for mount:

mount /dev/loop0 /BTRFS

or

mount /dev/loop1 /BTRFS

Add/remove block devices

Add new block device (extend size)

 btrfs device add /dev/loop2 /BTRFS

Remove device:

btrfs device delete /dev/loop0 /BTRFS

!!! It is possible to remove any device if there is free space to which data can be moved during removal.

Compression

https://btrfs.wiki.kernel.org/index.php/Compression

Compression:

  • ZLIB – slower, higher compression ratio (uses zlib level 3 setting)
  • LZO – faster compression and decompression than zlib, worse compression ratio, designed to be fast

Default compression for kernels up to 3.6 it's ZLIB. For compatibility with old kernel ZLIB is default. LZO is fast and new algo, but can be buggy.

Does a balance operation recompress files?

No. Balance moves entire file extents and does not change their contents. If you want to recompress files, use btrfs filesystem defrag with the -c option.
Balance does a defragmentation, but not on a file level rather on the block group level. It can move data from less used block groups to the remaining ones, eg. using the usage balance filter.

NOTE: Defragmentation with recompression destroys deduplication.

  • Size changed from 6,94 GB to 7,39 GB
  • After deduplication with 1024k block drops to 7.17 GB (net change in shared extents of: 490.0M)
  • After deduplication with 4k block drops from 7.17 GB to 6,94 GB(a net change in shared extents of: 306.6M)

mount options

Can be enabled during mount time:

mount BTRFS -o remount,compress=zlib
mount BTRFS -o remount,compress-force=zlib
if the first portion of data being compressed is not smaller than the original, the compression of the file is disabled – unless the filesystem is mounted with compress-force

compress-force=<method> - Enable compression even for files that don't compress well, like videos and dd images of disks.

Test data of size 651MB takes 371MB after compression.

chattr +c

Also single file compression possible using chattr +c filename.txt

To recompress exisitng files, start defragmentation with -c param:

btrfs filesystem defrag -c file.iso
btrfs filesystem defrag -c -r mydir

It is not possible to get compression ratio of file.

Internals:

  • In compressed extents, individual blocks are not compressed separately; rather, the compression stream spans the entire extent.
  • BTRFS: There is a simple decision logic: if the first portion of data being compressed is not smaller than the original, the compression of the file is disabled – unless the filesystem is mounted with compress-force

btrfs property

btrfs property set <file> compression <zlib|lzo>

Deduplication

External tool to find duplicated extens available https://github.com/markfasheh/duperemove

Simply do 'make' and 'make install' Following binaries will be installed:

  • btrfs-extent-same
  • csum-test
  • duperemove
  • hashstats
  • show-shared-extents

Recursively find duplicates in <dir>

duperemove -r <dir>

The same as above, but make real deduplication (on BTRFS only)

duperemove -rd <dir>

Increase deduplication chance by reducing block size to 4k:

duperemove -b 4k -rd <dir>

This deduplication works nice, it can find some shared parts inside Maildir.

Subvolumes

btrfs subvolume create /BTRFS/sub1
btrfs subvolume create /BTRFS/sub2
btrfs subvolume create /BTRFS/sub3
btrfs subvolume list /BTRFS
ID 256 gen 234 top level 5 path sub1
ID 257 gen 227 top level 5 path sub2
ID 258 gen 228 top level 5 path sub3

Mount subvolume

mount -o subvol=sv1/sv12 /dev/sdb /mnt

Getting data size on each subcvolume:

Different mount options

NOT SUPPORTED YET:

In general, btrfs doesn't _yet_ have the runtime infrastructure to handle 
per-subvolume mount options.  The on-device format and general approach 
in the kernel was designed to allow it, and it's on the roadmap, it just 
hasn't been done... yet.

To make workaround it is possible to use attributes on files or directories:

  • C disable COW feature (for newly created directories or zero sized files). New files in directores with C attributes will be created with C attrib. But not subdirectories.
  • c enable compression

Move data between subvolumes

See `cp` command arg:

When --reflink[=always] is specified, perform a lightweight copy, where the data blocks are copied  only  when  modified.   If  this  is  not  possible  the  copy  fails,  or  if
     --reflink=auto is specified, fall back to a standard copy.
cp -pr --reflink=always srcDirectory dstDirectory/
rm -r srcDirectory

Scrub

Start foreground, readonly scrub, print statistics at end:

btrfs scrub start -B -d -r /
 
scrub device /dev/sda3 (id 1) done
	scrub started at Mon Mar  6 07:02:56 2017 and finished after 00:04:06
	total bytes scrubbed: 18.50GiB with 0 errors
scrub device /dev/sdc5 (id 2) done
	scrub started at Mon Mar  6 07:02:56 2017 and finished after 00:06:52
	total bytes scrubbed: 18.47GiB with 0 errors

Get currently running scrub status:

btrfs scrub status /

periodic scrub

From arch linux btrfs-progs

/etc/systemd/system/btrfs-scrub@.service
[Unit]
Description=Btrfs scrub on %f

[Service]
Nice=19
IOSchedulingClass=idle
ExecStart=/bin/btrfs scrub start -B %f
/etc/systemd/system/btrfs-scrub@.timer
[Unit]
Description=Monthly Btrfs scrub on %f

[Timer]
OnCalendar=monthly
AccuracySec=1d
Persistent=true

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable btrfs-scrub@-.timer
systemctl start btrfs-scrub@-.timer # to enable for /
 
systemctl enable btrfs-scrub@home.timer # to enable for /home
systemctl start btrfs-scrub@home.timer # to enable for /home

migration to BTRFS

apt-get install btrfs-tools

Issues

error inheriting props for ino 3336468 (root 264): -28

 31 #define ENOSPC          28      /* No space left on device */
# btrfs fi show /mount
Label: none  uuid: 5db83d5e-4f38-40b0-ac5d-dadd9cd1d23f
	Total devices 1 FS bytes used 22.18GiB
	devid    1 size 30.00GiB used 27.58GiB path /dev/mapper/vg_spox-lv_home
 
# btrfs fi usage /mount
Overall:
    Device size:		  30.00GiB
    Device allocated:		  27.58GiB
    Device unallocated:		   2.42GiB
    Device missing:		     0.00B
    Used:			  22.52GiB
    Free (estimated):		   5.58GiB	(min: 4.37GiB)
    Data ratio:			      1.00
    Metadata ratio:		      1.99
    Global reserve:		 512.00MiB	(used: 0.00B)
 
Data,single: Size:25.01GiB, Used:21.84GiB
   /dev/mapper/...	  25.01GiB
 
Metadata,single: Size:8.00MiB, Used:0.00B
   /dev/mapper/...	   8.00MiB
 
Metadata,DUP: Size:1.25GiB, Used:348.23MiB
   /dev/mapper/...	   2.50GiB
 
System,single: Size:4.00MiB, Used:0.00B
   /dev/mapper/...	   4.00MiB
 
System,DUP: Size:32.00MiB, Used:16.00KiB
   /dev/mapper/...	  64.00MiB
 
Unallocated:
   /dev/mapper/...	   2.42GiB

https://bbs.archlinux.org/viewtopic.php?id=200817 http://www.mail-archive.com/linux-btrfs@vger.kernel.org/msg60637.html