Skip to content

Instantly share code, notes, and snippets.

@martin-ueding
Created May 3, 2020 15:26
Show Gist options
  • Save martin-ueding/55c51969d9551b6cbfa37eb7ec8b05f3 to your computer and use it in GitHub Desktop.
Save martin-ueding/55c51969d9551b6cbfa37eb7ec8b05f3 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Copyright © 2016 Martin Ueding <mu@martin-ueding.de>
import argparse
import re
import subprocess
PATTERN = re.compile(r'Boot([0-9A-F]{4})\*?\s+([^\t]+)\s(\S+)')
def main():
options = _parse_args()
lines = subprocess.check_output(['efibootmgr', '-v']).decode().split('\n')
entries = {}
for line in lines:
m = PATTERN.match(line)
if m:
number, kind, details = m.groups()
if kind not in entries:
entries[kind] = []
entries[kind].append(number)
if options.verbose:
for kind, numbers in sorted(entries.items()):
count = len(numbers)
print('{}, ({} {})'.format(kind, len(numbers), 'entry' if count == 1 else 'entries'))
print(sorted(numbers))
print()
if options.delete is not None:
to_delete = []
for name in options.delete:
if name in entries:
to_delete += entries[name]
if len(to_delete) > 50:
if options.verbose:
print()
print('The following ones will be deleted:')
print(', '.join(sorted(to_delete)))
for number in to_delete:
print('Deleting {} …'.format(number))
subprocess.check_call(['efibootmgr', '-b', number, '-B'])
def _parse_args():
'''
Parses the command line arguments.
:return: Namespace with arguments.
:rtype: Namespace
'''
parser = argparse.ArgumentParser(description='')
parser.add_argument('--delete', nargs='*', default=['Fedora', 'kubuntu'])
parser.add_argument('--verbose', action='store_true')
options = parser.parse_args()
return options
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment