Magellan Linux

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

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

revision 1850 by niro, Mon Jul 2 13:09:12 2012 UTC revision 1931 by niro, Mon Oct 1 12:06:26 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 205  struct keywordTypes grub2Keywords[] = { Line 205  struct keywordTypes grub2Keywords[] = {
205      { "default",    LT_DEFAULT,     ' ' },      { "default",    LT_DEFAULT,     ' ' },
206      { "fallback",   LT_FALLBACK,    ' ' },      { "fallback",   LT_FALLBACK,    ' ' },
207      { "linux",      LT_KERNEL,      ' ' },      { "linux",      LT_KERNEL,      ' ' },
208        { "linuxefi",   LT_KERNEL,      ' ' },
209      { "initrd",     LT_INITRD,      ' ', ' ' },      { "initrd",     LT_INITRD,      ' ', ' ' },
210        { "initrdefi",  LT_INITRD,      ' ', ' ' },
211      { "module",     LT_MBMODULE,    ' ' },      { "module",     LT_MBMODULE,    ' ' },
212      { "kernel",     LT_HYPER,       ' ' },      { "kernel",     LT_HYPER,       ' ' },
213      { NULL, 0, 0 },      { NULL, 0, 0 },
# Line 1310  static int writeConfig(struct grubConfig Line 1312  static int writeConfig(struct grubConfig
1312    
1313      /* most likely the symlink is relative, so change our      /* most likely the symlink is relative, so change our
1314         directory to the dir of the symlink */         directory to the dir of the symlink */
1315              rc = chdir(dirname(outName));      char *dir = strdupa(outName);
1316                rc = chdir(dirname(dir));
1317        free(dir);
1318      do {      do {
1319   buf = alloca(len + 1);   buf = alloca(len + 1);
1320   rc = readlink(basename(outName), buf, len);   rc = readlink(basename(outName), buf, len);
# Line 1867  void markRemovedImage(struct grubConfig Line 1871  void markRemovedImage(struct grubConfig
1871    
1872  void setDefaultImage(struct grubConfig * config, int hasNew,  void setDefaultImage(struct grubConfig * config, int hasNew,
1873       const char * defaultKernelPath, int newIsDefault,       const char * defaultKernelPath, int newIsDefault,
1874       const char * prefix, int flags) {       const char * prefix, int flags, int index) {
1875      struct singleEntry * entry, * entry2, * newDefault;      struct singleEntry * entry, * entry2, * newDefault;
1876      int i, j;      int i, j;
1877    
1878      if (newIsDefault) {      if (newIsDefault) {
1879   config->defaultImage = 0;   config->defaultImage = 0;
1880   return;   return;
1881        } else if ((index >= 0) && config->cfi->defaultIsIndex) {
1882     if (findEntryByIndex(config, index))
1883        config->defaultImage = index;
1884     else
1885        config->defaultImage = -1;
1886     return;
1887      } else if (defaultKernelPath) {      } else if (defaultKernelPath) {
1888   i = 0;   i = 0;
1889   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {
# Line 2350  void dumpSysconfigGrub(void) { Line 2360  void dumpSysconfigGrub(void) {
2360      char * boot = NULL;      char * boot = NULL;
2361      int lba;      int lba;
2362    
2363      if (!isSuseSystem()) {      if (isSuseSystem()) {
2364          if (parseSysconfigGrub(&lba, &boot)) {          if (parseSuseGrubConf(&lba, &boot)) {
2365      free(boot);      free(boot);
2366      return;      return;
2367   }   }
2368      } else {      } else {
2369          if (parseSuseGrubConf(&lba, &boot)) {          if (parseSysconfigGrub(&lba, &boot)) {
2370      free(boot);      free(boot);
2371      return;      return;
2372   }   }
# Line 3179  int checkForGrub(struct grubConfig * con Line 3189  int checkForGrub(struct grubConfig * con
3189      int fd;      int fd;
3190      unsigned char bootSect[512];      unsigned char bootSect[512];
3191      char * boot;      char * boot;
3192      int onSuse;      int onSuse = isSuseSystem();
3193    
3194      onSuse = isSuseSystem();  
3195      if (!onSuse) {      if (onSuse) {
3196   if (parseSysconfigGrub(NULL, &boot)) return 0;   if (parseSuseGrubConf(NULL, &boot))
3197        return 0;
3198      } else {      } else {
3199   if (parseSuseGrubConf(NULL, &boot)) return 0;   if (parseSysconfigGrub(NULL, &boot))
3200        return 0;
3201      }      }
3202    
3203      /* assume grub is not installed -- not an error condition */      /* assume grub is not installed -- not an error condition */
# Line 3205  int checkForGrub(struct grubConfig * con Line 3217  int checkForGrub(struct grubConfig * con
3217      }      }
3218      close(fd);      close(fd);
3219    
3220      if (!onSuse)      /* The more elaborate checks do not work on SuSE. The checks done
3221   return checkDeviceBootloader(boot, bootSect);       * seem to be reasonble (at least for now), so just return success
3222      else       */
3223   /*      if (onSuse)
  * The more elaborate checks do not work on SuSE. The checks done  
  * seem to be reasonble (at least for now), so just return success  
  */  
3224   return 2;   return 2;
3225    
3226        return checkDeviceBootloader(boot, bootSect);
3227  }  }
3228    
3229  int checkForExtLinux(struct grubConfig * config) {  int checkForExtLinux(struct grubConfig * config) {
# Line 3245  int checkForExtLinux(struct grubConfig * Line 3256  int checkForExtLinux(struct grubConfig *
3256      return checkDeviceBootloader(boot, bootSect);      return checkDeviceBootloader(boot, bootSect);
3257  }  }
3258    
3259    int checkForYaboot(struct grubConfig * config) {
3260        /*
3261         * This is a simplistic check that we consider good enough for own puporses
3262         *
3263         * If we were to properly check if yaboot is *installed* we'd need to:
3264         * 1) get the system boot device (LT_BOOT)
3265         * 2) considering it's a raw filesystem, check if the yaboot binary matches
3266         *    the content on the boot device
3267         * 3) if not, copy the binary to a temporary file and run "addnote" on it
3268         * 4) check again if binary and boot device contents match
3269         */
3270        if (!access("/etc/yaboot.conf", R_OK))
3271     return 2;
3272    
3273        return 1;
3274    }
3275    
3276    int checkForElilo(struct grubConfig * config) {
3277        if (!access("/etc/elilo.conf", R_OK))
3278     return 2;
3279    
3280        return 1;
3281    }
3282    
3283  static char * getRootSpecifier(char * str) {  static char * getRootSpecifier(char * str) {
3284      char * idx, * rootspec = NULL;      char * idx, * rootspec = NULL;
3285    
# Line 3715  int main(int argc, const char ** argv) { Line 3750  int main(int argc, const char ** argv) {
3750      int displayDefault = 0;      int displayDefault = 0;
3751      int displayDefaultIndex = 0;      int displayDefaultIndex = 0;
3752      int displayDefaultTitle = 0;      int displayDefaultTitle = 0;
3753        int defaultIndex = -1;
3754      struct poptOption options[] = {      struct poptOption options[] = {
3755   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,
3756      _("add an entry for the specified kernel"), _("kernel-path") },      _("add an entry for the specified kernel"), _("kernel-path") },
# Line 3732  int main(int argc, const char ** argv) { Line 3768  int main(int argc, const char ** argv) {
3768   { "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,   { "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,
3769      _("filestystem which contains /boot directory (for testing only)"),      _("filestystem which contains /boot directory (for testing only)"),
3770      _("bootfs") },      _("bootfs") },
3771  #if defined(__i386__) || defined(__x86_64__)  #if defined(__i386__) || defined(__x86_64__) || defined (__powerpc64__) || defined (__ia64__)
3772   { "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,   { "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,
3773      _("check if lilo is installed on lilo.conf boot sector") },      _("check which bootloader is installed on boot sector") },
3774  #endif  #endif
3775   { "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0,   { "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0,
3776      _("path to grub config file to update (\"-\" for stdin)"),      _("path to grub config file to update (\"-\" for stdin)"),
# Line 3787  int main(int argc, const char ** argv) { Line 3823  int main(int argc, const char ** argv) {
3823   { "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0,   { "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0,
3824      _("make the first entry referencing the specified kernel "      _("make the first entry referencing the specified kernel "
3825        "the default"), _("kernel-path") },        "the default"), _("kernel-path") },
3826     { "set-default-index", 0, POPT_ARG_INT, &defaultIndex, 0,
3827        _("make the given entry index the default entry"),
3828        _("entry-index") },
3829   { "silo", 0, POPT_ARG_NONE, &configureSilo, 0,   { "silo", 0, POPT_ARG_NONE, &configureSilo, 0,
3830      _("configure silo bootloader") },      _("configure silo bootloader") },
3831   { "title", 0, POPT_ARG_STRING, &newKernelTitle, 0,   { "title", 0, POPT_ARG_STRING, &newKernelTitle, 0,
# Line 3895  int main(int argc, const char ** argv) { Line 3934  int main(int argc, const char ** argv) {
3934      }      }
3935    
3936      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||
3937    newKernelPath || removeKernelPath || makeDefault ||      newKernelPath || removeKernelPath || makeDefault ||
3938    defaultKernel || displayDefaultIndex || displayDefaultTitle)) {      defaultKernel || displayDefaultIndex || displayDefaultTitle ||
3939        (defaultIndex >= 0))) {
3940   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "
3941    "specified option"));    "specified option"));
3942   return 1;   return 1;
# Line 3938  int main(int argc, const char ** argv) { Line 3978  int main(int argc, const char ** argv) {
3978   makeDefault = 1;   makeDefault = 1;
3979   defaultKernel = NULL;   defaultKernel = NULL;
3980      }      }
3981        else if (defaultKernel && (defaultIndex >= 0)) {
3982     fprintf(stderr, _("grubby: --set-default and --set-default-index "
3983      "may not be used together\n"));
3984     return 1;
3985        }
3986    
3987      if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) {      if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) {
3988   fprintf(stderr, _("grubby: output file must be specified if stdin "   fprintf(stderr, _("grubby: output file must be specified if stdin "
# Line 3946  int main(int argc, const char ** argv) { Line 3991  int main(int argc, const char ** argv) {
3991      }      }
3992    
3993      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel
3994   && !kernelInfo && !bootloaderProbe && !updateKernelPath   && !kernelInfo && !bootloaderProbe && !updateKernelPath
3995          && !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle) {   && !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle
3996     && (defaultIndex == -1)) {
3997   fprintf(stderr, _("grubby: no action specified\n"));   fprintf(stderr, _("grubby: no action specified\n"));
3998   return 1;   return 1;
3999      }      }
# Line 3974  int main(int argc, const char ** argv) { Line 4020  int main(int argc, const char ** argv) {
4020      }      }
4021    
4022      if (bootloaderProbe) {      if (bootloaderProbe) {
4023   int lrc = 0, grc = 0, gr2c = 0, erc = 0;   int lrc = 0, grc = 0, gr2c = 0, extrc = 0, yrc = 0, erc = 0;
4024   struct grubConfig * lconfig, * gconfig;   struct grubConfig * lconfig, * gconfig, * yconfig, * econfig;
4025    
4026   const char *grub2config = grub2FindConfig(&grub2ConfigType);   const char *grub2config = grub2FindConfig(&grub2ConfigType);
4027   if (grub2config) {   if (grub2config) {
# Line 4003  int main(int argc, const char ** argv) { Line 4049  int main(int argc, const char ** argv) {
4049   lrc = checkForLilo(lconfig);   lrc = checkForLilo(lconfig);
4050   }   }
4051    
4052     if (!access(eliloConfigType.defaultConfig, F_OK)) {
4053        econfig = readConfig(eliloConfigType.defaultConfig,
4054     &eliloConfigType);
4055        if (!econfig)
4056     erc = 1;
4057        else
4058     erc = checkForElilo(econfig);
4059     }
4060    
4061   if (!access(extlinuxConfigType.defaultConfig, F_OK)) {   if (!access(extlinuxConfigType.defaultConfig, F_OK)) {
4062      lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType);      lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType);
4063      if (!lconfig)      if (!lconfig)
4064   erc = 1;   extrc = 1;
4065      else      else
4066   erc = checkForExtLinux(lconfig);   extrc = checkForExtLinux(lconfig);
4067   }   }
4068    
4069   if (lrc == 1 || grc == 1 || gr2c == 1) return 1;  
4070     if (!access(yabootConfigType.defaultConfig, F_OK)) {
4071        yconfig = readConfig(yabootConfigType.defaultConfig,
4072     &yabootConfigType);
4073        if (!yconfig)
4074     yrc = 1;
4075        else
4076     yrc = checkForYaboot(yconfig);
4077     }
4078    
4079     if (lrc == 1 || grc == 1 || gr2c == 1 || extrc == 1 || yrc == 1 ||
4080     erc == 1)
4081        return 1;
4082    
4083   if (lrc == 2) printf("lilo\n");   if (lrc == 2) printf("lilo\n");
4084   if (gr2c == 2) printf("grub2\n");   if (gr2c == 2) printf("grub2\n");
4085   if (grc == 2) printf("grub\n");   if (grc == 2) printf("grub\n");
4086   if (erc == 2) printf("extlinux\n");   if (extrc == 2) printf("extlinux\n");
4087     if (yrc == 2) printf("yaboot\n");
4088     if (erc == 2) printf("elilo\n");
4089    
4090   return 0;   return 0;
4091      }      }
# Line 4083  int main(int argc, const char ** argv) { Line 4152  int main(int argc, const char ** argv) {
4152      markRemovedImage(config, removeKernelPath, bootPrefix);      markRemovedImage(config, removeKernelPath, bootPrefix);
4153      markRemovedImage(config, removeMBKernel, bootPrefix);      markRemovedImage(config, removeMBKernel, bootPrefix);
4154      setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault,      setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault,
4155      bootPrefix, flags);      bootPrefix, flags, defaultIndex);
4156      setFallbackImage(config, newKernelPath != NULL);      setFallbackImage(config, newKernelPath != NULL);
4157      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,
4158                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;

Legend:
Removed from v.1850  
changed lines
  Added in v.1931