Skip to content

Commit

Permalink
use object for installation of grub2
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Jan 6, 2016
1 parent a9b8720 commit a000adc
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 93 deletions.
4 changes: 4 additions & 0 deletions src/lib/bootloader/bootloader_base.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
require "yast"
require "bootloader/sysconfig"

Yast.import "BootStorage"

module Bootloader
# Represents base for all kinds of bootloaders
class BootloaderBase
Expand All @@ -8,6 +11,7 @@ def write
end

def read
Yast::BootStorage.detect_disks
end

protected
Expand Down
200 changes: 114 additions & 86 deletions src/lib/bootloader/grub2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require "bootloader/mbr_update"
require "bootloader/device_map_dialog"
require "bootloader/stage1"
require "bootloader/grub_install"

Yast.import "Arch"
Yast.import "BootStorage"
Expand All @@ -19,12 +20,15 @@ def initialize
super

textdomain "bootloader"
@stage1 = Stage1.new
@grub_install = GrubInstall.new(efi: false)
end

# Read settings from disk
# @param [Boolean] reread boolean true to force reread settings from system
def read(reread: false)
BootStorage.device_map.propose if BootStorage.device_map.empty?
@stage1.read

super
end
Expand All @@ -33,7 +37,7 @@ def read(reread: false)
# @return [Boolean] true on success
def write
# TODO: device map write
# TODO: install_dev write
@stage1.write

# something with PMBR needed
# TODO: own class handling PBMR
Expand All @@ -47,111 +51,33 @@ def write
pmbr_setup(BootCommon.pmbr_action, *gpt_disks_devices)
end

@grub_install.execute(devices: @stage1.model.devices)

super
end

def propose
super

# TODO: propose install_device file
# TODO: propose device map
end

# FATE#303643 Enable one-click changes in bootloader proposal
#
#
def url_location_summary
# TODO: convert to using grub_devices info
Builtins.y2milestone("Prepare url summary for GRUB2")
line = "<ul>\n<li>"
if BootCommon.globals["boot_mbr"] == "true"
line << _(
"Install bootcode into MBR (<a href=\"disable_boot_mbr\">do not install</a>)"
)
else
line << _(
"Do not install bootcode into MBR (<a href=\"enable_boot_mbr\">install</a>)"
)
end
line << "</li>\n"

# do not allow to switch on boot from partition that do not support it
if BootStorage.can_boot_from_partition
line << "<li>"

# check for separated boot partition, use root otherwise
if BootStorage.BootPartitionDevice != BootStorage.RootPartitionDevice
if BootCommon.globals["boot_boot"] == "true"
line << _(
"Install bootcode into /boot partition (<a href=\"disable_boot_boot\">do not install</a>)"
)
else
line << _(
"Do not install bootcode into /boot partition (<a href=\"enable_boot_boot\">install</a>)"
)
end
else
if BootCommon.globals["boot_root"] == "true"
line << _(
"Install bootcode into \"/\" partition (<a href=\"disable_boot_root\">do not install</a>)"
)
else
line << _(
"Do not install bootcode into \"/\" partition (<a href=\"enable_boot_root\">install</a>)"
)
end
end
line << "</li>"
end
@stage1.propose

if ["boot_root", "boot_boot", "boot_mbr", "boot_extended"].none? { |loc| BootCommon.globals[loc] == "true" } &&
(BootCommon.globals["boot_custom"].nil? || BootCommon.globals["boot_custom"].empty?)
# no location chosen, so warn user that it is problem unless he is sure
msg = _("Warning: No location for bootloader stage1 selected." \
"Unless you know what you are doing please select above location.")
line << "<li>" << HTML.Colorize(msg, "red") << "</li>"
end

line << "</ul>"

# TRANSLATORS: title for list of location proposals
_("Change Location: %s") % line
# TODO: propose device map
end

# Display bootloader summary
# @return a list of summary lines
def summary
# TODO: convert to using grub_devices info
result = [
Builtins.sformat(
_("Boot Loader Type: %1"),
"GRUB2"
)
]
locations = []

if BootCommon.globals["boot_boot"] == "true"
locations << BootStorage.BootPartitionDevice + " (\"/boot\")"
end
if BootCommon.globals["boot_extended"] == "true"
# TRANSLATORS: extended is here for extended partition. Keep translation short.
locations << BootStorage.ExtendedPartitionDevice + _(" (extended)")
end
if BootCommon.globals["boot_root"] == "true"
locations << BootStorage.RootPartitionDevice + " (\"/\")"
end
if BootCommon.globals["boot_mbr"] == "true"
# TRANSLATORS: MBR is acronym for Master Boot Record, if nothing locally specific
# is used in your language, then keep it as it is.
locations << BootCommon.mbrDisk + _(" (MBR)")
end
if BootCommon.globals["boot_custom"] && !BootCommon.globals["boot_custom"].empty?
locations << BootCommon.globals["boot_custom"]
end
if !locations.empty?
locations_val = locations
if !locations_val.empty?
result << Builtins.sformat(
_("Status Location: %1"),
locations.join(", ")
locations_val.join(", ")
)
end

Expand All @@ -170,5 +96,107 @@ def summary
def name
"grub2"
end

private

def locations
locations = []
already_mentioned = []

if BootStorage.BootPartitionDevice != BootStorage.RootPartitionDevice
if @stage1.include?(BootStorage.BootPartitionDevice)
locations << BootStorage.BootPartitionDevice + " (\"/boot\")"
already_mentioned << BootStorage.BootPartitionDevice
end
else
if @stage1.include?(BootStorage.RootPartitionDevice)
locations << BootStorage.RootPartitionDevice + " (\"/\")"
already_mentioned << BootStorage.RootPartitionDevice
end
end
if @stage1.include?(BootStorage.ExtendedPartitionDevice)
# TRANSLATORS: extended is here for extended partition. Keep translation short.
locations << BootStorage.ExtendedPartitionDevice + _(" (extended)")
already_mentioned << BootStorage.ExtendedPartitionDevice
end
if @stage1.include?(BootCommon.mbrDisk)
# TRANSLATORS: MBR is acronym for Master Boot Record, if nothing locally specific
# is used in your language, then keep it as it is.
locations << BootCommon.mbrDisk + _(" (MBR)")
already_mentioned << BootCommon.mbrDisk
end
if !(@stage1.model.devices - already_mentioned).empty?
locations << (@stage1.model.devices - already_mentioned)
end

locations
end

def mbr_line
if @stage1.include?(BootCommon.mbrDisk)
_(
"Install bootcode into MBR (<a href=\"disable_boot_mbr\">do not install</a>)"
)
else
_(
"Do not install bootcode into MBR (<a href=\"enable_boot_mbr\">install</a>)"
)
end
end

def partition_line
# check for separated boot partition, use root otherwise
if BootStorage.BootPartitionDevice != BootStorage.RootPartitionDevice
if @stage1.include?(BootStorage.BootPartitionDevice)
_(
"Install bootcode into /boot partition (<a href=\"disable_boot_boot\">do not install</a>)"
)
else
_(
"Do not install bootcode into /boot partition (<a href=\"enable_boot_boot\">install</a>)"
)
end
else
if @stage1.include?(BootStorage.RootPartitionDevice)
_(
"Install bootcode into \"/\" partition (<a href=\"disable_boot_root\">do not install</a>)"
)
else
_(
"Do not install bootcode into \"/\" partition (<a href=\"enable_boot_root\">install</a>)"
)
end
end
end

# FATE#303643 Enable one-click changes in bootloader proposal
#
#
def url_location_summary
# TODO: convert to using grub_devices info
log.info "Prepare url summary for GRUB2"
line = "<ul>\n<li>"
line << mbr_line
line << "</li>\n"

# do not allow to switch on boot from partition that do not support it
if BootStorage.can_boot_from_partition
line << "<li>"
line << partition_line
line << "</li>"
end

if @stage1.model.devices.empty?
# no location chosen, so warn user that it is problem unless he is sure
msg = _("Warning: No location for bootloader stage1 selected." \
"Unless you know what you are doing please select above location.")
line << "<li>" << HTML.Colorize(msg, "red") << "</li>"
end

line << "</ul>"

# TRANSLATORS: title for list of location proposals
_("Change Location: %s") % line
end
end
end
5 changes: 5 additions & 0 deletions src/lib/bootloader/grub2efi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "yast"
require "bootloader/grub2base"
require "bootloader/grub_install"
require "bootloader/sysconfig"

module Bootloader
Expand All @@ -13,6 +14,8 @@ def initialize
super

textdomain "bootloader"

@grub_install = GrubInstall.new(efi: true)
end

# Read settings from disk
Expand Down Expand Up @@ -40,6 +43,8 @@ def write

super

@grub_install.execute(secure_boot: @secure_boot)

ret
end

Expand Down
11 changes: 5 additions & 6 deletions src/lib/bootloader/grub_install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
module Bootloader
# Wraps grub install script for easier usage.
class GrubInstall
def initialize(efi: false, secure_boot: false)
def initialize(efi: false)
@efi = efi
@secure_boot = secure_boot

raise "cannot have secure boot without efi" if secure_boot && !efi
end

def execute(devices: nil)
def execute(devices: nil, secure_boot: false)
raise "cannot have secure boot without efi" if secure_boot && !@efi

cmd = []
if @secure_boot
if secure_boot
cmd << "/usr/sbin/shim-install" << "--config-file=/boot/grub2/grub.cfg"
else
cmd << "/usr/sbin/grub2-install" << "--target=#{target}"
Expand Down
10 changes: 9 additions & 1 deletion src/lib/bootloader/stage1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Bootloader
# and "generic_mbr" which is related to stage1 code
class Stage1
include Yast::Logger
attr_reader :model

def initialize
Yast.import "Arch"
Expand All @@ -26,7 +27,14 @@ def read

def write
@model.write
# TODO: really write it with grub2-install and parted
end

def include?(dev)
kernel_dev = Bootloader::UdevMapping.to_kernel_device(dev)

@model.devices.any? do |map_dev|
kernel_dev == Bootloader::UdevMapping.to_kernel_device(map_dev)
end
end

# Propose and set Stage1 location.
Expand Down

0 comments on commit a000adc

Please sign in to comment.