]>
git.wh0rd.org - dump.git/blob - compat/lib/bylabel.c
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 <sys/param.h>
20 #include <linux/ext2_fs.h>
21 #include <ext2fs/ext2fs.h>
24 #define PROC_PARTITIONS "/proc/partitions"
25 #define DEVLABELDIR "/dev"
27 void msg
__P((const char *fmt
, ...));
29 static struct uuidCache_s
{
30 struct uuidCache_s
*next
;
36 /* for now, only ext2 is supported */
38 get_label_uuid(const char *device
, char **label
, char *uuid
) {
40 /* start with a test for ext2, taken from mount_guess_fstype */
41 /* should merge these later */
43 struct ext2_super_block e2sb
;
45 fd
= open(device
, O_RDONLY
);
49 if (lseek(fd
, 1024, SEEK_SET
) != 1024
50 || read(fd
, (char *) &e2sb
, sizeof(e2sb
)) != sizeof(e2sb
)
51 || (e2sb
.s_magic
!= EXT2_SUPER_MAGIC
)) {
58 /* superblock is ext2 - now what is its label? */
59 memcpy(uuid
, e2sb
.s_uuid
, sizeof(e2sb
.s_uuid
));
60 *label
= strdup(e2sb
.s_volume_name
);
66 uuidcache_addentry(char *device
, char *label
, char *uuid
) {
67 struct uuidCache_s
*last
;
70 last
= uuidCache
= malloc(sizeof(*uuidCache
));
72 for (last
= uuidCache
; last
->next
; last
= last
->next
) ;
73 last
->next
= malloc(sizeof(*uuidCache
));
77 last
->device
= device
;
79 memcpy(last
->uuid
, uuid
, sizeof(last
->uuid
));
83 uuidcache_init(void) {
87 static char ptname
[100];
89 char uuid
[16], *label
;
97 procpt
= fopen(PROC_PARTITIONS
, "r");
101 for (firstPass
= 1; firstPass
>= 0; firstPass
--) {
102 fseek(procpt
, 0, SEEK_SET
);
104 while (fgets(line
, sizeof(line
), procpt
)) {
105 if (sscanf (line
, " %d %d %d %[^\n ]",
106 &ma
, &mi
, &sz
, ptname
) != 4)
109 /* skip extended partitions (heuristic: size 1) */
113 /* look only at md devices on first pass */
114 handleOnFirst
= !strncmp(ptname
, "md", 2);
115 if (firstPass
!= handleOnFirst
)
118 /* skip entire disk (minor 0, 64, ... on ide;
120 /* heuristic: partition name ends in a digit */
122 for(s
= ptname
; *s
; s
++);
123 if (isdigit(s
[-1])) {
125 * Note: this is a heuristic only - there is no reason
126 * why these devices should live in /dev.
127 * Perhaps this directory should be specifiable by option.
128 * One might for example have /devlabel with links to /dev
129 * for the devices that may be accessed in this way.
130 * (This is useful, if the cdrom on /dev/hdc must not
133 sprintf(device
, "%s/%s", DEVLABELDIR
, ptname
);
134 if (!get_label_uuid(device
, &label
, uuid
))
135 uuidcache_addentry(strdup(device
), label
, uuid
);
147 get_spec_by_x(int n
, const char *t
) {
148 struct uuidCache_s
*uc
;
156 if (!memcmp(t
, uc
->uuid
, sizeof(uc
->uuid
)))
157 return strdup(uc
->device
);
160 if (!strcmp(t
, uc
->label
))
161 return strdup(uc
->device
);
174 return (c
- 'a' + 10);
176 return (c
- 'A' + 10);
180 get_spec_by_uuid(const char *s
) {
184 if (strlen(s
) != 36 ||
185 s
[8] != '-' || s
[13] != '-' || s
[18] != '-' || s
[23] != '-')
187 for (i
=0; i
<16; i
++) {
189 if (!isxdigit(s
[0]) || !isxdigit(s
[1]))
191 uuid
[i
] = ((fromhex(s
[0])<<4) | fromhex(s
[1]));
194 return get_spec_by_x(UUID
, uuid
);
197 msg("mount: bad UUID\n");
198 return NULL
; /* just for gcc */
202 get_spec_by_volume_label(const char *s
) {
203 return get_spec_by_x(VOL
, s
);
207 get_device_name(const char * item
) {
210 if (!strncmp(item
, "UUID=", 5)) {
211 rc
= get_spec_by_uuid(item
+5);
214 if (!strncmp(item
, "LABEL=", 6)) {
215 rc
= get_spec_by_volume_label(item
+6);