Beispiel #1
0
def parseGrubEntry(entry):
    os_entry = {
        "os_type": "unknown",
        "title": entry.title,
    }
    for command in entry.commands:
        key = command.key
        value = command.value

        if key in ["root", "rootnoverify"]:
            os_entry["root"] = linuxAddress(value)

        elif key == "uuid":
            os_entry["uuid"] = value

        elif key == "initrd":
            os_entry["initrd"] = value
            if os_entry["initrd"].startswith("("):
                os_entry["initrd"] = os_entry["initrd"].split(")", 1)[1]

        elif key == "kernel":
            try:
                kernel, options = value.split(" ", 1)
                os_entry["kernel"] = kernel
                os_entry["options"] = options
            except ValueError:
                os_entry["kernel"] = value
            os_entry["os_type"] = "linux"
            if os_entry["kernel"].startswith("("):
                root, kernel = os_entry["kernel"].split(")", 1)
                os_entry["root"] = linuxAddress(root + ")")
                os_entry["kernel"] = kernel
            if os_entry["kernel"] == "/boot/xen.gz":
                os_entry["os_type"] = "xen"
            elif os_entry["kernel"] == "/boot/memtest":
                os_entry["os_type"] = "memtest"
                del os_entry["kernel"]

        elif key in ["chainloader", "makeactive"]:
            os_entry["os_type"] = "windows"

        elif key == "savedefault":
            os_entry["default"] = "saved"

        elif key == "module" and os_entry["os_type"] == "xen":
            if value.startswith("("):
                value = value.split(")", 1)[1]
            if value.startswith("/boot/kernel"):
                if " " in value:
                    os_entry["kernel"], os_entry["options"] = value.split(
                        " ", 1)
                else:
                    os_entry["kernel"] = value
            elif value.startswith("/boot/init"):
                os_entry["initrd"] = value
    return os_entry
Beispiel #2
0
def parseGrubEntry(entry):
    os_entry = {
        "os_type": "unknown",
        "title": entry.title,
    }
    for command in entry.commands:
        key = command.key
        value = command.value

        if key in ["root", "rootnoverify"]:
            os_entry["root"] = linuxAddress(value)

        elif key == "uuid":
            os_entry["uuid"] = value

        elif key == "initrd":
            os_entry["initrd"] = value
            if os_entry["initrd"].startswith("("):
                os_entry["initrd"] = os_entry["initrd"].split(")", 1)[1]

        elif key == "kernel":
            try:
                kernel, options = value.split(" ", 1)
                os_entry["kernel"] = kernel
                os_entry["options"] = options
            except ValueError:
                os_entry["kernel"] = value
            os_entry["os_type"] = "linux"
            if os_entry["kernel"].startswith("("):
                root, kernel = os_entry["kernel"].split(")", 1)
                os_entry["root"] = linuxAddress(root + ")")
                os_entry["kernel"] = kernel
            if os_entry["kernel"] == "/boot/xen.gz":
                os_entry["os_type"] = "xen"
            elif os_entry["kernel"] == "/boot/memtest":
                os_entry["os_type"] = "memtest"
                del os_entry["kernel"]

        elif key in ["chainloader", "makeactive"]:
            os_entry["os_type"] = "windows"

        elif key == "savedefault":
            os_entry["default"] = "saved"

        elif key == "module" and os_entry["os_type"] == "xen":
            if value.startswith("("):
                value = value.split(")", 1)[1]
            if value.startswith("/boot/kernel"):
                if " " in value:
                    os_entry["kernel"], os_entry["options"] = value.split(" ", 1)
                else:
                    os_entry["kernel"] = value
            elif value.startswith("/boot/init"):
                os_entry["initrd"] = value
    return os_entry
Beispiel #3
0
def groupEntries(grub, root):
    """
        Groups entries in GRUB configuration.

        Arguments:
            grub: GRUB configuration instance
            root: Root device
        Returns:
            Entries grouped by kernel and version
            e.g.:
            ({'pardus-pae': [4], 'other': [1, 3], 'pardus': [0, 2]}, {'pardus-pae': {4: '2.6.30.9-128'}, 'pardus': {0: '2.6.30.9-128', 2: '2.6.30.9-129'}})
    """

    groups = {}
    versions = {}

    def __addItem(_key, _index, _version=None):
        if _key not in groups:
            groups[_key] = []
        groups[_key].append(_index)
        if _version:
            if _key not in versions:
                versions[_key] = {}
            versions[_key][_index] = _version

    for i, title in enumerate(grub.listEntries()):
        # read entry
        entry = grub.getEntry(i)
        e_kernel = entry.getCommand("kernel")
        e_root = entry.getCommand("root")
        e_uuid = entry.getCommand("uuid")
        if e_kernel and "kernel-" in e_kernel.value and (e_root or e_uuid):
            # find linux device address
            rootDev = None
            if e_root:
                rootDev = linuxAddress(e_root.value)
            elif e_uuid:
                rootDev = getDeviceByUUID(e_uuid.value)
            # put all entries on other devices to a "other" list
            if rootDev != root:
                __addItem("other", i)
                continue
            # parse kernel version too see if it's Pardus kernel
            version = e_kernel.value.split("kernel-")[1].split(" ")[0]
            try:
                version, suffix = parseVersion("kernel-%s" % version)
            except ValueError:
                __addItem("other", i)
                continue
            if suffix:
                __addItem("pardus%s" % suffix, i, version)
            else:
                __addItem("pardus", i, version)
        else:
            # put all unknown entries to "other" list
            __addItem("other", i)

    return groups, versions
Beispiel #4
0
def groupEntries(grub, root):
    """
        Groups entries in GRUB configuration.

        Arguments:
            grub: GRUB configuration instance
            root: Root device
        Returns:
            Entries grouped by kernel and version
            e.g.:
            ({'pardus-pae': [4], 'other': [1, 3], 'pardus': [0, 2]}, {'pardus-pae': {4: '2.6.30.9-128'}, 'pardus': {0: '2.6.30.9-128', 2: '2.6.30.9-129'}})
    """

    groups = {}
    versions = {}

    def __addItem(_key, _index, _version=None):
        if _key not in groups:
            groups[_key] = []
        groups[_key].append(_index)
        if _version:
            if _key not in versions:
                versions[_key] = {}
            versions[_key][_index] = _version

    for i, title in enumerate(grub.listEntries()):
        # read entry
        entry = grub.getEntry(i)
        e_kernel = entry.getCommand("kernel")
        e_root = entry.getCommand("root")
        e_uuid = entry.getCommand("uuid")
        if e_kernel and "kernel-" in e_kernel.value and (e_root or e_uuid):
            # find linux device address
            rootDev = None
            if e_root:
                rootDev = linuxAddress(e_root.value)
            elif e_uuid:
                rootDev = getDeviceByUUID(e_uuid.value)
            # put all entries on other devices to a "other" list
            if rootDev != root:
                __addItem("other", i)
                continue
            # parse kernel version too see if it's Pardus kernel
            version = e_kernel.value.split("kernel-")[1].split(" ")[0]
            try:
                version, suffix = parseVersion("kernel-%s" % version)
            except (ValueError, TypeError):
                __addItem("other", i)
                continue
            if suffix:
                __addItem("pardus%s" % suffix, i, version)
            else:
                __addItem("pardus", i, version)
        else:
            # put all unknown entries to "other" list
            __addItem("other", i)

    return groups, versions