]>
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
16 #include <sys/param.h>
24 #define PROC_PARTITIONS "/proc/partitions"
25 #define DEVLABELDIR "/dev"
27 #define EXT2_SUPER_OFFSET 1024
28 #define EXT2_SUPER_SIZE sizeof(struct ext2_super_block)
29 #define EXT2_SUPER_MAGIC 0xEF53
31 struct ext2_super_block
{
32 unsigned char s_dummy1
[56];
33 unsigned char s_magic
[2];
34 unsigned char s_dummy2
[46];
35 unsigned char s_uuid
[16];
36 unsigned char s_volume_name
[16];
38 #define ext2magic(s) ((unsigned int) s.s_magic[0] + (((unsigned int) s.s_magic[1]) << 8))
40 void msg
__P((const char *fmt
, ...));
42 static struct uuidCache_s
{
43 struct uuidCache_s
*next
;
49 /* for now, only ext2 is supported */
51 get_label_uuid(const char *device
, char **label
, char *uuid
) {
53 /* start with a test for ext2, taken from mount_guess_fstype */
54 /* should merge these later */
56 struct ext2_super_block e2sb
;
58 fd
= open(device
, O_RDONLY
);
62 if (lseek(fd
, EXT2_SUPER_OFFSET
, SEEK_SET
) != EXT2_SUPER_OFFSET
||
63 read(fd
, (char *) &e2sb
, EXT2_SUPER_SIZE
) != EXT2_SUPER_SIZE
||
64 ext2magic(e2sb
) != EXT2_SUPER_MAGIC
) {
71 /* superblock is ext2 - now what is its label? */
72 memcpy(uuid
, e2sb
.s_uuid
, sizeof(e2sb
.s_uuid
));
73 *label
= strdup(e2sb
.s_volume_name
);
79 uuidcache_addentry(char *device
, char *label
, char *uuid
) {
80 struct uuidCache_s
*last
;
83 last
= uuidCache
= (struct uuidCache_s
*)malloc(sizeof(*uuidCache
));
85 for (last
= uuidCache
; last
->next
; last
= last
->next
) ;
86 last
->next
= (struct uuidCache_s
*)malloc(sizeof(*uuidCache
));
90 last
->device
= device
;
92 memcpy(last
->uuid
, uuid
, sizeof(last
->uuid
));
96 uuidcache_init(void) {
100 static char ptname
[100];
102 char uuid
[16], *label
;
110 procpt
= fopen(PROC_PARTITIONS
, "r");
114 for (firstPass
= 1; firstPass
>= 0; firstPass
--) {
115 fseek(procpt
, 0, SEEK_SET
);
117 while (fgets(line
, sizeof(line
), procpt
)) {
118 if (sscanf (line
, " %d %d %d %[^\n ]",
119 &ma
, &mi
, &sz
, ptname
) != 4)
122 /* skip extended partitions (heuristic: size 1) */
126 /* look only at md devices on first pass */
127 handleOnFirst
= !strncmp(ptname
, "md", 2);
128 if (firstPass
!= handleOnFirst
)
131 /* skip entire disk (minor 0, 64, ... on ide;
133 /* heuristic: partition name ends in a digit */
135 for(s
= ptname
; *s
; s
++);
136 if (isdigit(s
[-1])) {
138 * Note: this is a heuristic only - there is no reason
139 * why these devices should live in /dev.
140 * Perhaps this directory should be specifiable by option.
141 * One might for example have /devlabel with links to /dev
142 * for the devices that may be accessed in this way.
143 * (This is useful, if the cdrom on /dev/hdc must not
146 sprintf(device
, "%s/%s", DEVLABELDIR
, ptname
);
147 if (!get_label_uuid(device
, &label
, uuid
))
148 uuidcache_addentry(strdup(device
), label
, uuid
);
160 get_spec_by_x(int n
, const char *t
) {
161 struct uuidCache_s
*uc
;
169 if (!memcmp(t
, uc
->uuid
, sizeof(uc
->uuid
)))
170 return strdup(uc
->device
);
173 if (!strcmp(t
, uc
->label
))
174 return strdup(uc
->device
);
187 return (c
- 'a' + 10);
189 return (c
- 'A' + 10);
193 get_spec_by_uuid(const char *s
) {
197 if (strlen(s
) != 36 ||
198 s
[8] != '-' || s
[13] != '-' || s
[18] != '-' || s
[23] != '-')
200 for (i
=0; i
<16; i
++) {
202 if (!isxdigit(s
[0]) || !isxdigit(s
[1]))
204 uuid
[i
] = ((fromhex(s
[0])<<4) | fromhex(s
[1]));
207 return get_spec_by_x(UUID
, uuid
);
210 msg("mount: bad UUID\n");
211 return NULL
; /* just for gcc */
215 get_spec_by_volume_label(const char *s
) {
216 return get_spec_by_x(VOL
, s
);
220 get_device_name(const char * item
) {
223 if (!strncmp(item
, "UUID=", 5)) {
224 rc
= get_spec_by_uuid(item
+5);
227 if (!strncmp(item
, "LABEL=", 6)) {
228 rc
= get_spec_by_volume_label(item
+6);
238 get_device_label(const char * spec
) {
239 struct uuidCache_s
*uc
;
245 if (!strcmp(spec
, uc
->device
))
246 return uc
->label
[0] == '\0' ? NULL
: strdup(uc
->label
);