--- trunk/grubby/grubby.c 2017/06/27 14:14:00 3003 +++ trunk/grubby/grubby.c 2020/07/07 11:10:21 3137 @@ -132,6 +132,10 @@ #define NEED_DEVTREE (1 << 6) #define MAIN_DEFAULT (1 << 0) +#define FIRST_ENTRY_INDEX 0 /* boot entry index value begin and increment + from this initial value */ +#define NO_DEFAULT_ENTRY -1 /* indicates that no specific default boot + entry was set or currently exists */ #define DEFAULT_SAVED -2 #define DEFAULT_SAVED_GRUB2 -3 @@ -677,6 +681,8 @@ int fallbackImage; /* just like defaultImage */ int flags; struct configFileInfo *cfi; + int isModified; /* assumes only one entry added + per invocation of grubby */ }; blkid_cache blkid; @@ -1296,6 +1302,7 @@ cfg->theLines = NULL; cfg->entries = NULL; cfg->fallbackImage = 0; + cfg->isModified = 0; /* copy everything we have */ while (*head) { @@ -1615,7 +1622,7 @@ *end == ' ' || *end == '\t')) end++; if (*end) - cfg->defaultImage = -1; + cfg->defaultImage = NO_DEFAULT_ENTRY; } else if (defaultLine->numElements == 3) { char *value = defaultLine->elements[2].item; while (*value && (*value == '"' || @@ -1628,7 +1635,7 @@ *end == ' ' || *end == '\t')) end++; if (*end) - cfg->defaultImage = -1; + cfg->defaultImage = NO_DEFAULT_ENTRY; } } else if (cfi->defaultSupportSaved && !strncmp(defaultLine->elements[1].item, "saved", @@ -1638,7 +1645,7 @@ cfg->defaultImage = strtol(defaultLine->elements[1].item, &end, 10); if (*end) - cfg->defaultImage = -1; + cfg->defaultImage = NO_DEFAULT_ENTRY; } else if (defaultLine->numElements >= 2) { int i = 0; while ((entry = findEntryByIndex(cfg, i))) { @@ -1666,7 +1673,7 @@ if (entry) { cfg->defaultImage = i; } else { - cfg->defaultImage = -1; + cfg->defaultImage = NO_DEFAULT_ENTRY; } } } else if (cfg->cfi->defaultIsSaved && cfg->cfi->getEnv) { @@ -1683,7 +1690,7 @@ cfg->defaultImage = index; } } else { - cfg->defaultImage = 0; + cfg->defaultImage = FIRST_ENTRY_INDEX; } return cfg; @@ -1703,9 +1710,22 @@ fprintf(out, "%sdefault%ssaved\n", indent, separator); else if (cfg->cfi->defaultIsSaved) { fprintf(out, "%sset default=\"${saved_entry}\"\n", indent); - if (cfg->defaultImage >= 0 && cfg->cfi->setEnv) { + if (cfg->defaultImage >= FIRST_ENTRY_INDEX && cfg->cfi->setEnv) { char *title; - entry = findEntryByIndex(cfg, cfg->defaultImage); + int trueIndex, currentIndex; + + trueIndex = 0; + currentIndex = 0; + + while ((entry = findEntryByIndex(cfg, currentIndex))) { + if (!entry->skip) { + if (trueIndex == cfg->defaultImage) { + break; + } + trueIndex++; + } + currentIndex++; + } line = getLineByType(LT_MENUENTRY, entry->lines); if (!line) line = getLineByType(LT_TITLE, entry->lines); @@ -1716,7 +1736,7 @@ "saved_entry", title); } } - } else if (cfg->defaultImage > -1) { + } else if (cfg->defaultImage >= FIRST_ENTRY_INDEX) { if (cfg->cfi->defaultIsIndex) { if (cfg->cfi->defaultIsVariable) { fprintf(out, "%sset default=\"%d\"\n", indent, @@ -1772,6 +1792,7 @@ int needs = MAIN_DEFAULT; struct stat sb; int i; + int rc = 0; if (!strcmp(outName, "-")) { out = stdout; @@ -1886,16 +1907,42 @@ } if (tmpOutName) { - if (rename(tmpOutName, outName)) { - fprintf(stderr, - _("grubby: error moving %s to %s: %s\n"), - tmpOutName, outName, strerror(errno)); - unlink(outName); - return 1; + /* write userspace buffers */ + if (fflush(out)) + rc = 1; + + /* purge the write-back cache with fsync() */ + if (fsync(fileno(out))) + rc = 1; + + if (fclose(out)) + rc = 1; + + if (rc == 0 && rename(tmpOutName, outName)) { + unlink(tmpOutName); + rc = 1; } + + /* fsync() the destination directory after rename */ + if (rc == 0) { + int dirfd; + + dirfd = open(dirname(strdupa(outName)), O_RDONLY); + if (dirfd < 0) + rc = 1; + else if (fsync(dirfd)) + rc = 1; + + if (dirfd >= 0) + close(dirfd); + } + + if (rc == 1) + fprintf(stderr, + _("grubby: error flushing data: %m\n")); } - return 0; + return rc; } static int numEntries(struct grubConfig *cfg) @@ -2420,7 +2467,7 @@ } } } - } else if (cfg->defaultImage > -1) { + } else if (cfg->defaultImage >= FIRST_ENTRY_INDEX) { entry = findEntryByIndex(cfg, cfg->defaultImage); if (entry && suitableImage(entry, prefix, skipRemoved, flags)) { if (indexPtr) @@ -2432,8 +2479,11 @@ index = 0; while ((entry = findEntryByIndex(cfg, index))) { if (suitableImage(entry, prefix, skipRemoved, flags)) { - int j; - for (j = 0; j < index; j++) { + int j, unmodifiedIndex; + + unmodifiedIndex = index; + + for (j = 0; j < unmodifiedIndex; j++) { entry2 = findEntryByIndex(cfg, j); if (entry2->skip) index--; @@ -2494,66 +2544,147 @@ entry->skip = 1; } -void setDefaultImage(struct grubConfig *config, int hasNew, - const char *defaultKernelPath, int newIsDefault, - const char *prefix, int flags, int index) -{ - struct singleEntry *entry, *entry2, *newDefault; - int i, j; - - if (newIsDefault) { - config->defaultImage = 0; +void setDefaultImage(struct grubConfig *config, int isAddingBootEntry, + const char *defaultKernelPath, int newBootEntryIsDefault, + const char *prefix, int flags, + int newDefaultBootEntryIndex, int newBootEntryIndex) +{ + struct singleEntry *bootEntry, *newDefault; + int indexToVerify, firstKernelEntryIndex, currentLookupIndex; + + /* initialize */ + currentLookupIndex = FIRST_ENTRY_INDEX; + + /* handle the two cases where the user explictly picks the default + * boot entry index as it would exist post-modification */ + + /* Case 1: user chose to make the latest boot entry the default */ + if (newBootEntryIsDefault) { + config->defaultImage = newBootEntryIndex; return; - } else if ((index >= 0) && config->cfi->defaultIsIndex) { - if (findEntryByIndex(config, index)) - config->defaultImage = index; - else - config->defaultImage = -1; - return; - } else if (defaultKernelPath) { - i = 0; - if (findEntryByPath(config, defaultKernelPath, prefix, &i)) { - config->defaultImage = i; - } else { - config->defaultImage = -1; + } + + /* Case 2: user picked an arbitrary index as the default boot entry */ + if (newDefaultBootEntryIndex >= FIRST_ENTRY_INDEX) { + indexToVerify = newDefaultBootEntryIndex; + + /* user chose to make latest boot entry the default */ + if (newDefaultBootEntryIndex == newBootEntryIndex) { + config->defaultImage = newBootEntryIndex; return; } - } - /* defaultImage now points to what we'd like to use, but before any - * order changes */ - if ((config->defaultImage == DEFAULT_SAVED) || - (config->defaultImage == DEFAULT_SAVED_GRUB2)) - /* default is set to saved, we don't want to change it */ + /* the user picks the default index based on the + * order of the bootloader configuration after + * modification; ensure we are checking for the + * existence of the correct entry */ + if (newBootEntryIndex < newDefaultBootEntryIndex) { + if (!config->isModified) + indexToVerify--; + } + + /* verify the user selected index will exist */ + if (findEntryByIndex(config, indexToVerify)) { + config->defaultImage = newDefaultBootEntryIndex; + } else { + config->defaultImage = NO_DEFAULT_ENTRY; + } + return; + } - if (config->defaultImage > -1) - entry = findEntryByIndex(config, config->defaultImage); - else - entry = NULL; + /* handle cases where the index value may shift */ - if (entry && !entry->skip) { - /* we can preserve the default */ - if (hasNew) - config->defaultImage++; + /* check validity of existing default or first-entry-found + selection */ + if (defaultKernelPath) { + /* we must initialize this */ + firstKernelEntryIndex = 0; + /* user requested first-entry-found */ + if (!findEntryByPath(config, defaultKernelPath, + prefix, &firstKernelEntryIndex)) { + /* don't change default if can't find match */ + config->defaultImage = NO_DEFAULT_ENTRY; + return; + } + + config->defaultImage = firstKernelEntryIndex; + + /* this is where we start looking for decrement later */ + currentLookupIndex = config->defaultImage; - /* count the number of entries erased before this one */ - for (j = 0; j < config->defaultImage; j++) { - entry2 = findEntryByIndex(config, j); - if (entry2->skip) - config->defaultImage--; + if (isAddingBootEntry && !config->isModified && + (newBootEntryIndex < config->defaultImage)) { + /* increment because new entry added before default */ + config->defaultImage++; } - } else if (hasNew) { - config->defaultImage = 0; } else { - /* Either we just erased the default (or the default line was - * bad to begin with) and didn't put a new one in. We'll use - * the first valid image. */ + /* check to see if the default is stored in the environment */ + if (config->defaultImage < FIRST_ENTRY_INDEX) { + if (config->defaultImage == DEFAULT_SAVED || config->defaultImage == DEFAULT_SAVED_GRUB2) + { + if (config->cfi->defaultIsSaved) { + if (config->cfi->getEnv) { + char *defaultTitle = config->cfi->getEnv(config->cfi, "saved_entry"); + + if (defaultTitle) { + if (isnumber(defaultTitle)) { + currentLookupIndex = atoi(defaultTitle); + } else { + findEntryByTitle(config, defaultTitle, ¤tLookupIndex); + } + /* set the default Image to an actual index */ + config->defaultImage = currentLookupIndex; + } + } + } + } + } else { + /* use pre-existing default entry from the file*/ + currentLookupIndex = config->defaultImage; + } + + if (isAddingBootEntry + && (newBootEntryIndex <= config->defaultImage)) { + config->defaultImage++; + + if (config->isModified) { + currentLookupIndex++; + } + } + } + + /* sanity check - is this entry index valid? */ + bootEntry = findEntryByIndex(config, currentLookupIndex); + + if ((bootEntry && bootEntry->skip) || !bootEntry) { + /* entry is to be skipped or is invalid */ + if (isAddingBootEntry) { + config->defaultImage = newBootEntryIndex; + return; + } newDefault = findTemplate(config, prefix, &config->defaultImage, 1, flags); - if (!newDefault) - config->defaultImage = -1; + if (!newDefault) { + config->defaultImage = NO_DEFAULT_ENTRY; + } + + return; + } + + currentLookupIndex--; + + /* decrement index by the total number of entries deleted */ + + for (indexToVerify = currentLookupIndex; + indexToVerify >= FIRST_ENTRY_INDEX; indexToVerify--) { + + bootEntry = findEntryByIndex(config, indexToVerify); + + if (bootEntry && bootEntry->skip) { + config->defaultImage--; + } } } @@ -2582,7 +2713,7 @@ } } -void displayEntry(struct singleEntry *entry, const char *prefix, int index) +void displayEntry(struct grubConfig *config, struct singleEntry *entry, const char *prefix, int index) { struct singleLine *line; char *root = NULL; @@ -2678,7 +2809,14 @@ line = getLineByType(LT_TITLE, entry->lines); if (line) { - printf("title=%s\n", line->elements[1].item); + char *entryTitle; + /* if we can extractTitle, then it's a zipl config and + * if not then we go ahead with what's existed prior */ + entryTitle = extractTitle(config, line); + if (!entryTitle) { + entryTitle=line->elements[1].item; + } + printf("title=%s\n", entryTitle); } else { char *title; line = getLineByType(LT_MENUENTRY, entry->lines); @@ -3094,11 +3232,11 @@ printf("lba\n"); } - displayEntry(entry, prefix, i); + displayEntry(config, entry, prefix, i); i++; while ((entry = findEntryByPath(config, kernel, prefix, &i))) { - displayEntry(entry, prefix, i); + displayEntry(config, entry, prefix, i); i++; } @@ -3428,23 +3566,67 @@ line->numElements--; } -int argMatch(const char *one, const char *two) +static int argNameMatch(const char *one, const char *two) { char *first, *second; - char *chptr; + char *chptra, *chptrb; + int rc; first = strcpy(alloca(strlen(one) + 1), one); second = strcpy(alloca(strlen(two) + 1), two); - chptr = strchr(first, '='); - if (chptr) - *chptr = '\0'; + chptra = strchr(first, '='); + if (chptra) + *chptra = '\0'; + + chptrb = strchr(second, '='); + if (chptrb) + *chptrb = '\0'; + + rc = strcmp(first, second); + + if (chptra) + *chptra = '='; + if (chptrb) + *chptrb = '='; - chptr = strchr(second, '='); + return rc; +} + +static int argHasValue(const char *arg) +{ + char *chptr; + + chptr = strchr(arg, '='); if (chptr) - *chptr = '\0'; + return 1; + return 0; +} - return strcmp(first, second); +static int argValueMatch(const char *one, const char *two) +{ + char *first, *second; + char *chptra, *chptrb; + + first = strcpy(alloca(strlen(one) + 1), one); + second = strcpy(alloca(strlen(two) + 1), two); + + chptra = strchr(first, '='); + if (chptra) + chptra += 1; + + chptrb = strchr(second, '='); + if (chptrb) + chptrb += 1; + + if (!chptra && !chptrb) + return 0; + else if (!chptra) + return *chptrb - 0; + else if (!chptrb) + return 0 - *chptra; + else + return strcmp(chptra, chptrb); } int updateActualImage(struct grubConfig *cfg, const char *image, @@ -3588,7 +3770,7 @@ } if (usedElements[i]) continue; - if (!argMatch(line->elements[i].item, *arg)) { + if (!argNameMatch(line->elements[i].item, *arg)) { usedElements[i] = 1; break; } @@ -3647,9 +3829,12 @@ !strcmp(line->elements[i].item, "--")) /* reached the end of hyper args, stop here */ break; - if (!argMatch(line->elements[i].item, *arg)) { - removeElement(line, i); - break; + if (!argNameMatch(line->elements[i].item, *arg)) { + if (!argHasValue(*arg) || + !argValueMatch(line->elements[i].item, *arg)) { + removeElement(line, i); + break; + } } } /* handle removing LT_ROOT line too */ @@ -4210,16 +4395,22 @@ const char *newKernelArgs, const char *newKernelInitrd, const char **extraInitrds, int extraInitrdCount, const char *newMBKernel, const char *newMBKernelArgs, - const char *newDevTreePath) + const char *newDevTreePath, int newIndex) { - struct singleEntry *new; + struct singleEntry *new, *entry, *prev = NULL; struct singleLine *newLine = NULL, *tmplLine = NULL, *masterLine = NULL; int needs; + char *indexs; char *chptr; + int rc; if (!newKernelPath) return 0; + rc = asprintf(&indexs, "%d", newIndex); + if (rc < 0) + return 1; + /* if the newKernelTitle is too long silently munge it into something * we can live with. truncating is first check, then we'll just mess with * it until it looks better */ @@ -4242,9 +4433,20 @@ new = malloc(sizeof(*new)); new->skip = 0; new->multiboot = 0; - new->next = config->entries; new->lines = NULL; - config->entries = new; + entry = config->entries; + for (unsigned int i = 0; i < newIndex; i++) { + if (!entry) + break; + prev = entry; + entry = entry->next; + } + new->next = entry; + + if (prev) + prev->next = new; + else + config->entries = new; /* copy/update from the template */ needs = NEED_KERNEL | NEED_TITLE; @@ -4707,9 +4909,11 @@ abort(); } - if (updateImage(config, "0", prefix, newKernelArgs, NULL, - newMBKernelArgs, NULL)) + if (updateImage(config, indexs, prefix, newKernelArgs, NULL, + newMBKernelArgs, NULL)) { + config->isModified = 1; return 1; + } return 0; } @@ -4737,6 +4941,7 @@ char *newDevTreePath = NULL; char *newMBKernel = NULL; char *newMBKernelArgs = NULL; + int newIndex = 0; char *removeMBKernelArgs = NULL; char *removeMBKernel = NULL; char *bootPrefix = NULL; @@ -4771,7 +4976,7 @@ NULL}, {"boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0, _ - ("filestystem which contains /boot directory (for testing only)"), + ("filesystem which contains /boot directory (for testing only)"), _("bootfs")}, #if defined(__i386__) || defined(__x86_64__) || defined (__powerpc64__) || defined (__ia64__) {"bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0, @@ -4843,6 +5048,9 @@ {"set-default-index", 0, POPT_ARG_INT, &defaultIndex, 0, _("make the given entry index the default entry"), _("entry-index")}, + {"set-index", 0, POPT_ARG_INT, &newIndex, 0, + _("use the given index when creating a new entry"), + _("entry-index")}, {"silo", 0, POPT_ARG_NONE, &configureSilo, 0, _("configure silo bootloader")}, {"title", 0, POPT_ARG_STRING, &newKernelTitle, 0, @@ -4946,22 +5154,27 @@ if (!cfi) { if (grub2FindConfig(&grub2ConfigType)) { cfi = &grub2ConfigType; + configureGrub2 = 1; if (envPath) cfi->envFile = envPath; - } else + } else { #ifdef __ia64__ cfi = &eliloConfigType; -#elif __powerpc__ + configureLilo = 1; +#elif defined(__powerpc__) cfi = &yabootConfigType; -#elif __sparc__ + configureYaboot = 1; +#elif defined(__sparc__) cfi = &siloConfigType; -#elif __s390__ + configureSilo = 1; +#elif defined(__s390__) || defined(__s390x__) cfi = &ziplConfigType; -#elif __s390x__ - cfi = &ziplConfigtype; + configureZipl = 1; #else cfi = &grubConfigType; + configureGrub = 1; #endif + } } if (!grubConfig) { @@ -5157,11 +5370,11 @@ struct singleEntry *entry; char *rootspec; - if (config->defaultImage == -1) + if (config->defaultImage == NO_DEFAULT_ENTRY) return 0; if (config->defaultImage == DEFAULT_SAVED_GRUB2 && cfi->defaultIsSaved) - config->defaultImage = 0; + config->defaultImage = FIRST_ENTRY_INDEX; entry = findEntryByIndex(config, config->defaultImage); if (!entry) return 0; @@ -5184,11 +5397,11 @@ struct singleLine *line; struct singleEntry *entry; - if (config->defaultImage == -1) + if (config->defaultImage == NO_DEFAULT_ENTRY) return 0; if (config->defaultImage == DEFAULT_SAVED_GRUB2 && cfi->defaultIsSaved) - config->defaultImage = 0; + config->defaultImage = FIRST_ENTRY_INDEX; entry = findEntryByIndex(config, config->defaultImage); if (!entry) return 0; @@ -5218,11 +5431,11 @@ return 0; } else if (displayDefaultIndex) { - if (config->defaultImage == -1) + if (config->defaultImage == NO_DEFAULT_ENTRY) return 0; if (config->defaultImage == DEFAULT_SAVED_GRUB2 && cfi->defaultIsSaved) - config->defaultImage = 0; + config->defaultImage = FIRST_ENTRY_INDEX; printf("%i\n", config->defaultImage); return 0; @@ -5238,7 +5451,8 @@ markRemovedImage(config, removeKernelPath, bootPrefix); markRemovedImage(config, removeMBKernel, bootPrefix); setDefaultImage(config, newKernelPath != NULL, defaultKernel, - makeDefault, bootPrefix, flags, defaultIndex); + makeDefault, bootPrefix, flags, defaultIndex, + newIndex); setFallbackImage(config, newKernelPath != NULL); if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs, removeArgs, newMBKernelArgs, removeMBKernelArgs)) @@ -5258,7 +5472,8 @@ if (addNewKernel(config, template, bootPrefix, newKernelPath, newKernelTitle, newKernelArgs, newKernelInitrd, (const char **)extraInitrds, extraInitrdCount, - newMBKernel, newMBKernelArgs, newDevTreePath)) + newMBKernel, newMBKernelArgs, newDevTreePath, + newIndex)) return 1; if (numEntries(config) == 0) {