]> git.proxmox.com Git - grub2.git/blob - commands/search.c
2008-06-19 Robert Millan <rmh@aybabtu.com>
[grub2.git] / commands / search.c
1 /* search.c - search devices based on a file or a filesystem label */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2007,2008 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/types.h>
21 #include <grub/misc.h>
22 #include <grub/mm.h>
23 #include <grub/err.h>
24 #include <grub/dl.h>
25 #include <grub/normal.h>
26 #include <grub/arg.h>
27 #include <grub/device.h>
28 #include <grub/file.h>
29 #include <grub/env.h>
30
31 static const struct grub_arg_option options[] =
32 {
33 {"file", 'f', 0, "search devices by a file (default)", 0, 0},
34 {"label", 'l', 0, "search devices by a filesystem label", 0, 0},
35 {"fs-uuid", 'u', 0, "search devices by a filesystem UUID", 0, 0},
36 {"set", 's', GRUB_ARG_OPTION_OPTIONAL, "set a variable to the first device found", "VAR", ARG_TYPE_STRING},
37 {0, 0, 0, 0, 0, 0}
38 };
39
40 static void
41 search_label (const char *key, const char *var)
42 {
43 int count = 0;
44 auto int iterate_device (const char *name);
45
46 int iterate_device (const char *name)
47 {
48 grub_device_t dev;
49 int abort = 0;
50
51 dev = grub_device_open (name);
52 if (dev)
53 {
54 grub_fs_t fs;
55
56 fs = grub_fs_probe (dev);
57 if (fs && fs->label)
58 {
59 char *label;
60
61 (fs->label) (dev, &label);
62 if (grub_errno == GRUB_ERR_NONE && label)
63 {
64 if (grub_strcmp (label, key) == 0)
65 {
66 /* Found! */
67 count++;
68 if (var)
69 {
70 grub_env_set (var, name);
71 abort = 1;
72 }
73 else
74 grub_printf (" %s", name);
75 }
76
77 grub_free (label);
78 }
79 }
80
81 grub_device_close (dev);
82 }
83
84 grub_errno = GRUB_ERR_NONE;
85 return abort;
86 }
87
88 grub_device_iterate (iterate_device);
89
90 if (count == 0)
91 grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device: %s", key);
92 }
93
94 static void
95 search_fs_uuid (const char *key, const char *var)
96 {
97 int count = 0;
98 auto int iterate_device (const char *name);
99
100 int iterate_device (const char *name)
101 {
102 grub_device_t dev;
103 int abort = 0;
104
105 dev = grub_device_open (name);
106 if (dev)
107 {
108 grub_fs_t fs;
109
110 fs = grub_fs_probe (dev);
111 if (fs && fs->uuid)
112 {
113 char *uuid;
114
115 (fs->uuid) (dev, &uuid);
116 if (grub_errno == GRUB_ERR_NONE && uuid)
117 {
118 if (grub_strcmp (uuid, key) == 0)
119 {
120 /* Found! */
121 count++;
122 if (var)
123 {
124 grub_env_set (var, name);
125 abort = 1;
126 }
127 else
128 grub_printf (" %s", name);
129 }
130
131 grub_free (uuid);
132 }
133 }
134
135 grub_device_close (dev);
136 }
137
138 grub_errno = GRUB_ERR_NONE;
139 return abort;
140 }
141
142 grub_device_iterate (iterate_device);
143
144 if (count == 0)
145 grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device: %s", key);
146 }
147
148 static void
149 search_file (const char *key, const char *var)
150 {
151 int count = 0;
152 char *buf = 0;
153 auto int iterate_device (const char *name);
154
155 int iterate_device (const char *name)
156 {
157 grub_size_t len;
158 char *p;
159 grub_file_t file;
160 int abort = 0;
161
162 len = grub_strlen (name) + 2 + grub_strlen (key) + 1;
163 p = grub_realloc (buf, len);
164 if (! p)
165 return 1;
166
167 buf = p;
168 grub_sprintf (buf, "(%s)%s", name, key);
169
170 file = grub_file_open (buf);
171 if (file)
172 {
173 /* Found! */
174 count++;
175 if (var)
176 {
177 grub_env_set (var, name);
178 abort = 1;
179 }
180 else
181 grub_printf (" %s", name);
182
183 grub_file_close (file);
184 }
185
186 grub_errno = GRUB_ERR_NONE;
187 return abort;
188 }
189
190 grub_device_iterate (iterate_device);
191
192 grub_free (buf);
193
194 if (grub_errno == GRUB_ERR_NONE && count == 0)
195 grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device");
196 }
197
198 static grub_err_t
199 grub_cmd_search (struct grub_arg_list *state, int argc, char **args)
200 {
201 const char *var = 0;
202
203 if (argc == 0)
204 return grub_error (GRUB_ERR_INVALID_COMMAND, "no argument specified");
205
206 if (state[3].set)
207 var = state[3].arg ? state[3].arg : "root";
208
209 if (state[1].set)
210 search_label (args[0], var);
211 else if (state[2].set)
212 search_fs_uuid (args[0], var);
213 else
214 search_file (args[0], var);
215
216 return grub_errno;
217 }
218
219 GRUB_MOD_INIT(search)
220 {
221 (void) mod; /* To stop warning. */
222 grub_register_command ("search", grub_cmd_search, GRUB_COMMAND_FLAG_BOTH,
223 "search [-f|-l|-u|-s] NAME",
224 "Search devices by file, filesystem label or filesystem UUID."
225 " If --set is specified, the first device found is"
226 " set to a variable. If no variable name is"
227 " specified, \"root\" is used.",
228 options);
229 }
230
231 GRUB_MOD_FINI(search)
232 {
233 grub_unregister_command ("search");
234 }