Magellan Linux

Diff of /tags/grubby-8_32/grubby.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/mkinitrd-magellan/grubby/grubby.c revision 926 by niro, Wed Oct 28 13:29:38 2009 UTC trunk/grubby/grubby.c revision 1714 by niro, Sat Feb 18 00:32:14 2012 UTC
# Line 36  Line 36 
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)
# Line 56  struct lineElement { Line 58  struct lineElement {
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 {
# Line 99  struct singleEntry { Line 105  struct singleEntry {
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
# Line 110  struct keywordTypes { Line 117  struct keywordTypes {
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;
# Line 139  struct keywordTypes grubKeywords[] = { Line 157  struct keywordTypes grubKeywords[] = {
157  };  };
158    
159  struct configFileInfo grubConfigType = {  struct configFileInfo grubConfigType = {
160      "/boot/grub/grub.conf",    /* defaultConfig */      .defaultConfig = "/boot/grub/grub.conf",
161      grubKeywords,    /* keywords */      .keywords = grubKeywords,
162      1,    /* defaultIsIndex */      .defaultIsIndex = 1,
163      1,    /* defaultSupportSaved */      .defaultSupportSaved = 1,
164      LT_TITLE,    /* entrySeparator */      .entryStart = LT_TITLE,
165      1,    /* needsBootPrefix */      .needsBootPrefix = 1,
166      0,    /* argsInQuotes */      .mbHyperFirst = 1,
167      0,    /* maxTitleLength */      .mbInitRdIsModule = 1,
168      0,                                      /* titleBracketed */      .mbAllowExtraInitRds = 1,
169      1,                                      /* mbHyperFirst */  };
170      1,                                      /* mbInitRdIsModule */  
171      0,                                      /* mbConcatArgs */  struct keywordTypes grub2Keywords[] = {
172      1,                                      /* mbAllowExtraInitRds */      { "menuentry",  LT_MENUENTRY,   ' ' },
173        { "}",          LT_ENTRY_END,   ' ' },
174        { "echo",       LT_ECHO,        ' ' },
175        { "set",        LT_SET_VARIABLE,' ', '=' },
176        { "root",       LT_BOOTROOT,    ' ' },
177        { "default",    LT_DEFAULT,     ' ' },
178        { "fallback",   LT_FALLBACK,    ' ' },
179        { "linux",      LT_KERNEL,      ' ' },
180        { "initrd",     LT_INITRD,      ' ', ' ' },
181        { "module",     LT_MBMODULE,    ' ' },
182        { "kernel",     LT_HYPER,       ' ' },
183        { NULL, 0, 0 },
184    };
185    
186    const char *grub2FindConfig(struct configFileInfo *cfi) {
187        static const char *configFiles[] = {
188     "/boot/grub/grub-efi.cfg",
189     "/boot/grub/grub.cfg",
190     NULL
191        };
192        static int i = -1;
193        static const char *grub_cfg = "/boot/grub/grub.cfg";
194    
195        if (i == -1) {
196     for (i = 0; configFiles[i] != NULL; i++) {
197        dbgPrintf("Checking \"%s\": ", configFiles[i]);
198        if (!access(configFiles[i], R_OK)) {
199     dbgPrintf("found\n");
200     return configFiles[i];
201        }
202     }
203        }
204    
205        /* Ubuntu renames grub2 to grub, so check for the grub.d directory
206         * that isn't in grub1, and if it exists, return the config file path
207         * that they use. */
208        if (configFiles[i] == NULL && !access("/etc/grub.d/", R_OK)) {
209     dbgPrintf("found\n");
210     return grub_cfg;
211        }
212    
213        dbgPrintf("not found\n");
214        return configFiles[i];
215    }
216    
217    struct configFileInfo grub2ConfigType = {
218        .findConfig = grub2FindConfig,
219        .keywords = grub2Keywords,
220        .defaultIsIndex = 1,
221        .defaultSupportSaved = 0,
222        .defaultIsVariable = 1,
223        .entryStart = LT_MENUENTRY,
224        .entryEnd = LT_ENTRY_END,
225        .titlePosition = 1,
226        .needsBootPrefix = 1,
227        .mbHyperFirst = 1,
228        .mbInitRdIsModule = 1,
229        .mbAllowExtraInitRds = 1,
230  };  };
231    
232  struct keywordTypes yabootKeywords[] = {  struct keywordTypes yabootKeywords[] = {
# Line 249  struct keywordTypes extlinuxKeywords[] = Line 324  struct keywordTypes extlinuxKeywords[] =
324  };  };
325  int useextlinuxmenu;  int useextlinuxmenu;
326  struct configFileInfo eliloConfigType = {  struct configFileInfo eliloConfigType = {
327      "/boot/efi/EFI/redhat/elilo.conf",    /* defaultConfig */      .defaultConfig = "/boot/efi/EFI/redhat/elilo.conf",
328      eliloKeywords,    /* keywords */      .keywords = eliloKeywords,
329      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
330      0,    /* defaultSupportSaved */      .needsBootPrefix = 1,
331      LT_KERNEL,    /* entrySeparator */      .argsInQuotes = 1,
332      1,                    /* needsBootPrefix */      .mbConcatArgs = 1,
     1,    /* argsInQuotes */  
     0,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     1,                                      /* mbConcatArgs */  
     0,                                      /* mbAllowExtraInitRds */  
333  };  };
334    
335  struct configFileInfo liloConfigType = {  struct configFileInfo liloConfigType = {
336      "/etc/lilo.conf",    /* defaultConfig */      .defaultConfig = "/etc/lilo.conf",
337      liloKeywords,    /* keywords */      .keywords = liloKeywords,
338      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
339      0,    /* defaultSupportSaved */      .argsInQuotes = 1,
340      LT_KERNEL,    /* entrySeparator */      .maxTitleLength = 15,
     0,    /* needsBootPrefix */  
     1,    /* argsInQuotes */  
     15,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     0,                                      /* mbAllowExtraInitRds */  
341  };  };
342    
343  struct configFileInfo yabootConfigType = {  struct configFileInfo yabootConfigType = {
344      "/etc/yaboot.conf",    /* defaultConfig */      .defaultConfig = "/etc/yaboot.conf",
345      yabootKeywords,    /* keywords */      .keywords = yabootKeywords,
346      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
347      0,    /* defaultSupportSaved */      .needsBootPrefix = 1,
348      LT_KERNEL,    /* entrySeparator */      .argsInQuotes = 1,
349      1,    /* needsBootPrefix */      .maxTitleLength = 15,
350      1,    /* argsInQuotes */      .mbAllowExtraInitRds = 1,
     15,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     1,                                      /* mbAllowExtraInitRds */  
351  };  };
352    
353  struct configFileInfo siloConfigType = {  struct configFileInfo siloConfigType = {
354      "/etc/silo.conf",    /* defaultConfig */      .defaultConfig = "/etc/silo.conf",
355      siloKeywords,    /* keywords */      .keywords = siloKeywords,
356      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
357      0,    /* defaultSupportSaved */      .needsBootPrefix = 1,
358      LT_KERNEL,    /* entrySeparator */      .argsInQuotes = 1,
359      1,    /* needsBootPrefix */      .maxTitleLength = 15,
     1,    /* argsInQuotes */  
     15,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     0,                                      /* mbAllowExtraInitRds */  
360  };  };
361    
362  struct configFileInfo ziplConfigType = {  struct configFileInfo ziplConfigType = {
363      "/etc/zipl.conf",    /* defaultConfig */      .defaultConfig = "/etc/zipl.conf",
364      ziplKeywords,    /* keywords */      .keywords = ziplKeywords,
365      0,    /* defaultIsIndex */      .entryStart = LT_TITLE,
366      0,    /* defaultSupportSaved */      .argsInQuotes = 1,
367      LT_TITLE,    /* entrySeparator */      .titleBracketed = 1,
     0,    /* needsBootPrefix */  
     1,    /* argsInQuotes */  
     0,    /* maxTitleLength */  
     1,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     0,                                      /* mbAllowExtraInitRds */  
368  };  };
369    
370  struct configFileInfo extlinuxConfigType = {  struct configFileInfo extlinuxConfigType = {
371      "/boot/extlinux/extlinux.conf",         /* defaultConfig */      .defaultConfig = "/boot/extlinux/extlinux.conf",
372      extlinuxKeywords,                       /* keywords */      .keywords = extlinuxKeywords,
373      0,                                      /* defaultIsIndex */      .entryStart = LT_TITLE,
374      0,                                      /* defaultSupportSaved */      .needsBootPrefix = 1,
375      LT_TITLE,                               /* entrySeparator */      .maxTitleLength = 255,
376      1,                                      /* needsBootPrefix */      .mbAllowExtraInitRds = 1,
     0,                                      /* argsInQuotes */  
     255,                                    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     1,                                      /* mbAllowExtraInitRds */  
377  };  };
378    
379  struct grubConfig {  struct grubConfig {
# Line 356  struct grubConfig { Line 388  struct grubConfig {
388      struct configFileInfo * cfi;      struct configFileInfo * cfi;
389  };  };
390    
391    blkid_cache blkid;
392    
393  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index);  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index);
394  struct singleEntry * findEntryByPath(struct grubConfig * cfg,  struct singleEntry * findEntryByPath(struct grubConfig * cfg,
395       const char * path, const char * prefix,       const char * path, const char * prefix,
# Line 369  static int lineWrite(FILE * out, struct Line 403  static int lineWrite(FILE * out, struct
403  static int getNextLine(char ** bufPtr, struct singleLine * line,  static int getNextLine(char ** bufPtr, struct singleLine * line,
404         struct configFileInfo * cfi);         struct configFileInfo * cfi);
405  static char * getRootSpecifier(char * str);  static char * getRootSpecifier(char * str);
406    static void requote(struct singleLine *line, struct configFileInfo * cfi);
407  static void insertElement(struct singleLine * line,  static void insertElement(struct singleLine * line,
408    const char * item, int insertHere,    const char * item, int insertHere,
409    struct configFileInfo * cfi);    struct configFileInfo * cfi);
# Line 433  static struct keywordTypes * getKeywordB Line 468  static struct keywordTypes * getKeywordB
468      return NULL;      return NULL;
469  }  }
470    
471  static char * getpathbyspec(char *device) {  static char *getKeyByType(enum lineType_e type, struct configFileInfo * cfi) {
472      static blkid_cache blkid;      struct keywordTypes *kt = getKeywordByType(type, cfi);
473        if (kt)
474     return kt->key;
475        return "unknown";
476    }
477    
478    static char * getpathbyspec(char *device) {
479      if (!blkid)      if (!blkid)
480          blkid_get_cache(&blkid, NULL);          blkid_get_cache(&blkid, NULL);
481    
482      return blkid_get_devname(blkid, device, NULL);      return blkid_get_devname(blkid, device, NULL);
483  }  }
484    
485    static char * getuuidbydev(char *device) {
486        if (!blkid)
487     blkid_get_cache(&blkid, NULL);
488    
489        return blkid_get_tag_value(blkid, "UUID", device);
490    }
491    
492  static enum lineType_e getTypeByKeyword(char * keyword,  static enum lineType_e getTypeByKeyword(char * keyword,
493   struct configFileInfo * cfi) {   struct configFileInfo * cfi) {
494      struct keywordTypes * kw;      struct keywordTypes * kw;
# Line 477  static int isBracketedTitle(struct singl Line 524  static int isBracketedTitle(struct singl
524      return 0;      return 0;
525  }  }
526    
527  static int isEntrySeparator(struct singleLine * line,  static int isEntryStart(struct singleLine * line,
528                              struct configFileInfo * cfi) {                              struct configFileInfo * cfi) {
529      return line->type == cfi->entrySeparator || line->type == LT_OTHER ||      return line->type == cfi->entryStart || line->type == LT_OTHER ||
530   (cfi->titleBracketed && isBracketedTitle(line));   (cfi->titleBracketed && isBracketedTitle(line));
531  }  }
532    
# Line 802  static struct grubConfig * readConfig(co Line 849  static struct grubConfig * readConfig(co
849      cfg->secondaryIndent = strdup(line->indent);      cfg->secondaryIndent = strdup(line->indent);
850   }   }
851    
852   if (isEntrySeparator(line, cfi)) {   if (isEntryStart(line, cfi)) {
853      sawEntry = 1;      sawEntry = 1;
854      if (!entry) {      if (!entry) {
855   cfg->entries = malloc(sizeof(*entry));   cfg->entries = malloc(sizeof(*entry));
# Line 818  static struct grubConfig * readConfig(co Line 865  static struct grubConfig * readConfig(co
865      entry->next = NULL;      entry->next = NULL;
866   }   }
867    
868   if (line->type == LT_DEFAULT && line->numElements == 2) {   if (line->type == LT_SET_VARIABLE) {
869        int i;
870        dbgPrintf("found 'set' command (%d elements): ", line->numElements);
871        dbgPrintf("%s", line->indent);
872        for (i = 0; i < line->numElements; i++)
873     dbgPrintf("%s\"%s\"", line->elements[i].indent, line->elements[i].item);
874        dbgPrintf("\n");
875        struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);
876        if (kwType && line->numElements == 3 &&
877        !strcmp(line->elements[1].item, kwType->key)) {
878     dbgPrintf("Line sets default config\n");
879     cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
880     defaultLine = line;
881        }
882     } else if (line->type == LT_DEFAULT && line->numElements == 2) {
883      cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;      cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
884      defaultLine = line;      defaultLine = line;
885    
# Line 931  static struct grubConfig * readConfig(co Line 992  static struct grubConfig * readConfig(co
992   entry->lines = line;   entry->lines = line;
993      else      else
994   last->next = line;   last->next = line;
995      dbgPrintf("readConfig added %d to %p\n", line->type, entry);      dbgPrintf("readConfig added %s to %p\n", getKeyByType(line->type, cfi), entry);
996    
997        /* we could have seen this outside of an entry... if so, we
998         * ignore it like any other line we don't grok */
999        if (line->type == LT_ENTRY_END && sawEntry)
1000     sawEntry = 0;
1001   } else {   } else {
1002      if (!cfg->theLines)      if (!cfg->theLines)
1003   cfg->theLines = line;   cfg->theLines = line;
1004      else      else
1005   last->next = line;   last->next = line;
1006      dbgPrintf("readConfig added %d to cfg\n", line->type);      dbgPrintf("readConfig added %s to cfg\n", getKeyByType(line->type, cfi));
1007   }   }
1008    
1009   last = line;   last = line;
# Line 945  static struct grubConfig * readConfig(co Line 1011  static struct grubConfig * readConfig(co
1011    
1012      free(incoming);      free(incoming);
1013    
1014        dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset");
1015      if (defaultLine) {      if (defaultLine) {
1016   if (cfi->defaultSupportSaved &&   if (cfi->defaultIsVariable) {
1017        char *value = defaultLine->elements[2].item;
1018        while (*value && (*value == '"' || *value == '\'' ||
1019        *value == ' ' || *value == '\t'))
1020     value++;
1021        cfg->defaultImage = strtol(value, &end, 10);
1022        while (*end && (*end == '"' || *end == '\'' ||
1023        *end == ' ' || *end == '\t'))
1024     end++;
1025        if (*end) cfg->defaultImage = -1;
1026     } else if (cfi->defaultSupportSaved &&
1027   !strncmp(defaultLine->elements[1].item, "saved", 5)) {   !strncmp(defaultLine->elements[1].item, "saved", 5)) {
1028      cfg->defaultImage = DEFAULT_SAVED;      cfg->defaultImage = DEFAULT_SAVED;
1029   } else if (cfi->defaultIsIndex) {   } else if (cfi->defaultIsIndex) {
# Line 995  static void writeDefault(FILE * out, cha Line 1072  static void writeDefault(FILE * out, cha
1072   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
1073      else if (cfg->defaultImage > -1) {      else if (cfg->defaultImage > -1) {
1074   if (cfg->cfi->defaultIsIndex) {   if (cfg->cfi->defaultIsIndex) {
1075      fprintf(out, "%sdefault%s%d\n", indent, separator,      if (cfg->cfi->defaultIsVariable) {
1076      cfg->defaultImage);          fprintf(out, "%sset default=\"%d\"\n", indent,
1077     cfg->defaultImage);
1078        } else {
1079     fprintf(out, "%sdefault%s%d\n", indent, separator,
1080     cfg->defaultImage);
1081        }
1082   } else {   } else {
1083      int image = cfg->defaultImage;      int image = cfg->defaultImage;
1084    
# Line 1086  static int writeConfig(struct grubConfig Line 1168  static int writeConfig(struct grubConfig
1168      }      }
1169    
1170      line = cfg->theLines;      line = cfg->theLines;
1171        struct keywordTypes *defaultKw = getKeywordByType(LT_DEFAULT, cfg->cfi);
1172      while (line) {      while (line) {
1173   if (line->type == LT_DEFAULT) {          if (line->type == LT_SET_VARIABLE && defaultKw &&
1174     line->numElements == 3 &&
1175     !strcmp(line->elements[1].item, defaultKw->key)) {
1176        writeDefault(out, line->indent, line->elements[0].indent, cfg);
1177        needs &= ~MAIN_DEFAULT;
1178     } else if (line->type == LT_DEFAULT) {
1179      writeDefault(out, line->indent, line->elements[0].indent, cfg);      writeDefault(out, line->indent, line->elements[0].indent, cfg);
1180      needs &= ~MAIN_DEFAULT;      needs &= ~MAIN_DEFAULT;
1181   } else if (line->type == LT_FALLBACK) {   } else if (line->type == LT_FALLBACK) {
# Line 1155  static int numEntries(struct grubConfig Line 1243  static int numEntries(struct grubConfig
1243      return i;      return i;
1244  }  }
1245    
1246    static char *findDiskForRoot()
1247    {
1248        int fd;
1249        char buf[65536];
1250        char *devname;
1251        char *chptr;
1252        int rc;
1253    
1254        if ((fd = open(_PATH_MOUNTED, O_RDONLY)) < 0) {
1255            fprintf(stderr, "grubby: failed to open %s: %s\n",
1256                    _PATH_MOUNTED, strerror(errno));
1257            return NULL;
1258        }
1259    
1260        rc = read(fd, buf, sizeof(buf) - 1);
1261        if (rc <= 0) {
1262            fprintf(stderr, "grubby: failed to read %s: %s\n",
1263                    _PATH_MOUNTED, strerror(errno));
1264            close(fd);
1265            return NULL;
1266        }
1267        close(fd);
1268        buf[rc] = '\0';
1269        chptr = buf;
1270    
1271        while (chptr && chptr != buf+rc) {
1272            devname = chptr;
1273    
1274            /*
1275             * The first column of a mtab entry is the device, but if the entry is a
1276             * special device it won't start with /, so move on to the next line.
1277             */
1278            if (*devname != '/') {
1279                chptr = strchr(chptr, '\n');
1280                if (chptr)
1281                    chptr++;
1282                continue;
1283            }
1284    
1285            /* Seek to the next space */
1286            chptr = strchr(chptr, ' ');
1287            if (!chptr) {
1288                fprintf(stderr, "grubby: error parsing %s: %s\n",
1289                        _PATH_MOUNTED, strerror(errno));
1290                return NULL;
1291            }
1292    
1293            /*
1294             * The second column of a mtab entry is the mount point, we are looking
1295             * for '/' obviously.
1296             */
1297            if (*(++chptr) == '/' && *(++chptr) == ' ') {
1298                /*
1299                 * Move back 2, which is the first space after the device name, set
1300                 * it to \0 so strdup will just get the devicename.
1301                 */
1302                chptr -= 2;
1303                *chptr = '\0';
1304                return strdup(devname);
1305            }
1306    
1307            /* Next line */
1308            chptr = strchr(chptr, '\n');
1309            if (chptr)
1310                chptr++;
1311        }
1312    
1313        return NULL;
1314    }
1315    
1316  int suitableImage(struct singleEntry * entry, const char * bootPrefix,  int suitableImage(struct singleEntry * entry, const char * bootPrefix,
1317    int skipRemoved, int flags) {    int skipRemoved, int flags) {
1318      struct singleLine * line;      struct singleLine * line;
1319      char * fullName;      char * fullName;
1320      int i;      int i;
     struct stat sb, sb2;  
1321      char * dev;      char * dev;
1322      char * rootspec;      char * rootspec;
1323        char * rootdev;
1324    
1325      if (skipRemoved && entry->skip) return 0;      if (skipRemoved && entry->skip) return 0;
1326    
# Line 1212  int suitableImage(struct singleEntry * e Line 1370  int suitableImage(struct singleEntry * e
1370      if (!dev)      if (!dev)
1371          return 0;          return 0;
1372    
1373      i = stat(dev, &sb);      rootdev = findDiskForRoot();
1374      if (i)      if (!rootdev)
1375   return 0;   return 0;
1376    
1377      stat("/", &sb2);      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {
1378            free(rootdev);
1379            return 0;
1380        }
1381    
1382      if (sb.st_rdev != sb2.st_dev)      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {
1383     free(rootdev);
1384          return 0;          return 0;
1385        }
1386    
1387        free(rootdev);
1388    
1389      return 1;      return 1;
1390  }  }
# Line 1733  struct singleLine *  addLine(struct sing Line 1898  struct singleLine *  addLine(struct sing
1898   sprintf(tmpl.elements[0].item, "[%s]", val);   sprintf(tmpl.elements[0].item, "[%s]", val);
1899   tmpl.elements[0].indent = "";   tmpl.elements[0].indent = "";
1900   val = NULL;   val = NULL;
1901        } else if (type == LT_MENUENTRY) {
1902     char *lineend = "--class gnu-linux --class gnu --class os {";
1903     if (!val) {
1904        fprintf(stderr, "Line type LT_MENUENTRY requires a value\n");
1905        abort();
1906     }
1907     kw = getKeywordByType(type, cfi);
1908     if (!kw) {
1909        fprintf(stderr, "Looking up keyword for unknown type %d\n", type);
1910        abort();
1911     }
1912     tmpl.indent = "";
1913     tmpl.type = type;
1914     tmpl.numElements = 3;
1915     tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements);
1916     tmpl.elements[0].item = kw->key;
1917     tmpl.elements[0].indent = alloca(2);
1918     sprintf(tmpl.elements[0].indent, "%c", kw->nextChar);
1919     tmpl.elements[1].item = (char *)val;
1920     tmpl.elements[1].indent = alloca(2);
1921     sprintf(tmpl.elements[1].indent, "%c", kw->nextChar);
1922     tmpl.elements[2].item = alloca(strlen(lineend)+1);
1923     strcpy(tmpl.elements[2].item, lineend);
1924     tmpl.elements[2].indent = "";
1925      } else {      } else {
1926   kw = getKeywordByType(type, cfi);   kw = getKeywordByType(type, cfi);
1927   if (!kw) abort();   if (!kw) {
1928        fprintf(stderr, "Looking up keyword for unknown type %d\n", type);
1929        abort();
1930     }
1931   tmpl.type = type;   tmpl.type = type;
1932   tmpl.numElements = val ? 2 : 1;   tmpl.numElements = val ? 2 : 1;
1933   tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements);   tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements);
# Line 1759  struct singleLine *  addLine(struct sing Line 1951  struct singleLine *  addLine(struct sing
1951   if (!line->next && !prev) prev = line;   if (!line->next && !prev) prev = line;
1952      }      }
1953    
1954      if (prev == entry->lines)      struct singleLine *menuEntry;
1955   tmpl.indent = defaultIndent ?: "";      menuEntry = getLineByType(LT_MENUENTRY, entry->lines);
1956      else      if (tmpl.type == LT_ENTRY_END) {
1957   tmpl.indent = prev->indent;   if (menuEntry)
1958        tmpl.indent = menuEntry->indent;
1959     else
1960        tmpl.indent = defaultIndent ?: "";
1961        } else if (tmpl.type != LT_MENUENTRY) {
1962     if (menuEntry)
1963        tmpl.indent = "\t";
1964     else if (prev == entry->lines)
1965        tmpl.indent = defaultIndent ?: "";
1966     else
1967        tmpl.indent = prev->indent;
1968        }
1969    
1970      return addLineTmpl(entry, &tmpl, prev, val, cfi);      return addLineTmpl(entry, &tmpl, prev, val, cfi);
1971  }  }
# Line 1789  void removeLine(struct singleEntry * ent Line 1992  void removeLine(struct singleEntry * ent
1992      free(line);      free(line);
1993  }  }
1994    
1995    static int isquote(char q)
1996    {
1997        if (q == '\'' || q == '\"')
1998     return 1;
1999        return 0;
2000    }
2001    
2002    static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi)
2003    {
2004        struct singleLine newLine = {
2005     .indent = tmplLine->indent,
2006     .type = tmplLine->type,
2007     .next = tmplLine->next,
2008        };
2009        int firstQuotedItem = -1;
2010        int quoteLen = 0;
2011        int j;
2012        int element = 0;
2013        char *c;
2014    
2015        c = malloc(strlen(tmplLine->elements[0].item) + 1);
2016        strcpy(c, tmplLine->elements[0].item);
2017        insertElement(&newLine, c, element++, cfi);
2018        free(c);
2019        c = NULL;
2020    
2021        for (j = 1; j < tmplLine->numElements; j++) {
2022     if (firstQuotedItem == -1) {
2023        quoteLen += strlen(tmplLine->elements[j].item);
2024        
2025        if (isquote(tmplLine->elements[j].item[0])) {
2026     firstQuotedItem = j;
2027            quoteLen += strlen(tmplLine->elements[j].indent);
2028        } else {
2029     c = malloc(quoteLen + 1);
2030     strcpy(c, tmplLine->elements[j].item);
2031     insertElement(&newLine, c, element++, cfi);
2032     free(c);
2033     quoteLen = 0;
2034        }
2035     } else {
2036        int itemlen = strlen(tmplLine->elements[j].item);
2037        quoteLen += itemlen;
2038        quoteLen += strlen(tmplLine->elements[j].indent);
2039        
2040        if (isquote(tmplLine->elements[j].item[itemlen - 1])) {
2041     c = malloc(quoteLen + 1);
2042     c[0] = '\0';
2043     for (int i = firstQuotedItem; i < j+1; i++) {
2044        strcat(c, tmplLine->elements[i].item);
2045        strcat(c, tmplLine->elements[i].indent);
2046     }
2047     insertElement(&newLine, c, element++, cfi);
2048     free(c);
2049    
2050     firstQuotedItem = -1;
2051     quoteLen = 0;
2052        }
2053     }
2054        }
2055        while (tmplLine->numElements)
2056     removeElement(tmplLine, 0);
2057        if (tmplLine->elements)
2058     free(tmplLine->elements);
2059    
2060        tmplLine->numElements = newLine.numElements;
2061        tmplLine->elements = newLine.elements;
2062    }
2063    
2064  static void insertElement(struct singleLine * line,  static void insertElement(struct singleLine * line,
2065    const char * item, int insertHere,    const char * item, int insertHere,
2066    struct configFileInfo * cfi)    struct configFileInfo * cfi)
# Line 1895  int updateActualImage(struct grubConfig Line 2167  int updateActualImage(struct grubConfig
2167      const char ** arg;      const char ** arg;
2168      int useKernelArgs, useRoot;      int useKernelArgs, useRoot;
2169      int firstElement;      int firstElement;
2170      int *usedElements, *usedArgs;      int *usedElements;
2171      int doreplace;      int doreplace;
2172    
2173      if (!image) return 0;      if (!image) return 0;
# Line 1930  int updateActualImage(struct grubConfig Line 2202  int updateActualImage(struct grubConfig
2202      useRoot = (getKeywordByType(LT_ROOT, cfg->cfi)      useRoot = (getKeywordByType(LT_ROOT, cfg->cfi)
2203         && !multibootArgs);         && !multibootArgs);
2204    
     for (k = 0, arg = newArgs; *arg; arg++, k++) ;  
     usedArgs = calloc(k, sizeof(*usedArgs));  
   
2205      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
2206    
2207   if (multibootArgs && !entry->multiboot)   if (multibootArgs && !entry->multiboot)
# Line 2008  int updateActualImage(struct grubConfig Line 2277  int updateActualImage(struct grubConfig
2277          usedElements = calloc(line->numElements, sizeof(*usedElements));          usedElements = calloc(line->numElements, sizeof(*usedElements));
2278    
2279   for (k = 0, arg = newArgs; *arg; arg++, k++) {   for (k = 0, arg = newArgs; *arg; arg++, k++) {
             if (usedArgs[k]) continue;  
2280    
2281      doreplace = 1;      doreplace = 1;
2282      for (i = firstElement; i < line->numElements; i++) {      for (i = firstElement; i < line->numElements; i++) {
# Line 2023  int updateActualImage(struct grubConfig Line 2291  int updateActualImage(struct grubConfig
2291                      continue;                      continue;
2292   if (!argMatch(line->elements[i].item, *arg)) {   if (!argMatch(line->elements[i].item, *arg)) {
2293                      usedElements[i]=1;                      usedElements[i]=1;
                     usedArgs[k]=1;  
2294      break;      break;
2295                  }                  }
2296              }              }
# Line 2093  int updateActualImage(struct grubConfig Line 2360  int updateActualImage(struct grubConfig
2360   }   }
2361      }      }
2362    
     free(usedArgs);  
2363      free(newArgs);      free(newArgs);
2364      free(oldArgs);      free(oldArgs);
2365    
# Line 2119  int updateImage(struct grubConfig * cfg, Line 2385  int updateImage(struct grubConfig * cfg,
2385      return rc;      return rc;
2386  }  }
2387    
2388    int updateInitrd(struct grubConfig * cfg, const char * image,
2389                     const char * prefix, const char * initrd) {
2390        struct singleEntry * entry;
2391        struct singleLine * line, * kernelLine, *endLine = NULL;
2392        int index = 0;
2393    
2394        if (!image) return 0;
2395    
2396        for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
2397            kernelLine = getLineByType(LT_KERNEL, entry->lines);
2398            if (!kernelLine) continue;
2399    
2400            line = getLineByType(LT_INITRD, entry->lines);
2401            if (line)
2402                removeLine(entry, line);
2403            if (prefix) {
2404                int prefixLen = strlen(prefix);
2405                if (!strncmp(initrd, prefix, prefixLen))
2406                    initrd += prefixLen;
2407            }
2408     endLine = getLineByType(LT_ENTRY_END, entry->lines);
2409     if (endLine)
2410        removeLine(entry, endLine);
2411            line = addLine(entry, cfg->cfi, LT_INITRD, kernelLine->indent, initrd);
2412            if (!line)
2413        return 1;
2414     if (endLine) {
2415        line = addLine(entry, cfg->cfi, LT_ENTRY_END, "", NULL);
2416                if (!line)
2417     return 1;
2418     }
2419    
2420            break;
2421        }
2422    
2423        return 0;
2424    }
2425    
2426  int checkDeviceBootloader(const char * device, const unsigned char * boot) {  int checkDeviceBootloader(const char * device, const unsigned char * boot) {
2427      int fd;      int fd;
2428      unsigned char bootSect[512];      unsigned char bootSect[512];
# Line 2289  int checkForLilo(struct grubConfig * con Line 2593  int checkForLilo(struct grubConfig * con
2593      return checkDeviceBootloader(line->elements[1].item, boot);      return checkDeviceBootloader(line->elements[1].item, boot);
2594  }  }
2595    
2596    int checkForGrub2(struct grubConfig * config) {
2597        if (!access("/etc/grub.d/", R_OK))
2598     return 2;
2599    
2600        return 1;
2601    }
2602    
2603  int checkForGrub(struct grubConfig * config) {  int checkForGrub(struct grubConfig * config) {
2604      int fd;      int fd;
2605      unsigned char bootSect[512];      unsigned char bootSect[512];
# Line 2464  int addNewKernel(struct grubConfig * con Line 2775  int addNewKernel(struct grubConfig * con
2775      if (*chptr == '#') continue;      if (*chptr == '#') continue;
2776    
2777      if (tmplLine->type == LT_KERNEL &&      if (tmplLine->type == LT_KERNEL &&
2778   tmplLine->numElements >= 2) {      tmplLine->numElements >= 2) {
2779   if (!template->multiboot && (needs & NEED_MB)) {   if (!template->multiboot && (needs & NEED_MB)) {
2780      /* it's not a multiboot template and this is the kernel      /* it's not a multiboot template and this is the kernel
2781       * line.  Try to be intelligent about inserting the       * line.  Try to be intelligent about inserting the
# Line 2590  int addNewKernel(struct grubConfig * con Line 2901  int addNewKernel(struct grubConfig * con
2901      needs &= ~NEED_INITRD;      needs &= ~NEED_INITRD;
2902   }   }
2903    
2904        } else if (tmplLine->type == LT_MENUENTRY &&
2905           (needs & NEED_TITLE)) {
2906     requote(tmplLine, config->cfi);
2907     char *nkt = malloc(strlen(newKernelTitle)+3);
2908     strcpy(nkt, "'");
2909     strcat(nkt, newKernelTitle);
2910     strcat(nkt, "'");
2911     newLine = addLineTmpl(new, tmplLine, newLine, nkt, config->cfi);
2912     free(nkt);
2913     needs &= ~NEED_TITLE;
2914      } else if (tmplLine->type == LT_TITLE &&      } else if (tmplLine->type == LT_TITLE &&
2915         (needs & NEED_TITLE)) {         (needs & NEED_TITLE)) {
2916   if (tmplLine->numElements >= 2) {   if (tmplLine->numElements >= 2) {
# Line 2603  int addNewKernel(struct grubConfig * con Line 2924  int addNewKernel(struct grubConfig * con
2924        tmplLine->indent, newKernelTitle);        tmplLine->indent, newKernelTitle);
2925      needs &= ~NEED_TITLE;      needs &= ~NEED_TITLE;
2926   }   }
2927        } else if (tmplLine->type == LT_ECHO) {
2928        requote(tmplLine, config->cfi);
2929        if (tmplLine->numElements > 1 &&
2930        strstr(tmplLine->elements[1].item, "'Loading Linux ")) {
2931     char *prefix = "'Loading ";
2932     char *newTitle = malloc(strlen(prefix) +
2933     strlen(newKernelTitle) + 2);
2934    
2935     strcpy(newTitle, prefix);
2936     strcat(newTitle, newKernelTitle);
2937     strcat(newTitle, "'");
2938     newLine = addLine(new, config->cfi, LT_ECHO,
2939     tmplLine->indent, newTitle);
2940     free(newTitle);
2941        } else {
2942     /* pass through other lines from the template */
2943     newLine = addLineTmpl(new, tmplLine, newLine, NULL,
2944     config->cfi);
2945        }
2946      } else {      } else {
2947   /* pass through other lines from the template */   /* pass through other lines from the template */
2948   newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);   newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);
# Line 2614  int addNewKernel(struct grubConfig * con Line 2953  int addNewKernel(struct grubConfig * con
2953   /* don't have a template, so start the entry with the   /* don't have a template, so start the entry with the
2954   * appropriate starting line   * appropriate starting line
2955   */   */
2956   switch (config->cfi->entrySeparator) {   switch (config->cfi->entryStart) {
2957      case LT_KERNEL:      case LT_KERNEL:
2958   if (new->multiboot && config->cfi->mbHyperFirst) {   if (new->multiboot && config->cfi->mbHyperFirst) {
2959      /* fall through to LT_HYPER */      /* fall through to LT_HYPER */
# Line 2633  int addNewKernel(struct grubConfig * con Line 2972  int addNewKernel(struct grubConfig * con
2972   needs &= ~NEED_MB;   needs &= ~NEED_MB;
2973   break;   break;
2974    
2975        case LT_MENUENTRY: {
2976     char *nkt = malloc(strlen(newKernelTitle)+3);
2977     strcpy(nkt, "'");
2978     strcat(nkt, newKernelTitle);
2979     strcat(nkt, "'");
2980            newLine = addLine(new, config->cfi, LT_MENUENTRY,
2981      config->primaryIndent, nkt);
2982     free(nkt);
2983     needs &= ~NEED_TITLE;
2984     needs |= NEED_END;
2985     break;
2986        }
2987      case LT_TITLE:      case LT_TITLE:
2988   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)
2989   char * templabel;   char * templabel;
# Line 2666  int addNewKernel(struct grubConfig * con Line 3017  int addNewKernel(struct grubConfig * con
3017    
3018      /* add the remainder of the lines, i.e. those that either      /* add the remainder of the lines, i.e. those that either
3019       * 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,
3020       * all the lines following the entrySeparator.       * all the lines following the entryStart.
3021       */       */
3022      if (needs & NEED_TITLE) {      if (needs & NEED_TITLE) {
3023   newLine = addLine(new, config->cfi, LT_TITLE,   newLine = addLine(new, config->cfi, LT_TITLE,
# Line 2707  int addNewKernel(struct grubConfig * con Line 3058  int addNewKernel(struct grubConfig * con
3058   free(initrdVal);   free(initrdVal);
3059   needs &= ~NEED_INITRD;   needs &= ~NEED_INITRD;
3060      }      }
3061        if (needs & NEED_END) {
3062     newLine = addLine(new, config->cfi, LT_ENTRY_END,
3063     config->secondaryIndent, NULL);
3064     needs &= ~NEED_END;
3065        }
3066    
3067      if (needs) {      if (needs) {
3068   printf(_("grubby: needs=%d, aborting\n"), needs);   printf(_("grubby: needs=%d, aborting\n"), needs);
# Line 2736  static void traceback(int signum) Line 3092  static void traceback(int signum)
3092    
3093  int main(int argc, const char ** argv) {  int main(int argc, const char ** argv) {
3094      poptContext optCon;      poptContext optCon;
3095      char * grubConfig = NULL;      const char * grubConfig = NULL;
3096      char * outputFile = NULL;      char * outputFile = NULL;
3097      int arg = 0;      int arg = 0;
3098      int flags = 0;      int flags = 0;
3099      int badImageOkay = 0;      int badImageOkay = 0;
3100        int configureGrub2 = 0;
3101      int configureLilo = 0, configureELilo = 0, configureGrub = 0;      int configureLilo = 0, configureELilo = 0, configureGrub = 0;
3102      int configureYaboot = 0, configureSilo = 0, configureZipl = 0;      int configureYaboot = 0, configureSilo = 0, configureZipl = 0;
3103      int configureExtLinux = 0;      int configureExtLinux = 0;
# Line 2806  int main(int argc, const char ** argv) { Line 3163  int main(int argc, const char ** argv) {
3163      _("configure extlinux bootloader (from syslinux)") },      _("configure extlinux bootloader (from syslinux)") },
3164   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,
3165      _("configure grub bootloader") },      _("configure grub bootloader") },
3166     { "grub2", 0, POPT_ARG_NONE, &configureGrub2, 0,
3167        _("configure grub2 bootloader") },
3168   { "info", 0, POPT_ARG_STRING, &kernelInfo, 0,   { "info", 0, POPT_ARG_STRING, &kernelInfo, 0,
3169      _("display boot information for specified kernel"),      _("display boot information for specified kernel"),
3170      _("kernel-path") },      _("kernel-path") },
# Line 2885  int main(int argc, const char ** argv) { Line 3244  int main(int argc, const char ** argv) {
3244   return 1;   return 1;
3245      }      }
3246    
3247      if ((configureLilo + configureGrub + configureELilo +      if ((configureLilo + configureGrub2 + configureGrub + configureELilo +
3248   configureYaboot + configureSilo + configureZipl +   configureYaboot + configureSilo + configureZipl +
3249   configureExtLinux ) > 1) {   configureExtLinux ) > 1) {
3250   fprintf(stderr, _("grubby: cannot specify multiple bootloaders\n"));   fprintf(stderr, _("grubby: cannot specify multiple bootloaders\n"));
# Line 2894  int main(int argc, const char ** argv) { Line 3253  int main(int argc, const char ** argv) {
3253   fprintf(stderr,   fprintf(stderr,
3254      _("grubby: cannot specify config file with --bootloader-probe\n"));      _("grubby: cannot specify config file with --bootloader-probe\n"));
3255   return 1;   return 1;
3256        } else if (configureGrub2) {
3257     cfi = &grub2ConfigType;
3258      } else if (configureLilo) {      } else if (configureLilo) {
3259   cfi = &liloConfigType;   cfi = &liloConfigType;
3260      } else if (configureGrub) {      } else if (configureGrub) {
# Line 2923  int main(int argc, const char ** argv) { Line 3284  int main(int argc, const char ** argv) {
3284        #elif __s390x__        #elif __s390x__
3285          cfi = &ziplConfigtype;          cfi = &ziplConfigtype;
3286        #else        #else
3287   cfi = &grubConfigType;          if (grub2FindConfig(&grub2ConfigType))
3288        cfi = &grub2ConfigType;
3289     else
3290        cfi = &grubConfigType;
3291        #endif        #endif
3292      }      }
3293    
3294      if (!grubConfig)      if (!grubConfig) {
3295   grubConfig = cfi->defaultConfig;   if (cfi->findConfig)
3296        grubConfig = cfi->findConfig(cfi);
3297     if (!grubConfig)
3298        grubConfig = cfi->defaultConfig;
3299        }
3300    
3301      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||
3302    newKernelPath || removeKernelPath || makeDefault ||    newKernelPath || removeKernelPath || makeDefault ||
# Line 2948  int main(int argc, const char ** argv) { Line 3316  int main(int argc, const char ** argv) {
3316      if (newKernelPath && !newKernelTitle) {      if (newKernelPath && !newKernelTitle) {
3317   fprintf(stderr, _("grubby: kernel title must be specified\n"));   fprintf(stderr, _("grubby: kernel title must be specified\n"));
3318   return 1;   return 1;
3319      } else if (!newKernelPath && (newKernelTitle  || newKernelInitrd ||      } else if (!newKernelPath && (newKernelTitle  || copyDefault ||
3320    newKernelInitrd || copyDefault     ||    (newKernelInitrd && !updateKernelPath)||
3321    makeDefault || extraInitrdCount > 0)) {    makeDefault || extraInitrdCount > 0)) {
3322   fprintf(stderr, _("grubby: kernel path expected\n"));   fprintf(stderr, _("grubby: kernel path expected\n"));
3323   return 1;   return 1;
# Line 3010  int main(int argc, const char ** argv) { Line 3378  int main(int argc, const char ** argv) {
3378      }      }
3379    
3380      if (bootloaderProbe) {      if (bootloaderProbe) {
3381   int lrc = 0, grc = 0, erc = 0;   int lrc = 0, grc = 0, gr2c = 0, erc = 0;
3382   struct grubConfig * lconfig, * gconfig;   struct grubConfig * lconfig, * gconfig;
3383    
3384     const char *grub2config = grub2FindConfig(&grub2ConfigType);
3385     if (grub2config) {
3386        gconfig = readConfig(grub2config, &grub2ConfigType);
3387        if (!gconfig)
3388     gr2c = 1;
3389        else
3390     gr2c = checkForGrub2(gconfig);
3391     }
3392    
3393   if (!access(grubConfigType.defaultConfig, F_OK)) {   if (!access(grubConfigType.defaultConfig, F_OK)) {
3394      gconfig = readConfig(grubConfigType.defaultConfig, &grubConfigType);      gconfig = readConfig(grubConfigType.defaultConfig, &grubConfigType);
3395      if (!gconfig)      if (!gconfig)
# Line 3037  int main(int argc, const char ** argv) { Line 3414  int main(int argc, const char ** argv) {
3414   erc = checkForExtLinux(lconfig);   erc = checkForExtLinux(lconfig);
3415   }   }
3416    
3417   if (lrc == 1 || grc == 1) return 1;   if (lrc == 1 || grc == 1 || gr2c == 1) return 1;
3418    
3419   if (lrc == 2) printf("lilo\n");   if (lrc == 2) printf("lilo\n");
3420     if (gr2c == 2) printf("grub2\n");
3421   if (grc == 2) printf("grub\n");   if (grc == 2) printf("grub\n");
3422   if (erc == 2) printf("extlinux\n");   if (erc == 2) printf("extlinux\n");
3423    
# Line 3082  int main(int argc, const char ** argv) { Line 3460  int main(int argc, const char ** argv) {
3460      setFallbackImage(config, newKernelPath != NULL);      setFallbackImage(config, newKernelPath != NULL);
3461      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,
3462                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;
3463        if (updateKernelPath && newKernelInitrd) {
3464                if (updateInitrd(config, updateKernelPath, bootPrefix,
3465                                 newKernelInitrd)) return 1;
3466        }
3467      if (addNewKernel(config, template, bootPrefix, newKernelPath,      if (addNewKernel(config, template, bootPrefix, newKernelPath,
3468                       newKernelTitle, newKernelArgs, newKernelInitrd,                       newKernelTitle, newKernelArgs, newKernelInitrd,
3469                       extraInitrds, extraInitrdCount,                       extraInitrds, extraInitrdCount,
# Line 3095  int main(int argc, const char ** argv) { Line 3477  int main(int argc, const char ** argv) {
3477      }      }
3478    
3479      if (!outputFile)      if (!outputFile)
3480   outputFile = grubConfig;   outputFile = (char *)grubConfig;
3481    
3482      return writeConfig(config, outputFile, bootPrefix);      return writeConfig(config, outputFile, bootPrefix);
3483  }  }

Legend:
Removed from v.926  
changed lines
  Added in v.1714