Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

List drives in linux using Perl

by doubledecker (Scribe)
on May 21, 2012 at 13:03 UTC ( #971608=perlquestion: print w/ replies, xml ) Need Help??
doubledecker has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

I'm looking for any way to list all the drives in linux box using Perl. 'fdisk' is the best utility i can use but unable to parse the output of the command. Is there any module or code which can help me.

Comment on List drives in linux using Perl
Replies are listed 'Best First'.
Re: List drives in linux using Perl
by Corion (Pope) on May 21, 2012 at 13:05 UTC

    Depending on what you mean by "drives" on a unix-style file system, salva just released Linux::Proc::Mountinfo, which lists the mounted filesystems, which is as close to the concept of a drive as you can get on Linux.

Re: List drives in linux using Perl
by salva (Abbot) on May 21, 2012 at 13:25 UTC
    You can use strace to see what fdisk does under the hood and then reimplement it in Perl

    ...or just read fdisk source code!

Re: List drives in linux using Perl
by RichardK (Vicar) on May 21, 2012 at 14:05 UTC

    Or alternatively 'cat /proc/diskstats' :)

Re: List drives in linux using Perl
by Neighbour (Friar) on May 21, 2012 at 13:35 UTC
    How about `cat /proc/partitions` and go from there?
Re: List drives in linux using Perl
by Khen1950fx (Canon) on May 21, 2012 at 19:11 UTC
    FWIW, I used Sys::Filesystem to get mount points:
    #!/usr/bin/perl -l use strict; use warnings; use Sys::Filesystem; my $drive = Sys::Filesystem->new(); my (@drives) = $drive->filesystems(); foreach (@drives) { printf("%s \n", $drive->mount_point($_), ); }
Re: List drives in linux using Perl
by Anonymous Monk on May 21, 2012 at 17:30 UTC

    A word of caution on using fdisk -l, until recently, if you use LVM2, fdisk would report all the /dev/mapper names as well as the "dm-1, dm-2...dm-N" names, so you would need to remove the redundancies. This is fixed in util-linux 2.21.1 or so but your probably don't have a recent version like that unless you're on Arch Linux.

    Myself, I'd probably start with /proc/diskstats and then, perhaps, I'd dig into /sys/block for more details.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://971608]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2015-08-28 03:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The oldest computer book still on my shelves (or on my digital media) is ...













    Results (334 votes), past polls