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_ocfs1, { "ocfs1", } },
74 { volume_id_probe_ocfs2, { "ocfs2", } },
75 { volume_id_probe_vxfs, { "vxfs", } },
76 { volume_id_probe_squashfs, { "squashfs", } },
77 { volume_id_probe_netware, { "netware", } },
80 /* the user can overwrite this log function */
81 static void default_log(int priority, const char *file, int line, const char *format, ...)
86 volume_id_log_fn_t volume_id_log_fn = default_log;
89 * volume_id_get_label:
92 * Lookup the probing function for a specific type.
94 * Returns: The probing function for the given type, #NULL otherwise.
96 const volume_id_probe_fn_t *volume_id_get_prober_by_type(const char *type)
103 for (p = 0; p < ARRAY_SIZE(prober_raid); p++)
104 for (n = 0; prober_raid[p].name[n] != NULL; n++)
105 if (strcmp(type, prober_raid[p].name[n]) == 0)
106 return &prober_raid[p].prober;
107 for (p = 0; p < ARRAY_SIZE(prober_filesystem); p++)
108 for (n = 0; prober_filesystem[p].name[n] != NULL; n++)
109 if (strcmp(type, prober_filesystem[p].name[n]) == 0)
110 return &prober_filesystem[p].prober;
115 * volume_id_get_label:
116 * @id: Probing context.
117 * @label: Label string. Must not be freed by the caller.
119 * Get the label string after a successful probe. Unicode
120 * is translated to UTF-8.
122 * Returns: 1 if the value was set, 0 otherwise.
124 int volume_id_get_label(struct volume_id *id, const char **label)
130 if (id->usage_id == VOLUME_ID_UNUSED)
138 * volume_id_get_label_raw:
139 * @id: Probing context.
140 * @label: Label byte array. Must not be freed by the caller.
141 * @len: Length of raw label byte array.
143 * Get the raw label byte array after a successful probe. It may
144 * contain undecoded multibyte character streams.
146 * Returns: 1 if the value was set, 0 otherwise.
148 int volume_id_get_label_raw(struct volume_id *id, const uint8_t **label, size_t *len)
156 if (id->usage_id == VOLUME_ID_UNUSED)
159 *label = id->label_raw;
160 *len = id->label_raw_len;
165 * volume_id_get_uuid:
166 * @id: Probing context.
167 * @uuid: UUID string. Must not be freed by the caller.
169 * Get the raw UUID string after a successful probe.
171 * Returns: 1 if the value was set, 0 otherwise.
173 int volume_id_get_uuid(struct volume_id *id, const char **uuid)
179 if (id->usage_id == VOLUME_ID_UNUSED)
187 * volume_id_get_uuid_raw:
188 * @id: Probing context.
189 * @uuid: UUID byte array. Must not be freed by the caller.
190 * @len: Length of raw UUID byte array.
192 * Get the raw UUID byte array after a successful probe. It may
193 * contain unconverted endianes values.
195 * Returns: 1 if the value was set, 0 otherwise.
197 int volume_id_get_uuid_raw(struct volume_id *id, const uint8_t **uuid, size_t *len)
205 if (id->usage_id == VOLUME_ID_UNUSED)
208 *uuid = id->uuid_raw;
209 *len = id->uuid_raw_len;
214 * volume_id_get_usage:
215 * @id: Probing context.
216 * @usage: Usage string. Must not be freed by the caller.
218 * Get the usage string after a successful probe.
220 * Returns: 1 if the value was set, 0 otherwise.
222 int volume_id_get_usage(struct volume_id *id, const char **usage)
228 if (id->usage_id == VOLUME_ID_UNUSED)
236 * volume_id_get_type:
237 * @id: Probing context
238 * @type: Type string. Must not be freed by the caller.
240 * Get the type string after a successful probe.
242 * Returns: 1 if the value was set, 0 otherwise.
244 int volume_id_get_type(struct volume_id *id, const char **type)
250 if (id->usage_id == VOLUME_ID_UNUSED)
258 * volume_id_get_type_version:
259 * @id: Probing context.
260 * @type_version: Type version string. Must not be freed by the caller.
262 * Get the Type version string after a successful probe.
264 * Returns: 1 if the value was set, 0 otherwise.
266 int volume_id_get_type_version(struct volume_id *id, const char **type_version)
270 if (type_version == NULL)
272 if (id->usage_id == VOLUME_ID_UNUSED)
275 *type_version = id->type_version;
280 * volume_id_probe_raid:
281 * @id: Probing context.
282 * @off: Probing offset relative to the start of the device.
283 * @size: Total size of the device.
285 * Probe device for all known raid signatures.
287 * Returns: 0 on successful probe, otherwise negative value.
289 int volume_id_probe_raid(struct volume_id *id, uint64_t off, uint64_t size)
296 info("probing at offset 0x%llx, size 0x%llx",
297 (unsigned long long) off, (unsigned long long) size);
299 for (i = 0; i < ARRAY_SIZE(prober_raid); i++)
300 if (prober_raid[i].prober(id, off, size) == 0)
305 /* If recognized, we free the allocated buffers */
306 volume_id_free_buffer(id);
311 * volume_id_probe_filesystem:
312 * @id: Probing context.
313 * @off: Probing offset relative to the start of the device.
314 * @size: Total size of the device.
316 * Probe device for all known filesystem signatures.
318 * Returns: 0 on successful probe, otherwise negative value.
320 int volume_id_probe_filesystem(struct volume_id *id, uint64_t off, uint64_t size)
327 info("probing at offset 0x%llx, size 0x%llx",
328 (unsigned long long) off, (unsigned long long) size);
330 for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++)
331 if (prober_filesystem[i].prober(id, off, size) == 0)
336 /* If recognized, we free the allocated buffers */
337 volume_id_free_buffer(id);
342 * volume_id_probe_all:
343 * @id: Probing context.
344 * @off: Probing offset relative to the start of the device.
345 * @size: Total size of the device.
347 * Probe device for all known raid and filesystem signatures.
349 * Returns: 0 on successful probe, otherwise negative value.
351 int volume_id_probe_all(struct volume_id *id, uint64_t off, uint64_t size)
356 /* probe for raid first, because fs probes may be successful on raid members */
357 if (volume_id_probe_raid(id, off, size) == 0)
360 if (volume_id_probe_filesystem(id, off, size) == 0)
367 * volume_id_probe_raid:
368 * @all_probers_fn: prober function to called for all known probing routines.
369 * @id: Context passed to prober function.
370 * @off: Offset value passed to prober function.
371 * @size: Size value passed to prober function.
372 * @data: Arbitrary data passed to the prober function.
374 * Run a custom function for all known probing routines.
376 void volume_id_all_probers(all_probers_fn_t all_probers_fn,
377 struct volume_id *id, uint64_t off, uint64_t size,
382 if (all_probers_fn == NULL)
385 for (i = 0; i < ARRAY_SIZE(prober_raid); i++)
386 if (all_probers_fn(prober_raid[i].prober, id, off, size, data) != 0)
388 for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++)
389 if (all_probers_fn(prober_filesystem[i].prober, id, off, size, data) != 0)
397 * @id: Probing context.
398 * @fd: Open file descriptor of device to read from.
400 * Create the context for probing.
402 * Returns: Probing context, or #NULL on failure.
404 struct volume_id *volume_id_open_fd(int fd)
406 struct volume_id *id;
408 id = malloc(sizeof(struct volume_id));
411 memset(id, 0x00, sizeof(struct volume_id));
418 struct volume_id *volume_id_open_node(const char *path)
420 struct volume_id *id;
423 fd = open(path, O_RDONLY);
425 dbg("unable to open '%s'", path);
429 id = volume_id_open_fd(fd);
433 /* close fd on device close */
441 * @id: Probing context.
443 * Release probing context and free all associated data.
445 void volume_id_close(struct volume_id *id)
450 if (id->fd_close != 0)
453 volume_id_free_buffer(id);