blob: 34f5d119ff951c179589de22418144b6f7bb71dd [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
Denys Vlasenko8cae43c2017-07-27 10:58:08 +020014//kbuild:lib-$(CONFIG_FEATURE_SWAPONOFF_LABEL) += get_devname.o
Sven-Göran Bergh3b458012013-07-31 15:45:20 +020015
Denys Vlasenkoda49f582009-07-08 02:58:38 +020016#include <sys/mount.h> /* BLKGETSIZE64 */
17#if !defined(BLKGETSIZE64)
18# define BLKGETSIZE64 _IOR(0x12,114,size_t)
19#endif
Denis Vlasenkode7684a2008-02-18 21:08:49 +000020#include "volume_id_internal.h"
21
Denis Vlasenkode7684a2008-02-18 21:08:49 +000022static struct uuidCache_s {
23 struct uuidCache_s *next;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000024// int major, minor;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000025 char *device;
26 char *label;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000027 char *uc_uuid; /* prefix makes it easier to grep for */
Denys Vlasenko90615a02010-12-30 00:40:11 +010028 IF_FEATURE_BLKID_TYPE(const char *type;)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000029} *uuidCache;
30
Denys Vlasenko90615a02010-12-30 00:40:11 +010031#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)
36#endif
37
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000038/* Returns !0 on error.
39 * Otherwise, returns malloc'ed strings for label and uuid
Denis Vlasenko582dff02008-10-19 19:36:30 +000040 * (and they can't be NULL, although they can be "").
41 * NB: closes fd. */
Denis Vlasenkode7684a2008-02-18 21:08:49 +000042static int
Denys Vlasenko90615a02010-12-30 00:40:11 +010043get_label_uuid(int fd, char **label, char **uuid, const char **type)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000044{
45 int rv = 1;
46 uint64_t size;
47 struct volume_id *vid;
48
Denis Vlasenko582dff02008-10-19 19:36:30 +000049 /* fd is owned by vid now */
50 vid = volume_id_open_node(fd);
Denis Vlasenkode7684a2008-02-18 21:08:49 +000051
Denis Vlasenko582dff02008-10-19 19:36:30 +000052 if (ioctl(/*vid->*/fd, BLKGETSIZE64, &size) != 0)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000053 size = 0;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000054
Denis Vlasenko28ea4292009-02-15 05:51:19 +000055 if (volume_id_probe_all(vid, /*0,*/ size) != 0)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000056 goto ret;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000057
S-G Berghd2d50492012-11-05 13:16:07 +010058 if (vid->label[0] != '\0' || vid->uuid[0] != '\0'
59#if ENABLE_FEATURE_BLKID_TYPE
60 || vid->type != NULL
61#endif
62 ) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +000063 *label = xstrndup(vid->label, sizeof(vid->label));
64 *uuid = xstrndup(vid->uuid, sizeof(vid->uuid));
Denys Vlasenko90615a02010-12-30 00:40:11 +010065#if ENABLE_FEATURE_BLKID_TYPE
66 *type = vid->type;
67 dbg("found label '%s', uuid '%s', type '%s'", *label, *uuid, *type);
68#else
Vladimir Dronnikov662e8b72009-11-01 23:05:09 +010069 dbg("found label '%s', uuid '%s'", *label, *uuid);
Denys Vlasenko90615a02010-12-30 00:40:11 +010070#endif
Denis Vlasenkode7684a2008-02-18 21:08:49 +000071 rv = 0;
72 }
73 ret:
Denis Vlasenko582dff02008-10-19 19:36:30 +000074 free_volume_id(vid); /* also closes fd */
Denis Vlasenkode7684a2008-02-18 21:08:49 +000075 return rv;
76}
77
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000078/* NB: we take ownership of (malloc'ed) label and uuid */
Denis Vlasenkode7684a2008-02-18 21:08:49 +000079static void
Denys Vlasenko90615a02010-12-30 00:40:11 +010080uuidcache_addentry(char *device, /*int major, int minor,*/ char *label, char *uuid, const char *type)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000081{
82 struct uuidCache_s *last;
Denis Vlasenko42cc3042008-03-24 02:05:58 +000083
Denis Vlasenkode7684a2008-02-18 21:08:49 +000084 if (!uuidCache) {
85 last = uuidCache = xzalloc(sizeof(*uuidCache));
86 } else {
87 for (last = uuidCache; last->next; last = last->next)
88 continue;
89 last->next = xzalloc(sizeof(*uuidCache));
90 last = last->next;
91 }
92 /*last->next = NULL; - xzalloc did it*/
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000093// last->major = major;
94// last->minor = minor;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000095 last->device = device;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +000096 last->label = label;
97 last->uc_uuid = uuid;
Denys Vlasenko90615a02010-12-30 00:40:11 +010098 IF_FEATURE_BLKID_TYPE(last->type = type;)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000099}
100
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000101/* 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. */
Denis Vlasenko582dff02008-10-19 19:36:30 +0000104static int FAST_FUNC
105uuidcache_check_device(const char *device,
106 struct stat *statbuf,
107 void *userData UNUSED_PARAM,
108 int depth UNUSED_PARAM)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000109{
Denys Vlasenko4fc5ec52009-06-27 21:58:25 +0200110 /* note: this check rejects links to devices, among other nodes */
Serj Kalichevd42cdc22016-08-29 18:28:34 +0300111 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)
114#endif
115 )
Denis Vlasenko582dff02008-10-19 19:36:30 +0000116 return TRUE;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000117
Denys Vlasenko4fc5ec52009-06-27 21:58:25 +0200118 /* Users report that mucking with floppies (especially non-present
119 * ones) is significant PITA. This is a horribly dirty hack,
Denys Vlasenko1881ba42009-06-27 22:09:28 +0200120 * but it is very useful in real world.
121 * If this will ever need to be enabled, consider using O_NONBLOCK.
122 */
Denys Vlasenko4fc5ec52009-06-27 21:58:25 +0200123 if (major(statbuf->st_rdev) == 2)
124 return TRUE;
125
Denys Vlasenko90615a02010-12-30 00:40:11 +0100126 add_to_uuid_cache(device);
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000127
Denis Vlasenko582dff02008-10-19 19:36:30 +0000128 return TRUE;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000129}
130
Denys Vlasenkoc3375f02011-12-06 15:06:59 +0100131static struct uuidCache_s*
Denys Vlasenkoe8cfc3f2012-03-03 15:09:07 +0100132uuidcache_init(int scan_devices)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000133{
Denys Vlasenko90615a02010-12-30 00:40:11 +0100134 dbg("DBG: uuidCache=%x, uuidCache");
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000135 if (uuidCache)
Denys Vlasenkoc3375f02011-12-06 15:06:59 +0100136 return uuidCache;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000137
Denis Vlasenko802a7be2008-10-19 19:54:49 +0000138 /* 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
142 * /proc/devices
143 * /proc/evms/volumes
144 * /proc/lvm/VGs
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?) */
Denys Vlasenkoe8cfc3f2012-03-03 15:09:07 +0100148 if (scan_devices)
149 recursive_action("/dev", ACTION_RECURSE,
150 uuidcache_check_device, /* file_action */
151 NULL, /* dir_action */
152 NULL, /* userData */
153 0 /* depth */);
Denys Vlasenkoc3375f02011-12-06 15:06:59 +0100154
155 return uuidCache;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000156}
157
158#define UUID 1
159#define VOL 2
160
161#ifdef UNUSED
162static char *
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000163get_spec_by_x(int n, const char *t, int *majorPtr, int *minorPtr)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000164{
165 struct uuidCache_s *uc;
166
Denys Vlasenkoe8cfc3f2012-03-03 15:09:07 +0100167 uc = uuidcache_init(/*scan_devices:*/ 1);
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000168 while (uc) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000169 switch (n) {
170 case UUID:
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000171 if (strcmp(t, uc->uc_uuid) == 0) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000172 *majorPtr = uc->major;
173 *minorPtr = uc->minor;
174 return uc->device;
175 }
176 break;
177 case VOL:
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000178 if (strcmp(t, uc->label) == 0) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000179 *majorPtr = uc->major;
180 *minorPtr = uc->minor;
181 return uc->device;
182 }
183 break;
184 }
185 uc = uc->next;
186 }
187 return NULL;
188}
189
190static unsigned char
191fromhex(char c)
192{
193 if (isdigit(c))
194 return (c - '0');
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000195 return ((c|0x20) - 'a' + 10);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000196}
197
198static char *
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000199get_spec_by_uuid(const char *s, int *major, int *minor)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000200{
201 unsigned char uuid[16];
202 int i;
203
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000204 if (strlen(s) != 36 || s[8] != '-' || s[13] != '-'
205 || s[18] != '-' || s[23] != '-'
206 ) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000207 goto bad_uuid;
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000208 }
209 for (i = 0; i < 16; i++) {
210 if (*s == '-')
211 s++;
212 if (!isxdigit(s[0]) || !isxdigit(s[1]))
213 goto bad_uuid;
214 uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1]));
215 s += 2;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000216 }
217 return get_spec_by_x(UUID, (char *)uuid, major, minor);
218
219 bad_uuid:
220 fprintf(stderr, _("mount: bad UUID"));
221 return 0;
222}
223
224static char *
225get_spec_by_volume_label(const char *s, int *major, int *minor)
226{
227 return get_spec_by_x(VOL, s, major, minor);
228}
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000229#endif // UNUSED
230
Denis Vlasenko1e19afd2008-10-12 11:20:08 +0000231/* Used by blkid */
Denys Vlasenkoe8cfc3f2012-03-03 15:09:07 +0100232void display_uuid_cache(int scan_devices)
Denis Vlasenko1e19afd2008-10-12 11:20:08 +0000233{
Denys Vlasenkoc3375f02011-12-06 15:06:59 +0100234 struct uuidCache_s *uc;
Denis Vlasenko1e19afd2008-10-12 11:20:08 +0000235
Denys Vlasenkoe8cfc3f2012-03-03 15:09:07 +0100236 uc = uuidcache_init(scan_devices);
Denys Vlasenkoc3375f02011-12-06 15:06:59 +0100237 while (uc) {
238 printf("%s:", uc->device);
239 if (uc->label[0])
240 printf(" LABEL=\"%s\"", uc->label);
241 if (uc->uc_uuid[0])
242 printf(" UUID=\"%s\"", uc->uc_uuid);
Denys Vlasenko90615a02010-12-30 00:40:11 +0100243#if ENABLE_FEATURE_BLKID_TYPE
Denys Vlasenkoc3375f02011-12-06 15:06:59 +0100244 if (uc->type)
245 printf(" TYPE=\"%s\"", uc->type);
Denys Vlasenko90615a02010-12-30 00:40:11 +0100246#endif
Denis Vlasenko1e19afd2008-10-12 11:20:08 +0000247 bb_putchar('\n');
Denys Vlasenkoc3375f02011-12-06 15:06:59 +0100248 uc = uc->next;
Denis Vlasenko1e19afd2008-10-12 11:20:08 +0000249 }
250}
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000251
Denys Vlasenko90615a02010-12-30 00:40:11 +0100252int add_to_uuid_cache(const char *device)
253{
254 char *uuid = uuid; /* for compiler */
255 char *label = label;
256#if ENABLE_FEATURE_BLKID_TYPE
257 const char *type = type;
258#endif
259 int fd;
260
261 fd = open(device, O_RDONLY);
262 if (fd < 0)
263 return 0;
264
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);
269 return 1;
270 }
271 return 0;
272}
273
274
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000275/* Used by mount and findfs */
276
277char *get_devname_from_label(const char *spec)
278{
279 struct uuidCache_s *uc;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000280
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 Vlasenko9983d802009-03-31 19:47:34 +0000283 if (uc->label[0] && strcmp(spec, uc->label) == 0) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000284 return xstrdup(uc->device);
285 }
286 uc = uc->next;
287 }
288 return NULL;
289}
290
291char *get_devname_from_uuid(const char *spec)
292{
293 struct uuidCache_s *uc;
294
Denys Vlasenkoe8cfc3f2012-03-03 15:09:07 +0100295 uc = uuidcache_init(/*scan_devices:*/ 1);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000296 while (uc) {
Denis Vlasenkod25c33f2008-03-17 09:25:05 +0000297 /* case of hex numbers doesn't matter */
298 if (strcasecmp(spec, uc->uc_uuid) == 0) {
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000299 return xstrdup(uc->device);
300 }
301 uc = uc->next;
302 }
303 return NULL;
304}
Natanael Copa9aff2992009-09-20 04:28:22 +0200305
306int resolve_mount_spec(char **fsname)
307{
308 char *tmp = *fsname;
309
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100310 if (is_prefixed_with(*fsname, "UUID="))
Natanael Copa9aff2992009-09-20 04:28:22 +0200311 tmp = get_devname_from_uuid(*fsname + 5);
Alfonso Ranieria90490f2015-04-14 14:32:39 +0200312 else if (is_prefixed_with(*fsname, "LABEL="))
Natanael Copa9aff2992009-09-20 04:28:22 +0200313 tmp = get_devname_from_label(*fsname + 6);
314
315 if (tmp == *fsname)
316 return 0; /* no UUID= or LABEL= prefix found */
317
318 if (tmp)
319 *fsname = tmp;
320 return 1;
321}