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;
88 const volume_id_probe_fn_t *volume_id_get_prober_by_type(const char *type)
95 for (p = 0; p < ARRAY_SIZE(prober_raid); p++)
96 for (n = 0; prober_raid[p].name[n] != NULL; n++)
97 if (strcmp(type, prober_raid[p].name[n]) == 0)
98 return &prober_raid[p].prober;
99 for (p = 0; p < ARRAY_SIZE(prober_filesystem); p++)
100 for (n = 0; prober_filesystem[p].name[n] != NULL; n++)
101 if (strcmp(type, prober_filesystem[p].name[n]) == 0)
102 return &prober_filesystem[p].prober;
106 int volume_id_get_label(struct volume_id *id, const char **label)
112 if (id->usage_id == VOLUME_ID_UNUSED)
119 int volume_id_get_label_raw(struct volume_id *id, const uint8_t **label, size_t *len)
127 if (id->usage_id == VOLUME_ID_UNUSED)
130 *label = id->label_raw;
131 *len = id->label_raw_len;
135 int volume_id_get_uuid(struct volume_id *id, const char **uuid)
141 if (id->usage_id == VOLUME_ID_UNUSED)
148 int volume_id_get_uuid_raw(struct volume_id *id, const uint8_t **uuid, size_t *len)
156 if (id->usage_id == VOLUME_ID_UNUSED)
159 *uuid = id->uuid_raw;
160 *len = id->uuid_raw_len;
164 int volume_id_get_usage(struct volume_id *id, const char **usage)
170 if (id->usage_id == VOLUME_ID_UNUSED)
177 int volume_id_get_type(struct volume_id *id, const char **type)
183 if (id->usage_id == VOLUME_ID_UNUSED)
190 int volume_id_get_type_version(struct volume_id *id, const char **type_version)
194 if (type_version == NULL)
196 if (id->usage_id == VOLUME_ID_UNUSED)
199 *type_version = id->type_version;
203 int volume_id_probe_raid(struct volume_id *id, uint64_t off, uint64_t size)
210 info("probing at offset 0x%llx, size 0x%llx",
211 (unsigned long long) off, (unsigned long long) size);
213 for (i = 0; i < ARRAY_SIZE(prober_raid); i++)
214 if (prober_raid[i].prober(id, off, size) == 0)
219 /* If recognized, we free the allocated buffers */
220 volume_id_free_buffer(id);
224 int volume_id_probe_filesystem(struct volume_id *id, uint64_t off, uint64_t size)
231 info("probing at offset 0x%llx, size 0x%llx",
232 (unsigned long long) off, (unsigned long long) size);
234 for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++)
235 if (prober_filesystem[i].prober(id, off, size) == 0)
240 /* If recognized, we free the allocated buffers */
241 volume_id_free_buffer(id);
245 int volume_id_probe_all(struct volume_id *id, uint64_t off, uint64_t size)
250 /* probe for raid first, because fs probes may be successful on raid members */
251 if (volume_id_probe_raid(id, off, size) == 0)
254 if (volume_id_probe_filesystem(id, off, size) == 0)
260 void volume_id_all_probers(all_probers_fn_t all_probers_fn,
261 struct volume_id *id, uint64_t off, uint64_t size,
266 if (all_probers_fn == NULL)
269 for (i = 0; i < ARRAY_SIZE(prober_raid); i++)
270 if (all_probers_fn(prober_raid[i].prober, id, off, size, data) != 0)
272 for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++)
273 if (all_probers_fn(prober_filesystem[i].prober, id, off, size, data) != 0)
279 /* open volume by already open file descriptor */
280 struct volume_id *volume_id_open_fd(int fd)
282 struct volume_id *id;
284 id = malloc(sizeof(struct volume_id));
287 memset(id, 0x00, sizeof(struct volume_id));
294 /* open volume by device node */
295 struct volume_id *volume_id_open_node(const char *path)
297 struct volume_id *id;
300 fd = open(path, O_RDONLY);
302 dbg("unable to open '%s'", path);
306 id = volume_id_open_fd(fd);
310 /* close fd on device close */
316 void volume_id_close(struct volume_id *id)
321 if (id->fd_close != 0)
324 volume_id_free_buffer(id);