blob: ac69f78c451b5eb39965ef0ac447640dc71a1d92 [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 */
10
11#include "volume_id_internal.h"
12
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000013//#define BLKGETSIZE64 _IOR(0x12,114,size_t)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000014
15static struct uuidCache_s {
16 struct uuidCache_s *next;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000017// int major, minor;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000018 char *device;
19 char *label;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000020 char *uc_uuid; /* prefix makes it easier to grep for */
Denis Vlasenkode7684a2008-02-18 21:08:49 +000021} *uuidCache;
22
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000023/* Returns !0 on error.
24 * Otherwise, returns malloc'ed strings for label and uuid
Denis Vlasenko582dff02008-10-19 19:36:30 +000025 * (and they can't be NULL, although they can be "").
26 * NB: closes fd. */
Denis Vlasenkode7684a2008-02-18 21:08:49 +000027static int
Denis Vlasenko582dff02008-10-19 19:36:30 +000028get_label_uuid(int fd, char **label, char **uuid)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000029{
30 int rv = 1;
31 uint64_t size;
32 struct volume_id *vid;
33
Denis Vlasenko582dff02008-10-19 19:36:30 +000034 /* fd is owned by vid now */
35 vid = volume_id_open_node(fd);
Denis Vlasenkode7684a2008-02-18 21:08:49 +000036
Denis Vlasenko582dff02008-10-19 19:36:30 +000037 if (ioctl(/*vid->*/fd, BLKGETSIZE64, &size) != 0)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000038 size = 0;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000039
Denis Vlasenko28ea4292009-02-15 05:51:19 +000040 if (volume_id_probe_all(vid, /*0,*/ size) != 0)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000041 goto ret;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000042
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000043 if (vid->label[0] != '\0' || vid->uuid[0] != '\0') {
Denis Vlasenkode7684a2008-02-18 21:08:49 +000044 *label = xstrndup(vid->label, sizeof(vid->label));
45 *uuid = xstrndup(vid->uuid, sizeof(vid->uuid));
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000046 dbg("found label '%s', uuid '%s' on %s", *label, *uuid, device);
Denis Vlasenkode7684a2008-02-18 21:08:49 +000047 rv = 0;
48 }
49 ret:
Denis Vlasenko582dff02008-10-19 19:36:30 +000050 free_volume_id(vid); /* also closes fd */
Denis Vlasenkode7684a2008-02-18 21:08:49 +000051 return rv;
52}
53
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000054/* NB: we take ownership of (malloc'ed) label and uuid */
Denis Vlasenkode7684a2008-02-18 21:08:49 +000055static void
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000056uuidcache_addentry(char *device, /*int major, int minor,*/ char *label, char *uuid)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000057{
58 struct uuidCache_s *last;
Denis Vlasenko42cc3042008-03-24 02:05:58 +000059
Denis Vlasenkode7684a2008-02-18 21:08:49 +000060 if (!uuidCache) {
61 last = uuidCache = xzalloc(sizeof(*uuidCache));
62 } else {
63 for (last = uuidCache; last->next; last = last->next)
64 continue;
65 last->next = xzalloc(sizeof(*uuidCache));
66 last = last->next;
67 }
68 /*last->next = NULL; - xzalloc did it*/
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000069// last->major = major;
70// last->minor = minor;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000071 last->device = device;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000072 last->label = label;
73 last->uc_uuid = uuid;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000074}
75
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000076/* 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. */
Denis Vlasenko582dff02008-10-19 19:36:30 +000079static int FAST_FUNC
80uuidcache_check_device(const char *device,
81 struct stat *statbuf,
82 void *userData UNUSED_PARAM,
83 int depth UNUSED_PARAM)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000084{
Denis Vlasenko582dff02008-10-19 19:36:30 +000085 char *uuid = uuid; /* for compiler */
86 char *label = label;
87 int fd;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000088
Denis Vlasenko582dff02008-10-19 19:36:30 +000089 if (!S_ISBLK(statbuf->st_mode))
90 return TRUE;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000091
Denis Vlasenko582dff02008-10-19 19:36:30 +000092 fd = open(device, O_RDONLY);
93 if (fd < 0)
94 return TRUE;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000095
Denis Vlasenko582dff02008-10-19 19:36:30 +000096 /* get_label_uuid() closes fd in all cases (success & failure) */
97 if (get_label_uuid(fd, &label, &uuid) == 0) {
98 /* uuidcache_addentry() takes ownership of all three params */
99 uuidcache_addentry(xstrdup(device), /*ma, mi,*/ label, uuid);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000100 }
Denis Vlasenko582dff02008-10-19 19:36:30 +0000101 return TRUE;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000102}
103
104static void
105uuidcache_init(void)
106{
107 if (uuidCache)
108 return;
109
Denis Vlasenko802a7be2008-10-19 19:54:49 +0000110 /* We were scanning /proc/partitions
111 * and /proc/sys/dev/cdrom/info here.
112 * Missed volume managers. I see that "standard" blkid uses these:
113 * /dev/mapper/control
114 * /proc/devices
115 * /proc/evms/volumes
116 * /proc/lvm/VGs
117 * This is unacceptably complex. Let's just scan /dev.
118 * (Maybe add scanning of /sys/block/XXX/dev for devices
119 * somehow not having their /dev/XXX entries created?) */
120
Denis Vlasenko582dff02008-10-19 19:36:30 +0000121 recursive_action("/dev", ACTION_RECURSE,
122 uuidcache_check_device, /* file_action */
123 NULL, /* dir_action */
124 NULL, /* userData */
125 0 /* depth */);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000126}
127
128#define UUID 1
129#define VOL 2
130
131#ifdef UNUSED
132static char *
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000133get_spec_by_x(int n, const char *t, int *majorPtr, int *minorPtr)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000134{
135 struct uuidCache_s *uc;
136
137 uuidcache_init();
138 uc = uuidCache;
139
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000140 while (uc) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000141 switch (n) {
142 case UUID:
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000143 if (strcmp(t, uc->uc_uuid) == 0) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000144 *majorPtr = uc->major;
145 *minorPtr = uc->minor;
146 return uc->device;
147 }
148 break;
149 case VOL:
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000150 if (strcmp(t, uc->label) == 0) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000151 *majorPtr = uc->major;
152 *minorPtr = uc->minor;
153 return uc->device;
154 }
155 break;
156 }
157 uc = uc->next;
158 }
159 return NULL;
160}
161
162static unsigned char
163fromhex(char c)
164{
165 if (isdigit(c))
166 return (c - '0');
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000167 return ((c|0x20) - 'a' + 10);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000168}
169
170static char *
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000171get_spec_by_uuid(const char *s, int *major, int *minor)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000172{
173 unsigned char uuid[16];
174 int i;
175
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000176 if (strlen(s) != 36 || s[8] != '-' || s[13] != '-'
177 || s[18] != '-' || s[23] != '-'
178 ) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000179 goto bad_uuid;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000180 }
181 for (i = 0; i < 16; i++) {
182 if (*s == '-')
183 s++;
184 if (!isxdigit(s[0]) || !isxdigit(s[1]))
185 goto bad_uuid;
186 uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1]));
187 s += 2;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000188 }
189 return get_spec_by_x(UUID, (char *)uuid, major, minor);
190
191 bad_uuid:
192 fprintf(stderr, _("mount: bad UUID"));
193 return 0;
194}
195
196static char *
197get_spec_by_volume_label(const char *s, int *major, int *minor)
198{
199 return get_spec_by_x(VOL, s, major, minor);
200}
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000201#endif // UNUSED
202
Denis Vlasenko1e19afd2008-10-12 11:20:08 +0000203/* Used by blkid */
204void display_uuid_cache(void)
205{
206 struct uuidCache_s *u;
207
208 uuidcache_init();
209 u = uuidCache;
210 while (u) {
211 printf("%s:", u->device);
212 if (u->label[0])
213 printf(" LABEL=\"%s\"", u->label);
214 if (u->uc_uuid[0])
215 printf(" UUID=\"%s\"", u->uc_uuid);
216 bb_putchar('\n');
217 u = u->next;
218 }
219}
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000220
221/* Used by mount and findfs */
222
223char *get_devname_from_label(const char *spec)
224{
225 struct uuidCache_s *uc;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000226
227 uuidcache_init();
228 uc = uuidCache;
229 while (uc) {
Denis Vlasenko9983d802009-03-31 19:47:34 +0000230 if (uc->label[0] && strcmp(spec, uc->label) == 0) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000231 return xstrdup(uc->device);
232 }
233 uc = uc->next;
234 }
235 return NULL;
236}
237
238char *get_devname_from_uuid(const char *spec)
239{
240 struct uuidCache_s *uc;
241
242 uuidcache_init();
243 uc = uuidCache;
244 while (uc) {
Denis Vlasenkod25c33f2008-03-17 09:25:05 +0000245 /* case of hex numbers doesn't matter */
246 if (strcasecmp(spec, uc->uc_uuid) == 0) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000247 return xstrdup(uc->device);
248 }
249 uc = uc->next;
250 }
251 return NULL;
252}