1999-07-03 05:59:42 +04:00
|
|
|
/*
|
|
|
|
* get_device_by_label.h
|
|
|
|
*
|
2000-08-20 23:11:05 +04:00
|
|
|
* Copyright 1999 by Andries Brouwer
|
|
|
|
* Copyright 1999, 2000 by Theodore Ts'o
|
1999-07-03 05:59:42 +04:00
|
|
|
*
|
|
|
|
* This file may be redistributed under the terms of the GNU Public
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* Taken from aeb's mount, 990619
|
2000-07-27 06:45:55 +04:00
|
|
|
* Updated from aeb's mount, 20000725
|
2000-08-20 23:11:05 +04:00
|
|
|
* Added call to ext2fs_find_block_device, so that we can find devices
|
|
|
|
* even if devfs (ugh) is compiled in, but not mounted, since
|
|
|
|
* this messes up /proc/partitions, by TYT.
|
1999-07-03 05:59:42 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2000-08-20 23:11:05 +04:00
|
|
|
#include <stdlib.h>
|
1999-07-03 05:59:42 +04:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2000-08-20 23:11:05 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#ifdef HAVE_SYS_MKDEV_H
|
|
|
|
#include <sys/mkdev.h>
|
|
|
|
#endif
|
2001-01-06 07:20:03 +03:00
|
|
|
#ifdef HAVE_SYS_SYSMACROS_H
|
|
|
|
#include <sys/sysmacros.h>
|
|
|
|
#endif
|
Many files:
badblocks.c, chattr.c, dumpe2fs.c, e2label.c, findsuper.c, fsck.c,
get_device_by_label.c, lsattr.c, mke2fs.c, mklost+found.c,
nls-enable.h, partinfo.c, tune2fs.c, uuidgen.c: Add
Internationalization support as suggested by Marco d'Itri
<md@linux.it>.
2000-02-08 03:47:55 +03:00
|
|
|
#include "nls-enable.h"
|
1999-07-03 05:59:42 +04:00
|
|
|
#include "get_device_by_label.h"
|
2001-01-06 07:20:03 +03:00
|
|
|
#include "fsck.h"
|
1999-07-03 05:59:42 +04:00
|
|
|
|
2000-08-20 23:11:05 +04:00
|
|
|
/* function prototype from libext2 */
|
|
|
|
extern char *ext2fs_find_block_device(dev_t device);
|
|
|
|
|
1999-07-03 05:59:42 +04:00
|
|
|
#define PROC_PARTITIONS "/proc/partitions"
|
|
|
|
#define DEVLABELDIR "/dev"
|
|
|
|
|
|
|
|
#define EXT2_SUPER_MAGIC 0xEF53
|
|
|
|
struct ext2_super_block {
|
1999-10-26 18:38:36 +04:00
|
|
|
unsigned char s_dummy1[56];
|
|
|
|
unsigned char s_magic[2];
|
|
|
|
unsigned char s_dummy2[46];
|
|
|
|
unsigned char s_uuid[16];
|
|
|
|
unsigned char s_volume_name[16];
|
1999-07-03 05:59:42 +04:00
|
|
|
};
|
1999-10-26 18:38:36 +04:00
|
|
|
#define ext2magic(s) ((unsigned int) s.s_magic[0] + (((unsigned int) s.s_magic[1]) << 8))
|
1999-07-03 05:59:42 +04:00
|
|
|
|
2000-07-27 06:45:55 +04:00
|
|
|
static struct uuidCache_s {
|
|
|
|
struct uuidCache_s *next;
|
|
|
|
char uuid[16];
|
|
|
|
char *label;
|
|
|
|
char *device;
|
|
|
|
} *uuidCache = NULL;
|
1999-07-03 05:59:42 +04:00
|
|
|
|
|
|
|
/* for now, only ext2 is supported */
|
|
|
|
static int
|
2000-07-27 06:45:55 +04:00
|
|
|
get_label_uuid(const char *device, char **label, char *uuid) {
|
1999-07-03 05:59:42 +04:00
|
|
|
|
|
|
|
/* start with a test for ext2, taken from mount_guess_fstype */
|
2000-07-27 06:45:55 +04:00
|
|
|
/* should merge these later */
|
1999-07-03 05:59:42 +04:00
|
|
|
int fd;
|
|
|
|
struct ext2_super_block e2sb;
|
|
|
|
|
|
|
|
fd = open(device, O_RDONLY);
|
|
|
|
if (fd < 0)
|
2000-07-27 06:45:55 +04:00
|
|
|
return 1;
|
1999-07-03 05:59:42 +04:00
|
|
|
|
|
|
|
if (lseek(fd, 1024, SEEK_SET) != 1024
|
|
|
|
|| read(fd, (char *) &e2sb, sizeof(e2sb)) != sizeof(e2sb)
|
|
|
|
|| (ext2magic(e2sb) != EXT2_SUPER_MAGIC)) {
|
|
|
|
close(fd);
|
2000-07-27 06:45:55 +04:00
|
|
|
return 1;
|
1999-07-03 05:59:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
/* superblock is ext2 - now what is its label? */
|
2000-07-27 06:45:55 +04:00
|
|
|
memcpy(uuid, e2sb.s_uuid, sizeof(e2sb.s_uuid));
|
2000-10-25 06:24:33 +04:00
|
|
|
|
|
|
|
*label = calloc(sizeof(e2sb.s_volume_name) + 1, 1);
|
|
|
|
memcpy(*label, e2sb.s_volume_name, sizeof(e2sb.s_volume_name));
|
2000-07-27 06:45:55 +04:00
|
|
|
|
|
|
|
return 0;
|
1999-07-03 05:59:42 +04:00
|
|
|
}
|
|
|
|
|
2000-07-27 06:45:55 +04:00
|
|
|
static void
|
|
|
|
uuidcache_addentry(char *device, char *label, char *uuid) {
|
|
|
|
struct uuidCache_s *last;
|
|
|
|
|
|
|
|
if (!uuidCache) {
|
|
|
|
last = uuidCache = malloc(sizeof(*uuidCache));
|
|
|
|
} else {
|
|
|
|
for (last = uuidCache; last->next; last = last->next) ;
|
|
|
|
last->next = malloc(sizeof(*uuidCache));
|
|
|
|
last = last->next;
|
|
|
|
}
|
|
|
|
last->next = NULL;
|
|
|
|
last->device = device;
|
|
|
|
last->label = label;
|
|
|
|
memcpy(last->uuid, uuid, sizeof(last->uuid));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
uuidcache_init(void) {
|
|
|
|
char line[100];
|
|
|
|
char *s;
|
|
|
|
int ma, mi, sz;
|
|
|
|
static char ptname[100];
|
|
|
|
FILE *procpt;
|
2000-08-20 23:11:05 +04:00
|
|
|
char uuid[16], *label, *devname;
|
1999-07-03 05:59:42 +04:00
|
|
|
char device[110];
|
2000-08-20 23:11:05 +04:00
|
|
|
dev_t dev;
|
|
|
|
struct stat statbuf;
|
2000-07-27 06:45:55 +04:00
|
|
|
int firstPass;
|
|
|
|
int handleOnFirst;
|
|
|
|
|
|
|
|
if (uuidCache)
|
|
|
|
return;
|
|
|
|
|
|
|
|
procpt = fopen(PROC_PARTITIONS, "r");
|
|
|
|
if (!procpt)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (firstPass = 1; firstPass >= 0; firstPass--) {
|
|
|
|
fseek(procpt, 0, SEEK_SET);
|
|
|
|
|
|
|
|
while (fgets(line, sizeof(line), procpt)) {
|
|
|
|
if (sscanf (line, " %d %d %d %[^\n ]",
|
|
|
|
&ma, &mi, &sz, ptname) != 4)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* skip extended partitions (heuristic: size 1) */
|
|
|
|
if (sz == 1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* look only at md devices on first pass */
|
|
|
|
handleOnFirst = !strncmp(ptname, "md", 2);
|
|
|
|
if (firstPass != handleOnFirst)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* skip entire disk (minor 0, 64, ... on ide;
|
|
|
|
0, 16, ... on sd) */
|
|
|
|
/* heuristic: partition name ends in a digit */
|
|
|
|
|
|
|
|
for(s = ptname; *s; s++);
|
|
|
|
if (isdigit(s[-1])) {
|
2000-08-20 23:11:05 +04:00
|
|
|
/*
|
|
|
|
* We first look in /dev for the device, but
|
|
|
|
* if we don't find it, or if the stat
|
|
|
|
* information doesn't check out, we use
|
|
|
|
* ext2fs_find_block_device to find it.
|
|
|
|
*/
|
2000-07-27 06:45:55 +04:00
|
|
|
sprintf(device, "%s/%s", DEVLABELDIR, ptname);
|
2000-08-20 23:11:05 +04:00
|
|
|
dev = makedev(ma, mi);
|
|
|
|
if ((stat(device, &statbuf) < 0) ||
|
|
|
|
(statbuf.st_rdev != dev)) {
|
|
|
|
devname = ext2fs_find_block_device(dev);
|
|
|
|
} else
|
2001-01-06 07:20:03 +03:00
|
|
|
devname = string_copy(device);
|
2000-08-20 23:11:05 +04:00
|
|
|
if (!devname)
|
|
|
|
continue;
|
|
|
|
if (!get_label_uuid(devname, &label, uuid))
|
|
|
|
uuidcache_addentry(devname, label, uuid);
|
|
|
|
else
|
|
|
|
free(devname);
|
2000-07-27 06:45:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(procpt);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define UUID 1
|
|
|
|
#define VOL 2
|
1999-07-03 05:59:42 +04:00
|
|
|
|
2000-07-27 06:45:55 +04:00
|
|
|
static char *
|
|
|
|
get_spec_by_x(int n, const char *t) {
|
|
|
|
struct uuidCache_s *uc;
|
|
|
|
|
|
|
|
uuidcache_init();
|
|
|
|
uc = uuidCache;
|
|
|
|
|
|
|
|
while(uc) {
|
|
|
|
switch (n) {
|
|
|
|
case UUID:
|
|
|
|
if (!memcmp(t, uc->uuid, sizeof(uc->uuid)))
|
2001-01-06 07:20:03 +03:00
|
|
|
return string_copy(uc->device);
|
2000-07-27 06:45:55 +04:00
|
|
|
break;
|
|
|
|
case VOL:
|
|
|
|
if (!strcmp(t, uc->label))
|
2001-01-06 07:20:03 +03:00
|
|
|
return string_copy(uc->device);
|
2000-07-27 06:45:55 +04:00
|
|
|
break;
|
1999-07-03 05:59:42 +04:00
|
|
|
}
|
2000-07-27 06:45:55 +04:00
|
|
|
uc = uc->next;
|
1999-07-03 05:59:42 +04:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-01-06 07:20:03 +03:00
|
|
|
static char fromhex(char c)
|
|
|
|
{
|
1999-07-03 05:59:42 +04:00
|
|
|
if (isdigit(c))
|
|
|
|
return (c - '0');
|
|
|
|
else if (islower(c))
|
|
|
|
return (c - 'a' + 10);
|
|
|
|
else
|
|
|
|
return (c - 'A' + 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2001-01-06 07:20:03 +03:00
|
|
|
get_spec_by_uuid(const char *s)
|
|
|
|
{
|
|
|
|
char uuid[16];
|
1999-07-03 05:59:42 +04:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (strlen(s) != 36 ||
|
|
|
|
s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-')
|
|
|
|
goto bad_uuid;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
return get_spec_by_x(UUID, uuid);
|
|
|
|
|
|
|
|
bad_uuid:
|
2000-07-27 06:45:55 +04:00
|
|
|
fprintf(stderr, _("WARNING: %s: bad UUID"), s);
|
1999-07-03 05:59:42 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
get_spec_by_volume_label(const char *s) {
|
|
|
|
return get_spec_by_x(VOL, s);
|
|
|
|
}
|
2000-07-27 06:45:55 +04:00
|
|
|
|
|
|
|
const char *
|
|
|
|
get_volume_label_by_spec(const char *spec) {
|
|
|
|
struct uuidCache_s *uc;
|
|
|
|
|
|
|
|
uuidcache_init();
|
|
|
|
uc = uuidCache;
|
|
|
|
|
|
|
|
while(uc) {
|
|
|
|
if (!strcmp(spec, uc->device))
|
|
|
|
return uc->label;
|
|
|
|
uc = uc->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|