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

hi,
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!
__________________
[SIGPIC][/SIGPIC]

Last edited by vertextao; 4th June 2012 at 09:26 AM. Reason: Just change method.
Reply With Quote
  #2  
Old 31st May 2012, 11:00 AM
stevea Offline
Registered User
 
Join Date: Apr 2006
Location: Ohio, USA
Posts: 8,552
linuxfirefox
Re: how to get a partition's uuid

blkid

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.
__________________
None are more hopelessly enslaved than those who falsely believe they are free.
Johann Wolfgang von Goethe

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

lsblk
blkid

These 2 commands are most helpfull (to me at least) to find partition labels.
__________________
Notebook: Samsung NC210/NC110 CPU: Intel 2*1600 Mhz RAM: 1024 MB DDR2 OS: Fedora 19 DE: Awesome
Laptop: Toshiba Satellite L670 CPU: Intel(r) 2*1872 Mhz RAM: 3072 MB DDR3 OS: Fedora 17 DE: Awesome
Fedora Manual (RTFM) | The Linux Documentation Project | Script-Tools
Reply With Quote
  #4  
Old 31st May 2012, 11:47 AM
markus39 Offline
Registered User
 
Join Date: Jul 2008
Location: Maastricht, the Netherlands
Age: 47
Posts: 68
linuxfirefox
Re: how to get a partition's uuid

cd /dev/disk/by-uuid
ls -la
Reply With Quote
  #5  
Old 31st May 2012, 06:38 PM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 5,457
linuxfedorafirefox
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:
Code:
#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_do_probe(pr);
   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]);
   }
   blkid_free_probe(pr);
   return 0;
}
I then compiled it and ran it like this, with the partition device name as a command-line parameter:
Code:
$ gcc -o getuuid getuuid.c -lblkid
$ ./getuuid /dev/sda1
UUID=a52279f8-1ddf-44ec-9f17-637ff91256f0
__________________
OS: Fedora 20 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 5GB 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
  #6  
Old 28th June 2012, 04:33 AM
manhh Offline
Registered User
 
Join Date: Mar 2008
Location: China
Posts: 119
windows_7opera
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
  #7  
Old 28th June 2012, 07:07 PM
srs5694 Offline
Registered User
 
Join Date: Jan 2011
Location: Woonsocket, RI
Posts: 521
linuxfirefox
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
Reply

Tags
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 08:15 PM
Help with fstab/uuid reidr Alpha, Beta & Snapshots Discussions (Fedora 10 Only) 4 22nd November 2008 03: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 04: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: 10:36 (Friday, 18-04-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

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