1 /* grub-probe.c - probe device information for a given path */
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/>.
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>
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>
34 #include <grub_probe_init.h>
55 static unsigned int argument_is_device
= 0;
70 grub_term_get_current_input (void)
76 grub_term_get_current_output (void)
88 probe_partmap (grub_disk_t disk
)
93 if (disk
->partition
== NULL
)
95 grub_util_info ("No partition map found for %s", disk
->name
);
99 name
= strdup (disk
->partition
->partmap
->name
);
101 grub_util_error ("Not enough memory");
103 underscore
= strchr (name
, '_');
105 grub_util_error ("Invalid partition map %s", name
);
108 printf ("%s\n", name
);
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
;
124 if (! grub_util_check_block_device (device_name
))
125 grub_util_error ("%s is not a block device.\n", device_name
);
128 device_name
= grub_guess_root_device (path
);
131 grub_util_error ("cannot find a device for %s.\n", path
);
133 if (print
== PRINT_DEVICE
)
135 printf ("%s\n", device_name
);
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";
150 case GRUB_DEV_ABSTRACTION_RAID
:
151 abstraction_name
= "raid mdraid";
154 grub_util_info ("did not find LVM/RAID in %s, assuming raw device", device_name
);
157 printf ("%s\n", abstraction_name
);
161 drive_name
= grub_util_get_grub_dev (device_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
);
171 grub_util_info ("opening %s", drive_name
);
172 dev
= grub_device_open (drive_name
);
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
);
188 probe_partmap (list
->disk
);
196 fs
= grub_fs_probe (dev
);
198 grub_util_error ("%s", grub_errmsg
);
200 if (print
== PRINT_FS
)
206 if (st
.st_mode
== S_IFREG
)
208 /* Regular file. Verify that we can read it properly. */
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
)
232 grub_util_error ("%s does not support UUIDs", fs
->name
);
234 fs
->uuid (dev
, &uuid
);
236 printf ("%s\n", uuid
);
241 grub_device_close (dev
);
243 free (filebuf_via_grub
);
244 free (filebuf_via_sys
);
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'},
264 "Try ``grub-probe --help'' for more information.\n");
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
);
287 main (int argc
, char *argv
[])
292 progname
= "grub-probe";
294 /* Check for options. */
297 int c
= getopt_long (argc
, argv
, "dm:t:hVv", options
, 0);
305 argument_is_device
= 1;
312 dev_map
= xstrdup (optarg
);
316 if (!strcmp (optarg
, "fs"))
318 else if (!strcmp (optarg
, "fs_uuid"))
319 print
= PRINT_FS_UUID
;
320 else if (!strcmp (optarg
, "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
;
337 printf ("%s (%s) %s\n", progname
, PACKAGE_NAME
, PACKAGE_VERSION
);
351 grub_env_set ("debug", "all");
353 /* Obtain ARGUMENT. */
356 fprintf (stderr
, "No path or device is specified.\n");
360 if (optind
+ 1 != argc
)
362 fprintf (stderr
, "Unknown extra argument `%s'.\n", argv
[optind
+ 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. */
375 if (argument_is_device
)
376 probe (NULL
, argument
);
378 probe (argument
, NULL
);
380 /* Free resources. */
382 grub_util_biosdisk_fini ();