• Resize a BTRFS image file and underlying partition

    From SugarBug@21:1/5 to All on Fri Dec 29 17:13:44 2023
    I am trying to riddle out how to resize a BTRFS partition _and_ the
    underlying image file.

    I created a image file:

    $ truncate -s4G btrfs.img

    I mounted and formatted the image as BTRFS volume:

    $ ld=$(losetup --show --find btrfs.img); echo "$ld"

    $ mkfs -t btrfs "$ld"

    $ mkdir /mnt/btrfs_img

    $ mount "$ld" /mnt/btrfs_img

    I can resize the partition inside the image.

    $ btrfs filesystem resize 2G /mnt/btrfs_img
    $ btrfs filesystem resize 3G /mnt/btrfs_img

    How do I resize the partition and the image file (btrfs.img)? I know I
    can hack it with 'truncate' on the image file to shrink it but that does
    not allow to grow the image file or grow the partition while
    necessarily increasing the image file size. If I want to shrink the
    partition to 2G, or grow it to 6G, how do I get the image file to
    accurately shrink and grow with the partition?

    --
    3883@sugar.bug | sybershock.com | sci.crypt

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andreas Kempe@21:1/5 to All on Sat Dec 30 01:30:19 2023
    Den 2023-12-29 skrev SugarBug <3883@sugar.bug>:
    I am trying to riddle out how to resize a BTRFS partition _and_ the underlying image file.

    I created a image file:

    $ truncate -s4G btrfs.img

    I mounted and formatted the image as BTRFS volume:

    $ ld=$(losetup --show --find btrfs.img); echo "$ld"

    $ mkfs -t btrfs "$ld"

    $ mkdir /mnt/btrfs_img

    $ mount "$ld" /mnt/btrfs_img

    I can resize the partition inside the image.

    $ btrfs filesystem resize 2G /mnt/btrfs_img
    $ btrfs filesystem resize 3G /mnt/btrfs_img

    How do I resize the partition and the image file (btrfs.img)? I know I
    can hack it with 'truncate' on the image file to shrink it but that does
    not allow to grow the image file or grow the partition while
    necessarily increasing the image file size. If I want to shrink the
    partition to 2G, or grow it to 6G, how do I get the image file to
    accurately shrink and grow with the partition?


    You say that truncate won't allow you to grow the image, but I don't
    understand why you think that is the case. truncate can both shrink
    and extend a file.

    Since you have created the BTRFS filesystem directly on the image
    without any partition table, if your commands are to be believed,
    changing the size should be as simple as doing

    truncate -s 6G btrfs.img
    ld=$(losetup --show --find btrfs.img); echo "$ld"
    mount "$ld" /mnt/btrfs_img
    btrfs filesystem resize max /mnt/btrfs_img

    NOTE: Backup your image before doing this if you have data you care
    about!

    The loopback device reports the size of the underlying image to the
    file system and it should be able to expand to the max size available.
    I've done the same with BTRFS on LUKS devices on images without any
    issues.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rich@21:1/5 to SugarBug on Sat Dec 30 05:49:16 2023
    SugarBug <3883@sugar.bug> wrote:
    How do I resize the partition and the image file (btrfs.img)? I know I
    can hack it with 'truncate' on the image file to shrink it but that does
    not allow to grow the image file or grow the partition while
    necessarily increasing the image file size. If I want to shrink the
    partition to 2G, or grow it to 6G, how do I get the image file to
    accurately shrink and grow with the partition?

    Shrinking/growing the image file is fully separate and independent from
    telling btrfs to shrink/grow the filesystem that is stored inside the
    image file.

    According to the truncate man page, truncate will both shrink and grow
    files:

    Shrink or *extend* the size of each FILE to the specified size

    So, if you want to "grow" things, the first step is to make the image
    file larger (because the space within the file has to exist first in
    order for btrfs to grow the filesystem).

    So you'd "truncate" it to the new larger size you want (it is
    recommended that you do this only when it is not mounted and not
    attached to a loop back device).

    Then you would tell btrfs to grow the partition inside the image file.

    To shrink things, first you want to tell btrfs to shrink the filesystem
    that is inside the image file (presumably this will also cause it to
    move any data that is currently stored beyond the new smaller size to
    be within the new smaller size -- but double check this, I swore off
    btrfs years ago due to its unreliability and have not touched it
    since).

    Then, once btrfs has been told a new, smaller size, and has "compacted
    things" (hopefully it "compacts") then you unmount, remove from the
    loop back device, and finally truncate the file to a smaller size
    (note, don't truncate smaller than the size you told btrfs to shrink
    into, otherwise you may lose data).

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)