def listUnused(): # Main menu configuration grub = grubConf() if os.path.exists(CONF_GRUB): grub.parseConf(CONF_GRUB) root = getBoot() # Find kernel entries kernels_in_use = [] for entry in grub.entries: os_entry = parseGrubEntry(entry) # os_entry can have root or uuid depending on the distribution if os_entry["os_type"] in ["linux", "xen"]: if os_entry.get("root", "") == root or getDeviceByUUID(os_entry.get("uuid", "")) == root: kernel_version = os_entry["kernel"].split("kernel-")[1] kernels_in_use.append(kernel_version) # Find installed kernels kernels_installed = [] for _file in os.listdir(BOOT_DIR): if _file.startswith("kernel-"): kernel_version = _file.split("kernel-")[1] kernels_installed.append(kernel_version) kernels_unused = set(kernels_installed) - set(kernels_in_use) kernels_unused = list(kernels_unused) return kernels_unused
def setOption(option, value): # Main menu configuration grub = grubConf() if os.path.exists(CONF_GRUB): grub.parseConf(CONF_GRUB) # Alternate menu configuration grub_alt = grubConf() if os.path.exists(CONF_GRUB_ALT): grub_alt.parseConf(CONF_GRUB_ALT) if option == 'default': grub.setOption("default", value) for index, entry in enumerate(grub.entries): if value == "saved": entry.setCommand("savedefault", "") else: entry.unsetCommand("savedefault") default_entry = os.path.join(GRUB_DIR, "default") if not os.path.exists(default_entry): file(default_entry, "w").write("\x00\x30\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a") elif option in 'timeout': grub.setOption("timeout", value) elif option == 'password': #grub.setOption("password", "--md5 %s" % md5crypt(value)) grub.setOption("password", value) elif option == 'background': grub.setOption("background", value) elif option == 'splash': root = getBoot() root_grub = grubAddress(root) grub.setOption("splashimage", "%s%s" % (root_grub, value)) # Copy options to alternative configuration. copyOptions(grub, grub_alt) # Save changes to both files. grub.write(CONF_GRUB) grub_alt.write(CONF_GRUB_ALT) # Notify all COMAR clients notify("Boot.Loader", "Changed", "option")
def updateKernelEntry(version, root): # Root device if not len(root): root = getBoot() # Kernel version if not len(version): version = os.uname()[2] # Main menu configuration grub = grubConf() if os.path.exists(CONF_GRUB): grub.parseConf(CONF_GRUB) # Alternative menu configuration grub_alt = grubConf() if os.path.exists(CONF_GRUB_ALT): grub_alt.parseConf(CONF_GRUB_ALT) # Copy options to alternative configuration. copyOptions(grub, grub_alt) # Add new kernel to main configuration. addNewKernel(grub, version, root) # Move old kernels to alternative configuration. moveOldKernels(grub, grub_alt, root) # Regroup kernels in alternative configuration. This will shorten list. regroupKernels(grub_alt, root, MAX_ENTRIES) # Add cross links between two configuration files. addLinks(grub, grub_alt, CONF_GRUB, CONF_GRUB_ALT) # Save changes to both files. grub.write(CONF_GRUB) grub_alt.write(CONF_GRUB_ALT) # Notify all COMAR clients notify("Boot.Loader", "Changed", "option")
def removeEntry(index, title, uninstall): # Main menu configuration grub = grubConf() if os.path.exists(CONF_GRUB): grub.parseConf(CONF_GRUB) index = int(index) # Check entry title entry = grub.entries[index] if entry.title != title: fail(FAIL_NOENTRY) # Get default index default_index = grub.getOption("default", 0) try: default_index = int(default_index) except ValueError: default_index = 0 # Remove entry grub.removeEntry(entry) # Fix default index, if necessary if index <= default_index: default_index = max(0, default_index - 1) grub.setOption("default", default_index) # Save changes to both files. grub.write(CONF_GRUB) # Notify all COMAR clients notify("Boot.Loader", "Changed", "entry") if uninstall == "yes": os_entry = parseGrubEntry(entry) if os_entry["os_type"] in ["linux", "xen"] and os_entry["root"] == getBoot(): kernel_version = os_entry["kernel"].split("kernel-")[1] removeKernel(kernel_version)