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