this post was submitted on 20 Jul 2024
6 points (100.0% liked)

btrfs

112 readers
1 users here now

founded 1 year ago
MODERATORS
 

Hi! I'm learning how to use btrfs and I need some advice.

One one of my desktop, I made the mistake of creating 2 partitions, one for /(root) and one for home. Both are btrfs. I didn't know that I could use subvolumes so that they could share the same physical space.

My question: How can I merge the root and home btrfs partitions into only 1 partition that would use btrfs subvolumes?

I'm looking for something like that:

  • Partition1 (btrfs)
    • subvolume 1: @root (mounted to /)
    • subvolume 2: @home (mounted to /home)
  • Partition 2, 3, 4...

My current setup:

  • 1 physical hard drive (1 TB), shown as sda below
  • The partitions I want to merge are sda7 and sda8
  • That computer is an iMac also running MacOS so it has a few other partitions that I should not touch
$ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda      8:0    0 931,5G  0 disk 
├─sda1   8:1    0   200M  0 part /boot/efi
├─sda2   8:2    0 371,1G  0 part 
├─sda3   8:3    0 619,9M  0 part 
├─sda4   8:4    0   600M  0 part 
├─sda5   8:5    0  1023M  0 part 
├─sda7   8:7    0 422,9G  0 part /home
└─sda8   8:8    0 135,1G  0 part /
sdb      8:16   1     0B  0 disk 
sr0     11:0    1  1024M  0 rom  
$ blkid
/dev/sda4: UUID="d970eea2-142b-3f1c-9650-5e496d1e9b4b" BLOCK_SIZE="4096" LABEL="Linux HFS+ ESP" TYPE="hfsplus" PARTLABEL="Linux HFS+ ESP" PARTUUID="eab00592-b96d-4ecb-b2e9-816c95eaf860"
/dev/sda2: UUID="6a26963c-eabb-3e42-9e8d-8677a8282b61" BLOCK_SIZE="4096" LABEL="DD Macintosh" TYPE="hfsplus" PARTLABEL="DD Mac" PARTUUID="8257316c-1fd7-4885-bf2b-7e99557acd85"
/dev/sda7: UUID="22f5e59e-8509-484a-92d5-e7dc03bb70cd" UUID_SUB="78a998f5-db55-4a31-9506-afe548ec8d5e" BLOCK_SIZE="4096" TYPE="btrfs" PARTLABEL="Mint Home" PARTUUID="20b6c31d-5e2d-416f-beb3-faa295af67df"
/dev/sda5: UUID="587b0093-4b64-468a-9a01-b933630d184b" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="a47a3fbf-534a-435d-8916-0f83edebf296"
/dev/sda3: UUID="d0c171e8-572d-39f9-8bdc-38f33744a19a" BLOCK_SIZE="4096" LABEL="Recovery HD" TYPE="hfsplus" PARTLABEL="Recovery HD" PARTUUID="c9d0673b-bb2e-4322-9bf9-c661f7de6856"
/dev/sda1: LABEL_FATBOOT="EFI" LABEL="EFI" UUID="67E3-17ED" BLOCK_SIZE="512" TYPE="vfat" PARTLABEL="EFI System Partition" PARTUUID="d30954cb-b9b6-40fa-9202-a18cf146f7df"
/dev/sda8: UUID="7027382a-4369-4276-b916-9997c1007e5b" UUID_SUB="3e516464-dee1-4bda-9621-29591d54dc2d" BLOCK_SIZE="4096" TYPE="btrfs" PARTLABEL="Mint root" PARTUUID="f2fdb8cc-54fd-46c8-bf1f-85954c1dc363"
top 5 comments
sorted by: hot top controversial new old
[–] [email protected] 2 points 3 months ago (1 children)

You cannot merge them in any automated fashion. You have to manually copy one into the other.

btrfs provides some tools here though; you could use btrfs send | btrfs receive to transfer the subvolumes for instance.

You'd transfer all of your subvolumes in the root partition over to the "home" partition and then adjust your fstab accordingly. After a rebuild of the initrd, your system should boot into the root subvol of the formerly "home" partition. Once that works, you can delete the old root partition and extend the new combined partition.

[–] [email protected] 1 points 3 months ago* (last edited 3 months ago) (1 children)

Can I do this while the system is running? I guess it would be safer to boot to a liveUSB before attempting this?

Also, what do you mean by "After a rebuild of the initrd" ? That's something I have to do after editing fstab? How do I do that?

[–] [email protected] 1 points 3 months ago

You can generally do almost anything to a btrfs while the system is running with the filesystem mounted. In fact, most operations require the btrfs to be mounted.

You should keep a live USB handy though in case you mess up. Oh and also verify that you have a sufficient amount of backups of your important data. 1 copy can turn into 0 copies quite quickly. Rule of thumb places the minimum at 3 copies.

what do you mean by "After a rebuild of the initrd" ? That's something I have to do after editing fstab? How do I do that?

Yes, for modifications to the root mount, you must rebuild the initrd for those changes to actually be applied where it matters.

How this is done fully depends on the distribution and is one of the more significant technical details that differ between just about every distro family. Consult your distro's documentation on that.

[–] seaQueue 2 points 1 month ago

Assuming you have no subvolumes on either partition I would boot a live USB system and mount them then take snapshots of each. Send/receive the home snapshot to the root filesystem, remove the home filesystem and then extend the root partition to fill all space.

Once that's done mount the root snapshot (which is a subvolume in its own right, all snapshots are) and fix up your fstab to point at the new filesystem and subvolume names. Fix your boot cmdline as well using rootflags=subvol=SUBVNAME and if needed update GRUB.

Once you can successfully boot the system using the new subvolumes then (and only then) mount the root of the remaining btrfs partition and remove all of the old directories and files outside of your subvolumes.

A little tedious sure but easy enough with a bit of time.

[–] [email protected] 1 points 3 months ago

What I ended up doing:

  • Moving the root partition from one partition to the other, using btrfs send | btrfs receive
  • Updated fstab to use the correct partitions
  • The computer kept using the old root partition then booting, I had to use ubuntu's boot repair (https://help.ubuntu.com/community/Boot-Repair) to fix that
  • I probably should have moved the home partition instead of the root one. I would have avoided much trouble.