]>
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 <compatlfs.h>
16 #include <sys/types.h>
18 #include <sys/param.h>
24 #include <sys/cdefs.h>
29 #define PROC_PARTITIONS "/proc/partitions"
30 #define DEVLABELDIR "/dev"
32 #define EXT2_SUPER_OFFSET 1024
33 #define EXT2_SUPER_SIZE sizeof(struct ext2_super_block)
34 #define EXT2_SUPER_MAGIC 0xEF53
38 struct ext2_super_block
{
39 unsigned char s_dummy1
[56];
40 unsigned char s_magic
[2];
41 unsigned char s_dummy2
[46];
42 unsigned char s_uuid
[16];
43 unsigned char s_volume_name
[VOLNAMSZ
];
45 #define ext2magic(s) ((unsigned int) s.s_magic[0] + (((unsigned int) s.s_magic[1]) << 8))
47 void msg
__P((const char *fmt
, ...));
49 static struct uuidCache_s
{
50 struct uuidCache_s
*next
;
56 /* for now, only ext2 is supported */
58 get_label_uuid(const char *device
, char **label
, char *uuid
) {
60 /* start with a test for ext2, taken from mount_guess_fstype */
61 /* should merge these later */
63 struct ext2_super_block e2sb
;
65 fd
= OPEN(device
, O_RDONLY
);
69 if (LSEEK(fd
, EXT2_SUPER_OFFSET
, SEEK_SET
) != EXT2_SUPER_OFFSET
||
70 read(fd
, (char *) &e2sb
, EXT2_SUPER_SIZE
) != EXT2_SUPER_SIZE
||
71 ext2magic(e2sb
) != EXT2_SUPER_MAGIC
) {
78 /* superblock is ext2 - now what is its label? */
79 memcpy(uuid
, e2sb
.s_uuid
, sizeof(e2sb
.s_uuid
));
80 *label
= malloc(VOLNAMSZ
+ 1);
81 strncpy(*label
, e2sb
.s_volume_name
, VOLNAMSZ
);
82 (*label
)[VOLNAMSZ
] = 0;
88 uuidcache_addentry(char *device
, char *label
, char *uuid
) {
89 struct uuidCache_s
*last
;
92 last
= uuidCache
= (struct uuidCache_s
*)malloc(sizeof(*uuidCache
));
94 for (last
= uuidCache
; last
->next
; last
= last
->next
) ;
95 last
->next
= (struct uuidCache_s
*)malloc(sizeof(*uuidCache
));
99 last
->device
= device
;
101 memcpy(last
->uuid
, uuid
, sizeof(last
->uuid
));
105 uuidcache_init(void) {
109 static char ptname
[100];
111 char uuid
[16], *label
;
119 procpt
= fopen(PROC_PARTITIONS
, "r");
123 for (firstPass
= 1; firstPass
>= 0; firstPass
--) {
124 fseek(procpt
, 0, SEEK_SET
);
126 while (fgets(line
, sizeof(line
), procpt
)) {
127 if (sscanf (line
, " %d %d %d %[^\n ]",
128 &ma
, &mi
, &sz
, ptname
) != 4)
131 /* skip extended partitions (heuristic: size 1) */
135 /* look only at md devices on first pass */
136 handleOnFirst
= !strncmp(ptname
, "md", 2);
137 if (firstPass
!= handleOnFirst
)
140 /* skip entire disk (minor 0, 64, ... on ide;
142 /* heuristic: partition name ends in a digit */
144 for(s
= ptname
; *s
; s
++);
145 if (isdigit(s
[-1])) {
147 * Note: this is a heuristic only - there is no reason
148 * why these devices should live in /dev.
149 * Perhaps this directory should be specifiable by option.
150 * One might for example have /devlabel with links to /dev
151 * for the devices that may be accessed in this way.
152 * (This is useful, if the cdrom on /dev/hdc must not
155 sprintf(device
, "%s/%s", DEVLABELDIR
, ptname
);
156 if (!get_label_uuid(device
, &label
, uuid
))
157 uuidcache_addentry(strdup(device
), label
, uuid
);
169 get_spec_by_x(int n
, const char *t
) {
170 struct uuidCache_s
*uc
;
178 if (!memcmp(t
, uc
->uuid
, sizeof(uc
->uuid
)))
179 return strdup(uc
->device
);
182 if (!strcmp(t
, uc
->label
))
183 return strdup(uc
->device
);
196 return (c
- 'a' + 10);
198 return (c
- 'A' + 10);
202 get_spec_by_uuid(const char *s
) {
206 if (strlen(s
) != 36 ||
207 s
[8] != '-' || s
[13] != '-' || s
[18] != '-' || s
[23] != '-')
209 for (i
=0; i
<16; i
++) {
211 if (!isxdigit(s
[0]) || !isxdigit(s
[1]))
213 uuid
[i
] = ((fromhex(s
[0])<<4) | fromhex(s
[1]));
216 return get_spec_by_x(UUID
, uuid
);
219 msg("mount: bad UUID\n");
220 return NULL
; /* just for gcc */
224 get_spec_by_volume_label(const char *s
) {
225 return get_spec_by_x(VOL
, s
);
229 get_device_name(const char * item
) {
232 if (!strncmp(item
, "UUID=", 5)) {
233 rc
= get_spec_by_uuid(item
+5);
236 if (!strncmp(item
, "LABEL=", 6)) {
237 rc
= get_spec_by_volume_label(item
+6);
247 get_device_label(const char * spec
) {
248 struct uuidCache_s
*uc
;
254 if (!strcmp(spec
, uc
->device
))
255 return uc
->label
[0] == '\0' ? NULL
: strdup(uc
->label
);
261 #endif /* !HAVE_BLKID */