42

I have my /home on a BTRFS partition and I need to shrink it.

Unfortunately, Gparted does not have the resize option for this FS.

Can I resize it in any other way without losing my data?

Dan
  • 14,290
Borsook
  • 2,093

4 Answers4

32

According to this page in the btrfs wiki:

To shrink the filesystem by 4 GiB: btrfs filesystem resize -4g /mnt or btrfsctl -r -4g /mnt Set the FS size . To set the filesystem to a specific size, omit the leading + or - from the size.

The btrfs and btrfsctl commands in Ubuntu is provided by the package called btrfs-utils, which you can install in the Software Center. You run these commands in the Terminal (Ctrl+Alt+T). Replace 4g by however much you really want to shrink the partition, and /mnt by the mount point of the partition. For example, to shrink a btrfs partition mounted at /media/Alpha by 500 MiB, you would run:

btrfs filesystem resize -500m /media/Alpha

Or:

btrfsctl -r -500m /media/Alpha

Assuming the guidelines for resizing btrfs filesystems are the same as for other filesystems, you should avoid resizing a partition that is on the same physical disk as the running system. So, if necessary, boot from a live system to resize a partition on the drive that has your installed Ubuntu system. (This applies to resizing done in GParted, too.)

If some utilities or applications don't see the results of the resizing operation when it is performed, rebooting is a good idea.

See the btrfs and btrfsctl manual pages for more details about the use of these commands. And hopefully the btrfs wiki will be back up soon.

I am not personally very familiar with btrfs filesystems, but it seems strange to me that these resize commands take the mount point of the filesystem rather than the device name. If anyone can explain that, please comment or feel free to edit this answer to improve it.

Eliah Kagan
  • 119,820
  • 1
    It does seem strange, so I need to have to volume mounted to resize it? I always thought that it's much safer to do so with unmounted volumes... Anyway thanks for your answer! – Borsook Nov 03 '11 at 16:38
  • 19
    The commands listed resize only the filesystem, not the partition. That's why they work on a mounted system. Once you've resized the FS you should be able to use your favourite partition management tool to change the size of the actual partition. But, the latest gparted will just do it all for you, so there's little reason to bother. – Perkins May 14 '12 at 22:23
  • Is there a way to resize from the front so a separate lengthy move operation wouldn't be neccessary? – Paul Jun 01 '20 at 18:52
  • 1
    @Perkins to shrink the partition by 500M as well afterwards use (substitute the device and size) parted -a opt /dev/sdX "resizepart 3 -500m". The 3 is the partition number, i.e. /dev/sdX3. – Hi-Angel Aug 03 '22 at 12:10
  • @Hi-Angel If that's your favorite partition management tool, go for it! :D – Perkins Aug 30 '22 at 01:13
  • The reason for btrfs doing it on a mount point rather than device is that btrfs is a multi-volume filesystem and the chunks that need to be relocated should be relocated to the most appropriate volumes, not just moved within the volume to be shrunk and information on other volumes may need updating, e.g. for chunks that are part of a RAID set. – Joachim Wagner Sep 11 '23 at 13:52
  • Also, btrfs filesystem resize works on mounted filesystems – unlike resize tools for other filesystems, which require the filesystem to be unmounted. With btrfs, the filesystem has to be mounted, for the reasons given above. – user149408 Jan 03 '24 at 22:23
  • The tricky think with btrfs is that the same filesystem can span MANY devices, and you are expect to perform most operations with the filesystem mounted. So it really would not make sense to have the command reference the device name.

    And while you answer is correct for a filesystem that is on a single partition. More generally you are going to want to specify the devid's to reduce. (You find those with the btrfs filesystem show command.

    I believe the parted tool depreciated the ability to reduce size of the filesystem automatically.

    – user6856 Jun 02 '24 at 00:55
  • @Perkins I did use gparted to shrink a btrfs filesystem but it did not shrink the partition as well, now there's a warning saying that there is unallocated space in the partition but there is no way to shrink the partition properly. – xdevs23 Dec 14 '24 at 20:04
  • 1
    @xdevs23 Did the gparted commands complete successfully? That seems like the kind of thing that might happen if something went wrong halfway through. Also I'm not entirely certain how well it handles storage pools with multiple partitions, so if that's what you're doing then you may want to do some more reading. – Perkins Jan 06 '25 at 20:34
  • @Perkins Thanks for your suggestion. It turns out it was, in fact, not a btrfs partition but rather a btrfs filesystem on disk, without a partition table. While this works perfectly fine, it kind of confuses gparted as gparted does successfully shrink the btrfs filesystem, but the underlying disk is still the full size so an exclamation mark appears saying there's unallocated space and if I let gparted "fix" it, it would just resize the fs back to the disk size. In that case a partition table needs to be created. – xdevs23 Jan 07 '25 at 00:26
23

According to the GParted features page, btrfs shrink is supported. If the option isn't available, installing the btrfs-tools package may fix it. If that doesn't work, it could be because Ubuntu includes an older version of GParted, in which case you could use the GParted Live CD:

http://gparted.sourceforge.net/livecd.php

(which is also how Ubuntu recommends using it anyway)

bmaupin
  • 5,390
9

This isn't a direct answer to the question, it's for people who land here from a google search, wondering if you can do an online shrink of / - yes, you can

I needed more swap, and btrfs doesn't allow you to run swap from a file, so I had to shrink / and recreate partitions (carefully). Here is what I did, bearing in mind I wanted an extra 64G of swap:

sudo btrfs filesystem resize -96g /
sudo fdisk /dev/nvme0n1

I had 2 partitions, 1 for / and the other for swap. Deleted both, then recreated (before a reboot!). Old size of partition 1 was 867.5G and new size was 803.5, giving an extra 64G for the swap partition. When it asks if you want to overwrite the btrfs signature, say NO! If you're doing swap as I was, you'll need to change "type" of the swap partition to "82", either when creating, or afterwards using the "t" command

After reboot, I ran

sudo btrfs filesystem resize max /

df -h then reports:

/dev/nvme0n1p1  804G  498G  305G  63% /

So, the reason I shrank the filesystem by 96G initially, was because different tools calculate G and M slightly differently, so it's to make super sure the filesystem doesn't get clobbered by the new partition being slightly smaller than the filesystem on it!

After reboot, if you've changed your swap partition, you will also need to run mkswap on it e.g. sudo mkswap /dev/nvme0n1p2 and either "swapon" or reboot again

This should possibly go at the top of the answer, but needless to say, you run a set of the above commands at your own risk, and you should make sure any important files are backed up in case something goes wrong and you lose the whole filesystem! I hope this helps someone!

sotirov
  • 4,455
jpl888
  • 111
  • Good answer, I might suggest adding some info on not changing the start sector of the btrfs partition when deleting/recreating it using fdisk. Not necessary in your case as your btrfs partition was 1 but may not be so for others. To prevent these issues, I recommend using LVM in the future. – Pavin Joseph Aug 20 '23 at 12:03
  • 2
    2025 update: it is now possible to have swap on BTRFS: A. as long as you follow a few directive to make the swapfile behave like in non-BTRFS partitions (i.e.: the mapping of sectors is fixed and doesn't spread multiple devices) B. as long as you understand that some BTRFS operation could not be accomplished while the swapfile is mounted (anything that could need to move the swapfile's extents around). – DrYak Jan 16 '25 at 12:58
1

Use this command to install btrfs support in GParted:

sudo apt install btrfs-progs

After that you can easily shrink and resize your partition in GParted. This way you can do all the work in GUI. No command line needed