[TriLUG] using parted to change the size of a loop mounted filesystem

Robert Dale robdale at gmail.com
Wed Mar 14 23:03:13 EDT 2012

As parted implies, it works with partitions.  You could resize a
partition in a file, as if the file were a disk of partitions. I'm not
aware of it resizing filesystems directly in a file.

However, you don't need parted to do it!  You can resize a file with
dd and grow/shrink the fs accordingly.

Let's try it...

First create the file that will hold the filesystem...
# dd if=/dev/zero of=foo.part bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 0.742415 s, 141 MB/s

# mkfs.ext3 foo.part
mke2fs 1.41.14 (22-Dec-2010)
foo.part is not a block special device.
Proceed anyway? (y,n) y
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
25688 inodes, 102400 blocks
5120 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
13 block groups
8192 blocks per group, 8192 fragments per group
1976 inodes per group
Superblock backups stored on blocks:
	8193, 24577, 40961, 57345, 73729

Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 38 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

# mount -o loop foo.part /mnt
# df -h
Filesystem                        Size  Used Avail Use% Mounted on
/dev/loop0                         96M  4.1M   87M   5% /mnt

Note our 96M filesystem.  Now let's grow it!

# umount /mnt
# dd if=/dev/zero of=foo.part bs=1M count=100 seek=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 0.152461 s, 688 MB/s

# resize2fs foo.part 180M
resize2fs 1.41.14 (22-Dec-2010)
Please run 'e2fsck -f foo.part' first.

# e2fsck -f foo.part
e2fsck 1.41.14 (22-Dec-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
foo.part: 11/25688 files (0.0% non-contiguous), 8913/102400 blocks

# resize2fs foo.part 180M
resize2fs 1.41.14 (22-Dec-2010)
Resizing the filesystem on foo.part to 184320 (1k) blocks.
The filesystem on foo.part is now 184320 blocks long.

# mount -o loop foo.part /mnt
# df -h
Filesystem                        Size  Used Avail Use% Mounted on
/dev/loop0                        173M  4.2M  160M   3% /mnt

Now the filesystem is 173M. It worked!

To shrink, resize2fs first, then use dd or your favorite tool to
truncate the file.


On Wed, Mar 14, 2012 at 10:28 PM, Joseph Mack NA3T <jmack at wm7d.net> wrote:
> parted can change the size of an unmounted partition carrying a filesystem.
> A file carrying a filesystem within it and which can be loop mounted, could
> have its size changed too. I expect doing so would be quite a different
> kettle of fish.
> google doesn't show anyone doing this at least with parted. Has it just not
> been implemented?
> (I could make a new file and copy everything from the old file to the file
> etc, but that seems so old fashioned. I'd hoped there was a better way)
> Thanks
> Joe
> --
> Joseph Mack NA3T EME(B,D), FM05lw North Carolina
> jmack (at) wm7d (dot) net - azimuthal equidistant map
> generator at http://www.wm7d.net/azproj.shtml
> Homepage http://www.austintek.com/ It's GNU/Linux!
> --
> This message was sent to: Robert Dale <robdale at gmail.com>
> To unsubscribe, send a blank message to trilug-leave at trilug.org from that
> address.
> TriLUG mailing list : http://www.trilug.org/mailman/listinfo/trilug
> Unsubscribe or edit options on the web  :
> http://www.trilug.org/mailman/options/trilug/robdale%40gmail.com
> TriLUG FAQ          : http://www.trilug.org/wiki/Frequently_Asked_Questions

Robert Dale

More information about the TriLUG mailing list