Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
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
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 (Monsignor) 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 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 RichardK (Priest) on May 21, 2012 at 14:05 UTC

    Or alternatively 'cat /proc/diskstats' :)

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.

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($_), ); }

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 musing on the Monastery: (6)
As of 2014-08-01 06:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (257 votes), past polls