Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

(OT) how to identify a partition

by leocharre (Priest)
on Apr 18, 2008 at 15:49 UTC ( #681543=perlquestion: print w/replies, xml ) Need Help??
leocharre has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on keeping track of data accross a network. And yes I am convinced perl is just fine for managing heavy stuff like this. Or it will be. (Paul Graham is da man!!!)

How would I go about identifying a partition, or at least a physical drive?

Maybe id'ing a partition would be easier? It seems I could simply put a readonly file at the root of the partition, like /.ptnid.0123456789.

Maybe if I looked up a way to tap into the hd bios chip or whatnot, there would be an id there? But this would only be useful for the actual driv, not so much the concept of 'partition'.

Looked on cpan and I see little about this kind of thing- maybe that's the way it should be?

I'm using ext3, maybe some fs types keep an id themselves (in the partition/inode/etc table ) ?

Any suggestions where I could find out more? I've read up on inode/fs design and some other creepy beautiful things, it hurts my head. But I can delve deeper if that's the road to the wizard.

Replies are listed 'Best First'.
Re: (OT) how to identify a partition
by walto (Pilgrim) on Apr 18, 2008 at 16:25 UTC
    I am not sure that I understood what you need but maybe Sys::Filesystem fits your needs.

    After you specified your question this could be helpful
    #!/usr/bin/perl -w # # use strict; my $partitions = `hdparm -i /dev/hda|grep Model`; my ($model,$firmware,$serial_no)=split /,/,$partitions; my @model = split /=/,$model; my @serial_no=split /=/,$serial_no; my @firmware=split /=/,$firmware; print "Model = $model[1]\tFirmware=$firmware[1]\tSerial No=$serial_no[ +1]\n";
    The script must be run with root priviliges. If you use sudo you can use
    my $partitions = `sudo hdparm -i /dev/hda|grep Model`;

      Sys::Filesystem is a little bit helpful, yes.

      Your suggestion hdparm is spectacularly useful. I can get a serial id off the drive this way! This helps identify a drive, marvelously.

      I'm still looking into identifying an actual partition. I have a hack to identify a filesystem, via the filesystem. But there must be something in the fs format that has this as well.

      Thank you, very useful!

Re: (OT) how to identify a partition
by rowdog (Curate) on Apr 18, 2008 at 19:56 UTC
    Yes indeed, the e2fs family of filesystems generate a UUID for each partition. Under Linux (Ubuntu for sure), you can find your mounted disks listed by partition UUID in /dev/disks/by-uuid/*

    If you don't mind using root, you can get at the UUID through the e2fsprogs (e.g. vol_id and findfs).

Re: (OT) how to identify a partition
by mr_mischief (Monsignor) on Apr 18, 2008 at 16:12 UTC
    What drive are you looking to identify, and for what purpose?

    You can easily see what partitions are mounted on most Unixes using a command similar to 'mount'. In the case of Linux (with the /proc system in place, at least, which should be true on any general-purpose system), you can read a file called /proc/mounts that gives you that information as well.

    Are you looking for the partition device name, the DOS-compatible partition number, the drive's hardware serial number, or what?

      Thank you,
      I'm sorry, by identify- I mean track, I mean like.. tattoo it, like.. I want its soul.
      Thus, the basic mount info is useless- if I unplug a drive and plug it into another machine, for example.

      Drive serial number would be useful. I would like to do that via code, instead of busting the dang thing open.

      Getting an ID of the actual fs would be very interesting also- I am guessing some formats keep an id of some sort.

        You could look into hdparm or your could poke through the /proc entries for the hard drives.

        /proc/ide/ide0/hdb/ for instance will have lots of info on the second drive on your first IDE channel. /proc/ide3/hdg/model will have the model number of the first drive on your 4th IDE channel.

        I'm not entirely sure how unique it is, but I think it's partly from the serial number, so /proc/ide/ide0/hda/identify should have some identifying information for the first IDE drive on your first IDE channel. Hopefully it's universally unique.

Re: (OT) how to identify a partition
by dwm042 (Priest) on Apr 18, 2008 at 20:18 UTC
    If you have access to root on *nix there is the file /etc/fstab (or vfstab), the /dev/dsk directory and using fdisk (prtvtoc in Solaris) on all the system disks you can find. On systems with Veritas, you have vxdisk list, etc. mr_mischief has suggested 'mount', which should work well.

    walto's suggestion, using the module Sys::Filesystem, seems far more portable, however. Then you're not restricted to a single OS or file system.
Re: (OT) how to identify a partition
by oko1 (Deacon) on Apr 19, 2008 at 16:19 UTC

    Unfortunately, Sys::Filesystem's "OS-neutral" status is more of a wish than a reality; reading the documentation shows that many of the modules are still either "not written yet" or "require additional work". There is an easier way, however - one that doesn't require any "heavy stuff".

    Linux partitions already have a unique ID - and they're pretty easy to find. Better yet for your purposes, they're a unique identifier (to quote Wikipedia, "1 trillion UUIDs would have to be created every nanosecond for 10 billion years to exhaust the number of UUIDs.")

    ben@Tyr:~$ ls -l /dev/disk/by-uuid total 0 lrwxrwxrwx 1 root root 10 2008-04-18 20:40 41d131d8-3128-4d41-8230-ca1 +bd3c0e8c5 -> ../../hda5 lrwxrwxrwx 1 root root 10 2008-04-18 20:40 72aca3d8-6309-47ac-ad38-b71 +fb9cafddc -> ../../hda1

    So, the easy way to do this would be to 1) plant an SSH authorized_key on every machine you have, 2) interrogate them with 'ssh user@host "ls -l /dev/disk/by-uuid"', and 3) save the results for later reference. So there's your "over the network" bit, as well.

    Human history becomes more and more a race between education and catastrophe. -- HG Wells

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://681543]
Approved by grep
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2017-07-27 20:50 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (422 votes). Check out past polls.