-
Notifications
You must be signed in to change notification settings - Fork 22
/
sections.rb
79 lines (59 loc) · 2.3 KB
/
sections.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
require "yast"
require "yast2/execute"
Yast.import "Stage"
module Bootloader
# Represents available sections and handling of default boot entry
class Sections
include Yast::Logger
attr_reader :all
# @param [CFA::Grub2::GrubCfg, nil] grub_cfg - loaded parsed grub cfg tree
# or nil if not available yet
def initialize(grub_cfg = nil)
@data = grub_cfg ? grub_cfg.boot_entries : []
@all = @data.map { |e| e[:title] }
end
# @return [String] name of default section
def default
return @default if @default
return @default = "" if Yast::Stage.initial
default_path = read_default_path
@default = default_path ? path_to_title(default_path) : all.first
end
# Sets default section internally
# @note to write it to system use #write later
def default=(value)
log.info "set new default to '#{value.inspect}'"
# empty value mean no default specified
raise "Unknown value #{value.inspect}" if !all.empty? && !all.include?(value) && !value.empty?
@default = value
end
# writes default to system making it persistent
def write
return if default.empty?
Yast::Execute.on_target("/usr/sbin/grub2-set-default", title_to_path(default))
end
private
# @return [String, nil] return default boot path as string or nil if not set
# or something goes wrong
# @note shows error popup if calling grub2-editenv failed
def read_default_path
# Execute.on_target can return nil if call failed. It shows users error popup, but bootloader
# can continue with empty default section
saved = Yast::Execute.on_target("/usr/bin/grub2-editenv", "list", stdout: :capture) || ""
saved_line = saved.lines.grep(/saved_entry=/).first
saved_line ? saved_line[/saved_entry=(.*)$/, 1] : nil
end
# @return [String] convert grub boot path to title that can be displayed. If
# entry not found, then return argument
def path_to_title(path)
entry = @data.find { |e| e[:path] == path }
entry ? entry[:title] : path
end
# @return [String] convert displayable title to grub boot path. If
# entry not found, then return argument
def title_to_path(title)
entry = @data.find { |e| e[:title] == title }
entry ? entry[:path] : title
end
end
end