Skip to content

Commit

Permalink
Rework add_grub_bootloader()
Browse files Browse the repository at this point in the history
  • Loading branch information
codefiles committed Oct 8, 2022
1 parent 5c3c131 commit 9c30d69
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions archinstall/lib/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,19 +888,45 @@ def add_grub_bootloader(self, boot_partition :Partition, root_partition :Partiti

root_fs_type = get_mount_fs_type(root_partition.filesystem)

_file = "/etc/default/grub"

try:
with open(_file, 'r') as fh:
contents = fh.readlines()
except FileNotFoundError:
log(f"Could not configure GRUB, file not found: '{_file}'.", level=logging.DEBUG)
return False

cmds = [f'rootfstype={root_fs_type}']

if real_device := self.detect_encryption(root_partition):
root_uuid = SysCommand(f"blkid -s UUID -o value {real_device.path}").decode().rstrip()
_file = "/etc/default/grub"
add_to_CMDLINE_LINUX = f"sed -i 's/GRUB_CMDLINE_LINUX=\"\"/GRUB_CMDLINE_LINUX=\"cryptdevice=UUID={root_uuid}:cryptlvm rootfstype={root_fs_type}\"/'"
enable_CRYPTODISK = "sed -i 's/#GRUB_ENABLE_CRYPTODISK=y/GRUB_ENABLE_CRYPTODISK=y/'"

log(f"Using UUID {root_uuid} of {real_device} as encrypted root identifier.", level=logging.INFO)
SysCommand(f"/usr/bin/arch-chroot {self.target} {add_to_CMDLINE_LINUX} {_file}")
SysCommand(f"/usr/bin/arch-chroot {self.target} {enable_CRYPTODISK} {_file}")
cmds.append(f'cryptdevice=UUID={root_uuid}:cryptlvm')

enable_crypt = 'GRUB_ENABLE_CRYPTODISK=y\n'

if enable_crypt not in contents:
for index, line in enumerate(contents):
if line == '#' + enable_crypt:
contents[index] = enable_crypt
break
else:
contents.append(enable_crypt)

grub_cmdline = 'GRUB_CMDLINE_LINUX'
cmdline = f'{grub_cmdline}="{" ".join(cmds)}"\n'

for index, line in enumerate(contents):
if line.split('=')[0] == grub_cmdline:
contents[index] = cmdline
break
else:
_file = "/etc/default/grub"
add_to_CMDLINE_LINUX = f"sed -i 's/GRUB_CMDLINE_LINUX=\"\"/GRUB_CMDLINE_LINUX=\"rootfstype={root_fs_type}\"/'"
SysCommand(f"/usr/bin/arch-chroot {self.target} {add_to_CMDLINE_LINUX} {_file}")
contents.append(cmdline)

with open(_file, 'w') as fh:
fh.writelines(contents)

log(f"GRUB uses {boot_partition.path} as the boot partition.", level=logging.INFO)
if has_uefi():
Expand Down

0 comments on commit 9c30d69

Please sign in to comment.