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>
|
2019-09-26 16:19:22 +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-03-21 03:07:16 +00:00
|
|
|
local hook = require("hook")
|
|
|
|
|
2018-02-21 01:10:03 +00:00
|
|
|
local config = {}
|
|
|
|
local modules = {}
|
|
|
|
local carousel_choices = {}
|
2018-03-24 04:00:01 +00:00
|
|
|
-- Which variables we changed
|
|
|
|
local env_changed = {}
|
|
|
|
-- Values to restore env to (nil to unset)
|
|
|
|
local env_restore = {}
|
2018-02-20 18:04:08 +00:00
|
|
|
|
2018-03-01 02:31:28 +00:00
|
|
|
local MSG_FAILEXEC = "Failed to exec '%s'"
|
|
|
|
local MSG_FAILSETENV = "Failed to '%s' with value: %s"
|
|
|
|
local MSG_FAILOPENCFG = "Failed to open config: '%s'"
|
|
|
|
local MSG_FAILREADCFG = "Failed to read config: '%s'"
|
|
|
|
local MSG_FAILPARSECFG = "Failed to parse config: '%s'"
|
|
|
|
local MSG_FAILEXBEF = "Failed to execute '%s' before loading '%s'"
|
|
|
|
local MSG_FAILEXAF = "Failed to execute '%s' after loading '%s'"
|
|
|
|
local MSG_MALFORMED = "Malformed line (%d):\n\t'%s'"
|
|
|
|
local MSG_DEFAULTKERNFAIL = "No kernel set, failed to load from module_path"
|
|
|
|
local MSG_KERNFAIL = "Failed to load kernel '%s'"
|
2018-08-23 16:26:03 +00:00
|
|
|
local MSG_XENKERNFAIL = "Failed to load Xen kernel '%s'"
|
|
|
|
local MSG_XENKERNLOADING = "Loading Xen kernel..."
|
2018-03-01 02:31:28 +00:00
|
|
|
local MSG_KERNLOADING = "Loading kernel..."
|
|
|
|
local MSG_MODLOADING = "Loading configured modules..."
|
2018-10-07 01:53:43 +00:00
|
|
|
local MSG_MODBLACKLIST = "Not loading blacklisted module '%s'"
|
2018-03-01 02:31:28 +00:00
|
|
|
|
2018-08-21 23:42:20 +00:00
|
|
|
local MODULEEXPR = '([%w-_]+)'
|
2018-08-23 17:27:02 +00:00
|
|
|
local QVALEXPR = "\"([%w%s%p]-)\""
|
|
|
|
local QVALREPL = QVALEXPR:gsub('%%', '%%%%')
|
|
|
|
local WORDEXPR = "([%w]+)"
|
|
|
|
local WORDREPL = WORDEXPR:gsub('%%', '%%%%')
|
2018-08-21 23:42:20 +00:00
|
|
|
|
2020-04-30 20:58:58 +00:00
|
|
|
-- Entries that should never make it into the environment; each one should have
|
|
|
|
-- a documented reason for its existence, and these should all be implementation
|
|
|
|
-- details of the config module.
|
|
|
|
local loader_env_restricted_table = {
|
|
|
|
-- loader_conf_files should be considered write-only, and consumers
|
|
|
|
-- should not rely on any particular value; it's a loader implementation
|
|
|
|
-- detail. Moreover, it's not a particularly useful variable to have in
|
|
|
|
-- the kenv. Save the overhead, let it get fetched other ways.
|
|
|
|
loader_conf_files = true,
|
|
|
|
}
|
|
|
|
|
2018-03-24 04:00:01 +00:00
|
|
|
local function restoreEnv()
|
|
|
|
-- Examine changed environment variables
|
|
|
|
for k, v in pairs(env_changed) do
|
|
|
|
local restore_value = env_restore[k]
|
|
|
|
if restore_value == nil then
|
|
|
|
-- This one doesn't need restored for some reason
|
|
|
|
goto continue
|
|
|
|
end
|
|
|
|
local current_value = loader.getenv(k)
|
|
|
|
if current_value ~= v then
|
|
|
|
-- This was overwritten by some action taken on the menu
|
|
|
|
-- most likely; we'll leave it be.
|
|
|
|
goto continue
|
|
|
|
end
|
|
|
|
restore_value = restore_value.value
|
|
|
|
if restore_value ~= nil then
|
|
|
|
loader.setenv(k, restore_value)
|
|
|
|
else
|
|
|
|
loader.unsetenv(k)
|
|
|
|
end
|
|
|
|
::continue::
|
|
|
|
end
|
|
|
|
|
|
|
|
env_changed = {}
|
|
|
|
env_restore = {}
|
|
|
|
end
|
|
|
|
|
2020-04-30 20:58:58 +00:00
|
|
|
-- XXX This getEnv/setEnv should likely be exported at some point. We can save
|
|
|
|
-- the call back into loader.getenv for any variable that's been set or
|
|
|
|
-- overridden by any loader.conf using this implementation with little overhead
|
|
|
|
-- since we're already tracking the values.
|
|
|
|
local function getEnv(key)
|
|
|
|
if loader_env_restricted_table[key] ~= nil or
|
|
|
|
env_changed[key] ~= nil then
|
|
|
|
return env_changed[key]
|
|
|
|
end
|
|
|
|
|
|
|
|
return loader.getenv(key)
|
|
|
|
end
|
|
|
|
|
2018-03-24 04:00:01 +00:00
|
|
|
local function setEnv(key, value)
|
2020-04-30 20:58:58 +00:00
|
|
|
env_changed[key] = value
|
|
|
|
|
|
|
|
if loader_env_restricted_table[key] ~= nil then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2018-03-24 04:00:01 +00:00
|
|
|
-- Track the original value for this if we haven't already
|
|
|
|
if env_restore[key] == nil then
|
|
|
|
env_restore[key] = {value = loader.getenv(key)}
|
|
|
|
end
|
|
|
|
|
|
|
|
return loader.setenv(key, value)
|
|
|
|
end
|
|
|
|
|
2018-03-24 04:03:55 +00:00
|
|
|
-- name here is one of 'name', 'type', flags', 'before', 'after', or 'error.'
|
|
|
|
-- These are set from lines in loader.conf(5): ${key}_${name}="${value}" where
|
|
|
|
-- ${key} is a module name.
|
|
|
|
local function setKey(key, name, value)
|
|
|
|
if modules[key] == nil then
|
|
|
|
modules[key] = {}
|
|
|
|
end
|
|
|
|
modules[key][name] = value
|
|
|
|
end
|
|
|
|
|
2018-06-10 02:36:38 +00:00
|
|
|
-- Escapes the named value for use as a literal in a replacement pattern.
|
|
|
|
-- e.g. dhcp.host-name gets turned into dhcp%.host%-name to remove the special
|
|
|
|
-- meaning.
|
|
|
|
local function escapeName(name)
|
|
|
|
return name:gsub("([%p])", "%%%1")
|
|
|
|
end
|
|
|
|
|
|
|
|
local function processEnvVar(value)
|
|
|
|
for name in value:gmatch("${([^}]+)}") do
|
|
|
|
local replacement = loader.getenv(name) or ""
|
|
|
|
value = value:gsub("${" .. escapeName(name) .. "}", replacement)
|
|
|
|
end
|
|
|
|
for name in value:gmatch("$([%w%p]+)%s*") do
|
|
|
|
local replacement = loader.getenv(name) or ""
|
|
|
|
value = value:gsub("$" .. escapeName(name), replacement)
|
|
|
|
end
|
|
|
|
return value
|
|
|
|
end
|
|
|
|
|
2018-08-23 17:27:02 +00:00
|
|
|
local function checkPattern(line, pattern)
|
|
|
|
local function _realCheck(_line, _pattern)
|
|
|
|
return _line:match(_pattern)
|
|
|
|
end
|
|
|
|
|
|
|
|
if pattern:find('$VALUE') then
|
|
|
|
local k, v, c
|
|
|
|
k, v, c = _realCheck(line, pattern:gsub('$VALUE', QVALREPL))
|
|
|
|
if k ~= nil then
|
|
|
|
return k,v, c
|
|
|
|
end
|
|
|
|
return _realCheck(line, pattern:gsub('$VALUE', WORDREPL))
|
|
|
|
else
|
|
|
|
return _realCheck(line, pattern)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-21 23:42:20 +00:00
|
|
|
-- str in this table is a regex pattern. It will automatically be anchored to
|
|
|
|
-- the beginning of a line and any preceding whitespace will be skipped. The
|
|
|
|
-- pattern should have no more than two captures patterns, which correspond to
|
|
|
|
-- the two parameters (usually 'key' and 'value') that are passed to the
|
2018-08-23 17:27:02 +00:00
|
|
|
-- process function. All trailing characters will be validated. Any $VALUE
|
|
|
|
-- token included in a pattern will be tried first with a quoted value capture
|
|
|
|
-- group, then a single-word value capture group. This is our kludge for Lua
|
|
|
|
-- regex not supporting branching.
|
2018-08-22 01:52:55 +00:00
|
|
|
--
|
|
|
|
-- We have two special entries in this table: the first is the first entry,
|
|
|
|
-- a full-line comment. The second is for 'exec' handling. Both have a single
|
|
|
|
-- capture group, but the difference is that the full-line comment pattern will
|
|
|
|
-- match the entire line. This does not run afoul of the later end of line
|
|
|
|
-- validation that we'll do after a match. However, the 'exec' pattern will.
|
|
|
|
-- We document the exceptions with a special 'groups' index that indicates
|
|
|
|
-- the number of capture groups, if not two. We'll use this later to do
|
|
|
|
-- validation on the proper entry.
|
2018-08-23 17:27:02 +00:00
|
|
|
--
|
2018-02-25 03:30:24 +00:00
|
|
|
local pattern_table = {
|
|
|
|
{
|
2018-08-21 23:42:20 +00:00
|
|
|
str = "(#.*)",
|
2018-02-25 03:30:24 +00:00
|
|
|
process = function(_, _) end,
|
2018-08-22 01:52:55 +00:00
|
|
|
groups = 1,
|
2018-02-12 15:32:00 +00:00
|
|
|
},
|
|
|
|
-- module_load="value"
|
2018-02-25 03:30:24 +00:00
|
|
|
{
|
2018-08-23 17:27:02 +00:00
|
|
|
str = MODULEEXPR .. "_load%s*=%s*$VALUE",
|
2018-02-12 15:32:00 +00:00
|
|
|
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-25 03:30:24 +00:00
|
|
|
end,
|
2018-02-12 15:32:00 +00:00
|
|
|
},
|
|
|
|
-- module_name="value"
|
2018-02-25 03:30:24 +00:00
|
|
|
{
|
2018-08-23 17:27:02 +00:00
|
|
|
str = MODULEEXPR .. "_name%s*=%s*$VALUE",
|
2018-02-12 15:32:00 +00:00
|
|
|
process = function(k, v)
|
2018-03-24 04:03:55 +00:00
|
|
|
setKey(k, "name", v)
|
2018-02-25 03:30:24 +00:00
|
|
|
end,
|
2018-02-12 15:32:00 +00:00
|
|
|
},
|
|
|
|
-- module_type="value"
|
2018-02-25 03:30:24 +00:00
|
|
|
{
|
2018-08-23 17:27:02 +00:00
|
|
|
str = MODULEEXPR .. "_type%s*=%s*$VALUE",
|
2018-02-12 15:32:00 +00:00
|
|
|
process = function(k, v)
|
2018-03-24 04:03:55 +00:00
|
|
|
setKey(k, "type", v)
|
2018-02-25 03:30:24 +00:00
|
|
|
end,
|
2018-02-12 15:32:00 +00:00
|
|
|
},
|
|
|
|
-- module_flags="value"
|
2018-02-25 03:30:24 +00:00
|
|
|
{
|
2018-08-23 17:27:02 +00:00
|
|
|
str = MODULEEXPR .. "_flags%s*=%s*$VALUE",
|
2018-02-12 15:32:00 +00:00
|
|
|
process = function(k, v)
|
2018-03-24 04:03:55 +00:00
|
|
|
setKey(k, "flags", v)
|
2018-02-25 03:30:24 +00:00
|
|
|
end,
|
2018-02-12 15:32:00 +00:00
|
|
|
},
|
|
|
|
-- module_before="value"
|
2018-02-25 03:30:24 +00:00
|
|
|
{
|
2018-08-23 17:27:02 +00:00
|
|
|
str = MODULEEXPR .. "_before%s*=%s*$VALUE",
|
2018-02-12 15:32:00 +00:00
|
|
|
process = function(k, v)
|
2018-03-24 04:03:55 +00:00
|
|
|
setKey(k, "before", v)
|
2018-02-25 03:30:24 +00:00
|
|
|
end,
|
2018-02-12 15:32:00 +00:00
|
|
|
},
|
|
|
|
-- module_after="value"
|
2018-02-25 03:30:24 +00:00
|
|
|
{
|
2018-08-23 17:27:02 +00:00
|
|
|
str = MODULEEXPR .. "_after%s*=%s*$VALUE",
|
2018-02-12 15:32:00 +00:00
|
|
|
process = function(k, v)
|
2018-03-24 04:03:55 +00:00
|
|
|
setKey(k, "after", v)
|
2018-02-25 03:30:24 +00:00
|
|
|
end,
|
2018-02-12 15:32:00 +00:00
|
|
|
},
|
|
|
|
-- module_error="value"
|
2018-02-25 03:30:24 +00:00
|
|
|
{
|
2018-08-23 17:27:02 +00:00
|
|
|
str = MODULEEXPR .. "_error%s*=%s*$VALUE",
|
2018-02-12 15:32:00 +00:00
|
|
|
process = function(k, v)
|
2018-03-24 04:03:55 +00:00
|
|
|
setKey(k, "error", v)
|
2018-02-25 03:30:24 +00:00
|
|
|
end,
|
2018-02-12 15:32:00 +00:00
|
|
|
},
|
|
|
|
-- exec="command"
|
2018-02-25 03:30:24 +00:00
|
|
|
{
|
2018-08-23 17:27:02 +00:00
|
|
|
str = "exec%s*=%s*" .. QVALEXPR,
|
2018-02-22 04:15:02 +00:00
|
|
|
process = function(k, _)
|
2018-03-07 18:37:04 +00:00
|
|
|
if cli_execute_unparsed(k) ~= 0 then
|
2018-03-01 02:31:28 +00:00
|
|
|
print(MSG_FAILEXEC:format(k))
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
2018-02-25 03:30:24 +00:00
|
|
|
end,
|
2018-08-22 01:52:55 +00:00
|
|
|
groups = 1,
|
2018-02-12 15:32:00 +00:00
|
|
|
},
|
|
|
|
-- env_var="value"
|
2018-02-25 03:30:24 +00:00
|
|
|
{
|
2018-08-23 17:27:02 +00:00
|
|
|
str = "([%w%p]+)%s*=%s*$VALUE",
|
2018-02-12 15:32:00 +00:00
|
|
|
process = function(k, v)
|
2018-06-10 02:36:38 +00:00
|
|
|
if setEnv(k, processEnvVar(v)) ~= 0 then
|
2018-03-01 02:31:28 +00:00
|
|
|
print(MSG_FAILSETENV:format(k, v))
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
2018-02-25 03:30:24 +00:00
|
|
|
end,
|
2018-02-12 15:32:00 +00:00
|
|
|
},
|
|
|
|
-- env_var=num
|
2018-02-25 03:30:24 +00:00
|
|
|
{
|
2018-08-21 23:42:20 +00:00
|
|
|
str = "([%w%p]+)%s*=%s*(-?%d+)",
|
2018-02-12 15:32:00 +00:00
|
|
|
process = function(k, v)
|
2018-06-10 02:36:38 +00:00
|
|
|
if setEnv(k, processEnvVar(v)) ~= 0 then
|
2018-03-01 02:31:28 +00:00
|
|
|
print(MSG_FAILSETENV:format(k, tostring(v)))
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
2018-02-25 03:30:24 +00:00
|
|
|
end,
|
|
|
|
},
|
2018-02-21 01:10:03 +00:00
|
|
|
}
|
2018-02-12 15:32:00 +00:00
|
|
|
|
2018-03-24 04:03:55 +00:00
|
|
|
local function isValidComment(line)
|
|
|
|
if line ~= nil then
|
|
|
|
local s = line:match("^%s*#.*")
|
|
|
|
if s == nil then
|
|
|
|
s = line:match("^%s*$")
|
|
|
|
end
|
|
|
|
if s == nil then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2018-10-07 01:53:43 +00:00
|
|
|
local function getBlacklist()
|
2018-10-27 04:10:42 +00:00
|
|
|
local blacklist = {}
|
2018-10-07 01:53:43 +00:00
|
|
|
local blacklist_str = loader.getenv('module_blacklist')
|
|
|
|
if blacklist_str == nil then
|
2018-10-27 04:10:42 +00:00
|
|
|
return blacklist
|
2018-10-07 01:53:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
for mod in blacklist_str:gmatch("[;, ]?([%w-_]+)[;, ]?") do
|
|
|
|
blacklist[mod] = true
|
|
|
|
end
|
|
|
|
return blacklist
|
|
|
|
end
|
|
|
|
|
2018-03-24 04:03:55 +00:00
|
|
|
local function loadModule(mod, silent)
|
|
|
|
local status = true
|
2018-10-07 01:53:43 +00:00
|
|
|
local blacklist = getBlacklist()
|
2018-03-24 04:03:55 +00:00
|
|
|
local pstatus
|
|
|
|
for k, v in pairs(mod) do
|
2018-04-01 00:22:51 +00:00
|
|
|
if v.load ~= nil and v.load:lower() == "yes" then
|
2018-10-07 01:53:43 +00:00
|
|
|
local module_name = v.name or k
|
|
|
|
if blacklist[module_name] ~= nil then
|
|
|
|
if not silent then
|
|
|
|
print(MSG_MODBLACKLIST:format(module_name))
|
|
|
|
end
|
|
|
|
goto continue
|
|
|
|
end
|
2018-10-25 02:14:35 +00:00
|
|
|
if not silent then
|
|
|
|
loader.printc(module_name .. "...")
|
|
|
|
end
|
2018-03-24 04:03:55 +00:00
|
|
|
local str = "load "
|
|
|
|
if v.type ~= nil then
|
|
|
|
str = str .. "-t " .. v.type .. " "
|
|
|
|
end
|
2018-10-07 01:53:43 +00:00
|
|
|
str = str .. module_name
|
2018-06-12 18:42:41 +00:00
|
|
|
if v.flags ~= nil then
|
|
|
|
str = str .. " " .. v.flags
|
|
|
|
end
|
2018-03-24 04:03:55 +00:00
|
|
|
if v.before ~= nil then
|
|
|
|
pstatus = cli_execute_unparsed(v.before) == 0
|
|
|
|
if not pstatus and not silent then
|
|
|
|
print(MSG_FAILEXBEF:format(v.before, k))
|
|
|
|
end
|
|
|
|
status = status and pstatus
|
|
|
|
end
|
|
|
|
|
|
|
|
if cli_execute_unparsed(str) ~= 0 then
|
2018-10-25 02:14:35 +00:00
|
|
|
-- XXX Temporary shim: don't break the boot if
|
|
|
|
-- loader hadn't been recompiled with this
|
|
|
|
-- function exposed.
|
|
|
|
if loader.command_error then
|
|
|
|
print(loader.command_error())
|
|
|
|
end
|
2018-03-24 04:03:55 +00:00
|
|
|
if not silent then
|
2018-10-25 02:14:35 +00:00
|
|
|
print("failed!")
|
2018-03-24 04:03:55 +00:00
|
|
|
end
|
|
|
|
if v.error ~= nil then
|
|
|
|
cli_execute_unparsed(v.error)
|
|
|
|
end
|
|
|
|
status = false
|
2018-10-25 02:14:35 +00:00
|
|
|
elseif v.after ~= nil then
|
2018-03-24 04:03:55 +00:00
|
|
|
pstatus = cli_execute_unparsed(v.after) == 0
|
|
|
|
if not pstatus and not silent then
|
|
|
|
print(MSG_FAILEXAF:format(v.after, k))
|
|
|
|
end
|
2018-10-25 02:14:35 +00:00
|
|
|
if not silent then
|
|
|
|
print("ok")
|
|
|
|
end
|
2018-03-24 04:03:55 +00:00
|
|
|
status = status and pstatus
|
|
|
|
end
|
|
|
|
end
|
2018-10-07 01:53:43 +00:00
|
|
|
::continue::
|
2018-03-24 04:03:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return status
|
|
|
|
end
|
|
|
|
|
2018-02-24 20:21:21 +00:00
|
|
|
local function readFile(name, silent)
|
2018-02-24 20:07:39 +00:00
|
|
|
local f = io.open(name)
|
|
|
|
if f == nil then
|
|
|
|
if not silent then
|
2018-03-01 02:31:28 +00:00
|
|
|
print(MSG_FAILOPENCFG:format(name))
|
2018-02-24 20:07:39 +00:00
|
|
|
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)
|
|
|
|
|
2018-04-01 00:22:51 +00:00
|
|
|
if text == nil and not silent then
|
|
|
|
print(MSG_FAILREADCFG:format(name))
|
2018-02-24 20:07:39 +00:00
|
|
|
end
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
|
2018-02-24 20:21:21 +00:00
|
|
|
local function checkNextboot()
|
2018-07-27 11:35:58 +00:00
|
|
|
local nextboot_file = loader.getenv("nextboot_conf")
|
2018-02-24 03:35:35 +00:00
|
|
|
if nextboot_file == nil then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-02-24 20:21:21 +00:00
|
|
|
local text = readFile(nextboot_file, true)
|
2018-02-24 20:07:39 +00:00
|
|
|
if text == nil then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if text:match("^nextboot_enable=\"NO\"") ~= nil then
|
|
|
|
-- We're done; nextboot is not enabled
|
|
|
|
return
|
2018-02-24 03:35:35 +00:00
|
|
|
end
|
|
|
|
|
2018-02-24 20:07:39 +00:00
|
|
|
if not config.parse(text) then
|
2018-03-02 02:39:16 +00:00
|
|
|
print(MSG_FAILPARSECFG:format(nextboot_file))
|
2018-02-24 03:35:35 +00:00
|
|
|
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
|
2018-02-24 20:24:57 +00:00
|
|
|
-- check for that on load.
|
2018-02-24 04:02:06 +00:00
|
|
|
-- 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-20 14:45:58 +00:00
|
|
|
-- Module exports
|
2018-03-13 02:59:13 +00:00
|
|
|
config.verbose = false
|
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-04-01 00:22:51 +00:00
|
|
|
return carousel_choices[id] or 1
|
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-27 22:07:41 +00:00
|
|
|
-- Returns true if we processed the file successfully, false if we did not.
|
|
|
|
-- If 'silent' is true, being unable to read the file is not considered a
|
|
|
|
-- failure.
|
2018-02-24 20:07:39 +00:00
|
|
|
function config.processFile(name, silent)
|
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 20:21:21 +00:00
|
|
|
local text = readFile(name, silent)
|
2018-02-21 01:35:19 +00:00
|
|
|
if text == nil then
|
2018-02-27 22:07:41 +00:00
|
|
|
return silent
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
|
2018-02-24 20:00:31 +00:00
|
|
|
return config.parse(text)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- silent runs will not return false if we fail to open the file
|
|
|
|
function config.parse(text)
|
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-22 04:15:02 +00:00
|
|
|
for _, val in ipairs(pattern_table) do
|
2018-08-21 23:42:20 +00:00
|
|
|
local pattern = '^%s*' .. val.str .. '%s*(.*)';
|
2018-08-22 01:52:55 +00:00
|
|
|
local cgroups = val.groups or 2
|
2018-08-23 17:27:02 +00:00
|
|
|
local k, v, c = checkPattern(line, pattern)
|
2018-02-21 01:35:19 +00:00
|
|
|
if k ~= nil then
|
2018-08-22 01:52:55 +00:00
|
|
|
-- Offset by one, drats
|
|
|
|
if cgroups == 1 then
|
|
|
|
c = v
|
|
|
|
v = nil
|
|
|
|
end
|
2018-02-12 15:32:00 +00:00
|
|
|
|
2018-03-24 04:03:55 +00:00
|
|
|
if isValidComment(c) then
|
2018-02-21 01:10:03 +00:00
|
|
|
val.process(k, v)
|
2018-08-22 01:52:55 +00:00
|
|
|
goto nextline
|
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-08-22 01:52:55 +00:00
|
|
|
print(MSG_MALFORMED:format(n, line))
|
|
|
|
status = false
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
2018-08-22 01:52:55 +00:00
|
|
|
::nextline::
|
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
|
|
|
|
|
2020-04-30 21:04:39 +00:00
|
|
|
function config.readConf(file, loaded_files)
|
|
|
|
if loaded_files == nil then
|
|
|
|
loaded_files = {}
|
|
|
|
end
|
2020-04-28 02:03:03 +00:00
|
|
|
|
2020-04-30 21:04:39 +00:00
|
|
|
if loaded_files[file] ~= nil then
|
|
|
|
return
|
|
|
|
end
|
2020-04-28 02:03:03 +00:00
|
|
|
|
2020-04-30 21:04:39 +00:00
|
|
|
print("Loading " .. file)
|
|
|
|
|
|
|
|
-- The final value of loader_conf_files is not important, so just
|
|
|
|
-- clobber it here. We'll later check if it's no longer nil and process
|
|
|
|
-- the new value for files to read.
|
|
|
|
setEnv("loader_conf_files", nil)
|
|
|
|
|
|
|
|
-- 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.processFile(file, true) then
|
|
|
|
print(MSG_FAILPARSECFG:format(file))
|
|
|
|
end
|
|
|
|
|
|
|
|
loaded_files[file] = true
|
|
|
|
|
|
|
|
-- Going to process "loader_conf_files" extra-files
|
|
|
|
local loader_conf_files = getEnv("loader_conf_files")
|
|
|
|
if loader_conf_files ~= nil then
|
|
|
|
for name in loader_conf_files:gmatch("[%w%p]+") do
|
|
|
|
config.readConf(name, loaded_files)
|
2020-04-28 02:03:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
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
|
2018-02-24 20:21:21 +00:00
|
|
|
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-24 20:21:21 +00:00
|
|
|
local function tryLoad(names)
|
2018-02-12 15:32:00 +00:00
|
|
|
for name in names:gmatch("([^;]+)%s*;?") do
|
2018-06-19 15:05:31 +00:00
|
|
|
local r = loader.perform("load " .. name ..
|
|
|
|
" " .. flags)
|
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-08-25 04:28:02 +00:00
|
|
|
local function getModulePath()
|
|
|
|
local module_path = loader.getenv("module_path")
|
|
|
|
local kernel_path = loader.getenv("kernel_path")
|
|
|
|
|
|
|
|
if kernel_path == nil then
|
|
|
|
return module_path
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Strip the loaded kernel path from module_path. This currently assumes
|
|
|
|
-- that the kernel path will be prepended to the module_path when it's
|
|
|
|
-- found.
|
|
|
|
kernel_path = escapeName(kernel_path .. ';')
|
|
|
|
return module_path:gsub(kernel_path, '')
|
|
|
|
end
|
|
|
|
|
2018-02-24 20:21:21 +00:00
|
|
|
local function loadBootfile()
|
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-24 20:21:21 +00:00
|
|
|
return tryLoad(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-24 20:21:21 +00:00
|
|
|
local res = loadBootfile()
|
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-03-01 02:31:28 +00:00
|
|
|
print(MSG_DEFAULTKERNFAIL)
|
2018-02-21 01:10:03 +00:00
|
|
|
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-08-25 04:28:02 +00:00
|
|
|
local module_path = getModulePath()
|
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)
|
2018-02-24 20:21:21 +00:00
|
|
|
res = loadBootfile()
|
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-08-25 04:28:02 +00:00
|
|
|
loader.setenv("kernel_path", v)
|
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-24 20:21:21 +00:00
|
|
|
res = tryLoad(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-03-01 02:31:28 +00:00
|
|
|
print(MSG_KERNFAIL:format(kernel))
|
2018-02-21 01:10:03 +00:00
|
|
|
return false
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-24 20:21:21 +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
|
|
|
|
2018-06-06 18:28:17 +00:00
|
|
|
function config.load(file, reloading)
|
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
|
|
|
|
|
2020-04-30 21:04:39 +00:00
|
|
|
config.readConf(file)
|
2018-02-12 15:32:00 +00:00
|
|
|
|
2018-02-24 20:21:21 +00:00
|
|
|
checkNextboot()
|
2018-02-24 03:35:35 +00:00
|
|
|
|
2018-04-01 00:22:51 +00:00
|
|
|
local verbose = loader.getenv("verbose_loading") or "no"
|
2018-03-13 02:59:13 +00:00
|
|
|
config.verbose = verbose:lower() == "yes"
|
2018-06-06 18:28:17 +00:00
|
|
|
if not reloading then
|
|
|
|
hook.runAll("config.loaded")
|
|
|
|
end
|
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 = {}
|
2018-03-24 04:00:01 +00:00
|
|
|
restoreEnv()
|
2018-06-06 18:28:17 +00:00
|
|
|
config.load(file, true)
|
2018-03-21 03:07:16 +00:00
|
|
|
hook.runAll("config.reloaded")
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
|
2018-02-19 14:21:56 +00:00
|
|
|
function config.loadelf()
|
2018-08-23 16:26:03 +00:00
|
|
|
local xen_kernel = loader.getenv('xen_kernel')
|
2018-02-21 01:10:03 +00:00
|
|
|
local kernel = config.kernel_selected or config.kernel_loaded
|
2020-03-18 16:21:11 +00:00
|
|
|
local status
|
2018-02-12 15:32:00 +00:00
|
|
|
|
2018-08-23 16:26:03 +00:00
|
|
|
if xen_kernel ~= nil then
|
|
|
|
print(MSG_XENKERNLOADING)
|
|
|
|
if cli_execute_unparsed('load ' .. xen_kernel) ~= 0 then
|
|
|
|
print(MSG_XENKERNFAIL:format(xen_kernel))
|
2018-10-25 02:14:35 +00:00
|
|
|
return false
|
2018-08-23 16:26:03 +00:00
|
|
|
end
|
|
|
|
end
|
2018-03-01 02:31:28 +00:00
|
|
|
print(MSG_KERNLOADING)
|
2020-03-18 16:21:11 +00:00
|
|
|
if not config.loadKernel(kernel) then
|
2018-10-25 02:14:35 +00:00
|
|
|
return false
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
2020-03-18 16:21:11 +00:00
|
|
|
hook.runAll("kernel.loaded")
|
2018-02-12 15:32:00 +00:00
|
|
|
|
2018-03-01 02:31:28 +00:00
|
|
|
print(MSG_MODLOADING)
|
2020-01-25 03:52:16 +00:00
|
|
|
status = loadModule(modules, not config.verbose)
|
|
|
|
hook.runAll("modules.loaded")
|
|
|
|
return status
|
2018-02-12 15:32:00 +00:00
|
|
|
end
|
|
|
|
|
2018-06-06 18:28:17 +00:00
|
|
|
hook.registerType("config.loaded")
|
2018-03-21 03:07:16 +00:00
|
|
|
hook.registerType("config.reloaded")
|
2020-03-18 17:01:03 +00:00
|
|
|
hook.registerType("kernel.loaded")
|
2020-01-25 03:52:16 +00:00
|
|
|
hook.registerType("modules.loaded")
|
2018-02-21 01:10:03 +00:00
|
|
|
return config
|