Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Support functions for mounting devices by label/uuid |
| 4 | * |
| 5 | * Copyright (C) 2006 by Jason Schoon <floydpink@gmail.com> |
| 6 | * Some portions cribbed from e2fsprogs, util-linux, dosfstools |
| 7 | * |
| 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 9 | */ |
| 10 | |
| 11 | #include "volume_id_internal.h" |
| 12 | |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 13 | //#define BLKGETSIZE64 _IOR(0x12,114,size_t) |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 14 | |
| 15 | static struct uuidCache_s { |
| 16 | struct uuidCache_s *next; |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 17 | // int major, minor; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 18 | char *device; |
| 19 | char *label; |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 20 | char *uc_uuid; /* prefix makes it easier to grep for */ |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 21 | } *uuidCache; |
| 22 | |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 23 | /* Returns !0 on error. |
| 24 | * Otherwise, returns malloc'ed strings for label and uuid |
Denis Vlasenko | 582dff0 | 2008-10-19 19:36:30 +0000 | [diff] [blame] | 25 | * (and they can't be NULL, although they can be ""). |
| 26 | * NB: closes fd. */ |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 27 | static int |
Denis Vlasenko | 582dff0 | 2008-10-19 19:36:30 +0000 | [diff] [blame] | 28 | get_label_uuid(int fd, char **label, char **uuid) |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 29 | { |
| 30 | int rv = 1; |
| 31 | uint64_t size; |
| 32 | struct volume_id *vid; |
| 33 | |
Denis Vlasenko | 582dff0 | 2008-10-19 19:36:30 +0000 | [diff] [blame] | 34 | /* fd is owned by vid now */ |
| 35 | vid = volume_id_open_node(fd); |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 36 | |
Denis Vlasenko | 582dff0 | 2008-10-19 19:36:30 +0000 | [diff] [blame] | 37 | if (ioctl(/*vid->*/fd, BLKGETSIZE64, &size) != 0) |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 38 | size = 0; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 39 | |
Denis Vlasenko | 28ea429 | 2009-02-15 05:51:19 +0000 | [diff] [blame] | 40 | if (volume_id_probe_all(vid, /*0,*/ size) != 0) |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 41 | goto ret; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 42 | |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 43 | if (vid->label[0] != '\0' || vid->uuid[0] != '\0') { |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 44 | *label = xstrndup(vid->label, sizeof(vid->label)); |
| 45 | *uuid = xstrndup(vid->uuid, sizeof(vid->uuid)); |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 46 | dbg("found label '%s', uuid '%s' on %s", *label, *uuid, device); |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 47 | rv = 0; |
| 48 | } |
| 49 | ret: |
Denis Vlasenko | 582dff0 | 2008-10-19 19:36:30 +0000 | [diff] [blame] | 50 | free_volume_id(vid); /* also closes fd */ |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 51 | return rv; |
| 52 | } |
| 53 | |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 54 | /* NB: we take ownership of (malloc'ed) label and uuid */ |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 55 | static void |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 56 | uuidcache_addentry(char *device, /*int major, int minor,*/ char *label, char *uuid) |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 57 | { |
| 58 | struct uuidCache_s *last; |
Denis Vlasenko | 42cc304 | 2008-03-24 02:05:58 +0000 | [diff] [blame] | 59 | |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 60 | if (!uuidCache) { |
| 61 | last = uuidCache = xzalloc(sizeof(*uuidCache)); |
| 62 | } else { |
| 63 | for (last = uuidCache; last->next; last = last->next) |
| 64 | continue; |
| 65 | last->next = xzalloc(sizeof(*uuidCache)); |
| 66 | last = last->next; |
| 67 | } |
| 68 | /*last->next = NULL; - xzalloc did it*/ |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 69 | // last->major = major; |
| 70 | // last->minor = minor; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 71 | last->device = device; |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 72 | last->label = label; |
| 73 | last->uc_uuid = uuid; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 76 | /* If get_label_uuid() on device_name returns success, |
| 77 | * add a cache entry for this device. |
| 78 | * If device node does not exist, it will be temporarily created. */ |
Denis Vlasenko | 582dff0 | 2008-10-19 19:36:30 +0000 | [diff] [blame] | 79 | static int FAST_FUNC |
| 80 | uuidcache_check_device(const char *device, |
| 81 | struct stat *statbuf, |
| 82 | void *userData UNUSED_PARAM, |
| 83 | int depth UNUSED_PARAM) |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 84 | { |
Denis Vlasenko | 582dff0 | 2008-10-19 19:36:30 +0000 | [diff] [blame] | 85 | char *uuid = uuid; /* for compiler */ |
| 86 | char *label = label; |
| 87 | int fd; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 88 | |
Denis Vlasenko | 582dff0 | 2008-10-19 19:36:30 +0000 | [diff] [blame] | 89 | if (!S_ISBLK(statbuf->st_mode)) |
| 90 | return TRUE; |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 91 | |
Denis Vlasenko | 582dff0 | 2008-10-19 19:36:30 +0000 | [diff] [blame] | 92 | fd = open(device, O_RDONLY); |
| 93 | if (fd < 0) |
| 94 | return TRUE; |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 95 | |
Denis Vlasenko | 582dff0 | 2008-10-19 19:36:30 +0000 | [diff] [blame] | 96 | /* get_label_uuid() closes fd in all cases (success & failure) */ |
| 97 | if (get_label_uuid(fd, &label, &uuid) == 0) { |
| 98 | /* uuidcache_addentry() takes ownership of all three params */ |
| 99 | uuidcache_addentry(xstrdup(device), /*ma, mi,*/ label, uuid); |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 100 | } |
Denis Vlasenko | 582dff0 | 2008-10-19 19:36:30 +0000 | [diff] [blame] | 101 | return TRUE; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static void |
| 105 | uuidcache_init(void) |
| 106 | { |
| 107 | if (uuidCache) |
| 108 | return; |
| 109 | |
Denis Vlasenko | 802a7be | 2008-10-19 19:54:49 +0000 | [diff] [blame] | 110 | /* We were scanning /proc/partitions |
| 111 | * and /proc/sys/dev/cdrom/info here. |
| 112 | * Missed volume managers. I see that "standard" blkid uses these: |
| 113 | * /dev/mapper/control |
| 114 | * /proc/devices |
| 115 | * /proc/evms/volumes |
| 116 | * /proc/lvm/VGs |
| 117 | * This is unacceptably complex. Let's just scan /dev. |
| 118 | * (Maybe add scanning of /sys/block/XXX/dev for devices |
| 119 | * somehow not having their /dev/XXX entries created?) */ |
| 120 | |
Denis Vlasenko | 582dff0 | 2008-10-19 19:36:30 +0000 | [diff] [blame] | 121 | recursive_action("/dev", ACTION_RECURSE, |
| 122 | uuidcache_check_device, /* file_action */ |
| 123 | NULL, /* dir_action */ |
| 124 | NULL, /* userData */ |
| 125 | 0 /* depth */); |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | #define UUID 1 |
| 129 | #define VOL 2 |
| 130 | |
| 131 | #ifdef UNUSED |
| 132 | static char * |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 133 | get_spec_by_x(int n, const char *t, int *majorPtr, int *minorPtr) |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 134 | { |
| 135 | struct uuidCache_s *uc; |
| 136 | |
| 137 | uuidcache_init(); |
| 138 | uc = uuidCache; |
| 139 | |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 140 | while (uc) { |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 141 | switch (n) { |
| 142 | case UUID: |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 143 | if (strcmp(t, uc->uc_uuid) == 0) { |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 144 | *majorPtr = uc->major; |
| 145 | *minorPtr = uc->minor; |
| 146 | return uc->device; |
| 147 | } |
| 148 | break; |
| 149 | case VOL: |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 150 | if (strcmp(t, uc->label) == 0) { |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 151 | *majorPtr = uc->major; |
| 152 | *minorPtr = uc->minor; |
| 153 | return uc->device; |
| 154 | } |
| 155 | break; |
| 156 | } |
| 157 | uc = uc->next; |
| 158 | } |
| 159 | return NULL; |
| 160 | } |
| 161 | |
| 162 | static unsigned char |
| 163 | fromhex(char c) |
| 164 | { |
| 165 | if (isdigit(c)) |
| 166 | return (c - '0'); |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 167 | return ((c|0x20) - 'a' + 10); |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | static char * |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 171 | get_spec_by_uuid(const char *s, int *major, int *minor) |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 172 | { |
| 173 | unsigned char uuid[16]; |
| 174 | int i; |
| 175 | |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 176 | if (strlen(s) != 36 || s[8] != '-' || s[13] != '-' |
| 177 | || s[18] != '-' || s[23] != '-' |
| 178 | ) { |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 179 | goto bad_uuid; |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 180 | } |
| 181 | for (i = 0; i < 16; i++) { |
| 182 | if (*s == '-') |
| 183 | s++; |
| 184 | if (!isxdigit(s[0]) || !isxdigit(s[1])) |
| 185 | goto bad_uuid; |
| 186 | uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1])); |
| 187 | s += 2; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 188 | } |
| 189 | return get_spec_by_x(UUID, (char *)uuid, major, minor); |
| 190 | |
| 191 | bad_uuid: |
| 192 | fprintf(stderr, _("mount: bad UUID")); |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static char * |
| 197 | get_spec_by_volume_label(const char *s, int *major, int *minor) |
| 198 | { |
| 199 | return get_spec_by_x(VOL, s, major, minor); |
| 200 | } |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 201 | #endif // UNUSED |
| 202 | |
Denis Vlasenko | 1e19afd | 2008-10-12 11:20:08 +0000 | [diff] [blame] | 203 | /* Used by blkid */ |
| 204 | void display_uuid_cache(void) |
| 205 | { |
| 206 | struct uuidCache_s *u; |
| 207 | |
| 208 | uuidcache_init(); |
| 209 | u = uuidCache; |
| 210 | while (u) { |
| 211 | printf("%s:", u->device); |
| 212 | if (u->label[0]) |
| 213 | printf(" LABEL=\"%s\"", u->label); |
| 214 | if (u->uc_uuid[0]) |
| 215 | printf(" UUID=\"%s\"", u->uc_uuid); |
| 216 | bb_putchar('\n'); |
| 217 | u = u->next; |
| 218 | } |
| 219 | } |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 220 | |
| 221 | /* Used by mount and findfs */ |
| 222 | |
| 223 | char *get_devname_from_label(const char *spec) |
| 224 | { |
| 225 | struct uuidCache_s *uc; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 226 | |
| 227 | uuidcache_init(); |
| 228 | uc = uuidCache; |
| 229 | while (uc) { |
Denis Vlasenko | 9983d80 | 2009-03-31 19:47:34 +0000 | [diff] [blame] | 230 | if (uc->label[0] && strcmp(spec, uc->label) == 0) { |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 231 | return xstrdup(uc->device); |
| 232 | } |
| 233 | uc = uc->next; |
| 234 | } |
| 235 | return NULL; |
| 236 | } |
| 237 | |
| 238 | char *get_devname_from_uuid(const char *spec) |
| 239 | { |
| 240 | struct uuidCache_s *uc; |
| 241 | |
| 242 | uuidcache_init(); |
| 243 | uc = uuidCache; |
| 244 | while (uc) { |
Denis Vlasenko | d25c33f | 2008-03-17 09:25:05 +0000 | [diff] [blame] | 245 | /* case of hex numbers doesn't matter */ |
| 246 | if (strcasecmp(spec, uc->uc_uuid) == 0) { |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 247 | return xstrdup(uc->device); |
| 248 | } |
| 249 | uc = uc->next; |
| 250 | } |
| 251 | return NULL; |
| 252 | } |