[et-mgmt-tools] [PATCH] Provide a common block device size utility for Solaris & Linux
Daniel P. Berrange
berrange at redhat.com
Thu Dec 4 10:40:52 UTC 2008
On Thu, Dec 04, 2008 at 03:24:16AM +0000, John Levon wrote:
> On Wed, Dec 03, 2008 at 09:37:57AM +0000, Daniel P. Berrange wrote:
>
> > > +def blkdev_size(path):
> > > + if platform.system() == 'SunOS':
> > > + return os.stat(path)[stat.ST_SIZE]
> > > + else:
> > > + dummy, msg = commands.getstatusoutput('fdisk -s %s' % path)
> > > + # check
> > > + if msg.isdigit() == False:
> > > + lines = msg.splitlines()
> > > + # retry eg. for the GPT disk
> > > + msg = lines[len(lines)-1]
> > > + return (int(msg) * 1024)
> >
> >
> > ACK, to the general idea of adding a common routine. The original Linux
> > impl was kind of crazy. We could get a single portable impl for all
> > OS by using seek(), which is how libvirt does it in its storage code.
>
> Quite honestly, I wasn't entirely sure why you're using fdisk. But if
> you don't need to, why use lseek() when os.stat() does the job?
Because stat() returns 0 for the size of block devices on many UNIX
including Linux.
# perl -e 'use File::stat; my $f = stat("/dev/sda1"); printf "%d\n", $f->size'
0
# perl -e 'use POSIX; my $f = POSIX::open("/dev/sda1", O_RDONLY); printf "%d\n", (POSIX::lseek $f, 0, 2)'
106896384
Daniel
--
|: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :|
|: http://libvirt.org -o- http://virt-manager.org -o- http://ovirt.org :|
|: http://autobuild.org -o- http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
More information about the et-mgmt-tools
mailing list