예제 #1
0
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
예제 #2
0
def getOptions():
    # Main menu configuration
    grub = grubConf()
    if os.path.exists(CONF_GRUB):
        grub.parseConf(CONF_GRUB)

    # Default options
    options = {
        "default": grub.getOption("default", "0"),
        "timeout": grub.getOption("timeout", "0"),
    }

    # Password
    if "password" in grub.options:
        options["password"] = "******"

    # Background color
    if "background" in grub.options:
        options["background"] = grub.getOption("background")

    # Get splash image, strip device address
    if "splashimage" in grub.options:
        splash = grub.getOption("splashimage")
        if ")" in splash:
            splash = splash.split(")")[1]
        options["splash"] = splash

    return options
예제 #3
0
def listUnused():
    # Main menu configuration
    grub = grubConf()
    if os.path.exists(CONF_GRUB):
        grub.parseConf(CONF_GRUB)

    root = getRoot()

    # 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
예제 #4
0
def getOptions():
    # Main menu configuration
    grub = grubConf()
    if os.path.exists(CONF_GRUB):
        grub.parseConf(CONF_GRUB)

    # Default options
    options = {
        "default": grub.getOption("default", "0"),
        "timeout": grub.getOption("timeout", "0"),
    }

    # Password
    if "password" in grub.options:
        options["password"] = "******"

    # Background color
    if "background" in grub.options:
        options["background"] = grub.getOption("background")

    # Get splash image, strip device address
    if "splashimage" in grub.options:
        splash = grub.getOption("splashimage")
        if ")" in splash:
            splash = splash.split(")")[1]
        options["splash"] = splash

    return options
예제 #5
0
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):
            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 = getRoot()
        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")
예제 #6
0
def setEntry(title, os_type, root, kernel, initrd, options, default, index):
    if not len(title):
        fail(FAIL_NOTITLE)

    # 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)

    index = int(index)

    entry = makeGrubEntry(title, os_type, root, kernel, initrd, options)

    if index == -1:
        grub.addEntry(entry)
    else:
        grub.entries[index] = entry

    if default == "yes":
        grub.setOption("default", index)
    elif default == "saved":
        grub.setOption("default", "saved")
        for index, entry in enumerate(grub.entries):
            entry.setCommand("savedefault", "")
    elif default == "no" and index != -1:
        default_index = grub.getOption("default", "0")
        if default_index != "saved" and int(default_index) == index:
            grub.setOption("default", "0")

    # Relocate links
    addLinks(grub, grub_alt, CONF_GRUB, CONF_GRUB_ALT)

    # Save changes to both files.
    grub.write(CONF_GRUB)

    # Notify all COMAR clients
    notify("Boot.Loader", "Changed", "option")
예제 #7
0
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")
예제 #8
0
def setEntry(title, os_type, root, kernel, initrd, options, default, index):
    if not len(title):
        fail(FAIL_NOTITLE)

    # 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)

    index = int(index)

    entry = makeGrubEntry(title, os_type, root, kernel, initrd, options)

    if index == -1:
        grub.addEntry(entry)
    else:
        grub.entries[index] = entry

    if default == "yes":
        grub.setOption("default", index)
    elif default == "saved":
        grub.setOption("default", "saved")
        for index, entry in enumerate(grub.entries):
            entry.setCommand("savedefault", "")
    elif default == "no" and index != -1:
        default_index = grub.getOption("default", "0")
        if default_index != "saved" and int(default_index) == index:
            grub.setOption("default", "0")

    # Relocate links
    addLinks(grub, grub_alt, CONF_GRUB, CONF_GRUB_ALT)

    # Save changes to both files.
    grub.write(CONF_GRUB)

    # Notify all COMAR clients
    notify("Boot.Loader", "Changed", "option")
예제 #9
0
    def appendLinuxSystems(self, device, formatType):
        additional_conf = yali.util.get_grub_conf(device, formatType)
        if additional_conf and len(additional_conf.entries):
            self.grubConf = grubutils.grubConf()
            self.grubConf.parseConf(os.path.join(ctx.consts.target_dir, self._conf))
            for entry in additional_conf.entries:
                stage2Device = entry.getCommand("uuid").value if entry.getCommand("uuid") else entry.getCommand("root").value
                if stage2Device:
                    entry.title = entry.title + " [ %s ]" % device
                    self.grubConf.addEntry(entry)

            self.grubConf.write(os.path.join(ctx.consts.target_dir, self._conf))
예제 #10
0
def updateKernelEntry(version, root):
    # Root device
    if not len(root):
        root = getRoot()

    # 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")
예제 #11
0
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")
예제 #12
0
파일: util.py 프로젝트: suvari/project
def get_grub_conf(device_path, format_type):
    if os.path.exists(ctx.consts.tmp_mnt_dir):
        umount(ctx.consts.tmp_mnt_dir)

    grub_conf = None
    ctx.logger.debug("Mounting %s to %s to check partition" %
                     (device_path, ctx.consts.tmp_mnt_dir))
    rc = mount(device_path, ctx.consts.tmp_mnt_dir, format_type)
    if rc:
        ctx.logger.debug("Mount failed for %s " % device_path)
    else:
        is_exist = lambda p, f: os.path.exists(
            os.path.join(ctx.consts.tmp_mnt_dir, p, f))

        grub_path = None
        if is_exist("boot/grub", "grub.conf") or is_exist(
                "boot/grub", "menu.lst"):
            grub_path = "boot/grub"
        elif is_exist("grub", "grub.conf") or is_exist("grub", "menu.lst"):
            grub_path = "grub"

        if grub_path:
            ctx.logger.debug(
                "%s device has bootloader configuration to parse." %
                device_path)
            menulst = os.path.join(ctx.consts.tmp_mnt_dir, grub_path,
                                   "menu.lst")
            grubconf = os.path.join(ctx.consts.tmp_mnt_dir, grub_path,
                                    "grub.conf")
            if os.path.islink(menulst):
                ctx.logger.debug("Additional grub.conf found on device %s" %
                                 device_path)
                grub_path = grubconf
            else:
                ctx.logger.debug("Additional menu.lst found on device %s" %
                                 device_path)
                grub_path = menulst

            grub_conf = grubutils.grubConf()
            grub_conf.parseConf(grub_path)
        else:
            ctx.logger.debug(
                "%s device has not any bootloader configuration to parse." %
                device_path)

        umount(ctx.consts.tmp_mnt_dir)

    return grub_conf
예제 #13
0
    def appendLinuxSystems(self, device, formatType):
        additional_conf = yali.util.get_grub_conf(device, formatType)
        if additional_conf and len(additional_conf.entries):
            self.grubConf = grubutils.grubConf()
            self.grubConf.parseConf(os.path.join(ctx.consts.target_dir, self._conf))
            for entry in additional_conf.entries:
                stage2Device = None
                if entry.getCommand("uuid"):
                    stage2Device = entry.getCommand("uuid").value
                elif entry.getCommand("root"):
                    stage2Device = entry.getCommand("root").value
                if stage2Device:
                    entry.title = entry.title + " [ %s ]" % device
                    self.grubConf.addEntry(entry)

            self.grubConf.write(os.path.join(ctx.consts.target_dir, self._conf))
예제 #14
0
    def __init__(self, _dir, write=False, timeout=-1):
        self.dir = _dir
        self.write = write
        self.grub_conf = os.path.join(_dir, "grub.conf")

        self.lock = FileLock("%s/.grub.lock" % _dir)
        try:
            self.lock.lock(write, timeout)
        except IOError:
            fail(FAIL_TIMEOUT)

        # Fail if grub is not installed
        if os.path.exists(self.grub_conf):
            self.config = grubConf()
            self.config.parseConf(self.grub_conf)
        else:
            self.fail(FAIL_NOGRUB)
예제 #15
0
 def __init__(self, _dir, write=False, timeout=-1):
     self.dir = _dir
     self.write = write
     self.grub_conf = os.path.join(_dir, "grub.conf")
     
     self.lock = FileLock("%s/.grub.lock" % _dir)
     try:
         self.lock.lock(write, timeout)
     except IOError:
         fail(FAIL_TIMEOUT)
     
     # Fail if grub is not installed
     if os.path.exists(self.grub_conf):
         self.config = grubConf()
         self.config.parseConf(self.grub_conf)
     else:
         self.fail(FAIL_NOGRUB)
예제 #16
0
def listEntries():
    # Main menu configuration
    grub = grubConf()
    if os.path.exists(CONF_GRUB):
        grub.parseConf(CONF_GRUB)

    entries = []
    for index, entry in enumerate(grub.entries):
        os_entry = parseGrubEntry(entry)
        if os_entry["os_type"] == "unknown":
            continue
        os_entry["index"] = str(index)
        if not entry.getCommand("savedefault"):
            default_index = grub.getOption("default", "0")
            if default_index != "saved" and int(default_index) == index:
                os_entry["default"] = "yes"
        entries.append(os_entry)

    return entries
예제 #17
0
def listEntries():
    # Main menu configuration
    grub = grubConf()
    if os.path.exists(CONF_GRUB):
        grub.parseConf(CONF_GRUB)

    entries = []
    for index, entry in enumerate(grub.entries):
        os_entry = parseGrubEntry(entry)
        if os_entry["os_type"] == "unknown":
            continue
        os_entry["index"] = str(index)
        if not entry.getCommand("savedefault"):
            default_index = grub.getOption("default", "0")
            if default_index != "saved" and int(default_index) == index:
                os_entry["default"] = "yes"
        entries.append(os_entry)

    return entries
예제 #18
0
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)
    if default_index != "saved":
        try:
            default_index = int(default_index)
        except ValueError:
            default_index = 0

    # Remove entry
    grub.removeEntry(entry)

    # Fix default index, if necessary
    if default_index != "saved":
        if index < default_index:
            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"] == getRoot():
            kernel_version = os_entry["kernel"].split("kernel-")[1]
            removeKernel(kernel_version)
예제 #19
0
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)
    if default_index != "saved":
        try:
            default_index = int(default_index)
        except ValueError:
            default_index = 0

    # Remove entry
    grub.removeEntry(entry)

    # Fix default index, if necessary
    if default_index != "saved":
        if index < default_index:
            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"] == getRoot():
            kernel_version = os_entry["kernel"].split("kernel-")[1]
            removeKernel(kernel_version)
예제 #20
0
def get_grub_conf(device_path, format_type):
    if os.path.exists(ctx.consts.tmp_mnt_dir):
        umount(ctx.consts.tmp_mnt_dir)

    grub_conf = None
    ctx.logger.debug("Mounting %s to %s to check partition" % (device_path, ctx.consts.tmp_mnt_dir))
    rc = mount(device_path, ctx.consts.tmp_mnt_dir, format_type)
    if rc:
        ctx.logger.debug("Mount failed for %s " % device_path)
    else:
        is_exist = lambda p, f: os.path.exists(os.path.join(ctx.consts.tmp_mnt_dir, p, f))

        grub_path = None
        if is_exist("boot/grub", "grub.conf") or is_exist("boot/grub", "menu.lst"):
            grub_path = "boot/grub"
        elif is_exist("grub", "grub.conf") or is_exist("grub", "menu.lst"):
            grub_path = "grub"

        if grub_path:
            ctx.logger.debug("%s device has bootloader configuration to parse." % device_path)
            menulst = os.path.join(ctx.consts.tmp_mnt_dir, grub_path, "menu.lst")
            grubconf = os.path.join(ctx.consts.tmp_mnt_dir, grub_path, "grub.conf")
            if os.path.islink(menulst):
                ctx.logger.debug("Additional grub.conf found on device %s" % device_path)
                grub_path = grubconf
            else:
                ctx.logger.debug("Additional menu.lst found on device %s" % device_path)
                grub_path = menulst

            grub_conf = grubutils.grubConf()
            grub_conf.parseConf(grub_path)
        else:
            ctx.logger.debug("%s device has not any bootloader configuration to parse." % device_path)

        umount(ctx.consts.tmp_mnt_dir)

    return grub_conf
예제 #21
0
파일: bootloader.py 프로젝트: Tayyib/uludag
    def appendLinuxSystems(self, device, formatType):
        self.grubConf = grubConf()
        self.grubConf.parseConf(os.path.join(ctx.consts.target_dir, self._conf))
        if not os.path.isdir(ctx.consts.tmp_mnt_dir):
            ctx.logger.debug("Creating temporary mount point %s for %s to check partitions" % (ctx.consts.tmp_mnt_dir, device))
            os.makedirs(ctx.consts.tmp_mnt_dir)
        else:
            yali.util.umount(ctx.consts.tmp_mnt_dir)

        try:
            ctx.logger.debug("Mounting %s to %s to check partition" % (device, ctx.consts.tmp_mnt_dir))
            yali.util.mount(device, ctx.consts.tmp_mnt_dir, formatType)
        except Exception:
            ctx.logger.debug("Mount failed for %s " % device)
            return None
        else:
            is_exist = lambda p, f: os.path.exists(os.path.join(ctx.consts.tmp_mnt_dir, p, f))

            boot_path = None
            if is_exist("boot/grub", "grub.conf") or is_exist("boot/grub", "menu.lst"):
                boot_path = "boot/grub"
            elif is_exist("grub", "grub.conf") or is_exist("grub", "menu.lst"):
                boot_path = "grub"

            if boot_path:
                ctx.logger.debug("%s device has bootloader configuration to parse." % device)
                menulst = os.path.join(ctx.consts.tmp_mnt_dir, boot_path, "menu.lst")
                grubconf = os.path.join(ctx.consts.tmp_mnt_dir, boot_path, "grub.conf")
                path = None
                if os.path.islink(menulst):
                    ctx.logger.debug("Additional grub.conf found on device %s" % device)
                    path = grubconf
                else:
                    ctx.logger.debug("Additional menu.lst found on device %s" % device)
                    path = menulst

                guestGrubConf = None
                if path and os.path.exists(path):
                    guestGrubConf = grubConf()
                    guestGrubConf.parseConf(path)
                    for entry in guestGrubConf.entries:
                        if entry.getCommand("kernel"):
                            entry.title = entry.title + " [ %s ]" % device

                            if entry.getCommand("root"):
                                rootCommand = entry.getCommand("root")
                                if rootCommand.value != "":
                                    rootCommand.value = get_partition_name(self.storage, self.storage.devicetree.getDeviceByPath(device))

                                kernelCommand = entry.getCommand("kernel")
                                if kernelCommand and rootCommand.value:
                                    if kernelCommand.value.startswith('('):
                                        kernelCommand.value = ''.join([rootCommand.value, kernelCommand.value.split(')')[1]])

                                # update device order for initrd command if already defined
                                initrdCommand = entry.getCommand("initrd")
                                if initrdCommand and rootCommand.value:
                                    if initrdCommand.value.startswith('('):
                                        initrdCommand.value = ''.join([rootCommand.value, initrdCommand.value.split(')')[1]])

                            self.grubConf.addEntry(entry)

                self.grubConf.write(os.path.join(ctx.consts.target_dir, self._conf))

            yali.util.umount(ctx.consts.tmp_mnt_dir)