Linux: btrfs command line examples

Hi,

my collection of btrfs command line snippets.



Create and mount a filesystem

root@debdev:~# mkfs.btrfs /dev/sdb1
root@debdev:~# mount /dev/sdb1 /mnt/sdb1

Create and delete subvolume

root@debdev:~# btrfs subvolume create  /mnt/sdb1/subvol
root@debdev:~# btrfs subvolume delete  /mnt/sdb1/subvol

Show information of a btrfs volume

root@debdev:~# btrfs filesystem show /mnt/sdb1/
Label: none  uuid: c818d114-5541-4cd3-9614-a4a96cb82427
        Total devices 1 FS bytes used 208.00KiB
        devid    1 size 8.00GiB used 843.00MiB path /dev/sdb1

Disk free

root@debdev:~# btrfs filesystem df /mnt/sdb1

Disk usage

root@debdev:~# btrfs filesystem du /mnt/sdb1

Start scrubbing(check data integrity on your volume)

root@debdev:~# btrfs scrub start /mnt/sdb1

Mount a subvolume

root@debdev:~# btrfs subvolume list /mnt/sdb1
ID 257 gen 12 top level 5 path subvol
root@debdev:~# mount -o subvolid=257 /dev/sdb1 /mnt/subvol

Create a snapshot, a snapshot is a subvolume

root@debdev:~# mkdir /mnt/sdb1/.snaps
root@debdev:~# btrfs subvolume snapshot /mnt/sdb1 /mnt/sdb1/.snaps/snapshot-`date +\%Y\%m\%d-\%H\%M%S`
Create a snapshot of '/mnt/sdb1' in '/mnt/sdb1/.snaps/snapshot-20160718-215933'
root@debdev:~# btrfs subvolume delete /mnt/sdb1/.snaps/snapshot-20160718-215933

Make a snapshot/subvolume as default mount point

root@debdev:~# btrfs subvolume set-default 257 /mnt/sdb1

And set back to default

root@debdev:~# btrfs subvolume set-default 0 /mnt/sdb1

Set a Quota on a subvolume

root@debdev:~# btrfs qgroup limit 30M /mnt/sdb1

Add more space by adding a new harddisk to the volume. Note: If one device fails the whole volume is lost.

root@debdev:~# btrfs device add /dev/sdc1 /mnt/sdb1

Show filesystem information

root@debdev:~# btrfs filesystem show  /mnt/sdb1/
Label: none  uuid: c818d114-5541-4cd3-9614-a4a96cb82427
        Total devices 2 FS bytes used 272.00KiB
        devid    1 size 8.00GiB used 891.00MiB path /dev/sdb1
        devid    2 size 8.00GiB used 0.00B path /dev/sdc1

Remove a disk/device from a volume. All datas are moved to the remaining devices

root@debdev:~# btrfs device delete /dev/sdc1 /mnt/sdb1

Convert your volume to RAID1 (two devices necessary), Data (-d) and Metadata (-m)

root@debdev:~# btrfs balance start -mconvert=raid1 -dconvert=raid1 /mnt/sdb1

Check RAID level and errors

root@debdev:~#  btrfs device usage /mnt/sdb1
/dev/sdb1, ID: 1
   Device size:             8.00GiB
   Data,single:             1.00GiB
   Data,RAID1:              2.00GiB
   Metadata,RAID1:        256.00MiB
   System,RAID1:           32.00MiB
   Unallocated:             4.72GiB

/dev/sdc1, ID: 2
   Device size:             8.00GiB
   Data,RAID1:              2.00GiB
   Metadata,RAID1:        256.00MiB
   System,RAID1:           32.00MiB
   Unallocated:             5.72GiB

RAID1 for new disks

root@debdev:~#  mkfs.btrfs -d raid1 -m raid1 /dev/sdb1 /dev/sdc1

Replace a Disk if it is missing or failed

root@debdev:~# mount -t btrfs -o degraded /dev/sdc1 /mnt/sdb1

Find partition with btrfs signature

root@debdev:~# blkid

Recover if device 1 (dev/sdb1) is missing. After a reboot /dev/sdc1 is now /dev/sdb1

root@debdev:~# btrfs filesystem show /dev/sdb1
warning, device 1 is missing
root@debdev:~# mount -t btrfs -o ro,degraded,usebackuproot /dev/sdb1 /mnt/sdb1

Get the missing device ID

root@debdev:~# btrfs device stats /mnt/sdb1
[devid:1].write_io_errs   0
[devid:1].read_io_errs    0
[devid:1].flush_io_errs   0
[devid:1].corruption_errs 0
[devid:1].generation_errs 0
[/dev/sdb1].write_io_errs   0
[/dev/sdb1].read_io_errs    0
[/dev/sdb1].flush_io_errs   0
[/dev/sdb1].corruption_errs 0
[/dev/sdb1].generation_errs 0

And recover to a new disk/partition, currently /dev/sdc1

root@debdev:~# btrfs replace start 1 /dev/sdc1 /mnt/sdb1
root@debdev:~# btrfs balance /mnt/sdb1


Convert your volume to RAID0

root@debdev:~# btrfs balance start -dconvert=raid0 -mconvert=raid0 -f /mnt/sdb1

Or to single volume

root@debdev:~# btrfs balance start -mconvert=single -dconvert=single /mnt/sdb1

Reorganize all datas

root@debdev:~# btrfs balance

to be continued

Michael

Leave a Reply