36 |
#include <signal.h> |
#include <signal.h> |
37 |
#include <blkid/blkid.h> |
#include <blkid/blkid.h> |
38 |
|
|
39 |
|
#ifndef DEBUG |
40 |
#define DEBUG 0 |
#define DEBUG 0 |
41 |
|
#endif |
42 |
|
|
43 |
#if DEBUG |
#if DEBUG |
44 |
#define dbgPrintf(format, args...) fprintf(stderr, format , ## args) |
#define dbgPrintf(format, args...) fprintf(stderr, format , ## args) |
58 |
}; |
}; |
59 |
|
|
60 |
enum lineType_e { |
enum lineType_e { |
61 |
LT_WHITESPACE = 1 << 0, |
LT_WHITESPACE = 1 << 0, |
62 |
LT_TITLE = 1 << 1, |
LT_TITLE = 1 << 1, |
63 |
LT_KERNEL = 1 << 2, |
LT_KERNEL = 1 << 2, |
64 |
LT_INITRD = 1 << 3, |
LT_INITRD = 1 << 3, |
65 |
LT_HYPER = 1 << 4, |
LT_HYPER = 1 << 4, |
66 |
LT_DEFAULT = 1 << 5, |
LT_DEFAULT = 1 << 5, |
67 |
LT_MBMODULE = 1 << 6, |
LT_MBMODULE = 1 << 6, |
68 |
LT_ROOT = 1 << 7, |
LT_ROOT = 1 << 7, |
69 |
LT_FALLBACK = 1 << 8, |
LT_FALLBACK = 1 << 8, |
70 |
LT_KERNELARGS = 1 << 9, |
LT_KERNELARGS = 1 << 9, |
71 |
LT_BOOT = 1 << 10, |
LT_BOOT = 1 << 10, |
72 |
LT_BOOTROOT = 1 << 11, |
LT_BOOTROOT = 1 << 11, |
73 |
LT_LBA = 1 << 12, |
LT_LBA = 1 << 12, |
74 |
LT_OTHER = 1 << 13, |
LT_OTHER = 1 << 13, |
75 |
LT_GENERIC = 1 << 14, |
LT_GENERIC = 1 << 14, |
76 |
LT_UNKNOWN = 1 << 15, |
LT_ECHO = 1 << 16, |
77 |
|
LT_MENUENTRY = 1 << 17, |
78 |
|
LT_ENTRY_END = 1 << 18, |
79 |
|
LT_SET_VARIABLE = 1 << 19, |
80 |
|
LT_UNKNOWN = 1 << 20, |
81 |
}; |
}; |
82 |
|
|
83 |
struct singleLine { |
struct singleLine { |
105 |
#define NEED_TITLE (1 << 2) |
#define NEED_TITLE (1 << 2) |
106 |
#define NEED_ARGS (1 << 3) |
#define NEED_ARGS (1 << 3) |
107 |
#define NEED_MB (1 << 4) |
#define NEED_MB (1 << 4) |
108 |
|
#define NEED_END (1 << 5) |
109 |
|
|
110 |
#define MAIN_DEFAULT (1 << 0) |
#define MAIN_DEFAULT (1 << 0) |
111 |
#define DEFAULT_SAVED -2 |
#define DEFAULT_SAVED -2 |
117 |
char separatorChar; |
char separatorChar; |
118 |
}; |
}; |
119 |
|
|
120 |
|
struct configFileInfo; |
121 |
|
|
122 |
|
typedef const char *(*findConfigFunc)(struct configFileInfo *); |
123 |
|
typedef const int (*writeLineFunc)(struct configFileInfo *, |
124 |
|
struct singleLine *line); |
125 |
|
|
126 |
struct configFileInfo { |
struct configFileInfo { |
127 |
char * defaultConfig; |
char * defaultConfig; |
128 |
|
findConfigFunc findConfig; |
129 |
|
writeLineFunc writeLine; |
130 |
struct keywordTypes * keywords; |
struct keywordTypes * keywords; |
131 |
int defaultIsIndex; |
int defaultIsIndex; |
132 |
|
int defaultIsVariable; |
133 |
int defaultSupportSaved; |
int defaultSupportSaved; |
134 |
enum lineType_e entrySeparator; |
enum lineType_e entryStart; |
135 |
|
enum lineType_e entryEnd; |
136 |
int needsBootPrefix; |
int needsBootPrefix; |
137 |
int argsInQuotes; |
int argsInQuotes; |
138 |
int maxTitleLength; |
int maxTitleLength; |
139 |
int titleBracketed; |
int titleBracketed; |
140 |
|
int titlePosition; |
141 |
int mbHyperFirst; |
int mbHyperFirst; |
142 |
int mbInitRdIsModule; |
int mbInitRdIsModule; |
143 |
int mbConcatArgs; |
int mbConcatArgs; |
156 |
{ NULL, 0, 0 }, |
{ NULL, 0, 0 }, |
157 |
}; |
}; |
158 |
|
|
159 |
|
const char *grubFindConfig(struct configFileInfo *cfi) { |
160 |
|
static const char *configFiles[] = { |
161 |
|
"/etc/grub.conf", |
162 |
|
"/boot/grub/grub.conf", |
163 |
|
"/boot/grub/menu.lst", |
164 |
|
NULL |
165 |
|
}; |
166 |
|
static int i = -1; |
167 |
|
|
168 |
|
if (i == -1) { |
169 |
|
for (i = 0; configFiles[i] != NULL; i++) { |
170 |
|
dbgPrintf("Checking \"%s\": ", configFiles[i]); |
171 |
|
if (!access(configFiles[i], R_OK)) { |
172 |
|
dbgPrintf("found\n"); |
173 |
|
return configFiles[i]; |
174 |
|
} |
175 |
|
dbgPrintf("not found\n"); |
176 |
|
} |
177 |
|
} |
178 |
|
return configFiles[i]; |
179 |
|
} |
180 |
|
|
181 |
struct configFileInfo grubConfigType = { |
struct configFileInfo grubConfigType = { |
182 |
"/boot/grub/grub.conf", /* defaultConfig */ |
.findConfig = grubFindConfig, |
183 |
grubKeywords, /* keywords */ |
.keywords = grubKeywords, |
184 |
1, /* defaultIsIndex */ |
.defaultIsIndex = 1, |
185 |
1, /* defaultSupportSaved */ |
.defaultSupportSaved = 1, |
186 |
LT_TITLE, /* entrySeparator */ |
.entryStart = LT_TITLE, |
187 |
1, /* needsBootPrefix */ |
.needsBootPrefix = 1, |
188 |
0, /* argsInQuotes */ |
.mbHyperFirst = 1, |
189 |
0, /* maxTitleLength */ |
.mbInitRdIsModule = 1, |
190 |
0, /* titleBracketed */ |
.mbAllowExtraInitRds = 1, |
191 |
1, /* mbHyperFirst */ |
}; |
192 |
1, /* mbInitRdIsModule */ |
|
193 |
0, /* mbConcatArgs */ |
struct keywordTypes grub2Keywords[] = { |
194 |
1, /* mbAllowExtraInitRds */ |
{ "menuentry", LT_MENUENTRY, ' ' }, |
195 |
|
{ "}", LT_ENTRY_END, ' ' }, |
196 |
|
{ "echo", LT_ECHO, ' ' }, |
197 |
|
{ "set", LT_SET_VARIABLE,' ', '=' }, |
198 |
|
{ "root", LT_BOOTROOT, ' ' }, |
199 |
|
{ "default", LT_DEFAULT, ' ' }, |
200 |
|
{ "fallback", LT_FALLBACK, ' ' }, |
201 |
|
{ "linux", LT_KERNEL, ' ' }, |
202 |
|
{ "initrd", LT_INITRD, ' ', ' ' }, |
203 |
|
{ "module", LT_MBMODULE, ' ' }, |
204 |
|
{ "kernel", LT_HYPER, ' ' }, |
205 |
|
{ NULL, 0, 0 }, |
206 |
|
}; |
207 |
|
|
208 |
|
const char *grub2FindConfig(struct configFileInfo *cfi) { |
209 |
|
static const char *configFiles[] = { |
210 |
|
"/boot/grub/grub-efi.cfg", |
211 |
|
"/boot/grub/grub.cfg", |
212 |
|
NULL |
213 |
|
}; |
214 |
|
static int i = -1; |
215 |
|
static const char *grub_cfg = "/boot/grub/grub.cfg"; |
216 |
|
|
217 |
|
if (i == -1) { |
218 |
|
for (i = 0; configFiles[i] != NULL; i++) { |
219 |
|
dbgPrintf("Checking \"%s\": ", configFiles[i]); |
220 |
|
if (!access(configFiles[i], R_OK)) { |
221 |
|
dbgPrintf("found\n"); |
222 |
|
return configFiles[i]; |
223 |
|
} |
224 |
|
} |
225 |
|
} |
226 |
|
|
227 |
|
/* Ubuntu renames grub2 to grub, so check for the grub.d directory |
228 |
|
* that isn't in grub1, and if it exists, return the config file path |
229 |
|
* that they use. */ |
230 |
|
if (configFiles[i] == NULL && !access("/etc/grub.d/", R_OK)) { |
231 |
|
dbgPrintf("found\n"); |
232 |
|
return grub_cfg; |
233 |
|
} |
234 |
|
|
235 |
|
dbgPrintf("not found\n"); |
236 |
|
return configFiles[i]; |
237 |
|
} |
238 |
|
|
239 |
|
struct configFileInfo grub2ConfigType = { |
240 |
|
.findConfig = grub2FindConfig, |
241 |
|
.keywords = grub2Keywords, |
242 |
|
.defaultIsIndex = 1, |
243 |
|
.defaultSupportSaved = 0, |
244 |
|
.defaultIsVariable = 1, |
245 |
|
.entryStart = LT_MENUENTRY, |
246 |
|
.entryEnd = LT_ENTRY_END, |
247 |
|
.titlePosition = 1, |
248 |
|
.needsBootPrefix = 1, |
249 |
|
.mbHyperFirst = 1, |
250 |
|
.mbInitRdIsModule = 1, |
251 |
|
.mbAllowExtraInitRds = 1, |
252 |
}; |
}; |
253 |
|
|
254 |
struct keywordTypes yabootKeywords[] = { |
struct keywordTypes yabootKeywords[] = { |
346 |
}; |
}; |
347 |
int useextlinuxmenu; |
int useextlinuxmenu; |
348 |
struct configFileInfo eliloConfigType = { |
struct configFileInfo eliloConfigType = { |
349 |
"/boot/efi/EFI/redhat/elilo.conf", /* defaultConfig */ |
.defaultConfig = "/boot/efi/EFI/redhat/elilo.conf", |
350 |
eliloKeywords, /* keywords */ |
.keywords = eliloKeywords, |
351 |
0, /* defaultIsIndex */ |
.entryStart = LT_KERNEL, |
352 |
0, /* defaultSupportSaved */ |
.needsBootPrefix = 1, |
353 |
LT_KERNEL, /* entrySeparator */ |
.argsInQuotes = 1, |
354 |
1, /* needsBootPrefix */ |
.mbConcatArgs = 1, |
|
1, /* argsInQuotes */ |
|
|
0, /* maxTitleLength */ |
|
|
0, /* titleBracketed */ |
|
|
0, /* mbHyperFirst */ |
|
|
0, /* mbInitRdIsModule */ |
|
|
1, /* mbConcatArgs */ |
|
|
0, /* mbAllowExtraInitRds */ |
|
355 |
}; |
}; |
356 |
|
|
357 |
struct configFileInfo liloConfigType = { |
struct configFileInfo liloConfigType = { |
358 |
"/etc/lilo.conf", /* defaultConfig */ |
.defaultConfig = "/etc/lilo.conf", |
359 |
liloKeywords, /* keywords */ |
.keywords = liloKeywords, |
360 |
0, /* defaultIsIndex */ |
.entryStart = LT_KERNEL, |
361 |
0, /* defaultSupportSaved */ |
.argsInQuotes = 1, |
362 |
LT_KERNEL, /* entrySeparator */ |
.maxTitleLength = 15, |
|
0, /* needsBootPrefix */ |
|
|
1, /* argsInQuotes */ |
|
|
15, /* maxTitleLength */ |
|
|
0, /* titleBracketed */ |
|
|
0, /* mbHyperFirst */ |
|
|
0, /* mbInitRdIsModule */ |
|
|
0, /* mbConcatArgs */ |
|
|
0, /* mbAllowExtraInitRds */ |
|
363 |
}; |
}; |
364 |
|
|
365 |
struct configFileInfo yabootConfigType = { |
struct configFileInfo yabootConfigType = { |
366 |
"/etc/yaboot.conf", /* defaultConfig */ |
.defaultConfig = "/etc/yaboot.conf", |
367 |
yabootKeywords, /* keywords */ |
.keywords = yabootKeywords, |
368 |
0, /* defaultIsIndex */ |
.entryStart = LT_KERNEL, |
369 |
0, /* defaultSupportSaved */ |
.needsBootPrefix = 1, |
370 |
LT_KERNEL, /* entrySeparator */ |
.argsInQuotes = 1, |
371 |
1, /* needsBootPrefix */ |
.maxTitleLength = 15, |
372 |
1, /* argsInQuotes */ |
.mbAllowExtraInitRds = 1, |
|
15, /* maxTitleLength */ |
|
|
0, /* titleBracketed */ |
|
|
0, /* mbHyperFirst */ |
|
|
0, /* mbInitRdIsModule */ |
|
|
0, /* mbConcatArgs */ |
|
|
1, /* mbAllowExtraInitRds */ |
|
373 |
}; |
}; |
374 |
|
|
375 |
struct configFileInfo siloConfigType = { |
struct configFileInfo siloConfigType = { |
376 |
"/etc/silo.conf", /* defaultConfig */ |
.defaultConfig = "/etc/silo.conf", |
377 |
siloKeywords, /* keywords */ |
.keywords = siloKeywords, |
378 |
0, /* defaultIsIndex */ |
.entryStart = LT_KERNEL, |
379 |
0, /* defaultSupportSaved */ |
.needsBootPrefix = 1, |
380 |
LT_KERNEL, /* entrySeparator */ |
.argsInQuotes = 1, |
381 |
1, /* needsBootPrefix */ |
.maxTitleLength = 15, |
|
1, /* argsInQuotes */ |
|
|
15, /* maxTitleLength */ |
|
|
0, /* titleBracketed */ |
|
|
0, /* mbHyperFirst */ |
|
|
0, /* mbInitRdIsModule */ |
|
|
0, /* mbConcatArgs */ |
|
|
0, /* mbAllowExtraInitRds */ |
|
382 |
}; |
}; |
383 |
|
|
384 |
struct configFileInfo ziplConfigType = { |
struct configFileInfo ziplConfigType = { |
385 |
"/etc/zipl.conf", /* defaultConfig */ |
.defaultConfig = "/etc/zipl.conf", |
386 |
ziplKeywords, /* keywords */ |
.keywords = ziplKeywords, |
387 |
0, /* defaultIsIndex */ |
.entryStart = LT_TITLE, |
388 |
0, /* defaultSupportSaved */ |
.argsInQuotes = 1, |
389 |
LT_TITLE, /* entrySeparator */ |
.titleBracketed = 1, |
|
0, /* needsBootPrefix */ |
|
|
1, /* argsInQuotes */ |
|
|
0, /* maxTitleLength */ |
|
|
1, /* titleBracketed */ |
|
|
0, /* mbHyperFirst */ |
|
|
0, /* mbInitRdIsModule */ |
|
|
0, /* mbConcatArgs */ |
|
|
0, /* mbAllowExtraInitRds */ |
|
390 |
}; |
}; |
391 |
|
|
392 |
struct configFileInfo extlinuxConfigType = { |
struct configFileInfo extlinuxConfigType = { |
393 |
"/boot/extlinux/extlinux.conf", /* defaultConfig */ |
.defaultConfig = "/boot/extlinux/extlinux.conf", |
394 |
extlinuxKeywords, /* keywords */ |
.keywords = extlinuxKeywords, |
395 |
0, /* defaultIsIndex */ |
.entryStart = LT_TITLE, |
396 |
0, /* defaultSupportSaved */ |
.needsBootPrefix = 1, |
397 |
LT_TITLE, /* entrySeparator */ |
.maxTitleLength = 255, |
398 |
1, /* needsBootPrefix */ |
.mbAllowExtraInitRds = 1, |
|
0, /* argsInQuotes */ |
|
|
255, /* maxTitleLength */ |
|
|
0, /* titleBracketed */ |
|
|
0, /* mbHyperFirst */ |
|
|
0, /* mbInitRdIsModule */ |
|
|
0, /* mbConcatArgs */ |
|
|
1, /* mbAllowExtraInitRds */ |
|
399 |
}; |
}; |
400 |
|
|
401 |
struct grubConfig { |
struct grubConfig { |
410 |
struct configFileInfo * cfi; |
struct configFileInfo * cfi; |
411 |
}; |
}; |
412 |
|
|
413 |
|
blkid_cache blkid; |
414 |
|
|
415 |
struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index); |
struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index); |
416 |
struct singleEntry * findEntryByPath(struct grubConfig * cfg, |
struct singleEntry * findEntryByPath(struct grubConfig * cfg, |
417 |
const char * path, const char * prefix, |
const char * path, const char * prefix, |
425 |
static int getNextLine(char ** bufPtr, struct singleLine * line, |
static int getNextLine(char ** bufPtr, struct singleLine * line, |
426 |
struct configFileInfo * cfi); |
struct configFileInfo * cfi); |
427 |
static char * getRootSpecifier(char * str); |
static char * getRootSpecifier(char * str); |
428 |
|
static void requote(struct singleLine *line, struct configFileInfo * cfi); |
429 |
static void insertElement(struct singleLine * line, |
static void insertElement(struct singleLine * line, |
430 |
const char * item, int insertHere, |
const char * item, int insertHere, |
431 |
struct configFileInfo * cfi); |
struct configFileInfo * cfi); |
490 |
return NULL; |
return NULL; |
491 |
} |
} |
492 |
|
|
493 |
static char * getpathbyspec(char *device) { |
static char *getKeyByType(enum lineType_e type, struct configFileInfo * cfi) { |
494 |
static blkid_cache blkid; |
struct keywordTypes *kt = getKeywordByType(type, cfi); |
495 |
|
if (kt) |
496 |
|
return kt->key; |
497 |
|
return "unknown"; |
498 |
|
} |
499 |
|
|
500 |
|
static char * getpathbyspec(char *device) { |
501 |
if (!blkid) |
if (!blkid) |
502 |
blkid_get_cache(&blkid, NULL); |
blkid_get_cache(&blkid, NULL); |
503 |
|
|
504 |
return blkid_get_devname(blkid, device, NULL); |
return blkid_get_devname(blkid, device, NULL); |
505 |
} |
} |
506 |
|
|
507 |
|
static char * getuuidbydev(char *device) { |
508 |
|
if (!blkid) |
509 |
|
blkid_get_cache(&blkid, NULL); |
510 |
|
|
511 |
|
return blkid_get_tag_value(blkid, "UUID", device); |
512 |
|
} |
513 |
|
|
514 |
static enum lineType_e getTypeByKeyword(char * keyword, |
static enum lineType_e getTypeByKeyword(char * keyword, |
515 |
struct configFileInfo * cfi) { |
struct configFileInfo * cfi) { |
516 |
struct keywordTypes * kw; |
struct keywordTypes * kw; |
546 |
return 0; |
return 0; |
547 |
} |
} |
548 |
|
|
549 |
static int isEntrySeparator(struct singleLine * line, |
static int isEntryStart(struct singleLine * line, |
550 |
struct configFileInfo * cfi) { |
struct configFileInfo * cfi) { |
551 |
return line->type == cfi->entrySeparator || line->type == LT_OTHER || |
return line->type == cfi->entryStart || line->type == LT_OTHER || |
552 |
(cfi->titleBracketed && isBracketedTitle(line)); |
(cfi->titleBracketed && isBracketedTitle(line)); |
553 |
} |
} |
554 |
|
|
871 |
cfg->secondaryIndent = strdup(line->indent); |
cfg->secondaryIndent = strdup(line->indent); |
872 |
} |
} |
873 |
|
|
874 |
if (isEntrySeparator(line, cfi)) { |
if (isEntryStart(line, cfi)) { |
875 |
sawEntry = 1; |
sawEntry = 1; |
876 |
if (!entry) { |
if (!entry) { |
877 |
cfg->entries = malloc(sizeof(*entry)); |
cfg->entries = malloc(sizeof(*entry)); |
887 |
entry->next = NULL; |
entry->next = NULL; |
888 |
} |
} |
889 |
|
|
890 |
if (line->type == LT_DEFAULT && line->numElements == 2) { |
if (line->type == LT_SET_VARIABLE) { |
891 |
|
int i; |
892 |
|
dbgPrintf("found 'set' command (%d elements): ", line->numElements); |
893 |
|
dbgPrintf("%s", line->indent); |
894 |
|
for (i = 0; i < line->numElements; i++) |
895 |
|
dbgPrintf("%s\"%s\"", line->elements[i].indent, line->elements[i].item); |
896 |
|
dbgPrintf("\n"); |
897 |
|
struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi); |
898 |
|
if (kwType && line->numElements == 3 && |
899 |
|
!strcmp(line->elements[1].item, kwType->key)) { |
900 |
|
dbgPrintf("Line sets default config\n"); |
901 |
|
cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT; |
902 |
|
defaultLine = line; |
903 |
|
} |
904 |
|
} else if (line->type == LT_DEFAULT && line->numElements == 2) { |
905 |
cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT; |
cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT; |
906 |
defaultLine = line; |
defaultLine = line; |
907 |
|
|
1014 |
entry->lines = line; |
entry->lines = line; |
1015 |
else |
else |
1016 |
last->next = line; |
last->next = line; |
1017 |
dbgPrintf("readConfig added %d to %p\n", line->type, entry); |
dbgPrintf("readConfig added %s to %p\n", getKeyByType(line->type, cfi), entry); |
1018 |
|
|
1019 |
|
/* we could have seen this outside of an entry... if so, we |
1020 |
|
* ignore it like any other line we don't grok */ |
1021 |
|
if (line->type == LT_ENTRY_END && sawEntry) |
1022 |
|
sawEntry = 0; |
1023 |
} else { |
} else { |
1024 |
if (!cfg->theLines) |
if (!cfg->theLines) |
1025 |
cfg->theLines = line; |
cfg->theLines = line; |
1026 |
else |
else |
1027 |
last->next = line; |
last->next = line; |
1028 |
dbgPrintf("readConfig added %d to cfg\n", line->type); |
dbgPrintf("readConfig added %s to cfg\n", getKeyByType(line->type, cfi)); |
1029 |
} |
} |
1030 |
|
|
1031 |
last = line; |
last = line; |
1033 |
|
|
1034 |
free(incoming); |
free(incoming); |
1035 |
|
|
1036 |
|
dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset"); |
1037 |
if (defaultLine) { |
if (defaultLine) { |
1038 |
if (cfi->defaultSupportSaved && |
if (cfi->defaultIsVariable) { |
1039 |
|
char *value = defaultLine->elements[2].item; |
1040 |
|
while (*value && (*value == '"' || *value == '\'' || |
1041 |
|
*value == ' ' || *value == '\t')) |
1042 |
|
value++; |
1043 |
|
cfg->defaultImage = strtol(value, &end, 10); |
1044 |
|
while (*end && (*end == '"' || *end == '\'' || |
1045 |
|
*end == ' ' || *end == '\t')) |
1046 |
|
end++; |
1047 |
|
if (*end) cfg->defaultImage = -1; |
1048 |
|
} else if (cfi->defaultSupportSaved && |
1049 |
!strncmp(defaultLine->elements[1].item, "saved", 5)) { |
!strncmp(defaultLine->elements[1].item, "saved", 5)) { |
1050 |
cfg->defaultImage = DEFAULT_SAVED; |
cfg->defaultImage = DEFAULT_SAVED; |
1051 |
} else if (cfi->defaultIsIndex) { |
} else if (cfi->defaultIsIndex) { |
1094 |
fprintf(out, "%sdefault%ssaved\n", indent, separator); |
fprintf(out, "%sdefault%ssaved\n", indent, separator); |
1095 |
else if (cfg->defaultImage > -1) { |
else if (cfg->defaultImage > -1) { |
1096 |
if (cfg->cfi->defaultIsIndex) { |
if (cfg->cfi->defaultIsIndex) { |
1097 |
fprintf(out, "%sdefault%s%d\n", indent, separator, |
if (cfg->cfi->defaultIsVariable) { |
1098 |
cfg->defaultImage); |
fprintf(out, "%sset default=\"%d\"\n", indent, |
1099 |
|
cfg->defaultImage); |
1100 |
|
} else { |
1101 |
|
fprintf(out, "%sdefault%s%d\n", indent, separator, |
1102 |
|
cfg->defaultImage); |
1103 |
|
} |
1104 |
} else { |
} else { |
1105 |
int image = cfg->defaultImage; |
int image = cfg->defaultImage; |
1106 |
|
|
1190 |
} |
} |
1191 |
|
|
1192 |
line = cfg->theLines; |
line = cfg->theLines; |
1193 |
|
struct keywordTypes *defaultKw = getKeywordByType(LT_DEFAULT, cfg->cfi); |
1194 |
while (line) { |
while (line) { |
1195 |
if (line->type == LT_DEFAULT) { |
if (line->type == LT_SET_VARIABLE && defaultKw && |
1196 |
|
line->numElements == 3 && |
1197 |
|
!strcmp(line->elements[1].item, defaultKw->key)) { |
1198 |
|
writeDefault(out, line->indent, line->elements[0].indent, cfg); |
1199 |
|
needs &= ~MAIN_DEFAULT; |
1200 |
|
} else if (line->type == LT_DEFAULT) { |
1201 |
writeDefault(out, line->indent, line->elements[0].indent, cfg); |
writeDefault(out, line->indent, line->elements[0].indent, cfg); |
1202 |
needs &= ~MAIN_DEFAULT; |
needs &= ~MAIN_DEFAULT; |
1203 |
} else if (line->type == LT_FALLBACK) { |
} else if (line->type == LT_FALLBACK) { |
1265 |
return i; |
return i; |
1266 |
} |
} |
1267 |
|
|
1268 |
|
static char *findDiskForRoot() |
1269 |
|
{ |
1270 |
|
int fd; |
1271 |
|
char buf[65536]; |
1272 |
|
char *devname; |
1273 |
|
char *chptr; |
1274 |
|
int rc; |
1275 |
|
|
1276 |
|
if ((fd = open(_PATH_MOUNTED, O_RDONLY)) < 0) { |
1277 |
|
fprintf(stderr, "grubby: failed to open %s: %s\n", |
1278 |
|
_PATH_MOUNTED, strerror(errno)); |
1279 |
|
return NULL; |
1280 |
|
} |
1281 |
|
|
1282 |
|
rc = read(fd, buf, sizeof(buf) - 1); |
1283 |
|
if (rc <= 0) { |
1284 |
|
fprintf(stderr, "grubby: failed to read %s: %s\n", |
1285 |
|
_PATH_MOUNTED, strerror(errno)); |
1286 |
|
close(fd); |
1287 |
|
return NULL; |
1288 |
|
} |
1289 |
|
close(fd); |
1290 |
|
buf[rc] = '\0'; |
1291 |
|
chptr = buf; |
1292 |
|
|
1293 |
|
while (chptr && chptr != buf+rc) { |
1294 |
|
devname = chptr; |
1295 |
|
|
1296 |
|
/* |
1297 |
|
* The first column of a mtab entry is the device, but if the entry is a |
1298 |
|
* special device it won't start with /, so move on to the next line. |
1299 |
|
*/ |
1300 |
|
if (*devname != '/') { |
1301 |
|
chptr = strchr(chptr, '\n'); |
1302 |
|
if (chptr) |
1303 |
|
chptr++; |
1304 |
|
continue; |
1305 |
|
} |
1306 |
|
|
1307 |
|
/* Seek to the next space */ |
1308 |
|
chptr = strchr(chptr, ' '); |
1309 |
|
if (!chptr) { |
1310 |
|
fprintf(stderr, "grubby: error parsing %s: %s\n", |
1311 |
|
_PATH_MOUNTED, strerror(errno)); |
1312 |
|
return NULL; |
1313 |
|
} |
1314 |
|
|
1315 |
|
/* |
1316 |
|
* The second column of a mtab entry is the mount point, we are looking |
1317 |
|
* for '/' obviously. |
1318 |
|
*/ |
1319 |
|
if (*(++chptr) == '/' && *(++chptr) == ' ') { |
1320 |
|
/* |
1321 |
|
* Move back 2, which is the first space after the device name, set |
1322 |
|
* it to \0 so strdup will just get the devicename. |
1323 |
|
*/ |
1324 |
|
chptr -= 2; |
1325 |
|
*chptr = '\0'; |
1326 |
|
return strdup(devname); |
1327 |
|
} |
1328 |
|
|
1329 |
|
/* Next line */ |
1330 |
|
chptr = strchr(chptr, '\n'); |
1331 |
|
if (chptr) |
1332 |
|
chptr++; |
1333 |
|
} |
1334 |
|
|
1335 |
|
return NULL; |
1336 |
|
} |
1337 |
|
|
1338 |
int suitableImage(struct singleEntry * entry, const char * bootPrefix, |
int suitableImage(struct singleEntry * entry, const char * bootPrefix, |
1339 |
int skipRemoved, int flags) { |
int skipRemoved, int flags) { |
1340 |
struct singleLine * line; |
struct singleLine * line; |
1341 |
char * fullName; |
char * fullName; |
1342 |
int i; |
int i; |
|
struct stat sb, sb2; |
|
1343 |
char * dev; |
char * dev; |
1344 |
char * rootspec; |
char * rootspec; |
1345 |
|
char * rootdev; |
1346 |
|
|
1347 |
if (skipRemoved && entry->skip) return 0; |
if (skipRemoved && entry->skip) return 0; |
1348 |
|
|
1392 |
if (!dev) |
if (!dev) |
1393 |
return 0; |
return 0; |
1394 |
|
|
1395 |
i = stat(dev, &sb); |
rootdev = findDiskForRoot(); |
1396 |
if (i) |
if (!rootdev) |
1397 |
return 0; |
return 0; |
1398 |
|
|
1399 |
stat("/", &sb2); |
if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) { |
1400 |
|
free(rootdev); |
1401 |
|
return 0; |
1402 |
|
} |
1403 |
|
|
1404 |
if (sb.st_rdev != sb2.st_dev) |
if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) { |
1405 |
|
free(rootdev); |
1406 |
return 0; |
return 0; |
1407 |
|
} |
1408 |
|
|
1409 |
|
free(rootdev); |
1410 |
|
|
1411 |
return 1; |
return 1; |
1412 |
} |
} |
1920 |
sprintf(tmpl.elements[0].item, "[%s]", val); |
sprintf(tmpl.elements[0].item, "[%s]", val); |
1921 |
tmpl.elements[0].indent = ""; |
tmpl.elements[0].indent = ""; |
1922 |
val = NULL; |
val = NULL; |
1923 |
|
} else if (type == LT_MENUENTRY) { |
1924 |
|
char *lineend = "--class gnu-linux --class gnu --class os {"; |
1925 |
|
if (!val) { |
1926 |
|
fprintf(stderr, "Line type LT_MENUENTRY requires a value\n"); |
1927 |
|
abort(); |
1928 |
|
} |
1929 |
|
kw = getKeywordByType(type, cfi); |
1930 |
|
if (!kw) { |
1931 |
|
fprintf(stderr, "Looking up keyword for unknown type %d\n", type); |
1932 |
|
abort(); |
1933 |
|
} |
1934 |
|
tmpl.indent = ""; |
1935 |
|
tmpl.type = type; |
1936 |
|
tmpl.numElements = 3; |
1937 |
|
tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements); |
1938 |
|
tmpl.elements[0].item = kw->key; |
1939 |
|
tmpl.elements[0].indent = alloca(2); |
1940 |
|
sprintf(tmpl.elements[0].indent, "%c", kw->nextChar); |
1941 |
|
tmpl.elements[1].item = (char *)val; |
1942 |
|
tmpl.elements[1].indent = alloca(2); |
1943 |
|
sprintf(tmpl.elements[1].indent, "%c", kw->nextChar); |
1944 |
|
tmpl.elements[2].item = alloca(strlen(lineend)+1); |
1945 |
|
strcpy(tmpl.elements[2].item, lineend); |
1946 |
|
tmpl.elements[2].indent = ""; |
1947 |
} else { |
} else { |
1948 |
kw = getKeywordByType(type, cfi); |
kw = getKeywordByType(type, cfi); |
1949 |
if (!kw) abort(); |
if (!kw) { |
1950 |
|
fprintf(stderr, "Looking up keyword for unknown type %d\n", type); |
1951 |
|
abort(); |
1952 |
|
} |
1953 |
tmpl.type = type; |
tmpl.type = type; |
1954 |
tmpl.numElements = val ? 2 : 1; |
tmpl.numElements = val ? 2 : 1; |
1955 |
tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements); |
tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements); |
1973 |
if (!line->next && !prev) prev = line; |
if (!line->next && !prev) prev = line; |
1974 |
} |
} |
1975 |
|
|
1976 |
if (prev == entry->lines) |
struct singleLine *menuEntry; |
1977 |
tmpl.indent = defaultIndent ?: ""; |
menuEntry = getLineByType(LT_MENUENTRY, entry->lines); |
1978 |
else |
if (tmpl.type == LT_ENTRY_END) { |
1979 |
tmpl.indent = prev->indent; |
if (menuEntry) |
1980 |
|
tmpl.indent = menuEntry->indent; |
1981 |
|
else |
1982 |
|
tmpl.indent = defaultIndent ?: ""; |
1983 |
|
} else if (tmpl.type != LT_MENUENTRY) { |
1984 |
|
if (menuEntry) |
1985 |
|
tmpl.indent = "\t"; |
1986 |
|
else if (prev == entry->lines) |
1987 |
|
tmpl.indent = defaultIndent ?: ""; |
1988 |
|
else |
1989 |
|
tmpl.indent = prev->indent; |
1990 |
|
} |
1991 |
|
|
1992 |
return addLineTmpl(entry, &tmpl, prev, val, cfi); |
return addLineTmpl(entry, &tmpl, prev, val, cfi); |
1993 |
} |
} |
2014 |
free(line); |
free(line); |
2015 |
} |
} |
2016 |
|
|
2017 |
|
static int isquote(char q) |
2018 |
|
{ |
2019 |
|
if (q == '\'' || q == '\"') |
2020 |
|
return 1; |
2021 |
|
return 0; |
2022 |
|
} |
2023 |
|
|
2024 |
|
static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi) |
2025 |
|
{ |
2026 |
|
struct singleLine newLine = { |
2027 |
|
.indent = tmplLine->indent, |
2028 |
|
.type = tmplLine->type, |
2029 |
|
.next = tmplLine->next, |
2030 |
|
}; |
2031 |
|
int firstQuotedItem = -1; |
2032 |
|
int quoteLen = 0; |
2033 |
|
int j; |
2034 |
|
int element = 0; |
2035 |
|
char *c; |
2036 |
|
|
2037 |
|
c = malloc(strlen(tmplLine->elements[0].item) + 1); |
2038 |
|
strcpy(c, tmplLine->elements[0].item); |
2039 |
|
insertElement(&newLine, c, element++, cfi); |
2040 |
|
free(c); |
2041 |
|
c = NULL; |
2042 |
|
|
2043 |
|
for (j = 1; j < tmplLine->numElements; j++) { |
2044 |
|
if (firstQuotedItem == -1) { |
2045 |
|
quoteLen += strlen(tmplLine->elements[j].item); |
2046 |
|
|
2047 |
|
if (isquote(tmplLine->elements[j].item[0])) { |
2048 |
|
firstQuotedItem = j; |
2049 |
|
quoteLen += strlen(tmplLine->elements[j].indent); |
2050 |
|
} else { |
2051 |
|
c = malloc(quoteLen + 1); |
2052 |
|
strcpy(c, tmplLine->elements[j].item); |
2053 |
|
insertElement(&newLine, c, element++, cfi); |
2054 |
|
free(c); |
2055 |
|
quoteLen = 0; |
2056 |
|
} |
2057 |
|
} else { |
2058 |
|
int itemlen = strlen(tmplLine->elements[j].item); |
2059 |
|
quoteLen += itemlen; |
2060 |
|
quoteLen += strlen(tmplLine->elements[j].indent); |
2061 |
|
|
2062 |
|
if (isquote(tmplLine->elements[j].item[itemlen - 1])) { |
2063 |
|
c = malloc(quoteLen + 1); |
2064 |
|
c[0] = '\0'; |
2065 |
|
for (int i = firstQuotedItem; i < j+1; i++) { |
2066 |
|
strcat(c, tmplLine->elements[i].item); |
2067 |
|
strcat(c, tmplLine->elements[i].indent); |
2068 |
|
} |
2069 |
|
insertElement(&newLine, c, element++, cfi); |
2070 |
|
free(c); |
2071 |
|
|
2072 |
|
firstQuotedItem = -1; |
2073 |
|
quoteLen = 0; |
2074 |
|
} |
2075 |
|
} |
2076 |
|
} |
2077 |
|
while (tmplLine->numElements) |
2078 |
|
removeElement(tmplLine, 0); |
2079 |
|
if (tmplLine->elements) |
2080 |
|
free(tmplLine->elements); |
2081 |
|
|
2082 |
|
tmplLine->numElements = newLine.numElements; |
2083 |
|
tmplLine->elements = newLine.elements; |
2084 |
|
} |
2085 |
|
|
2086 |
static void insertElement(struct singleLine * line, |
static void insertElement(struct singleLine * line, |
2087 |
const char * item, int insertHere, |
const char * item, int insertHere, |
2088 |
struct configFileInfo * cfi) |
struct configFileInfo * cfi) |
2189 |
const char ** arg; |
const char ** arg; |
2190 |
int useKernelArgs, useRoot; |
int useKernelArgs, useRoot; |
2191 |
int firstElement; |
int firstElement; |
2192 |
int *usedElements, *usedArgs; |
int *usedElements; |
2193 |
int doreplace; |
int doreplace; |
2194 |
|
|
2195 |
if (!image) return 0; |
if (!image) return 0; |
2224 |
useRoot = (getKeywordByType(LT_ROOT, cfg->cfi) |
useRoot = (getKeywordByType(LT_ROOT, cfg->cfi) |
2225 |
&& !multibootArgs); |
&& !multibootArgs); |
2226 |
|
|
|
for (k = 0, arg = newArgs; *arg; arg++, k++) ; |
|
|
usedArgs = calloc(k, sizeof(*usedArgs)); |
|
|
|
|
2227 |
for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) { |
for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) { |
2228 |
|
|
2229 |
if (multibootArgs && !entry->multiboot) |
if (multibootArgs && !entry->multiboot) |
2299 |
usedElements = calloc(line->numElements, sizeof(*usedElements)); |
usedElements = calloc(line->numElements, sizeof(*usedElements)); |
2300 |
|
|
2301 |
for (k = 0, arg = newArgs; *arg; arg++, k++) { |
for (k = 0, arg = newArgs; *arg; arg++, k++) { |
|
if (usedArgs[k]) continue; |
|
2302 |
|
|
2303 |
doreplace = 1; |
doreplace = 1; |
2304 |
for (i = firstElement; i < line->numElements; i++) { |
for (i = firstElement; i < line->numElements; i++) { |
2313 |
continue; |
continue; |
2314 |
if (!argMatch(line->elements[i].item, *arg)) { |
if (!argMatch(line->elements[i].item, *arg)) { |
2315 |
usedElements[i]=1; |
usedElements[i]=1; |
|
usedArgs[k]=1; |
|
2316 |
break; |
break; |
2317 |
} |
} |
2318 |
} |
} |
2382 |
} |
} |
2383 |
} |
} |
2384 |
|
|
|
free(usedArgs); |
|
2385 |
free(newArgs); |
free(newArgs); |
2386 |
free(oldArgs); |
free(oldArgs); |
2387 |
|
|
2407 |
return rc; |
return rc; |
2408 |
} |
} |
2409 |
|
|
2410 |
|
int updateInitrd(struct grubConfig * cfg, const char * image, |
2411 |
|
const char * prefix, const char * initrd) { |
2412 |
|
struct singleEntry * entry; |
2413 |
|
struct singleLine * line, * kernelLine, *endLine = NULL; |
2414 |
|
int index = 0; |
2415 |
|
|
2416 |
|
if (!image) return 0; |
2417 |
|
|
2418 |
|
for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) { |
2419 |
|
kernelLine = getLineByType(LT_KERNEL, entry->lines); |
2420 |
|
if (!kernelLine) continue; |
2421 |
|
|
2422 |
|
line = getLineByType(LT_INITRD, entry->lines); |
2423 |
|
if (line) |
2424 |
|
removeLine(entry, line); |
2425 |
|
if (prefix) { |
2426 |
|
int prefixLen = strlen(prefix); |
2427 |
|
if (!strncmp(initrd, prefix, prefixLen)) |
2428 |
|
initrd += prefixLen; |
2429 |
|
} |
2430 |
|
endLine = getLineByType(LT_ENTRY_END, entry->lines); |
2431 |
|
if (endLine) |
2432 |
|
removeLine(entry, endLine); |
2433 |
|
line = addLine(entry, cfg->cfi, LT_INITRD, kernelLine->indent, initrd); |
2434 |
|
if (!line) |
2435 |
|
return 1; |
2436 |
|
if (endLine) { |
2437 |
|
line = addLine(entry, cfg->cfi, LT_ENTRY_END, "", NULL); |
2438 |
|
if (!line) |
2439 |
|
return 1; |
2440 |
|
} |
2441 |
|
|
2442 |
|
break; |
2443 |
|
} |
2444 |
|
|
2445 |
|
return 0; |
2446 |
|
} |
2447 |
|
|
2448 |
int checkDeviceBootloader(const char * device, const unsigned char * boot) { |
int checkDeviceBootloader(const char * device, const unsigned char * boot) { |
2449 |
int fd; |
int fd; |
2450 |
unsigned char bootSect[512]; |
unsigned char bootSect[512]; |
2615 |
return checkDeviceBootloader(line->elements[1].item, boot); |
return checkDeviceBootloader(line->elements[1].item, boot); |
2616 |
} |
} |
2617 |
|
|
2618 |
|
int checkForGrub2(struct grubConfig * config) { |
2619 |
|
if (!access("/etc/grub.d/", R_OK)) |
2620 |
|
return 2; |
2621 |
|
|
2622 |
|
return 1; |
2623 |
|
} |
2624 |
|
|
2625 |
int checkForGrub(struct grubConfig * config) { |
int checkForGrub(struct grubConfig * config) { |
2626 |
int fd; |
int fd; |
2627 |
unsigned char bootSect[512]; |
unsigned char bootSect[512]; |
2797 |
if (*chptr == '#') continue; |
if (*chptr == '#') continue; |
2798 |
|
|
2799 |
if (tmplLine->type == LT_KERNEL && |
if (tmplLine->type == LT_KERNEL && |
2800 |
tmplLine->numElements >= 2) { |
tmplLine->numElements >= 2) { |
2801 |
if (!template->multiboot && (needs & NEED_MB)) { |
if (!template->multiboot && (needs & NEED_MB)) { |
2802 |
/* it's not a multiboot template and this is the kernel |
/* it's not a multiboot template and this is the kernel |
2803 |
* line. Try to be intelligent about inserting the |
* line. Try to be intelligent about inserting the |
2923 |
needs &= ~NEED_INITRD; |
needs &= ~NEED_INITRD; |
2924 |
} |
} |
2925 |
|
|
2926 |
|
} else if (tmplLine->type == LT_MENUENTRY && |
2927 |
|
(needs & NEED_TITLE)) { |
2928 |
|
requote(tmplLine, config->cfi); |
2929 |
|
char *nkt = malloc(strlen(newKernelTitle)+3); |
2930 |
|
strcpy(nkt, "'"); |
2931 |
|
strcat(nkt, newKernelTitle); |
2932 |
|
strcat(nkt, "'"); |
2933 |
|
newLine = addLineTmpl(new, tmplLine, newLine, nkt, config->cfi); |
2934 |
|
free(nkt); |
2935 |
|
needs &= ~NEED_TITLE; |
2936 |
} else if (tmplLine->type == LT_TITLE && |
} else if (tmplLine->type == LT_TITLE && |
2937 |
(needs & NEED_TITLE)) { |
(needs & NEED_TITLE)) { |
2938 |
if (tmplLine->numElements >= 2) { |
if (tmplLine->numElements >= 2) { |
2946 |
tmplLine->indent, newKernelTitle); |
tmplLine->indent, newKernelTitle); |
2947 |
needs &= ~NEED_TITLE; |
needs &= ~NEED_TITLE; |
2948 |
} |
} |
2949 |
|
} else if (tmplLine->type == LT_ECHO) { |
2950 |
|
requote(tmplLine, config->cfi); |
2951 |
|
if (tmplLine->numElements > 1 && |
2952 |
|
strstr(tmplLine->elements[1].item, "'Loading Linux ")) { |
2953 |
|
char *prefix = "'Loading "; |
2954 |
|
char *newTitle = malloc(strlen(prefix) + |
2955 |
|
strlen(newKernelTitle) + 2); |
2956 |
|
|
2957 |
|
strcpy(newTitle, prefix); |
2958 |
|
strcat(newTitle, newKernelTitle); |
2959 |
|
strcat(newTitle, "'"); |
2960 |
|
newLine = addLine(new, config->cfi, LT_ECHO, |
2961 |
|
tmplLine->indent, newTitle); |
2962 |
|
free(newTitle); |
2963 |
|
} else { |
2964 |
|
/* pass through other lines from the template */ |
2965 |
|
newLine = addLineTmpl(new, tmplLine, newLine, NULL, |
2966 |
|
config->cfi); |
2967 |
|
} |
2968 |
} else { |
} else { |
2969 |
/* pass through other lines from the template */ |
/* pass through other lines from the template */ |
2970 |
newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi); |
newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi); |
2975 |
/* don't have a template, so start the entry with the |
/* don't have a template, so start the entry with the |
2976 |
* appropriate starting line |
* appropriate starting line |
2977 |
*/ |
*/ |
2978 |
switch (config->cfi->entrySeparator) { |
switch (config->cfi->entryStart) { |
2979 |
case LT_KERNEL: |
case LT_KERNEL: |
2980 |
if (new->multiboot && config->cfi->mbHyperFirst) { |
if (new->multiboot && config->cfi->mbHyperFirst) { |
2981 |
/* fall through to LT_HYPER */ |
/* fall through to LT_HYPER */ |
2994 |
needs &= ~NEED_MB; |
needs &= ~NEED_MB; |
2995 |
break; |
break; |
2996 |
|
|
2997 |
|
case LT_MENUENTRY: { |
2998 |
|
char *nkt = malloc(strlen(newKernelTitle)+3); |
2999 |
|
strcpy(nkt, "'"); |
3000 |
|
strcat(nkt, newKernelTitle); |
3001 |
|
strcat(nkt, "'"); |
3002 |
|
newLine = addLine(new, config->cfi, LT_MENUENTRY, |
3003 |
|
config->primaryIndent, nkt); |
3004 |
|
free(nkt); |
3005 |
|
needs &= ~NEED_TITLE; |
3006 |
|
needs |= NEED_END; |
3007 |
|
break; |
3008 |
|
} |
3009 |
case LT_TITLE: |
case LT_TITLE: |
3010 |
if( useextlinuxmenu != 0 ){ // We just need useextlinuxmenu to not be zero (set above) |
if( useextlinuxmenu != 0 ){ // We just need useextlinuxmenu to not be zero (set above) |
3011 |
char * templabel; |
char * templabel; |
3039 |
|
|
3040 |
/* add the remainder of the lines, i.e. those that either |
/* add the remainder of the lines, i.e. those that either |
3041 |
* weren't present in the template, or in the case of no template, |
* weren't present in the template, or in the case of no template, |
3042 |
* all the lines following the entrySeparator. |
* all the lines following the entryStart. |
3043 |
*/ |
*/ |
3044 |
if (needs & NEED_TITLE) { |
if (needs & NEED_TITLE) { |
3045 |
newLine = addLine(new, config->cfi, LT_TITLE, |
newLine = addLine(new, config->cfi, LT_TITLE, |
3080 |
free(initrdVal); |
free(initrdVal); |
3081 |
needs &= ~NEED_INITRD; |
needs &= ~NEED_INITRD; |
3082 |
} |
} |
3083 |
|
if (needs & NEED_END) { |
3084 |
|
newLine = addLine(new, config->cfi, LT_ENTRY_END, |
3085 |
|
config->secondaryIndent, NULL); |
3086 |
|
needs &= ~NEED_END; |
3087 |
|
} |
3088 |
|
|
3089 |
if (needs) { |
if (needs) { |
3090 |
printf(_("grubby: needs=%d, aborting\n"), needs); |
printf(_("grubby: needs=%d, aborting\n"), needs); |
3114 |
|
|
3115 |
int main(int argc, const char ** argv) { |
int main(int argc, const char ** argv) { |
3116 |
poptContext optCon; |
poptContext optCon; |
3117 |
char * grubConfig = NULL; |
const char * grubConfig = NULL; |
3118 |
char * outputFile = NULL; |
char * outputFile = NULL; |
3119 |
int arg = 0; |
int arg = 0; |
3120 |
int flags = 0; |
int flags = 0; |
3121 |
int badImageOkay = 0; |
int badImageOkay = 0; |
3122 |
|
int configureGrub2 = 0; |
3123 |
int configureLilo = 0, configureELilo = 0, configureGrub = 0; |
int configureLilo = 0, configureELilo = 0, configureGrub = 0; |
3124 |
int configureYaboot = 0, configureSilo = 0, configureZipl = 0; |
int configureYaboot = 0, configureSilo = 0, configureZipl = 0; |
3125 |
int configureExtLinux = 0; |
int configureExtLinux = 0; |
3185 |
_("configure extlinux bootloader (from syslinux)") }, |
_("configure extlinux bootloader (from syslinux)") }, |
3186 |
{ "grub", 0, POPT_ARG_NONE, &configureGrub, 0, |
{ "grub", 0, POPT_ARG_NONE, &configureGrub, 0, |
3187 |
_("configure grub bootloader") }, |
_("configure grub bootloader") }, |
3188 |
|
{ "grub2", 0, POPT_ARG_NONE, &configureGrub2, 0, |
3189 |
|
_("configure grub2 bootloader") }, |
3190 |
{ "info", 0, POPT_ARG_STRING, &kernelInfo, 0, |
{ "info", 0, POPT_ARG_STRING, &kernelInfo, 0, |
3191 |
_("display boot information for specified kernel"), |
_("display boot information for specified kernel"), |
3192 |
_("kernel-path") }, |
_("kernel-path") }, |
3266 |
return 1; |
return 1; |
3267 |
} |
} |
3268 |
|
|
3269 |
if ((configureLilo + configureGrub + configureELilo + |
if ((configureLilo + configureGrub2 + configureGrub + configureELilo + |
3270 |
configureYaboot + configureSilo + configureZipl + |
configureYaboot + configureSilo + configureZipl + |
3271 |
configureExtLinux ) > 1) { |
configureExtLinux ) > 1) { |
3272 |
fprintf(stderr, _("grubby: cannot specify multiple bootloaders\n")); |
fprintf(stderr, _("grubby: cannot specify multiple bootloaders\n")); |
3275 |
fprintf(stderr, |
fprintf(stderr, |
3276 |
_("grubby: cannot specify config file with --bootloader-probe\n")); |
_("grubby: cannot specify config file with --bootloader-probe\n")); |
3277 |
return 1; |
return 1; |
3278 |
|
} else if (configureGrub2) { |
3279 |
|
cfi = &grub2ConfigType; |
3280 |
} else if (configureLilo) { |
} else if (configureLilo) { |
3281 |
cfi = &liloConfigType; |
cfi = &liloConfigType; |
3282 |
} else if (configureGrub) { |
} else if (configureGrub) { |
3306 |
#elif __s390x__ |
#elif __s390x__ |
3307 |
cfi = &ziplConfigtype; |
cfi = &ziplConfigtype; |
3308 |
#else |
#else |
3309 |
cfi = &grubConfigType; |
if (grub2FindConfig(&grub2ConfigType)) |
3310 |
|
cfi = &grub2ConfigType; |
3311 |
|
else |
3312 |
|
cfi = &grubConfigType; |
3313 |
#endif |
#endif |
3314 |
} |
} |
3315 |
|
|
3316 |
if (!grubConfig) |
if (!grubConfig) { |
3317 |
grubConfig = cfi->defaultConfig; |
if (cfi->findConfig) |
3318 |
|
grubConfig = cfi->findConfig(cfi); |
3319 |
|
if (!grubConfig) |
3320 |
|
grubConfig = cfi->defaultConfig; |
3321 |
|
} |
3322 |
|
|
3323 |
if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion || |
if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion || |
3324 |
newKernelPath || removeKernelPath || makeDefault || |
newKernelPath || removeKernelPath || makeDefault || |
3338 |
if (newKernelPath && !newKernelTitle) { |
if (newKernelPath && !newKernelTitle) { |
3339 |
fprintf(stderr, _("grubby: kernel title must be specified\n")); |
fprintf(stderr, _("grubby: kernel title must be specified\n")); |
3340 |
return 1; |
return 1; |
3341 |
} else if (!newKernelPath && (newKernelTitle || newKernelInitrd || |
} else if (!newKernelPath && (newKernelTitle || copyDefault || |
3342 |
newKernelInitrd || copyDefault || |
(newKernelInitrd && !updateKernelPath)|| |
3343 |
makeDefault || extraInitrdCount > 0)) { |
makeDefault || extraInitrdCount > 0)) { |
3344 |
fprintf(stderr, _("grubby: kernel path expected\n")); |
fprintf(stderr, _("grubby: kernel path expected\n")); |
3345 |
return 1; |
return 1; |
3400 |
} |
} |
3401 |
|
|
3402 |
if (bootloaderProbe) { |
if (bootloaderProbe) { |
3403 |
int lrc = 0, grc = 0, erc = 0; |
int lrc = 0, grc = 0, gr2c = 0, erc = 0; |
3404 |
struct grubConfig * lconfig, * gconfig; |
struct grubConfig * lconfig, * gconfig; |
3405 |
|
|
3406 |
if (!access(grubConfigType.defaultConfig, F_OK)) { |
const char *grub2config = grub2FindConfig(&grub2ConfigType); |
3407 |
gconfig = readConfig(grubConfigType.defaultConfig, &grubConfigType); |
if (grub2config) { |
3408 |
|
gconfig = readConfig(grub2config, &grub2ConfigType); |
3409 |
|
if (!gconfig) |
3410 |
|
gr2c = 1; |
3411 |
|
else |
3412 |
|
gr2c = checkForGrub2(gconfig); |
3413 |
|
} |
3414 |
|
|
3415 |
|
const char *grubconfig = grubFindConfig(&grubConfigType); |
3416 |
|
if (!access(grubconfig, F_OK)) { |
3417 |
|
gconfig = readConfig(grubconfig, &grubConfigType); |
3418 |
if (!gconfig) |
if (!gconfig) |
3419 |
grc = 1; |
grc = 1; |
3420 |
else |
else |
3437 |
erc = checkForExtLinux(lconfig); |
erc = checkForExtLinux(lconfig); |
3438 |
} |
} |
3439 |
|
|
3440 |
if (lrc == 1 || grc == 1) return 1; |
if (lrc == 1 || grc == 1 || gr2c == 1) return 1; |
3441 |
|
|
3442 |
if (lrc == 2) printf("lilo\n"); |
if (lrc == 2) printf("lilo\n"); |
3443 |
|
if (gr2c == 2) printf("grub2\n"); |
3444 |
if (grc == 2) printf("grub\n"); |
if (grc == 2) printf("grub\n"); |
3445 |
if (erc == 2) printf("extlinux\n"); |
if (erc == 2) printf("extlinux\n"); |
3446 |
|
|
3483 |
setFallbackImage(config, newKernelPath != NULL); |
setFallbackImage(config, newKernelPath != NULL); |
3484 |
if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs, |
if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs, |
3485 |
removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1; |
removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1; |
3486 |
|
if (updateKernelPath && newKernelInitrd) { |
3487 |
|
if (updateInitrd(config, updateKernelPath, bootPrefix, |
3488 |
|
newKernelInitrd)) return 1; |
3489 |
|
} |
3490 |
if (addNewKernel(config, template, bootPrefix, newKernelPath, |
if (addNewKernel(config, template, bootPrefix, newKernelPath, |
3491 |
newKernelTitle, newKernelArgs, newKernelInitrd, |
newKernelTitle, newKernelArgs, newKernelInitrd, |
3492 |
extraInitrds, extraInitrdCount, |
extraInitrds, extraInitrdCount, |
3500 |
} |
} |
3501 |
|
|
3502 |
if (!outputFile) |
if (!outputFile) |
3503 |
outputFile = grubConfig; |
outputFile = (char *)grubConfig; |
3504 |
|
|
3505 |
return writeConfig(config, outputFile, bootPrefix); |
return writeConfig(config, outputFile, bootPrefix); |
3506 |
} |
} |