Nikita Kodkani
Nikita Kodkani

Reputation: 165

Linux kernel: How to get dev_t of whole disk from partitioned disk?

I want to retrieve the dev_t structure for a whole disk, given that of a partition on the disk. This is for for a blkext driver, like nvme.

Something like:

dev_t part_disk;
dev_t whole_disk = get_whole_disk_dev_t(part_disk);

I would like my the interface of get_whole_disk_dev_t() to be:

Argument: dev_t: part_disk
Return: dev_t: whole_disk

My proposed algorithm:

  1. Get path of partitioned disk from dev_t i.e "/dev/nvme1n1p3". I couldn't find any API to get path from dev_t.
  2. Do some string manipulation to remove "p3", giving the path for whole disk
  3. Pass the path_name of whole disk to blk_lookup_devt to get the dev_t of whole disk.
    i.e whole_disk=blk_lookup_devt(path_name,0)

Is this the right approach or is there a better approach? If the former, then how can I get path from dev_t?

P.S: I need to implement this in a device driver (i.e in kernel context). In user space I know I can use the udev API.

Upvotes: 1

Views: 1499

Answers (2)

Oleshii Wood
Oleshii Wood

Reputation: 1

It's not a problem: you can read /proc/partitions file, obtain needed sd[a|b|c] drive id. Suppose, that id: char devId[] = "sdb" after that invoking devno = blk_lookup_devt(devId, 0);

Upvotes: 0

Nikita Kodkani
Nikita Kodkani

Reputation: 165

static int hello_init(void)

{

dev_t devno;

struct block_device *bddev;

struct gendisk *bddisk;

char disk_major[32];

devno=MKDEV(252,67);

printk(KERN_ALERT "Hello, world .... current devno is %x \n", devno);

bddev=bdget(devno);

if(bddev) {

    bddisk=bddev->bd_disk;

    if(bddisk) {

         strcpy(disk_major,bddisk->disk_name);

         printk(KERN_ALERT "bddisk is present %s", disk_major);

     } else {

       printk(KERN_ALERT "bddisk is not present");

    }

} else {

printk(KERN_ALERT "bdev failed ");

}

devno = blk_lookup_devt(disk_major,0);
printk(KERN_ALERT "after blk_lookup_dev:  dev_t: 0x%x  Major No %d   Minor No %d \n", devno, MAJOR(devno), MINOR(devno));

return 0;

}


static void hello_exit(void)
{

printk(KERN_ALERT "Goodbye, world\n");

}

Upvotes: 1

Related Questions