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 1696 by niro, Fri Feb 17 23:46:24 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    
194        if (i == -1) {
195     for (i = 0; configFiles[i] != NULL; i++) {
196        dbgPrintf("Checking \"%s\": ", configFiles[i]);
197        if (!access(configFiles[i], R_OK)) {
198     dbgPrintf("found\n");
199     return configFiles[i];
200        }
201        dbgPrintf("not found\n");
202     }
203        }
204        return configFiles[i];
205    }
206    
207    struct configFileInfo grub2ConfigType = {
208        .findConfig = grub2FindConfig,
209        .keywords = grub2Keywords,
210        .defaultIsIndex = 1,
211        .defaultSupportSaved = 0,
212        .defaultIsVariable = 1,
213        .entryStart = LT_MENUENTRY,
214        .entryEnd = LT_ENTRY_END,
215        .titlePosition = 1,
216        .needsBootPrefix = 1,
217        .mbHyperFirst = 1,
218        .mbInitRdIsModule = 1,
219        .mbAllowExtraInitRds = 1,
220  };  };
221    
222  struct keywordTypes yabootKeywords[] = {  struct keywordTypes yabootKeywords[] = {
# Line 249  struct keywordTypes extlinuxKeywords[] = Line 314  struct keywordTypes extlinuxKeywords[] =
314  };  };
315  int useextlinuxmenu;  int useextlinuxmenu;
316  struct configFileInfo eliloConfigType = {  struct configFileInfo eliloConfigType = {
317      "/boot/efi/EFI/redhat/elilo.conf",    /* defaultConfig */      .defaultConfig = "/boot/efi/EFI/redhat/elilo.conf",
318      eliloKeywords,    /* keywords */      .keywords = eliloKeywords,
319      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
320      0,    /* defaultSupportSaved */      .needsBootPrefix = 1,
321      LT_KERNEL,    /* entrySeparator */      .argsInQuotes = 1,
322      1,                    /* needsBootPrefix */      .mbConcatArgs = 1,
     1,    /* argsInQuotes */  
     0,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     1,                                      /* mbConcatArgs */  
     0,                                      /* mbAllowExtraInitRds */  
323  };  };
324    
325  struct configFileInfo liloConfigType = {  struct configFileInfo liloConfigType = {
326      "/etc/lilo.conf",    /* defaultConfig */      .defaultConfig = "/etc/lilo.conf",
327      liloKeywords,    /* keywords */      .keywords = liloKeywords,
328      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
329      0,    /* defaultSupportSaved */      .argsInQuotes = 1,
330      LT_KERNEL,    /* entrySeparator */      .maxTitleLength = 15,
     0,    /* needsBootPrefix */  
     1,    /* argsInQuotes */  
     15,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     0,                                      /* mbAllowExtraInitRds */  
331  };  };
332    
333  struct configFileInfo yabootConfigType = {  struct configFileInfo yabootConfigType = {
334      "/etc/yaboot.conf",    /* defaultConfig */      .defaultConfig = "/etc/yaboot.conf",
335      yabootKeywords,    /* keywords */      .keywords = yabootKeywords,
336      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
337      0,    /* defaultSupportSaved */      .needsBootPrefix = 1,
338      LT_KERNEL,    /* entrySeparator */      .argsInQuotes = 1,
339      1,    /* needsBootPrefix */      .maxTitleLength = 15,
340      1,    /* argsInQuotes */      .mbAllowExtraInitRds = 1,
     15,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     1,                                      /* mbAllowExtraInitRds */  
341  };  };
342    
343  struct configFileInfo siloConfigType = {  struct configFileInfo siloConfigType = {
344      "/etc/silo.conf",    /* defaultConfig */      .defaultConfig = "/etc/silo.conf",
345      siloKeywords,    /* keywords */      .keywords = siloKeywords,
346      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
347      0,    /* defaultSupportSaved */      .needsBootPrefix = 1,
348      LT_KERNEL,    /* entrySeparator */      .argsInQuotes = 1,
349      1,    /* needsBootPrefix */      .maxTitleLength = 15,
     1,    /* argsInQuotes */  
     15,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     0,                                      /* mbAllowExtraInitRds */  
350  };  };
351    
352  struct configFileInfo ziplConfigType = {  struct configFileInfo ziplConfigType = {
353      "/etc/zipl.conf",    /* defaultConfig */      .defaultConfig = "/etc/zipl.conf",
354      ziplKeywords,    /* keywords */      .keywords = ziplKeywords,
355      0,    /* defaultIsIndex */      .entryStart = LT_TITLE,
356      0,    /* defaultSupportSaved */      .argsInQuotes = 1,
357      LT_TITLE,    /* entrySeparator */      .titleBracketed = 1,
     0,    /* needsBootPrefix */  
     1,    /* argsInQuotes */  
     0,    /* maxTitleLength */  
     1,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     0,                                      /* mbAllowExtraInitRds */  
358  };  };
359    
360  struct configFileInfo extlinuxConfigType = {  struct configFileInfo extlinuxConfigType = {
361      "/boot/extlinux/extlinux.conf",         /* defaultConfig */      .defaultConfig = "/boot/extlinux/extlinux.conf",
362      extlinuxKeywords,                       /* keywords */      .keywords = extlinuxKeywords,
363      0,                                      /* defaultIsIndex */      .entryStart = LT_TITLE,
364      0,                                      /* defaultSupportSaved */      .needsBootPrefix = 1,
365      LT_TITLE,                               /* entrySeparator */      .maxTitleLength = 255,
366      1,                                      /* needsBootPrefix */      .mbAllowExtraInitRds = 1,
     0,                                      /* argsInQuotes */  
     255,                                    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     1,                                      /* mbAllowExtraInitRds */  
367  };  };
368    
369  struct grubConfig {  struct grubConfig {
# Line 356  struct grubConfig { Line 378  struct grubConfig {
378      struct configFileInfo * cfi;      struct configFileInfo * cfi;
379  };  };
380    
381    blkid_cache blkid;
382    
383  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index);  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index);
384  struct singleEntry * findEntryByPath(struct grubConfig * cfg,  struct singleEntry * findEntryByPath(struct grubConfig * cfg,
385       const char * path, const char * prefix,       const char * path, const char * prefix,
# Line 369  static int lineWrite(FILE * out, struct Line 393  static int lineWrite(FILE * out, struct
393  static int getNextLine(char ** bufPtr, struct singleLine * line,  static int getNextLine(char ** bufPtr, struct singleLine * line,
394         struct configFileInfo * cfi);         struct configFileInfo * cfi);
395  static char * getRootSpecifier(char * str);  static char * getRootSpecifier(char * str);
396    static void requote(struct singleLine *line, struct configFileInfo * cfi);
397  static void insertElement(struct singleLine * line,  static void insertElement(struct singleLine * line,
398    const char * item, int insertHere,    const char * item, int insertHere,
399    struct configFileInfo * cfi);    struct configFileInfo * cfi);
# Line 433  static struct keywordTypes * getKeywordB Line 458  static struct keywordTypes * getKeywordB
458      return NULL;      return NULL;
459  }  }
460    
461  static char * getpathbyspec(char *device) {  static char *getKeyByType(enum lineType_e type, struct configFileInfo * cfi) {
462      static blkid_cache blkid;      struct keywordTypes *kt = getKeywordByType(type, cfi);
463        if (kt)
464     return kt->key;
465        return "unknown";
466    }
467    
468    static char * getpathbyspec(char *device) {
469      if (!blkid)      if (!blkid)
470          blkid_get_cache(&blkid, NULL);          blkid_get_cache(&blkid, NULL);
471    
472      return blkid_get_devname(blkid, device, NULL);      return blkid_get_devname(blkid, device, NULL);
473  }  }
474    
475    static char * getuuidbydev(char *device) {
476        if (!blkid)
477     blkid_get_cache(&blkid, NULL);
478    
479        return blkid_get_tag_value(blkid, "UUID", device);
480    }
481    
482  static enum lineType_e getTypeByKeyword(char * keyword,  static enum lineType_e getTypeByKeyword(char * keyword,
483   struct configFileInfo * cfi) {   struct configFileInfo * cfi) {
484      struct keywordTypes * kw;      struct keywordTypes * kw;
# Line 477  static int isBracketedTitle(struct singl Line 514  static int isBracketedTitle(struct singl
514      return 0;      return 0;
515  }  }
516    
517  static int isEntrySeparator(struct singleLine * line,  static int isEntryStart(struct singleLine * line,
518                              struct configFileInfo * cfi) {                              struct configFileInfo * cfi) {
519      return line->type == cfi->entrySeparator || line->type == LT_OTHER ||      return line->type == cfi->entryStart || line->type == LT_OTHER ||
520   (cfi->titleBracketed && isBracketedTitle(line));   (cfi->titleBracketed && isBracketedTitle(line));
521  }  }
522    
# Line 802  static struct grubConfig * readConfig(co Line 839  static struct grubConfig * readConfig(co
839      cfg->secondaryIndent = strdup(line->indent);      cfg->secondaryIndent = strdup(line->indent);
840   }   }
841    
842   if (isEntrySeparator(line, cfi)) {   if (isEntryStart(line, cfi)) {
843      sawEntry = 1;      sawEntry = 1;
844      if (!entry) {      if (!entry) {
845   cfg->entries = malloc(sizeof(*entry));   cfg->entries = malloc(sizeof(*entry));
# Line 818  static struct grubConfig * readConfig(co Line 855  static struct grubConfig * readConfig(co
855      entry->next = NULL;      entry->next = NULL;
856   }   }
857    
858   if (line->type == LT_DEFAULT && line->numElements == 2) {   if (line->type == LT_SET_VARIABLE) {
859        int i;
860        dbgPrintf("found 'set' command (%d elements): ", line->numElements);
861        dbgPrintf("%s", line->indent);
862        for (i = 0; i < line->numElements; i++)
863     dbgPrintf("%s\"%s\"", line->elements[i].indent, line->elements[i].item);
864        dbgPrintf("\n");
865        struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);
866        if (kwType && line->numElements == 3 &&
867        !strcmp(line->elements[1].item, kwType->key)) {
868     dbgPrintf("Line sets default config\n");
869     cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
870     defaultLine = line;
871        }
872     } else if (line->type == LT_DEFAULT && line->numElements == 2) {
873      cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;      cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
874      defaultLine = line;      defaultLine = line;
875    
# Line 931  static struct grubConfig * readConfig(co Line 982  static struct grubConfig * readConfig(co
982   entry->lines = line;   entry->lines = line;
983      else      else
984   last->next = line;   last->next = line;
985      dbgPrintf("readConfig added %d to %p\n", line->type, entry);      dbgPrintf("readConfig added %s to %p\n", getKeyByType(line->type, cfi), entry);
986    
987        /* we could have seen this outside of an entry... if so, we
988         * ignore it like any other line we don't grok */
989        if (line->type == LT_ENTRY_END && sawEntry)
990     sawEntry = 0;
991   } else {   } else {
992      if (!cfg->theLines)      if (!cfg->theLines)
993   cfg->theLines = line;   cfg->theLines = line;
994      else      else
995   last->next = line;   last->next = line;
996      dbgPrintf("readConfig added %d to cfg\n", line->type);      dbgPrintf("readConfig added %s to cfg\n", getKeyByType(line->type, cfi));
997   }   }
998    
999   last = line;   last = line;
# Line 945  static struct grubConfig * readConfig(co Line 1001  static struct grubConfig * readConfig(co
1001    
1002      free(incoming);      free(incoming);
1003    
1004        dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset");
1005      if (defaultLine) {      if (defaultLine) {
1006   if (cfi->defaultSupportSaved &&   if (cfi->defaultIsVariable) {
1007        char *value = defaultLine->elements[2].item;
1008        while (*value && (*value == '"' || *value == '\'' ||
1009        *value == ' ' || *value == '\t'))
1010     value++;
1011        cfg->defaultImage = strtol(value, &end, 10);
1012        while (*end && (*end == '"' || *end == '\'' ||
1013        *end == ' ' || *end == '\t'))
1014     end++;
1015        if (*end) cfg->defaultImage = -1;
1016     } else if (cfi->defaultSupportSaved &&
1017   !strncmp(defaultLine->elements[1].item, "saved", 5)) {   !strncmp(defaultLine->elements[1].item, "saved", 5)) {
1018      cfg->defaultImage = DEFAULT_SAVED;      cfg->defaultImage = DEFAULT_SAVED;
1019   } else if (cfi->defaultIsIndex) {   } else if (cfi->defaultIsIndex) {
# Line 995  static void writeDefault(FILE * out, cha Line 1062  static void writeDefault(FILE * out, cha
1062   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
1063      else if (cfg->defaultImage > -1) {      else if (cfg->defaultImage > -1) {
1064   if (cfg->cfi->defaultIsIndex) {   if (cfg->cfi->defaultIsIndex) {
1065      fprintf(out, "%sdefault%s%d\n", indent, separator,      if (cfg->cfi->defaultIsVariable) {
1066      cfg->defaultImage);          fprintf(out, "%sset default=\"%d\"\n", indent,
1067     cfg->defaultImage);
1068        } else {
1069     fprintf(out, "%sdefault%s%d\n", indent, separator,
1070     cfg->defaultImage);
1071        }
1072   } else {   } else {
1073      int image = cfg->defaultImage;      int image = cfg->defaultImage;
1074    
# Line 1086  static int writeConfig(struct grubConfig Line 1158  static int writeConfig(struct grubConfig
1158      }      }
1159    
1160      line = cfg->theLines;      line = cfg->theLines;
1161        struct keywordTypes *defaultKw = getKeywordByType(LT_DEFAULT, cfg->cfi);
1162      while (line) {      while (line) {
1163   if (line->type == LT_DEFAULT) {          if (line->type == LT_SET_VARIABLE && defaultKw &&
1164     line->numElements == 3 &&
1165     !strcmp(line->elements[1].item, defaultKw->key)) {
1166        writeDefault(out, line->indent, line->elements[0].indent, cfg);
1167        needs &= ~MAIN_DEFAULT;
1168     } else if (line->type == LT_DEFAULT) {
1169      writeDefault(out, line->indent, line->elements[0].indent, cfg);      writeDefault(out, line->indent, line->elements[0].indent, cfg);
1170      needs &= ~MAIN_DEFAULT;      needs &= ~MAIN_DEFAULT;
1171   } else if (line->type == LT_FALLBACK) {   } else if (line->type == LT_FALLBACK) {
# Line 1155  static int numEntries(struct grubConfig Line 1233  static int numEntries(struct grubConfig
1233      return i;      return i;
1234  }  }
1235    
1236    static char *findDiskForRoot()
1237    {
1238        int fd;
1239        char buf[65536];
1240        char *devname;
1241        char *chptr;
1242        int rc;
1243    
1244        if ((fd = open(_PATH_MOUNTED, O_RDONLY)) < 0) {
1245            fprintf(stderr, "grubby: failed to open %s: %s\n",
1246                    _PATH_MOUNTED, strerror(errno));
1247            return NULL;
1248        }
1249    
1250        rc = read(fd, buf, sizeof(buf) - 1);
1251        if (rc <= 0) {
1252            fprintf(stderr, "grubby: failed to read %s: %s\n",
1253                    _PATH_MOUNTED, strerror(errno));
1254            close(fd);
1255            return NULL;
1256        }
1257        close(fd);
1258        buf[rc] = '\0';
1259        chptr = buf;
1260    
1261        while (chptr && chptr != buf+rc) {
1262            devname = chptr;
1263    
1264            /*
1265             * The first column of a mtab entry is the device, but if the entry is a
1266             * special device it won't start with /, so move on to the next line.
1267             */
1268            if (*devname != '/') {
1269                chptr = strchr(chptr, '\n');
1270                if (chptr)
1271                    chptr++;
1272                continue;
1273            }
1274    
1275            /* Seek to the next space */
1276            chptr = strchr(chptr, ' ');
1277            if (!chptr) {
1278                fprintf(stderr, "grubby: error parsing %s: %s\n",
1279                        _PATH_MOUNTED, strerror(errno));
1280                return NULL;
1281            }
1282    
1283            /*
1284             * The second column of a mtab entry is the mount point, we are looking
1285             * for '/' obviously.
1286             */
1287            if (*(++chptr) == '/' && *(++chptr) == ' ') {
1288                /*
1289                 * Move back 2, which is the first space after the device name, set
1290                 * it to \0 so strdup will just get the devicename.
1291                 */
1292                chptr -= 2;
1293                *chptr = '\0';
1294                return strdup(devname);
1295            }
1296    
1297            /* Next line */
1298            chptr = strchr(chptr, '\n');
1299            if (chptr)
1300                chptr++;
1301        }
1302    
1303        return NULL;
1304    }
1305    
1306  int suitableImage(struct singleEntry * entry, const char * bootPrefix,  int suitableImage(struct singleEntry * entry, const char * bootPrefix,
1307    int skipRemoved, int flags) {    int skipRemoved, int flags) {
1308      struct singleLine * line;      struct singleLine * line;
1309      char * fullName;      char * fullName;
1310      int i;      int i;
     struct stat sb, sb2;  
1311      char * dev;      char * dev;
1312      char * rootspec;      char * rootspec;
1313        char * rootdev;
1314    
1315      if (skipRemoved && entry->skip) return 0;      if (skipRemoved && entry->skip) return 0;
1316    
# Line 1212  int suitableImage(struct singleEntry * e Line 1360  int suitableImage(struct singleEntry * e
1360      if (!dev)      if (!dev)
1361          return 0;          return 0;
1362    
1363      i = stat(dev, &sb);      rootdev = findDiskForRoot();
1364      if (i)      if (!rootdev)
1365   return 0;   return 0;
1366    
1367      stat("/", &sb2);      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {
1368            free(rootdev);
1369            return 0;
1370        }
1371    
1372      if (sb.st_rdev != sb2.st_dev)      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {
1373     free(rootdev);
1374          return 0;          return 0;
1375        }
1376    
1377        free(rootdev);
1378    
1379      return 1;      return 1;
1380  }  }
# Line 1733  struct singleLine *  addLine(struct sing Line 1888  struct singleLine *  addLine(struct sing
1888   sprintf(tmpl.elements[0].item, "[%s]", val);   sprintf(tmpl.elements[0].item, "[%s]", val);
1889   tmpl.elements[0].indent = "";   tmpl.elements[0].indent = "";
1890   val = NULL;   val = NULL;
1891        } else if (type == LT_MENUENTRY) {
1892     char *lineend = "--class gnu-linux --class gnu --class os {";
1893     if (!val) {
1894        fprintf(stderr, "Line type LT_MENUENTRY requires a value\n");
1895        abort();
1896     }
1897     kw = getKeywordByType(type, cfi);
1898     if (!kw) {
1899        fprintf(stderr, "Looking up keyword for unknown type %d\n", type);
1900        abort();
1901     }
1902     tmpl.indent = "";
1903     tmpl.type = type;
1904     tmpl.numElements = 3;
1905     tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements);
1906     tmpl.elements[0].item = kw->key;
1907     tmpl.elements[0].indent = alloca(2);
1908     sprintf(tmpl.elements[0].indent, "%c", kw->nextChar);
1909     tmpl.elements[1].item = (char *)val;
1910     tmpl.elements[1].indent = alloca(2);
1911     sprintf(tmpl.elements[1].indent, "%c", kw->nextChar);
1912     tmpl.elements[2].item = alloca(strlen(lineend)+1);
1913     strcpy(tmpl.elements[2].item, lineend);
1914     tmpl.elements[2].indent = "";
1915      } else {      } else {
1916   kw = getKeywordByType(type, cfi);   kw = getKeywordByType(type, cfi);
1917   if (!kw) abort();   if (!kw) {
1918        fprintf(stderr, "Looking up keyword for unknown type %d\n", type);
1919        abort();
1920     }
1921   tmpl.type = type;   tmpl.type = type;
1922   tmpl.numElements = val ? 2 : 1;   tmpl.numElements = val ? 2 : 1;
1923   tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements);   tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements);
# Line 1759  struct singleLine *  addLine(struct sing Line 1941  struct singleLine *  addLine(struct sing
1941   if (!line->next && !prev) prev = line;   if (!line->next && !prev) prev = line;
1942      }      }
1943    
1944      if (prev == entry->lines)      struct singleLine *menuEntry;
1945   tmpl.indent = defaultIndent ?: "";      menuEntry = getLineByType(LT_MENUENTRY, entry->lines);
1946      else      if (tmpl.type == LT_ENTRY_END) {
1947   tmpl.indent = prev->indent;   if (menuEntry)
1948        tmpl.indent = menuEntry->indent;
1949     else
1950        tmpl.indent = defaultIndent ?: "";
1951        } else if (tmpl.type != LT_MENUENTRY) {
1952     if (menuEntry)
1953        tmpl.indent = "\t";
1954     else if (prev == entry->lines)
1955        tmpl.indent = defaultIndent ?: "";
1956     else
1957        tmpl.indent = prev->indent;
1958        }
1959    
1960      return addLineTmpl(entry, &tmpl, prev, val, cfi);      return addLineTmpl(entry, &tmpl, prev, val, cfi);
1961  }  }
# Line 1789  void removeLine(struct singleEntry * ent Line 1982  void removeLine(struct singleEntry * ent
1982      free(line);      free(line);
1983  }  }
1984    
1985    static int isquote(char q)
1986    {
1987        if (q == '\'' || q == '\"')
1988     return 1;
1989        return 0;
1990    }
1991    
1992    static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi)
1993    {
1994        struct singleLine newLine = {
1995     .indent = tmplLine->indent,
1996     .type = tmplLine->type,
1997     .next = tmplLine->next,
1998        };
1999        int firstQuotedItem = -1;
2000        int quoteLen = 0;
2001        int j;
2002        int element = 0;
2003        char *c;
2004    
2005        c = malloc(strlen(tmplLine->elements[0].item) + 1);
2006        strcpy(c, tmplLine->elements[0].item);
2007        insertElement(&newLine, c, element++, cfi);
2008        free(c);
2009        c = NULL;
2010    
2011        for (j = 1; j < tmplLine->numElements; j++) {
2012     if (firstQuotedItem == -1) {
2013        quoteLen += strlen(tmplLine->elements[j].item);
2014        
2015        if (isquote(tmplLine->elements[j].item[0])) {
2016     firstQuotedItem = j;
2017            quoteLen += strlen(tmplLine->elements[j].indent);
2018        } else {
2019     c = malloc(quoteLen + 1);
2020     strcpy(c, tmplLine->elements[j].item);
2021     insertElement(&newLine, c, element++, cfi);
2022     free(c);
2023     quoteLen = 0;
2024        }
2025     } else {
2026        int itemlen = strlen(tmplLine->elements[j].item);
2027        quoteLen += itemlen;
2028        quoteLen += strlen(tmplLine->elements[j].indent);
2029        
2030        if (isquote(tmplLine->elements[j].item[itemlen - 1])) {
2031     c = malloc(quoteLen + 1);
2032     c[0] = '\0';
2033     for (int i = firstQuotedItem; i < j+1; i++) {
2034        strcat(c, tmplLine->elements[i].item);
2035        strcat(c, tmplLine->elements[i].indent);
2036     }
2037     insertElement(&newLine, c, element++, cfi);
2038     free(c);
2039    
2040     firstQuotedItem = -1;
2041     quoteLen = 0;
2042        }
2043     }
2044        }
2045        while (tmplLine->numElements)
2046     removeElement(tmplLine, 0);
2047        if (tmplLine->elements)
2048     free(tmplLine->elements);
2049    
2050        tmplLine->numElements = newLine.numElements;
2051        tmplLine->elements = newLine.elements;
2052    }
2053    
2054  static void insertElement(struct singleLine * line,  static void insertElement(struct singleLine * line,
2055    const char * item, int insertHere,    const char * item, int insertHere,
2056    struct configFileInfo * cfi)    struct configFileInfo * cfi)
# Line 1895  int updateActualImage(struct grubConfig Line 2157  int updateActualImage(struct grubConfig
2157      const char ** arg;      const char ** arg;
2158      int useKernelArgs, useRoot;      int useKernelArgs, useRoot;
2159      int firstElement;      int firstElement;
2160      int *usedElements, *usedArgs;      int *usedElements;
2161      int doreplace;      int doreplace;
2162    
2163      if (!image) return 0;      if (!image) return 0;
# Line 1930  int updateActualImage(struct grubConfig Line 2192  int updateActualImage(struct grubConfig
2192      useRoot = (getKeywordByType(LT_ROOT, cfg->cfi)      useRoot = (getKeywordByType(LT_ROOT, cfg->cfi)
2193         && !multibootArgs);         && !multibootArgs);
2194    
     for (k = 0, arg = newArgs; *arg; arg++, k++) ;  
     usedArgs = calloc(k, sizeof(*usedArgs));  
   
2195      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
2196    
2197   if (multibootArgs && !entry->multiboot)   if (multibootArgs && !entry->multiboot)
# Line 2008  int updateActualImage(struct grubConfig Line 2267  int updateActualImage(struct grubConfig
2267          usedElements = calloc(line->numElements, sizeof(*usedElements));          usedElements = calloc(line->numElements, sizeof(*usedElements));
2268    
2269   for (k = 0, arg = newArgs; *arg; arg++, k++) {   for (k = 0, arg = newArgs; *arg; arg++, k++) {
             if (usedArgs[k]) continue;  
2270    
2271      doreplace = 1;      doreplace = 1;
2272      for (i = firstElement; i < line->numElements; i++) {      for (i = firstElement; i < line->numElements; i++) {
# Line 2023  int updateActualImage(struct grubConfig Line 2281  int updateActualImage(struct grubConfig
2281                      continue;                      continue;
2282   if (!argMatch(line->elements[i].item, *arg)) {   if (!argMatch(line->elements[i].item, *arg)) {
2283                      usedElements[i]=1;                      usedElements[i]=1;
                     usedArgs[k]=1;  
2284      break;      break;
2285                  }                  }
2286              }              }
# Line 2093  int updateActualImage(struct grubConfig Line 2350  int updateActualImage(struct grubConfig
2350   }   }
2351      }      }
2352    
     free(usedArgs);  
2353      free(newArgs);      free(newArgs);
2354      free(oldArgs);      free(oldArgs);
2355    
# Line 2119  int updateImage(struct grubConfig * cfg, Line 2375  int updateImage(struct grubConfig * cfg,
2375      return rc;      return rc;
2376  }  }
2377    
2378    int updateInitrd(struct grubConfig * cfg, const char * image,
2379                     const char * prefix, const char * initrd) {
2380        struct singleEntry * entry;
2381        struct singleLine * line, * kernelLine, *endLine = NULL;
2382        int index = 0;
2383    
2384        if (!image) return 0;
2385    
2386        for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
2387            kernelLine = getLineByType(LT_KERNEL, entry->lines);
2388            if (!kernelLine) continue;
2389    
2390            line = getLineByType(LT_INITRD, entry->lines);
2391            if (line)
2392                removeLine(entry, line);
2393            if (prefix) {
2394                int prefixLen = strlen(prefix);
2395                if (!strncmp(initrd, prefix, prefixLen))
2396                    initrd += prefixLen;
2397            }
2398     endLine = getLineByType(LT_ENTRY_END, entry->lines);
2399     if (endLine)
2400        removeLine(entry, endLine);
2401            line = addLine(entry, cfg->cfi, LT_INITRD, kernelLine->indent, initrd);
2402            if (!line)
2403        return 1;
2404     if (endLine) {
2405        line = addLine(entry, cfg->cfi, LT_ENTRY_END, "", NULL);
2406                if (!line)
2407     return 1;
2408     }
2409    
2410            break;
2411        }
2412    
2413        return 0;
2414    }
2415    
2416  int checkDeviceBootloader(const char * device, const unsigned char * boot) {  int checkDeviceBootloader(const char * device, const unsigned char * boot) {
2417      int fd;      int fd;
2418      unsigned char bootSect[512];      unsigned char bootSect[512];
# Line 2289  int checkForLilo(struct grubConfig * con Line 2583  int checkForLilo(struct grubConfig * con
2583      return checkDeviceBootloader(line->elements[1].item, boot);      return checkDeviceBootloader(line->elements[1].item, boot);
2584  }  }
2585    
2586    int checkForGrub2(struct grubConfig * config) {
2587        if (!access("/boot/grub2", R_OK))
2588     return 2;
2589    
2590        return 1;
2591    }
2592    
2593  int checkForGrub(struct grubConfig * config) {  int checkForGrub(struct grubConfig * config) {
2594      int fd;      int fd;
2595      unsigned char bootSect[512];      unsigned char bootSect[512];
# Line 2464  int addNewKernel(struct grubConfig * con Line 2765  int addNewKernel(struct grubConfig * con
2765      if (*chptr == '#') continue;      if (*chptr == '#') continue;
2766    
2767      if (tmplLine->type == LT_KERNEL &&      if (tmplLine->type == LT_KERNEL &&
2768   tmplLine->numElements >= 2) {      tmplLine->numElements >= 2) {
2769   if (!template->multiboot && (needs & NEED_MB)) {   if (!template->multiboot && (needs & NEED_MB)) {
2770      /* it's not a multiboot template and this is the kernel      /* it's not a multiboot template and this is the kernel
2771       * line.  Try to be intelligent about inserting the       * line.  Try to be intelligent about inserting the
# Line 2590  int addNewKernel(struct grubConfig * con Line 2891  int addNewKernel(struct grubConfig * con
2891      needs &= ~NEED_INITRD;      needs &= ~NEED_INITRD;
2892   }   }
2893    
2894        } else if (tmplLine->type == LT_MENUENTRY &&
2895           (needs & NEED_TITLE)) {
2896     requote(tmplLine, config->cfi);
2897     char *nkt = malloc(strlen(newKernelTitle)+3);
2898     strcpy(nkt, "'");
2899     strcat(nkt, newKernelTitle);
2900     strcat(nkt, "'");
2901     newLine = addLineTmpl(new, tmplLine, newLine, nkt, config->cfi);
2902     free(nkt);
2903     needs &= ~NEED_TITLE;
2904      } else if (tmplLine->type == LT_TITLE &&      } else if (tmplLine->type == LT_TITLE &&
2905         (needs & NEED_TITLE)) {         (needs & NEED_TITLE)) {
2906   if (tmplLine->numElements >= 2) {   if (tmplLine->numElements >= 2) {
# Line 2603  int addNewKernel(struct grubConfig * con Line 2914  int addNewKernel(struct grubConfig * con
2914        tmplLine->indent, newKernelTitle);        tmplLine->indent, newKernelTitle);
2915      needs &= ~NEED_TITLE;      needs &= ~NEED_TITLE;
2916   }   }
2917        } else if (tmplLine->type == LT_ECHO) {
2918        requote(tmplLine, config->cfi);
2919        if (tmplLine->numElements > 1 &&
2920        strstr(tmplLine->elements[1].item, "'Loading Linux ")) {
2921     char *prefix = "'Loading ";
2922     char *newTitle = malloc(strlen(prefix) +
2923     strlen(newKernelTitle) + 2);
2924    
2925     strcpy(newTitle, prefix);
2926     strcat(newTitle, newKernelTitle);
2927     strcat(newTitle, "'");
2928     newLine = addLine(new, config->cfi, LT_ECHO,
2929     tmplLine->indent, newTitle);
2930     free(newTitle);
2931        } else {
2932     /* pass through other lines from the template */
2933     newLine = addLineTmpl(new, tmplLine, newLine, NULL,
2934     config->cfi);
2935        }
2936      } else {      } else {
2937   /* pass through other lines from the template */   /* pass through other lines from the template */
2938   newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);   newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);
# Line 2614  int addNewKernel(struct grubConfig * con Line 2943  int addNewKernel(struct grubConfig * con
2943   /* don't have a template, so start the entry with the   /* don't have a template, so start the entry with the
2944   * appropriate starting line   * appropriate starting line
2945   */   */
2946   switch (config->cfi->entrySeparator) {   switch (config->cfi->entryStart) {
2947      case LT_KERNEL:      case LT_KERNEL:
2948   if (new->multiboot && config->cfi->mbHyperFirst) {   if (new->multiboot && config->cfi->mbHyperFirst) {
2949      /* fall through to LT_HYPER */      /* fall through to LT_HYPER */
# Line 2633  int addNewKernel(struct grubConfig * con Line 2962  int addNewKernel(struct grubConfig * con
2962   needs &= ~NEED_MB;   needs &= ~NEED_MB;
2963   break;   break;
2964    
2965        case LT_MENUENTRY: {
2966     char *nkt = malloc(strlen(newKernelTitle)+3);
2967     strcpy(nkt, "'");
2968     strcat(nkt, newKernelTitle);
2969     strcat(nkt, "'");
2970            newLine = addLine(new, config->cfi, LT_MENUENTRY,
2971      config->primaryIndent, nkt);
2972     free(nkt);
2973     needs &= ~NEED_TITLE;
2974     needs |= NEED_END;
2975     break;
2976        }
2977      case LT_TITLE:      case LT_TITLE:
2978   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)
2979   char * templabel;   char * templabel;
# Line 2666  int addNewKernel(struct grubConfig * con Line 3007  int addNewKernel(struct grubConfig * con
3007    
3008      /* add the remainder of the lines, i.e. those that either      /* add the remainder of the lines, i.e. those that either
3009       * 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,
3010       * all the lines following the entrySeparator.       * all the lines following the entryStart.
3011       */       */
3012      if (needs & NEED_TITLE) {      if (needs & NEED_TITLE) {
3013   newLine = addLine(new, config->cfi, LT_TITLE,   newLine = addLine(new, config->cfi, LT_TITLE,
# Line 2707  int addNewKernel(struct grubConfig * con Line 3048  int addNewKernel(struct grubConfig * con
3048   free(initrdVal);   free(initrdVal);
3049   needs &= ~NEED_INITRD;   needs &= ~NEED_INITRD;
3050      }      }
3051        if (needs & NEED_END) {
3052     newLine = addLine(new, config->cfi, LT_ENTRY_END,
3053     config->secondaryIndent, NULL);
3054     needs &= ~NEED_END;
3055        }
3056    
3057      if (needs) {      if (needs) {
3058   printf(_("grubby: needs=%d, aborting\n"), needs);   printf(_("grubby: needs=%d, aborting\n"), needs);
# Line 2736  static void traceback(int signum) Line 3082  static void traceback(int signum)
3082    
3083  int main(int argc, const char ** argv) {  int main(int argc, const char ** argv) {
3084      poptContext optCon;      poptContext optCon;
3085      char * grubConfig = NULL;      const char * grubConfig = NULL;
3086      char * outputFile = NULL;      char * outputFile = NULL;
3087      int arg = 0;      int arg = 0;
3088      int flags = 0;      int flags = 0;
3089      int badImageOkay = 0;      int badImageOkay = 0;
3090        int configureGrub2 = 0;
3091      int configureLilo = 0, configureELilo = 0, configureGrub = 0;      int configureLilo = 0, configureELilo = 0, configureGrub = 0;
3092      int configureYaboot = 0, configureSilo = 0, configureZipl = 0;      int configureYaboot = 0, configureSilo = 0, configureZipl = 0;
3093      int configureExtLinux = 0;      int configureExtLinux = 0;
# Line 2806  int main(int argc, const char ** argv) { Line 3153  int main(int argc, const char ** argv) {
3153      _("configure extlinux bootloader (from syslinux)") },      _("configure extlinux bootloader (from syslinux)") },
3154   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,
3155      _("configure grub bootloader") },      _("configure grub bootloader") },
3156     { "grub2", 0, POPT_ARG_NONE, &configureGrub2, 0,
3157        _("configure grub2 bootloader") },
3158   { "info", 0, POPT_ARG_STRING, &kernelInfo, 0,   { "info", 0, POPT_ARG_STRING, &kernelInfo, 0,
3159      _("display boot information for specified kernel"),      _("display boot information for specified kernel"),
3160      _("kernel-path") },      _("kernel-path") },
# Line 2885  int main(int argc, const char ** argv) { Line 3234  int main(int argc, const char ** argv) {
3234   return 1;   return 1;
3235      }      }
3236    
3237      if ((configureLilo + configureGrub + configureELilo +      if ((configureLilo + configureGrub2 + configureGrub + configureELilo +
3238   configureYaboot + configureSilo + configureZipl +   configureYaboot + configureSilo + configureZipl +
3239   configureExtLinux ) > 1) {   configureExtLinux ) > 1) {
3240   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 3243  int main(int argc, const char ** argv) {
3243   fprintf(stderr,   fprintf(stderr,
3244      _("grubby: cannot specify config file with --bootloader-probe\n"));      _("grubby: cannot specify config file with --bootloader-probe\n"));
3245   return 1;   return 1;
3246        } else if (configureGrub2) {
3247     cfi = &grub2ConfigType;
3248      } else if (configureLilo) {      } else if (configureLilo) {
3249   cfi = &liloConfigType;   cfi = &liloConfigType;
3250      } else if (configureGrub) {      } else if (configureGrub) {
# Line 2923  int main(int argc, const char ** argv) { Line 3274  int main(int argc, const char ** argv) {
3274        #elif __s390x__        #elif __s390x__
3275          cfi = &ziplConfigtype;          cfi = &ziplConfigtype;
3276        #else        #else
3277   cfi = &grubConfigType;          if (grub2FindConfig(&grub2ConfigType))
3278        cfi = &grub2ConfigType;
3279     else
3280        cfi = &grubConfigType;
3281        #endif        #endif
3282      }      }
3283    
3284      if (!grubConfig)      if (!grubConfig) {
3285   grubConfig = cfi->defaultConfig;   if (cfi->findConfig)
3286        grubConfig = cfi->findConfig(cfi);
3287     if (!grubConfig)
3288        grubConfig = cfi->defaultConfig;
3289        }
3290    
3291      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||
3292    newKernelPath || removeKernelPath || makeDefault ||    newKernelPath || removeKernelPath || makeDefault ||
# Line 2948  int main(int argc, const char ** argv) { Line 3306  int main(int argc, const char ** argv) {
3306      if (newKernelPath && !newKernelTitle) {      if (newKernelPath && !newKernelTitle) {
3307   fprintf(stderr, _("grubby: kernel title must be specified\n"));   fprintf(stderr, _("grubby: kernel title must be specified\n"));
3308   return 1;   return 1;
3309      } else if (!newKernelPath && (newKernelTitle  || newKernelInitrd ||      } else if (!newKernelPath && (newKernelTitle  || copyDefault ||
3310    newKernelInitrd || copyDefault     ||    (newKernelInitrd && !updateKernelPath)||
3311    makeDefault || extraInitrdCount > 0)) {    makeDefault || extraInitrdCount > 0)) {
3312   fprintf(stderr, _("grubby: kernel path expected\n"));   fprintf(stderr, _("grubby: kernel path expected\n"));
3313   return 1;   return 1;
# Line 3010  int main(int argc, const char ** argv) { Line 3368  int main(int argc, const char ** argv) {
3368      }      }
3369    
3370      if (bootloaderProbe) {      if (bootloaderProbe) {
3371   int lrc = 0, grc = 0, erc = 0;   int lrc = 0, grc = 0, gr2c = 0, erc = 0;
3372   struct grubConfig * lconfig, * gconfig;   struct grubConfig * lconfig, * gconfig;
3373    
3374     const char *grub2config = grub2FindConfig(&grub2ConfigType);
3375     if (grub2config) {
3376        gconfig = readConfig(grub2config, &grub2ConfigType);
3377        if (!gconfig)
3378     gr2c = 1;
3379        else
3380     gr2c = checkForGrub2(gconfig);
3381     }
3382    
3383   if (!access(grubConfigType.defaultConfig, F_OK)) {   if (!access(grubConfigType.defaultConfig, F_OK)) {
3384      gconfig = readConfig(grubConfigType.defaultConfig, &grubConfigType);      gconfig = readConfig(grubConfigType.defaultConfig, &grubConfigType);
3385      if (!gconfig)      if (!gconfig)
# Line 3037  int main(int argc, const char ** argv) { Line 3404  int main(int argc, const char ** argv) {
3404   erc = checkForExtLinux(lconfig);   erc = checkForExtLinux(lconfig);
3405   }   }
3406    
3407   if (lrc == 1 || grc == 1) return 1;   if (lrc == 1 || grc == 1 || gr2c == 1) return 1;
3408    
3409   if (lrc == 2) printf("lilo\n");   if (lrc == 2) printf("lilo\n");
3410     if (gr2c == 2) printf("grub2\n");
3411   if (grc == 2) printf("grub\n");   if (grc == 2) printf("grub\n");
3412   if (erc == 2) printf("extlinux\n");   if (erc == 2) printf("extlinux\n");
3413    
# Line 3082  int main(int argc, const char ** argv) { Line 3450  int main(int argc, const char ** argv) {
3450      setFallbackImage(config, newKernelPath != NULL);      setFallbackImage(config, newKernelPath != NULL);
3451      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,
3452                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;
3453        if (updateKernelPath && newKernelInitrd) {
3454                if (updateInitrd(config, updateKernelPath, bootPrefix,
3455                                 newKernelInitrd)) return 1;
3456        }
3457      if (addNewKernel(config, template, bootPrefix, newKernelPath,      if (addNewKernel(config, template, bootPrefix, newKernelPath,
3458                       newKernelTitle, newKernelArgs, newKernelInitrd,                       newKernelTitle, newKernelArgs, newKernelInitrd,
3459                       extraInitrds, extraInitrdCount,                       extraInitrds, extraInitrdCount,
# Line 3095  int main(int argc, const char ** argv) { Line 3467  int main(int argc, const char ** argv) {
3467      }      }
3468    
3469      if (!outputFile)      if (!outputFile)
3470   outputFile = grubConfig;   outputFile = (char *)grubConfig;
3471    
3472      return writeConfig(config, outputFile, bootPrefix);      return writeConfig(config, outputFile, bootPrefix);
3473  }  }

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