Resizing loop back fileystems

11.03.2006 at 21:40

I recently needed to resize an existing loop back fileystem which was created with dd after some research i came up with the following which worked fine for me.

First you need to get the current image size preferably in megabytes this can be done with

# du -m loop-back-image.img
300 loop-back-image.img

It says the size is 300mb, so here is how we can add another 100mb to it.

# dd if=/dev/zero of=loop-back-image.img bs=1M seek=300 count=100

seek=300 means that dd will start at this point which is the end, and count=100 tells dd to add 100mb to that point, so we end up with a total size of 400mb. Run resize2fs on the enlarged 400mb image, it will resize the ext2 filesystem on it to fill the image.

# resize2fs loop-back-image.img

If you want to reduce the loop back image size you first have to resize the filesystem on it.

# resize2fs loop-back-image.img 200M

Afterwards you can run 'dd' on the 300mb image to reduce it to 200mb.

# dd if=/dev/zero of=loop-back-image.img bs=1M seek=200 count=0

Again seek=200 says dd to start at 200mb and count=0 means that it should add nothing to that point so we end up with a total image size of 200mb.

Comments (2)

Resize thhe second (of 2) partition on a loopback file

24.05.2017 by -dmeinert (dmeinert[at]rosen-group[dot]com)

to do so, enlarge the file as written above. then: get the <start> offset of the second partition (in blocks of 512 bytes): > /sbin/fdisk ./loopback.img fdisk> p <some output> Device Boot Start End Blocks Id System ./loopback.img1 8192 137215 64512 c W95 FAT32 (LBA) ./loopback.img2 137216 8581119 4221952 83 Linux fdisk> q <start> here is 137216 mount the second partition as loopback file, e.g. : > sudo mount -v -o offset=512*<start> ./loopback.img ./loopback_2 -t ext4 -o loop=/dev/loop1 The device file of this partition is /dev/loop1. Now, resize this partition: >resize2fs /dev/loop1 Now I could add files to this enlarged partition. Did I miss something?

Resize thhe second (of 2) partition on a loopback file

24.05.2017 by -dmeinert (dmeinert[at]rosen-group[dot]com)

to do so, enlarge the file as written above.<br> <br> then: <br> <br> get the <start> offset of the second partition (in blocks of 512 bytes):<br> <br> > /sbin/fdisk ./loopback.img<br> fdisk> p<br> <some output><br> Device Boot Start End <br> Blocks Id System<br> ./loopback.img1 8192 137215 64512 c W95 FAT32 (LBA)<br> ./loopback.img2 137216 8581119 4221952 83 Linux<br> fdisk> q<br> <br> <start> here is 137216<br> <br> mount the second partition as loopback file, e.g. :<br> <br> > sudo mount -v -o offset=512*<start> ./loopback.img ./loopback_2 -t ext4 -o loop=/dev/loop1<br> The device file of this partition is /dev/loop1. Now, resize this partition:<br> <br> >resize2fs /dev/loop1<br> <br> Now I could add files to this enlarged partition.<br> Did I miss something?<br> (except for newlines which were not interpreted, sorry for that)

1