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 tarball for details.
11 #include "volume_id_internal.h"
13 //#define BLKGETSIZE64 _IOR(0x12,114,size_t)
15 static struct uuidCache_s {
16 struct uuidCache_s *next;
20 char *uc_uuid; /* prefix makes it easier to grep for */
23 /* Returns !0 on error.
24 * Otherwise, returns malloc'ed strings for label and uuid
25 * (and they can't be NULL, although they can be "").
28 get_label_uuid(int fd, char **label, char **uuid)
32 struct volume_id *vid;
34 /* fd is owned by vid now */
35 vid = volume_id_open_node(fd);
37 if (ioctl(/*vid->*/fd, BLKGETSIZE64, &size) != 0)
40 if (volume_id_probe_all(vid, /*0,*/ size) != 0)
43 if (vid->label[0] != '\0' || vid->uuid[0] != '\0') {
44 *label = xstrndup(vid->label, sizeof(vid->label));
45 *uuid = xstrndup(vid->uuid, sizeof(vid->uuid));
46 dbg("found label '%s', uuid '%s' on %s", *label, *uuid, device);
50 free_volume_id(vid); /* also closes fd */
54 /* NB: we take ownership of (malloc'ed) label and uuid */
56 uuidcache_addentry(char *device, /*int major, int minor,*/ char *label, char *uuid)
58 struct uuidCache_s *last;
61 last = uuidCache = xzalloc(sizeof(*uuidCache));
63 for (last = uuidCache; last->next; last = last->next)
65 last->next = xzalloc(sizeof(*uuidCache));
68 /*last->next = NULL; - xzalloc did it*/
69 // last->major = major;
70 // last->minor = minor;
71 last->device = device;
76 /* If get_label_uuid() on device_name returns success,
77 * add a cache entry for this device.
78 * If device node does not exist, it will be temporarily created. */
80 uuidcache_check_device(const char *device,
82 void *userData UNUSED_PARAM,
83 int depth UNUSED_PARAM)
85 char *uuid = uuid; /* for compiler */
89 /* note: this check rejects links to devices, among other nodes */
90 if (!S_ISBLK(statbuf->st_mode))
93 /* Users report that mucking with floppies (especially non-present
94 * ones) is significant PITA. This is a horribly dirty hack,
95 * but it is very useful in real world.
96 * If this will ever need to be enabled, consider using O_NONBLOCK.
98 if (major(statbuf->st_rdev) == 2)
101 fd = open(device, O_RDONLY);
105 /* get_label_uuid() closes fd in all cases (success & failure) */
106 if (get_label_uuid(fd, &label, &uuid) == 0) {
107 /* uuidcache_addentry() takes ownership of all three params */
108 uuidcache_addentry(xstrdup(device), /*ma, mi,*/ label, uuid);
119 /* We were scanning /proc/partitions
120 * and /proc/sys/dev/cdrom/info here.
121 * Missed volume managers. I see that "standard" blkid uses these:
122 * /dev/mapper/control
126 * This is unacceptably complex. Let's just scan /dev.
127 * (Maybe add scanning of /sys/block/XXX/dev for devices
128 * somehow not having their /dev/XXX entries created?) */
130 recursive_action("/dev", ACTION_RECURSE,
131 uuidcache_check_device, /* file_action */
132 NULL, /* dir_action */
142 get_spec_by_x(int n, const char *t, int *majorPtr, int *minorPtr)
144 struct uuidCache_s *uc;
152 if (strcmp(t, uc->uc_uuid) == 0) {
153 *majorPtr = uc->major;
154 *minorPtr = uc->minor;
159 if (strcmp(t, uc->label) == 0) {
160 *majorPtr = uc->major;
161 *minorPtr = uc->minor;
176 return ((c|0x20) - 'a' + 10);
180 get_spec_by_uuid(const char *s, int *major, int *minor)
182 unsigned char uuid[16];
185 if (strlen(s) != 36 || s[8] != '-' || s[13] != '-'
186 || s[18] != '-' || s[23] != '-'
190 for (i = 0; i < 16; i++) {
193 if (!isxdigit(s[0]) || !isxdigit(s[1]))
195 uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1]));
198 return get_spec_by_x(UUID, (char *)uuid, major, minor);
201 fprintf(stderr, _("mount: bad UUID"));
206 get_spec_by_volume_label(const char *s, int *major, int *minor)
208 return get_spec_by_x(VOL, s, major, minor);
213 void display_uuid_cache(void)
215 struct uuidCache_s *u;
220 printf("%s:", u->device);
222 printf(" LABEL=\"%s\"", u->label);
224 printf(" UUID=\"%s\"", u->uc_uuid);
230 /* Used by mount and findfs */
232 char *get_devname_from_label(const char *spec)
234 struct uuidCache_s *uc;
239 if (uc->label[0] && strcmp(spec, uc->label) == 0) {
240 return xstrdup(uc->device);
247 char *get_devname_from_uuid(const char *spec)
249 struct uuidCache_s *uc;
254 /* case of hex numbers doesn't matter */
255 if (strcasecmp(spec, uc->uc_uuid) == 0) {
256 return xstrdup(uc->device);