[Hampshire] stat(1)

Top Page
Author: Hugo Mills
Date:  
To: Hants LUG
Subject: [Hampshire] stat(1)

Reply to this message
gpg: failed to create temporary file '/var/lib/lurker/.#lk0x581fc100.hantslug.org.uk.12295': Permission denied
gpg: keyblock resource '/var/lib/lurker/pubring.gpg': Permission denied
gpg: Signature made Tue Apr 29 15:36:08 2008 BST
gpg: using DSA key 20ACB3BE515C238D
gpg: Can't check signature: No public key
I'm trying to determine the size of a block device in a shell
script, and I'm having some trouble.

For a normal file:

hrm@vlad:vamos $ sudo stat -c "blocks: %b block size: %B I/O block size: %o total size: %s" COPYING
blocks: 40 block size: 512 I/O block size: 4096 total size: 18007

this is exactly what I wanted -- I get the block size, and the number
of blocks of the file. However, for a block device:

hrm@vlad:vamos $ sudo stat -c "blocks: %b block size: %B I/O block size: %o total size: %s" /dev/sda1
blocks: 0 block size: 512 I/O block size: 4096 total size: 0

Not quite what I wanted (and not entirely true). There's a -f
option to stat that talks about filesystems, but that doesn't seem to
work either:

hrm@vlad:vamos $ sudo stat -f -c "blocks: %b Block size: %s fundamental size: %S" /dev/loop0
blocks: 2560 Block size: 4096 fundamental size: 4096

/dev/loop0 in this case is a 512-byte file, so I don't think it
contains 2560 blocks of 4k each. (Actually, on investigation, it's
telling me about the filesystem that /dev/loop0 is held on... but
since the block devices I'm working with don't necessarily *have* a
filesystem yet, that's also less than useful).

The information I want appears to be in /sys/block/ but I can't see
how to find the right device, given that the device name I'm given may
be a symlink, or even a device node with a non-canonical name, so I
can't see that that would be useful.

Anyone got any ideas?

Hugo.

-- 
=== Hugo Mills: hugo@... carfax.org.uk | darksatanic.net | lug.org.uk ===
  PGP key: 515C238D from wwwkeys.eu.pgp.net or http://www.carfax.org.uk
  --- I am but mad north-north-west:  when the wind is southerly, I ---  
                       know a hawk from a handsaw.