2 * volume_id - reads volume label and uuid
4 * Copyright (C) 2005-2007 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation version 2 of the License.
28 #include "libvolume_id.h"
31 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
34 volume_id_probe_fn_t prober;
38 static const struct prober prober_raid[] = {
39 { volume_id_probe_linux_raid, { "linux_raid", } },
40 { volume_id_probe_ddf_raid, { "ddf_raid", } },
41 { volume_id_probe_intel_software_raid, { "isw_raid", } },
42 { volume_id_probe_lsi_mega_raid, { "lsi_mega_raid", } },
43 { volume_id_probe_via_raid, { "via_raid", } },
44 { volume_id_probe_silicon_medley_raid, { "silicon_medley_raid", } },
45 { volume_id_probe_nvidia_raid, { "nvidia_raid", } },
46 { volume_id_probe_promise_fasttrack_raid, { "promise_fasttrack_raid", } },
47 { volume_id_probe_highpoint_45x_raid, { "highpoint_raid", } },
48 { volume_id_probe_adaptec_raid, { "adaptec_raid", } },
49 { volume_id_probe_jmicron_raid, { "jmicron_raid", } },
50 { volume_id_probe_lvm1, { "lvm1", } },
51 { volume_id_probe_lvm2, { "lvm2", } },
52 { volume_id_probe_highpoint_37x_raid, { "highpoint_raid", } },
55 static const struct prober prober_filesystem[] = {
56 { volume_id_probe_vfat, { "vfat", } },
57 { volume_id_probe_linux_swap, { "swap", } },
58 { volume_id_probe_luks, { "luks", } },
59 { volume_id_probe_xfs, { "xfs", } },
60 { volume_id_probe_ext, { "ext2", "ext3", "jbd", } },
61 { volume_id_probe_reiserfs, { "reiserfs", "reiser4", } },
62 { volume_id_probe_jfs, { "jfs", } },
63 { volume_id_probe_udf, { "udf", } },
64 { volume_id_probe_iso9660, { "iso9660", } },
65 { volume_id_probe_hfs_hfsplus, { "hfs", "hfsplus", } },
66 { volume_id_probe_ufs, { "ufs", } },
67 { volume_id_probe_ntfs, { "ntfs", } },
68 { volume_id_probe_cramfs, { "cramfs", } },
69 { volume_id_probe_romfs, { "romfs", } },
70 { volume_id_probe_hpfs, { "hpfs", } },
71 { volume_id_probe_sysv, { "sysv", "xenix", } },
72 { volume_id_probe_minix, { "minix", } },
73 { volume_id_probe_gfs, { "gfs", } },
74 { volume_id_probe_gfs2, { "gfs2", } },
75 { volume_id_probe_ocfs1, { "ocfs1", } },
76 { volume_id_probe_ocfs2, { "ocfs2", } },
77 { volume_id_probe_vxfs, { "vxfs", } },
78 { volume_id_probe_squashfs, { "squashfs", } },
79 { volume_id_probe_netware, { "netware", } },
80 { volume_id_probe_oracleasm, { "oracleasm", } },
83 /* the user can overwrite this log function */
84 static void default_log(int priority, const char *file, int line, const char *format, ...)
89 volume_id_log_fn_t volume_id_log_fn = default_log;
92 * volume_id_get_prober_by_type:
95 * Lookup the probing function for a specific type.
97 * Returns: The probing function for the given type, #NULL otherwise.
99 const volume_id_probe_fn_t *volume_id_get_prober_by_type(const char *type)
106 for (p = 0; p < ARRAY_SIZE(prober_raid); p++)
107 for (n = 0; prober_raid[p].name[n] != NULL; n++)
108 if (strcmp(type, prober_raid[p].name[n]) == 0)
109 return &prober_raid[p].prober;
110 for (p = 0; p < ARRAY_SIZE(prober_filesystem); p++)
111 for (n = 0; prober_filesystem[p].name[n] != NULL; n++)
112 if (strcmp(type, prober_filesystem[p].name[n]) == 0)
113 return &prober_filesystem[p].prober;
118 * volume_id_get_label:
119 * @id: Probing context.
120 * @label: Label string. Must not be freed by the caller.
122 * Get the label string after a successful probe. Unicode
123 * is translated to UTF-8.
125 * Returns: 1 if the value was set, 0 otherwise.
127 int volume_id_get_label(struct volume_id *id, const char **label)
133 if (id->usage_id == VOLUME_ID_UNUSED)
141 * volume_id_get_label_raw:
142 * @id: Probing context.
143 * @label: Label byte array. Must not be freed by the caller.
144 * @len: Length of raw label byte array.
146 * Get the raw label byte array after a successful probe. It may
147 * contain undecoded multibyte character streams.
149 * Returns: 1 if the value was set, 0 otherwise.
151 int volume_id_get_label_raw(struct volume_id *id, const uint8_t **label, size_t *len)
159 if (id->usage_id == VOLUME_ID_UNUSED)
162 *label = id->label_raw;
163 *len = id->label_raw_len;
168 * volume_id_get_uuid:
169 * @id: Probing context.
170 * @uuid: UUID string. Must not be freed by the caller.
172 * Get the raw UUID string after a successful probe.
174 * Returns: 1 if the value was set, 0 otherwise.
176 int volume_id_get_uuid(struct volume_id *id, const char **uuid)
182 if (id->usage_id == VOLUME_ID_UNUSED)
190 * volume_id_get_uuid_raw:
191 * @id: Probing context.
192 * @uuid: UUID byte array. Must not be freed by the caller.
193 * @len: Length of raw UUID byte array.
195 * Get the raw UUID byte array after a successful probe. It may
196 * contain unconverted endianes values.
198 * Returns: 1 if the value was set, 0 otherwise.
200 int volume_id_get_uuid_raw(struct volume_id *id, const uint8_t **uuid, size_t *len)
208 if (id->usage_id == VOLUME_ID_UNUSED)
211 *uuid = id->uuid_raw;
212 *len = id->uuid_raw_len;
217 * volume_id_get_usage:
218 * @id: Probing context.
219 * @usage: Usage string. Must not be freed by the caller.
221 * Get the usage string after a successful probe.
223 * Returns: 1 if the value was set, 0 otherwise.
225 int volume_id_get_usage(struct volume_id *id, const char **usage)
231 if (id->usage_id == VOLUME_ID_UNUSED)
239 * volume_id_get_type:
240 * @id: Probing context
241 * @type: Type string. Must not be freed by the caller.
243 * Get the type string after a successful probe.
245 * Returns: 1 if the value was set, 0 otherwise.
247 int volume_id_get_type(struct volume_id *id, const char **type)
253 if (id->usage_id == VOLUME_ID_UNUSED)
261 * volume_id_get_type_version:
262 * @id: Probing context.
263 * @type_version: Type version string. Must not be freed by the caller.
265 * Get the Type version string after a successful probe.
267 * Returns: 1 if the value was set, 0 otherwise.
269 int volume_id_get_type_version(struct volume_id *id, const char **type_version)
273 if (type_version == NULL)
275 if (id->usage_id == VOLUME_ID_UNUSED)
278 *type_version = id->type_version;
282 static int needs_encoding(const char c)
284 if ((c >= '0' && c <= '9') ||
285 (c >= 'A' && c <= 'Z') ||
286 (c >= 'a' && c <= 'z') ||
287 strchr(ALLOWED_CHARS, c))
293 * volume_id_encode_string:
294 * @str: Input string to be encoded.
295 * @str_enc: Target string to store the encoded input.
296 * @len: Location to store the encoded string. The target string,
297 * which may be four times as long as the input string.
299 * Encode all potentially unsafe characters of a string to the
300 * corresponding hex value prefixed by '\x'.
302 * Returns: 1 if the entire string was copied, 0 otherwise.
304 int volume_id_encode_string(const char *str, char *str_enc, size_t len)
308 if (str == NULL || str_enc == NULL || len == 0)
312 for (i = 0, j = 0; str[i] != '\0'; i++) {
315 seqlen = volume_id_utf8_encoded_valid_unichar(&str[i]);
317 memcpy(&str_enc[j], &str[i], seqlen);
320 } else if (str[i] == '\\' || needs_encoding(str[i])) {
321 sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]);
336 /* run only once into a timeout for unreadable devices */
337 static int device_is_readable(struct volume_id *id, uint64_t off)
339 if (volume_id_get_buffer(id, off, 0x200) != NULL)
345 * volume_id_probe_raid:
346 * @id: Probing context.
347 * @off: Probing offset relative to the start of the device.
348 * @size: Total size of the device.
350 * Probe device for all known raid signatures.
352 * Returns: 0 on successful probe, otherwise negative value.
354 int volume_id_probe_raid(struct volume_id *id, uint64_t off, uint64_t size)
361 if (!device_is_readable(id, off))
364 info("probing at offset 0x%llx, size 0x%llx\n",
365 (unsigned long long) off, (unsigned long long) size);
367 for (i = 0; i < ARRAY_SIZE(prober_raid); i++)
368 if (prober_raid[i].prober(id, off, size) == 0)
373 /* If recognized, we free the allocated buffers */
374 volume_id_free_buffer(id);
379 * volume_id_probe_filesystem:
380 * @id: Probing context.
381 * @off: Probing offset relative to the start of the device.
382 * @size: Total size of the device.
384 * Probe device for all known filesystem signatures.
386 * Returns: 0 on successful probe, otherwise negative value.
388 int volume_id_probe_filesystem(struct volume_id *id, uint64_t off, uint64_t size)
395 if (!device_is_readable(id, off))
398 info("probing at offset 0x%llx, size 0x%llx\n",
399 (unsigned long long) off, (unsigned long long) size);
401 for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++)
402 if (prober_filesystem[i].prober(id, off, size) == 0)
407 /* If recognized, we free the allocated buffers */
408 volume_id_free_buffer(id);
413 * volume_id_probe_all:
414 * @id: Probing context.
415 * @off: Probing offset relative to the start of the device.
416 * @size: Total size of the device.
418 * Probe device for all known raid and filesystem signatures.
420 * Returns: 0 on successful probe, otherwise negative value.
422 int volume_id_probe_all(struct volume_id *id, uint64_t off, uint64_t size)
427 if (!device_is_readable(id, off))
430 /* probe for raid first, because fs probes may be successful on raid members */
431 if (volume_id_probe_raid(id, off, size) == 0)
434 if (volume_id_probe_filesystem(id, off, size) == 0)
441 * volume_id_probe_raid:
442 * @all_probers_fn: prober function to called for all known probing routines.
443 * @id: Context passed to prober function.
444 * @off: Offset value passed to prober function.
445 * @size: Size value passed to prober function.
446 * @data: Arbitrary data passed to the prober function.
448 * Run a custom function for all known probing routines.
450 void volume_id_all_probers(all_probers_fn_t all_probers_fn,
451 struct volume_id *id, uint64_t off, uint64_t size,
456 if (all_probers_fn == NULL)
459 for (i = 0; i < ARRAY_SIZE(prober_raid); i++)
460 if (all_probers_fn(prober_raid[i].prober, id, off, size, data) != 0)
462 for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++)
463 if (all_probers_fn(prober_filesystem[i].prober, id, off, size, data) != 0)
471 * @id: Probing context.
472 * @fd: Open file descriptor of device to read from.
474 * Create the context for probing.
476 * Returns: Probing context, or #NULL on failure.
478 struct volume_id *volume_id_open_fd(int fd)
480 struct volume_id *id;
482 id = malloc(sizeof(struct volume_id));
485 memset(id, 0x00, sizeof(struct volume_id));
492 struct volume_id *volume_id_open_node(const char *path)
494 struct volume_id *id;
497 fd = open(path, O_RDONLY);
499 dbg("unable to open '%s'\n", path);
503 id = volume_id_open_fd(fd);
507 /* close fd on device close */
515 * @id: Probing context.
517 * Release probing context and free all associated data.
519 void volume_id_close(struct volume_id *id)
524 if (id->fd_close != 0)
527 volume_id_free_buffer(id);