2018-02-12 15:32:00 +00:00
|
|
|
--
|
2018-02-23 02:51:35 +00:00
|
|
|
-- SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
--
|
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*(#.*)",
|
2018-02-22 04:15:02 +00:00
|
|
|
process = function(_, _) end
|
2018-02-12 15:32:00 +00:00
|
|
|
},
|
|
|
|
-- 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*(.*)",
|
2018-02-22 04:15:02 +00:00
|
|
|
process = function(k, _)
|
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-24 03:35:35 +00:00
|
|
|
local function check_nextboot()
|
|
|
|
local nextboot_file = loader.getenv("nextboot_file")
|
|
|
|
if nextboot_file == nil then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-02-24 03:43:10 +00:00
|
|
|
local function check_nextboot_enabled(text)
|
|
|
|
return text:match("^nextboot_enable=\"NO\"") == nil
|
2018-02-24 03:35:35 +00:00
|
|
|
end
|
|
|
|
|
2018-02-24 03:43:10 +00:00
|
|
|
if not config.parse(nextboot_file, true, check_nextboot_enabled) then
|
2018-02-24 03:35:35 +00:00
|
|
|
-- This only fails if it actually hit a parse error
|
|
|
|
print("Failed to parse nextboot configuration: '" ..
|
|
|
|
nextboot_file .. "'")
|
|
|
|
end
|
|
|
|
|
2018-02-24 04:02:06 +00:00
|
|
|
-- Attempt to rewrite the first line and only the first line of the
|
|
|
|
-- nextboot_file. We overwrite it with nextboot_enable="NO", then
|
|
|
|
-- check for that on load. See: check_nextboot_enabled
|
|
|
|
-- It's worth noting that this won't work on every filesystem, so we
|
|
|
|
-- won't do anything notable if we have any errors in this process.
|
2018-02-24 03:35:35 +00:00
|
|
|
local nfile = io.open(nextboot_file, 'w')
|
2018-02-24 03:47:04 +00:00
|
|
|
if nfile ~= nil then
|
2018-02-24 04:02:06 +00:00
|
|
|
-- We need the trailing space here to account for the extra
|
|
|
|
-- character taken up by the string nextboot_enable="YES"
|
|
|
|
-- Or new end quotation mark lands on the S, and we want to
|
|
|
|
-- rewrite the entirety of the first line.
|
2018-02-24 03:47:04 +00:00
|
|
|
io.write(nfile, "nextboot_enable=\"NO\" ")
|
|
|
|
io.close(nfile)
|
|
|
|
end
|
2018-02-24 03:35:35 +00:00
|
|
|
end
|
|
|
|
|
2018-02-24 19:51:18 +00:00
|
|
|
local function read_file(name, silent)
|
|
|
|
local f = io.open(name)
|
|
|
|
if f == nil then
|
|
|
|
if not silent then
|
|
|
|
print("Failed to open config: '" .. name .. "'")
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
local text, _ = io.read(f)
|
|
|
|
-- We might have read in the whole file, this won't be needed any more.
|
|
|
|
io.close(f)
|
|
|
|
|
|
|
|
if text == nil then
|
|
|
|
if not silent then
|
|
|
|
print("Failed to read config: '" .. name .. "'")
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
2018-02-22 04:30:52 +00:00
|
|
|
-- else
|
|
|
|
-- if not silent then
|
|
|
|
-- print("Skipping module '". . k .. "'")
|
|
|
|
-- 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
|
|
|
|
|
2018-02-21 01:50:46 +00:00
|
|
|
-- silent runs will not return false if we fail to open the file
|
2018-02-24 03:35:35 +00:00
|
|
|
-- check_and_halt, if it's set, will be executed on the full text of the config
|
|
|
|
-- file. If it returns false, we are to halt immediately.
|
|
|
|
function config.parse(name, silent, check_and_halt)
|
2018-02-21 01:50:46 +00:00
|
|
|
if silent == nil then
|
|
|
|
silent = false
|
|
|
|
end
|
2018-02-12 15:32:00 +00:00
|
|
|
|
2018-02-24 19:51:18 +00:00
|
|
|
local text = read_file(name, silent)
|
2018-02-21 01:35:19 +00:00
|
|
|
if text == nil then
|
2018-02-24 19:51:18 +00:00
|
|
|
return not silent
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
|
2018-02-24 03:35:35 +00:00
|
|
|
if check_and_halt ~= nil then
|
|
|
|
if not check_and_halt(text) then
|
|
|
|
-- We'll just pretend that everything is fine...
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
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
|
|
|
|
2018-02-22 04:15:02 +00:00
|
|
|
for _, 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:39:33 +00:00
|
|
|
if not found 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
|
|
|
|
2018-02-23 04:03:07 +00:00
|
|
|
local function try_load(names)
|
2018-02-12 15:32:00 +00:00
|
|
|
for name in names:gmatch("([^;]+)%s*;?") do
|
2018-02-22 20:10:23 +00:00
|
|
|
local 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
|
|
|
|
2018-02-23 04:03:07 +00:00
|
|
|
local function load_bootfile()
|
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
|
2018-02-22 04:15:02 +00:00
|
|
|
local res
|
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
|
|
|
|
2018-02-22 04:15:02 +00:00
|
|
|
for _, 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
|
2018-02-21 01:52:42 +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:52:42 +00:00
|
|
|
-- These may or may not exist, and that's ok. Do a
|
|
|
|
-- silent parse so that we complain on parse errors but
|
|
|
|
-- not for them simply not existing.
|
|
|
|
if not config.parse(name, true) then
|
|
|
|
print("Failed to parse configuration: '" ..
|
|
|
|
name .. "'")
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-24 03:35:35 +00:00
|
|
|
check_nextboot()
|
|
|
|
|
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
|
2018-02-22 04:15:02 +00:00
|
|
|
local loaded
|
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
|