2008-02-18 21:08:49 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Support functions for mounting devices by label/uuid
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 by Jason Schoon <floydpink@gmail.com>
|
|
|
|
* Some portions cribbed from e2fsprogs, util-linux, dosfstools
|
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2008-02-18 21:08:49 +00:00
|
|
|
*/
|
2013-07-31 13:45:20 +00:00
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_BLKID) += get_devname.o
|
|
|
|
//kbuild:lib-$(CONFIG_FINDFS) += get_devname.o
|
|
|
|
//kbuild:lib-$(CONFIG_FEATURE_MOUNT_LABEL) += get_devname.o
|
|
|
|
|
2009-07-08 00:58:38 +00:00
|
|
|
#include <sys/mount.h> /* BLKGETSIZE64 */
|
|
|
|
#if !defined(BLKGETSIZE64)
|
|
|
|
# define BLKGETSIZE64 _IOR(0x12,114,size_t)
|
|
|
|
#endif
|
2008-02-18 21:08:49 +00:00
|
|
|
#include "volume_id_internal.h"
|
|
|
|
|
|
|
|
static struct uuidCache_s {
|
|
|
|
struct uuidCache_s *next;
|
2008-03-17 09:21:26 +00:00
|
|
|
// int major, minor;
|
2008-02-18 21:08:49 +00:00
|
|
|
char *device;
|
|
|
|
char *label;
|
2008-03-17 09:21:26 +00:00
|
|
|
char *uc_uuid; /* prefix makes it easier to grep for */
|
2010-12-29 23:40:11 +00:00
|
|
|
IF_FEATURE_BLKID_TYPE(const char *type;)
|
2008-02-18 21:08:49 +00:00
|
|
|
} *uuidCache;
|
|
|
|
|
2010-12-29 23:40:11 +00:00
|
|
|
#if !ENABLE_FEATURE_BLKID_TYPE
|
|
|
|
#define get_label_uuid(fd, label, uuid, type) \
|
|
|
|
get_label_uuid(fd, label, uuid)
|
|
|
|
#define uuidcache_addentry(device, label, uuid, type) \
|
|
|
|
uuidcache_addentry(device, label, uuid)
|
|
|
|
#endif
|
|
|
|
|
2008-03-17 09:21:26 +00:00
|
|
|
/* Returns !0 on error.
|
|
|
|
* Otherwise, returns malloc'ed strings for label and uuid
|
2008-10-19 19:36:30 +00:00
|
|
|
* (and they can't be NULL, although they can be "").
|
|
|
|
* NB: closes fd. */
|
2008-02-18 21:08:49 +00:00
|
|
|
static int
|
2010-12-29 23:40:11 +00:00
|
|
|
get_label_uuid(int fd, char **label, char **uuid, const char **type)
|
2008-02-18 21:08:49 +00:00
|
|
|
{
|
|
|
|
int rv = 1;
|
|
|
|
uint64_t size;
|
|
|
|
struct volume_id *vid;
|
|
|
|
|
2008-10-19 19:36:30 +00:00
|
|
|
/* fd is owned by vid now */
|
|
|
|
vid = volume_id_open_node(fd);
|
2008-02-18 21:08:49 +00:00
|
|
|
|
2008-10-19 19:36:30 +00:00
|
|
|
if (ioctl(/*vid->*/fd, BLKGETSIZE64, &size) != 0)
|
2008-02-18 21:08:49 +00:00
|
|
|
size = 0;
|
|
|
|
|
2009-02-15 05:51:19 +00:00
|
|
|
if (volume_id_probe_all(vid, /*0,*/ size) != 0)
|
2008-02-18 21:08:49 +00:00
|
|
|
goto ret;
|
|
|
|
|
2012-11-05 12:16:07 +00:00
|
|
|
if (vid->label[0] != '\0' || vid->uuid[0] != '\0'
|
|
|
|
#if ENABLE_FEATURE_BLKID_TYPE
|
|
|
|
|| vid->type != NULL
|
|
|
|
#endif
|
|
|
|
) {
|
2008-02-18 21:08:49 +00:00
|
|
|
*label = xstrndup(vid->label, sizeof(vid->label));
|
|
|
|
*uuid = xstrndup(vid->uuid, sizeof(vid->uuid));
|
2010-12-29 23:40:11 +00:00
|
|
|
#if ENABLE_FEATURE_BLKID_TYPE
|
|
|
|
*type = vid->type;
|
|
|
|
dbg("found label '%s', uuid '%s', type '%s'", *label, *uuid, *type);
|
|
|
|
#else
|
2009-11-01 22:05:09 +00:00
|
|
|
dbg("found label '%s', uuid '%s'", *label, *uuid);
|
2010-12-29 23:40:11 +00:00
|
|
|
#endif
|
2008-02-18 21:08:49 +00:00
|
|
|
rv = 0;
|
|
|
|
}
|
|
|
|
ret:
|
2008-10-19 19:36:30 +00:00
|
|
|
free_volume_id(vid); /* also closes fd */
|
2008-02-18 21:08:49 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2008-03-17 09:21:26 +00:00
|
|
|
/* NB: we take ownership of (malloc'ed) label and uuid */
|
2008-02-18 21:08:49 +00:00
|
|
|
static void
|
2010-12-29 23:40:11 +00:00
|
|
|
uuidcache_addentry(char *device, /*int major, int minor,*/ char *label, char *uuid, const char *type)
|
2008-02-18 21:08:49 +00:00
|
|
|
{
|
|
|
|
struct uuidCache_s *last;
|
2008-03-24 02:05:58 +00:00
|
|
|
|
2008-02-18 21:08:49 +00:00
|
|
|
if (!uuidCache) {
|
|
|
|
last = uuidCache = xzalloc(sizeof(*uuidCache));
|
|
|
|
} else {
|
|
|
|
for (last = uuidCache; last->next; last = last->next)
|
|
|
|
continue;
|
|
|
|
last->next = xzalloc(sizeof(*uuidCache));
|
|
|
|
last = last->next;
|
|
|
|
}
|
|
|
|
/*last->next = NULL; - xzalloc did it*/
|
2008-03-17 09:21:26 +00:00
|
|
|
// last->major = major;
|
|
|
|
// last->minor = minor;
|
2008-02-18 21:08:49 +00:00
|
|
|
last->device = device;
|
2008-03-17 09:21:26 +00:00
|
|
|
last->label = label;
|
|
|
|
last->uc_uuid = uuid;
|
2010-12-29 23:40:11 +00:00
|
|
|
IF_FEATURE_BLKID_TYPE(last->type = type;)
|
2008-02-18 21:08:49 +00:00
|
|
|
}
|
|
|
|
|
2008-03-17 09:21:26 +00:00
|
|
|
/* If get_label_uuid() on device_name returns success,
|
|
|
|
* add a cache entry for this device.
|
|
|
|
* If device node does not exist, it will be temporarily created. */
|
2008-10-19 19:36:30 +00:00
|
|
|
static int FAST_FUNC
|
|
|
|
uuidcache_check_device(const char *device,
|
|
|
|
struct stat *statbuf,
|
|
|
|
void *userData UNUSED_PARAM,
|
|
|
|
int depth UNUSED_PARAM)
|
2008-02-18 21:08:49 +00:00
|
|
|
{
|
2009-06-27 19:58:25 +00:00
|
|
|
/* note: this check rejects links to devices, among other nodes */
|
2008-10-19 19:36:30 +00:00
|
|
|
if (!S_ISBLK(statbuf->st_mode))
|
|
|
|
return TRUE;
|
2008-03-17 09:21:26 +00:00
|
|
|
|
2009-06-27 19:58:25 +00:00
|
|
|
/* Users report that mucking with floppies (especially non-present
|
|
|
|
* ones) is significant PITA. This is a horribly dirty hack,
|
2009-06-27 20:09:28 +00:00
|
|
|
* but it is very useful in real world.
|
|
|
|
* If this will ever need to be enabled, consider using O_NONBLOCK.
|
|
|
|
*/
|
2009-06-27 19:58:25 +00:00
|
|
|
if (major(statbuf->st_rdev) == 2)
|
|
|
|
return TRUE;
|
|
|
|
|
2010-12-29 23:40:11 +00:00
|
|
|
add_to_uuid_cache(device);
|
2008-03-17 09:25:05 +00:00
|
|
|
|
2008-10-19 19:36:30 +00:00
|
|
|
return TRUE;
|
2008-02-18 21:08:49 +00:00
|
|
|
}
|
|
|
|
|
2011-12-06 14:06:59 +00:00
|
|
|
static struct uuidCache_s*
|
2012-03-03 14:09:07 +00:00
|
|
|
uuidcache_init(int scan_devices)
|
2008-02-18 21:08:49 +00:00
|
|
|
{
|
2010-12-29 23:40:11 +00:00
|
|
|
dbg("DBG: uuidCache=%x, uuidCache");
|
2008-02-18 21:08:49 +00:00
|
|
|
if (uuidCache)
|
2011-12-06 14:06:59 +00:00
|
|
|
return uuidCache;
|
2008-02-18 21:08:49 +00:00
|
|
|
|
2008-10-19 19:54:49 +00:00
|
|
|
/* We were scanning /proc/partitions
|
|
|
|
* and /proc/sys/dev/cdrom/info here.
|
|
|
|
* Missed volume managers. I see that "standard" blkid uses these:
|
|
|
|
* /dev/mapper/control
|
|
|
|
* /proc/devices
|
|
|
|
* /proc/evms/volumes
|
|
|
|
* /proc/lvm/VGs
|
|
|
|
* This is unacceptably complex. Let's just scan /dev.
|
|
|
|
* (Maybe add scanning of /sys/block/XXX/dev for devices
|
|
|
|
* somehow not having their /dev/XXX entries created?) */
|
2012-03-03 14:09:07 +00:00
|
|
|
if (scan_devices)
|
|
|
|
recursive_action("/dev", ACTION_RECURSE,
|
|
|
|
uuidcache_check_device, /* file_action */
|
|
|
|
NULL, /* dir_action */
|
|
|
|
NULL, /* userData */
|
|
|
|
0 /* depth */);
|
2011-12-06 14:06:59 +00:00
|
|
|
|
|
|
|
return uuidCache;
|
2008-02-18 21:08:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define UUID 1
|
|
|
|
#define VOL 2
|
|
|
|
|
|
|
|
#ifdef UNUSED
|
|
|
|
static char *
|
2008-03-17 09:21:26 +00:00
|
|
|
get_spec_by_x(int n, const char *t, int *majorPtr, int *minorPtr)
|
2008-02-18 21:08:49 +00:00
|
|
|
{
|
|
|
|
struct uuidCache_s *uc;
|
|
|
|
|
2012-03-03 14:09:07 +00:00
|
|
|
uc = uuidcache_init(/*scan_devices:*/ 1);
|
2008-03-17 09:21:26 +00:00
|
|
|
while (uc) {
|
2008-02-18 21:08:49 +00:00
|
|
|
switch (n) {
|
|
|
|
case UUID:
|
2008-03-17 09:21:26 +00:00
|
|
|
if (strcmp(t, uc->uc_uuid) == 0) {
|
2008-02-18 21:08:49 +00:00
|
|
|
*majorPtr = uc->major;
|
|
|
|
*minorPtr = uc->minor;
|
|
|
|
return uc->device;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VOL:
|
2008-03-17 09:21:26 +00:00
|
|
|
if (strcmp(t, uc->label) == 0) {
|
2008-02-18 21:08:49 +00:00
|
|
|
*majorPtr = uc->major;
|
|
|
|
*minorPtr = uc->minor;
|
|
|
|
return uc->device;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
uc = uc->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char
|
|
|
|
fromhex(char c)
|
|
|
|
{
|
|
|
|
if (isdigit(c))
|
|
|
|
return (c - '0');
|
2008-03-17 09:21:26 +00:00
|
|
|
return ((c|0x20) - 'a' + 10);
|
2008-02-18 21:08:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2008-03-17 09:21:26 +00:00
|
|
|
get_spec_by_uuid(const char *s, int *major, int *minor)
|
2008-02-18 21:08:49 +00:00
|
|
|
{
|
|
|
|
unsigned char uuid[16];
|
|
|
|
int i;
|
|
|
|
|
2008-03-17 09:21:26 +00:00
|
|
|
if (strlen(s) != 36 || s[8] != '-' || s[13] != '-'
|
|
|
|
|| s[18] != '-' || s[23] != '-'
|
|
|
|
) {
|
2008-02-18 21:08:49 +00:00
|
|
|
goto bad_uuid;
|
2008-03-17 09:21:26 +00:00
|
|
|
}
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
if (*s == '-')
|
|
|
|
s++;
|
|
|
|
if (!isxdigit(s[0]) || !isxdigit(s[1]))
|
|
|
|
goto bad_uuid;
|
|
|
|
uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1]));
|
|
|
|
s += 2;
|
2008-02-18 21:08:49 +00:00
|
|
|
}
|
|
|
|
return get_spec_by_x(UUID, (char *)uuid, major, minor);
|
|
|
|
|
|
|
|
bad_uuid:
|
|
|
|
fprintf(stderr, _("mount: bad UUID"));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
get_spec_by_volume_label(const char *s, int *major, int *minor)
|
|
|
|
{
|
|
|
|
return get_spec_by_x(VOL, s, major, minor);
|
|
|
|
}
|
2008-10-12 11:20:08 +00:00
|
|
|
#endif // UNUSED
|
2008-02-18 21:08:49 +00:00
|
|
|
|
2008-10-12 11:20:08 +00:00
|
|
|
/* Used by blkid */
|
2012-03-03 14:09:07 +00:00
|
|
|
void display_uuid_cache(int scan_devices)
|
2008-02-18 21:08:49 +00:00
|
|
|
{
|
2011-12-06 14:06:59 +00:00
|
|
|
struct uuidCache_s *uc;
|
|
|
|
|
2012-03-03 14:09:07 +00:00
|
|
|
uc = uuidcache_init(scan_devices);
|
2011-12-06 14:06:59 +00:00
|
|
|
while (uc) {
|
|
|
|
printf("%s:", uc->device);
|
|
|
|
if (uc->label[0])
|
|
|
|
printf(" LABEL=\"%s\"", uc->label);
|
|
|
|
if (uc->uc_uuid[0])
|
|
|
|
printf(" UUID=\"%s\"", uc->uc_uuid);
|
2010-12-29 23:40:11 +00:00
|
|
|
#if ENABLE_FEATURE_BLKID_TYPE
|
2011-12-06 14:06:59 +00:00
|
|
|
if (uc->type)
|
|
|
|
printf(" TYPE=\"%s\"", uc->type);
|
2010-12-29 23:40:11 +00:00
|
|
|
#endif
|
2008-10-12 11:20:08 +00:00
|
|
|
bb_putchar('\n');
|
2011-12-06 14:06:59 +00:00
|
|
|
uc = uc->next;
|
2008-02-18 21:08:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-29 23:40:11 +00:00
|
|
|
int add_to_uuid_cache(const char *device)
|
|
|
|
{
|
|
|
|
char *uuid = uuid; /* for compiler */
|
|
|
|
char *label = label;
|
|
|
|
#if ENABLE_FEATURE_BLKID_TYPE
|
|
|
|
const char *type = type;
|
|
|
|
#endif
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open(device, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* get_label_uuid() closes fd in all cases (success & failure) */
|
|
|
|
if (get_label_uuid(fd, &label, &uuid, &type) == 0) {
|
|
|
|
/* uuidcache_addentry() takes ownership of all four params */
|
|
|
|
uuidcache_addentry(xstrdup(device), /*ma, mi,*/ label, uuid, type);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-18 21:08:49 +00:00
|
|
|
/* Used by mount and findfs */
|
|
|
|
|
|
|
|
char *get_devname_from_label(const char *spec)
|
|
|
|
{
|
|
|
|
struct uuidCache_s *uc;
|
|
|
|
|
2012-03-03 14:09:07 +00:00
|
|
|
uc = uuidcache_init(/*scan_devices:*/ 1);
|
2008-02-18 21:08:49 +00:00
|
|
|
while (uc) {
|
2009-03-31 19:47:34 +00:00
|
|
|
if (uc->label[0] && strcmp(spec, uc->label) == 0) {
|
2008-02-18 21:08:49 +00:00
|
|
|
return xstrdup(uc->device);
|
|
|
|
}
|
|
|
|
uc = uc->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *get_devname_from_uuid(const char *spec)
|
|
|
|
{
|
|
|
|
struct uuidCache_s *uc;
|
|
|
|
|
2012-03-03 14:09:07 +00:00
|
|
|
uc = uuidcache_init(/*scan_devices:*/ 1);
|
2008-02-18 21:08:49 +00:00
|
|
|
while (uc) {
|
2008-03-17 09:25:05 +00:00
|
|
|
/* case of hex numbers doesn't matter */
|
|
|
|
if (strcasecmp(spec, uc->uc_uuid) == 0) {
|
2008-02-18 21:08:49 +00:00
|
|
|
return xstrdup(uc->device);
|
|
|
|
}
|
|
|
|
uc = uc->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-09-20 02:28:22 +00:00
|
|
|
|
|
|
|
int resolve_mount_spec(char **fsname)
|
|
|
|
{
|
|
|
|
char *tmp = *fsname;
|
|
|
|
|
|
|
|
if (strncmp(*fsname, "UUID=", 5) == 0)
|
|
|
|
tmp = get_devname_from_uuid(*fsname + 5);
|
|
|
|
else if (strncmp(*fsname, "LABEL=", 6) == 0)
|
|
|
|
tmp = get_devname_from_label(*fsname + 6);
|
|
|
|
|
|
|
|
if (tmp == *fsname)
|
|
|
|
return 0; /* no UUID= or LABEL= prefix found */
|
|
|
|
|
|
|
|
if (tmp)
|
|
|
|
*fsname = tmp;
|
|
|
|
return 1;
|
|
|
|
}
|