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/biosdisk.h>
30 #include <grub/util/getroot.h>
31 #include <grub/term.h>
34 #include <grub_probe_init.h>
54 static unsigned int argument_is_device
= 0;
69 grub_term_get_current (void)
80 probe_partmap (grub_disk_t disk
)
85 if (disk
->partition
== NULL
)
87 grub_util_info ("No partition map found for %s", disk
->name
);
91 name
= strdup (disk
->partition
->partmap
->name
);
93 grub_util_error ("Not enough memory");
95 underscore
= strchr (name
, '_');
97 grub_util_error ("Invalid partition map %s", name
);
100 printf ("%s\n", name
);
105 probe (const char *path
, char *device_name
)
107 char *drive_name
= NULL
;
108 char *grub_path
= NULL
;
109 char *filebuf_via_grub
= NULL
, *filebuf_via_sys
= NULL
;
110 int abstraction_type
;
111 grub_device_t dev
= NULL
;
115 if (! grub_util_check_block_device (device_name
))
116 grub_util_error ("%s is not a block device.\n", device_name
);
119 device_name
= grub_guess_root_device (path
);
122 grub_util_error ("cannot find a device for %s.\n", path
);
124 if (print
== PRINT_DEVICE
)
126 printf ("%s\n", device_name
);
130 abstraction_type
= grub_util_get_dev_abstraction (device_name
);
131 /* No need to check for errors; lack of abstraction is permissible. */
133 if (print
== PRINT_ABSTRACTION
)
135 char *abstraction_name
;
136 switch (abstraction_type
)
138 case GRUB_DEV_ABSTRACTION_LVM
:
139 abstraction_name
= "lvm";
141 case GRUB_DEV_ABSTRACTION_RAID
:
142 abstraction_name
= "raid";
145 grub_util_info ("did not find LVM/RAID in %s, assuming raw device", device_name
);
148 printf ("%s\n", abstraction_name
);
152 drive_name
= grub_util_get_grub_dev (device_name
);
154 grub_util_error ("cannot find a GRUB drive for %s.\n", device_name
);
156 if (print
== PRINT_DRIVE
)
158 printf ("(%s)\n", drive_name
);
162 grub_util_info ("opening %s", drive_name
);
163 dev
= grub_device_open (drive_name
);
165 grub_util_error ("%s", grub_errmsg
);
167 if (print
== PRINT_PARTMAP
)
169 grub_disk_memberlist_t list
= NULL
, tmp
;
171 /* Check if dev->disk itself is contained in a partmap. */
172 probe_partmap (dev
->disk
);
174 /* In case of LVM/RAID, check the member devices as well. */
175 if (dev
->disk
->dev
->memberlist
)
176 list
= dev
->disk
->dev
->memberlist (dev
->disk
);
179 probe_partmap (list
->disk
);
187 if (print
== PRINT_FS
)
194 if (st
.st_mode
== S_IFREG
)
196 /* Regular file. Verify that we can read it properly. */
199 grub_util_info ("reading %s via OS facilities", path
);
200 filebuf_via_sys
= grub_util_read_image (path
);
202 grub_util_info ("reading %s via GRUB facilities", path
);
203 asprintf (&grub_path
, "(%s)%s", drive_name
, path
);
204 file
= grub_file_open (grub_path
);
205 filebuf_via_grub
= xmalloc (file
->size
);
206 grub_file_read (file
, filebuf_via_grub
, file
->size
);
208 grub_util_info ("comparing");
210 if (memcmp (filebuf_via_grub
, filebuf_via_sys
, file
->size
))
211 grub_util_error ("files differ");
217 fs
= grub_fs_probe (dev
);
219 grub_util_error ("%s", grub_errmsg
);
222 printf ("%s\n", fs
->name
);
227 grub_device_close (dev
);
229 free (filebuf_via_grub
);
230 free (filebuf_via_sys
);
234 static struct option options
[] =
236 {"device", no_argument
, 0, 'd'},
237 {"device-map", required_argument
, 0, 'm'},
238 {"target", required_argument
, 0, 't'},
239 {"help", no_argument
, 0, 'h'},
240 {"version", no_argument
, 0, 'V'},
241 {"verbose", no_argument
, 0, 'v'},
250 "Try ``grub-probe --help'' for more information.\n");
253 Usage: grub-probe [OPTION]... [PATH|DEVICE]\n\
255 Probe device information for a given path (or device, if the -d option is given).\n\
257 -d, --device given argument is a system device, not a path\n\
258 -m, --device-map=FILE use FILE as the device map [default=%s]\n\
259 -t, --target=(fs|drive|device|partmap|abstraction)\n\
260 print filesystem module, GRUB drive, system device, partition map module or abstraction module [default=fs]\n\
261 -h, --help display this message and exit\n\
262 -V, --version print version information and exit\n\
263 -v, --verbose print verbose messages\n\
265 Report bugs to <%s>.\n\
267 DEFAULT_DEVICE_MAP
, PACKAGE_BUGREPORT
);
273 main (int argc
, char *argv
[])
278 progname
= "grub-probe";
280 /* Check for options. */
283 int c
= getopt_long (argc
, argv
, "dm:t:hVv", options
, 0);
291 argument_is_device
= 1;
298 dev_map
= xstrdup (optarg
);
302 if (!strcmp (optarg
, "fs"))
304 else if (!strcmp (optarg
, "drive"))
306 else if (!strcmp (optarg
, "device"))
307 print
= PRINT_DEVICE
;
308 else if (!strcmp (optarg
, "partmap"))
309 print
= PRINT_PARTMAP
;
310 else if (!strcmp (optarg
, "abstraction"))
311 print
= PRINT_ABSTRACTION
;
321 printf ("%s (%s) %s\n", progname
, PACKAGE_NAME
, PACKAGE_VERSION
);
335 grub_env_set ("debug", "all");
337 /* Obtain ARGUMENT. */
340 fprintf (stderr
, "No path or device is specified.\n");
344 if (optind
+ 1 != argc
)
346 fprintf (stderr
, "Unknown extra argument `%s'.\n", argv
[optind
+ 1]);
350 argument
= argv
[optind
];
352 /* Initialize the emulated biosdisk driver. */
353 grub_util_biosdisk_init (dev_map
? : DEFAULT_DEVICE_MAP
);
355 /* Initialize all modules. */
359 if (argument_is_device
)
360 probe (NULL
, argument
);
362 probe (argument
, NULL
);
364 /* Free resources. */
366 grub_util_biosdisk_fini ();