Announcement

Collapse
No announcement yet.

Moving, resizing, and replacing BTRFS file systems and their partitions

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Moving, resizing, and replacing BTRFS file systems and their partitions

    I was checking my 4 NMVE drives for wear and my boot NVME drive showed "Percentage Used: 49%" while the other NVME drives are 0-3%. This number is the manufacturers best guess about the life span of the drive - so half dead.
    I decided to move my booting BTRFS file system to a different drive and use the more worn one for backups. I detailed this here thinking someone else might find it useful.

    The root file system takes the whole drive - 932G and is 50% full, The target drive also was 932G and had about 550G of backups on it. I wanted to reverse the setup: make the current backup drive the boot drive and vice versa. After reviewing all my file systems, I decided I would move the backup file system somewhere, move the root file system onto that drive, then move the backup file system to the worn drive, but I didn't have enough free space available and contiguous to do it.

    I ended up following these steps:

    Step 1) I had 2 EXT4 file systems - VM drives and their backups - that were less than half full so I moved the files off of one onto the other.
    This left 290G free on that drive. There was also a sacrificial partition of 410G on the same drive, if combined: enough room in total to hold the backup file system, but still not contiguous.
    Between these two available partitions was my Steam partition (BTRFS) of 250G and about 85% full.

    Step 2) I used the "btrfs add" command to add the Steam file system to the 290G partition. This happens instantly. I then used "btrfs remove" to remove the 250G partition from the combined file system. This took about 50 minutes to complete.
    I now had 590G free (290+250) and contiguous which is enough to hold the backup file system. A bonus was the getting-close-to-full Steam partition now has 40G more space.

    Next, to move the backup file system to this drive so I can put the root file system in it's place.

    Step 3) I again did "btrfs add" followed by "btrfs remove". This time it took about 2 hours to complete.
    Note that since the target partition is smaller than the source, the file system will be reduced in size to fit in the smaller space. If there isn't enough room on the target partition for all the data, the command will abort immediately.​

    Next, to move the root file system to the now empty backup drive.

    Step 4) This time, rather than add+remove which is slow, I used the "btrfs replace" command, which is much faster. This took less than 20 minutes.
    Note that the "replace" command requires one extra step to run it. You need to know the device ID number which appears as "devid" when you use "btrfs filesystem show".
    I now was more than half-way to completing my task. However, up to this point I had not rebooted and was still using the system for other tasks while btrfs did it's thing.


    UH-OH!) Then I snafu'd by deciding to run "partprobe" since I had been moving partitions around. This froze the system immediately requiring a hard reset to reboot.
    To make it more fun, on reboot I ended up in the GRUB console and had to manually boot from it. No biggie as I had done that before (too many times). Once I was booted, I simply ran "update-grub" and it rebooted successfully. I suspect had I updated grub without running partprobe, I would have been able to proceed without a reboot.


    Step 5) Last task to complete my goal was to move the root backup file system from the Steam drive to the old root drive. Here, I used "btrfs replace" again.
    However this time the file system was only 590G in size and was "replaced" to a 953G partition. In this scenario you have to "grow" the file system before you can use the additional free space.​

    Step 6) The command is simple and immediate: "btrfs fi resize max <mount point>"

    My goal was complete. All that was left was to re-create the deleted EXT4 partition for the VM backups and move the files back.


    Key Take-aways and more info:

    These operations are potentially harmful if you were to lose power. Backups are essential, and if possible, also a battery backup system for a desktop PC.

    Notice that at no time did I document editing fstab or updating grub after moving btrfs file systems around. That's because I didn't need to. The add, remove, and replace functions retain and move file system labels and UUIDs. Unless you are mounting using something other than one of these two entries to mount or boot, there's no need to edit anything.

    Except for my snafu, all of this happened while continuing to use the system normally. In fact, all the file systems must be mounted to use these commands. The whole operation (minus the grub repair) took about 4 hours.

    In Step 2 I could have added the two partitions together without using "add|remove" to relocate the Steam partition. However, then Step 4 would have required two sets of "add|remove" commands instead of the single and much faster "replace" command.

    The "btrfs replace" command cannot be cancelled or interrupted. To use "replace" the file system must be equal to or smaller that the target partition. Space permitting, you can shrink the file system in place and then use the command.

    The "remove" command can be cancelled and resumed later. The "add" command is quickly reversible with "remove" unless you added files to the combined file system or have run a "balance" command.


    Commands used and examples(all require sudo rights):

    btrfs device add <device name> <mountpoint>
    btrfs de ad /dev/nvme3n1p3 /mnt/steam

    btrfs device remove <device name> <mountpoint>
    btrfs dev remove /dev/nvme3n1p4 /mnt/steam
    btrfs filesystem show (to retrieve the "devid" to use the replace command)
    btrfs fi sh

    btrfs replace start <devid> <device-to-remove> <mount point>
    btrfs rep start 2 /dev/nvme1n1 /mnt/root_backup

    btrfs replace status (to see how the command is proceeding)
    btrfs rep status

    btrfs filesystem resize <mount point>
    btrfs fi re max /mnt/root_backup/

    Please Read Me

Users Viewing This Topic

Collapse

There are 0 users viewing this topic.

Working...
X