Setting up btrfs with two disks into raid1

This setup is done on Ubuntu 16.04, uname -r 4.4.0-18-generic.

I have two disks /dev/sdb and /dev/sdc, disk are non-system, OS will be not installed on these disks.

Formatting and creating raid1 configuration

Install btrfs file-system support:

sudo apt-get install btrfs-tools

Format a disk with btrfs file system, two obvious options are command line or using GUI tool gnome-disk-utily.

There are two different approaches to this setup. 

1. If you have two empty disks, you easily just format two disks into raid1 with:

$ sudo mkfs.btrfs -m raid1 -d raid1 /dev/sdb /dev/sdc

and it is done, ready to use, just mount it, only one device is needed to mount either /dev/sdb or /dev/sdc.

$ sudo mount /dev/sdb /mount_point

2. If you have limited number of disks, one disk full of data, other disk is empty, format the empty one with btrfs and copy the data from the other disk to the new btrfs formated disk. Then format the second disk with btrfs and add it to this existing system. Then balance disks so data and metadata is distributed evenly between the disks, after that balance does not have to be run manually. 


From command line if your device name is /dev/sdb

$ sudo mkfs.btrfs /dev/sdb

of from gnome-disk-utility:

Format BTRFS in gnome-disk-utilility

Check if system recognizes your disk, for each of them you should see something like this:

$ sudo btrfs fi show

Label: 'disk1'  uuid: 1b962b21-3130-498b-9543-e84c90f12fce
Total devices 2 FS bytes used 3.86TiB
devid    1 size 7.28TiB used 3.87TiB path /dev/sdb

Adding device (once both disks are formatted btrfs), first mount somewhere your first disk, then add second disk to the mountpoint, balance disk into btrfs raid1 configuration, check btrfs-balance(8) Manual Page for explanation.

{your-username} is for you to change, or use /mnt ...

$ sudo mount /dev/sdb /media/{your-username}/disk1
$ sudo btrfs device add /dev/sdc /media/{your-username}/disk1 -f
$ sudo btrfs balance start -dconvert=raid1 -mconvert=raid1 /media/{your-username}/disk1

This takes some time, on 8TB disks half full, it took about 9 hours with approx. 1 million files, no need to worry about this procedure, if power failure or accidental restart, it will continue where it left of, user may manually stop balance process, but it is works automatically in background. 

To see status of balance process, run commands:

Shows % of balancing procedure:

$ sudo btrfs balance status -v

Shows standard file system status, now two devices show up under same label and the second one has only 2.56 TiB of 3.86 TiB done copying. 

$ sudo btrfs fi show

Label: 'disk1'  uuid: 1b962b21-3130-498b-9543-e84c90f12fce
Total devices 2 FS bytes used 3.86TiB
devid    1 size 7.28TiB used 3.87TiB path /dev/sdb
devid    2 size 7.28TiB used 2.56TiB path /dev/sdc

Mounting disk 

From $ sudo btrfs fi show copy the uuid, ex.: 1b962b21-3130-498b-9543-e84c90f12fce, similar as sudo blkid.

To mount disk automatically after system start, edit /etc/fstab and add this line:

$ sudo nano /etc/fstab

    UUID=1b962b21-3130-498b-9543-e84c90f12fce /media/{your-username}/disk1 defaults 0 0

Don't try to mount both disks, your system might not boot, or boot to errors, only one the two disks should be mounted.

To edit fstab with GUI, use gnome-disk-utility, choose Edit Mount Options on selected disk and change appropriately:

Turning off COW 

For heavy copy & paste, write, overwrite operations, COW is sometimes good to be turned off, otherwise performance will suffer. Example is MySQL database storage, where system is changing constantly the same files and overwriting them, COW will always try to create new file, it is slow. It is possible to turn of COW for just one directory, or one directory recursively for example. Use chattr to set and lsattr to check for status.

$ sudo chattr -R +C /media/{your-username}/disk1/mysql-storage

The above command will turn off COW for  /media/{your-username}/disk1/mysql-storage and all it's sub-directories, for more man chattr.