]>
Commit | Line | Data |
---|---|---|
a65627a8 | 1 | # |
d7274593 | 2 | # Copyright 2019-2022 Canonical Ltd. |
a65627a8 TL |
3 | # Authors: |
4 | # - dann frazier <dann.frazier@canonical.com> | |
5 | # | |
6 | # This program is free software: you can redistribute it and/or modify it | |
7 | # under the terms of the GNU General Public License version 3, as published | |
8 | # by the Free Software Foundation. | |
9 | # | |
10 | # This program is distributed in the hope that it will be useful, but WITHOUT | |
11 | # ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, | |
12 | # SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | # General Public License for more details. | |
14 | # | |
15 | # You should have received a copy of the GNU General Public License along with | |
16 | # this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | # | |
18 | ||
19 | import os | |
20 | import shutil | |
21 | import subprocess | |
22 | import tempfile | |
23 | ||
24 | ||
25 | class FatFsImage: | |
26 | def __init__(self, size_in_mb): | |
27 | with tempfile.NamedTemporaryFile(delete=False) as f: | |
28 | self.path = f.name | |
29 | ||
30 | subprocess.check_call( | |
31 | [ | |
32 | 'dd', 'if=/dev/zero', 'of=%s' % (self.path), | |
33 | 'count=0', 'bs=1M', 'seek=%d' % (size_in_mb), 'status=none' | |
34 | ] | |
35 | ) | |
36 | new_env = os.environ.copy() | |
37 | new_env['PATH'] = f"{os.environ['PATH']}:/sbin" | |
38 | subprocess.check_call(['mkdosfs', '-F', '32', self.path], env=new_env) | |
39 | ||
40 | def __del__(self): | |
41 | os.unlink(self.path) | |
42 | ||
43 | def mkdir(self, dir): | |
44 | subprocess.run(['mmd', '-i', self.path, dir]) | |
45 | ||
46 | def makedirs(self, dir): | |
47 | dirs = dir.split(os.path.sep) | |
48 | for dir_idx in range(1, len(dirs)+1): | |
49 | next_dir = os.path.sep.join(dirs[:dir_idx]) | |
50 | self.mkdir(next_dir) | |
51 | ||
52 | def insert_file(self, src, dest): | |
53 | subprocess.check_call( | |
54 | [ | |
55 | 'mcopy', '-i', self.path, src, '::%s' % (dest) | |
56 | ] | |
57 | ) | |
58 | ||
59 | ||
60 | class EfiBootableIsoImage: | |
61 | def __init__(self, eltorito_img): | |
62 | with tempfile.TemporaryDirectory() as iso_root: | |
63 | eltorito_iso_root = 'boot' | |
64 | eltorito_iso_path = os.path.join(eltorito_iso_root, 'efi.img') | |
65 | eltorito_local_root = os.path.join(iso_root, eltorito_iso_root) | |
66 | eltorito_local_path = os.path.join(iso_root, eltorito_iso_path) | |
67 | ||
68 | os.makedirs(eltorito_local_root) | |
69 | shutil.copyfile(eltorito_img.path, eltorito_local_path) | |
70 | ||
71 | with tempfile.NamedTemporaryFile(delete=False) as f: | |
72 | self.path = f.name | |
73 | ||
74 | subprocess.check_call( | |
75 | [ | |
76 | 'xorriso', '-as', 'mkisofs', '-J', '-l', | |
77 | '-c', 'boot/boot.cat', | |
78 | '-partition_offset', '16', '-append_partition', '2', | |
79 | '0xef', eltorito_local_path, | |
80 | '-e', '--interval:appended_partition_2:all::', | |
81 | '-no-emul-boot', '-o', self.path, iso_root | |
82 | ] | |
83 | ) | |
84 | ||
85 | def __del__(self): | |
86 | os.unlink(self.path) | |
87 | ||
88 | ||
89 | class GrubShellBootableIsoImage(EfiBootableIsoImage): | |
d7274593 | 90 | def __init__(self, efi_arch, shim_path, grub_path): |
a65627a8 TL |
91 | efi_img = FatFsImage(64) |
92 | efi_img.makedirs(os.path.join('EFI', 'BOOT')) | |
93 | removable_media_path = os.path.join( | |
94 | 'EFI', 'BOOT', 'BOOT%s.EFI' % (efi_arch.upper()) | |
95 | ) | |
a65627a8 TL |
96 | grub_dest = os.path.join( |
97 | 'EFI', 'BOOT', 'GRUB%s.EFI' % (efi_arch.upper()) | |
98 | ) | |
d7274593 TL |
99 | efi_img.insert_file(shim_path, removable_media_path) |
100 | efi_img.insert_file(grub_path, grub_dest) | |
a65627a8 | 101 | super().__init__(efi_img) |