]>
git.wh0rd.org - dump.git/blob - compat/lib/bylabel.c
c91457d3d232d7b47ff0209dc2a7c0531c79d4c6
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>
23 #define PROC_PARTITIONS "/proc/partitions"
24 #define DEVLABELDIR "/dev"
26 #define EXT2_SUPER_MAGIC 0xEF53
27 struct ext2_super_block
{
28 unsigned char s_dummy1
[56];
29 unsigned char s_magic
[2];
30 unsigned char s_dummy2
[46];
31 unsigned char s_uuid
[16];
32 unsigned char s_volume_name
[16];
34 #define ext2magic(s) ((unsigned int) s.s_magic[0] + (((unsigned int) s.s_magic[1]) << 8))
36 void msg
__P((const char *fmt
, ...));
38 static struct uuidCache_s
{
39 struct uuidCache_s
*next
;
45 /* for now, only ext2 is supported */
47 get_label_uuid(const char *device
, char **label
, char *uuid
) {
49 /* start with a test for ext2, taken from mount_guess_fstype */
50 /* should merge these later */
52 struct ext2_super_block e2sb
;
54 fd
= open(device
, O_RDONLY
);
58 if (lseek(fd
, 1024, SEEK_SET
) != 1024
59 || read(fd
, (char *) &e2sb
, sizeof(e2sb
)) != sizeof(e2sb
)
60 || (ext2magic(e2sb
) != EXT2_SUPER_MAGIC
)) {
67 /* superblock is ext2 - now what is its label? */
68 memcpy(uuid
, e2sb
.s_uuid
, sizeof(e2sb
.s_uuid
));
69 *label
= strdup(e2sb
.s_volume_name
);
75 uuidcache_addentry(char *device
, char *label
, char *uuid
) {
76 struct uuidCache_s
*last
;
79 last
= uuidCache
= (struct uuidCache_s
*)malloc(sizeof(*uuidCache
));
81 for (last
= uuidCache
; last
->next
; last
= last
->next
) ;
82 last
->next
= (struct uuidCache_s
*)malloc(sizeof(*uuidCache
));
86 last
->device
= device
;
88 memcpy(last
->uuid
, uuid
, sizeof(last
->uuid
));
92 uuidcache_init(void) {
96 static char ptname
[100];
98 char uuid
[16], *label
;
106 procpt
= fopen(PROC_PARTITIONS
, "r");
110 for (firstPass
= 1; firstPass
>= 0; firstPass
--) {
111 fseek(procpt
, 0, SEEK_SET
);
113 while (fgets(line
, sizeof(line
), procpt
)) {
114 if (sscanf (line
, " %d %d %d %[^\n ]",
115 &ma
, &mi
, &sz
, ptname
) != 4)
118 /* skip extended partitions (heuristic: size 1) */
122 /* look only at md devices on first pass */
123 handleOnFirst
= !strncmp(ptname
, "md", 2);
124 if (firstPass
!= handleOnFirst
)
127 /* skip entire disk (minor 0, 64, ... on ide;
129 /* heuristic: partition name ends in a digit */
131 for(s
= ptname
; *s
; s
++);
132 if (isdigit(s
[-1])) {
134 * Note: this is a heuristic only - there is no reason
135 * why these devices should live in /dev.
136 * Perhaps this directory should be specifiable by option.
137 * One might for example have /devlabel with links to /dev
138 * for the devices that may be accessed in this way.
139 * (This is useful, if the cdrom on /dev/hdc must not
142 sprintf(device
, "%s/%s", DEVLABELDIR
, ptname
);
143 if (!get_label_uuid(device
, &label
, uuid
))
144 uuidcache_addentry(strdup(device
), label
, uuid
);
156 get_spec_by_x(int n
, const char *t
) {
157 struct uuidCache_s
*uc
;
165 if (!memcmp(t
, uc
->uuid
, sizeof(uc
->uuid
)))
166 return strdup(uc
->device
);
169 if (!strcmp(t
, uc
->label
))
170 return strdup(uc
->device
);
183 return (c
- 'a' + 10);
185 return (c
- 'A' + 10);
189 get_spec_by_uuid(const char *s
) {
193 if (strlen(s
) != 36 ||
194 s
[8] != '-' || s
[13] != '-' || s
[18] != '-' || s
[23] != '-')
196 for (i
=0; i
<16; i
++) {
198 if (!isxdigit(s
[0]) || !isxdigit(s
[1]))
200 uuid
[i
] = ((fromhex(s
[0])<<4) | fromhex(s
[1]));
203 return get_spec_by_x(UUID
, uuid
);
206 msg("mount: bad UUID\n");
207 return NULL
; /* just for gcc */
211 get_spec_by_volume_label(const char *s
) {
212 return get_spec_by_x(VOL
, s
);
216 get_device_name(const char * item
) {
219 if (!strncmp(item
, "UUID=", 5)) {
220 rc
= get_spec_by_uuid(item
+5);
223 if (!strncmp(item
, "LABEL=", 6)) {
224 rc
= get_spec_by_volume_label(item
+6);