Skip to content

Commit

Permalink
use pbl script to communicate
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Oct 24, 2013
1 parent e82cf91 commit 1b78c91
Showing 1 changed file with 154 additions and 71 deletions.
225 changes: 154 additions & 71 deletions src/include/bootloader/routines/lib_iface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
#
# $Id$
#

require "tempfile"
require "yaml"

module Yast
module BootloaderRoutinesLibIfaceInclude
def initialize_bootloader_routines_lib_iface(include_target)
Expand All @@ -31,6 +35,23 @@ def initialize_bootloader_routines_lib_iface(include_target)
@library_initialized = nil
end

STATE_FILE = "/var/lib/YaST2/pbl-state"

def tmp_yaml_file(data=nil)
file = Tempfile.new("y2-yamldata")
file.write YAML.dump(data) if data
file.close

return file
end

def run_pbl_yaml(*args)
cmd = "pbl-yaml --state=#{STATE_FILE} "
cmd << args.map{|e| "'#{e}'"}.join(" ")

SCR.Execute(path(".target.bash"), cmd)
end

# Retrieve the data for perl-Bootloader library from Storage module
# and pass it along
# @return nothing
Expand All @@ -51,24 +72,20 @@ def SetDiskInfo
BootStorage.multipath_mapping
)

System::Bootloader_API.setMountPoints(
Convert.convert(
BootStorage.mountpoints,
:from => "map <string, any>",
:to => "map <string, string>"
)
)
System::Bootloader_API.setPartitions(
Convert.convert(
BootStorage.partinfo,
:from => "list <list>",
:to => "list <list <string>>"
)
)
System::Bootloader_API.setMDArrays(BootStorage.md_info)
mp_data = tmp_yaml_file(BootStorage.mountpoints)
part_data = tmp_yaml_file(BootStorage.partinfo)
md_data = tmp_yaml_file(BootStorage.md_info)

run_pbl_yaml "DefineMountPoints(#{mp_data.path})",
"DefinePartitions(#{part_data.path})",
"DefineMDArrays(#{md_data.path})"
DefineMultipath(BootStorage.multipath_mapping)

nil
ensure
mp_data.unlink
part_data.unlink
md_data.unlink
end

# Initialize the bootloader library
Expand All @@ -81,17 +98,23 @@ def InitializeLibrary(force, loader)
BootStorage.InitMapDevices
Builtins.y2milestone("Initializing lib for %1", loader)
architecture = BootArch.StrArch
System::Bootloader_API.setLoaderType(loader, architecture)
out = System::Bootloader_API.defineUdevMapping(BootStorage.all_devices)
if out == nil
Builtins.y2error("perl-Bootloader library was not initialized")
end
loader_data = tmp_yaml_file(loader)
arch_data = tmp_yaml_file( architecture)
udev_data = tmp_yaml_file(BootStorage.all_devices)

run_pbl_yaml "SetLoaderType(#{loader_data.path},#{arch_data.path})",
"DefineUdevMapping(#{udev_data.path})"

Builtins.y2milestone("Putting partitioning into library")
# pass all needed disk/partition information to library
SetDiskInfo()
Builtins.y2milestone("Library initialization finished")
@library_initialized = loader
true
ensure
loader_data.unlink if loader_data
arch_data.unlink if arch_data
udev_data.unlink if udev_data
end

# Set boot loader sections
Expand All @@ -111,22 +134,30 @@ def SetSections(sections)
deep_copy(s)
end
Builtins.y2milestone("Storing bootloader sections %1", sections)
ret = System::Bootloader_API.setSections(sections)
Builtins.y2error("Storing bootloader sections failed") if !ret
ret
sections_data = tmp_yaml_file(sections)
run_pbl_yaml "SetSections(#{sections_data.path})"

true
ensure
sections_data.unlink
end

# Get boot loader sections
# @return a list of all loader sections (as maps)
def GetSections
sections_data = tmp_yaml_file
Builtins.y2milestone("Reading bootloader sections")
sects = System::Bootloader_API.getSections
run_pbl_yaml "#{sections_data.path}=GetSections()"
sects = YAML.load File.read(sections_data.path)
if sects == nil
Builtins.y2error("Reading sections failed")
return []
end
Builtins.y2milestone("Read sections: %1", sects)
deep_copy(sects)

sects
ensure
sections_data.unlink
end

# Set global bootloader options
Expand All @@ -136,63 +167,85 @@ def SetGlobal(globals)
globals = deep_copy(globals)
Builtins.y2milestone("Storing global settings %1", globals)
Ops.set(globals, "__modified", "1")
ret = System::Bootloader_API.setGlobalSettings(globals)
Builtins.y2error("Storing global settings failed") if !ret
ret
globals_data = tmp_yaml_file(globals)

run_pbl_yaml "SetGlobalSettings(#{globals_data.path})"

true
ensure
globals_data.unlink
end

# Get global bootloader options
# @return a map of global bootloader options
def GetGlobal
Builtins.y2milestone("Reading bootloader global settings")
glob = System::Bootloader_API.getGlobalSettings
globals_data = tmp_yaml_file
run_pbl_yaml "#{globals_data.path}=GetGlobalSettings()"
glob = YAML.load File.read(globals_data.path)

if glob == nil
Builtins.y2error("Reading global settings failed")
return {}
end
Builtins.y2milestone("Read global settings: %1", glob)
deep_copy(glob)

Builtins.y2milestone("Read global settings: %1", glob)
glob
ensure
globals_data.unlink
end

# Set the device mapping (Linux <-> Firmware)
# @param [Hash{String => String}] device_map a map from Linux device to Firmware device identification
# @return [Boolean] true on success
def SetDeviceMap(device_map)
device_map = deep_copy(device_map)
arg_data = tmp_yaml_file(device_map)

Builtins.y2milestone("Storing device map")
ret = System::Bootloader_API.setDeviceMapping(device_map)
Builtins.y2error("Storing device map failed") if !ret
ret
run_pbl_yaml "SetDeviceMapping(#{arg_data.path})"

true
ensure
arg_data.unlink
end

# Set the mapping (real device <-> multipath)
# @param map<string,string> map from real device to multipath device
# @return [Boolean] true on success
def DefineMultipath(multipath_map)
multipath_map = deep_copy(multipath_map)
Builtins.y2milestone("Storing multipath map: %1", multipath_map)
if Builtins.size(multipath_map) == 0
Builtins.y2milestone("Multipath was not detected")
return true
end
ret = System::Bootloader_API.defineMultipath(multipath_map)
Builtins.y2error("Storing multipath map failed") if !ret
ret

arg_data = tmp_yaml_file(multipath_map)
run_pbl_yaml "DefineMultipath(#{arg_data.path})"

true
ensure
arg_data.unlink if arg_data
end


# Get the device mapping (Linux <-> Firmware)
# @return a map from Linux device to Firmware device identification
def GetDeviceMap
Builtins.y2milestone("Reading device mapping")
devmap = System::Bootloader_API.getDeviceMapping

res_data = tmp_yaml_file

run_pbl_yaml "#{res_data.path}=GetDeviceMap()"

devmap = YAML.load(File.read(res_data.path))

if devmap == nil
Builtins.y2error("Reading device mapping failed")
return {}
end

Builtins.y2milestone("Read device mapping: %1", devmap)
deep_copy(devmap)
devmap
end

# Display the log file written by the underlying bootloader libraries
Expand All @@ -218,39 +271,47 @@ def bootloaderError(error)
# data
# @return [Boolean] true on success
def ReadFiles(avoid_reading_device_map)
param_data = tmp_yaml_file(avoid_reading_device_map)
Builtins.y2milestone("Reading Files")
ret = System::Bootloader_API.readSettings(avoid_reading_device_map)
Builtins.y2error("Reading files failed") if !ret
ret

run_pbl_yaml "ReadSettings(#{param_data.path})"

true
ensure
param_data.unlink
end

# Flush the internal cache of the library to the disk
# @return [Boolean] true on success
def CommitSettings
Builtins.y2milestone("Writing files to system")
ret = System::Bootloader_API.writeSettings
bootloaderError("Writing files to system failed") if !ret
ret
run_pbl_yaml "WriteSettings()"

true
end

# Update the bootloader settings, make updated saved settings active
# @return [Boolean] true on success
def UpdateBootloader
# true mean avoid init of bootloader
arg_data = tmp_yaml_file(true)

Builtins.y2milestone("Updating bootloader configuration")
ret = System::Bootloader_API.updateBootloader(true)
Builtins.y2milestone("return value from updateBootloader: %1", ret)
if !ret
bootloaderError("Error occurred while updating configuration files")
end
ret
run_pbl_yaml "UpdateBootloader(#{arg_data.path})"
ensure
arg_data.unlink
end

def SetSecureBoot(enable)
arg_data = tmp_yaml_file(enable)

Builtins.y2milestone("Set SecureBoot")
ret = System::Bootloader_API.setSecureBoot(enable)
Builtins.y2milestone("return value from setSecureBoot: %1", ret)
bootloaderError("Error occurred while setting secureboot") if !ret
ret
run_pbl_yaml "SetSecureBoot(#{arg_data.path})"
System::Bootloader_API.setSecureBoot(enable)

true
ensure
arg_data.unlink
end


Expand All @@ -261,47 +322,61 @@ def SetSecureBoot(enable)
# @param [String] console from section
# @return [String] updated append with console
def UpdateSerialConsole(append, console)
append_data = tmp_yaml_file(append)
console_data = tmp_yaml_file(console)
Builtins.y2milestone(
"Updating append: %1 with console: %2",
append,
console
)
ret = System::Bootloader_API.updateSerialConsole(append, console)
Builtins.y2milestone("return updated value of append: %1", ret)
bootloaderError("Error occurred while updating append") if ret == nil
ret

run_pbl_yaml "UpdateSerialConsole(#{append_data.path},#{console_data.path})"

true
ensure
append_data.unlink
console_data.unlink
end

# Initialize the boot loader (eg. modify firmware, depending on architecture)
# @return [Boolean] true on success
def InitializeBootloader
Builtins.y2milestone("Initializing bootloader")
ret = System::Bootloader_API.initializeBootloader
bootloaderError("Error occurred while initializing bootloader") if !ret
ret

run_pbl_yaml "InitializeBootloader()"
end

# Get contents of files from the library cache
# @return a map filename -> contents, empty map in case of fail
def GetFilesContents
Builtins.y2milestone("Getting contents of files")
ret = System::Bootloader_API.getFilesContents
ret_data = tmp_yaml_file

run_pbl_yaml "#{res_data.path}=GetFilesContents()"

ret = YAML.load(File.read(ret_data.path))
if ret == nil
Builtins.y2error("Getting contents of files failed")
return {}
end
deep_copy(ret)

ret
ensure
ret_data.unlink
end

# Set the contents of all files to library cache
# @param [Hash{String => String}] files a map filename -> contents
# @return [Boolean] true on success
def SetFilesContents(files)
files = deep_copy(files)
files_data = tmp_yaml_file(files)

Builtins.y2milestone("Storing contents of files")
ret = System::Bootloader_API.setFilesContents(files)
Builtins.y2error("Setting file contents failed") if !ret
ret
run_pbl_yaml "SetFilesContents(#{files_data.path})"

true
ensure
files_data.unlink
end

# Analyse content of MBR
Expand All @@ -310,9 +385,17 @@ def SetFilesContents(files)
# @return [String] result of analyse ("GRUB stage1", "uknown",...)

def examineMBR(device)
ret = System::Bootloader_API.examineMBR(device)
device_data = tmp_yaml_file(device)
ret_data = tmp_yaml_file

run_pbl_yaml "#{ret_data.path}=ExamineMBR(#{device_data})"
ret = YAML.load(File.read(ret_data.path))

Builtins.y2milestone("Device: %1 includes in MBR: %2", device, ret)
ret
ensure
device_data.unlink
ret_data.unlink
end
end
end

0 comments on commit 1b78c91

Please sign in to comment.