blob: 4d9f9ecd0106e7395bee08cb58bf18988ce20d53 [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 *
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9 */
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 */
Denis Vlasenkode7684a2008-02-18 21:08:49 +000022} *uuidCache;
23
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000024/* Returns !0 on error.
25 * Otherwise, returns malloc'ed strings for label and uuid
Denis Vlasenko582dff02008-10-19 19:36:30 +000026 * (and they can't be NULL, although they can be "").
27 * NB: closes fd. */
Denis Vlasenkode7684a2008-02-18 21:08:49 +000028static int
Denis Vlasenko582dff02008-10-19 19:36:30 +000029get_label_uuid(int fd, char **label, char **uuid)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000030{
31 int rv = 1;
32 uint64_t size;
33 struct volume_id *vid;
34
Denis Vlasenko582dff02008-10-19 19:36:30 +000035 /* fd is owned by vid now */
36 vid = volume_id_open_node(fd);
Denis Vlasenkode7684a2008-02-18 21:08:49 +000037
Denis Vlasenko582dff02008-10-19 19:36:30 +000038 if (ioctl(/*vid->*/fd, BLKGETSIZE64, &size) != 0)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000039 size = 0;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000040
Denis Vlasenko28ea4292009-02-15 05:51:19 +000041 if (volume_id_probe_all(vid, /*0,*/ size) != 0)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000042 goto ret;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000043
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000044 if (vid->label[0] != '\0' || vid->uuid[0] != '\0') {
Denis Vlasenkode7684a2008-02-18 21:08:49 +000045 *label = xstrndup(vid->label, sizeof(vid->label));
46 *uuid = xstrndup(vid->uuid, sizeof(vid->uuid));
Vladimir Dronnikov662e8b72009-11-01 23:05:09 +010047 dbg("found label '%s', uuid '%s'", *label, *uuid);
Denis Vlasenkode7684a2008-02-18 21:08:49 +000048 rv = 0;
49 }
50 ret:
Denis Vlasenko582dff02008-10-19 19:36:30 +000051 free_volume_id(vid); /* also closes fd */
Denis Vlasenkode7684a2008-02-18 21:08:49 +000052 return rv;
53}
54
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000055/* NB: we take ownership of (malloc'ed) label and uuid */
Denis Vlasenkode7684a2008-02-18 21:08:49 +000056static void
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000057uuidcache_addentry(char *device, /*int major, int minor,*/ char *label, char *uuid)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000058{
59 struct uuidCache_s *last;
Denis Vlasenko42cc3042008-03-24 02:05:58 +000060
Denis Vlasenkode7684a2008-02-18 21:08:49 +000061 if (!uuidCache) {
62 last = uuidCache = xzalloc(sizeof(*uuidCache));
63 } else {
64 for (last = uuidCache; last->next; last = last->next)
65 continue;
66 last->next = xzalloc(sizeof(*uuidCache));
67 last = last->next;
68 }
69 /*last->next = NULL; - xzalloc did it*/
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000070// last->major = major;
71// last->minor = minor;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000072 last->device = device;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000073 last->label = label;
74 last->uc_uuid = uuid;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000075}
76
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000077/* If get_label_uuid() on device_name returns success,
78 * add a cache entry for this device.
79 * If device node does not exist, it will be temporarily created. */
Denis Vlasenko582dff02008-10-19 19:36:30 +000080static int FAST_FUNC
81uuidcache_check_device(const char *device,
82 struct stat *statbuf,
83 void *userData UNUSED_PARAM,
84 int depth UNUSED_PARAM)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000085{
Denis Vlasenko582dff02008-10-19 19:36:30 +000086 char *uuid = uuid; /* for compiler */
87 char *label = label;
88 int fd;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000089
Denys Vlasenko4fc5ec52009-06-27 21:58:25 +020090 /* note: this check rejects links to devices, among other nodes */
Denis Vlasenko582dff02008-10-19 19:36:30 +000091 if (!S_ISBLK(statbuf->st_mode))
92 return TRUE;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000093
Denys Vlasenko4fc5ec52009-06-27 21:58:25 +020094 /* Users report that mucking with floppies (especially non-present
95 * ones) is significant PITA. This is a horribly dirty hack,
Denys Vlasenko1881ba42009-06-27 22:09:28 +020096 * but it is very useful in real world.
97 * If this will ever need to be enabled, consider using O_NONBLOCK.
98 */
Denys Vlasenko4fc5ec52009-06-27 21:58:25 +020099 if (major(statbuf->st_rdev) == 2)
100 return TRUE;
101
Denis Vlasenko582dff02008-10-19 19:36:30 +0000102 fd = open(device, O_RDONLY);
103 if (fd < 0)
104 return TRUE;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000105
Denis Vlasenko582dff02008-10-19 19:36:30 +0000106 /* get_label_uuid() closes fd in all cases (success & failure) */
107 if (get_label_uuid(fd, &label, &uuid) == 0) {
108 /* uuidcache_addentry() takes ownership of all three params */
109 uuidcache_addentry(xstrdup(device), /*ma, mi,*/ label, uuid);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000110 }
Denis Vlasenko582dff02008-10-19 19:36:30 +0000111 return TRUE;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000112}
113
114static void
115uuidcache_init(void)
116{
117 if (uuidCache)
118 return;
119
Denis Vlasenko802a7be2008-10-19 19:54:49 +0000120 /* We were scanning /proc/partitions
121 * and /proc/sys/dev/cdrom/info here.
122 * Missed volume managers. I see that "standard" blkid uses these:
123 * /dev/mapper/control
124 * /proc/devices
125 * /proc/evms/volumes
126 * /proc/lvm/VGs
127 * This is unacceptably complex. Let's just scan /dev.
128 * (Maybe add scanning of /sys/block/XXX/dev for devices
129 * somehow not having their /dev/XXX entries created?) */
130
Denis Vlasenko582dff02008-10-19 19:36:30 +0000131 recursive_action("/dev", ACTION_RECURSE,
132 uuidcache_check_device, /* file_action */
133 NULL, /* dir_action */
134 NULL, /* userData */
135 0 /* depth */);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000136}
137
138#define UUID 1
139#define VOL 2
140
141#ifdef UNUSED
142static char *
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000143get_spec_by_x(int n, const char *t, int *majorPtr, int *minorPtr)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000144{
145 struct uuidCache_s *uc;
146
147 uuidcache_init();
148 uc = uuidCache;
149
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000150 while (uc) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000151 switch (n) {
152 case UUID:
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000153 if (strcmp(t, uc->uc_uuid) == 0) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000154 *majorPtr = uc->major;
155 *minorPtr = uc->minor;
156 return uc->device;
157 }
158 break;
159 case VOL:
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000160 if (strcmp(t, uc->label) == 0) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000161 *majorPtr = uc->major;
162 *minorPtr = uc->minor;
163 return uc->device;
164 }
165 break;
166 }
167 uc = uc->next;
168 }
169 return NULL;
170}
171
172static unsigned char
173fromhex(char c)
174{
175 if (isdigit(c))
176 return (c - '0');
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000177 return ((c|0x20) - 'a' + 10);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000178}
179
180static char *
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000181get_spec_by_uuid(const char *s, int *major, int *minor)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000182{
183 unsigned char uuid[16];
184 int i;
185
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000186 if (strlen(s) != 36 || s[8] != '-' || s[13] != '-'
187 || s[18] != '-' || s[23] != '-'
188 ) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000189 goto bad_uuid;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000190 }
191 for (i = 0; i < 16; i++) {
192 if (*s == '-')
193 s++;
194 if (!isxdigit(s[0]) || !isxdigit(s[1]))
195 goto bad_uuid;
196 uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1]));
197 s += 2;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000198 }
199 return get_spec_by_x(UUID, (char *)uuid, major, minor);
200
201 bad_uuid:
202 fprintf(stderr, _("mount: bad UUID"));
203 return 0;
204}
205
206static char *
207get_spec_by_volume_label(const char *s, int *major, int *minor)
208{
209 return get_spec_by_x(VOL, s, major, minor);
210}
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000211#endif // UNUSED
212
Denis Vlasenko1e19afd2008-10-12 11:20:08 +0000213/* Used by blkid */
214void display_uuid_cache(void)
215{
216 struct uuidCache_s *u;
217
218 uuidcache_init();
219 u = uuidCache;
220 while (u) {
221 printf("%s:", u->device);
222 if (u->label[0])
223 printf(" LABEL=\"%s\"", u->label);
224 if (u->uc_uuid[0])
225 printf(" UUID=\"%s\"", u->uc_uuid);
226 bb_putchar('\n');
227 u = u->next;
228 }
229}
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000230
231/* Used by mount and findfs */
232
233char *get_devname_from_label(const char *spec)
234{
235 struct uuidCache_s *uc;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000236
237 uuidcache_init();
238 uc = uuidCache;
239 while (uc) {
Denis Vlasenko9983d802009-03-31 19:47:34 +0000240 if (uc->label[0] && strcmp(spec, uc->label) == 0) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000241 return xstrdup(uc->device);
242 }
243 uc = uc->next;
244 }
245 return NULL;
246}
247
248char *get_devname_from_uuid(const char *spec)
249{
250 struct uuidCache_s *uc;
251
252 uuidcache_init();
253 uc = uuidCache;
254 while (uc) {
Denis Vlasenkod25c33f2008-03-17 09:25:05 +0000255 /* case of hex numbers doesn't matter */
256 if (strcasecmp(spec, uc->uc_uuid) == 0) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000257 return xstrdup(uc->device);
258 }
259 uc = uc->next;
260 }
261 return NULL;
262}
Natanael Copa9aff2992009-09-20 04:28:22 +0200263
264int resolve_mount_spec(char **fsname)
265{
266 char *tmp = *fsname;
267
268 if (strncmp(*fsname, "UUID=", 5) == 0)
269 tmp = get_devname_from_uuid(*fsname + 5);
270 else if (strncmp(*fsname, "LABEL=", 6) == 0)
271 tmp = get_devname_from_label(*fsname + 6);
272
273 if (tmp == *fsname)
274 return 0; /* no UUID= or LABEL= prefix found */
275
276 if (tmp)
277 *fsname = tmp;
278 return 1;
279}