Skip to content

Commit

Permalink
[modules] bootloader: workaround grub-install sillynesses when instal…
Browse files Browse the repository at this point in the history
…ling on Macs

Signed-off-by: Eugenio Paolantonio (g7) <[email protected]>
  • Loading branch information
g7 committed Aug 17, 2016
1 parent e1e9d28 commit d2016a3
Showing 1 changed file with 58 additions and 4 deletions.
62 changes: 58 additions & 4 deletions linstaller/modules/bootloader/inst/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import os, sys, fileinput
import subprocess

import shutil

## Why are we using the "manual" install method instead of debconf?
## First, we want to keep some sort of compatibility with non-Debian
## distros.
Expand Down Expand Up @@ -53,7 +55,20 @@ def grub_pkgs_install(self):
# This workaround fixes that.
self.moduleclass.cache.change_rootdir("/")
self.moduleclass.cache = None


def get_efi_target_is_mac(self):
""" Returns True if the target /boot/efi partition is HFS+. """

# Get changed.
changed = self.moduleclass.modules_settings["partdisks"]["changed"]

for key, value in changed.items():
if value["changes"].get("useas", None) == "/boot/efi":
# Found our partition
return (value["obj"].fileSystem.type == "hfs+")

return False

def grub_install(self):
""" Installs grub. """

Expand All @@ -62,6 +77,8 @@ def grub_install(self):

verbose("Selected location: %s" % target)

efi_target_mac = False

if "uefidetect.inst" in self.moduleclass.modules_settings and self.moduleclass.modules_settings["uefidetect.inst"]["uefi"] == True:
# UEFI (need blank grub-install)

Expand All @@ -73,7 +90,15 @@ def grub_install(self):
arch = subprocess.check_output(["/usr/bin/dpkg-architecture", "-qDEB_BUILD_ARCH"]).strip()
else:
arch = "amd64"


# If the target /boot/efi partition is hfs+, it's highly
# probable that the system is a Mac.
try:
efi_target_mac = self.get_efi_target_is_mac()
except:
# Don't bother
pass

location = ""
args = "--target=%s" % UEFIplatforms[arch]
elif target == "root":
Expand Down Expand Up @@ -126,9 +151,36 @@ def grub_install(self):
db = debconf.DebconfCommunicator("linstaller")
db.set("grub-pc/install_devices", location)
db.shutdown() # Exit.



efidir = None
if efi_target_mac:
# grub-install hardcodes the installation to <efi-directory>/EFI/<vendor>,
# which is not supported on Macs. We will move the contents later,
# just obtain the installation path for now
efidir = os.path.join("/boot/efi/EFI", self.moduleclass.settings["grub_bootloader_id"])

if not os.path.exists(efidir):
os.makedirs(efidir)

# Create an empty mach_kernel file otherwise grub-install will
# fail
with open(os.path.join(efidir, "mach_kernel"), "w") as f:
f.write("Generated by linstaller\n")

# Ensure we install to the specified dir
args = "%s --efi-directory=/boot/efi --bootloader-id=%s" % (args, self.moduleclass.settings["grub_bootloader_id"])

m.sexec("grub-install %(args)s '%(location)s'" % {"args":args,"location":location})

if efi_target_mac and efidir:
# Finally move everything where it belongs
shutil.move(os.path.join(efidir, "mach_kernel"), "/boot/efi")
shutil.move(os.path.join(efidir, "System"), "/boot/efi")

# Copy icon if any
icon = self.moduleclass.settings["efi_volumeicon"]
if icon and os.path.exists(icon):
shutil.copy(icon, "/boot/efi/.VolumeIcon.icns")

should_hide_menu = self.moduleclass.modules_settings["bootloader"]["should_hide_menu"]
# Let's see if we can hide the GRUB menu...
Expand Down Expand Up @@ -262,3 +314,5 @@ def seedpre(self):

self.cache("grub_cmdline_linux_default")
self.cache("grub_cmdline_linux")
self.cache("grub_bootloader_id", "grub")
self.cache("efi_volumeicon", None)

0 comments on commit d2016a3

Please sign in to comment.