Fedora Linux Support Community & Resources Center
Old 31st May 2012, 10:40 AM
vertextao Offline
Registered User
Join Date: Sep 2010
Location: China
Posts: 75
Question how to get a partition's uuid

I need to identify whether a partition is the right one I need, I know a disk partition has a UUID, but I don't know how to get it in C programming[Linux], is there a system call to get it? Or other methods.

thanks a lot for sharing your opinion!


hey, everybody! I have changed my mind to identify the partition -- through [cylinder range], cause not every system[especially embedded systems] has UUID map.
I'd like to make things simple, so do you have any simply way to get the cylinder range of partitions, Thanks!

Last edited by vertextao; 4th June 2012 at 09:26 AM. Reason: Just change method.
Reply With Quote
Old 31st May 2012, 11:00 AM
Posts: n/a
Re: how to get a partition's uuid


Oh - C.
Pull the source for blkid. The uuid is in the ext2/3/4 info ad buried in the 1st block or 2 of swap.

---------- Post added at 06:00 AM ---------- Previous post was at 05:48 AM ----------

libblkid-devel package and the related libuuid-devel hold the magic sauce.

Last edited by stevea; 31st May 2012 at 10:50 AM.
Reply With Quote
Old 31st May 2012, 11:38 AM
sea Offline
"Shells" (of a sub world)
Join Date: May 2011
Location: Confoederatio Helvetica (Swissh)
Age: 37
Posts: 4,277
Re: how to get a partition's uuid


These 2 commands are most helpfull (to me at least) to find partition labels.
EFI Cheatsheet :: http://forums.fedoraforum.org/showthread.php?t=298546
Video Handler Script (VHS) (mass re-encode videos, screenrecorder, console music/webradio player, ...) :: http://forums.fedoraforum.org/showthread.php?t=299182
Windows 8+ & Fedora 20+ Dualboot :: http://forums.fedoraforum.org/showthread.php?t=298161
Reply With Quote
Old 31st May 2012, 11:47 AM
markus39 Offline
Registered User
Join Date: Jul 2008
Location: Maastricht, the Netherlands
Age: 50
Posts: 68
Re: how to get a partition's uuid

cd /dev/disk/by-uuid
ls -la
Reply With Quote
Old 31st May 2012, 06:38 PM
RupertPupkin Offline
Registered User
Join Date: Nov 2006
Location: Detroit
Posts: 6,633
Re: how to get a partition's uuid

Look at the first example here: http://stackoverflow.com/questions/6...of-a-partition
After installing the libblkid-devel package I copied that example on my F17 system and it worked.

I modified the code a little bit, and called it getuuid.c:
#include <stdio.h>
#include <string.h>
#include <err.h>
#include <blkid/blkid.h>
int main (int argc, char *argv[]) {
   blkid_probe pr = blkid_new_probe_from_filename(argv[1]);
   if (!pr) {
      err(1, "Failed to open %s", argv[1]);
   const char *uuid;
   blkid_probe_lookup_value(pr, "UUID", &uuid, NULL);
   if (strlen(uuid) > 1) {
      printf("UUID=%s\n", uuid);
   } else {
      printf("%s has no UUID\n", argv[1]);
   return 0;
I then compiled it and ran it like this, with the partition device name as a command-line parameter:
$ gcc -o getuuid getuuid.c -lblkid
$ ./getuuid /dev/sda1
OS: Fedora 26 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 7GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N

Last edited by RupertPupkin; 1st June 2012 at 04:26 AM. Reason: Added modified code
Reply With Quote
Old 28th June 2012, 04:33 AM
manhh Offline
Registered User
Join Date: Mar 2008
Location: China
Posts: 121
Re: how to get a partition's uuid

Join Date: Sep 2010
Location: China
Posts: 74

which city base on?
Lenovo G475g
amd e300 apu + ram 3g
win8 + ubuntu 1204 amd64
Reply With Quote
Old 28th June 2012, 07:07 PM
srs5694 Offline
Registered User
Join Date: Jan 2011
Location: Woonsocket, RI
Posts: 521
Re: how to get a partition's uuid

One further note: The UUID is a feature of certain filesystems (and also Linux swap space). A partition without a filesystem will have no UUID, and some filesystems (such as FAT and NTFS) lack UUID values, although utilities like blkid return these filesystems' non-UUID serial numbers and label them as "UUIDs." The code that RupertPupkin presented does the same. Depending on your needs, that might be fine, but if your code assumes a full UUID, it could run into bugs if you use it on a filesystem that has a non-UUID serial number. IIRC, partitions that use ReiserFS prepared from Fedora lack UUIDs entirely.

If your disk uses the GUID Partition Table (GPT), every partition has associated with it a GUID, which is basically a UUID with some minor representational differences. This is true no matter what filesystem the partition holds, or even if it's entirely empty. Thus, if you know you'll be using GPT disks and if you need a unique identifier for every partition even if it's empty or if its filesystem might lack a UUID, using the partition's GUID might be a better option than using the filesystem's UUID. The drawback to this approach is that it works only on GPT disks; if the disks might be or definitely are Master Boot Record (MBR) disks, it won't work at all. Likewise if you're interested in filesystems held within an LVM configuration. I don't have any C code offhand that demonstrates how to get a GPT partition's GUID, but it's conceivable that something in libparted will help with the task, so you could look into that. GPT fdisk (gdisk) also contains code to do the job, but you'd need to use an awful lot of gdisk's code to get at the GUID values. Be aware that every partition on a GPT disk has two GUIDs associated with it. One is unique to the partition in question, and the other is a partition type code. You presumably want the unique GUID, not the type code GUID, so be sure to use the correct one.
Reply With Quote

partition, uuid

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
uuid issue? Jongi Using Fedora 1 27th November 2009 09:15 PM
Help with fstab/uuid reidr Alpha, Beta & Snapshots Discussions (Fedora 10 Only) 4 22nd November 2008 04:59 AM
Force UUID my_linux Using Fedora 0 11th October 2007 01:21 PM
Mount by UUID Jongi Linux Chat 1 13th January 2007 05:41 AM
How to use debugfs to recover partition's root dir. fnxweb Using Fedora 2 25th June 2005 06:40 PM

Current GMT-time: 15:56 (Thursday, 21-09-2017)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat