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

« back to all changes in this revision

Viewing changes to grub-core/osdep/unix/getroot.c

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2014-01-16 15:18:04 UTC
  • mfrom: (17.6.38 experimental)
  • Revision ID: package-import@ubuntu.com-20140116151804-3foouk7fpqcq3sxx
Tags: 2.02~beta2-2
* Convert patch handling to git-dpm.
* Add bi-endian support to ELF parser (Tomohiro B Berry).
* Adjust restore_mkdevicemap.patch to mark get_kfreebsd_version as static,
  to appease "gcc -Werror=missing-prototypes".
* Cherry-pick from upstream:
  - Change grub-macbless' manual page section to 8.
* Install grub-glue-efi, grub-macbless, grub-render-label, and
  grub-syslinux2cfg.
* grub-shell: Pass -no-pad to xorriso when building floppy images.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  GRUB  --  GRand Unified Bootloader
 
3
 *  Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009,2010,2011  Free Software Foundation, Inc.
 
4
 *
 
5
 *  GRUB is free software: you can redistribute it and/or modify
 
6
 *  it under the terms of the GNU General Public License as published by
 
7
 *  the Free Software Foundation, either version 3 of the License, or
 
8
 *  (at your option) any later version.
 
9
 *
 
10
 *  GRUB is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 
17
 */
 
18
 
 
19
#include <config-util.h>
 
20
#include <config.h>
 
21
 
 
22
#include <sys/stat.h>
 
23
#include <sys/types.h>
 
24
#include <assert.h>
 
25
#include <fcntl.h>
 
26
#include <unistd.h>
 
27
#include <string.h>
 
28
#include <dirent.h>
 
29
#include <errno.h>
 
30
#include <error.h>
 
31
#include <stdio.h>
 
32
#include <stdlib.h>
 
33
#include <stdint.h>
 
34
#ifdef HAVE_LIMITS_H
 
35
#include <limits.h>
 
36
#endif
 
37
#include <grub/util/misc.h>
 
38
#include <grub/emu/exec.h>
 
39
 
 
40
#include <grub/cryptodisk.h>
 
41
#include <grub/i18n.h>
 
42
 
 
43
#if !defined (__MINGW32__) && !defined (__CYGWIN__) && !defined (__AROS__) && !defined (__HAIKU__)
 
44
 
 
45
#ifdef __linux__
 
46
#include <sys/ioctl.h>         /* ioctl */
 
47
#include <sys/mount.h>
 
48
#ifndef MAJOR
 
49
# ifndef MINORBITS
 
50
#  define MINORBITS     8
 
51
# endif /* ! MINORBITS */
 
52
# define MAJOR(dev)     ((unsigned) ((dev) >> MINORBITS))
 
53
#endif /* ! MAJOR */
 
54
#ifndef FLOPPY_MAJOR
 
55
# define FLOPPY_MAJOR   2
 
56
#endif /* ! FLOPPY_MAJOR */
 
57
#endif
 
58
 
 
59
#include <sys/types.h>
 
60
 
 
61
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
 
62
# include <grub/util/libzfs.h>
 
63
# include <grub/util/libnvpair.h>
 
64
#endif
 
65
 
 
66
#include <grub/mm.h>
 
67
#include <grub/misc.h>
 
68
#include <grub/emu/misc.h>
 
69
#include <grub/emu/hostdisk.h>
 
70
#include <grub/emu/getroot.h>
 
71
 
 
72
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
 
73
# define MAJOR(dev) major(dev)
 
74
# define FLOPPY_MAJOR   2
 
75
#endif
 
76
 
 
77
#if defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
 
78
#include <sys/mount.h>
 
79
#endif
 
80
 
 
81
#if defined(__NetBSD__) || defined(__OpenBSD__)
 
82
# include <sys/ioctl.h>
 
83
# include <sys/disklabel.h>    /* struct disklabel */
 
84
# include <sys/disk.h>    /* struct dkwedge_info */
 
85
#include <sys/param.h>
 
86
#include <sys/mount.h>
 
87
#endif /* defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) */
 
88
 
 
89
#if defined(__NetBSD__) || defined(__OpenBSD__)
 
90
# define MAJOR(dev) major(dev)
 
91
# ifdef HAVE_GETRAWPARTITION
 
92
#  include <util.h>    /* getrawpartition */
 
93
# endif /* HAVE_GETRAWPARTITION */
 
94
#if defined(__NetBSD__)
 
95
# include <sys/fdio.h>
 
96
#endif
 
97
# ifndef FLOPPY_MAJOR
 
98
#  define FLOPPY_MAJOR  2
 
99
# endif /* ! FLOPPY_MAJOR */
 
100
# ifndef RAW_FLOPPY_MAJOR
 
101
#  define RAW_FLOPPY_MAJOR      9
 
102
# endif /* ! RAW_FLOPPY_MAJOR */
 
103
#endif /* defined(__NetBSD__) */
 
104
 
 
105
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
 
106
#define LVM_DEV_MAPPER_STRING "/dev/linux_lvm/"
 
107
#else
 
108
#define LVM_DEV_MAPPER_STRING "/dev/mapper/"
 
109
#endif
 
110
 
 
111
#include <sys/types.h>
 
112
#include <sys/wait.h>
 
113
 
 
114
#if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) && defined(HAVE_STRUCT_STATFS_F_MNTFROMNAME)
 
115
#include <sys/param.h>
 
116
#include <sys/mount.h>
 
117
#endif
 
118
 
 
119
#if !defined (__GNU__)
 
120
static void
 
121
strip_extra_slashes (char *dir)
 
122
{
 
123
  char *p = dir;
 
124
 
 
125
  while ((p = strchr (p, '/')) != 0)
 
126
    {
 
127
      if (p[1] == '/')
 
128
        {
 
129
          memmove (p, p + 1, strlen (p));
 
130
          continue;
 
131
        }
 
132
      else if (p[1] == '\0')
 
133
        {
 
134
          if (p > dir)
 
135
            p[0] = '\0';
 
136
          break;
 
137
        }
 
138
 
 
139
      p++;
 
140
    }
 
141
}
 
142
 
 
143
static char *
 
144
xgetcwd (void)
 
145
{
 
146
  size_t size = 10;
 
147
  char *path;
 
148
 
 
149
  path = xmalloc (size);
 
150
  while (! getcwd (path, size))
 
151
    {
 
152
      size <<= 1;
 
153
      path = xrealloc (path, size);
 
154
    }
 
155
 
 
156
  return path;
 
157
}
 
158
 
 
159
char **
 
160
grub_util_find_root_devices_from_poolname (char *poolname)
 
161
{
 
162
  char **devices = 0;
 
163
  size_t ndevices = 0;
 
164
  size_t devices_allocated = 0;
 
165
 
 
166
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
 
167
  zpool_handle_t *zpool;
 
168
  libzfs_handle_t *libzfs;
 
169
  nvlist_t *config, *vdev_tree;
 
170
  nvlist_t **children;
 
171
  unsigned int nvlist_count;
 
172
  unsigned int i;
 
173
  char *device = 0;
 
174
 
 
175
  libzfs = grub_get_libzfs_handle ();
 
176
  if (! libzfs)
 
177
    return NULL;
 
178
 
 
179
  zpool = zpool_open (libzfs, poolname);
 
180
  config = zpool_get_config (zpool, NULL);
 
181
 
 
182
  if (nvlist_lookup_nvlist (config, "vdev_tree", &vdev_tree) != 0)
 
183
    error (1, errno, "nvlist_lookup_nvlist (\"vdev_tree\")");
 
184
 
 
185
  if (nvlist_lookup_nvlist_array (vdev_tree, "children", &children, &nvlist_count) != 0)
 
186
    error (1, errno, "nvlist_lookup_nvlist_array (\"children\")");
 
187
  assert (nvlist_count > 0);
 
188
 
 
189
  while (nvlist_lookup_nvlist_array (children[0], "children",
 
190
                                     &children, &nvlist_count) == 0)
 
191
    assert (nvlist_count > 0);
 
192
 
 
193
  for (i = 0; i < nvlist_count; i++)
 
194
    {
 
195
      if (nvlist_lookup_string (children[i], "path", &device) != 0)
 
196
        error (1, errno, "nvlist_lookup_string (\"path\")");
 
197
 
 
198
      struct stat st;
 
199
      if (stat (device, &st) == 0)
 
200
        {
 
201
#ifdef __sun__
 
202
          if (grub_memcmp (device, "/dev/dsk/", sizeof ("/dev/dsk/") - 1)
 
203
              == 0)
 
204
            device = xasprintf ("/dev/rdsk/%s",
 
205
                                device + sizeof ("/dev/dsk/") - 1);
 
206
          else if (grub_memcmp (device, "/devices", sizeof ("/devices") - 1)
 
207
                   == 0
 
208
                   && grub_memcmp (device + strlen (device) - 4,
 
209
                                   ",raw", 4) != 0)
 
210
            device = xasprintf ("%s,raw", device);
 
211
          else
 
212
#endif
 
213
            device = xstrdup (device);
 
214
          if (ndevices >= devices_allocated)
 
215
            {
 
216
              devices_allocated = 2 * (devices_allocated + 8);
 
217
              devices = xrealloc (devices, sizeof (devices[0])
 
218
                                  * devices_allocated);
 
219
            }
 
220
          devices[ndevices++] = device;
 
221
        }
 
222
 
 
223
      device = NULL;
 
224
    }
 
225
 
 
226
  zpool_close (zpool);
 
227
#else
 
228
  FILE *fp;
 
229
  int ret;
 
230
  char *line;
 
231
  size_t len;
 
232
  int st;
 
233
 
 
234
  char name[PATH_MAX + 1], state[257], readlen[257], writelen[257];
 
235
  char cksum[257], notes[257];
 
236
  unsigned int dummy;
 
237
  const char *argv[4];
 
238
  pid_t pid;
 
239
  int fd;
 
240
 
 
241
  argv[0] = "zpool";
 
242
  argv[1] = "status";
 
243
  argv[2] = poolname;
 
244
  argv[3] = NULL;
 
245
 
 
246
  pid = grub_util_exec_pipe (argv, &fd);
 
247
  if (!pid)
 
248
    return NULL;
 
249
 
 
250
  fp = fdopen (fd, "r");
 
251
  if (!fp)
 
252
    {
 
253
      grub_util_warn (_("Unable to open stream from %s: %s"),
 
254
                      "zpool", strerror (errno));
 
255
      goto out;
 
256
    }
 
257
 
 
258
  st = 0;
 
259
  while (1)
 
260
    {
 
261
      line = NULL;
 
262
      ret = getline (&line, &len, fp);
 
263
      if (ret == -1)
 
264
        break;
 
265
        
 
266
      if (sscanf (line, " %s %256s %256s %256s %256s %256s",
 
267
                  name, state, readlen, writelen, cksum, notes) >= 5)
 
268
        switch (st)
 
269
          {
 
270
          case 0:
 
271
            if (!strcmp (name, "NAME")
 
272
                && !strcmp (state, "STATE")
 
273
                && !strcmp (readlen, "READ")
 
274
                && !strcmp (writelen, "WRITE")
 
275
                && !strcmp (cksum, "CKSUM"))
 
276
              st++;
 
277
            break;
 
278
          case 1:
 
279
            {
 
280
              char *ptr = line;
 
281
              while (1)
 
282
                {
 
283
                  if (strncmp (ptr, poolname, strlen (poolname)) == 0
 
284
                      && grub_isspace(ptr[strlen (poolname)]))
 
285
                    st++;
 
286
                  if (!grub_isspace (*ptr))
 
287
                    break;
 
288
                  ptr++;
 
289
                }
 
290
            }
 
291
            break;
 
292
          case 2:
 
293
            if (strcmp (name, "mirror") && !sscanf (name, "mirror-%u", &dummy)
 
294
                && !sscanf (name, "raidz%u", &dummy)
 
295
                && !sscanf (name, "raidz1%u", &dummy)
 
296
                && !sscanf (name, "raidz2%u", &dummy)
 
297
                && !sscanf (name, "raidz3%u", &dummy)
 
298
                && !strcmp (state, "ONLINE"))
 
299
              {
 
300
                if (ndevices >= devices_allocated)
 
301
                  {
 
302
                    devices_allocated = 2 * (devices_allocated + 8);
 
303
                    devices = xrealloc (devices, sizeof (devices[0])
 
304
                                        * devices_allocated);
 
305
                  }
 
306
                if (name[0] == '/')
 
307
                  devices[ndevices++] = xstrdup (name);
 
308
                else
 
309
                  devices[ndevices++] = xasprintf ("/dev/%s", name);
 
310
              }
 
311
            break;
 
312
          }
 
313
        
 
314
      free (line);
 
315
    }
 
316
 
 
317
 out:
 
318
  close (fd);
 
319
  waitpid (pid, NULL, 0);
 
320
#endif
 
321
  if (devices)
 
322
    {
 
323
      if (ndevices >= devices_allocated)
 
324
        {
 
325
          devices_allocated = 2 * (devices_allocated + 8);
 
326
          devices = xrealloc (devices, sizeof (devices[0])
 
327
                              * devices_allocated);
 
328
        }
 
329
      devices[ndevices++] = 0;
 
330
    }
 
331
  return devices;
 
332
}
 
333
 
 
334
static char **
 
335
find_root_devices_from_libzfs (const char *dir)
 
336
{
 
337
  char **devices = NULL;
 
338
  char *poolname;
 
339
  char *poolfs;
 
340
 
 
341
  grub_find_zpool_from_dir (dir, &poolname, &poolfs);
 
342
  if (! poolname)
 
343
    return NULL;
 
344
 
 
345
  devices = grub_util_find_root_devices_from_poolname (poolname);
 
346
 
 
347
  free (poolname);
 
348
  if (poolfs)
 
349
    free (poolfs);
 
350
 
 
351
  return devices;
 
352
}
 
353
 
 
354
char *
 
355
grub_find_device (const char *dir, dev_t dev)
 
356
{
 
357
  DIR *dp;
 
358
  char *saved_cwd;
 
359
  struct dirent *ent;
 
360
 
 
361
  if (! dir)
 
362
    dir = "/dev";
 
363
 
 
364
  dp = opendir (dir);
 
365
  if (! dp)
 
366
    return 0;
 
367
 
 
368
  saved_cwd = xgetcwd ();
 
369
 
 
370
  grub_util_info ("changing current directory to %s", dir);
 
371
  if (chdir (dir) < 0)
 
372
    {
 
373
      free (saved_cwd);
 
374
      closedir (dp);
 
375
      return 0;
 
376
    }
 
377
 
 
378
  while ((ent = readdir (dp)) != 0)
 
379
    {
 
380
      struct stat st;
 
381
 
 
382
      /* Avoid:
 
383
         - dotfiles (like "/dev/.tmp.md0") since they could be duplicates.
 
384
         - dotdirs (like "/dev/.static") since they could contain duplicates.  */
 
385
      if (ent->d_name[0] == '.')
 
386
        continue;
 
387
 
 
388
      if (lstat (ent->d_name, &st) < 0)
 
389
        /* Ignore any error.  */
 
390
        continue;
 
391
 
 
392
      if (S_ISLNK (st.st_mode)) {
 
393
#ifdef __linux__
 
394
        if (strcmp (dir, "mapper") == 0 || strcmp (dir, "/dev/mapper") == 0) {
 
395
          /* Follow symbolic links under /dev/mapper/; the canonical name
 
396
             may be something like /dev/dm-0, but the names under
 
397
             /dev/mapper/ are more human-readable and so we prefer them if
 
398
             we can get them.  */
 
399
          if (stat (ent->d_name, &st) < 0)
 
400
            continue;
 
401
        } else
 
402
#endif /* __linux__ */
 
403
        /* Don't follow other symbolic links.  */
 
404
        continue;
 
405
      }
 
406
 
 
407
      if (S_ISDIR (st.st_mode))
 
408
        {
 
409
          /* Find it recursively.  */
 
410
          char *res;
 
411
 
 
412
          res = grub_find_device (ent->d_name, dev);
 
413
 
 
414
          if (res)
 
415
            {
 
416
              if (chdir (saved_cwd) < 0)
 
417
                grub_util_error ("%s",
 
418
                                 _("cannot restore the original directory"));
 
419
 
 
420
              free (saved_cwd);
 
421
              closedir (dp);
 
422
              return res;
 
423
            }
 
424
        }
 
425
 
 
426
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__)
 
427
      if (S_ISCHR (st.st_mode) && st.st_rdev == dev)
 
428
#else
 
429
      if (S_ISBLK (st.st_mode) && st.st_rdev == dev)
 
430
#endif
 
431
        {
 
432
#ifdef __linux__
 
433
          /* Skip device names like /dev/dm-0, which are short-hand aliases
 
434
             to more descriptive device names, e.g. those under /dev/mapper */
 
435
          if (ent->d_name[0] == 'd' &&
 
436
              ent->d_name[1] == 'm' &&
 
437
              ent->d_name[2] == '-' &&
 
438
              ent->d_name[3] >= '0' &&
 
439
              ent->d_name[3] <= '9')
 
440
            continue;
 
441
#endif
 
442
 
 
443
          /* Found!  */
 
444
          char *res;
 
445
          char *cwd;
 
446
 
 
447
          cwd = xgetcwd ();
 
448
          res = xmalloc (strlen (cwd) + strlen (ent->d_name) + 3);
 
449
          sprintf (res, 
 
450
#if defined(__NetBSD__) || defined(__OpenBSD__)
 
451
                   /* Convert this block device to its character (raw) device.  */
 
452
                   "%s/r%s",
 
453
#else
 
454
                   /* Keep the device name as it is.  */
 
455
                   "%s/%s",
 
456
#endif
 
457
                   cwd, ent->d_name);
 
458
          strip_extra_slashes (res);
 
459
          free (cwd);
 
460
 
 
461
          /* /dev/root is not a real block device keep looking, takes care
 
462
             of situation where root filesystem is on the same partition as
 
463
             grub files */
 
464
 
 
465
          if (strcmp(res, "/dev/root") == 0)
 
466
            {
 
467
              free (res);
 
468
              continue;
 
469
            }
 
470
 
 
471
          if (chdir (saved_cwd) < 0)
 
472
            grub_util_error ("%s", _("cannot restore the original directory"));
 
473
 
 
474
          free (saved_cwd);
 
475
          closedir (dp);
 
476
          return res;
 
477
        }
 
478
    }
 
479
 
 
480
  if (chdir (saved_cwd) < 0)
 
481
    grub_util_error ("%s", _("cannot restore the original directory"));
 
482
 
 
483
  free (saved_cwd);
 
484
  closedir (dp);
 
485
  return 0;
 
486
}
 
487
 
 
488
char **
 
489
grub_guess_root_devices (const char *dir_in)
 
490
{
 
491
  char **os_dev = NULL;
 
492
  struct stat st;
 
493
  dev_t dev;
 
494
  char *dir = canonicalize_file_name (dir_in);
 
495
 
 
496
  if (!dir)
 
497
    grub_util_error (_("failed to get canonical path of `%s'"), dir_in);
 
498
 
 
499
#ifdef __linux__
 
500
  if (!os_dev)
 
501
    os_dev = grub_find_root_devices_from_mountinfo (dir, NULL);
 
502
#endif /* __linux__ */
 
503
 
 
504
  if (!os_dev)
 
505
    os_dev = find_root_devices_from_libzfs (dir);
 
506
 
 
507
  if (os_dev)
 
508
    {
 
509
      char **cur;
 
510
      for (cur = os_dev; *cur; cur++)
 
511
        {
 
512
          char *tmp = *cur;
 
513
          int root, dm;
 
514
          if (strcmp (*cur, "/dev/root") == 0
 
515
              || strncmp (*cur, "/dev/dm-", sizeof ("/dev/dm-") - 1) == 0)
 
516
            *cur = tmp;
 
517
          else
 
518
            {
 
519
              *cur = canonicalize_file_name (tmp);
 
520
              if (*cur == NULL)
 
521
                grub_util_error (_("failed to get canonical path of `%s'"), tmp);
 
522
              free (tmp);
 
523
            }
 
524
          root = (strcmp (*cur, "/dev/root") == 0);
 
525
          dm = (strncmp (*cur, "/dev/dm-", sizeof ("/dev/dm-") - 1) == 0);
 
526
          if (!dm && !root)
 
527
            continue;
 
528
          if (stat (*cur, &st) < 0)
 
529
            break;
 
530
          free (*cur);
 
531
          dev = st.st_rdev;
 
532
          *cur = grub_find_device (dm ? "/dev/mapper" : "/dev", dev);
 
533
        }
 
534
      if (!*cur)
 
535
        return os_dev;
 
536
      for (cur = os_dev; *cur; cur++)
 
537
        free (*cur);
 
538
      free (os_dev);
 
539
      os_dev = 0;
 
540
    }
 
541
 
 
542
  if (stat (dir, &st) < 0)
 
543
    grub_util_error (_("cannot stat `%s': %s"), dir, strerror (errno));
 
544
 
 
545
  dev = st.st_dev;
 
546
 
 
547
  os_dev = xmalloc (2 * sizeof (os_dev[0]));
 
548
 
 
549
  /* This might be truly slow, but is there any better way?  */
 
550
  os_dev[0] = grub_find_device ("/dev", dev);
 
551
 
 
552
  if (!os_dev[0])
 
553
    {
 
554
      free (os_dev);
 
555
      return 0;
 
556
    }
 
557
 
 
558
  os_dev[1] = 0;
 
559
 
 
560
  return os_dev;
 
561
}
 
562
 
 
563
#endif
 
564
 
 
565
void
 
566
grub_util_pull_lvm_by_command (const char *os_dev)
 
567
{
 
568
  const char *argv[8];
 
569
  int fd;
 
570
  pid_t pid;
 
571
  FILE *vgs;
 
572
  char *buf = NULL;
 
573
  size_t len = 0;
 
574
  char *vgname = NULL;
 
575
  const char *iptr;
 
576
  char *optr;
 
577
  char *vgid = NULL;
 
578
  grub_size_t vgidlen = 0;
 
579
 
 
580
  vgid = grub_util_get_vg_uuid (os_dev);
 
581
  if (vgid)
 
582
    vgidlen = grub_strlen (vgid);
 
583
 
 
584
  if (!vgid)
 
585
    {
 
586
      if (strncmp (os_dev, LVM_DEV_MAPPER_STRING,
 
587
                   sizeof (LVM_DEV_MAPPER_STRING) - 1)
 
588
          != 0)
 
589
        return;
 
590
 
 
591
      vgname = xmalloc (strlen (os_dev + sizeof (LVM_DEV_MAPPER_STRING) - 1) + 1);
 
592
      for (iptr = os_dev + sizeof (LVM_DEV_MAPPER_STRING) - 1, optr = vgname; *iptr; )
 
593
        if (*iptr != '-')
 
594
          *optr++ = *iptr++;
 
595
        else if (iptr[0] == '-' && iptr[1] == '-')
 
596
          {
 
597
            iptr += 2;
 
598
            *optr++ = '-';
 
599
          }
 
600
        else
 
601
          break;
 
602
      *optr = '\0';
 
603
    }
 
604
 
 
605
  /* by default PV name is left aligned in 10 character field, meaning that
 
606
     we do not know where name ends. Using dummy --separator disables
 
607
     alignment. We have a single field, so separator itself is not output */
 
608
  argv[0] = "vgs";
 
609
  argv[1] = "--options";
 
610
  if (vgid)
 
611
    argv[2] = "vg_uuid,pv_name";
 
612
  else
 
613
    argv[2] = "pv_name";
 
614
  argv[3] = "--noheadings";
 
615
  argv[4] = "--separator";
 
616
  argv[5] = ":";
 
617
  argv[6] = vgname;
 
618
  argv[7] = NULL;
 
619
 
 
620
  pid = grub_util_exec_pipe (argv, &fd);
 
621
  free (vgname);
 
622
 
 
623
  if (!pid)
 
624
    return;
 
625
 
 
626
  /* Parent.  Read vgs' output.  */
 
627
  vgs = fdopen (fd, "r");
 
628
  if (! vgs)
 
629
    {
 
630
      grub_util_warn (_("Unable to open stream from %s: %s"),
 
631
                      "vgs", strerror (errno));
 
632
      goto out;
 
633
    }
 
634
 
 
635
  while (getline (&buf, &len, vgs) > 0)
 
636
    {
 
637
      char *ptr;
 
638
      /* LVM adds two spaces as standard prefix */
 
639
      for (ptr = buf; ptr < buf + 2 && *ptr == ' '; ptr++);
 
640
 
 
641
      if (vgid && (grub_strncmp (vgid, ptr, vgidlen) != 0
 
642
                   || ptr[vgidlen] != ':'))
 
643
        continue;
 
644
      if (vgid)
 
645
        ptr += vgidlen + 1;
 
646
      if (*ptr == '\0')
 
647
        continue;
 
648
      *(ptr + strlen (ptr) - 1) = '\0';
 
649
      grub_util_pull_device (ptr);
 
650
    }
 
651
 
 
652
out:
 
653
  close (fd);
 
654
  waitpid (pid, NULL, 0);
 
655
  free (buf);
 
656
}
 
657
 
 
658
/* ZFS has similar problems to those of btrfs (see above).  */
 
659
void
 
660
grub_find_zpool_from_dir (const char *dir, char **poolname, char **poolfs)
 
661
{
 
662
  char *slash;
 
663
 
 
664
  *poolname = *poolfs = NULL;
 
665
 
 
666
#if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) && defined(HAVE_STRUCT_STATFS_F_MNTFROMNAME)
 
667
  /* FreeBSD and GNU/kFreeBSD.  */
 
668
  {
 
669
    struct statfs mnt;
 
670
 
 
671
    if (statfs (dir, &mnt) != 0)
 
672
      return;
 
673
 
 
674
    if (strcmp (mnt.f_fstypename, "zfs") != 0)
 
675
      return;
 
676
 
 
677
    *poolname = xstrdup (mnt.f_mntfromname);
 
678
  }
 
679
#elif defined(HAVE_GETEXTMNTENT)
 
680
  /* Solaris.  */
 
681
  {
 
682
    struct stat st;
 
683
    struct extmnttab mnt;
 
684
 
 
685
    if (stat (dir, &st) != 0)
 
686
      return;
 
687
 
 
688
    FILE *mnttab = grub_util_fopen ("/etc/mnttab", "r");
 
689
    if (! mnttab)
 
690
      return;
 
691
 
 
692
    while (getextmntent (mnttab, &mnt, sizeof (mnt)) == 0)
 
693
      {
 
694
        if (makedev (mnt.mnt_major, mnt.mnt_minor) == st.st_dev
 
695
            && !strcmp (mnt.mnt_fstype, "zfs"))
 
696
          {
 
697
            *poolname = xstrdup (mnt.mnt_special);
 
698
            break;
 
699
          }
 
700
      }
 
701
 
 
702
    fclose (mnttab);
 
703
  }
 
704
#endif
 
705
 
 
706
  if (! *poolname)
 
707
    return;
 
708
 
 
709
  slash = strchr (*poolname, '/');
 
710
  if (slash)
 
711
    {
 
712
      *slash = '\0';
 
713
      *poolfs = xstrdup (slash + 1);
 
714
    }
 
715
  else
 
716
    *poolfs = xstrdup ("");
 
717
}
 
718
 
 
719
int
 
720
grub_util_biosdisk_is_floppy (grub_disk_t disk)
 
721
{
 
722
  struct stat st;
 
723
  int fd;
 
724
  const char *dname;
 
725
 
 
726
  dname = grub_util_biosdisk_get_osdev (disk);
 
727
 
 
728
  if (!dname)
 
729
    return 0;
 
730
 
 
731
  fd = open (dname, O_RDONLY);
 
732
  /* Shouldn't happen.  */
 
733
  if (fd == -1)
 
734
    return 0;
 
735
 
 
736
  /* Shouldn't happen either.  */
 
737
  if (fstat (fd, &st) < 0)
 
738
    {
 
739
      close (fd);
 
740
      return 0;
 
741
    }
 
742
 
 
743
  close (fd);
 
744
 
 
745
#if defined(__NetBSD__)
 
746
  if (major(st.st_rdev) == RAW_FLOPPY_MAJOR)
 
747
    return 1;
 
748
#endif
 
749
 
 
750
#if defined(FLOPPY_MAJOR)
 
751
  if (major(st.st_rdev) == FLOPPY_MAJOR)
 
752
#else
 
753
  /* Some kernels (e.g. kFreeBSD) don't have a static major number
 
754
     for floppies, but they still use a "fd[0-9]" pathname.  */
 
755
  if (dname[5] == 'f'
 
756
      && dname[6] == 'd'
 
757
      && dname[7] >= '0'
 
758
      && dname[7] <= '9')
 
759
#endif
 
760
    return 1;
 
761
 
 
762
  return 0;
 
763
}
 
764
 
 
765
#else
 
766
 
 
767
#include <grub/emu/getroot.h>
 
768
 
 
769
void
 
770
grub_util_pull_lvm_by_command (const char *os_dev __attribute__ ((unused)))
 
771
{
 
772
}
 
773
 
 
774
#endif