]> git.proxmox.com Git - grub2.git/blame - util/grub-probe.c
2008-01-25 Robert Millan <rmh@aybabtu.com>
[grub2.git] / util / grub-probe.c
CommitLineData
ddd5cee9 1/* grub-probe.c - probe device information for a given path */
8b5f3938 2/*
3 * GRUB -- GRand Unified Bootloader
1eb8c802 4 * Copyright (C) 2005,2006,2007,2008 Free Software Foundation, Inc.
8b5f3938 5 *
5a79f472 6 * GRUB is free software: you can redistribute it and/or modify
8b5f3938 7 * it under the terms of the GNU General Public License as published by
5a79f472 8 * the Free Software Foundation, either version 3 of the License, or
8b5f3938 9 * (at your option) any later version.
10 *
5a79f472 11 * GRUB is distributed in the hope that it will be useful,
8b5f3938 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.
15 *
16 * You should have received a copy of the GNU General Public License
5a79f472 17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
8b5f3938 18 */
19
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/fs.h>
26#include <grub/partition.h>
27#include <grub/pc_partition.h>
46b9d128 28#include <grub/util/biosdisk.h>
8b5f3938 29#include <grub/util/getroot.h>
9cacaa17 30#include <grub/term.h>
8b5f3938 31
daf0f0ba 32#include <grub_probe_init.h>
33
8b5f3938 34#include <stdio.h>
35#include <unistd.h>
36#include <string.h>
37#include <stdlib.h>
38
39#define _GNU_SOURCE 1
40#include <getopt.h>
41
1eb8c802 42enum {
43 PRINT_FS,
44 PRINT_DRIVE,
45 PRINT_DEVICE,
46 PRINT_PARTMAP,
47 PRINT_ABSTRACTION,
48};
ddd5cee9 49
50int print = PRINT_FS;
51
8b5f3938 52void
53grub_putchar (int c)
54{
55 putchar (c);
56}
57
9cacaa17 58int
59grub_getkey (void)
60{
61 return -1;
62}
63
64grub_term_t
65grub_term_get_current (void)
66{
67 return 0;
68}
69
8b5f3938 70void
71grub_refresh (void)
72{
73}
74
75static void
76probe (const char *path)
77{
78 char *device_name;
0215dcbf 79 char *drive_name = NULL;
1eb8c802 80 int abstraction_type;
8b5f3938 81 grub_device_t dev;
82 grub_fs_t fs;
83
84 device_name = grub_guess_root_device (path);
85 if (! device_name)
baa574b4 86 grub_util_error ("cannot find a device for %s.\n", path);
ddd5cee9 87
88 if (print == PRINT_DEVICE)
89 {
90 printf ("%s\n", device_name);
e0994b8b 91 goto end;
ddd5cee9 92 }
93
1eb8c802 94 abstraction_type = grub_util_get_dev_abstraction (device_name);
95 /* No need to check for errors; lack of abstraction is permissible. */
96
97 if (print == PRINT_ABSTRACTION)
98 {
99 char *abstraction_name;
100 switch (abstraction_type)
101 {
102 case GRUB_DEV_ABSTRACTION_NONE:
103 grub_util_info ("did not find LVM/RAID in %s, assuming raw device", device_name);
104 goto end;
105 case GRUB_DEV_ABSTRACTION_LVM:
106 abstraction_name = "lvm";
107 break;
108 case GRUB_DEV_ABSTRACTION_RAID:
109 abstraction_name = "raid";
110 break;
111 }
112 printf ("%s\n", abstraction_name);
113 goto end;
114 }
115
849d55d3 116 drive_name = grub_util_get_grub_dev (device_name);
117 if (! drive_name)
baa574b4 118 grub_util_error ("cannot find a GRUB drive for %s.\n", device_name);
790707f2 119
ddd5cee9 120 if (print == PRINT_DRIVE)
121 {
122 printf ("(%s)\n", drive_name);
e0994b8b 123 goto end;
8b5f3938 124 }
125
e0994b8b 126 grub_util_info ("opening %s", drive_name);
127 dev = grub_device_open (drive_name);
8b5f3938 128 if (! dev)
129 grub_util_error ("%s", grub_errmsg);
130
75f396cc 131 if (print == PRINT_PARTMAP)
132 {
02e7b75e 133 if (dev->disk->partition == NULL)
134 grub_util_error ("Cannot detect partition map for %s", drive_name);
135
75f396cc 136 if (strcmp (dev->disk->partition->partmap->name, "amiga_partition_map") == 0)
137 printf ("amiga\n");
138 else if (strcmp (dev->disk->partition->partmap->name, "apple_partition_map") == 0)
139 printf ("apple\n");
140 else if (strcmp (dev->disk->partition->partmap->name, "gpt_partition_map") == 0)
141 printf ("gpt\n");
142 else if (strcmp (dev->disk->partition->partmap->name, "pc_partition_map") == 0)
143 printf ("pc\n");
144 else if (strcmp (dev->disk->partition->partmap->name, "sun_partition_map") == 0)
145 printf ("sun\n");
146 else
147 grub_util_error ("Unknown partition map %s", dev->disk->partition->partmap->name);
148 goto end;
149 }
150
8b5f3938 151 fs = grub_fs_probe (dev);
152 if (! fs)
153 grub_util_error ("%s", grub_errmsg);
154
155 printf ("%s\n", fs->name);
156
157 grub_device_close (dev);
e0994b8b 158
159 end:
160
8b5f3938 161 free (device_name);
e0994b8b 162 free (drive_name);
8b5f3938 163}
164
165static struct option options[] =
166 {
167 {"device-map", required_argument, 0, 'm'},
ddd5cee9 168 {"target", required_argument, 0, 't'},
8b5f3938 169 {"help", no_argument, 0, 'h'},
170 {"version", no_argument, 0, 'V'},
171 {"verbose", no_argument, 0, 'v'},
172 {0, 0, 0, 0}
173 };
174
175static void
176usage (int status)
177{
178 if (status)
179 fprintf (stderr,
ddd5cee9 180 "Try ``grub-probe --help'' for more information.\n");
8b5f3938 181 else
182 printf ("\
ddd5cee9 183Usage: grub-probe [OPTION]... PATH\n\
8b5f3938 184\n\
ddd5cee9 185Probe device information for a given path.\n\
8b5f3938 186\n\
187 -m, --device-map=FILE use FILE as the device map [default=%s]\n\
1eb8c802 188 -t, --target=(fs|drive|device|partmap|abstraction)\n\
189 print filesystem module, GRUB drive, system device, partition map module or abstraction module [default=fs]\n\
8b5f3938 190 -h, --help display this message and exit\n\
191 -V, --version print version information and exit\n\
192 -v, --verbose print verbose messages\n\
193\n\
194Report bugs to <%s>.\n\
195",
196 DEFAULT_DEVICE_MAP, PACKAGE_BUGREPORT);
197
198 exit (status);
199}
200
201int
202main (int argc, char *argv[])
203{
204 char *dev_map = 0;
205 char *path;
206
ddd5cee9 207 progname = "grub-probe";
8b5f3938 208
209 /* Check for options. */
210 while (1)
211 {
ddd5cee9 212 int c = getopt_long (argc, argv, "m:t:hVv", options, 0);
8b5f3938 213
214 if (c == -1)
215 break;
216 else
217 switch (c)
218 {
219 case 'm':
220 if (dev_map)
221 free (dev_map);
222
223 dev_map = xstrdup (optarg);
224 break;
225
ddd5cee9 226 case 't':
227 if (!strcmp (optarg, "fs"))
228 print = PRINT_FS;
229 else if (!strcmp (optarg, "drive"))
230 print = PRINT_DRIVE;
231 else if (!strcmp (optarg, "device"))
232 print = PRINT_DEVICE;
75f396cc 233 else if (!strcmp (optarg, "partmap"))
234 print = PRINT_PARTMAP;
1eb8c802 235 else if (!strcmp (optarg, "abstraction"))
236 print = PRINT_ABSTRACTION;
ddd5cee9 237 else
238 usage (1);
239 break;
240
8b5f3938 241 case 'h':
242 usage (0);
243 break;
244
245 case 'V':
246 printf ("%s (%s) %s\n", progname, PACKAGE_NAME, PACKAGE_VERSION);
247 return 0;
248
249 case 'v':
250 verbosity++;
251 break;
252
253 default:
254 usage (1);
255 break;
256 }
257 }
258
259 /* Obtain PATH. */
260 if (optind >= argc)
261 {
262 fprintf (stderr, "No path is specified.\n");
263 usage (1);
264 }
265
266 if (optind + 1 != argc)
267 {
268 fprintf (stderr, "Unknown extra argument `%s'.\n", argv[optind + 1]);
269 usage (1);
270 }
271
272 path = argv[optind];
273
274 /* Initialize the emulated biosdisk driver. */
275 grub_util_biosdisk_init (dev_map ? : DEFAULT_DEVICE_MAP);
790707f2 276
daf0f0ba 277 /* Initialize all modules. */
278 grub_init_all ();
8b5f3938 279
280 /* Do it. */
281 probe (path);
282
283 /* Free resources. */
daf0f0ba 284 grub_fini_all ();
8b5f3938 285 grub_util_biosdisk_fini ();
286
287 free (dev_map);
288
289 return 0;
290}