2012-04-30 17:19:31

by Daniel Drake

[permalink] [raw]
Subject: Determining if an ext4 fs uses the whole partition

Hi,

OLPC has started using ext4 online resizing to grow our filesystems to
use the whole SD card on first boot - something we never did before.
Working very nicely, thanks!

I'm trying to simplify/improve the scripts involved in doing this.

How can I programatically check if an ext4 fs already fills its
partition, or if it has room to grow?


The numbers produced by dumpe2fs (e.g. block count) or "df" don't seem
to exactly line up with the sizes produced by fdisk.
One easy solution, if possible, would be to find out the number of the
last sector used by the filesystem. I could then very easily compare
this to the "end" information found in sysfs for the partition. Then I
can make the decision on whether to grow or not.

Thanks
Daniel


2012-04-30 17:30:04

by Eric Sandeen

[permalink] [raw]
Subject: Re: Determining if an ext4 fs uses the whole partition

On 4/30/12 12:19 PM, Daniel Drake wrote:
> Hi,
>
> OLPC has started using ext4 online resizing to grow our filesystems to
> use the whole SD card on first boot - something we never did before.
> Working very nicely, thanks!
>
> I'm trying to simplify/improve the scripts involved in doing this.
>
> How can I programatically check if an ext4 fs already fills its
> partition, or if it has room to grow?
>
>
> The numbers produced by dumpe2fs (e.g. block count) or "df" don't seem
> to exactly line up with the sizes produced by fdisk.

Do you have an example of this?

For starters, use fdisk -u to get 512-byte sector units,
otherwise it's just inscrutable CHS magic.

i.e.:

# fdisk -lu /dev/sda2

Disk /dev/sda2: 526 MB, 526417920 bytes
255 heads, 63 sectors/track, 64 cylinders, total 1028160 sectors
Units = sectors of 1 * 512 = 512 bytes

so 1028160 512-byte sectors.

# dumpe2fs -h /dev/sda2 | grep "Block count\|Block size"
dumpe2fs 1.42.2 (27-Mar-2012)
Block count: 514080
Block size: 1024

so 514080 1k blocks, or 1028160 512-byte sectors, so bingo, it's full.

Of course you can't use an odd sector hanging off the end, so you'd
need to do a little rounding down to the nearest fs block size.
Otherwise, it should be straightforward.

Or, FWIW, it's harmless to invoke resize2fs if the fs already fills the
partition; it should just exit with a no-op.

> One easy solution, if possible, would be to find out the number of the
> last sector used by the filesystem. I could then very easily compare
> this to the "end" information found in sysfs for the partition. Then I
> can make the decision on whether to grow or not.

dumpe2fs should certainly be able to tell you. Mounting the fs, and
doing statfs would, as well (f_blocks). There should also be libext2fs
functions you could use if you want to do it in C...

-Eric

2012-04-30 17:33:30

by djwong

[permalink] [raw]
Subject: Re: Determining if an ext4 fs uses the whole partition

On Mon, Apr 30, 2012 at 11:19:31AM -0600, Daniel Drake wrote:
> Hi,
>
> OLPC has started using ext4 online resizing to grow our filesystems to
> use the whole SD card on first boot - something we never did before.
> Working very nicely, thanks!
>
> I'm trying to simplify/improve the scripts involved in doing this.
>
> How can I programatically check if an ext4 fs already fills its
> partition, or if it has room to grow?

Is it not the case that there's room to grow if:

(ext4_block_count * ext4_block_size) - (bdev_size * 512) >= ext4_block_size

> The numbers produced by dumpe2fs (e.g. block count) or "df" don't seem
> to exactly line up with the sizes produced by fdisk.

How far off are the counts? A small number of sectors, or many?

--D

> One easy solution, if possible, would be to find out the number of the
> last sector used by the filesystem. I could then very easily compare
> this to the "end" information found in sysfs for the partition. Then I
> can make the decision on whether to grow or not.
>
> Thanks
> Daniel
> --
> To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>


2012-04-30 17:37:55

by Jeff Moyer

[permalink] [raw]
Subject: Re: Determining if an ext4 fs uses the whole partition

Eric Sandeen <[email protected]> writes:

> On 4/30/12 12:19 PM, Daniel Drake wrote:
>> Hi,
>>
>> OLPC has started using ext4 online resizing to grow our filesystems to
>> use the whole SD card on first boot - something we never did before.
>> Working very nicely, thanks!
>>
>> I'm trying to simplify/improve the scripts involved in doing this.
>>
>> How can I programatically check if an ext4 fs already fills its
>> partition, or if it has room to grow?
>>
>>
>> The numbers produced by dumpe2fs (e.g. block count) or "df" don't seem
>> to exactly line up with the sizes produced by fdisk.
>
> Do you have an example of this?
>
> For starters, use fdisk -u to get 512-byte sector units,
> otherwise it's just inscrutable CHS magic.

Note that fdisk -u gives sector units, which may or may not be 512
bytes:

$ sudo fdisk -lu /dev/sdb
Note: sector size is 4096 (not 512)

Disk /dev/sdb: 300.1 GB, 300069052416 bytes
255 heads, 63 sectors/track, 4560 cylinders, total 73259046 sectors
Units = sectors of 1 * 4096 = 4096 bytes
Sector size (logical/physical): 4096 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x00000000

Cheers,
Jeff

2012-04-30 18:18:49

by Daniel Drake

[permalink] [raw]
Subject: Re: Determining if an ext4 fs uses the whole partition

On Mon, Apr 30, 2012 at 11:29 AM, Eric Sandeen <[email protected]> wrote:
> For starters, use fdisk -u to get 512-byte sector units,
> otherwise it's just inscrutable CHS magic.
>
> i.e.:
>
> # fdisk -lu /dev/sda2
>
> Disk /dev/sda2: 526 MB, 526417920 bytes
> 255 heads, 63 sectors/track, 64 cylinders, total 1028160 sectors
> Units = sectors of 1 * 512 = 512 bytes
>
> so 1028160 512-byte sectors.
>
> # dumpe2fs -h /dev/sda2 | grep "Block count\|Block size"
> dumpe2fs 1.42.2 (27-Mar-2012)
> Block count: ? ? ? ? ? ? ?514080
> Block size: ? ? ? ? ? ? ? 1024
>
> so 514080 1k blocks, or 1028160 512-byte sectors, so bingo, it's full.

Hmm yes, following the same process I can get the same results. I must
have misread/miscalculated something when I looked earlier.
User error :)

> Or, FWIW, it's harmless to invoke resize2fs if the fs already fills the
> partition; it should just exit with a no-op.

Thanks for pointing this out - its probably my best option, coming to
think of it.

>> One easy solution, if possible, would be to find out the number of the
>> last sector used by the filesystem. I could then very easily compare
>> this to the "end" information found in sysfs for the partition. Then I
>> can make the decision on whether to grow or not.
>
> dumpe2fs should certainly be able to tell you. ?Mounting the fs, and
> doing statfs would, as well (f_blocks). ?There should also be libext2fs
> functions you could use if you want to do it in C...

Trying the statfs approach (the fs in question is already mounted):

# dumpe2fs -h /dev/mmcblk0p2 | grep "Block count\|Block size"
dumpe2fs 1.42 (29-Nov-2011)
Block count: 949248
Block size: 4096

# stat -f /
File: "/"
ID: f09a7645207bdd68 Namelen: 255 Type: ext2/ext3
Block size: 4096 Fundamental block size: 4096
Blocks: Total: 934935 Free: 198205 Available: 188947
Inodes: Total: 227824 Free: 133103

The numbers don't agree.

(Not a big deal, since I can use the other 2 approaches you mentioned,
just wanted to point it out)

Thanks
Daniel

2012-04-30 18:26:17

by Eric Sandeen

[permalink] [raw]
Subject: Re: Determining if an ext4 fs uses the whole partition

On 4/30/12 1:18 PM, Daniel Drake wrote:

...

> Trying the statfs approach (the fs in question is already mounted):
>
> # dumpe2fs -h /dev/mmcblk0p2 | grep "Block count\|Block size"
> dumpe2fs 1.42 (29-Nov-2011)
> Block count: 949248
> Block size: 4096
>
> # stat -f /
> File: "/"
> ID: f09a7645207bdd68 Namelen: 255 Type: ext2/ext3
> Block size: 4096 Fundamental block size: 4096
> Blocks: Total: 934935 Free: 198205 Available: 188947
> Inodes: Total: 227824 Free: 133103
>
> The numbers don't agree.
>
> (Not a big deal, since I can use the other 2 approaches you mentioned,
> just wanted to point it out)

Oh, that's because statfs on extN subtracts out the "overhead" from superblocks,
block group descriptors, etc. I guess that's a reasonable interpretation
of "/* total data blocks in file system */" but honestly I forgot that
it did that... TBH, not entirely sure why it does.

-Eric

2012-04-30 19:01:56

by Theodore Ts'o

[permalink] [raw]
Subject: Re: Determining if an ext4 fs uses the whole partition

On Mon, Apr 30, 2012 at 01:37:42PM -0400, Jeff Moyer wrote:
>
> Note that fdisk -u gives sector units, which may or may not be 512
> bytes:
>
> $ sudo fdisk -lu /dev/sdb
> Note: sector size is 4096 (not 512)

Note: you can also get the same numbers from /proc/partitions.

Regards,

- Ted