The usual warning: resizing file systems and partitions may cause data loss because of software bugs, faulty hardware, power loss, human errors. Make sure you always backup your data.
A loop device is a block device, logically analogous to a physical disk. As such, partition managers are not meant to directly resize it.
The couple of useful pieces you haven't found yet are losetup -c
, to make the loop driver update the size of a block device when its backing file's size changes, and partprobe
, to make the kernel update its internal representation of the partitions on a device. (Though you likely don't need to explicitly invoke the latter if you use a GUI partition manager such as GParted).
Let's set up a working example:
$ fallocate -l 100M volume
$ sudo parted <<'EOT'
select ./volume
mklabel msdos
mkpart primary ext4 1MiB 50M
mkpart primary ext4 50M 100%FREE
quit
EOT
$ sudo losetup -P /dev/loop0 volume
$ sudo mkfs.ext4 /dev/loop0p1
$ sudo mkfs.ext4 /dev/loop0p2
$ mkdir mp1 mp2
$ sudo mount /dev/loop0p1 mp1
$ sudo mount /dev/loop0p2 mp2
This gives:
$ lsblk /dev/loop0
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 100M 0 loop
├─loop0p1 259:0 0 47M 0 part /path/to/mp1
└─loop0p2 259:1 0 52M 0 part /path/to/mp2
$ df -h /dev/loop0p*
Filesystem Size Used Avail Use% Mounted on
/dev/loop0p1 42M 1.1M 38M 3% /path/to/mp1
/dev/loop0p2 47M 1.1M 42M 3% /path/to/mp2
Then we can grow the file that backs our loop device and let losetup
re-read the file's size to update the capacity of loop0
:
$ fallocate -l 200M volume
$ sudo losetup -c /dev/loop0
Without unmounting the two partitions, we will only be able to grow them and the contained file systems by the amount of free space on their right (only /dev/loop0p2
can be extended in our case). Also, to be able to resize the file systems we need to make the kernel know about the changed partition table:
$ sudo parted <<'EOT'
select ./volume
resizepart 2 100%FREE
quit
EOT
$ sudo partprobe -s /dev/loop0
$ sudo resize2fs /dev/loop0p2
(Unmounting the two file systems will also allow you to shrink or move your partitions around. Note that, when shrinking a partition, the order of these steps is the other way round: first, resize the file system, then resize the partition (and tell the kernel about the changes if needed), then reduce the size of the backing file).
Finally obtaining:
$ lsblk /dev/loop0
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 200M 0 loop
├─loop0p1 259:0 0 47M 0 part /path/to/mp1
└─loop0p2 259:1 0 152M 0 part /path/to/mp2
$ df -h /dev/loop0p*
Filesystem Size Used Avail Use% Mounted on
/dev/loop0p1 42M 1.1M 38M 3% /path/to/mp1
/dev/loop0p2 144M 1.6M 135M 2% /path/to/mp2
losetup
. With gparted/parted you can only shrink the loop partitions. The limitation comes from the static loop image mapping. – sugarman Jun 29 '20 at 09:15