2018-02-12 15:32:00 +00:00
|
|
|
--
|
|
|
|
-- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org>
|
2018-02-19 18:11:35 +00:00
|
|
|
-- Copyright (C) 2018 Kyle Evans <kevans@FreeBSD.org>
|
2018-02-12 15:32:00 +00:00
|
|
|
-- All rights reserved.
|
|
|
|
--
|
|
|
|
-- Redistribution and use in source and binary forms, with or without
|
|
|
|
-- modification, are permitted provided that the following conditions
|
|
|
|
-- are met:
|
|
|
|
-- 1. Redistributions of source code must retain the above copyright
|
|
|
|
-- notice, this list of conditions and the following disclaimer.
|
|
|
|
-- 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
-- notice, this list of conditions and the following disclaimer in the
|
|
|
|
-- documentation and/or other materials provided with the distribution.
|
|
|
|
--
|
|
|
|
-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
-- ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
-- SUCH DAMAGE.
|
|
|
|
--
|
|
|
|
-- $FreeBSD$
|
|
|
|
--
|
|
|
|
|
2018-02-21 01:10:03 +00:00
|
|
|
local config = {}
|
2018-02-12 15:32:00 +00:00
|
|
|
|
2018-02-21 01:10:03 +00:00
|
|
|
local modules = {}
|
2018-02-12 15:32:00 +00:00
|
|
|
|
2018-02-21 01:10:03 +00:00
|
|
|
local pattern_table
|
|
|
|
local carousel_choices = {}
|
2018-02-20 18:04:08 +00:00
|
|
|
|
|
|
|
pattern_table = {
|
2018-02-12 15:32:00 +00:00
|
|
|
[1] = {
|
|
|
|
str = "^%s*(#.*)",
|
|
|
|
process = function(k, v) end
|
|
|
|
},
|
|
|
|
-- module_load="value"
|
|
|
|
[2] = {
|
|
|
|
str = "^%s*([%w_]+)_load%s*=%s*\"([%w%s%p]-)\"%s*(.*)",
|
|
|
|
process = function(k, v)
|
2018-02-21 01:35:19 +00:00
|
|
|
if modules[k] == nil then
|
2018-02-21 01:10:03 +00:00
|
|
|
modules[k] = {}
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
2018-02-21 01:10:03 +00:00
|
|
|
modules[k].load = v:upper()
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
},
|
|
|
|
-- module_name="value"
|
|
|
|
[3] = {
|
|
|
|
str = "^%s*([%w_]+)_name%s*=%s*\"([%w%s%p]-)\"%s*(.*)",
|
|
|
|
process = function(k, v)
|
2018-02-21 01:10:03 +00:00
|
|
|
config.setKey(k, "name", v)
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
},
|
|
|
|
-- module_type="value"
|
|
|
|
[4] = {
|
|
|
|
str = "^%s*([%w_]+)_type%s*=%s*\"([%w%s%p]-)\"%s*(.*)",
|
|
|
|
process = function(k, v)
|
2018-02-21 01:10:03 +00:00
|
|
|
config.setKey(k, "type", v)
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
},
|
|
|
|
-- module_flags="value"
|
|
|
|
[5] = {
|
|
|
|
str = "^%s*([%w_]+)_flags%s*=%s*\"([%w%s%p]-)\"%s*(.*)",
|
|
|
|
process = function(k, v)
|
2018-02-21 01:10:03 +00:00
|
|
|
config.setKey(k, "flags", v)
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
},
|
|
|
|
-- module_before="value"
|
|
|
|
[6] = {
|
|
|
|
str = "^%s*([%w_]+)_before%s*=%s*\"([%w%s%p]-)\"%s*(.*)",
|
|
|
|
process = function(k, v)
|
2018-02-21 01:10:03 +00:00
|
|
|
config.setKey(k, "before", v)
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
},
|
|
|
|
-- module_after="value"
|
|
|
|
[7] = {
|
|
|
|
str = "^%s*([%w_]+)_after%s*=%s*\"([%w%s%p]-)\"%s*(.*)",
|
|
|
|
process = function(k, v)
|
2018-02-21 01:10:03 +00:00
|
|
|
config.setKey(k, "after", v)
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
},
|
|
|
|
-- module_error="value"
|
|
|
|
[8] = {
|
|
|
|
str = "^%s*([%w_]+)_error%s*=%s*\"([%w%s%p]-)\"%s*(.*)",
|
|
|
|
process = function(k, v)
|
2018-02-21 01:10:03 +00:00
|
|
|
config.setKey(k, "error", v)
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
},
|
|
|
|
-- exec="command"
|
|
|
|
[9] = {
|
|
|
|
str = "^%s*exec%s*=%s*\"([%w%s%p]-)\"%s*(.*)",
|
|
|
|
process = function(k, v)
|
2018-02-21 01:35:19 +00:00
|
|
|
if loader.perform(k) ~= 0 then
|
2018-02-21 01:10:03 +00:00
|
|
|
print("Failed to exec '" .. k .. "'")
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
},
|
|
|
|
-- env_var="value"
|
|
|
|
[10] = {
|
|
|
|
str = "^%s*([%w%p]+)%s*=%s*\"([%w%s%p]-)\"%s*(.*)",
|
|
|
|
process = function(k, v)
|
2018-02-21 01:35:19 +00:00
|
|
|
if config.setenv(k, v) ~= 0 then
|
2018-02-18 01:35:46 +00:00
|
|
|
print("Failed to set '" .. k ..
|
2018-02-21 01:10:03 +00:00
|
|
|
"' with value: " .. v .. "")
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
},
|
|
|
|
-- env_var=num
|
|
|
|
[11] = {
|
|
|
|
str = "^%s*([%w%p]+)%s*=%s*(%d+)%s*(.*)",
|
|
|
|
process = function(k, v)
|
2018-02-21 01:35:19 +00:00
|
|
|
if config.setenv(k, v) ~= 0 then
|
2018-02-18 01:35:46 +00:00
|
|
|
print("Failed to set '" .. k ..
|
2018-02-21 01:10:03 +00:00
|
|
|
"' with value: " .. v .. "")
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
2018-02-21 01:10:03 +00:00
|
|
|
}
|
2018-02-12 15:32:00 +00:00
|
|
|
|
2018-02-20 14:45:58 +00:00
|
|
|
-- Module exports
|
|
|
|
-- Which variables we changed
|
2018-02-21 01:10:03 +00:00
|
|
|
config.env_changed = {}
|
2018-02-20 14:45:58 +00:00
|
|
|
-- Values to restore env to (nil to unset)
|
2018-02-21 01:10:03 +00:00
|
|
|
config.env_restore = {}
|
2018-02-20 14:45:58 +00:00
|
|
|
|
2018-02-20 18:04:08 +00:00
|
|
|
-- The first item in every carousel is always the default item.
|
|
|
|
function config.getCarouselIndex(id)
|
2018-02-21 01:10:03 +00:00
|
|
|
local val = carousel_choices[id]
|
2018-02-21 01:35:19 +00:00
|
|
|
if val == nil then
|
2018-02-21 01:10:03 +00:00
|
|
|
return 1
|
2018-02-20 18:04:08 +00:00
|
|
|
end
|
2018-02-21 01:10:03 +00:00
|
|
|
return val
|
2018-02-20 18:04:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function config.setCarouselIndex(id, idx)
|
2018-02-21 01:10:03 +00:00
|
|
|
carousel_choices[id] = idx
|
2018-02-20 18:04:08 +00:00
|
|
|
end
|
|
|
|
|
2018-02-20 14:45:58 +00:00
|
|
|
function config.restoreEnv()
|
2018-02-20 22:47:49 +00:00
|
|
|
-- Examine changed environment variables
|
2018-02-20 14:45:58 +00:00
|
|
|
for k, v in pairs(config.env_changed) do
|
2018-02-21 01:10:03 +00:00
|
|
|
local restore_value = config.env_restore[k]
|
2018-02-21 01:35:19 +00:00
|
|
|
if restore_value == nil then
|
2018-02-20 22:47:49 +00:00
|
|
|
-- This one doesn't need restored for some reason
|
2018-02-21 01:10:03 +00:00
|
|
|
goto continue
|
2018-02-20 22:47:49 +00:00
|
|
|
end
|
2018-02-21 01:10:03 +00:00
|
|
|
local current_value = loader.getenv(k)
|
2018-02-21 01:35:19 +00:00
|
|
|
if current_value ~= v then
|
2018-02-20 22:47:49 +00:00
|
|
|
-- This was overwritten by some action taken on the menu
|
|
|
|
-- most likely; we'll leave it be.
|
2018-02-21 01:10:03 +00:00
|
|
|
goto continue
|
2018-02-20 22:47:49 +00:00
|
|
|
end
|
2018-02-21 01:10:03 +00:00
|
|
|
restore_value = restore_value.value
|
2018-02-21 01:35:19 +00:00
|
|
|
if restore_value ~= nil then
|
2018-02-21 01:10:03 +00:00
|
|
|
loader.setenv(k, restore_value)
|
2018-02-20 14:45:58 +00:00
|
|
|
else
|
2018-02-21 01:10:03 +00:00
|
|
|
loader.unsetenv(k)
|
2018-02-20 14:45:58 +00:00
|
|
|
end
|
2018-02-20 22:47:49 +00:00
|
|
|
::continue::
|
2018-02-20 14:45:58 +00:00
|
|
|
end
|
|
|
|
|
2018-02-21 01:10:03 +00:00
|
|
|
config.env_changed = {}
|
|
|
|
config.env_restore = {}
|
2018-02-20 14:45:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function config.setenv(k, v)
|
2018-02-20 22:47:49 +00:00
|
|
|
-- Track the original value for this if we haven't already
|
2018-02-21 01:35:19 +00:00
|
|
|
if config.env_restore[k] == nil then
|
2018-02-21 01:10:03 +00:00
|
|
|
config.env_restore[k] = {value = loader.getenv(k)}
|
2018-02-20 14:45:58 +00:00
|
|
|
end
|
|
|
|
|
2018-02-21 01:10:03 +00:00
|
|
|
config.env_changed[k] = v
|
2018-02-20 22:47:49 +00:00
|
|
|
|
2018-02-21 01:10:03 +00:00
|
|
|
return loader.setenv(k, v)
|
2018-02-20 14:45:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function config.setKey(k, n, v)
|
2018-02-21 01:35:19 +00:00
|
|
|
if modules[k] == nil then
|
2018-02-21 01:10:03 +00:00
|
|
|
modules[k] = {}
|
2018-02-20 14:45:58 +00:00
|
|
|
end
|
2018-02-21 01:10:03 +00:00
|
|
|
modules[k][n] = v
|
2018-02-20 14:45:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function config.lsModules()
|
2018-02-21 01:10:03 +00:00
|
|
|
print("== Listing modules")
|
2018-02-20 14:45:58 +00:00
|
|
|
for k, v in pairs(modules) do
|
2018-02-21 01:10:03 +00:00
|
|
|
print(k, v.load)
|
2018-02-20 14:45:58 +00:00
|
|
|
end
|
2018-02-21 01:10:03 +00:00
|
|
|
print("== List of modules ended")
|
2018-02-20 14:45:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2018-02-12 15:32:00 +00:00
|
|
|
function config.isValidComment(c)
|
2018-02-21 01:35:19 +00:00
|
|
|
if c ~= nil then
|
2018-02-21 01:10:03 +00:00
|
|
|
local s = c:match("^%s*#.*")
|
2018-02-21 01:35:19 +00:00
|
|
|
if s == nil then
|
2018-02-21 01:10:03 +00:00
|
|
|
s = c:match("^%s*$")
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
2018-02-21 01:35:19 +00:00
|
|
|
if s == nil then
|
2018-02-21 01:10:03 +00:00
|
|
|
return false
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
end
|
2018-02-21 01:10:03 +00:00
|
|
|
return true
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function config.loadmod(mod, silent)
|
2018-02-21 01:10:03 +00:00
|
|
|
local status = true
|
2018-02-12 15:32:00 +00:00
|
|
|
for k, v in pairs(mod) do
|
2018-02-21 01:35:19 +00:00
|
|
|
if v.load == "YES" then
|
2018-02-21 01:10:03 +00:00
|
|
|
local str = "load "
|
2018-02-21 01:35:19 +00:00
|
|
|
if v.flags ~= nil then
|
2018-02-21 01:10:03 +00:00
|
|
|
str = str .. v.flags .. " "
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
2018-02-21 01:35:19 +00:00
|
|
|
if v.type ~= nil then
|
2018-02-21 01:10:03 +00:00
|
|
|
str = str .. "-t " .. v.type .. " "
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
2018-02-21 01:35:19 +00:00
|
|
|
if v.name ~= nil then
|
2018-02-21 01:10:03 +00:00
|
|
|
str = str .. v.name
|
2018-02-12 15:32:00 +00:00
|
|
|
else
|
2018-02-21 01:10:03 +00:00
|
|
|
str = str .. k
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
|
2018-02-21 01:35:19 +00:00
|
|
|
if v.before ~= nil then
|
|
|
|
if loader.perform(v.before) ~= 0 then
|
|
|
|
if not silent then
|
2018-02-18 01:35:46 +00:00
|
|
|
print("Failed to execute '" ..
|
|
|
|
v.before ..
|
2018-02-19 17:51:33 +00:00
|
|
|
"' before loading '" .. k ..
|
2018-02-21 01:10:03 +00:00
|
|
|
"'")
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
2018-02-21 01:10:03 +00:00
|
|
|
status = false
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-21 01:35:19 +00:00
|
|
|
if loader.perform(str) ~= 0 then
|
|
|
|
if not silent then
|
2018-02-18 01:35:46 +00:00
|
|
|
print("Failed to execute '" .. str ..
|
2018-02-21 01:10:03 +00:00
|
|
|
"'")
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
2018-02-21 01:35:19 +00:00
|
|
|
if v.error ~= nil then
|
2018-02-21 01:10:03 +00:00
|
|
|
loader.perform(v.error)
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
2018-02-21 01:10:03 +00:00
|
|
|
status = false
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
|
2018-02-21 01:35:19 +00:00
|
|
|
if v.after ~= nil then
|
|
|
|
if loader.perform(v.after) ~= 0 then
|
|
|
|
if not silent then
|
2018-02-18 01:35:46 +00:00
|
|
|
print("Failed to execute '" ..
|
|
|
|
v.after ..
|
|
|
|
"' after loading '" .. k ..
|
2018-02-21 01:10:03 +00:00
|
|
|
"'")
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
2018-02-21 01:10:03 +00:00
|
|
|
status = false
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
else
|
2018-02-19 17:51:33 +00:00
|
|
|
-- if not silent then
|
2018-02-21 01:35:19 +00:00
|
|
|
-- print("Skipping module '". . k .. "'")
|
2018-02-19 17:51:33 +00:00
|
|
|
-- end
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-21 01:10:03 +00:00
|
|
|
return status
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function config.parse(name, silent)
|
2018-02-21 01:10:03 +00:00
|
|
|
local f = io.open(name)
|
2018-02-21 01:35:19 +00:00
|
|
|
if f == nil then
|
|
|
|
if not silent then
|
2018-02-21 01:10:03 +00:00
|
|
|
print("Failed to open config: '" .. name .. "'")
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
2018-02-21 01:10:03 +00:00
|
|
|
return false
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
|
2018-02-21 01:10:03 +00:00
|
|
|
local text
|
|
|
|
local r
|
2018-02-12 15:32:00 +00:00
|
|
|
|
2018-02-21 01:10:03 +00:00
|
|
|
text, r = io.read(f)
|
2018-02-12 15:32:00 +00:00
|
|
|
|
2018-02-21 01:35:19 +00:00
|
|
|
if text == nil then
|
|
|
|
if not silent then
|
2018-02-21 01:10:03 +00:00
|
|
|
print("Failed to read config: '" .. name .. "'")
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
2018-02-21 01:10:03 +00:00
|
|
|
return false
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
|
2018-02-21 01:10:03 +00:00
|
|
|
local n = 1
|
|
|
|
local status = true
|
2018-02-12 15:32:00 +00:00
|
|
|
|
2018-02-17 04:43:41 +00:00
|
|
|
for line in text:gmatch("([^\n]+)") do
|
2018-02-21 01:35:19 +00:00
|
|
|
if line:match("^%s*$") == nil then
|
2018-02-21 01:10:03 +00:00
|
|
|
local found = false
|
2018-02-12 15:32:00 +00:00
|
|
|
|
|
|
|
for i, val in ipairs(pattern_table) do
|
2018-02-21 01:10:03 +00:00
|
|
|
local k, v, c = line:match(val.str)
|
2018-02-21 01:35:19 +00:00
|
|
|
if k ~= nil then
|
2018-02-21 01:10:03 +00:00
|
|
|
found = true
|
2018-02-12 15:32:00 +00:00
|
|
|
|
2018-02-21 01:35:19 +00:00
|
|
|
if config.isValidComment(c) then
|
2018-02-21 01:10:03 +00:00
|
|
|
val.process(k, v)
|
2018-02-12 15:32:00 +00:00
|
|
|
else
|
2018-02-18 01:35:46 +00:00
|
|
|
print("Malformed line (" .. n ..
|
2018-02-21 01:10:03 +00:00
|
|
|
"):\n\t'" .. line .. "'")
|
|
|
|
status = false
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
|
2018-02-21 01:10:03 +00:00
|
|
|
break
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-21 01:35:19 +00:00
|
|
|
if found == false then
|
2018-02-18 01:35:46 +00:00
|
|
|
print("Malformed line (" .. n .. "):\n\t'" ..
|
2018-02-21 01:10:03 +00:00
|
|
|
line .. "'")
|
|
|
|
status = false
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
end
|
2018-02-21 01:10:03 +00:00
|
|
|
n = n + 1
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
|
2018-02-21 01:10:03 +00:00
|
|
|
return status
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
|
2018-02-17 05:02:38 +00:00
|
|
|
-- other_kernel is optionally the name of a kernel to load, if not the default
|
|
|
|
-- or autoloaded default from the module_path
|
|
|
|
function config.loadkernel(other_kernel)
|
2018-02-21 01:10:03 +00:00
|
|
|
local flags = loader.getenv("kernel_options") or ""
|
|
|
|
local kernel = other_kernel or loader.getenv("kernel")
|
2018-02-12 15:32:00 +00:00
|
|
|
|
|
|
|
local try_load = function (names)
|
|
|
|
for name in names:gmatch("([^;]+)%s*;?") do
|
2018-02-21 01:10:03 +00:00
|
|
|
r = loader.perform("load " .. flags .. " " .. name)
|
2018-02-21 01:35:19 +00:00
|
|
|
if r == 0 then
|
2018-02-21 01:10:03 +00:00
|
|
|
return name
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
end
|
2018-02-21 01:10:03 +00:00
|
|
|
return nil
|
2018-02-17 05:02:38 +00:00
|
|
|
end
|
2018-02-12 15:32:00 +00:00
|
|
|
|
|
|
|
local load_bootfile = function()
|
2018-02-21 01:10:03 +00:00
|
|
|
local bootfile = loader.getenv("bootfile")
|
2018-02-12 15:32:00 +00:00
|
|
|
|
|
|
|
-- append default kernel name
|
2018-02-21 01:35:19 +00:00
|
|
|
if bootfile == nil then
|
2018-02-21 01:10:03 +00:00
|
|
|
bootfile = "kernel"
|
2018-02-12 15:32:00 +00:00
|
|
|
else
|
2018-02-21 01:10:03 +00:00
|
|
|
bootfile = bootfile .. ";kernel"
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
|
2018-02-21 01:10:03 +00:00
|
|
|
return try_load(bootfile)
|
2018-02-17 05:52:25 +00:00
|
|
|
end
|
2018-02-12 15:32:00 +00:00
|
|
|
|
|
|
|
-- kernel not set, try load from default module_path
|
2018-02-21 01:35:19 +00:00
|
|
|
if kernel == nil then
|
2018-02-21 01:10:03 +00:00
|
|
|
local res = load_bootfile()
|
2018-02-12 15:32:00 +00:00
|
|
|
|
2018-02-21 01:35:19 +00:00
|
|
|
if res ~= nil then
|
2018-02-19 03:52:02 +00:00
|
|
|
-- Default kernel is loaded
|
2018-02-21 01:10:03 +00:00
|
|
|
config.kernel_loaded = nil
|
|
|
|
return true
|
2018-02-12 15:32:00 +00:00
|
|
|
else
|
2018-02-21 01:10:03 +00:00
|
|
|
print("No kernel set, failed to load from module_path")
|
|
|
|
return false
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
else
|
2018-02-18 00:56:12 +00:00
|
|
|
-- Use our cached module_path, so we don't end up with multiple
|
|
|
|
-- automatically added kernel paths to our final module_path
|
2018-02-21 01:10:03 +00:00
|
|
|
local module_path = config.module_path
|
|
|
|
local res = nil
|
2018-02-12 15:32:00 +00:00
|
|
|
|
2018-02-21 01:35:19 +00:00
|
|
|
if other_kernel ~= nil then
|
2018-02-21 01:10:03 +00:00
|
|
|
kernel = other_kernel
|
2018-02-17 05:02:38 +00:00
|
|
|
end
|
2018-02-12 15:32:00 +00:00
|
|
|
-- first try load kernel with module_path = /boot/${kernel}
|
|
|
|
-- then try load with module_path=${kernel}
|
2018-02-21 01:10:03 +00:00
|
|
|
local paths = {"/boot/" .. kernel, kernel}
|
2018-02-12 15:32:00 +00:00
|
|
|
|
|
|
|
for k,v in pairs(paths) do
|
2018-02-21 01:10:03 +00:00
|
|
|
loader.setenv("module_path", v)
|
|
|
|
res = load_bootfile()
|
2018-02-12 15:32:00 +00:00
|
|
|
|
2018-02-18 00:56:12 +00:00
|
|
|
-- succeeded, add path to module_path
|
2018-02-21 01:35:19 +00:00
|
|
|
if res ~= nil then
|
2018-02-21 01:10:03 +00:00
|
|
|
config.kernel_loaded = kernel
|
2018-02-21 01:35:19 +00:00
|
|
|
if module_path ~= nil then
|
2018-02-18 01:35:46 +00:00
|
|
|
loader.setenv("module_path", v .. ";" ..
|
2018-02-21 01:10:03 +00:00
|
|
|
module_path)
|
2018-02-17 04:22:36 +00:00
|
|
|
end
|
2018-02-21 01:10:03 +00:00
|
|
|
return true
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- failed to load with ${kernel} as a directory
|
|
|
|
-- try as a file
|
2018-02-21 01:10:03 +00:00
|
|
|
res = try_load(kernel)
|
2018-02-21 01:35:19 +00:00
|
|
|
if res ~= nil then
|
2018-02-21 01:10:03 +00:00
|
|
|
config.kernel_loaded = kernel
|
|
|
|
return true
|
2018-02-12 15:32:00 +00:00
|
|
|
else
|
2018-02-21 01:10:03 +00:00
|
|
|
print("Failed to load kernel '" .. kernel .. "'")
|
|
|
|
return false
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-19 14:21:56 +00:00
|
|
|
function config.selectkernel(kernel)
|
2018-02-21 01:10:03 +00:00
|
|
|
config.kernel_selected = kernel
|
2018-02-19 14:21:56 +00:00
|
|
|
end
|
2018-02-12 15:32:00 +00:00
|
|
|
|
|
|
|
function config.load(file)
|
2018-02-21 01:35:19 +00:00
|
|
|
if not file then
|
2018-02-21 01:10:03 +00:00
|
|
|
file = "/boot/defaults/loader.conf"
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
|
2018-02-21 01:35:19 +00:00
|
|
|
if not config.parse(file) then
|
|
|
|
-- XXX TODO: Why is this commented out?
|
2018-02-21 01:10:03 +00:00
|
|
|
-- print("Failed to parse configuration: '" .. file .. "'")
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
|
2018-02-21 01:10:03 +00:00
|
|
|
local f = loader.getenv("loader_conf_files")
|
2018-02-21 01:35:19 +00:00
|
|
|
if f ~= nil then
|
2018-02-17 04:43:41 +00:00
|
|
|
for name in f:gmatch("([%w%p]+)%s*") do
|
2018-02-21 01:35:19 +00:00
|
|
|
if not config.parse(name) then
|
|
|
|
-- XXX TODO: Ditto the above
|
2018-02-18 01:35:46 +00:00
|
|
|
-- print("Failed to parse configuration: '" ..
|
2018-02-21 01:10:03 +00:00
|
|
|
-- name .. "'")
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-18 00:56:12 +00:00
|
|
|
-- Cache the provided module_path at load time for later use
|
2018-02-21 01:10:03 +00:00
|
|
|
config.module_path = loader.getenv("module_path")
|
2018-02-19 14:21:56 +00:00
|
|
|
end
|
2018-02-18 00:56:12 +00:00
|
|
|
|
2018-02-19 14:21:56 +00:00
|
|
|
-- Reload configuration
|
|
|
|
function config.reload(file)
|
2018-02-21 01:10:03 +00:00
|
|
|
modules = {}
|
|
|
|
config.restoreEnv()
|
|
|
|
config.load(file)
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
|
2018-02-19 14:21:56 +00:00
|
|
|
function config.loadelf()
|
2018-02-21 01:10:03 +00:00
|
|
|
local kernel = config.kernel_selected or config.kernel_loaded
|
|
|
|
local loaded = false
|
2018-02-12 15:32:00 +00:00
|
|
|
|
2018-02-21 01:10:03 +00:00
|
|
|
print("Loading kernel...")
|
|
|
|
loaded = config.loadkernel(kernel)
|
2018-02-12 15:32:00 +00:00
|
|
|
|
2018-02-21 01:35:19 +00:00
|
|
|
if not loaded then
|
2018-02-21 01:10:03 +00:00
|
|
|
print("Failed to load any kernel")
|
|
|
|
return
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
|
2018-02-21 01:10:03 +00:00
|
|
|
print("Loading configured modules...")
|
2018-02-21 01:35:19 +00:00
|
|
|
if not config.loadmod(modules) then
|
2018-02-21 01:10:03 +00:00
|
|
|
print("Could not load one or more modules!")
|
2018-02-19 14:21:56 +00:00
|
|
|
end
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
|
2018-02-21 01:10:03 +00:00
|
|
|
return config
|