Btrfs/Replacing a disk

From Forza's ramblings


Replacing a disk in a btrfs filesystem[edit | edit source]

The read/write head crashed on the platter in a HDD

btrfs replace replaces an existing disk by doing a byte-for-byte copy of all data from the old to the new disk. This method is very easy and very fast compared to btrfs send|receive.

Prepare the new disk[edit | edit source]

Before you start you should prepare the new disk. Even though Btrfs supports raw disks, it is recommended that you do have a partition table on your disk to avoid confusion with other filesystems and tools. You can use fdisk or cfdisk from the util-linux package or GNU parted to create a partition table and a partion. I recommend you create a GUID Partition Table (GPT) instead of the old DOS MBR style partition tables.

Replace a disk in a single disk filesystem[edit | edit source]

This guide will help you replace the disk in a single disk filesystem with a new disk.

We will replace existing disk /dev/sdc with a new disk /dev/sdd.

btrfs filesystem show shows all btrfs filesystems and disks connected to them.

# btrfs filesystem show
Label: 'my-vault'  uuid: df68a30d-d26e-4b9c-9606-a130e66ce63d
        Total devices 1 FS bytes used 658.88GiB
        devid    1 size 931.51GiB used 667.02GiB path /dev/sdc1

Replacing with equal sized or a larger disk[edit | edit source]

The most common case is when you want to replace a disk with a new, larger disk.

First you need to check what devid the old disk has:

# btrfs filesystem usage
Label: 'my-vault'  uuid: df68a30d-d26e-4b9c-9606-a130e66ce63d
        Total devices 1 FS bytes used 658.88GiB
        devid    1 size 931.51GiB used 667.02GiB path /dev/sdc1 

Now you can start the replacing process.

btrfs replace start <id> <new-disk> <mount-point>
btrfs replace start  1   /dev/sdd   /mnt/my-vault/

This will move all data from the old disk (/dev/sdc) to the new disk (/dev/sdd). When it is complete you can physically remove the old disk from your system.

Since the new disk is larger than the old, you need to resize the filesystem to take advantage of the new size:

btrfs filesystem resize 1:max /mnt/my-vault

Replacing with a smaller disk[edit | edit source]

Btrfs replace can only replace a disk of equal size or bigger. So if your new disk is smaller you need to shrink the filesystem before you can attempt a replacement.

You need to check what devid and size the old disk has:

# btrfs filesystem usage
Label: 'my-vault'  uuid: df68a30d-d26e-4b9c-9606-a130e66ce63d
        Total devices 1 FS bytes used 658.88GiB
        devid    1 size 931.51GiB used 667.02GiB path /dev/sdc1

For example, if the new disk is only 800GiB so we need to resize /dev/sdc1 to less than that

btrfs filesystem resize <id>:<size> <mount-point>
btrfs filesystem resize  1:799GiB   /mnt/my-vault

Now you can start the replacing process.

btrfs replace start <id> <new-disk> <mount-point>
btrfs replace start  1   /dev/sdd   /mnt/my-vault/

This will move all data from the old (/dev/sdc) to the new disk (/dev/sdd). When it is complete you can physically remove the old disk from your system.

Finish by making sure the filesystem uses all space on the new disk.

btrfs filesystem resize 1:max /mnt/my-vault

Status monitoring[edit | edit source]

On a large disk, a replacement can take several hours. It is possible to monitor the status using btrfs replace status <mount-point>.

# btrfs replace status /mnt/my-vault
Started on 24.Jul 11:02:41, finished on 24.Jul 11:41:51, 0 write errs, 0 uncorr. read errs

Reference[edit | edit source]

The btrfs-replace reference manual can be found at https://btrfs.wiki.kernel.org/index.php/Manpage/btrfs-replace