Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

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.

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 (Parson) 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," 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://971608]
Approved by ww
[LanX]: hmm this joke may be too old for millennials
[LanX]: s/joke/reference/
[Eily]: well, I didn't get the joke and I didn't get the image either because the proxy won't let me
[erix]: Leasure Suit L.
[LanX]: THEY are restricting your Internet?
[LanX]: join the fight brother

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (15)
As of 2017-03-23 17:44 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (292 votes). Check out past polls.