]> git.ipfire.org Git - thirdparty/systemd.git/blob - extras/volume_id/lib/volume_id.c
vol_id: add --skip-raid and --probe-all option
[thirdparty/systemd.git] / extras / volume_id / lib / volume_id.c
1 /*
2 * volume_id - reads volume label and uuid
3 *
4 * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
5 *
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.
9 */
10
11 #ifndef _GNU_SOURCE
12 #define _GNU_SOURCE 1
13 #endif
14
15 #ifdef HAVE_CONFIG_H
16 # include <config.h>
17 #endif
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <ctype.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27
28 #include "libvolume_id.h"
29 #include "util.h"
30
31 /* the user can overwrite this log function */
32 static void default_log(int priority, const char *file, int line, const char *format, ...)
33 {
34 return;
35 }
36
37 volume_id_log_fn_t volume_id_log_fn = default_log;
38
39 int volume_id_probe_raid(struct volume_id *id, uint64_t off, uint64_t size)
40 {
41 if (id == NULL)
42 return -EINVAL;
43
44 info("probing at offset 0x%llx, size 0x%llx",
45 (unsigned long long) off, (unsigned long long) size);
46
47 /* probe for raid first, because fs probes may be successful on raid members */
48 if (size) {
49 if (volume_id_probe_linux_raid(id, off, size) == 0)
50 goto found;
51
52 if (volume_id_probe_intel_software_raid(id, off, size) == 0)
53 goto found;
54
55 if (volume_id_probe_lsi_mega_raid(id, off, size) == 0)
56 goto found;
57
58 if (volume_id_probe_via_raid(id, off, size) == 0)
59 goto found;
60
61 if (volume_id_probe_silicon_medley_raid(id, off, size) == 0)
62 goto found;
63
64 if (volume_id_probe_nvidia_raid(id, off, size) == 0)
65 goto found;
66
67 if (volume_id_probe_promise_fasttrack_raid(id, off, size) == 0)
68 goto found;
69
70 if (volume_id_probe_highpoint_45x_raid(id, off, size) == 0)
71 goto found;
72
73 if (volume_id_probe_adaptec_raid(id, off, size) == 0)
74 goto found;
75
76 if (volume_id_probe_jmicron_raid(id, off, size) == 0)
77 goto found;
78 }
79
80 if (volume_id_probe_lvm1(id, off) == 0)
81 goto found;
82
83 if (volume_id_probe_lvm2(id, off) == 0)
84 goto found;
85
86 if (volume_id_probe_highpoint_37x_raid(id, off) == 0)
87 goto found;
88
89 return -1;
90
91 found:
92 /* If recognized, we free the allocated buffers */
93 volume_id_free_buffer(id);
94 return 0;
95 }
96
97 int volume_id_probe_filesystem(struct volume_id *id, uint64_t off, uint64_t size)
98 {
99 if (id == NULL)
100 return -EINVAL;
101
102 info("probing at offset 0x%llx, size 0x%llx",
103 (unsigned long long) off, (unsigned long long) size);
104
105 if (volume_id_probe_vfat(id, off) == 0)
106 goto found;
107
108 /* fill buffer with maximum */
109 volume_id_get_buffer(id, 0, SB_BUFFER_SIZE);
110
111 if (volume_id_probe_linux_swap(id, off) == 0)
112 goto found;
113
114 if (volume_id_probe_luks(id, off) == 0)
115 goto found;
116
117 if (volume_id_probe_xfs(id, off) == 0)
118 goto found;
119
120 if (volume_id_probe_ext(id, off) == 0)
121 goto found;
122
123 if (volume_id_probe_reiserfs(id, off) == 0)
124 goto found;
125
126 if (volume_id_probe_jfs(id, off) == 0)
127 goto found;
128
129 if (volume_id_probe_udf(id, off) == 0)
130 goto found;
131
132 if (volume_id_probe_iso9660(id, off) == 0)
133 goto found;
134
135 if (volume_id_probe_hfs_hfsplus(id, off) == 0)
136 goto found;
137
138 if (volume_id_probe_ufs(id, off) == 0)
139 goto found;
140
141 if (volume_id_probe_ntfs(id, off) == 0)
142 goto found;
143
144 if (volume_id_probe_cramfs(id, off) == 0)
145 goto found;
146
147 if (volume_id_probe_romfs(id, off) == 0)
148 goto found;
149
150 if (volume_id_probe_hpfs(id, off) == 0)
151 goto found;
152
153 if (volume_id_probe_sysv(id, off) == 0)
154 goto found;
155
156 if (volume_id_probe_minix(id, off) == 0)
157 goto found;
158
159 if (volume_id_probe_ocfs1(id, off) == 0)
160 goto found;
161
162 if (volume_id_probe_ocfs2(id, off) == 0)
163 goto found;
164
165 if (volume_id_probe_vxfs(id, off) == 0)
166 goto found;
167
168 if (volume_id_probe_squashfs(id, off) == 0)
169 goto found;
170
171 if (volume_id_probe_netware(id, off) == 0)
172 goto found;
173
174 if (volume_id_probe_gfs(id, off) == 0)
175 goto found;
176
177 if (volume_id_probe_gfs2(id, off) == 0)
178 goto found;
179
180 return -1;
181
182 found:
183 /* If recognized, we free the allocated buffers */
184 volume_id_free_buffer(id);
185 return 0;
186 }
187
188 int volume_id_probe_all(struct volume_id *id, uint64_t off, uint64_t size)
189 {
190 if (id == NULL)
191 return -EINVAL;
192
193 if (volume_id_probe_raid(id, off, size) == 0)
194 return 0;
195
196 if (volume_id_probe_filesystem(id, off, size) == 0)
197 return 0;
198
199 return -1;
200 }
201
202 /* open volume by already open file descriptor */
203 struct volume_id *volume_id_open_fd(int fd)
204 {
205 struct volume_id *id;
206
207 id = malloc(sizeof(struct volume_id));
208 if (id == NULL)
209 return NULL;
210 memset(id, 0x00, sizeof(struct volume_id));
211
212 id->fd = fd;
213
214 return id;
215 }
216
217 /* open volume by device node */
218 struct volume_id *volume_id_open_node(const char *path)
219 {
220 struct volume_id *id;
221 int fd;
222
223 fd = open(path, O_RDONLY);
224 if (fd < 0) {
225 dbg("unable to open '%s'", path);
226 return NULL;
227 }
228
229 id = volume_id_open_fd(fd);
230 if (id == NULL)
231 return NULL;
232
233 /* close fd on device close */
234 id->fd_close = 1;
235
236 return id;
237 }
238
239 void volume_id_close(struct volume_id *id)
240 {
241 if (id == NULL)
242 return;
243
244 if (id->fd_close != 0)
245 close(id->fd);
246
247 volume_id_free_buffer(id);
248
249 free(id);
250 }