blob: 0c6bdfddf7754bc9bb0c77732c12fdbf469f565e [file] [log] [blame]
Denis Vlasenkode7684a2008-02-18 21:08:49 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Support functions for mounting devices by label/uuid
4 *
5 * Copyright (C) 2006 by Jason Schoon <floydpink@gmail.com>
6 * Some portions cribbed from e2fsprogs, util-linux, dosfstools
7 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkode7684a2008-02-18 21:08:49 +00009 */
Sven-Göran Bergh3b458012013-07-31 15:45:20 +020010
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
Denys Vlasenkoda49f582009-07-08 02:58:38 +020015#include <sys/mount.h> /* BLKGETSIZE64 */
16#if !defined(BLKGETSIZE64)
17# define BLKGETSIZE64 _IOR(0x12,114,size_t)
18#endif
Denis Vlasenkode7684a2008-02-18 21:08:49 +000019#include "volume_id_internal.h"
20
Denis Vlasenkode7684a2008-02-18 21:08:49 +000021static struct uuidCache_s {
22 struct uuidCache_s *next;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000023// int major, minor;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000024 char *device;
25 char *label;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000026 char *uc_uuid; /* prefix makes it easier to grep for */
Denys Vlasenko90615a02010-12-30 00:40:11 +010027 IF_FEATURE_BLKID_TYPE(const char *type;)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000028} *uuidCache;
29
Denys Vlasenko90615a02010-12-30 00:40:11 +010030#if !ENABLE_FEATURE_BLKID_TYPE
31#define get_label_uuid(fd, label, uuid, type) \
32 get_label_uuid(fd, label, uuid)
33#define uuidcache_addentry(device, label, uuid, type) \
34 uuidcache_addentry(device, label, uuid)
35#endif
36
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000037/* Returns !0 on error.
38 * Otherwise, returns malloc'ed strings for label and uuid
Denis Vlasenko582dff02008-10-19 19:36:30 +000039 * (and they can't be NULL, although they can be "").
40 * NB: closes fd. */
Denis Vlasenkode7684a2008-02-18 21:08:49 +000041static int
Denys Vlasenko90615a02010-12-30 00:40:11 +010042get_label_uuid(int fd, char **label, char **uuid, const char **type)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000043{
44 int rv = 1;
45 uint64_t size;
46 struct volume_id *vid;
47
Denis Vlasenko582dff02008-10-19 19:36:30 +000048 /* fd is owned by vid now */
49 vid = volume_id_open_node(fd);
Denis Vlasenkode7684a2008-02-18 21:08:49 +000050
Denis Vlasenko582dff02008-10-19 19:36:30 +000051 if (ioctl(/*vid->*/fd, BLKGETSIZE64, &size) != 0)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000052 size = 0;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000053
Denis Vlasenko28ea4292009-02-15 05:51:19 +000054 if (volume_id_probe_all(vid, /*0,*/ size) != 0)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000055 goto ret;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000056
S-G Berghd2d50492012-11-05 13:16:07 +010057 if (vid->label[0] != '\0' || vid->uuid[0] != '\0'
58#if ENABLE_FEATURE_BLKID_TYPE
59 || vid->type != NULL
60#endif
61 ) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +000062 *label = xstrndup(vid->label, sizeof(vid->label));
63 *uuid = xstrndup(vid->uuid, sizeof(vid->uuid));
Denys Vlasenko90615a02010-12-30 00:40:11 +010064#if ENABLE_FEATURE_BLKID_TYPE
65 *type = vid->type;
66 dbg("found label '%s', uuid '%s', type '%s'", *label, *uuid, *type);
67#else
Vladimir Dronnikov662e8b72009-11-01 23:05:09 +010068 dbg("found label '%s', uuid '%s'", *label, *uuid);
Denys Vlasenko90615a02010-12-30 00:40:11 +010069#endif
Denis Vlasenkode7684a2008-02-18 21:08:49 +000070 rv = 0;
71 }
72 ret:
Denis Vlasenko582dff02008-10-19 19:36:30 +000073 free_volume_id(vid); /* also closes fd */
Denis Vlasenkode7684a2008-02-18 21:08:49 +000074 return rv;
75}
76
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000077/* NB: we take ownership of (malloc'ed) label and uuid */
Denis Vlasenkode7684a2008-02-18 21:08:49 +000078static void
Denys Vlasenko90615a02010-12-30 00:40:11 +010079uuidcache_addentry(char *device, /*int major, int minor,*/ char *label, char *uuid, const char *type)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000080{
81 struct uuidCache_s *last;
Denis Vlasenko42cc3042008-03-24 02:05:58 +000082
Denis Vlasenkode7684a2008-02-18 21:08:49 +000083 if (!uuidCache) {
84 last = uuidCache = xzalloc(sizeof(*uuidCache));
85 } else {
86 for (last = uuidCache; last->next; last = last->next)
87 continue;
88 last->next = xzalloc(sizeof(*uuidCache));
89 last = last->next;
90 }
91 /*last->next = NULL; - xzalloc did it*/
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000092// last->major = major;
93// last->minor = minor;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000094 last->device = device;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000095 last->label = label;
96 last->uc_uuid = uuid;
Denys Vlasenko90615a02010-12-30 00:40:11 +010097 IF_FEATURE_BLKID_TYPE(last->type = type;)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000098}
99
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000100/* If get_label_uuid() on device_name returns success,
101 * add a cache entry for this device.
102 * If device node does not exist, it will be temporarily created. */
Denis Vlasenko582dff02008-10-19 19:36:30 +0000103static int FAST_FUNC
104uuidcache_check_device(const char *device,
105 struct stat *statbuf,
106 void *userData UNUSED_PARAM,
107 int depth UNUSED_PARAM)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000108{
Denys Vlasenko4fc5ec52009-06-27 21:58:25 +0200109 /* note: this check rejects links to devices, among other nodes */
Denis Vlasenko582dff02008-10-19 19:36:30 +0000110 if (!S_ISBLK(statbuf->st_mode))
111 return TRUE;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000112
Denys Vlasenko4fc5ec52009-06-27 21:58:25 +0200113 /* Users report that mucking with floppies (especially non-present
114 * ones) is significant PITA. This is a horribly dirty hack,
Denys Vlasenko1881ba42009-06-27 22:09:28 +0200115 * but it is very useful in real world.
116 * If this will ever need to be enabled, consider using O_NONBLOCK.
117 */
Denys Vlasenko4fc5ec52009-06-27 21:58:25 +0200118 if (major(statbuf->st_rdev) == 2)
119 return TRUE;
120
Denys Vlasenko90615a02010-12-30 00:40:11 +0100121 add_to_uuid_cache(device);
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000122
Denis Vlasenko582dff02008-10-19 19:36:30 +0000123 return TRUE;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000124}
125
Denys Vlasenkoc3375f02011-12-06 15:06:59 +0100126static struct uuidCache_s*
Denys Vlasenkoe8cfc3f2012-03-03 15:09:07 +0100127uuidcache_init(int scan_devices)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000128{
Denys Vlasenko90615a02010-12-30 00:40:11 +0100129 dbg("DBG: uuidCache=%x, uuidCache");
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000130 if (uuidCache)
Denys Vlasenkoc3375f02011-12-06 15:06:59 +0100131 return uuidCache;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000132
Denis Vlasenko802a7be2008-10-19 19:54:49 +0000133 /* We were scanning /proc/partitions
134 * and /proc/sys/dev/cdrom/info here.
135 * Missed volume managers. I see that "standard" blkid uses these:
136 * /dev/mapper/control
137 * /proc/devices
138 * /proc/evms/volumes
139 * /proc/lvm/VGs
140 * This is unacceptably complex. Let's just scan /dev.
141 * (Maybe add scanning of /sys/block/XXX/dev for devices
142 * somehow not having their /dev/XXX entries created?) */
Denys Vlasenkoe8cfc3f2012-03-03 15:09:07 +0100143 if (scan_devices)
144 recursive_action("/dev", ACTION_RECURSE,
145 uuidcache_check_device, /* file_action */
146 NULL, /* dir_action */
147 NULL, /* userData */
148 0 /* depth */);
Denys Vlasenkoc3375f02011-12-06 15:06:59 +0100149
150 return uuidCache;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000151}
152
153#define UUID 1
154#define VOL 2
155
156#ifdef UNUSED
157static char *
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000158get_spec_by_x(int n, const char *t, int *majorPtr, int *minorPtr)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000159{
160 struct uuidCache_s *uc;
161
Denys Vlasenkoe8cfc3f2012-03-03 15:09:07 +0100162 uc = uuidcache_init(/*scan_devices:*/ 1);
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000163 while (uc) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000164 switch (n) {
165 case UUID:
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000166 if (strcmp(t, uc->uc_uuid) == 0) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000167 *majorPtr = uc->major;
168 *minorPtr = uc->minor;
169 return uc->device;
170 }
171 break;
172 case VOL:
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000173 if (strcmp(t, uc->label) == 0) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000174 *majorPtr = uc->major;
175 *minorPtr = uc->minor;
176 return uc->device;
177 }
178 break;
179 }
180 uc = uc->next;
181 }
182 return NULL;
183}
184
185static unsigned char
186fromhex(char c)
187{
188 if (isdigit(c))
189 return (c - '0');
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000190 return ((c|0x20) - 'a' + 10);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000191}
192
193static char *
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000194get_spec_by_uuid(const char *s, int *major, int *minor)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000195{
196 unsigned char uuid[16];
197 int i;
198
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000199 if (strlen(s) != 36 || s[8] != '-' || s[13] != '-'
200 || s[18] != '-' || s[23] != '-'
201 ) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000202 goto bad_uuid;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000203 }
204 for (i = 0; i < 16; i++) {
205 if (*s == '-')
206 s++;
207 if (!isxdigit(s[0]) || !isxdigit(s[1]))
208 goto bad_uuid;
209 uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1]));
210 s += 2;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000211 }
212 return get_spec_by_x(UUID, (char *)uuid, major, minor);
213
214 bad_uuid:
215 fprintf(stderr, _("mount: bad UUID"));
216 return 0;
217}
218
219static char *
220get_spec_by_volume_label(const char *s, int *major, int *minor)
221{
222 return get_spec_by_x(VOL, s, major, minor);
223}
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000224#endif // UNUSED
225
Denis Vlasenko1e19afd2008-10-12 11:20:08 +0000226/* Used by blkid */
Denys Vlasenkoe8cfc3f2012-03-03 15:09:07 +0100227void display_uuid_cache(int scan_devices)
Denis Vlasenko1e19afd2008-10-12 11:20:08 +0000228{
Denys Vlasenkoc3375f02011-12-06 15:06:59 +0100229 struct uuidCache_s *uc;
Denis Vlasenko1e19afd2008-10-12 11:20:08 +0000230
Denys Vlasenkoe8cfc3f2012-03-03 15:09:07 +0100231 uc = uuidcache_init(scan_devices);
Denys Vlasenkoc3375f02011-12-06 15:06:59 +0100232 while (uc) {
233 printf("%s:", uc->device);
234 if (uc->label[0])
235 printf(" LABEL=\"%s\"", uc->label);
236 if (uc->uc_uuid[0])
237 printf(" UUID=\"%s\"", uc->uc_uuid);
Denys Vlasenko90615a02010-12-30 00:40:11 +0100238#if ENABLE_FEATURE_BLKID_TYPE
Denys Vlasenkoc3375f02011-12-06 15:06:59 +0100239 if (uc->type)
240 printf(" TYPE=\"%s\"", uc->type);
Denys Vlasenko90615a02010-12-30 00:40:11 +0100241#endif
Denis Vlasenko1e19afd2008-10-12 11:20:08 +0000242 bb_putchar('\n');
Denys Vlasenkoc3375f02011-12-06 15:06:59 +0100243 uc = uc->next;
Denis Vlasenko1e19afd2008-10-12 11:20:08 +0000244 }
245}
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000246
Denys Vlasenko90615a02010-12-30 00:40:11 +0100247int add_to_uuid_cache(const char *device)
248{
249 char *uuid = uuid; /* for compiler */
250 char *label = label;
251#if ENABLE_FEATURE_BLKID_TYPE
252 const char *type = type;
253#endif
254 int fd;
255
256 fd = open(device, O_RDONLY);
257 if (fd < 0)
258 return 0;
259
260 /* get_label_uuid() closes fd in all cases (success & failure) */
261 if (get_label_uuid(fd, &label, &uuid, &type) == 0) {
262 /* uuidcache_addentry() takes ownership of all four params */
263 uuidcache_addentry(xstrdup(device), /*ma, mi,*/ label, uuid, type);
264 return 1;
265 }
266 return 0;
267}
268
269
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000270/* Used by mount and findfs */
271
272char *get_devname_from_label(const char *spec)
273{
274 struct uuidCache_s *uc;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000275
Denys Vlasenkoe8cfc3f2012-03-03 15:09:07 +0100276 uc = uuidcache_init(/*scan_devices:*/ 1);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000277 while (uc) {
Denis Vlasenko9983d802009-03-31 19:47:34 +0000278 if (uc->label[0] && strcmp(spec, uc->label) == 0) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000279 return xstrdup(uc->device);
280 }
281 uc = uc->next;
282 }
283 return NULL;
284}
285
286char *get_devname_from_uuid(const char *spec)
287{
288 struct uuidCache_s *uc;
289
Denys Vlasenkoe8cfc3f2012-03-03 15:09:07 +0100290 uc = uuidcache_init(/*scan_devices:*/ 1);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000291 while (uc) {
Denis Vlasenkod25c33f2008-03-17 09:25:05 +0000292 /* case of hex numbers doesn't matter */
293 if (strcasecmp(spec, uc->uc_uuid) == 0) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000294 return xstrdup(uc->device);
295 }
296 uc = uc->next;
297 }
298 return NULL;
299}
Natanael Copa9aff2992009-09-20 04:28:22 +0200300
301int resolve_mount_spec(char **fsname)
302{
303 char *tmp = *fsname;
304
305 if (strncmp(*fsname, "UUID=", 5) == 0)
306 tmp = get_devname_from_uuid(*fsname + 5);
307 else if (strncmp(*fsname, "LABEL=", 6) == 0)
308 tmp = get_devname_from_label(*fsname + 6);
309
310 if (tmp == *fsname)
311 return 0; /* no UUID= or LABEL= prefix found */
312
313 if (tmp)
314 *fsname = tmp;
315 return 1;
316}