2 * vol_id - read filesystem label and uuid
4 * Copyright (C) 2005-2006 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.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 675 Mass Ave, Cambridge, MA 02139, USA.
33 #include <sys/ioctl.h>
35 #include "../../udev.h"
36 #include "lib/libvolume_id.h"
38 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
41 void log_message(int priority, const char *format, ...)
44 static int udev_log = -1;
49 value = getenv("UDEV_LOG");
51 udev_log = log_priority(value);
56 if (priority > udev_log)
59 va_start(args, format);
60 vsyslog(priority, format, args);
65 static void vid_log(int priority, const char *file, int line, const char *format, ...)
71 va_start(args, format);
72 vsnprintf(log_str, sizeof(log_str), format, args);
73 log_str[sizeof(log_str)-1] = '\0';
74 log_message(priority, "%s:%i %s", file, line, log_str);
80 static void set_str(char *to, const char *from, size_t count)
84 /* strip trailing whitespace */
85 len = strnlen(from, count);
86 while (len && isspace(from[len-1]))
89 /* strip leading whitespace */
91 while (isspace(from[i]) && (i < len))
96 /* substitute multiple whitespace */
97 if (isspace(from[i])) {
98 while (isspace(from[i]))
103 if (from[i] == '/') {
112 int main(int argc, char *argv[])
114 const char help[] = "Usage: vol_id [options] <device>\n"
115 " --export export key/value pairs\n"
116 " -t filesystem type\n"
117 " -l filesystem label\n"
118 " -u filesystem uuid\n"
120 " --skip-raid don't probe for raid\n"
121 " --probe-all find possibly conflicting signatures\n"
130 } print = PRINT_EXPORT;
131 struct volume_id *vid = NULL;
132 static char name[VOLUME_ID_LABEL_SIZE];
137 const char *node = NULL;
142 logging_init("vol_id");
144 /* hook in our debug into libvolume_id */
145 volume_id_log_fn = vid_log;
147 for (i = 1 ; i < argc; i++) {
150 if (strcmp(arg, "--export") == 0) {
151 print = PRINT_EXPORT;
152 } else if (strcmp(arg, "-t") == 0) {
154 } else if (strcmp(arg, "-l") == 0) {
156 } else if (strcmp(arg, "-u") == 0) {
158 } else if (strcmp(arg, "-L") == 0) {
159 print = PRINT_RAW_LABEL;
160 } else if (strcmp(arg, "--skip-raid") == 0) {
162 } else if (strcmp(arg, "--probe-all") == 0) {
164 } else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) {
171 err("no node specified");
172 fprintf(stderr, help);
177 vid = volume_id_open_node(node);
179 fprintf(stderr, "%s: error open volume\n", node);
184 if (ioctl(vid->fd, BLKGETSIZE64, &size) != 0)
186 dbg("BLKGETSIZE64=%llu", size);
188 /* try to drop all privileges before reading disk content */
189 pw = getpwnam ("nobody");
190 if (pw != NULL && pw->pw_uid > 0 && pw->pw_gid > 0) {
191 dbg("dropping privileges to %u:%u", (unsigned int)pw->pw_uid, (unsigned int)pw->pw_gid);
192 if (setgroups(0, NULL) != 0 ||
193 setgid(pw->pw_gid) != 0 ||
194 setuid(pw->pw_uid) != 0) {
195 fprintf(stderr, "error dropping privileges: %s\n", strerror(errno));
202 if (volume_id_probe_linux_raid(vid, 0, size) == 0)
203 printf("%s\n", vid->type);
204 if (volume_id_probe_intel_software_raid(vid, 0, size) == 0)
205 printf("%s\n", vid->type);
206 if (volume_id_probe_lsi_mega_raid(vid, 0, size) == 0)
207 printf("%s\n", vid->type);
208 if (volume_id_probe_via_raid(vid, 0, size) == 0)
209 printf("%s\n", vid->type);
210 if (volume_id_probe_silicon_medley_raid(vid, 0, size) == 0)
211 printf("%s\n", vid->type);
212 if (volume_id_probe_nvidia_raid(vid, 0, size) == 0)
213 printf("%s\n", vid->type);
214 if (volume_id_probe_promise_fasttrack_raid(vid, 0, size) == 0)
215 printf("%s\n", vid->type);
216 if (volume_id_probe_highpoint_45x_raid(vid, 0, size) == 0)
217 printf("%s\n", vid->type);
218 if (volume_id_probe_adaptec_raid(vid, 0, size) == 0)
219 printf("%s\n", vid->type);
220 if (volume_id_probe_jmicron_raid(vid, 0, size) == 0)
221 printf("%s\n", vid->type);
222 if (volume_id_probe_vfat(vid, 0, 0) == 0)
223 printf("%s\n", vid->type);
224 if (volume_id_probe_linux_swap(vid, 0, 0) == 0)
225 printf("%s\n", vid->type);
226 if (volume_id_probe_luks(vid, 0, 0) == 0)
227 printf("%s\n", vid->type);
228 if (volume_id_probe_xfs(vid, 0, 0) == 0)
229 printf("%s\n", vid->type);
230 if (volume_id_probe_ext(vid, 0, 0) == 0)
231 printf("%s\n", vid->type);
232 if (volume_id_probe_reiserfs(vid, 0, 0) == 0)
233 printf("%s\n", vid->type);
234 if (volume_id_probe_jfs(vid, 0, 0) == 0)
235 printf("%s\n", vid->type);
236 if (volume_id_probe_udf(vid, 0, 0) == 0)
237 printf("%s\n", vid->type);
238 if (volume_id_probe_iso9660(vid, 0, 0) == 0)
239 printf("%s\n", vid->type);
240 if (volume_id_probe_hfs_hfsplus(vid, 0, 0) == 0)
241 printf("%s\n", vid->type);
242 if (volume_id_probe_ufs(vid, 0, 0) == 0)
243 printf("%s\n", vid->type);
244 if (volume_id_probe_ntfs(vid, 0, 0) == 0)
245 printf("%s\n", vid->type);
246 if (volume_id_probe_cramfs(vid, 0, 0) == 0)
247 printf("%s\n", vid->type);
248 if (volume_id_probe_romfs(vid, 0, 0) == 0)
249 printf("%s\n", vid->type);
250 if (volume_id_probe_hpfs(vid, 0, 0) == 0)
251 printf("%s\n", vid->type);
252 if (volume_id_probe_sysv(vid, 0, 0) == 0)
253 printf("%s\n", vid->type);
254 if (volume_id_probe_minix(vid, 0, 0) == 0)
255 printf("%s\n", vid->type);
256 if (volume_id_probe_ocfs1(vid, 0, 0) == 0)
257 printf("%s\n", vid->type);
258 if (volume_id_probe_ocfs2(vid, 0, 0) == 0)
259 printf("%s\n", vid->type);
260 if (volume_id_probe_vxfs(vid, 0, 0) == 0)
261 printf("%s\n", vid->type);
262 if (volume_id_probe_squashfs(vid, 0, 0) == 0)
263 printf("%s\n", vid->type);
264 if (volume_id_probe_netware(vid, 0, 0) == 0)
265 printf("%s\n", vid->type);
266 if (volume_id_probe_gfs(vid, 0, 0) == 0)
267 printf("%s\n", vid->type);
268 if (volume_id_probe_gfs2(vid, 0, 0) == 0)
269 printf("%s\n", vid->type);
275 retval = volume_id_probe_filesystem(vid, 0, size);
277 retval = volume_id_probe_all(vid, 0, size);
279 fprintf(stderr, "%s: unknown volume type\n", node);
284 set_str(name, vid->label, sizeof(vid->label));
285 replace_untrusted_chars(name);
289 printf("ID_FS_USAGE=%s\n", vid->usage);
290 printf("ID_FS_TYPE=%s\n", vid->type);
291 printf("ID_FS_VERSION=%s\n", vid->type_version);
292 printf("ID_FS_UUID=%s\n", vid->uuid);
293 printf("ID_FS_LABEL=%s\n", vid->label);
294 printf("ID_FS_LABEL_SAFE=%s\n", name);
297 printf("%s\n", vid->type);
300 if (name[0] == '\0' || vid->usage_id == VOLUME_ID_RAID) {
304 printf("%s\n", name);
307 if (vid->uuid[0] == '\0' || vid->usage_id == VOLUME_ID_RAID) {
311 printf("%s\n", vid->uuid);
313 case PRINT_RAW_LABEL:
314 if (vid->label[0] == '\0' || vid->usage_id == VOLUME_ID_RAID) {
318 printf("%s\n", vid->label);
324 volume_id_close(vid);