'Remove any kernel image and associated files (vmlinux, system.map,
modules) for any image found in the boot directory that is not
referenced by entries in the bootloader configuration.
:param boot_dir: boot directory path string, default \'/boot\''

def cleanup_kernels(self, boot_dir='/boot'):

    boot_info = self.bootloader.get_entries()
    used_kernver = []
    for boot in boot_info.itervalues():
        k = os.path.basename(boot['kernel'])[len('vmlinuz-'):]
        if (k not in used_kernver):
            used_kernver.append(k)
    vmlinuz_prefix = os.path.join(boot_dir, 'vmlinuz-')
    all_vmlinuz = self.list_files_glob((vmlinuz_prefix + '*'))
    used_vmlinuz = self.symlink_closure(((vmlinuz_prefix + kernver) for kernver in used_kernver))
    unused_vmlinuz = (set(all_vmlinuz) - set(used_vmlinuz))
    vmlinux_prefix = os.path.join(boot_dir, 'vmlinux-')
    all_vmlinux = self.list_files_glob((vmlinux_prefix + '*'))
    used_vmlinux = self.symlink_closure(((vmlinux_prefix + kernver) for kernver in used_kernver))
    unused_vmlinux = (set(all_vmlinux) - set(used_vmlinux))
    systemmap_prefix = os.path.join(boot_dir, 'System.map-')
    all_system_map = self.list_files_glob((systemmap_prefix + '*'))
    used_system_map = self.symlink_closure(((systemmap_prefix + kernver) for kernver in used_kernver))
    unused_system_map = (set(all_system_map) - set(used_system_map))
    modules_prefix = '/lib/modules/'
    all_moddirs = [mod_dir for mod_dir in self.list_files_glob((modules_prefix + '*')) if re.match((modules_prefix + '\\d+\\.\\d+\\.\\d+.*'), mod_dir)]
    used_moddirs = self.symlink_closure(((modules_prefix + kernver) for kernver in used_kernver))
    unused_moddirs = (set(all_moddirs) - set(used_moddirs))
    for vmlinuz in unused_vmlinuz:
        rpm = self.run('rpm    -qf', args=(vmlinuz,), ignore_status=True, timeout=120)
        if (rpm.exit_status == 0):
            packages = set((line.strip() for line in rpm.stdout.splitlines()))
            for package in packages:
                self.run('rpm    -e', args=(package,), ignore_status=True, timeout=120)
        self.run('rm    -f', args=(vmlinuz,), ignore_status=True, timeout=120)
    for f in (unused_vmlinux | unused_system_map):
        self.run('rm    -f', args=(f,), ignore_status=True, timeout=120)
    for moddir in unused_moddirs:
        self.run('rm    -fr', args=(moddir,), ignore_status=True)
