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

« back to all changes in this revision

Viewing changes to kern/device.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2011-02-08 11:39:26 UTC
  • mfrom: (17.6.26 experimental)
  • mto: (17.6.27 experimental)
  • mto: This revision was merged to the branch mainline in revision 104.
  • Revision ID: james.westby@ubuntu.com-20110208113926-clfs90haboyk9zip
Tags: 1.99~rc1-2
* Merge 1.98+20100804-13 and 1.98+20100804-14, updating translations:
  - Kazakh (Baurzhan Muftakhidinov / Timur Birsh).
* mkconfig_skip_dmcrypt.patch: Refer to GRUB_PRELOAD_MODULES rather than
  suggesting people write a /etc/grub.d/01_modules script (thanks, Jordan
  Uggla).
* Handle empty dir passed to grub_find_root_device_from_mountinfo; fixes
  grub-mkrelpath on btrfs subvolumes (LP: #712029).
* Add rootflags=subvol=<name> if / is on a btrfs subvolume (LP: #712029).
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* device.c - device manager */
2
 
/*
3
 
 *  GRUB  --  GRand Unified Bootloader
4
 
 *  Copyright (C) 2002,2005,2007,2008,2009  Free Software Foundation, Inc.
5
 
 *
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.
10
 
 *
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.
15
 
 *
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/>.
18
 
 */
19
 
 
20
 
#include <grub/device.h>
21
 
#include <grub/disk.h>
22
 
#include <grub/net.h>
23
 
#include <grub/fs.h>
24
 
#include <grub/mm.h>
25
 
#include <grub/misc.h>
26
 
#include <grub/env.h>
27
 
#include <grub/partition.h>
28
 
 
29
 
grub_device_t
30
 
grub_device_open (const char *name)
31
 
{
32
 
  grub_disk_t disk = 0;
33
 
  grub_device_t dev = 0;
34
 
 
35
 
  if (! name)
36
 
    {
37
 
      name = grub_env_get ("root");
38
 
      if (*name == '\0')
39
 
        {
40
 
          grub_error (GRUB_ERR_BAD_DEVICE, "no device is set");
41
 
          goto fail;
42
 
        }
43
 
    }
44
 
 
45
 
  dev = grub_malloc (sizeof (*dev));
46
 
  if (! dev)
47
 
    goto fail;
48
 
 
49
 
  /* Try to open a disk.  */
50
 
  disk = grub_disk_open (name);
51
 
  if (! disk)
52
 
    goto fail;
53
 
 
54
 
  dev->disk = disk;
55
 
  dev->net = 0; /* FIXME */
56
 
 
57
 
  return dev;
58
 
 
59
 
 fail:
60
 
  if (disk)
61
 
    grub_disk_close (disk);
62
 
 
63
 
  grub_free (dev);
64
 
 
65
 
  return 0;
66
 
}
67
 
 
68
 
grub_err_t
69
 
grub_device_close (grub_device_t device)
70
 
{
71
 
  if (device->disk)
72
 
    grub_disk_close (device->disk);
73
 
 
74
 
  grub_free (device);
75
 
 
76
 
  return grub_errno;
77
 
}
78
 
 
79
 
int
80
 
grub_device_iterate (int (*hook) (const char *name))
81
 
{
82
 
  auto int iterate_disk (const char *disk_name);
83
 
  auto int iterate_partition (grub_disk_t disk,
84
 
                              const grub_partition_t partition);
85
 
 
86
 
  struct part_ent
87
 
  {
88
 
    struct part_ent *next;
89
 
    char *name;
90
 
  } *ents;
91
 
 
92
 
  int iterate_disk (const char *disk_name)
93
 
    {
94
 
      grub_device_t dev;
95
 
 
96
 
      if (hook (disk_name))
97
 
        return 1;
98
 
 
99
 
      dev = grub_device_open (disk_name);
100
 
      if (! dev)
101
 
        {
102
 
          grub_errno = GRUB_ERR_NONE;
103
 
          return 0;
104
 
        }
105
 
 
106
 
      if (dev->disk && dev->disk->has_partitions)
107
 
        {
108
 
          struct part_ent *p;
109
 
          int ret = 0;
110
 
 
111
 
          ents = NULL;
112
 
          (void) grub_partition_iterate (dev->disk, iterate_partition);
113
 
          grub_device_close (dev);
114
 
 
115
 
          grub_errno = GRUB_ERR_NONE;
116
 
 
117
 
          p = ents;
118
 
          while (p != NULL)
119
 
            {
120
 
              struct part_ent *next = p->next;
121
 
 
122
 
              if (!ret)
123
 
                ret = hook (p->name);
124
 
              grub_free (p->name);
125
 
              grub_free (p);
126
 
              p = next;
127
 
            }
128
 
 
129
 
          return ret;
130
 
        }
131
 
 
132
 
      grub_device_close (dev);
133
 
      return 0;
134
 
    }
135
 
 
136
 
  int iterate_partition (grub_disk_t disk, const grub_partition_t partition)
137
 
    {
138
 
      char *partition_name;
139
 
      struct part_ent *p;
140
 
 
141
 
      partition_name = grub_partition_get_name (partition);
142
 
      if (! partition_name)
143
 
        return 1;
144
 
 
145
 
      p = grub_malloc (sizeof (*p));
146
 
      if (!p)
147
 
        {
148
 
          grub_free (partition_name);
149
 
          return 1;
150
 
        }
151
 
 
152
 
      p->name = grub_xasprintf ("%s,%s", disk->name, partition_name);
153
 
      if (!p->name)
154
 
        {
155
 
          grub_free (partition_name);
156
 
          grub_free (p);
157
 
          return 1;
158
 
        }
159
 
      grub_free (partition_name);
160
 
 
161
 
      p->next = ents;
162
 
      ents = p;
163
 
 
164
 
      return 0;
165
 
    }
166
 
 
167
 
  /* Only disk devices are supported at the moment.  */
168
 
  return grub_disk_dev_iterate (iterate_disk);
169
 
}