git: 2b78b1ef093e - stable/12 - MFC lualoader: module-manipulation commands

Kyle Evans kevans at FreeBSD.org
Sun Dec 27 21:43:17 UTC 2020


The branch stable/12 has been updated by kevans:

URL: https://cgit.FreeBSD.org/src/commit/?id=2b78b1ef093e430196eb43c67682207de989a1d5

commit 2b78b1ef093e430196eb43c67682207de989a1d5
Author:     Kyle Evans <kevans at FreeBSD.org>
AuthorDate: 2020-12-12 05:57:42 +0000
Commit:     Kyle Evans <kevans at FreeBSD.org>
CommitDate: 2020-12-27 21:42:02 +0000

    MFC lualoader: module-manipulation commands
    
    4634bb1f: lualoader: provide module-manipulation commands
    
    Specifically, we have:
    - enable-module
    - disable-module
    - toggle-module
    
    These can be used to add/remove modules to be loaded or force modules to be
    loaded in spite of modules_blacklist. In the typical case, a user is
    expected to use them to recover an issue happening due to a module directive
    they've added to their loader.conf or because they discover that they've
    under-specified what to load.
    
    10aeb6cd: lualoader: config: fix module enabled check
    
    A last minute rewrite left this logically wrong; if it's present in
    modules_blacklist, then we do not load it.
    
    7ed84fa1: lualoader: cli: provide a show-module-options loader command
    
    This effectively dumps everything lualoader knows about to the console using
    the libsa pager; that particular lua interface was added in r368591.
    
    A pager stub implementation has been added that just dumps the output as-is
    as a compat shim for older loader binaries that do not have lpager. This
    stub should be moved into a more appropriate .lua file if we add anything
    else that needs the pager.
    
    (cherry picked from commit 4634bb1f4052ff5f1c0a423fd8cce11396ca7fd2)
    (cherry picked from commit 10aeb6cdab8fb09e2cc3ee2d8b2c68c395481c23)
    (cherry picked from commit 7ed84fa14b00cdacfe9b43019cba7a14b33af352)
---
 stand/lua/cli.lua      | 106 +++++++++++++++++++++++++++++++++++++++++++++++++
 stand/lua/cli.lua.8    |  44 ++++++++++++++++----
 stand/lua/config.lua   |  48 +++++++++++++++++++++-
 stand/lua/config.lua.8 |  30 +++++++++++++-
 4 files changed, 218 insertions(+), 10 deletions(-)

diff --git a/stand/lua/cli.lua b/stand/lua/cli.lua
index 188292191448..d1947ca1021d 100644
--- a/stand/lua/cli.lua
+++ b/stand/lua/cli.lua
@@ -32,6 +32,18 @@ local core = require("core")
 
 local cli = {}
 
+if not pager then
+	-- shim for the pager module that just doesn't do it.
+	-- XXX Remove after 12.2 goes EoL.
+	pager = {
+		open = function() end,
+		close = function() end,
+		output = function(str)
+			printc(str)
+		end,
+	}
+end
+
 -- Internal function
 -- Parses arguments to boot and returns two values: kernel_name, argstr
 -- Defaults to nil and "" respectively.
@@ -65,6 +77,14 @@ local function parseBootArgs(argv, with_kernel)
 	end
 end
 
+local function setModule(module, loading)
+	if loading and config.enableModule(module) then
+		print(module .. " will be loaded")
+	elseif not loading and config.disableModule(module) then
+		print(module .. " will not be loaded")
+	end
+end
+
 -- Declares a global function cli_execute that attempts to dispatch the
 -- arguments passed as a lua function. This gives lua a chance to intercept
 -- builtin CLI commands like "boot"
@@ -134,6 +154,92 @@ cli['reload-conf'] = function(...)
 	config.reload()
 end
 
+cli["enable-module"] = function(...)
+	local _, argv = cli.arguments(...)
+	if #argv == 0 then
+		print("usage error: enable-module module")
+		return
+	end
+
+	setModule(argv[1], true)
+end
+
+cli["disable-module"] = function(...)
+	local _, argv = cli.arguments(...)
+	if #argv == 0 then
+		print("usage error: disable-module module")
+		return
+	end
+
+	setModule(argv[1], false)
+end
+
+cli["toggle-module"] = function(...)
+	local _, argv = cli.arguments(...)
+	if #argv == 0 then
+		print("usage error: toggle-module module")
+		return
+	end
+
+	local module = argv[1]
+	setModule(module, not config.isModuleEnabled(module))
+end
+
+cli["show-module-options"] = function()
+	local module_info = config.getModuleInfo()
+	local modules = module_info['modules']
+	local blacklist = module_info['blacklist']
+	local lines = {}
+
+	for module, info in pairs(modules) do
+		if #lines > 0 then
+			lines[#lines + 1] = ""
+		end
+
+		lines[#lines + 1] = "Name:        " .. module
+		if info.name then
+			lines[#lines + 1] = "Path:        " .. info.name
+		end
+
+		if info.type then
+			lines[#lines + 1] = "Type:        " .. info.type
+		end
+
+		if info.flags then
+			lines[#lines + 1] = "Flags:       " .. info.flags
+		end
+
+		if info.before then
+			lines[#lines + 1] = "Before load: " .. info.before
+		end
+
+		if info.after then
+			lines[#lines + 1] = "After load:  " .. info.after
+		end
+
+		if info.error then
+			lines[#lines + 1] = "Error:       " .. info.error
+		end
+
+		local status
+		if blacklist[module] and not info.force then
+			status = "Blacklisted"
+		elseif info.load == "YES" then
+			status = "Load"
+		else
+			status = "Don't load"
+		end
+
+		lines[#lines + 1] = "Status:      " .. status
+	end
+
+	pager.open()
+	for i, v in ipairs(lines) do
+		pager.output(v .. "\n")
+	end
+	pager.close()
+end
+
 -- Used for splitting cli varargs into cmd_name and the rest of argv
 function cli.arguments(...)
 	local argv = {...}
diff --git a/stand/lua/cli.lua.8 b/stand/lua/cli.lua.8
index ac9ed3580448..390831d21452 100644
--- a/stand/lua/cli.lua.8
+++ b/stand/lua/cli.lua.8
@@ -26,7 +26,7 @@
 .\"
 .\" $FreeBSD$
 .\"
-.Dd September 13, 2019
+.Dd December 17, 2020
 .Dt CLI.LUA 8
 .Os
 .Sh NAME
@@ -77,14 +77,28 @@ This function may be invoked by a user at the loader prompt by simply typing
 .Ic foo .
 Arguments may be passed to it as usual, space-delimited.
 .Ss Default Commands
-As of present, the
+The
 .Nm
-module by default provides commands for
-.Ic autoboot ,
-.Ic boot ,
-.Ic boot-conf ,
-and
-.Ic reload-conf .
+module provides the following default commands:
+.Bl -bullet
+.\"-width toggle-module -offset indent
+.It
+.Ic autoboot
+.It
+.Ic boot
+.It
+.Ic boot-conf
+.It
+.Ic reload-conf
+.It
+.Ic enable-module
+.It
+.Ic disable-module
+.It
+.Ic toggle-module
+.It
+.Ic show-module-options
+.El
 .Pp
 For
 .Ic autoboot ,
@@ -103,6 +117,20 @@ The
 command will reload the configuration from disk.
 This is useful if you have manually changed currdev and would like to easily
 reload the configuration from the new device.
+.Pp
+The
+.Ic enable-module ,
+.Ic disable-module ,
+and
+.Ic toggle-module
+commands manipulate the list of modules to be loaded along with the kernel.
+Modules blacklisted are considered disabled by
+.Ic toggle-module .
+These commands will override any such restriction as needed.
+The
+.Ic show-module-options
+command will dump the list of modules that loader has been made aware of and
+any applicable options using paged output.
 .Ss Exported Functions
 The following functions are exported from
 .Nm :
diff --git a/stand/lua/config.lua b/stand/lua/config.lua
index 0d9e78aa02dc..5b554806fc9f 100644
--- a/stand/lua/config.lua
+++ b/stand/lua/config.lua
@@ -312,7 +312,7 @@ local function loadModule(mod, silent)
 	for k, v in pairs(mod) do
 		if v.load ~= nil and v.load:lower() == "yes" then
 			local module_name = v.name or k
-			if blacklist[module_name] ~= nil then
+			if not v.force and blacklist[module_name] ~= nil then
 				if not silent then
 					print(MSG_MODBLACKLIST:format(module_name))
 				end
@@ -673,6 +673,52 @@ function config.loadelf()
 	return status
 end
 
+function config.enableModule(modname)
+	if modules[modname] == nil then
+		modules[modname] = {}
+	elseif modules[modname].load == "YES" then
+		modules[modname].force = true
+		return true
+	end
+
+	modules[modname].load = "YES"
+	modules[modname].force = true
+	return true
+end
+
+function config.disableModule(modname)
+	if modules[modname] == nil then
+		return false
+	elseif modules[modname].load ~= "YES" then
+		return true
+	end
+
+	modules[modname].load = "NO"
+	modules[modname].force = nil
+	return true
+end
+
+function config.isModuleEnabled(modname)
+	local mod = modules[modname]
+	if not mod or mod.load ~= "YES" then
+		return false
+	end
+
+	if mod.force then
+		return true
+	end
+
+	local blacklist = getBlacklist()
+	return not blacklist[modname]
+end
+
+function config.getModuleInfo()
+	return {
+		modules = modules,
+		blacklist = getBlacklist()
+	}
+end
+
 hook.registerType("config.loaded")
 hook.registerType("config.reloaded")
 hook.registerType("kernel.loaded")
diff --git a/stand/lua/config.lua.8 b/stand/lua/config.lua.8
index 098b607271f7..4fc51c90045f 100644
--- a/stand/lua/config.lua.8
+++ b/stand/lua/config.lua.8
@@ -26,7 +26,7 @@
 .\"
 .\" $FreeBSD$
 .\"
-.Dd April 30, 2020
+.Dd December 17, 2020
 .Dt CONFIG.LUA 8
 .Os
 .Sh NAME
@@ -184,6 +184,34 @@ This will be called by the Lua intercepted
 and
 .Ic boot
 commands.
+.It Fn config.enableModule modname
+Marks a module named
+.Fa modname
+to be loaded during
+.Fn config.loadelf .
+If the module was previously blacklisted, then it will be forcefully allowed to
+load.
+.It Fn config.disableModule modname
+Marks a module named
+.Fa modname
+to not be loaded during
+.Fn config.loadelf .
+.It Fn config.isModuleEnabled modname
+Checks if the module named
+.Fa modname
+will be loaded during
+.Fn config.loadelf .
+It checks both that the module is marked for loading and that it is either
+forced or not blacklisted.
+.It Fn config.getModuleInfo
+Returns a table with
+.Dq modules
+and
+.Dq blacklist
+tables describing the modules that the config module has been made aware of via
+.Xr loader.conf 5
+as well as a representation of
+.Ar module_blacklist .
 .El
 .Ss Defined Hooks
 The following hooks are defined in


More information about the dev-commits-src-all mailing list