run autogen.sh ...
[grub2/phcoder/solaris.git] / util / grub-probe.c
blob402b7583940e2c831304f0e112a0583e382661b2
1 /* grub-probe.c - probe device information for a given path */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2006,2007,2008 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <grub/types.h>
22 #include <grub/util/misc.h>
23 #include <grub/device.h>
24 #include <grub/disk.h>
25 #include <grub/file.h>
26 #include <grub/fs.h>
27 #include <grub/partition.h>
28 #include <grub/pc_partition.h>
29 #include <grub/util/hostdisk.h>
30 #include <grub/util/getroot.h>
31 #include <grub/term.h>
32 #include <grub/env.h>
34 #include <grub_probe_init.h>
36 #include <stdio.h>
37 #include <unistd.h>
38 #include <string.h>
39 #include <stdlib.h>
40 #include <sys/stat.h>
42 #define _GNU_SOURCE 1
43 #include <getopt.h>
45 enum {
46 PRINT_FS,
47 PRINT_FS_UUID,
48 PRINT_DRIVE,
49 PRINT_DEVICE,
50 PRINT_PARTMAP,
51 PRINT_ABSTRACTION,
54 int print = PRINT_FS;
55 static unsigned int argument_is_device = 0;
57 void
58 grub_putchar (int c)
60 putchar (c);
63 int
64 grub_getkey (void)
66 return -1;
69 grub_term_input_t
70 grub_term_get_current_input (void)
72 return 0;
75 grub_term_output_t
76 grub_term_get_current_output (void)
78 return 0;
81 void
82 grub_refresh (void)
84 fflush (stdout);
87 static void
88 probe_partmap (grub_disk_t disk)
90 char *name;
91 char *underscore;
93 if (disk->partition == NULL)
95 grub_util_info ("No partition map found for %s", disk->name);
96 return;
99 name = strdup (disk->partition->partmap->name);
100 if (! name)
101 grub_util_error ("Not enough memory");
103 underscore = strchr (name, '_');
104 if (! underscore)
105 grub_util_error ("Invalid partition map %s", name);
107 *underscore = '\0';
108 printf ("%s\n", name);
109 free (name);
112 static void
113 probe (const char *path, char *device_name)
115 char *drive_name = NULL;
116 char *grub_path = NULL;
117 char *filebuf_via_grub = NULL, *filebuf_via_sys = NULL;
118 int abstraction_type;
119 grub_device_t dev = NULL;
120 grub_fs_t fs;
122 if (path == NULL)
124 if (! grub_util_check_block_device (device_name))
125 grub_util_error ("%s is not a block device.\n", device_name);
127 else
128 device_name = grub_guess_root_device (path);
130 if (! device_name)
131 grub_util_error ("cannot find a device for %s.\n", path);
133 if (print == PRINT_DEVICE)
135 printf ("%s\n", device_name);
136 goto end;
139 abstraction_type = grub_util_get_dev_abstraction (device_name);
140 /* No need to check for errors; lack of abstraction is permissible. */
142 if (print == PRINT_ABSTRACTION)
144 char *abstraction_name;
145 switch (abstraction_type)
147 case GRUB_DEV_ABSTRACTION_LVM:
148 abstraction_name = "lvm";
149 break;
150 case GRUB_DEV_ABSTRACTION_RAID:
151 abstraction_name = "raid mdraid";
152 break;
153 default:
154 grub_util_info ("did not find LVM/RAID in %s, assuming raw device", device_name);
155 goto end;
157 printf ("%s\n", abstraction_name);
158 goto end;
161 drive_name = grub_util_get_grub_dev (device_name);
162 if (! drive_name)
163 grub_util_error ("Cannot find a GRUB drive for %s. Check your device.map.\n", device_name);
165 if (print == PRINT_DRIVE)
167 printf ("(%s)\n", drive_name);
168 goto end;
171 grub_util_info ("opening %s", drive_name);
172 dev = grub_device_open (drive_name);
173 if (! dev)
174 grub_util_error ("%s", grub_errmsg);
176 if (print == PRINT_PARTMAP)
178 grub_disk_memberlist_t list = NULL, tmp;
180 /* Check if dev->disk itself is contained in a partmap. */
181 probe_partmap (dev->disk);
183 /* In case of LVM/RAID, check the member devices as well. */
184 if (dev->disk->dev->memberlist)
185 list = dev->disk->dev->memberlist (dev->disk);
186 while (list)
188 probe_partmap (list->disk);
189 tmp = list->next;
190 free (list);
191 list = tmp;
193 goto end;
196 fs = grub_fs_probe (dev);
197 if (! fs)
198 grub_util_error ("%s", grub_errmsg);
200 if (print == PRINT_FS)
202 struct stat st;
204 stat (path, &st);
206 if (st.st_mode == S_IFREG)
208 /* Regular file. Verify that we can read it properly. */
210 grub_file_t file;
211 grub_util_info ("reading %s via OS facilities", path);
212 filebuf_via_sys = grub_util_read_image (path);
214 grub_util_info ("reading %s via GRUB facilities", path);
215 asprintf (&grub_path, "(%s)%s", drive_name, path);
216 file = grub_file_open (grub_path);
217 filebuf_via_grub = xmalloc (file->size);
218 grub_file_read (file, filebuf_via_grub, file->size);
220 grub_util_info ("comparing");
222 if (memcmp (filebuf_via_grub, filebuf_via_sys, file->size))
223 grub_util_error ("files differ");
225 printf ("%s\n", fs->name);
228 if (print == PRINT_FS_UUID)
230 char *uuid;
231 if (! fs->uuid)
232 grub_util_error ("%s does not support UUIDs", fs->name);
234 fs->uuid (dev, &uuid);
236 printf ("%s\n", uuid);
239 end:
240 if (dev)
241 grub_device_close (dev);
242 free (grub_path);
243 free (filebuf_via_grub);
244 free (filebuf_via_sys);
245 free (drive_name);
248 static struct option options[] =
250 {"device", no_argument, 0, 'd'},
251 {"device-map", required_argument, 0, 'm'},
252 {"target", required_argument, 0, 't'},
253 {"help", no_argument, 0, 'h'},
254 {"version", no_argument, 0, 'V'},
255 {"verbose", no_argument, 0, 'v'},
256 {0, 0, 0, 0}
259 static void
260 usage (int status)
262 if (status)
263 fprintf (stderr,
264 "Try ``grub-probe --help'' for more information.\n");
265 else
266 printf ("\
267 Usage: grub-probe [OPTION]... [PATH|DEVICE]\n\
269 Probe device information for a given path (or device, if the -d option is given).\n\
271 -d, --device given argument is a system device, not a path\n\
272 -m, --device-map=FILE use FILE as the device map [default=%s]\n\
273 -t, --target=(fs|fs_uuid|drive|device|partmap|abstraction)\n\
274 print filesystem module, GRUB drive, system device, partition map module or abstraction module [default=fs]\n\
275 -h, --help display this message and exit\n\
276 -V, --version print version information and exit\n\
277 -v, --verbose print verbose messages\n\
279 Report bugs to <%s>.\n\
281 DEFAULT_DEVICE_MAP, PACKAGE_BUGREPORT);
283 exit (status);
287 main (int argc, char *argv[])
289 char *dev_map = 0;
290 char *argument;
292 progname = "grub-probe";
294 /* Check for options. */
295 while (1)
297 int c = getopt_long (argc, argv, "dm:t:hVv", options, 0);
299 if (c == -1)
300 break;
301 else
302 switch (c)
304 case 'd':
305 argument_is_device = 1;
306 break;
308 case 'm':
309 if (dev_map)
310 free (dev_map);
312 dev_map = xstrdup (optarg);
313 break;
315 case 't':
316 if (!strcmp (optarg, "fs"))
317 print = PRINT_FS;
318 else if (!strcmp (optarg, "fs_uuid"))
319 print = PRINT_FS_UUID;
320 else if (!strcmp (optarg, "drive"))
321 print = PRINT_DRIVE;
322 else if (!strcmp (optarg, "device"))
323 print = PRINT_DEVICE;
324 else if (!strcmp (optarg, "partmap"))
325 print = PRINT_PARTMAP;
326 else if (!strcmp (optarg, "abstraction"))
327 print = PRINT_ABSTRACTION;
328 else
329 usage (1);
330 break;
332 case 'h':
333 usage (0);
334 break;
336 case 'V':
337 printf ("%s (%s) %s\n", progname, PACKAGE_NAME, PACKAGE_VERSION);
338 return 0;
340 case 'v':
341 verbosity++;
342 break;
344 default:
345 usage (1);
346 break;
350 if (verbosity > 1)
351 grub_env_set ("debug", "all");
353 /* Obtain ARGUMENT. */
354 if (optind >= argc)
356 fprintf (stderr, "No path or device is specified.\n");
357 usage (1);
360 if (optind + 1 != argc)
362 fprintf (stderr, "Unknown extra argument `%s'.\n", argv[optind + 1]);
363 usage (1);
366 argument = argv[optind];
368 /* Initialize the emulated biosdisk driver. */
369 grub_util_biosdisk_init (dev_map ? : DEFAULT_DEVICE_MAP);
371 /* Initialize all modules. */
372 grub_init_all ();
374 /* Do it. */
375 if (argument_is_device)
376 probe (NULL, argument);
377 else
378 probe (argument, NULL);
380 /* Free resources. */
381 grub_fini_all ();
382 grub_util_biosdisk_fini ();
384 free (dev_map);
386 return 0;