Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

These following ext3 partitions contain identical data. As we can see, the larger the partition size, the more space is required for the same files:

Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/loop11            3965777    561064   3199964  15% [...]
/dev/loop19             573029    543843     29186  95% [...]

Filesystem            Size  Used Avail Use% Mounted on
/dev/loop11           3.8G  548M  3.1G  15% [...]
/dev/loop19           560M  532M   29M  95% [...]

Filesystem            Inodes   IUsed   IFree IUse% Mounted on
/dev/loop11          1024000    1656 1022344    1% [...]
/dev/loop19          1024000    1656 1022344    1% [...]

I start with a partition of fixed size that possibly wasted a lot of space and I want to create a partition that is able to hold that data but with (almost) minimal size. How can I reliably calculate that minimal partition size needed for storing a certain amount of data? The amount of data changes over time, and I need to automate these calculations.

share|improve this question
Since the data is usually more than the half gb here I'd like to prevent having to use a ridiculously time consuming iterative approach. – Daniel Beck Aug 5 '11 at 18:15
I'd appreciate if the person down voting would leave a comment explaining it. – Daniel Beck Sep 4 '11 at 10:12
up vote 4 down vote accepted

Here's a procedure you could try:

  • Create a disk image a bit larger than what you need.
  • Create the filesystem directly in the image. You may want to set filesystem parameters, as suggests.
  • Loop-mount the image and copy your files. Unmount.
  • Shrink the image to its minimum size using resize2fs -M. If you prefer, use Gparted as a graphical front-end.

Basically, resize2fs can do the calculation you want dynamically, considering the actual space used by the files.

If you just want to calculate the minimum size, rather than actually resizing, use resize2fs -P instead.

Since your data is only a few hundred megabytes, you could create the intermediate disk image in RAM (tmpfs) to save time in any file system with free space (e.g. /tmp), resize, and then dd it to your hard disk.

If your data doesn't change once written, you should consider using SquashFS or a similar filesystem instead, which will be more compact than ext3. Otherwise, consider ext2, which avoids the overhead of journalling.

share|improve this answer
This will leave the image file the same size after shrinking the partition on it. And while it's larger than the partition anyway, I need the data after that partition to be left untouched by this process. I mentioned that the amount of data varies. It is in almost all cases multiple GB. I'm already using compressed file systems, but in this case, I need it to be ext2. – Daniel Beck Aug 6 '11 at 5:17
@Daniel Beck: ext2 or ext3? – Mechanical snail Aug 6 '11 at 5:31
@Daniel Beck: On my system (resize2fs 1.41.12), it does shrink the image. What do you mean by "data after that partition"? – Mechanical snail Aug 6 '11 at 5:37
Sorry, ext3, just like I wrote in the original question. ALso, there are multiple partitions on the image, complete with boot sector. That's probably why nothing gets resized for me (and I actually don't want any resizing). I admit it's a particular problem with odd constraints. – Daniel Beck Aug 6 '11 at 15:12
Why do you need to put multiple partitions on your temporary image? – Mechanical snail Aug 7 '11 at 4:52

You can use the -b option of mkfs (or mke2fs) to explicitly set the block size or you can use the -T option to specify the type of files. Check the manual page for details.

You can also need to make sure the number of inodes and journal size is appropriate though.

share|improve this answer
I already use -b. I reserve no sectors for root. I'll have to look into journal size though, I'm not sure right now. – Daniel Beck Aug 5 '11 at 20:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .