2008-02-19 02:38:49 +05:30
|
|
|
/* 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
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "volume_id_internal.h"
|
|
|
|
|
2008-03-17 14:51:26 +05:30
|
|
|
//#define BLKGETSIZE64 _IOR(0x12,114,size_t)
|
2008-02-19 02:38:49 +05:30
|
|
|
|
|
|
|
static struct uuidCache_s {
|
|
|
|
struct uuidCache_s *next;
|
2008-03-17 14:51:26 +05:30
|
|
|
// int major, minor;
|
2008-02-19 02:38:49 +05:30
|
|
|
char *device;
|
|
|
|
char *label;
|
2008-03-17 14:51:26 +05:30
|
|
|
char *uc_uuid; /* prefix makes it easier to grep for */
|
2008-02-19 02:38:49 +05:30
|
|
|
} *uuidCache;
|
|
|
|
|
2008-03-17 14:51:26 +05:30
|
|
|
/* Returns !0 on error.
|
|
|
|
* Otherwise, returns malloc'ed strings for label and uuid
|
2008-10-20 01:06:30 +05:30
|
|
|
* (and they can't be NULL, although they can be "").
|
|
|
|
* NB: closes fd. */
|
2008-02-19 02:38:49 +05:30
|
|
|
static int
|
2008-10-20 01:06:30 +05:30
|
|
|
get_label_uuid(int fd, char **label, char **uuid)
|
2008-02-19 02:38:49 +05:30
|
|
|
{
|
|
|
|
int rv = 1;
|
|
|
|
uint64_t size;
|
|
|
|
struct volume_id *vid;
|
|
|
|
|
2008-10-20 01:06:30 +05:30
|
|
|
/* fd is owned by vid now */
|
|
|
|
vid = volume_id_open_node(fd);
|
2008-02-19 02:38:49 +05:30
|
|
|
|
2008-10-20 01:06:30 +05:30
|
|
|
if (ioctl(/*vid->*/fd, BLKGETSIZE64, &size) != 0)
|
2008-02-19 02:38:49 +05:30
|
|
|
size = 0;
|
|
|
|
|
2009-02-15 11:21:19 +05:30
|
|
|
if (volume_id_probe_all(vid, /*0,*/ size) != 0)
|
2008-02-19 02:38:49 +05:30
|
|
|
goto ret;
|
|
|
|
|
2008-03-17 14:51:26 +05:30
|
|
|
if (vid->label[0] != '\0' || vid->uuid[0] != '\0') {
|
2008-02-19 02:38:49 +05:30
|
|
|
*label = xstrndup(vid->label, sizeof(vid->label));
|
|
|
|
*uuid = xstrndup(vid->uuid, sizeof(vid->uuid));
|
2008-03-17 14:51:26 +05:30
|
|
|
dbg("found label '%s', uuid '%s' on %s", *label, *uuid, device);
|
2008-02-19 02:38:49 +05:30
|
|
|
rv = 0;
|
|
|
|
}
|
|
|
|
ret:
|
2008-10-20 01:06:30 +05:30
|
|
|
free_volume_id(vid); /* also closes fd */
|
2008-02-19 02:38:49 +05:30
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2008-03-17 14:51:26 +05:30
|
|
|
/* NB: we take ownership of (malloc'ed) label and uuid */
|
2008-02-19 02:38:49 +05:30
|
|
|
static void
|
2008-03-17 14:51:26 +05:30
|
|
|
uuidcache_addentry(char *device, /*int major, int minor,*/ char *label, char *uuid)
|
2008-02-19 02:38:49 +05:30
|
|
|
{
|
|
|
|
struct uuidCache_s *last;
|
2008-03-24 07:35:58 +05:30
|
|
|
|
2008-02-19 02:38:49 +05:30
|
|
|
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 14:51:26 +05:30
|
|
|
// last->major = major;
|
|
|
|
// last->minor = minor;
|
2008-02-19 02:38:49 +05:30
|
|
|
last->device = device;
|
2008-03-17 14:51:26 +05:30
|
|
|
last->label = label;
|
|
|
|
last->uc_uuid = uuid;
|
2008-02-19 02:38:49 +05:30
|
|
|
}
|
|
|
|
|
2008-03-17 14:51:26 +05:30
|
|
|
/* 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-20 01:06:30 +05:30
|
|
|
static int FAST_FUNC
|
|
|
|
uuidcache_check_device(const char *device,
|
|
|
|
struct stat *statbuf,
|
|
|
|
void *userData UNUSED_PARAM,
|
|
|
|
int depth UNUSED_PARAM)
|
2008-02-19 02:38:49 +05:30
|
|
|
{
|
2008-10-20 01:06:30 +05:30
|
|
|
char *uuid = uuid; /* for compiler */
|
|
|
|
char *label = label;
|
|
|
|
int fd;
|
2008-02-19 02:38:49 +05:30
|
|
|
|
2008-10-20 01:06:30 +05:30
|
|
|
if (!S_ISBLK(statbuf->st_mode))
|
|
|
|
return TRUE;
|
2008-03-17 14:51:26 +05:30
|
|
|
|
2008-10-20 01:06:30 +05:30
|
|
|
fd = open(device, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return TRUE;
|
2008-03-17 14:55:05 +05:30
|
|
|
|
2008-10-20 01:06:30 +05:30
|
|
|
/* get_label_uuid() closes fd in all cases (success & failure) */
|
|
|
|
if (get_label_uuid(fd, &label, &uuid) == 0) {
|
|
|
|
/* uuidcache_addentry() takes ownership of all three params */
|
|
|
|
uuidcache_addentry(xstrdup(device), /*ma, mi,*/ label, uuid);
|
2008-02-19 02:38:49 +05:30
|
|
|
}
|
2008-10-20 01:06:30 +05:30
|
|
|
return TRUE;
|
2008-02-19 02:38:49 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
uuidcache_init(void)
|
|
|
|
{
|
|
|
|
if (uuidCache)
|
|
|
|
return;
|
|
|
|
|
2008-10-20 01:24:49 +05:30
|
|
|
/* 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?) */
|
|
|
|
|
2008-10-20 01:06:30 +05:30
|
|
|
recursive_action("/dev", ACTION_RECURSE,
|
|
|
|
uuidcache_check_device, /* file_action */
|
|
|
|
NULL, /* dir_action */
|
|
|
|
NULL, /* userData */
|
|
|
|
0 /* depth */);
|
2008-02-19 02:38:49 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#define UUID 1
|
|
|
|
#define VOL 2
|
|
|
|
|
|
|
|
#ifdef UNUSED
|
|
|
|
static char *
|
2008-03-17 14:51:26 +05:30
|
|
|
get_spec_by_x(int n, const char *t, int *majorPtr, int *minorPtr)
|
2008-02-19 02:38:49 +05:30
|
|
|
{
|
|
|
|
struct uuidCache_s *uc;
|
|
|
|
|
|
|
|
uuidcache_init();
|
|
|
|
uc = uuidCache;
|
|
|
|
|
2008-03-17 14:51:26 +05:30
|
|
|
while (uc) {
|
2008-02-19 02:38:49 +05:30
|
|
|
switch (n) {
|
|
|
|
case UUID:
|
2008-03-17 14:51:26 +05:30
|
|
|
if (strcmp(t, uc->uc_uuid) == 0) {
|
2008-02-19 02:38:49 +05:30
|
|
|
*majorPtr = uc->major;
|
|
|
|
*minorPtr = uc->minor;
|
|
|
|
return uc->device;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VOL:
|
2008-03-17 14:51:26 +05:30
|
|
|
if (strcmp(t, uc->label) == 0) {
|
2008-02-19 02:38:49 +05:30
|
|
|
*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 14:51:26 +05:30
|
|
|
return ((c|0x20) - 'a' + 10);
|
2008-02-19 02:38:49 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2008-03-17 14:51:26 +05:30
|
|
|
get_spec_by_uuid(const char *s, int *major, int *minor)
|
2008-02-19 02:38:49 +05:30
|
|
|
{
|
|
|
|
unsigned char uuid[16];
|
|
|
|
int i;
|
|
|
|
|
2008-03-17 14:51:26 +05:30
|
|
|
if (strlen(s) != 36 || s[8] != '-' || s[13] != '-'
|
|
|
|
|| s[18] != '-' || s[23] != '-'
|
|
|
|
) {
|
2008-02-19 02:38:49 +05:30
|
|
|
goto bad_uuid;
|
2008-03-17 14:51:26 +05:30
|
|
|
}
|
|
|
|
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-19 02:38:49 +05:30
|
|
|
}
|
|
|
|
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 16:50:08 +05:30
|
|
|
#endif // UNUSED
|
2008-02-19 02:38:49 +05:30
|
|
|
|
2008-10-12 16:50:08 +05:30
|
|
|
/* Used by blkid */
|
|
|
|
void display_uuid_cache(void)
|
2008-02-19 02:38:49 +05:30
|
|
|
{
|
|
|
|
struct uuidCache_s *u;
|
|
|
|
|
|
|
|
uuidcache_init();
|
|
|
|
u = uuidCache;
|
|
|
|
while (u) {
|
2008-10-12 16:50:08 +05:30
|
|
|
printf("%s:", u->device);
|
|
|
|
if (u->label[0])
|
|
|
|
printf(" LABEL=\"%s\"", u->label);
|
|
|
|
if (u->uc_uuid[0])
|
|
|
|
printf(" UUID=\"%s\"", u->uc_uuid);
|
|
|
|
bb_putchar('\n');
|
2008-02-19 02:38:49 +05:30
|
|
|
u = u->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Used by mount and findfs */
|
|
|
|
|
|
|
|
char *get_devname_from_label(const char *spec)
|
|
|
|
{
|
|
|
|
struct uuidCache_s *uc;
|
|
|
|
int spec_len = strlen(spec);
|
|
|
|
|
|
|
|
uuidcache_init();
|
|
|
|
uc = uuidCache;
|
|
|
|
while (uc) {
|
2008-03-17 14:51:26 +05:30
|
|
|
// FIXME: empty label ("LABEL=") matches anything??!
|
|
|
|
if (uc->label[0] && strncmp(spec, uc->label, spec_len) == 0) {
|
2008-02-19 02:38:49 +05:30
|
|
|
return xstrdup(uc->device);
|
|
|
|
}
|
|
|
|
uc = uc->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *get_devname_from_uuid(const char *spec)
|
|
|
|
{
|
|
|
|
struct uuidCache_s *uc;
|
|
|
|
|
|
|
|
uuidcache_init();
|
|
|
|
uc = uuidCache;
|
|
|
|
while (uc) {
|
2008-03-17 14:55:05 +05:30
|
|
|
/* case of hex numbers doesn't matter */
|
|
|
|
if (strcasecmp(spec, uc->uc_uuid) == 0) {
|
2008-02-19 02:38:49 +05:30
|
|
|
return xstrdup(uc->device);
|
|
|
|
}
|
|
|
|
uc = uc->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|