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"
29 #include "libvolume_id-private.h"
32 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
35 volume_id_probe_fn_t prober;
39 static const struct prober prober_raid[] = {
40 { volume_id_probe_linux_raid, { "linux_raid", } },
41 { volume_id_probe_ddf_raid, { "ddf_raid", } },
42 { volume_id_probe_intel_software_raid, { "isw_raid", } },
43 { volume_id_probe_lsi_mega_raid, { "lsi_mega_raid", } },
44 { volume_id_probe_via_raid, { "via_raid", } },
45 { volume_id_probe_silicon_medley_raid, { "silicon_medley_raid", } },
46 { volume_id_probe_nvidia_raid, { "nvidia_raid", } },
47 { volume_id_probe_promise_fasttrack_raid, { "promise_fasttrack_raid", } },
48 { volume_id_probe_highpoint_45x_raid, { "highpoint_raid", } },
49 { volume_id_probe_adaptec_raid, { "adaptec_raid", } },
50 { volume_id_probe_jmicron_raid, { "jmicron_raid", } },
51 { volume_id_probe_lvm1, { "lvm1", } },
52 { volume_id_probe_lvm2, { "lvm2", } },
53 { volume_id_probe_highpoint_37x_raid, { "highpoint_raid", } },
56 static const struct prober prober_filesystem[] = {
57 { volume_id_probe_vfat, { "vfat", } },
58 { volume_id_probe_linux_swap, { "swap", } },
59 { volume_id_probe_luks, { "luks", } },
60 { volume_id_probe_xfs, { "xfs", } },
61 { volume_id_probe_ext, { "ext2", "ext3", "jbd", } },
62 { volume_id_probe_reiserfs, { "reiserfs", "reiser4", } },
63 { volume_id_probe_jfs, { "jfs", } },
64 { volume_id_probe_udf, { "udf", } },
65 { volume_id_probe_iso9660, { "iso9660", } },
66 { volume_id_probe_hfs_hfsplus, { "hfs", "hfsplus", } },
67 { volume_id_probe_ufs, { "ufs", } },
68 { volume_id_probe_ntfs, { "ntfs", } },
69 { volume_id_probe_cramfs, { "cramfs", } },
70 { volume_id_probe_romfs, { "romfs", } },
71 { volume_id_probe_hpfs, { "hpfs", } },
72 { volume_id_probe_sysv, { "sysv", "xenix", } },
73 { volume_id_probe_minix, { "minix", } },
74 { volume_id_probe_gfs, { "gfs", } },
75 { volume_id_probe_gfs2, { "gfs2", } },
76 { volume_id_probe_ocfs1, { "ocfs1", } },
77 { volume_id_probe_ocfs2, { "ocfs2", } },
78 { volume_id_probe_vxfs, { "vxfs", } },
79 { volume_id_probe_squashfs, { "squashfs", } },
80 { volume_id_probe_netware, { "netware", } },
81 { volume_id_probe_oracleasm, { "oracleasm", } },
84 /* the user can overwrite this log function */
85 static void default_log(int priority, const char *file, int line, const char *format, ...)
90 volume_id_log_fn_t volume_id_log_fn = default_log;
93 * volume_id_get_prober_by_type:
96 * Lookup the probing function for a specific type.
98 * Returns: The probing function for the given type, #NULL otherwise.
100 const volume_id_probe_fn_t *volume_id_get_prober_by_type(const char *type)
107 for (p = 0; p < ARRAY_SIZE(prober_raid); p++)
108 for (n = 0; prober_raid[p].name[n] != NULL; n++)
109 if (strcmp(type, prober_raid[p].name[n]) == 0)
110 return &prober_raid[p].prober;
111 for (p = 0; p < ARRAY_SIZE(prober_filesystem); p++)
112 for (n = 0; prober_filesystem[p].name[n] != NULL; n++)
113 if (strcmp(type, prober_filesystem[p].name[n]) == 0)
114 return &prober_filesystem[p].prober;
119 * volume_id_get_label:
120 * @id: Probing context.
121 * @label: Label string. Must not be freed by the caller.
123 * Get the label string after a successful probe. Unicode
124 * is translated to UTF-8.
126 * Returns: 1 if the value was set, 0 otherwise.
128 int volume_id_get_label(struct volume_id *id, const char **label)
134 if (id->usage_id == VOLUME_ID_UNUSED)
142 * volume_id_get_label_raw:
143 * @id: Probing context.
144 * @label: Label byte array. Must not be freed by the caller.
145 * @len: Length of raw label byte array.
147 * Get the raw label byte array after a successful probe. It may
148 * contain undecoded multibyte character streams.
150 * Returns: 1 if the value was set, 0 otherwise.
152 int volume_id_get_label_raw(struct volume_id *id, const uint8_t **label, size_t *len)
160 if (id->usage_id == VOLUME_ID_UNUSED)
163 *label = id->label_raw;
164 *len = id->label_raw_len;
169 * volume_id_get_uuid:
170 * @id: Probing context.
171 * @uuid: UUID string. Must not be freed by the caller.
173 * Get the raw UUID string after a successful probe.
175 * Returns: 1 if the value was set, 0 otherwise.
177 int volume_id_get_uuid(struct volume_id *id, const char **uuid)
183 if (id->usage_id == VOLUME_ID_UNUSED)
191 * volume_id_get_uuid_raw:
192 * @id: Probing context.
193 * @uuid: UUID byte array. Must not be freed by the caller.
194 * @len: Length of raw UUID byte array.
196 * Get the raw UUID byte array after a successful probe. It may
197 * contain unconverted endianes values.
199 * Returns: 1 if the value was set, 0 otherwise.
201 int volume_id_get_uuid_raw(struct volume_id *id, const uint8_t **uuid, size_t *len)
209 if (id->usage_id == VOLUME_ID_UNUSED)
212 *uuid = id->uuid_raw;
213 *len = id->uuid_raw_len;
218 * volume_id_get_usage:
219 * @id: Probing context.
220 * @usage: Usage string. Must not be freed by the caller.
222 * Get the usage string after a successful probe.
224 * Returns: 1 if the value was set, 0 otherwise.
226 int volume_id_get_usage(struct volume_id *id, const char **usage)
232 if (id->usage_id == VOLUME_ID_UNUSED)
240 * volume_id_get_type:
241 * @id: Probing context
242 * @type: Type string. Must not be freed by the caller.
244 * Get the type string after a successful probe.
246 * Returns: 1 if the value was set, 0 otherwise.
248 int volume_id_get_type(struct volume_id *id, const char **type)
254 if (id->usage_id == VOLUME_ID_UNUSED)
262 * volume_id_get_type_version:
263 * @id: Probing context.
264 * @type_version: Type version string. Must not be freed by the caller.
266 * Get the Type version string after a successful probe.
268 * Returns: 1 if the value was set, 0 otherwise.
270 int volume_id_get_type_version(struct volume_id *id, const char **type_version)
274 if (type_version == NULL)
276 if (id->usage_id == VOLUME_ID_UNUSED)
279 *type_version = id->type_version;
283 static int needs_encoding(const char c)
285 if ((c >= '0' && c <= '9') ||
286 (c >= 'A' && c <= 'Z') ||
287 (c >= 'a' && c <= 'z') ||
288 strchr(ALLOWED_CHARS, c))
294 * volume_id_encode_string:
295 * @str: Input string to be encoded.
296 * @str_enc: Target string to store the encoded input.
297 * @len: Location to store the encoded string. The target string,
298 * which may be four times as long as the input string.
300 * Encode all potentially unsafe characters of a string to the
301 * corresponding hex value prefixed by '\x'.
303 * Returns: 1 if the entire string was copied, 0 otherwise.
305 int volume_id_encode_string(const char *str, char *str_enc, size_t len)
309 if (str == NULL || str_enc == NULL || len == 0)
313 for (i = 0, j = 0; str[i] != '\0'; i++) {
316 seqlen = volume_id_utf8_encoded_valid_unichar(&str[i]);
318 memcpy(&str_enc[j], &str[i], seqlen);
321 } else if (str[i] == '\\' || needs_encoding(str[i])) {
322 sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]);
337 /* run only once into a timeout for unreadable devices */
338 static int device_is_readable(struct volume_id *id, uint64_t off)
340 if (volume_id_get_buffer(id, off, 0x200) != NULL)
346 * volume_id_probe_raid:
347 * @id: Probing context.
348 * @off: Probing offset relative to the start of the device.
349 * @size: Total size of the device.
351 * Probe device for all known raid signatures.
353 * Returns: 0 on successful probe, otherwise negative value.
355 int volume_id_probe_raid(struct volume_id *id, uint64_t off, uint64_t size)
362 if (!device_is_readable(id, off))
365 info("probing at offset 0x%llx, size 0x%llx\n",
366 (unsigned long long) off, (unsigned long long) size);
368 for (i = 0; i < ARRAY_SIZE(prober_raid); i++)
369 if (prober_raid[i].prober(id, off, size) == 0)
374 /* If recognized, we free the allocated buffers */
375 volume_id_free_buffer(id);
380 * volume_id_probe_filesystem:
381 * @id: Probing context.
382 * @off: Probing offset relative to the start of the device.
383 * @size: Total size of the device.
385 * Probe device for all known filesystem signatures.
387 * Returns: 0 on successful probe, otherwise negative value.
389 int volume_id_probe_filesystem(struct volume_id *id, uint64_t off, uint64_t size)
396 if (!device_is_readable(id, off))
399 info("probing at offset 0x%llx, size 0x%llx\n",
400 (unsigned long long) off, (unsigned long long) size);
402 for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++)
403 if (prober_filesystem[i].prober(id, off, size) == 0)
408 /* If recognized, we free the allocated buffers */
409 volume_id_free_buffer(id);
414 * volume_id_probe_all:
415 * @id: Probing context.
416 * @off: Probing offset relative to the start of the device.
417 * @size: Total size of the device.
419 * Probe device for all known raid and filesystem signatures.
421 * Returns: 0 on successful probe, otherwise negative value.
423 int volume_id_probe_all(struct volume_id *id, uint64_t off, uint64_t size)
428 if (!device_is_readable(id, off))
431 /* probe for raid first, because fs probes may be successful on raid members */
432 if (volume_id_probe_raid(id, off, size) == 0)
435 if (volume_id_probe_filesystem(id, off, size) == 0)
442 * volume_id_probe_raid:
443 * @all_probers_fn: prober function to called for all known probing routines.
444 * @id: Context passed to prober function.
445 * @off: Offset value passed to prober function.
446 * @size: Size value passed to prober function.
447 * @data: Arbitrary data passed to the prober function.
449 * Run a custom function for all known probing routines.
451 void volume_id_all_probers(all_probers_fn_t all_probers_fn,
452 struct volume_id *id, uint64_t off, uint64_t size,
457 if (all_probers_fn == NULL)
460 for (i = 0; i < ARRAY_SIZE(prober_raid); i++)
461 if (all_probers_fn(prober_raid[i].prober, id, off, size, data) != 0)
463 for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++)
464 if (all_probers_fn(prober_filesystem[i].prober, id, off, size, data) != 0)
472 * @id: Probing context.
473 * @fd: Open file descriptor of device to read from.
475 * Create the context for probing.
477 * Returns: Probing context, or #NULL on failure.
479 struct volume_id *volume_id_open_fd(int fd)
481 struct volume_id *id;
483 id = malloc(sizeof(struct volume_id));
486 memset(id, 0x00, sizeof(struct volume_id));
495 * @id: Probing context.
497 * Release probing context and free all associated data.
499 void volume_id_close(struct volume_id *id)
504 if (id->fd_close != 0)
507 volume_id_free_buffer(id);