Skip to content

Commit

Permalink
Custom device path validation / conversion. bnc#1092950
Browse files Browse the repository at this point in the history
  • Loading branch information
mchf committed Oct 8, 2021
1 parent 88f7ff4 commit a4c6b41
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
47 changes: 47 additions & 0 deletions src/lib/bootloader/device_path.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require "yast"

module Bootloader
# Class for device path
#
# @example device path can be defined explicitly
# DevicePath.new("/devs/sda")
# @example definition by UUID is translated to device path
# dev = DevicePath.new("UUID=\"0000-00-00\"")
# dev.path -> "/dev/disk/by-uuid/0000-00-00"
class DevicePath
attr_reader :path

# Performs initialization
#
# @param dev [<String>] either a path like /dev/sda or special string for uuid or label
def initialize(dev)
@path = if dev_by_uuid?(dev)
# if defined by uuid, convert it
File.realpath(dev.gsub(/UUID="([-a-zA-Z0-9]*)"/, '/dev/disk/by-uuid/\1'))
elsif dev_by_label?(dev)
# as well for label
File.realpath(dev.gsub(/LABEL="(.*)"/, '/dev/disk/by-label/\1'))
else
# add it exactly (but whitespaces) as specified by the user
dev.strip
end
end

# @return [Boolean] true if the @path exists in the system
def exists?
File.exists?(path)
end

alias :valid? :exists?

private

def dev_by_uuid?(dev)
dev =~ /UUID=".+"/
end

def dev_by_label?(dev)
dev =~ /LABEL=".+"/
end
end
end
18 changes: 15 additions & 3 deletions src/lib/bootloader/grub2_widgets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require "bootloader/cpu_mitigations"
require "bootloader/systeminfo"
require "bootloader/os_prober"
require "bootloader/device_path"
require "cfa/matcher"

Yast.import "BootStorage"
Expand Down Expand Up @@ -880,16 +881,20 @@ def store

devs = Yast::UI.QueryWidget(:custom_list, :Value)
devs.split(",").each do |dev|
# Add it exactly as specified by the user
stage1.add_device(dev.strip)
stage1.add_device(DevicePath.new(dev).path)
end
end

def validate
if Yast::UI.QueryWidget(:custom, :Value)
devs = Yast::UI.QueryWidget(:custom_list, :Value)
if devs.strip.empty?
Yast::Report.Error(_("Custom boot device have to be specied if checked"))
Yast::Report.Error(_("Custom boot device has to be specified if checked"))
Yast::UI.SetFocus(Id(:custom_list))
return false
end
if !valid_custom_devices?(devs)
Yast::Report.Error(_("A custom device is wrong"))
Yast::UI.SetFocus(Id(:custom_list))
return false
end
Expand All @@ -911,6 +916,13 @@ def init_custom_devices(custom_devices)
end
end

# Validates list of devices
#
# @param devs_list[String] comma separated list of device definitions
def valid_custom_devices?(devs_list)
devs_list.split(",").all? { |d| DevicePath.new(d).valid? }
end

def locations
@locations ||= stage1.available_locations
end
Expand Down

0 comments on commit a4c6b41

Please sign in to comment.