2 * mount_by_label.c - aeb
4 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
5 * - added Native Language Support
6 * 2000-01-20 James Antill <james@and.org>
7 * - Added error message if /proc/partitions cannot be opened
8 * 2000-05-09 Erik Troan <ewt@redhat.com>
9 * - Added cache for UUID and disk labels
10 * Wed Aug 16 2000 Erik Troan <ewt@redhat.com>
11 * - Ported to dump/restore
15 #include <compatlfs.h>
16 #include <sys/types.h>
18 #include <sys/param.h>
24 #include <sys/cdefs.h>
27 #define PROC_PARTITIONS "/proc/partitions"
28 #define DEVLABELDIR "/dev"
30 #define EXT2_SUPER_OFFSET 1024
31 #define EXT2_SUPER_SIZE sizeof(struct ext2_super_block)
32 #define EXT2_SUPER_MAGIC 0xEF53
36 struct ext2_super_block {
37 unsigned char s_dummy1[56];
38 unsigned char s_magic[2];
39 unsigned char s_dummy2[46];
40 unsigned char s_uuid[16];
41 unsigned char s_volume_name[VOLNAMSZ];
43 #define ext2magic(s) ((unsigned int) s.s_magic[0] + (((unsigned int) s.s_magic[1]) << 8))
45 void msg __P((const char *fmt, ...));
47 static struct uuidCache_s {
48 struct uuidCache_s *next;
54 /* for now, only ext2 is supported */
56 get_label_uuid(const char *device, char **label, char *uuid) {
58 /* start with a test for ext2, taken from mount_guess_fstype */
59 /* should merge these later */
61 struct ext2_super_block e2sb;
63 fd = OPEN(device, O_RDONLY);
67 if (LSEEK(fd, EXT2_SUPER_OFFSET, SEEK_SET) != EXT2_SUPER_OFFSET ||
68 read(fd, (char *) &e2sb, EXT2_SUPER_SIZE) != EXT2_SUPER_SIZE ||
69 ext2magic(e2sb) != EXT2_SUPER_MAGIC) {
76 /* superblock is ext2 - now what is its label? */
77 memcpy(uuid, e2sb.s_uuid, sizeof(e2sb.s_uuid));
78 *label = malloc(VOLNAMSZ + 1);
79 strncpy(*label, e2sb.s_volume_name, VOLNAMSZ);
80 (*label)[VOLNAMSZ] = 0;
86 uuidcache_addentry(char *device, char *label, char *uuid) {
87 struct uuidCache_s *last;
90 last = uuidCache = (struct uuidCache_s *)malloc(sizeof(*uuidCache));
92 for (last = uuidCache; last->next; last = last->next) ;
93 last->next = (struct uuidCache_s *)malloc(sizeof(*uuidCache));
97 last->device = device;
99 memcpy(last->uuid, uuid, sizeof(last->uuid));
103 uuidcache_init(void) {
107 static char ptname[100];
109 char uuid[16], *label;
117 procpt = fopen(PROC_PARTITIONS, "r");
121 for (firstPass = 1; firstPass >= 0; firstPass--) {
122 fseek(procpt, 0, SEEK_SET);
124 while (fgets(line, sizeof(line), procpt)) {
125 if (sscanf (line, " %d %d %d %[^\n ]",
126 &ma, &mi, &sz, ptname) != 4)
129 /* skip extended partitions (heuristic: size 1) */
133 /* look only at md devices on first pass */
134 handleOnFirst = !strncmp(ptname, "md", 2);
135 if (firstPass != handleOnFirst)
138 /* skip entire disk (minor 0, 64, ... on ide;
140 /* heuristic: partition name ends in a digit */
142 for(s = ptname; *s; s++);
143 if (isdigit(s[-1])) {
145 * Note: this is a heuristic only - there is no reason
146 * why these devices should live in /dev.
147 * Perhaps this directory should be specifiable by option.
148 * One might for example have /devlabel with links to /dev
149 * for the devices that may be accessed in this way.
150 * (This is useful, if the cdrom on /dev/hdc must not
153 sprintf(device, "%s/%s", DEVLABELDIR, ptname);
154 if (!get_label_uuid(device, &label, uuid))
155 uuidcache_addentry(strdup(device), label, uuid);
167 get_spec_by_x(int n, const char *t) {
168 struct uuidCache_s *uc;
176 if (!memcmp(t, uc->uuid, sizeof(uc->uuid)))
177 return strdup(uc->device);
180 if (!strcmp(t, uc->label))
181 return strdup(uc->device);
194 return (c - 'a' + 10);
196 return (c - 'A' + 10);
200 get_spec_by_uuid(const char *s) {
204 if (strlen(s) != 36 ||
205 s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-')
207 for (i=0; i<16; i++) {
209 if (!isxdigit(s[0]) || !isxdigit(s[1]))
211 uuid[i] = ((fromhex(s[0])<<4) | fromhex(s[1]));
214 return get_spec_by_x(UUID, uuid);
217 msg("mount: bad UUID\n");
218 return NULL; /* just for gcc */
222 get_spec_by_volume_label(const char *s) {
223 return get_spec_by_x(VOL, s);
227 get_device_name(const char * item) {
230 if (!strncmp(item, "UUID=", 5)) {
231 rc = get_spec_by_uuid(item+5);
234 if (!strncmp(item, "LABEL=", 6)) {
235 rc = get_spec_by_volume_label(item+6);
245 get_device_label(const char * spec) {
246 struct uuidCache_s *uc;
252 if (!strcmp(spec, uc->device))
253 return uc->label[0] == '\0' ? NULL : strdup(uc->label);