~ubuntu-branches/ubuntu/trusty/grub2/trusty-updates

« back to all changes in this revision

Viewing changes to util/grub-probe.c

  • Committer: Bazaar Package Importer
  • Author(s): Robert Millan
  • Date: 2008-01-28 00:01:11 UTC
  • mto: (17.3.1 squeeze) (1.9.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20080128000111-0764agvqzg601o1d
Tags: upstream-1.95+20080128
ImportĀ upstreamĀ versionĀ 1.95+20080128

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* grub-probe.c - probe device information for a given path */
2
2
/*
3
3
 *  GRUB  --  GRand Unified Bootloader
4
 
 *  Copyright (C) 2005,2006,2007 Free Software Foundation, Inc.
 
4
 *  Copyright (C) 2005,2006,2007,2008 Free Software Foundation, Inc.
5
5
 *
6
6
 *  GRUB is free software: you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
22
22
#include <grub/util/misc.h>
23
23
#include <grub/device.h>
24
24
#include <grub/disk.h>
 
25
#include <grub/file.h>
25
26
#include <grub/fs.h>
26
27
#include <grub/partition.h>
27
28
#include <grub/pc_partition.h>
35
36
#include <unistd.h>
36
37
#include <string.h>
37
38
#include <stdlib.h>
 
39
#include <sys/stat.h>
38
40
 
39
41
#define _GNU_SOURCE     1
40
42
#include <getopt.h>
41
43
 
42
 
#define PRINT_FS        0
43
 
#define PRINT_DRIVE     1
44
 
#define PRINT_DEVICE    2
45
 
#define PRINT_PARTMAP   3
 
44
enum {
 
45
  PRINT_FS,
 
46
  PRINT_DRIVE,
 
47
  PRINT_DEVICE,
 
48
  PRINT_PARTMAP,
 
49
  PRINT_ABSTRACTION,
 
50
};
46
51
 
47
52
int print = PRINT_FS;
48
53
 
74
79
{
75
80
  char *device_name;
76
81
  char *drive_name = NULL;
77
 
  grub_device_t dev;
78
 
  grub_fs_t fs;
 
82
  char *grub_path = NULL;
 
83
  char *filebuf_via_grub = NULL, *filebuf_via_sys = NULL;
 
84
  int abstraction_type;
 
85
  grub_device_t dev = NULL;
79
86
  
80
87
  device_name = grub_guess_root_device (path);
81
88
  if (! device_name)
87
94
      goto end;
88
95
    }
89
96
 
 
97
  abstraction_type = grub_util_get_dev_abstraction (device_name);
 
98
  /* No need to check for errors; lack of abstraction is permissible.  */
 
99
  
 
100
  if (print == PRINT_ABSTRACTION)
 
101
    {
 
102
      char *abstraction_name;
 
103
      switch (abstraction_type)
 
104
        {
 
105
        case GRUB_DEV_ABSTRACTION_NONE:
 
106
          grub_util_info ("did not find LVM/RAID in %s, assuming raw device", device_name);
 
107
          goto end;
 
108
        case GRUB_DEV_ABSTRACTION_LVM:
 
109
          abstraction_name = "lvm";
 
110
          break;
 
111
        case GRUB_DEV_ABSTRACTION_RAID:
 
112
          abstraction_name = "raid";
 
113
          break;
 
114
        }
 
115
      printf ("%s\n", abstraction_name);
 
116
      goto end;
 
117
    }
 
118
 
90
119
  drive_name = grub_util_get_grub_dev (device_name);
91
120
  if (! drive_name)
92
121
    grub_util_error ("cannot find a GRUB drive for %s.\n", device_name);
122
151
      goto end;
123
152
    }
124
153
 
125
 
  fs = grub_fs_probe (dev);
126
 
  if (! fs)
127
 
    grub_util_error ("%s", grub_errmsg);
128
 
 
129
 
  printf ("%s\n", fs->name);
130
 
  
131
 
  grub_device_close (dev);
 
154
  if (print == PRINT_FS)
 
155
    {
 
156
      struct stat st;
 
157
      grub_fs_t fs;
 
158
 
 
159
      stat (path, &st);
 
160
 
 
161
      if (st.st_mode == S_IFREG)
 
162
        {
 
163
          /* Regular file.  Verify that we can read it properly.  */
 
164
 
 
165
          grub_file_t file;
 
166
          grub_util_info ("reading %s via OS facilities", path);
 
167
          filebuf_via_sys = grub_util_read_image (path);
 
168
          
 
169
          grub_util_info ("reading %s via GRUB facilities", path);
 
170
          asprintf (&grub_path, "(%s)%s", drive_name, path);
 
171
          file = grub_file_open (grub_path);
 
172
          filebuf_via_grub = xmalloc (file->size);
 
173
          grub_file_read (file, filebuf_via_grub, file->size);
 
174
          
 
175
          grub_util_info ("comparing");
 
176
          
 
177
          if (memcmp (filebuf_via_grub, filebuf_via_sys, file->size))
 
178
            grub_util_error ("files differ");
 
179
 
 
180
          fs = file->fs;
 
181
        }
 
182
      else
 
183
        {
 
184
          fs = grub_fs_probe (dev);
 
185
          if (! fs)
 
186
            grub_util_error ("%s", grub_errmsg);
 
187
        }
 
188
 
 
189
      printf ("%s\n", fs->name);
 
190
    }
132
191
 
133
192
 end:
134
 
  
 
193
  if (dev)
 
194
    grub_device_close (dev);
 
195
  free (grub_path);
 
196
  free (filebuf_via_grub);
 
197
  free (filebuf_via_sys);
135
198
  free (device_name);
136
199
  free (drive_name);
137
200
}
159
222
Probe device information for a given path.\n\
160
223
\n\
161
224
  -m, --device-map=FILE     use FILE as the device map [default=%s]\n\
162
 
  -t, --target=(fs|drive|device|partmap)\n\
163
 
                            print filesystem module, GRUB drive, system device or partition map module [default=fs]\n\
 
225
  -t, --target=(fs|drive|device|partmap|abstraction)\n\
 
226
                            print filesystem module, GRUB drive, system device, partition map module or abstraction module [default=fs]\n\
164
227
  -h, --help                display this message and exit\n\
165
228
  -V, --version             print version information and exit\n\
166
229
  -v, --verbose             print verbose messages\n\
206
269
              print = PRINT_DEVICE;
207
270
            else if (!strcmp (optarg, "partmap"))
208
271
              print = PRINT_PARTMAP;
 
272
            else if (!strcmp (optarg, "abstraction"))
 
273
              print = PRINT_ABSTRACTION;
209
274
            else
210
275
              usage (1);
211
276
            break;