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]))
35 volume_id_probe_fn_t prober;
38 static const struct prober prober_raid[] = {
39 {"linux_raid_member", volume_id_probe_linux_raid },
40 {"ddf_raid_member", volume_id_probe_ddf_raid },
41 {"isw_raid_member", volume_id_probe_intel_software_raid },
42 {"lsi_mega_raid_member", volume_id_probe_lsi_mega_raid },
43 {"via_raid_member", volume_id_probe_via_raid },
44 {"silicon_medley_raid_member", volume_id_probe_silicon_medley_raid },
45 {"nvidia_raid_member", volume_id_probe_nvidia_raid },
46 {"promise_fasttrack_raid_member", volume_id_probe_promise_fasttrack_raid },
47 {"highpoint_raid_member", volume_id_probe_highpoint_45x_raid },
48 {"adaptec_raid_member", volume_id_probe_adaptec_raid },
49 {"jmicron_raid_member", volume_id_probe_jmicron_raid },
50 {"LVM1_member", volume_id_probe_lvm1 },
51 {"LVM2_member", volume_id_probe_lvm2 },
52 {"highpoint_raid_member", volume_id_probe_highpoint_37x_raid },
55 static const struct prober prober_filesystem[] = {
56 { "vfat", volume_id_probe_vfat },
57 { "linux_swap", volume_id_probe_linux_swap },
58 { "luks", volume_id_probe_luks },
59 { "xfs", volume_id_probe_xfs },
60 { "ext", volume_id_probe_ext },
61 { "reiserfs", volume_id_probe_reiserfs },
62 { "jfs", volume_id_probe_jfs },
63 { "udf", volume_id_probe_udf },
64 { "iso9660", volume_id_probe_iso9660 },
65 { "hfs", volume_id_probe_hfs_hfsplus },
66 { "ufs", volume_id_probe_ufs },
67 { "ntfs", volume_id_probe_ntfs },
68 { "cramfs", volume_id_probe_cramfs },
69 { "romfs", volume_id_probe_romfs },
70 { "hpfs", volume_id_probe_hpfs },
71 { "sysv", volume_id_probe_sysv },
72 { "minix", volume_id_probe_minix },
73 { "ocfs1", volume_id_probe_ocfs1 },
74 { "ocfs2", volume_id_probe_ocfs2 },
75 { "vxfs", volume_id_probe_vxfs },
76 { "squashfs", volume_id_probe_squashfs },
77 { "netware", volume_id_probe_netware },
78 { "gfs", volume_id_probe_gfs },
79 { "gfs2", volume_id_probe_gfs2 },
82 /* the user can overwrite this log function */
83 static void default_log(int priority, const char *file, int line, const char *format, ...)
88 volume_id_log_fn_t volume_id_log_fn = default_log;
90 const volume_id_probe_fn_t *volume_id_get_prober_by_type(const char *type)
94 for (i = 0; i < ARRAY_SIZE(prober_raid); i++)
95 if (strcmp(type, prober_raid[i].type) == 0)
96 return &prober_raid[i].prober;
97 for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++)
98 if (strcmp(type, prober_filesystem[i].type) == 0)
99 return &prober_filesystem[i].prober;
103 int volume_id_get_label(struct volume_id *id, const char **label)
109 if (id->usage_id == VOLUME_ID_UNUSED)
116 int volume_id_get_label_raw(struct volume_id *id, const uint8_t **label, size_t *len)
124 if (id->usage_id == VOLUME_ID_UNUSED)
127 *label = id->label_raw;
128 *len = id->label_raw_len;
132 int volume_id_get_uuid(struct volume_id *id, const char **uuid)
138 if (id->usage_id == VOLUME_ID_UNUSED)
145 int volume_id_get_uuid_raw(struct volume_id *id, const uint8_t **uuid, size_t *len)
153 if (id->usage_id == VOLUME_ID_UNUSED)
156 *uuid = id->uuid_raw;
157 *len = id->uuid_raw_len;
161 int volume_id_get_usage(struct volume_id *id, const char **usage)
167 if (id->usage_id == VOLUME_ID_UNUSED)
174 int volume_id_get_type(struct volume_id *id, const char **type)
180 if (id->usage_id == VOLUME_ID_UNUSED)
187 int volume_id_get_type_version(struct volume_id *id, const char **type_version)
191 if (type_version == NULL)
193 if (id->usage_id == VOLUME_ID_UNUSED)
196 *type_version = id->type_version;
200 int volume_id_probe_raid(struct volume_id *id, uint64_t off, uint64_t size)
207 info("probing at offset 0x%llx, size 0x%llx",
208 (unsigned long long) off, (unsigned long long) size);
210 for (i = 0; i < ARRAY_SIZE(prober_raid); i++)
211 if (prober_raid[i].prober(id, off, size) == 0)
216 /* If recognized, we free the allocated buffers */
217 volume_id_free_buffer(id);
221 int volume_id_probe_filesystem(struct volume_id *id, uint64_t off, uint64_t size)
228 info("probing at offset 0x%llx, size 0x%llx",
229 (unsigned long long) off, (unsigned long long) size);
231 for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++)
232 if (prober_filesystem[i].prober(id, off, size) == 0)
237 /* If recognized, we free the allocated buffers */
238 volume_id_free_buffer(id);
242 int volume_id_probe_all(struct volume_id *id, uint64_t off, uint64_t size)
247 /* probe for raid first, because fs probes may be successful on raid members */
248 if (volume_id_probe_raid(id, off, size) == 0)
251 if (volume_id_probe_filesystem(id, off, size) == 0)
257 void volume_id_all_probers(all_probers_fn_t all_probers_fn,
258 struct volume_id *id, uint64_t off, uint64_t size,
263 if (all_probers_fn == NULL)
266 for (i = 0; i < ARRAY_SIZE(prober_raid); i++)
267 if (all_probers_fn(prober_raid[i].prober, id, off, size, data) != 0)
269 for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++)
270 if (all_probers_fn(prober_filesystem[i].prober, id, off, size, data) != 0)
276 /* open volume by already open file descriptor */
277 struct volume_id *volume_id_open_fd(int fd)
279 struct volume_id *id;
281 id = malloc(sizeof(struct volume_id));
284 memset(id, 0x00, sizeof(struct volume_id));
291 /* open volume by device node */
292 struct volume_id *volume_id_open_node(const char *path)
294 struct volume_id *id;
297 fd = open(path, O_RDONLY);
299 dbg("unable to open '%s'", path);
303 id = volume_id_open_fd(fd);
307 /* close fd on device close */
313 void volume_id_close(struct volume_id *id)
318 if (id->fd_close != 0)
321 volume_id_free_buffer(id);