2016-03-31 11:09:16 -07:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
|
|
import os
|
2016-05-09 20:40:38 +00:00
|
|
|
import re
|
2016-03-31 12:18:00 -07:00
|
|
|
import string
|
2016-05-09 20:40:38 +00:00
|
|
|
import subprocess
|
2016-03-31 11:09:16 -07:00
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GrubUpdater(object):
|
|
|
|
|
|
2016-05-09 20:40:38 +00:00
|
|
|
_VOLUME_ID_REGEX = re.compile(b'^Volume id: (?P<volume_id>.+)$', re.MULTILINE)
|
2016-03-31 12:18:00 -07:00
|
|
|
_HOTKEYS = string.digits + string.ascii_letters
|
|
|
|
|
|
2016-03-31 11:09:16 -07:00
|
|
|
def __init__(self, image_dir, boot_dir):
|
|
|
|
|
self._image_dir = image_dir
|
|
|
|
|
self._boot_dir = boot_dir
|
|
|
|
|
|
|
|
|
|
assert self._image_dir.startswith(self._boot_dir)
|
|
|
|
|
|
|
|
|
|
self._image_path = '/' + os.path.relpath(self._image_dir, self._boot_dir)
|
|
|
|
|
|
2016-05-09 20:40:38 +00:00
|
|
|
def _GetVolumeID(self, path):
|
|
|
|
|
isoinfo = subprocess.check_output([
|
|
|
|
|
'isoinfo',
|
|
|
|
|
'-d',
|
|
|
|
|
'-i', path,
|
|
|
|
|
])
|
|
|
|
|
match = self._VOLUME_ID_REGEX.search(isoinfo)
|
|
|
|
|
return match.group('volume_id').decode('ascii')
|
|
|
|
|
|
2016-03-31 11:09:16 -07:00
|
|
|
def Update(self):
|
2016-03-31 11:14:58 -07:00
|
|
|
current = os.readlink(os.path.join(self._image_dir, 'current'))
|
|
|
|
|
|
2016-03-31 11:12:02 -07:00
|
|
|
sys.stdout.write("""
|
|
|
|
|
set timeout=5
|
2016-03-31 11:14:58 -07:00
|
|
|
set default=%(default_image_filename)s
|
|
|
|
|
""" % {
|
|
|
|
|
'default_image_filename': os.path.basename(current),
|
|
|
|
|
})
|
2016-03-31 11:12:02 -07:00
|
|
|
|
|
|
|
|
files = []
|
2016-03-31 11:09:16 -07:00
|
|
|
for filename in os.listdir(self._image_dir):
|
|
|
|
|
if not filename.endswith('.iso'):
|
|
|
|
|
continue
|
2016-03-31 11:12:02 -07:00
|
|
|
files.append(filename)
|
|
|
|
|
|
2016-03-31 12:18:00 -07:00
|
|
|
for i, filename in enumerate(sorted(files, reverse=True)):
|
2016-03-31 11:09:16 -07:00
|
|
|
sys.stdout.write("""
|
2016-05-09 20:40:38 +00:00
|
|
|
menuentry "%(image_filename)s (%(volume_id)s)" --hotkey=%(hotkey)s {
|
2016-03-31 11:09:16 -07:00
|
|
|
search --no-floppy --file --set=root %(image_path)s/%(image_filename)s
|
|
|
|
|
iso_path="%(image_path)s/%(image_filename)s"
|
|
|
|
|
export iso_path
|
|
|
|
|
loopback loop "%(image_path)s/%(image_filename)s"
|
|
|
|
|
set root=(loop)
|
|
|
|
|
configfile /boot/grub/loopback.cfg
|
|
|
|
|
}
|
|
|
|
|
""" % {
|
|
|
|
|
'image_filename': filename,
|
|
|
|
|
'image_path': self._image_path,
|
2016-03-31 12:18:00 -07:00
|
|
|
'hotkey': self._HOTKEYS[i],
|
2016-05-09 20:40:38 +00:00
|
|
|
'volume_id': self._GetVolumeID(os.path.join(self._image_dir, filename)),
|
2016-03-31 11:09:16 -07:00
|
|
|
})
|