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.
11 //kbuild:lib-$(CONFIG_BLKID) += get_devname.o
12 //kbuild:lib-$(CONFIG_FINDFS) += get_devname.o
13 //kbuild:lib-$(CONFIG_FEATURE_MOUNT_LABEL) += get_devname.o
14 //kbuild:lib-$(CONFIG_FEATURE_SWAPONOFF_LABEL) += get_devname.o
16 #include <sys/mount.h> /* BLKGETSIZE64 */
17 #if !defined(BLKGETSIZE64)
18 # define BLKGETSIZE64 _IOR(0x12,114,size_t)
20 #include "volume_id_internal.h"
22 static struct uuidCache_s {
23 struct uuidCache_s *next;
27 char *uc_uuid; /* prefix makes it easier to grep for */
28 IF_FEATURE_BLKID_TYPE(const char *type;)
31 #if !ENABLE_FEATURE_BLKID_TYPE
32 #define get_label_uuid(fd, label, uuid, type) \
33 get_label_uuid(fd, label, uuid)
34 #define uuidcache_addentry(device, label, uuid, type) \
35 uuidcache_addentry(device, label, uuid)
38 /* Returns !0 on error.
39 * Otherwise, returns malloc'ed strings for label and uuid
40 * (and they can't be NULL, although they can be "").
43 get_label_uuid(int fd, char **label, char **uuid, const char **type)
47 struct volume_id *vid;
49 /* fd is owned by vid now */
50 vid = volume_id_open_node(fd);
52 if (ioctl(/*vid->*/fd, BLKGETSIZE64, &size) != 0)
55 if (volume_id_probe_all(vid, /*0,*/ size) != 0)
58 if (vid->label[0] != '\0' || vid->uuid[0] != '\0'
59 #if ENABLE_FEATURE_BLKID_TYPE
63 *label = xstrndup(vid->label, sizeof(vid->label));
64 *uuid = xstrndup(vid->uuid, sizeof(vid->uuid));
65 #if ENABLE_FEATURE_BLKID_TYPE
67 dbg("found label '%s', uuid '%s', type '%s'", *label, *uuid, *type);
69 dbg("found label '%s', uuid '%s'", *label, *uuid);
74 free_volume_id(vid); /* also closes fd */
78 /* NB: we take ownership of (malloc'ed) label and uuid */
80 uuidcache_addentry(char *device, /*int major, int minor,*/ char *label, char *uuid, const char *type)
82 struct uuidCache_s *last;
85 last = uuidCache = xzalloc(sizeof(*uuidCache));
87 for (last = uuidCache; last->next; last = last->next)
89 last->next = xzalloc(sizeof(*uuidCache));
92 /*last->next = NULL; - xzalloc did it*/
93 // last->major = major;
94 // last->minor = minor;
95 last->device = device;
98 IF_FEATURE_BLKID_TYPE(last->type = type;)
101 /* If get_label_uuid() on device_name returns success,
102 * add a cache entry for this device.
103 * If device node does not exist, it will be temporarily created. */
105 uuidcache_check_device(const char *device,
106 struct stat *statbuf,
107 void *userData UNUSED_PARAM,
108 int depth UNUSED_PARAM)
110 /* note: this check rejects links to devices, among other nodes */
111 if (!S_ISBLK(statbuf->st_mode)
112 #if ENABLE_FEATURE_VOLUMEID_UBIFS
113 && !(S_ISCHR(statbuf->st_mode) && strncmp(bb_basename(device), "ubi", 3) == 0)
118 /* Users report that mucking with floppies (especially non-present
119 * ones) is significant PITA. This is a horribly dirty hack,
120 * but it is very useful in real world.
121 * If this will ever need to be enabled, consider using O_NONBLOCK.
123 if (major(statbuf->st_rdev) == 2)
126 add_to_uuid_cache(device);
131 static struct uuidCache_s*
132 uuidcache_init(int scan_devices)
134 dbg("DBG: uuidCache=%x, uuidCache");
138 /* We were scanning /proc/partitions
139 * and /proc/sys/dev/cdrom/info here.
140 * Missed volume managers. I see that "standard" blkid uses these:
141 * /dev/mapper/control
145 * This is unacceptably complex. Let's just scan /dev.
146 * (Maybe add scanning of /sys/block/XXX/dev for devices
147 * somehow not having their /dev/XXX entries created?) */
149 recursive_action("/dev", ACTION_RECURSE,
150 uuidcache_check_device, /* file_action */
151 NULL, /* dir_action */
163 get_spec_by_x(int n, const char *t, int *majorPtr, int *minorPtr)
165 struct uuidCache_s *uc;
167 uc = uuidcache_init(/*scan_devices:*/ 1);
171 if (strcmp(t, uc->uc_uuid) == 0) {
172 *majorPtr = uc->major;
173 *minorPtr = uc->minor;
178 if (strcmp(t, uc->label) == 0) {
179 *majorPtr = uc->major;
180 *minorPtr = uc->minor;
195 return ((c|0x20) - 'a' + 10);
199 get_spec_by_uuid(const char *s, int *major, int *minor)
201 unsigned char uuid[16];
204 if (strlen(s) != 36 || s[8] != '-' || s[13] != '-'
205 || s[18] != '-' || s[23] != '-'
209 for (i = 0; i < 16; i++) {
212 if (!isxdigit(s[0]) || !isxdigit(s[1]))
214 uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1]));
217 return get_spec_by_x(UUID, (char *)uuid, major, minor);
220 fprintf(stderr, _("mount: bad UUID"));
225 get_spec_by_volume_label(const char *s, int *major, int *minor)
227 return get_spec_by_x(VOL, s, major, minor);
232 void display_uuid_cache(int scan_devices)
234 struct uuidCache_s *uc;
236 uc = uuidcache_init(scan_devices);
238 printf("%s:", uc->device);
240 printf(" LABEL=\"%s\"", uc->label);
242 printf(" UUID=\"%s\"", uc->uc_uuid);
243 #if ENABLE_FEATURE_BLKID_TYPE
245 printf(" TYPE=\"%s\"", uc->type);
252 int add_to_uuid_cache(const char *device)
254 char *uuid = uuid; /* for compiler */
256 #if ENABLE_FEATURE_BLKID_TYPE
257 const char *type = type;
261 fd = open(device, O_RDONLY);
265 /* get_label_uuid() closes fd in all cases (success & failure) */
266 if (get_label_uuid(fd, &label, &uuid, &type) == 0) {
267 /* uuidcache_addentry() takes ownership of all four params */
268 uuidcache_addentry(xstrdup(device), /*ma, mi,*/ label, uuid, type);
275 /* Used by mount and findfs */
277 char *get_devname_from_label(const char *spec)
279 struct uuidCache_s *uc;
281 uc = uuidcache_init(/*scan_devices:*/ 1);
283 if (uc->label[0] && strcmp(spec, uc->label) == 0) {
284 return xstrdup(uc->device);
291 char *get_devname_from_uuid(const char *spec)
293 struct uuidCache_s *uc;
295 uc = uuidcache_init(/*scan_devices:*/ 1);
297 /* case of hex numbers doesn't matter */
298 if (strcasecmp(spec, uc->uc_uuid) == 0) {
299 return xstrdup(uc->device);
306 int resolve_mount_spec(char **fsname)
310 if (is_prefixed_with(*fsname, "UUID="))
311 tmp = get_devname_from_uuid(*fsname + 5);
312 else if (is_prefixed_with(*fsname, "LABEL="))
313 tmp = get_devname_from_label(*fsname + 6);
316 return 0; /* no UUID= or LABEL= prefix found */