stand/lua: per-product conf if requested via product_vars

If product_vars is set, it must be a space separated list of environment
variable names to walk through to guess the product. Each time a product can be
guessed (i.e., the corresponding variable is defined), prepend
/boot/loader.conf.d/PRODUCT/ to loader_conf_dirs.

It can be typically used as follow:

    smbios.system.planar.maker="PLANAR_MAKER"
    smbios.system.planar.product="PLANAR_PRODUCT"
    smbios.system.product="PRODUCT"
    uboot.m_product="M_PRODUCT"
    product_vars="smbios.system.planar.maker smbios.system.planar.product smbios.system.product uboot.m_product"

to read files found in the following directories, in that order:

    /boot/loader.conf.d/PLANAR_MAKER
    /boot/loader.conf.d/PLANAR_PRODUCT
    /boot/loader.conf.d/PRODUCT
    /boot/loader.conf.d/M_PRODUCT
This commit is contained in:
Stéphane Rochoy 2023-05-04 09:23:47 +02:00 committed by Warner Losh
parent 619f455b8f
commit 754cac4b28
2 changed files with 58 additions and 1 deletions

View file

@ -21,7 +21,7 @@
.\" 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.
.Dd January 10, 2024
.Dd February 2, 2024
.Dt LOADER.CONF 5
.Os
.Sh NAME
@ -138,6 +138,38 @@ present file.
should be treated as write-only.
One cannot depend on any value remaining in the loader environment or carried
over into the kernel environment.
.It Ar product_vars
When set, must be a space separated list of environment variable names to walk
through to guess product information.
The order matters as reading a config file override the previously defined
values.
Undefined variables are silently ignored.
.Pp
When product information can be guessed, for each product information found,
append
.Pa /boot/loader.conf.d/PRODUCT
to
.Ar loader_conf_dirs .
It can be typically used as follow:
.Bd -literal
smbios.system.planar.maker="PLANAR_MAKER"
smbios.system.planar.product="PLANAR_PRODUCT"
smbios.system.product="PRODUCT"
uboot.m_product="M_PRODUCT"
product_vars="smbios.system.planar.maker smbios.system.planar.product smbios.system.product uboot.m_product"
.Ed
.Pp
to read files found in the following directories, in that order:
.Bl -bullet -compact
.It
.Pa /boot/loader.conf.d/PLANAR_MAKER
.It
.Pa /boot/loader.conf.d/PLANAR_PRODUCT
.It
.Pa /boot/loader.conf.d/PRODUCT
.It
.Pa /boot/loader.conf.d/M_PRODUCT
.El
.It Ar kernel
Name of the kernel to be loaded.
If no kernel name is set, no additional

View file

@ -658,12 +658,37 @@ function config.readConf(file, loaded_files)
if load_conf_dirs then
local loader_conf_dirs = getEnv("loader_conf_dirs")
-- If product_vars is set, it must be a list of environment variable names
-- to walk through to guess product information. The order matters as
-- reading a config files override the previously defined values.
--
-- If product information can be guessed, for each product information
-- found, also read config files found in /boot/loader.conf.d/PRODUCT/.
local product_vars = getEnv("product_vars")
if product_vars then
local product_conf_dirs = ""
for var in product_vars:gmatch("%S+") do
local product = getEnv(var)
if product then
product_conf_dirs = product_conf_dirs .. " /boot/loader.conf.d/" .. product
end
end
if loader_conf_dirs then
loader_conf_dirs = loader_conf_dirs .. product_conf_dirs
else
loader_conf_dirs = product_conf_dirs
end
end
if loader_conf_dirs ~= nil then
for name in loader_conf_dirs:gmatch("[%w%p]+") do
if lfs.attributes(name, "mode") ~= "directory" then
print(MSG_FAILDIR:format(name))
goto nextdir
end
for cfile in lfs.dir(name) do
if cfile:match(".conf$") then
local fpath = name .. "/" .. cfile