Magellan Linux

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

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

revision 1844 by niro, Mon Jul 2 12:59:07 2012 UTC revision 1868 by niro, Mon Jul 2 13:22:30 2012 UTC
# Line 164  struct keywordTypes grubKeywords[] = { Line 164  struct keywordTypes grubKeywords[] = {
164    
165  const char *grubFindConfig(struct configFileInfo *cfi) {  const char *grubFindConfig(struct configFileInfo *cfi) {
166      static const char *configFiles[] = {      static const char *configFiles[] = {
  "/etc/grub.conf",  
167   "/boot/grub/grub.conf",   "/boot/grub/grub.conf",
168   "/boot/grub/menu.lst",   "/boot/grub/menu.lst",
169     "/etc/grub.conf",
170   NULL   NULL
171      };      };
172      static int i = -1;      static int i = -1;
# Line 243  const char *grub2FindConfig(struct confi Line 243  const char *grub2FindConfig(struct confi
243  }  }
244    
245  int sizeOfSingleLine(struct singleLine * line) {  int sizeOfSingleLine(struct singleLine * line) {
   int i;  
246    int count = 0;    int count = 0;
247    
248    for (i = 0; i < line->numElements; i++) {    for (int i = 0; i < line->numElements; i++) {
249      int indentSize = 0;      int indentSize = 0;
250    
251      count = count + strlen(line->elements[i].item);      count = count + strlen(line->elements[i].item);
# Line 573  static char * sdupprintf(const char *for Line 572  static char * sdupprintf(const char *for
572    
573  static struct keywordTypes * getKeywordByType(enum lineType_e type,  static struct keywordTypes * getKeywordByType(enum lineType_e type,
574        struct configFileInfo * cfi) {        struct configFileInfo * cfi) {
575      struct keywordTypes * kw;      for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {
     for (kw = cfi->keywords; kw->key; kw++) {  
576   if (kw->type == type)   if (kw->type == type)
577      return kw;      return kw;
578      }      }
# Line 604  static char * getuuidbydev(char *device) Line 602  static char * getuuidbydev(char *device)
602    
603  static enum lineType_e getTypeByKeyword(char * keyword,  static enum lineType_e getTypeByKeyword(char * keyword,
604   struct configFileInfo * cfi) {   struct configFileInfo * cfi) {
605      struct keywordTypes * kw;      for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {
     for (kw = cfi->keywords; kw->key; kw++) {  
606   if (!strcmp(keyword, kw->key))   if (!strcmp(keyword, kw->key))
607      return kw->type;      return kw->type;
608      }      }
# Line 692  static void lineInit(struct singleLine * Line 689  static void lineInit(struct singleLine *
689  }  }
690    
691  struct singleLine * lineDup(struct singleLine * line) {  struct singleLine * lineDup(struct singleLine * line) {
     int i;  
692      struct singleLine * newLine = malloc(sizeof(*newLine));      struct singleLine * newLine = malloc(sizeof(*newLine));
693    
694      newLine->indent = strdup(line->indent);      newLine->indent = strdup(line->indent);
# Line 702  struct singleLine * lineDup(struct singl Line 698  struct singleLine * lineDup(struct singl
698      newLine->elements = malloc(sizeof(*newLine->elements) *      newLine->elements = malloc(sizeof(*newLine->elements) *
699         newLine->numElements);         newLine->numElements);
700    
701      for (i = 0; i < newLine->numElements; i++) {      for (int i = 0; i < newLine->numElements; i++) {
702   newLine->elements[i].indent = strdup(line->elements[i].indent);   newLine->elements[i].indent = strdup(line->elements[i].indent);
703   newLine->elements[i].item = strdup(line->elements[i].item);   newLine->elements[i].item = strdup(line->elements[i].item);
704      }      }
# Line 711  struct singleLine * lineDup(struct singl Line 707  struct singleLine * lineDup(struct singl
707  }  }
708    
709  static void lineFree(struct singleLine * line) {  static void lineFree(struct singleLine * line) {
     int i;  
   
710      if (line->indent) free(line->indent);      if (line->indent) free(line->indent);
711    
712      for (i = 0; i < line->numElements; i++) {      for (int i = 0; i < line->numElements; i++) {
713   free(line->elements[i].item);   free(line->elements[i].item);
714   free(line->elements[i].indent);   free(line->elements[i].indent);
715      }      }
# Line 726  static void lineFree(struct singleLine * Line 720  static void lineFree(struct singleLine *
720    
721  static int lineWrite(FILE * out, struct singleLine * line,  static int lineWrite(FILE * out, struct singleLine * line,
722       struct configFileInfo * cfi) {       struct configFileInfo * cfi) {
     int i;  
   
723      if (fprintf(out, "%s", line->indent) == -1) return -1;      if (fprintf(out, "%s", line->indent) == -1) return -1;
724    
725      for (i = 0; i < line->numElements; i++) {      for (int i = 0; i < line->numElements; i++) {
726   /* Need to handle this, because we strip the quotes from   /* Need to handle this, because we strip the quotes from
727   * menuentry when read it. */   * menuentry when read it. */
728   if (line->type == LT_MENUENTRY && i == 1) {   if (line->type == LT_MENUENTRY && i == 1) {
# Line 836  static int getNextLine(char ** bufPtr, s Line 828  static int getNextLine(char ** bufPtr, s
828      if (*line->elements[0].item == '#') {      if (*line->elements[0].item == '#') {
829   char * fullLine;   char * fullLine;
830   int len;   int len;
  int i;  
831    
832   len = strlen(line->indent);   len = strlen(line->indent);
833   for (i = 0; i < line->numElements; i++)   for (int i = 0; i < line->numElements; i++)
834      len += strlen(line->elements[i].item) +      len += strlen(line->elements[i].item) +
835     strlen(line->elements[i].indent);     strlen(line->elements[i].indent);
836    
# Line 848  static int getNextLine(char ** bufPtr, s Line 839  static int getNextLine(char ** bufPtr, s
839   free(line->indent);   free(line->indent);
840   line->indent = fullLine;   line->indent = fullLine;
841    
842   for (i = 0; i < line->numElements; i++) {   for (int i = 0; i < line->numElements; i++) {
843      strcat(fullLine, line->elements[i].item);      strcat(fullLine, line->elements[i].item);
844      strcat(fullLine, line->elements[i].indent);      strcat(fullLine, line->elements[i].indent);
845      free(line->elements[i].item);      free(line->elements[i].item);
# Line 867  static int getNextLine(char ** bufPtr, s Line 858  static int getNextLine(char ** bufPtr, s
858   * elements up more   * elements up more
859   */   */
860   if (!isspace(kw->separatorChar)) {   if (!isspace(kw->separatorChar)) {
     int i;  
861      char indent[2] = "";      char indent[2] = "";
862      indent[0] = kw->separatorChar;      indent[0] = kw->separatorChar;
863      for (i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
864   char *p;   char *p;
  int j;  
865   int numNewElements;   int numNewElements;
866    
867   numNewElements = 0;   numNewElements = 0;
# Line 888  static int getNextLine(char ** bufPtr, s Line 877  static int getNextLine(char ** bufPtr, s
877      sizeof(*line->elements) * elementsAlloced);      sizeof(*line->elements) * elementsAlloced);
878   }   }
879    
880   for (j = line->numElements; j > i; j--) {   for (int j = line->numElements; j > i; j--) {
881   line->elements[j + numNewElements] = line->elements[j];   line->elements[j + numNewElements] = line->elements[j];
882   }   }
883   line->numElements += numNewElements;   line->numElements += numNewElements;
# Line 926  static struct grubConfig * readConfig(co Line 915  static struct grubConfig * readConfig(co
915      struct singleLine * last = NULL, * line, * defaultLine = NULL;      struct singleLine * last = NULL, * line, * defaultLine = NULL;
916      char * end;      char * end;
917      struct singleEntry * entry = NULL;      struct singleEntry * entry = NULL;
918      int i, len;      int len;
919      char * buf;      char * buf;
920    
921      if (!strcmp(inName, "-")) {      if (!strcmp(inName, "-")) {
# Line 989  static struct grubConfig * readConfig(co Line 978  static struct grubConfig * readConfig(co
978   }   }
979    
980   if (line->type == LT_SET_VARIABLE) {   if (line->type == LT_SET_VARIABLE) {
     int i;  
981      dbgPrintf("found 'set' command (%d elements): ", line->numElements);      dbgPrintf("found 'set' command (%d elements): ", line->numElements);
982      dbgPrintf("%s", line->indent);      dbgPrintf("%s", line->indent);
983      for (i = 0; i < line->numElements; i++)      for (int i = 0; i < line->numElements; i++)
984   dbgPrintf("\"%s\"%s", line->elements[i].item, line->elements[i].indent);   dbgPrintf("\"%s\"%s", line->elements[i].item, line->elements[i].indent);
985      dbgPrintf("\n");      dbgPrintf("\n");
986      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);
# Line 1020  static struct grubConfig * readConfig(co Line 1008  static struct grubConfig * readConfig(co
1008       * This only applies to grub, but that's the only place we       * This only applies to grub, but that's the only place we
1009       * should find LT_MBMODULE lines anyway.       * should find LT_MBMODULE lines anyway.
1010       */       */
1011      struct singleLine * l;      for (struct singleLine *l = entry->lines; l; l = l->next) {
     for (l = entry->lines; l; l = l->next) {  
1012   if (l->type == LT_HYPER)   if (l->type == LT_HYPER)
1013      break;      break;
1014   else if (l->type == LT_KERNEL) {   else if (l->type == LT_KERNEL) {
# Line 1041  static struct grubConfig * readConfig(co Line 1028  static struct grubConfig * readConfig(co
1028   } else if (line->type == LT_TITLE && line->numElements > 1) {   } else if (line->type == LT_TITLE && line->numElements > 1) {
1029      /* make the title a single argument (undoing our parsing) */      /* make the title a single argument (undoing our parsing) */
1030      len = 0;      len = 0;
1031      for (i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
1032   len += strlen(line->elements[i].item);   len += strlen(line->elements[i].item);
1033   len += strlen(line->elements[i].indent);   len += strlen(line->elements[i].indent);
1034      }      }
1035      buf = malloc(len + 1);      buf = malloc(len + 1);
1036      *buf = '\0';      *buf = '\0';
1037    
1038      for (i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
1039   strcat(buf, line->elements[i].item);   strcat(buf, line->elements[i].item);
1040   free(line->elements[i].item);   free(line->elements[i].item);
1041    
# Line 1068  static struct grubConfig * readConfig(co Line 1055  static struct grubConfig * readConfig(co
1055      char *extras;      char *extras;
1056      char *title;      char *title;
1057    
1058      for (i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
1059   len += strlen(line->elements[i].item);   len += strlen(line->elements[i].item);
1060   len += strlen(line->elements[i].indent);   len += strlen(line->elements[i].indent);
1061      }      }
# Line 1080  static struct grubConfig * readConfig(co Line 1067  static struct grubConfig * readConfig(co
1067      *extras = '\0';      *extras = '\0';
1068    
1069      /* get title. */      /* get title. */
1070      for (i = 0; i < line->numElements; i++) {      for (int i = 0; i < line->numElements; i++) {
1071   if (!strcmp(line->elements[i].item, "menuentry"))   if (!strcmp(line->elements[i].item, "menuentry"))
1072      continue;      continue;
1073   if (isquote(*line->elements[i].item))   if (isquote(*line->elements[i].item))
# Line 1100  static struct grubConfig * readConfig(co Line 1087  static struct grubConfig * readConfig(co
1087    
1088      /* get extras */      /* get extras */
1089      int count = 0;      int count = 0;
1090      for (i = 0; i < line->numElements; i++) {      for (int i = 0; i < line->numElements; i++) {
1091   if (count >= 2) {   if (count >= 2) {
1092      strcat(extras, line->elements[i].item);      strcat(extras, line->elements[i].item);
1093      strcat(extras, line->elements[i].indent);      strcat(extras, line->elements[i].indent);
# Line 1221  static struct grubConfig * readConfig(co Line 1208  static struct grubConfig * readConfig(co
1208      cfg->defaultImage = strtol(defaultLine->elements[1].item, &end, 10);      cfg->defaultImage = strtol(defaultLine->elements[1].item, &end, 10);
1209      if (*end) cfg->defaultImage = -1;      if (*end) cfg->defaultImage = -1;
1210   } else if (defaultLine->numElements >= 2) {   } else if (defaultLine->numElements >= 2) {
1211      i = 0;      int i = 0;
1212      while ((entry = findEntryByIndex(cfg, i))) {      while ((entry = findEntryByIndex(cfg, i))) {
1213   for (line = entry->lines; line; line = line->next)   for (line = entry->lines; line; line = line->next)
1214      if (line->type == LT_TITLE) break;      if (line->type == LT_TITLE) break;
# Line 1323  static int writeConfig(struct grubConfig Line 1310  static int writeConfig(struct grubConfig
1310    
1311      /* most likely the symlink is relative, so change our      /* most likely the symlink is relative, so change our
1312         directory to the dir of the symlink */         directory to the dir of the symlink */
1313              rc = chdir(dirname(strdupa(outName)));      char *dir = strdupa(outName);
1314                rc = chdir(dirname(dir));
1315        free(dir);
1316      do {      do {
1317   buf = alloca(len + 1);   buf = alloca(len + 1);
1318   rc = readlink(basename(outName), buf, len);   rc = readlink(basename(outName), buf, len);
# Line 1880  void markRemovedImage(struct grubConfig Line 1869  void markRemovedImage(struct grubConfig
1869    
1870  void setDefaultImage(struct grubConfig * config, int hasNew,  void setDefaultImage(struct grubConfig * config, int hasNew,
1871       const char * defaultKernelPath, int newIsDefault,       const char * defaultKernelPath, int newIsDefault,
1872       const char * prefix, int flags) {       const char * prefix, int flags, int index) {
1873      struct singleEntry * entry, * entry2, * newDefault;      struct singleEntry * entry, * entry2, * newDefault;
1874      int i, j;      int i, j;
1875    
1876      if (newIsDefault) {      if (newIsDefault) {
1877   config->defaultImage = 0;   config->defaultImage = 0;
1878   return;   return;
1879        } else if ((index >= 0) && config->cfi->defaultIsIndex) {
1880     if (findEntryByIndex(config, index))
1881        config->defaultImage = index;
1882     else
1883        config->defaultImage = -1;
1884     return;
1885      } else if (defaultKernelPath) {      } else if (defaultKernelPath) {
1886   i = 0;   i = 0;
1887   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {
# Line 1967  void displayEntry(struct singleEntry * e Line 1962  void displayEntry(struct singleEntry * e
1962          return;          return;
1963      }      }
1964    
1965      printf("kernel=%s%s\n", prefix, line->elements[1].item);      if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
1966     printf("kernel=%s\n", line->elements[1].item);
1967        else
1968     printf("kernel=%s%s\n", prefix, line->elements[1].item);
1969    
1970      if (line->numElements >= 3) {      if (line->numElements >= 3) {
1971   printf("args=\"");   printf("args=\"");
# Line 2026  void displayEntry(struct singleEntry * e Line 2024  void displayEntry(struct singleEntry * e
2024      line = getLineByType(LT_INITRD, entry->lines);      line = getLineByType(LT_INITRD, entry->lines);
2025    
2026      if (line && line->numElements >= 2) {      if (line && line->numElements >= 2) {
2027   printf("initrd=%s", prefix);   if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
2028        printf("initrd=");
2029     else
2030        printf("initrd=%s", prefix);
2031    
2032   for (i = 1; i < line->numElements; i++)   for (i = 1; i < line->numElements; i++)
2033      printf("%s%s", line->elements[i].item, line->elements[i].indent);      printf("%s%s", line->elements[i].item, line->elements[i].indent);
2034   printf("\n");   printf("\n");
# Line 2044  void displayEntry(struct singleEntry * e Line 2046  void displayEntry(struct singleEntry * e
2046      }      }
2047  }  }
2048    
2049    int isSuseSystem(void) {
2050        const char * path;
2051        const static char default_path[] = "/etc/SuSE-release";
2052    
2053        if ((path = getenv("GRUBBY_SUSE_RELEASE")) == NULL)
2054     path = default_path;
2055    
2056        if (!access(path, R_OK))
2057     return 1;
2058        return 0;
2059    }
2060    
2061    int isSuseGrubConf(const char * path) {
2062        FILE * grubConf;
2063        char * line = NULL;
2064        size_t len = 0, res = 0;
2065    
2066        grubConf = fopen(path, "r");
2067        if (!grubConf) {
2068            dbgPrintf("Could not open SuSE configuration file '%s'\n", path);
2069     return 0;
2070        }
2071    
2072        while ((res = getline(&line, &len, grubConf)) != -1) {
2073     if (!strncmp(line, "setup", 5)) {
2074        fclose(grubConf);
2075        free(line);
2076        return 1;
2077     }
2078        }
2079    
2080        dbgPrintf("SuSE configuration file '%s' does not appear to be valid\n",
2081          path);
2082    
2083        fclose(grubConf);
2084        free(line);
2085        return 0;
2086    }
2087    
2088    int suseGrubConfGetLba(const char * path, int * lbaPtr) {
2089        FILE * grubConf;
2090        char * line = NULL;
2091        size_t res = 0, len = 0;
2092    
2093        if (!path) return 1;
2094        if (!lbaPtr) return 1;
2095    
2096        grubConf = fopen(path, "r");
2097        if (!grubConf) return 1;
2098    
2099        while ((res = getline(&line, &len, grubConf)) != -1) {
2100     if (line[res - 1] == '\n')
2101        line[res - 1] = '\0';
2102     else if (len > res)
2103        line[res] = '\0';
2104     else {
2105        line = realloc(line, res + 1);
2106        line[res] = '\0';
2107     }
2108    
2109     if (!strncmp(line, "setup", 5)) {
2110        if (strstr(line, "--force-lba")) {
2111            *lbaPtr = 1;
2112        } else {
2113            *lbaPtr = 0;
2114        }
2115        dbgPrintf("lba: %i\n", *lbaPtr);
2116        break;
2117     }
2118        }
2119    
2120        free(line);
2121        fclose(grubConf);
2122        return 0;
2123    }
2124    
2125    int suseGrubConfGetInstallDevice(const char * path, char ** devicePtr) {
2126        FILE * grubConf;
2127        char * line = NULL;
2128        size_t res = 0, len = 0;
2129        char * lastParamPtr = NULL;
2130        char * secLastParamPtr = NULL;
2131        char installDeviceNumber = '\0';
2132        char * bounds = NULL;
2133    
2134        if (!path) return 1;
2135        if (!devicePtr) return 1;
2136    
2137        grubConf = fopen(path, "r");
2138        if (!grubConf) return 1;
2139    
2140        while ((res = getline(&line, &len, grubConf)) != -1) {
2141     if (strncmp(line, "setup", 5))
2142        continue;
2143    
2144     if (line[res - 1] == '\n')
2145        line[res - 1] = '\0';
2146     else if (len > res)
2147        line[res] = '\0';
2148     else {
2149        line = realloc(line, res + 1);
2150        line[res] = '\0';
2151     }
2152    
2153     lastParamPtr = bounds = line + res;
2154    
2155     /* Last parameter in grub may be an optional IMAGE_DEVICE */
2156     while (!isspace(*lastParamPtr))
2157        lastParamPtr--;
2158     lastParamPtr++;
2159    
2160     secLastParamPtr = lastParamPtr - 2;
2161     dbgPrintf("lastParamPtr: %s\n", lastParamPtr);
2162    
2163     if (lastParamPtr + 3 > bounds) {
2164        dbgPrintf("lastParamPtr going over boundary");
2165        fclose(grubConf);
2166        free(line);
2167        return 1;
2168     }
2169     if (!strncmp(lastParamPtr, "(hd", 3))
2170        lastParamPtr += 3;
2171     dbgPrintf("lastParamPtr: %c\n", *lastParamPtr);
2172    
2173     /*
2174     * Second last parameter will decide wether last parameter is
2175     * an IMAGE_DEVICE or INSTALL_DEVICE
2176     */
2177     while (!isspace(*secLastParamPtr))
2178        secLastParamPtr--;
2179     secLastParamPtr++;
2180    
2181     if (secLastParamPtr + 3 > bounds) {
2182        dbgPrintf("secLastParamPtr going over boundary");
2183        fclose(grubConf);
2184        free(line);
2185        return 1;
2186     }
2187     dbgPrintf("secLastParamPtr: %s\n", secLastParamPtr);
2188     if (!strncmp(secLastParamPtr, "(hd", 3)) {
2189        secLastParamPtr += 3;
2190        dbgPrintf("secLastParamPtr: %c\n", *secLastParamPtr);
2191        installDeviceNumber = *secLastParamPtr;
2192     } else {
2193        installDeviceNumber = *lastParamPtr;
2194     }
2195    
2196     *devicePtr = malloc(6);
2197     snprintf(*devicePtr, 6, "(hd%c)", installDeviceNumber);
2198     dbgPrintf("installDeviceNumber: %c\n", installDeviceNumber);
2199     fclose(grubConf);
2200     free(line);
2201     return 0;
2202        }
2203    
2204        free(line);
2205        fclose(grubConf);
2206        return 1;
2207    }
2208    
2209    int grubGetBootFromDeviceMap(const char * device,
2210         char ** bootPtr) {
2211        FILE * deviceMap;
2212        char * line = NULL;
2213        size_t res = 0, len = 0;
2214        char * devicePtr;
2215        char * bounds = NULL;
2216        const char * path;
2217        const static char default_path[] = "/boot/grub/device.map";
2218    
2219        if (!device) return 1;
2220        if (!bootPtr) return 1;
2221    
2222        if ((path = getenv("GRUBBY_GRUB_DEVICE_MAP")) == NULL)
2223     path = default_path;
2224    
2225        dbgPrintf("opening grub device.map file from: %s\n", path);
2226        deviceMap = fopen(path, "r");
2227        if (!deviceMap)
2228     return 1;
2229    
2230        while ((res = getline(&line, &len, deviceMap)) != -1) {
2231            if (!strncmp(line, "#", 1))
2232        continue;
2233    
2234     if (line[res - 1] == '\n')
2235        line[res - 1] = '\0';
2236     else if (len > res)
2237        line[res] = '\0';
2238     else {
2239        line = realloc(line, res + 1);
2240        line[res] = '\0';
2241     }
2242    
2243     devicePtr = line;
2244     bounds = line + res;
2245    
2246     while ((isspace(*line) && ((devicePtr + 1) <= bounds)))
2247        devicePtr++;
2248     dbgPrintf("device: %s\n", devicePtr);
2249    
2250     if (!strncmp(devicePtr, device, strlen(device))) {
2251        devicePtr += strlen(device);
2252        while (isspace(*devicePtr) && ((devicePtr + 1) <= bounds))
2253            devicePtr++;
2254    
2255        *bootPtr = strdup(devicePtr);
2256        break;
2257     }
2258        }
2259    
2260        free(line);
2261        fclose(deviceMap);
2262        return 0;
2263    }
2264    
2265    int suseGrubConfGetBoot(const char * path, char ** bootPtr) {
2266        char * grubDevice;
2267    
2268        if (suseGrubConfGetInstallDevice(path, &grubDevice))
2269     dbgPrintf("error looking for grub installation device\n");
2270        else
2271     dbgPrintf("grubby installation device: %s\n", grubDevice);
2272    
2273        if (grubGetBootFromDeviceMap(grubDevice, bootPtr))
2274     dbgPrintf("error looking for grub boot device\n");
2275        else
2276     dbgPrintf("grubby boot device: %s\n", *bootPtr);
2277    
2278        free(grubDevice);
2279        return 0;
2280    }
2281    
2282    int parseSuseGrubConf(int * lbaPtr, char ** bootPtr) {
2283        /*
2284         * This SuSE grub configuration file at this location is not your average
2285         * grub configuration file, but instead the grub commands used to setup
2286         * grub on that system.
2287         */
2288        const char * path;
2289        const static char default_path[] = "/etc/grub.conf";
2290    
2291        if ((path = getenv("GRUBBY_SUSE_GRUB_CONF")) == NULL)
2292     path = default_path;
2293    
2294        if (!isSuseGrubConf(path)) return 1;
2295    
2296        if (lbaPtr) {
2297            *lbaPtr = 0;
2298            if (suseGrubConfGetLba(path, lbaPtr))
2299                return 1;
2300        }
2301    
2302        if (bootPtr) {
2303            *bootPtr = NULL;
2304            suseGrubConfGetBoot(path, bootPtr);
2305        }
2306    
2307        return 0;
2308    }
2309    
2310  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {
2311      FILE * in;      FILE * in;
2312      char buf[1024];      char buf[1024];
# Line 2092  int parseSysconfigGrub(int * lbaPtr, cha Line 2355  int parseSysconfigGrub(int * lbaPtr, cha
2355  }  }
2356    
2357  void dumpSysconfigGrub(void) {  void dumpSysconfigGrub(void) {
2358      char * boot;      char * boot = NULL;
2359      int lba;      int lba;
2360    
2361      if (!parseSysconfigGrub(&lba, &boot)) {      if (isSuseSystem()) {
2362   if (lba) printf("lba\n");          if (parseSuseGrubConf(&lba, &boot)) {
2363   if (boot) printf("boot=%s\n", boot);      free(boot);
2364        return;
2365     }
2366        } else {
2367            if (parseSysconfigGrub(&lba, &boot)) {
2368        free(boot);
2369        return;
2370     }
2371        }
2372    
2373        if (lba) printf("lba\n");
2374        if (boot) {
2375     printf("boot=%s\n", boot);
2376     free(boot);
2377      }      }
2378  }  }
2379    
# Line 2911  int checkForGrub(struct grubConfig * con Line 3187  int checkForGrub(struct grubConfig * con
3187      int fd;      int fd;
3188      unsigned char bootSect[512];      unsigned char bootSect[512];
3189      char * boot;      char * boot;
3190        int onSuse = isSuseSystem();
3191    
3192      if (parseSysconfigGrub(NULL, &boot))  
3193   return 0;      if (onSuse) {
3194     if (parseSuseGrubConf(NULL, &boot))
3195        return 0;
3196        } else {
3197     if (parseSysconfigGrub(NULL, &boot))
3198        return 0;
3199        }
3200    
3201      /* assume grub is not installed -- not an error condition */      /* assume grub is not installed -- not an error condition */
3202      if (!boot)      if (!boot)
# Line 2932  int checkForGrub(struct grubConfig * con Line 3215  int checkForGrub(struct grubConfig * con
3215      }      }
3216      close(fd);      close(fd);
3217    
3218        /* The more elaborate checks do not work on SuSE. The checks done
3219         * seem to be reasonble (at least for now), so just return success
3220         */
3221        if (onSuse)
3222     return 2;
3223    
3224      return checkDeviceBootloader(boot, bootSect);      return checkDeviceBootloader(boot, bootSect);
3225  }  }
3226    
# Line 2965  int checkForExtLinux(struct grubConfig * Line 3254  int checkForExtLinux(struct grubConfig *
3254      return checkDeviceBootloader(boot, bootSect);      return checkDeviceBootloader(boot, bootSect);
3255  }  }
3256    
3257    int checkForYaboot(struct grubConfig * config) {
3258        /*
3259         * This is a simplistic check that we consider good enough for own puporses
3260         *
3261         * If we were to properly check if yaboot is *installed* we'd need to:
3262         * 1) get the system boot device (LT_BOOT)
3263         * 2) considering it's a raw filesystem, check if the yaboot binary matches
3264         *    the content on the boot device
3265         * 3) if not, copy the binary to a temporary file and run "addnote" on it
3266         * 4) check again if binary and boot device contents match
3267         */
3268        if (!access("/etc/yaboot.conf", R_OK))
3269     return 2;
3270    
3271        return 1;
3272    }
3273    
3274    int checkForElilo(struct grubConfig * config) {
3275        if (!access("/etc/elilo.conf", R_OK))
3276     return 2;
3277    
3278        return 1;
3279    }
3280    
3281  static char * getRootSpecifier(char * str) {  static char * getRootSpecifier(char * str) {
3282      char * idx, * rootspec = NULL;      char * idx, * rootspec = NULL;
3283    
# Line 3435  int main(int argc, const char ** argv) { Line 3748  int main(int argc, const char ** argv) {
3748      int displayDefault = 0;      int displayDefault = 0;
3749      int displayDefaultIndex = 0;      int displayDefaultIndex = 0;
3750      int displayDefaultTitle = 0;      int displayDefaultTitle = 0;
3751        int defaultIndex = -1;
3752      struct poptOption options[] = {      struct poptOption options[] = {
3753   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,
3754      _("add an entry for the specified kernel"), _("kernel-path") },      _("add an entry for the specified kernel"), _("kernel-path") },
# Line 3452  int main(int argc, const char ** argv) { Line 3766  int main(int argc, const char ** argv) {
3766   { "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,   { "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,
3767      _("filestystem which contains /boot directory (for testing only)"),      _("filestystem which contains /boot directory (for testing only)"),
3768      _("bootfs") },      _("bootfs") },
3769  #if defined(__i386__) || defined(__x86_64__)  #if defined(__i386__) || defined(__x86_64__) || defined (__powerpc64__) || defined (__ia64__)
3770   { "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,   { "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,
3771      _("check if lilo is installed on lilo.conf boot sector") },      _("check which bootloader is installed on boot sector") },
3772  #endif  #endif
3773   { "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0,   { "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0,
3774      _("path to grub config file to update (\"-\" for stdin)"),      _("path to grub config file to update (\"-\" for stdin)"),
# Line 3507  int main(int argc, const char ** argv) { Line 3821  int main(int argc, const char ** argv) {
3821   { "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0,   { "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0,
3822      _("make the first entry referencing the specified kernel "      _("make the first entry referencing the specified kernel "
3823        "the default"), _("kernel-path") },        "the default"), _("kernel-path") },
3824     { "set-default-index", 0, POPT_ARG_INT, &defaultIndex, 0,
3825        _("make the given entry index the default entry"),
3826        _("entry-index") },
3827   { "silo", 0, POPT_ARG_NONE, &configureSilo, 0,   { "silo", 0, POPT_ARG_NONE, &configureSilo, 0,
3828      _("configure silo bootloader") },      _("configure silo bootloader") },
3829   { "title", 0, POPT_ARG_STRING, &newKernelTitle, 0,   { "title", 0, POPT_ARG_STRING, &newKernelTitle, 0,
# Line 3615  int main(int argc, const char ** argv) { Line 3932  int main(int argc, const char ** argv) {
3932      }      }
3933    
3934      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||
3935    newKernelPath || removeKernelPath || makeDefault ||      newKernelPath || removeKernelPath || makeDefault ||
3936    defaultKernel || displayDefaultIndex || displayDefaultTitle)) {      defaultKernel || displayDefaultIndex || displayDefaultTitle ||
3937        (defaultIndex >= 0))) {
3938   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "
3939    "specified option"));    "specified option"));
3940   return 1;   return 1;
# Line 3658  int main(int argc, const char ** argv) { Line 3976  int main(int argc, const char ** argv) {
3976   makeDefault = 1;   makeDefault = 1;
3977   defaultKernel = NULL;   defaultKernel = NULL;
3978      }      }
3979        else if (defaultKernel && (defaultIndex >= 0)) {
3980     fprintf(stderr, _("grubby: --set-default and --set-default-index "
3981      "may not be used together\n"));
3982     return 1;
3983        }
3984    
3985      if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) {      if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) {
3986   fprintf(stderr, _("grubby: output file must be specified if stdin "   fprintf(stderr, _("grubby: output file must be specified if stdin "
# Line 3666  int main(int argc, const char ** argv) { Line 3989  int main(int argc, const char ** argv) {
3989      }      }
3990    
3991      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel
3992   && !kernelInfo && !bootloaderProbe && !updateKernelPath   && !kernelInfo && !bootloaderProbe && !updateKernelPath
3993          && !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle) {   && !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle
3994     && (defaultIndex == -1)) {
3995   fprintf(stderr, _("grubby: no action specified\n"));   fprintf(stderr, _("grubby: no action specified\n"));
3996   return 1;   return 1;
3997      }      }
# Line 3694  int main(int argc, const char ** argv) { Line 4018  int main(int argc, const char ** argv) {
4018      }      }
4019    
4020      if (bootloaderProbe) {      if (bootloaderProbe) {
4021   int lrc = 0, grc = 0, gr2c = 0, erc = 0;   int lrc = 0, grc = 0, gr2c = 0, extrc = 0, yrc = 0, erc = 0;
4022   struct grubConfig * lconfig, * gconfig;   struct grubConfig * lconfig, * gconfig, * yconfig, * econfig;
4023    
4024   const char *grub2config = grub2FindConfig(&grub2ConfigType);   const char *grub2config = grub2FindConfig(&grub2ConfigType);
4025   if (grub2config) {   if (grub2config) {
# Line 3723  int main(int argc, const char ** argv) { Line 4047  int main(int argc, const char ** argv) {
4047   lrc = checkForLilo(lconfig);   lrc = checkForLilo(lconfig);
4048   }   }
4049    
4050     if (!access(eliloConfigType.defaultConfig, F_OK)) {
4051        econfig = readConfig(eliloConfigType.defaultConfig,
4052     &eliloConfigType);
4053        if (!econfig)
4054     erc = 1;
4055        else
4056     erc = checkForElilo(econfig);
4057     }
4058    
4059   if (!access(extlinuxConfigType.defaultConfig, F_OK)) {   if (!access(extlinuxConfigType.defaultConfig, F_OK)) {
4060      lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType);      lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType);
4061      if (!lconfig)      if (!lconfig)
4062   erc = 1;   extrc = 1;
4063      else      else
4064   erc = checkForExtLinux(lconfig);   extrc = checkForExtLinux(lconfig);
4065   }   }
4066    
4067   if (lrc == 1 || grc == 1 || gr2c == 1) return 1;  
4068     if (!access(yabootConfigType.defaultConfig, F_OK)) {
4069        yconfig = readConfig(yabootConfigType.defaultConfig,
4070     &yabootConfigType);
4071        if (!yconfig)
4072     yrc = 1;
4073        else
4074     yrc = checkForYaboot(yconfig);
4075     }
4076    
4077     if (lrc == 1 || grc == 1 || gr2c == 1 || extrc == 1 || yrc == 1 ||
4078     erc == 1)
4079        return 1;
4080    
4081   if (lrc == 2) printf("lilo\n");   if (lrc == 2) printf("lilo\n");
4082   if (gr2c == 2) printf("grub2\n");   if (gr2c == 2) printf("grub2\n");
4083   if (grc == 2) printf("grub\n");   if (grc == 2) printf("grub\n");
4084   if (erc == 2) printf("extlinux\n");   if (extrc == 2) printf("extlinux\n");
4085     if (yrc == 2) printf("yaboot\n");
4086     if (erc == 2) printf("elilo\n");
4087    
4088   return 0;   return 0;
4089      }      }
# Line 3803  int main(int argc, const char ** argv) { Line 4150  int main(int argc, const char ** argv) {
4150      markRemovedImage(config, removeKernelPath, bootPrefix);      markRemovedImage(config, removeKernelPath, bootPrefix);
4151      markRemovedImage(config, removeMBKernel, bootPrefix);      markRemovedImage(config, removeMBKernel, bootPrefix);
4152      setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault,      setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault,
4153      bootPrefix, flags);      bootPrefix, flags, defaultIndex);
4154      setFallbackImage(config, newKernelPath != NULL);      setFallbackImage(config, newKernelPath != NULL);
4155      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,
4156                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;

Legend:
Removed from v.1844  
changed lines
  Added in v.1868