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
[Corion]: thezip: Yeah - we have "CAP", but that means "Change Ablauf Plan" (Change Sequence Plan). Likely there is a true ITIL name for this too.
[Corion]: While I'm not really a fan of ITIL, it certainly is better than nothing and it at least provides a common vocabulary, so if you have ITIL somewhere at your disposal, it might be worth a look
[Corion]: Hmm - that should be somewhere in the "Release Deployment Plan", but I don't find a good English link (nor a good German one ;) )
[thezip]: My project manager is asking "Why do we need this?" Ahem...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2017-02-23 17:57 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (350 votes). Check out past polls.