1 /* vi: set sw=4 ts=4: */
3 * Support functions for mounting devices by label/uuid
5 * Copyright (C) 2006 by Jason Schoon <floydpink@gmail.com>
6 * Some portions cribbed from e2fsprogs, util-linux, dosfstools
8 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10 #include <sys/mount.h> /* BLKGETSIZE64 */
11 #if !defined(BLKGETSIZE64)
12 # define BLKGETSIZE64 _IOR(0x12,114,size_t)
14 #include "volume_id_internal.h"
16 static struct uuidCache_s {
17 struct uuidCache_s *next;
21 char *uc_uuid; /* prefix makes it easier to grep for */
22 IF_FEATURE_BLKID_TYPE(const char *type;)
25 #if !ENABLE_FEATURE_BLKID_TYPE
26 #define get_label_uuid(fd, label, uuid, type) \
27 get_label_uuid(fd, label, uuid)
28 #define uuidcache_addentry(device, label, uuid, type) \
29 uuidcache_addentry(device, label, uuid)
32 /* Returns !0 on error.
33 * Otherwise, returns malloc'ed strings for label and uuid
34 * (and they can't be NULL, although they can be "").
37 get_label_uuid(int fd, char **label, char **uuid, const char **type)
41 struct volume_id *vid;
43 /* fd is owned by vid now */
44 vid = volume_id_open_node(fd);
46 if (ioctl(/*vid->*/fd, BLKGETSIZE64, &size) != 0)
49 if (volume_id_probe_all(vid, /*0,*/ size) != 0)
52 if (vid->label[0] != '\0' || vid->uuid[0] != '\0') {
53 *label = xstrndup(vid->label, sizeof(vid->label));
54 *uuid = xstrndup(vid->uuid, sizeof(vid->uuid));
55 #if ENABLE_FEATURE_BLKID_TYPE
57 dbg("found label '%s', uuid '%s', type '%s'", *label, *uuid, *type);
59 dbg("found label '%s', uuid '%s'", *label, *uuid);
64 free_volume_id(vid); /* also closes fd */
68 /* NB: we take ownership of (malloc'ed) label and uuid */
70 uuidcache_addentry(char *device, /*int major, int minor,*/ char *label, char *uuid, const char *type)
72 struct uuidCache_s *last;
75 last = uuidCache = xzalloc(sizeof(*uuidCache));
77 for (last = uuidCache; last->next; last = last->next)
79 last->next = xzalloc(sizeof(*uuidCache));
82 /*last->next = NULL; - xzalloc did it*/
83 // last->major = major;
84 // last->minor = minor;
85 last->device = device;
88 IF_FEATURE_BLKID_TYPE(last->type = type;)
91 /* If get_label_uuid() on device_name returns success,
92 * add a cache entry for this device.
93 * If device node does not exist, it will be temporarily created. */
95 uuidcache_check_device(const char *device,
97 void *userData UNUSED_PARAM,
98 int depth UNUSED_PARAM)
100 /* note: this check rejects links to devices, among other nodes */
101 if (!S_ISBLK(statbuf->st_mode))
104 /* Users report that mucking with floppies (especially non-present
105 * ones) is significant PITA. This is a horribly dirty hack,
106 * but it is very useful in real world.
107 * If this will ever need to be enabled, consider using O_NONBLOCK.
109 if (major(statbuf->st_rdev) == 2)
112 add_to_uuid_cache(device);
120 dbg("DBG: uuidCache=%x, uuidCache");
124 /* We were scanning /proc/partitions
125 * and /proc/sys/dev/cdrom/info here.
126 * Missed volume managers. I see that "standard" blkid uses these:
127 * /dev/mapper/control
131 * This is unacceptably complex. Let's just scan /dev.
132 * (Maybe add scanning of /sys/block/XXX/dev for devices
133 * somehow not having their /dev/XXX entries created?) */
135 recursive_action("/dev", ACTION_RECURSE,
136 uuidcache_check_device, /* file_action */
137 NULL, /* dir_action */
147 get_spec_by_x(int n, const char *t, int *majorPtr, int *minorPtr)
149 struct uuidCache_s *uc;
157 if (strcmp(t, uc->uc_uuid) == 0) {
158 *majorPtr = uc->major;
159 *minorPtr = uc->minor;
164 if (strcmp(t, uc->label) == 0) {
165 *majorPtr = uc->major;
166 *minorPtr = uc->minor;
181 return ((c|0x20) - 'a' + 10);
185 get_spec_by_uuid(const char *s, int *major, int *minor)
187 unsigned char uuid[16];
190 if (strlen(s) != 36 || s[8] != '-' || s[13] != '-'
191 || s[18] != '-' || s[23] != '-'
195 for (i = 0; i < 16; i++) {
198 if (!isxdigit(s[0]) || !isxdigit(s[1]))
200 uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1]));
203 return get_spec_by_x(UUID, (char *)uuid, major, minor);
206 fprintf(stderr, _("mount: bad UUID"));
211 get_spec_by_volume_label(const char *s, int *major, int *minor)
213 return get_spec_by_x(VOL, s, major, minor);
218 void display_uuid_cache(void)
220 struct uuidCache_s *u;
225 printf("%s:", u->device);
227 printf(" LABEL=\"%s\"", u->label);
229 printf(" UUID=\"%s\"", u->uc_uuid);
230 #if ENABLE_FEATURE_BLKID_TYPE
232 printf(" TYPE=\"%s\"", u->type);
239 int add_to_uuid_cache(const char *device)
241 char *uuid = uuid; /* for compiler */
243 #if ENABLE_FEATURE_BLKID_TYPE
244 const char *type = type;
248 fd = open(device, O_RDONLY);
252 /* get_label_uuid() closes fd in all cases (success & failure) */
253 if (get_label_uuid(fd, &label, &uuid, &type) == 0) {
254 /* uuidcache_addentry() takes ownership of all four params */
255 uuidcache_addentry(xstrdup(device), /*ma, mi,*/ label, uuid, type);
262 /* Used by mount and findfs */
264 char *get_devname_from_label(const char *spec)
266 struct uuidCache_s *uc;
271 if (uc->label[0] && strcmp(spec, uc->label) == 0) {
272 return xstrdup(uc->device);
279 char *get_devname_from_uuid(const char *spec)
281 struct uuidCache_s *uc;
286 /* case of hex numbers doesn't matter */
287 if (strcasecmp(spec, uc->uc_uuid) == 0) {
288 return xstrdup(uc->device);
295 int resolve_mount_spec(char **fsname)
299 if (strncmp(*fsname, "UUID=", 5) == 0)
300 tmp = get_devname_from_uuid(*fsname + 5);
301 else if (strncmp(*fsname, "LABEL=", 6) == 0)
302 tmp = get_devname_from_label(*fsname + 6);
305 return 0; /* no UUID= or LABEL= prefix found */