steve landiss
steve landiss

Reputation: 1913

How do I get the device given a UUID from libblkid?

I know how to get the UUID of a device via libblkid... how do I do the reverse?

Given a UUID, I want to find what the device path is.

Upvotes: 0

Views: 397

Answers (2)

steve landiss
steve landiss

Reputation: 1913

I wanted a programmatic way of doing this and this below worked.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <blkid/blkid.h>
char *get_disk(char *disk) {
   const char *uuid;
   char query[4096];

   snprintf(query, sizeof(query), "UUID=%s", disk);

   uuid = blkid_evaluate_tag(query, NULL, NULL);

   if (uuid == NULL) {
       uuid = "";
   }

   return strdup(uuid);
}

int main(int argc, char **argv)
{
    fprintf(stderr, "%s\n", get_disk(argv[1]));
}

Upvotes: 1

jforberg
jforberg

Reputation: 6752

It's as easy as /dev/disk/by-uuid/{your-uuid}. No C code or libaries necessary.

Upvotes: 0

Related Questions