How to get device name with device id?

Smit Shah getsmit at gmail.com
Fri Jun 26 01:58:04 UTC 2009


Hey,

I just ran through this post, and since no solution was posted, here's
a piece of code that i had written
Infact when i wrote this i wanted both the device name and also the
file system on that partition.

But for this, one needs the root privileges.

#include<stdio.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include<assert.h>

#include<blkid/blkid.h>

int main(int argc,char *argv[])
{
        struct blkid_struct_dev *dev;
        struct blkid_struct_cache *cache;
        char *type,*dev_name;
        struct stat buf;

        assert(argc == 2);
        assert(argv[1]);
        assert(lstat(argv[1],&buf) == 0);
        dev_name=blkid_devno_to_devname(buf.st_dev);
        assert(dev_name);
        printf("Device name :%s\n",dev_name);

        assert(blkid_get_cache(&cache,"/dev/null") == 0);
        dev = (struct blkid_struct_dev *)blkid_get_dev(cache,dev_name,
BLKID_DEV_NORMAL);
        assert(dev);
        type = blkid_get_tag_value(cache,"TYPE",dev_name);

        printf("File System type is : %s\n",type);
        blkid_free_dev(dev);
        return (0);
}

You have to compile it with the libblkid.
Do man for libblkid, that should be really helpful.

-Smit Shah.


--- On Thu, 20/3/08, liuyue <liuyue ncic ac cn> wrote:

> From: liuyue <liuyue ncic ac cn>
> Subject: Re: How to get device name with device id?
> To: "ashitpro yahoo co in" <ashitpro yahoo co in>, "ext3-users redhat com" <ext3-users redhat com>

> Date: Thursday, 20 March, 2008, 4:34 PM
> ashish mahamuni,
>
> I guess maybe the following function does what you want.
> But it is a kernel function, sorry :(
>
> int __file_to_disk (char * file_name,  char *disk_name) {

>         int err = 0;
>         struct nameidata        nd;
>         struct super_block      * sb;
>         struct vfsmount *mnt;
>         err = path_lookup(file_name, LOOKUP_FOLLOW,
> &nd);

>         if(err){
>                 DCFS3_ERROR("error to parse the file
> name, %s\n", file_name);
>                 goto exit;
>         }
>         mnt = nd.mnt;
>         sb = mnt->mnt_sb;

>         strcpy (disk_name,
> sb->s_bdev->bd_disk->disk_name);
>         path_release(&nd);
> exit:
>         return err;
> }
>
> ======= 2008-03-20 19:21:04

> 您在来信中写道:=======
>
> >Hi all,
> >
> >I want to open a device(/dev/sda1, /dev/hda2 etc) in
> which my file exists.
> >I've used 'stat' system call to get the

> device id.
> >
> >But now I want the device name from this id(st_dev).
> >How to get that one?
> >Or
> >Do you have any other method to know the device name
> where my file resides?

> >
> >Thanks
> >
> >
> >      Bollywood, fun, friendship, sports and more. You
> name it, we have it on http://in.promos.yahoo.com/groups

> >
> >
> >_______________________________________________
> >Ext3-users mailing list
> >Ext3-users redhat com
> >https://www.redhat.com/mailman/listinfo/ext3-users

> >
> >
>
> = = = = = = = = = = = = = = = = = = = =
> 			
>
>         致
> 礼!
>
> 				
>         liuyue
>         liuyue ncic ac cn
>           2008-03-20
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listman.redhat.com/archives/ext3-users/attachments/20090625/3fd25fe2/attachment.htm>


More information about the Ext3-users mailing list