 |
 |
 |
 |
| Programming & Packaging A place to discuss programming and packaging. |

31st May 2012, 10:40 AM
|
 |
Registered User
|
|
Join Date: Sep 2010
Location: China
Posts: 75

|
|
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.
|

31st May 2012, 11:00 AM
|
 |
Registered User
|
|
Join Date: Apr 2006
Location: Ohio, USA
Posts: 8,300

|
|
|
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.
|

31st May 2012, 11:38 AM
|
 |
"Shells" (of a sub world)
|
|
Join Date: May 2011
Location: Helvetic Federation (Swissh)
Age: 33
Posts: 2,602

|
|
|
Re: how to get a partition's uuid
lsblk
blkid
These 2 commands are most helpfull (to me at least) to find partition labels.
__________________
Fedora Manual: http://docs.fedoraproject.org
Script-Tools: https://sourceforge.net/projects/script-tools/
sudo st tweak repo toggle fedora-rawhide ; st iso dl-fed -respin && st iso usb
|

31st May 2012, 11:47 AM
|
|
Registered User
|
|
Join Date: Jul 2008
Location: Maastricht, the Netherlands
Age: 46
Posts: 65

|
|
|
Re: how to get a partition's uuid
cd /dev/disk/by-uuid
ls -la
|

31st May 2012, 06:38 PM
|
 |
Registered User
|
|
Join Date: Nov 2006
Location: Detroit
Posts: 4,617

|
|
|
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 18 x86_64 | CPU: AMD64 3700+ 2.2GHz | RAM: 2GB PC3200 DDR | Disk: 160GB PATA | Video: ATI Radeon 7500 AGP 64MB | Sound: Turtle Beach Santa Cruz CS4630 | Ethernet: Realtek 8110SC
Last edited by RupertPupkin; 1st June 2012 at 04:26 AM.
Reason: Added modified code
|

28th June 2012, 04:33 AM
|
|
Registered User
|
|
Join Date: Mar 2008
Location: China
Posts: 108

|
|
|
Re: how to get a partition's uuid
Join Date: Sep 2010
Location: China
Posts: 74
which city base on?
__________________
---manhh3---
Lenovo G475g
amd e300 apu + ram 3g
centos6 amd64
|

28th June 2012, 07:07 PM
|
|
Registered User
|
|
Join Date: Jan 2011
Location: Woonsocket, RI
Posts: 377

|
|
|
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.
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Current GMT-time: 04:46 (Wednesday, 22-05-2013)
|
|
 |
 |
 |
 |
|
|