blob: 230102d892d5dd5800279daac382e50b33bc517f [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 */
Denys Vlasenkoda49f582009-07-08 02:58:38 +020010#include <sys/mount.h> /* BLKGETSIZE64 */
11#if !defined(BLKGETSIZE64)
12# define BLKGETSIZE64 _IOR(0x12,114,size_t)
13#endif
Denis Vlasenkode7684a2008-02-18 21:08:49 +000014#include "volume_id_internal.h"
15
Denis Vlasenkode7684a2008-02-18 21:08:49 +000016static struct uuidCache_s {
17 struct uuidCache_s *next;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000018// int major, minor;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000019 char *device;
20 char *label;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000021 char *uc_uuid; /* prefix makes it easier to grep for */
Denys Vlasenko90615a02010-12-30 00:40:11 +010022 IF_FEATURE_BLKID_TYPE(const char *type;)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000023} *uuidCache;
24
Denys Vlasenko90615a02010-12-30 00:40:11 +010025#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)
30#endif
31
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000032/* Returns !0 on error.
33 * Otherwise, returns malloc'ed strings for label and uuid
Denis Vlasenko582dff02008-10-19 19:36:30 +000034 * (and they can't be NULL, although they can be "").
35 * NB: closes fd. */
Denis Vlasenkode7684a2008-02-18 21:08:49 +000036static int
Denys Vlasenko90615a02010-12-30 00:40:11 +010037get_label_uuid(int fd, char **label, char **uuid, const char **type)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000038{
39 int rv = 1;
40 uint64_t size;
41 struct volume_id *vid;
42
Denis Vlasenko582dff02008-10-19 19:36:30 +000043 /* fd is owned by vid now */
44 vid = volume_id_open_node(fd);
Denis Vlasenkode7684a2008-02-18 21:08:49 +000045
Denis Vlasenko582dff02008-10-19 19:36:30 +000046 if (ioctl(/*vid->*/fd, BLKGETSIZE64, &size) != 0)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000047 size = 0;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000048
Denis Vlasenko28ea4292009-02-15 05:51:19 +000049 if (volume_id_probe_all(vid, /*0,*/ size) != 0)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000050 goto ret;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000051
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000052 if (vid->label[0] != '\0' || vid->uuid[0] != '\0') {
Denis Vlasenkode7684a2008-02-18 21:08:49 +000053 *label = xstrndup(vid->label, sizeof(vid->label));
54 *uuid = xstrndup(vid->uuid, sizeof(vid->uuid));
Denys Vlasenko90615a02010-12-30 00:40:11 +010055#if ENABLE_FEATURE_BLKID_TYPE
56 *type = vid->type;
57 dbg("found label '%s', uuid '%s', type '%s'", *label, *uuid, *type);
58#else
Vladimir Dronnikov662e8b72009-11-01 23:05:09 +010059 dbg("found label '%s', uuid '%s'", *label, *uuid);
Denys Vlasenko90615a02010-12-30 00:40:11 +010060#endif
Denis Vlasenkode7684a2008-02-18 21:08:49 +000061 rv = 0;
62 }
63 ret:
Denis Vlasenko582dff02008-10-19 19:36:30 +000064 free_volume_id(vid); /* also closes fd */
Denis Vlasenkode7684a2008-02-18 21:08:49 +000065 return rv;
66}
67
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000068/* NB: we take ownership of (malloc'ed) label and uuid */
Denis Vlasenkode7684a2008-02-18 21:08:49 +000069static void
Denys Vlasenko90615a02010-12-30 00:40:11 +010070uuidcache_addentry(char *device, /*int major, int minor,*/ char *label, char *uuid, const char *type)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000071{
72 struct uuidCache_s *last;
Denis Vlasenko42cc3042008-03-24 02:05:58 +000073
Denis Vlasenkode7684a2008-02-18 21:08:49 +000074 if (!uuidCache) {
75 last = uuidCache = xzalloc(sizeof(*uuidCache));
76 } else {
77 for (last = uuidCache; last->next; last = last->next)
78 continue;
79 last->next = xzalloc(sizeof(*uuidCache));
80 last = last->next;
81 }
82 /*last->next = NULL; - xzalloc did it*/
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000083// last->major = major;
84// last->minor = minor;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000085 last->device = device;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000086 last->label = label;
87 last->uc_uuid = uuid;
Denys Vlasenko90615a02010-12-30 00:40:11 +010088 IF_FEATURE_BLKID_TYPE(last->type = type;)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000089}
90
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000091/* 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. */
Denis Vlasenko582dff02008-10-19 19:36:30 +000094static int FAST_FUNC
95uuidcache_check_device(const char *device,
96 struct stat *statbuf,
97 void *userData UNUSED_PARAM,
98 int depth UNUSED_PARAM)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000099{
Denys Vlasenko4fc5ec52009-06-27 21:58:25 +0200100 /* note: this check rejects links to devices, among other nodes */
Denis Vlasenko582dff02008-10-19 19:36:30 +0000101 if (!S_ISBLK(statbuf->st_mode))
102 return TRUE;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000103
Denys Vlasenko4fc5ec52009-06-27 21:58:25 +0200104 /* Users report that mucking with floppies (especially non-present
105 * ones) is significant PITA. This is a horribly dirty hack,
Denys Vlasenko1881ba42009-06-27 22:09:28 +0200106 * but it is very useful in real world.
107 * If this will ever need to be enabled, consider using O_NONBLOCK.
108 */
Denys Vlasenko4fc5ec52009-06-27 21:58:25 +0200109 if (major(statbuf->st_rdev) == 2)
110 return TRUE;
111
Denys Vlasenko90615a02010-12-30 00:40:11 +0100112 add_to_uuid_cache(device);
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000113
Denis Vlasenko582dff02008-10-19 19:36:30 +0000114 return TRUE;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000115}
116
Denys Vlasenkoc3375f02011-12-06 15:06:59 +0100117static struct uuidCache_s*
Denys Vlasenkoe8cfc3f2012-03-03 15:09:07 +0100118uuidcache_init(int scan_devices)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000119{
Denys Vlasenko90615a02010-12-30 00:40:11 +0100120 dbg("DBG: uuidCache=%x, uuidCache");
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000121 if (uuidCache)
Denys Vlasenkoc3375f02011-12-06 15:06:59 +0100122 return uuidCache;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000123
Denis Vlasenko802a7be2008-10-19 19:54:49 +0000124 /* 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
128 * /proc/devices
129 * /proc/evms/volumes
130 * /proc/lvm/VGs
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?) */
Denys Vlasenkoe8cfc3f2012-03-03 15:09:07 +0100134 if (scan_devices)
135 recursive_action("/dev", ACTION_RECURSE,
136 uuidcache_check_device, /* file_action */
137 NULL, /* dir_action */
138 NULL, /* userData */
139 0 /* depth */);
Denys Vlasenkoc3375f02011-12-06 15:06:59 +0100140
141 return uuidCache;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000142}
143
144#define UUID 1
145#define VOL 2
146
147#ifdef UNUSED
148static char *
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000149get_spec_by_x(int n, const char *t, int *majorPtr, int *minorPtr)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000150{
151 struct uuidCache_s *uc;
152
Denys Vlasenkoe8cfc3f2012-03-03 15:09:07 +0100153 uc = uuidcache_init(/*scan_devices:*/ 1);
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000154 while (uc) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000155 switch (n) {
156 case UUID:
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000157 if (strcmp(t, uc->uc_uuid) == 0) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000158 *majorPtr = uc->major;
159 *minorPtr = uc->minor;
160 return uc->device;
161 }
162 break;
163 case VOL:
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000164 if (strcmp(t, uc->label) == 0) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000165 *majorPtr = uc->major;
166 *minorPtr = uc->minor;
167 return uc->device;
168 }
169 break;
170 }
171 uc = uc->next;
172 }
173 return NULL;
174}
175
176static unsigned char
177fromhex(char c)
178{
179 if (isdigit(c))
180 return (c - '0');
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000181 return ((c|0x20) - 'a' + 10);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000182}
183
184static char *
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000185get_spec_by_uuid(const char *s, int *major, int *minor)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000186{
187 unsigned char uuid[16];
188 int i;
189
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000190 if (strlen(s) != 36 || s[8] != '-' || s[13] != '-'
191 || s[18] != '-' || s[23] != '-'
192 ) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000193 goto bad_uuid;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000194 }
195 for (i = 0; i < 16; i++) {
196 if (*s == '-')
197 s++;
198 if (!isxdigit(s[0]) || !isxdigit(s[1]))
199 goto bad_uuid;
200 uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1]));
201 s += 2;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000202 }
203 return get_spec_by_x(UUID, (char *)uuid, major, minor);
204
205 bad_uuid:
206 fprintf(stderr, _("mount: bad UUID"));
207 return 0;
208}
209
210static char *
211get_spec_by_volume_label(const char *s, int *major, int *minor)
212{
213 return get_spec_by_x(VOL, s, major, minor);
214}
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000215#endif // UNUSED
216
Denis Vlasenko1e19afd2008-10-12 11:20:08 +0000217/* Used by blkid */
Denys Vlasenkoe8cfc3f2012-03-03 15:09:07 +0100218void display_uuid_cache(int scan_devices)
Denis Vlasenko1e19afd2008-10-12 11:20:08 +0000219{
Denys Vlasenkoc3375f02011-12-06 15:06:59 +0100220 struct uuidCache_s *uc;
Denis Vlasenko1e19afd2008-10-12 11:20:08 +0000221
Denys Vlasenkoe8cfc3f2012-03-03 15:09:07 +0100222 uc = uuidcache_init(scan_devices);
Denys Vlasenkoc3375f02011-12-06 15:06:59 +0100223 while (uc) {
224 printf("%s:", uc->device);
225 if (uc->label[0])
226 printf(" LABEL=\"%s\"", uc->label);
227 if (uc->uc_uuid[0])
228 printf(" UUID=\"%s\"", uc->uc_uuid);
Denys Vlasenko90615a02010-12-30 00:40:11 +0100229#if ENABLE_FEATURE_BLKID_TYPE
Denys Vlasenkoc3375f02011-12-06 15:06:59 +0100230 if (uc->type)
231 printf(" TYPE=\"%s\"", uc->type);
Denys Vlasenko90615a02010-12-30 00:40:11 +0100232#endif
Denis Vlasenko1e19afd2008-10-12 11:20:08 +0000233 bb_putchar('\n');
Denys Vlasenkoc3375f02011-12-06 15:06:59 +0100234 uc = uc->next;
Denis Vlasenko1e19afd2008-10-12 11:20:08 +0000235 }
236}
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000237
Denys Vlasenko90615a02010-12-30 00:40:11 +0100238int add_to_uuid_cache(const char *device)
239{
240 char *uuid = uuid; /* for compiler */
241 char *label = label;
242#if ENABLE_FEATURE_BLKID_TYPE
243 const char *type = type;
244#endif
245 int fd;
246
247 fd = open(device, O_RDONLY);
248 if (fd < 0)
249 return 0;
250
251 /* get_label_uuid() closes fd in all cases (success & failure) */
252 if (get_label_uuid(fd, &label, &uuid, &type) == 0) {
253 /* uuidcache_addentry() takes ownership of all four params */
254 uuidcache_addentry(xstrdup(device), /*ma, mi,*/ label, uuid, type);
255 return 1;
256 }
257 return 0;
258}
259
260
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000261/* Used by mount and findfs */
262
263char *get_devname_from_label(const char *spec)
264{
265 struct uuidCache_s *uc;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000266
Denys Vlasenkoe8cfc3f2012-03-03 15:09:07 +0100267 uc = uuidcache_init(/*scan_devices:*/ 1);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000268 while (uc) {
Denis Vlasenko9983d802009-03-31 19:47:34 +0000269 if (uc->label[0] && strcmp(spec, uc->label) == 0) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000270 return xstrdup(uc->device);
271 }
272 uc = uc->next;
273 }
274 return NULL;
275}
276
277char *get_devname_from_uuid(const char *spec)
278{
279 struct uuidCache_s *uc;
280
Denys Vlasenkoe8cfc3f2012-03-03 15:09:07 +0100281 uc = uuidcache_init(/*scan_devices:*/ 1);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000282 while (uc) {
Denis Vlasenkod25c33f2008-03-17 09:25:05 +0000283 /* case of hex numbers doesn't matter */
284 if (strcasecmp(spec, uc->uc_uuid) == 0) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000285 return xstrdup(uc->device);
286 }
287 uc = uc->next;
288 }
289 return NULL;
290}
Natanael Copa9aff2992009-09-20 04:28:22 +0200291
292int resolve_mount_spec(char **fsname)
293{
294 char *tmp = *fsname;
295
296 if (strncmp(*fsname, "UUID=", 5) == 0)
297 tmp = get_devname_from_uuid(*fsname + 5);
298 else if (strncmp(*fsname, "LABEL=", 6) == 0)
299 tmp = get_devname_from_label(*fsname + 6);
300
301 if (tmp == *fsname)
302 return 0; /* no UUID= or LABEL= prefix found */
303
304 if (tmp)
305 *fsname = tmp;
306 return 1;
307}