1-- 2-- SPDX-License-Identifier: BSD-2-Clause 3-- 4-- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org> 5-- Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org> 6-- All rights reserved. 7-- 8-- Redistribution and use in source and binary forms, with or without 9-- modification, are permitted provided that the following conditions 10-- are met: 11-- 1. Redistributions of source code must retain the above copyright 12-- notice, this list of conditions and the following disclaimer. 13-- 2. Redistributions in binary form must reproduce the above copyright 14-- notice, this list of conditions and the following disclaimer in the 15-- documentation and/or other materials provided with the distribution. 16-- 17-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20-- ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27-- SUCH DAMAGE. 28-- 29 30local hook = require("hook") 31 32local config = {} 33local modules = {} 34local carousel_choices = {} 35-- Which variables we changed 36local env_changed = {} 37-- Values to restore env to (nil to unset) 38local env_restore = {} 39 40local MSG_FAILDIR = "Failed to load conf dir '%s': not a directory" 41local MSG_FAILEXEC = "Failed to exec '%s'" 42local MSG_FAILSETENV = "Failed to '%s' with value: %s" 43local MSG_FAILOPENCFG = "Failed to open config: '%s'" 44local MSG_FAILREADCFG = "Failed to read config: '%s'" 45local MSG_FAILPARSECFG = "Failed to parse config: '%s'" 46local MSG_FAILEXECLUA = "Failed to execute lua conf '%s': '%s'" 47local MSG_FAILPARSEVAR = "Failed to parse variable '%s': %s" 48local MSG_FAILEXBEF = "Failed to execute '%s' before loading '%s'" 49local MSG_FAILEXAF = "Failed to execute '%s' after loading '%s'" 50local MSG_MALFORMED = "Malformed line (%d):\n\t'%s'" 51local MSG_DEFAULTKERNFAIL = "No kernel set, failed to load from module_path" 52local MSG_KERNFAIL = "Failed to load kernel '%s'" 53local MSG_XENKERNFAIL = "Failed to load Xen kernel '%s'" 54local MSG_XENKERNLOADING = "Loading Xen kernel..." 55local MSG_KERNLOADING = "Loading kernel..." 56local MSG_MODLOADING = "Loading configured modules..." 57local MSG_MODBLACKLIST = "Not loading blacklisted module '%s'" 58 59local MSG_FAILSYN_QUOTE = "Stray quote at position '%d'" 60local MSG_FAILSYN_EOLESC = "Stray escape at end of line" 61local MSG_FAILSYN_EOLVAR = "Unescaped $ at end of line" 62local MSG_FAILSYN_BADVAR = "Malformed variable expression at position '%d'" 63 64-- MODULEEXPR should more or less allow the exact same set of characters as the 65-- env_var entries in the pattern table. This is perhaps a good target for a 66-- little refactoring. 67local MODULEEXPR = '([%w%d-_.]+)' 68local QVALEXPR = '"(.*)"' 69local QVALREPL = QVALEXPR:gsub('%%', '%%%%') 70local WORDEXPR = "([-%w%d][-%w%d_.]*)" 71local WORDREPL = WORDEXPR:gsub('%%', '%%%%') 72 73-- Entries that should never make it into the environment; each one should have 74-- a documented reason for its existence, and these should all be implementation 75-- details of the config module. 76local loader_env_restricted_table = { 77 -- loader_conf_files should be considered write-only, and consumers 78 -- should not rely on any particular value; it's a loader implementation 79 -- detail. Moreover, it's not a particularly useful variable to have in 80 -- the kenv. Save the overhead, let it get fetched other ways. 81 loader_conf_files = true, 82} 83 84local function restoreEnv() 85 -- Examine changed environment variables 86 for k, v in pairs(env_changed) do 87 local restore_value = env_restore[k] 88 if restore_value == nil then 89 -- This one doesn't need restored for some reason 90 goto continue 91 end 92 local current_value = loader.getenv(k) 93 if current_value ~= v then 94 -- This was overwritten by some action taken on the menu 95 -- most likely; we'll leave it be. 96 goto continue 97 end 98 restore_value = restore_value.value 99 if restore_value ~= nil then 100 loader.setenv(k, restore_value) 101 else 102 loader.unsetenv(k) 103 end 104 ::continue:: 105 end 106 107 env_changed = {} 108 env_restore = {} 109end 110 111-- XXX This getEnv/setEnv should likely be exported at some point. We can save 112-- the call back into loader.getenv for any variable that's been set or 113-- overridden by any loader.conf using this implementation with little overhead 114-- since we're already tracking the values. 115local function getEnv(key) 116 if loader_env_restricted_table[key] ~= nil or 117 env_changed[key] ~= nil then 118 return env_changed[key] 119 end 120 121 return loader.getenv(key) 122end 123 124local function setEnv(key, value) 125 env_changed[key] = value 126 127 if loader_env_restricted_table[key] ~= nil then 128 return 0 129 end 130 131 -- Track the original value for this if we haven't already 132 if env_restore[key] == nil then 133 env_restore[key] = {value = loader.getenv(key)} 134 end 135 136 return loader.setenv(key, value) 137end 138 139-- name here is one of 'name', 'type', flags', 'before', 'after', or 'error.' 140-- These are set from lines in loader.conf(5): ${key}_${name}="${value}" where 141-- ${key} is a module name. 142local function setKey(key, name, value) 143 if modules[key] == nil then 144 modules[key] = {} 145 end 146 modules[key][name] = value 147end 148 149-- Escapes the named value for use as a literal in a replacement pattern. 150-- e.g. dhcp.host-name gets turned into dhcp%.host%-name to remove the special 151-- meaning. 152local function escapeName(name) 153 return name:gsub("([%p])", "%%%1") 154end 155 156local function processEnvVar(value) 157 local pval, vlen = '', #value 158 local nextpos, vdelim, vinit = 1 159 local vpat 160 for i = 1, vlen do 161 if i < nextpos then 162 goto nextc 163 end 164 165 local c = value:sub(i, i) 166 if c == '\\' then 167 if i == vlen then 168 return nil, MSG_FAILSYN_EOLESC 169 end 170 nextpos = i + 2 171 pval = pval .. value:sub(i + 1, i + 1) 172 elseif c == '"' then 173 return nil, MSG_FAILSYN_QUOTE:format(i) 174 elseif c == "$" then 175 if i == vlen then 176 return nil, MSG_FAILSYN_EOLVAR 177 else 178 if value:sub(i + 1, i + 1) == "{" then 179 -- Skip ${ 180 vinit = i + 2 181 vdelim = '}' 182 vpat = "^([^}]+)}" 183 else 184 -- Skip the $ 185 vinit = i + 1 186 vdelim = nil 187 vpat = "^([%w][-%w%d_.]*)" 188 end 189 190 local name = value:match(vpat, vinit) 191 if not name then 192 return nil, MSG_FAILSYN_BADVAR:format(i) 193 else 194 nextpos = vinit + #name 195 if vdelim then 196 nextpos = nextpos + 1 197 end 198 199 local repl = loader.getenv(name) or "" 200 pval = pval .. repl 201 end 202 end 203 else 204 pval = pval .. c 205 end 206 ::nextc:: 207 end 208 209 return pval 210end 211 212local function checkPattern(line, pattern) 213 local function _realCheck(_line, _pattern) 214 return _line:match(_pattern) 215 end 216 217 if pattern:find('$VALUE') then 218 local k, v, c 219 k, v, c = _realCheck(line, pattern:gsub('$VALUE', QVALREPL)) 220 if k ~= nil then 221 return k,v, c 222 end 223 return _realCheck(line, pattern:gsub('$VALUE', WORDREPL)) 224 else 225 return _realCheck(line, pattern) 226 end 227end 228 229-- str in this table is a regex pattern. It will automatically be anchored to 230-- the beginning of a line and any preceding whitespace will be skipped. The 231-- pattern should have no more than two captures patterns, which correspond to 232-- the two parameters (usually 'key' and 'value') that are passed to the 233-- process function. All trailing characters will be validated. Any $VALUE 234-- token included in a pattern will be tried first with a quoted value capture 235-- group, then a single-word value capture group. This is our kludge for Lua 236-- regex not supporting branching. 237-- 238-- We have two special entries in this table: the first is the first entry, 239-- a full-line comment. The second is for 'exec' handling. Both have a single 240-- capture group, but the difference is that the full-line comment pattern will 241-- match the entire line. This does not run afoul of the later end of line 242-- validation that we'll do after a match. However, the 'exec' pattern will. 243-- We document the exceptions with a special 'groups' index that indicates 244-- the number of capture groups, if not two. We'll use this later to do 245-- validation on the proper entry. 246-- 247local pattern_table = { 248 { 249 luaexempt = true, 250 str = "(#.*)", 251 process = function(_, _) end, 252 groups = 1, 253 }, 254 -- module_load="value" 255 { 256 name = MODULEEXPR .. "_load%s*", 257 val = "%s*$VALUE", 258 process = function(k, v) 259 if modules[k] == nil then 260 modules[k] = {} 261 end 262 modules[k].load = v:upper() 263 setEnv(k .. "_load", v:upper()) 264 end, 265 }, 266 -- module_name="value" 267 { 268 name = MODULEEXPR .. "_name%s*", 269 val = "%s*$VALUE", 270 process = function(k, v) 271 setKey(k, "name", v) 272 setEnv(k .. "_name", v) 273 end, 274 }, 275 -- module_type="value" 276 { 277 name = MODULEEXPR .. "_type%s*", 278 val = "%s*$VALUE", 279 process = function(k, v) 280 setKey(k, "type", v) 281 setEnv(k .. "_type", v) 282 end, 283 }, 284 -- module_flags="value" 285 { 286 name = MODULEEXPR .. "_flags%s*", 287 val = "%s*$VALUE", 288 process = function(k, v) 289 setKey(k, "flags", v) 290 setEnv(k .. "_flags", v) 291 end, 292 }, 293 -- module_before="value" 294 { 295 name = MODULEEXPR .. "_before%s*", 296 val = "%s*$VALUE", 297 process = function(k, v) 298 setKey(k, "before", v) 299 setEnv(k .. "_before", v) 300 end, 301 }, 302 -- module_after="value" 303 { 304 name = MODULEEXPR .. "_after%s*", 305 val = "%s*$VALUE", 306 process = function(k, v) 307 setKey(k, "after", v) 308 setEnv(k .. "_after", v) 309 end, 310 }, 311 -- module_error="value" 312 { 313 name = MODULEEXPR .. "_error%s*", 314 val = "%s*$VALUE", 315 process = function(k, v) 316 setKey(k, "error", v) 317 setEnv(k .. "_error", v) 318 end, 319 }, 320 -- exec="command" 321 { 322 luaexempt = true, 323 name = "exec%s*", 324 val = "%s*" .. QVALEXPR, 325 process = function(k, _) 326 if cli_execute_unparsed(k) ~= 0 then 327 print(MSG_FAILEXEC:format(k)) 328 end 329 end, 330 groups = 1, 331 }, 332 -- env_var="value" or env_var=[word|num] 333 { 334 name = "([%w][%w%d-_.]*)%s*", 335 val = "%s*$VALUE", 336 process = function(k, v) 337 local pv, msg = processEnvVar(v) 338 if not pv then 339 print(MSG_FAILPARSEVAR:format(k, msg)) 340 return 341 end 342 if setEnv(k, pv) ~= 0 then 343 print(MSG_FAILSETENV:format(k, v)) 344 else 345 return pv 346 end 347 end, 348 }, 349} 350 351local function isValidComment(line) 352 if line ~= nil then 353 local s = line:match("^%s*#.*") 354 if s == nil then 355 s = line:match("^%s*$") 356 end 357 if s == nil then 358 return false 359 end 360 end 361 return true 362end 363 364local function getBlacklist() 365 local blacklist = {} 366 local blacklist_str = loader.getenv('module_blacklist') 367 if blacklist_str == nil then 368 return blacklist 369 end 370 371 for mod in blacklist_str:gmatch("[;, ]?([-%w_]+)[;, ]?") do 372 blacklist[mod] = true 373 end 374 return blacklist 375end 376 377local function loadModule(mod, silent) 378 local status = true 379 local blacklist = getBlacklist() 380 local pstatus 381 for k, v in pairs(mod) do 382 if v.load ~= nil and v.load:lower() == "yes" then 383 local module_name = v.name or k 384 if not v.force and blacklist[module_name] ~= nil then 385 if not silent then 386 print(MSG_MODBLACKLIST:format(module_name)) 387 end 388 goto continue 389 end 390 if not silent then 391 loader.printc(module_name .. "...") 392 end 393 local str = "load " 394 if v.type ~= nil then 395 str = str .. "-t " .. v.type .. " " 396 end 397 str = str .. module_name 398 if v.flags ~= nil then 399 str = str .. " " .. v.flags 400 end 401 if v.before ~= nil then 402 pstatus = cli_execute_unparsed(v.before) == 0 403 if not pstatus and not silent then 404 print(MSG_FAILEXBEF:format(v.before, k)) 405 end 406 status = status and pstatus 407 end 408 409 if cli_execute_unparsed(str) ~= 0 then 410 print(loader.command_error()) 411 if not silent then 412 print("failed!") 413 end 414 if v.error ~= nil then 415 cli_execute_unparsed(v.error) 416 end 417 status = false 418 elseif v.after ~= nil then 419 pstatus = cli_execute_unparsed(v.after) == 0 420 if not pstatus and not silent then 421 print(MSG_FAILEXAF:format(v.after, k)) 422 end 423 if not silent then 424 print("ok") 425 end 426 status = status and pstatus 427 end 428 end 429 ::continue:: 430 end 431 432 return status 433end 434 435local function readFile(name, silent) 436 local f = io.open(name) 437 if f == nil then 438 if not silent then 439 print(MSG_FAILOPENCFG:format(name)) 440 end 441 return nil 442 end 443 444 local text, _ = io.read(f) 445 -- We might have read in the whole file, this won't be needed any more. 446 io.close(f) 447 448 if text == nil and not silent then 449 print(MSG_FAILREADCFG:format(name)) 450 end 451 return text 452end 453 454local function checkNextboot() 455 local nextboot_file = loader.getenv("nextboot_conf") 456 local nextboot_enable = loader.getenv("nextboot_enable") 457 458 if nextboot_file == nil then 459 return 460 end 461 462 -- is nextboot_enable set in nvstore? 463 if nextboot_enable == "NO" then 464 return 465 end 466 467 local text = readFile(nextboot_file, true) 468 if text == nil then 469 return 470 end 471 472 if nextboot_enable == nil and 473 text:match("^nextboot_enable=\"NO\"") ~= nil then 474 -- We're done; nextboot is not enabled 475 return 476 end 477 478 if not config.parse(text) then 479 print(MSG_FAILPARSECFG:format(nextboot_file)) 480 end 481 482 -- Attempt to rewrite the first line and only the first line of the 483 -- nextboot_file. We overwrite it with nextboot_enable="NO", then 484 -- check for that on load. 485 -- It's worth noting that this won't work on every filesystem, so we 486 -- won't do anything notable if we have any errors in this process. 487 local nfile = io.open(nextboot_file, 'w') 488 if nfile ~= nil then 489 -- We need the trailing space here to account for the extra 490 -- character taken up by the string nextboot_enable="YES" 491 -- Or new end quotation mark lands on the S, and we want to 492 -- rewrite the entirety of the first line. 493 io.write(nfile, "nextboot_enable=\"NO\" ") 494 io.close(nfile) 495 end 496 loader.setenv("nextboot_enable", "NO") 497end 498 499local function processEnv(k, v) 500 for _, val in ipairs(pattern_table) do 501 if not val.luaexempt and val.name then 502 local matched = k:match(val.name) 503 504 if matched then 505 return val.process(matched, v) 506 end 507 end 508 end 509end 510 511-- Module exports 512config.verbose = false 513 514-- The first item in every carousel is always the default item. 515function config.getCarouselIndex(id) 516 return carousel_choices[id] or 1 517end 518 519function config.setCarouselIndex(id, idx) 520 carousel_choices[id] = idx 521end 522 523-- Returns true if we processed the file successfully, false if we did not. 524-- If 'silent' is true, being unable to read the file is not considered a 525-- failure. 526function config.processFile(name, silent) 527 if silent == nil then 528 silent = false 529 end 530 531 local text = readFile(name, silent) 532 if text == nil then 533 return silent 534 end 535 536 if name:match(".lua$") then 537 local cfg_env = setmetatable({}, { 538 indices = {}, 539 __index = function(env, key) 540 if getmetatable(env).indices[key] then 541 return rawget(env, key) 542 end 543 544 return loader.getenv(key) 545 end, 546 __newindex = function(env, key, val) 547 getmetatable(env).indices[key] = true 548 rawset(env, key, val) 549 end, 550 }) 551 552 -- Give local modules a chance to populate the config 553 -- environment. 554 hook.runAll("config.buildenv", cfg_env) 555 local res, err = pcall(load(text, name, "t", cfg_env)) 556 if res then 557 for k, v in pairs(cfg_env) do 558 local t = type(v) 559 if t ~= "function" and t ~= "table" then 560 if t ~= "string" then 561 v = tostring(v) 562 end 563 local pval = processEnv(k, v) 564 if pval then 565 setEnv(k, pval) 566 end 567 end 568 end 569 else 570 print(MSG_FAILEXECLUA:format(name, err)) 571 end 572 573 return res 574 else 575 return config.parse(text) 576 end 577end 578 579-- silent runs will not return false if we fail to open the file 580function config.parse(text) 581 local n = 1 582 local status = true 583 584 for line in text:gmatch("([^\n]+)") do 585 if line:match("^%s*$") == nil then 586 for _, val in ipairs(pattern_table) do 587 if val.str == nil then 588 val.str = val.name .. "=" .. val.val 589 end 590 local pattern = '^%s*' .. val.str .. '%s*(.*)'; 591 local cgroups = val.groups or 2 592 local k, v, c = checkPattern(line, pattern) 593 if k ~= nil then 594 -- Offset by one, drats 595 if cgroups == 1 then 596 c = v 597 v = nil 598 end 599 600 if isValidComment(c) then 601 val.process(k, v) 602 goto nextline 603 end 604 605 break 606 end 607 end 608 609 print(MSG_MALFORMED:format(n, line)) 610 status = false 611 end 612 ::nextline:: 613 n = n + 1 614 end 615 616 return status 617end 618 619function config.readConf(file, loaded_files) 620 if loaded_files == nil then 621 loaded_files = {} 622 end 623 624 if loaded_files[file] ~= nil then 625 return 626 end 627 628 local top_level = next(loaded_files) == nil -- Are we the top-level readConf? 629 print("Loading " .. file) 630 631 -- The final value of loader_conf_files is not important, so just 632 -- clobber it here. We'll later check if it's no longer nil and process 633 -- the new value for files to read. 634 setEnv("loader_conf_files", nil) 635 636 -- These may or may not exist, and that's ok. Do a 637 -- silent parse so that we complain on parse errors but 638 -- not for them simply not existing. 639 if not config.processFile(file, true) then 640 print(MSG_FAILPARSECFG:format(file)) 641 end 642 643 loaded_files[file] = true 644 645 -- Going to process "loader_conf_files" extra-files 646 local loader_conf_files = getEnv("loader_conf_files") 647 if loader_conf_files ~= nil then 648 for name in loader_conf_files:gmatch("[%w%p]+") do 649 config.readConf(name, loaded_files) 650 end 651 end 652 653 if top_level then 654 local loader_conf_dirs = getEnv("loader_conf_dirs") 655 656 -- If product_vars is set, it must be a list of environment variable names 657 -- to walk through to guess product information. The order matters as 658 -- reading a config files override the previously defined values. 659 -- 660 -- If product information can be guessed, for each product information 661 -- found, also read config files found in /boot/loader.conf.d/PRODUCT/. 662 local product_vars = getEnv("product_vars") 663 if product_vars then 664 local product_conf_dirs = "" 665 for var in product_vars:gmatch("%S+") do 666 local product = getEnv(var) 667 if product then 668 product_conf_dirs = product_conf_dirs .. " /boot/loader.conf.d/" .. product 669 end 670 end 671 672 if loader_conf_dirs then 673 loader_conf_dirs = loader_conf_dirs .. product_conf_dirs 674 else 675 loader_conf_dirs = product_conf_dirs 676 end 677 end 678 679 -- Process "loader_conf_dirs" extra-directories 680 if loader_conf_dirs ~= nil then 681 for name in loader_conf_dirs:gmatch("[%w%p]+") do 682 if lfs.attributes(name, "mode") ~= "directory" then 683 print(MSG_FAILDIR:format(name)) 684 goto nextdir 685 end 686 687 for cfile in lfs.dir(name) do 688 if cfile:match(".conf$") then 689 local fpath = name .. "/" .. cfile 690 if lfs.attributes(fpath, "mode") == "file" then 691 config.readConf(fpath, loaded_files) 692 end 693 end 694 end 695 ::nextdir:: 696 end 697 end 698 699 -- Always allow overriding with local config files, e.g., 700 -- /boot/loader.conf.local. 701 local local_loader_conf_files = getEnv("local_loader_conf_files") 702 if local_loader_conf_files then 703 for name in local_loader_conf_files:gmatch("[%w%p]+") do 704 config.readConf(name, loaded_files) 705 end 706 end 707 end 708end 709 710-- other_kernel is optionally the name of a kernel to load, if not the default 711-- or autoloaded default from the module_path 712function config.loadKernel(other_kernel) 713 local flags = loader.getenv("kernel_options") or "" 714 local kernel = other_kernel or loader.getenv("kernel") 715 716 local function tryLoad(names) 717 for name in names:gmatch("([^;]+)%s*;?") do 718 local r = loader.perform("load " .. name .. 719 " " .. flags) 720 if r == 0 then 721 return name 722 end 723 end 724 return nil 725 end 726 727 local function getModulePath() 728 local module_path = loader.getenv("module_path") 729 local kernel_path = loader.getenv("kernel_path") 730 731 if kernel_path == nil then 732 return module_path 733 end 734 735 -- Strip the loaded kernel path from module_path. This currently assumes 736 -- that the kernel path will be prepended to the module_path when it's 737 -- found. 738 kernel_path = escapeName(kernel_path .. ';') 739 return module_path:gsub(kernel_path, '') 740 end 741 742 local function loadBootfile() 743 local bootfile = loader.getenv("bootfile") 744 745 -- append default kernel name 746 if bootfile == nil then 747 bootfile = "kernel" 748 else 749 bootfile = bootfile .. ";kernel" 750 end 751 752 return tryLoad(bootfile) 753 end 754 755 -- kernel not set, try load from default module_path 756 if kernel == nil then 757 local res = loadBootfile() 758 759 if res ~= nil then 760 -- Default kernel is loaded 761 config.kernel_loaded = nil 762 return true 763 else 764 print(MSG_DEFAULTKERNFAIL) 765 return false 766 end 767 else 768 -- Use our cached module_path, so we don't end up with multiple 769 -- automatically added kernel paths to our final module_path 770 local module_path = getModulePath() 771 local res 772 773 if other_kernel ~= nil then 774 kernel = other_kernel 775 end 776 -- first try load kernel with module_path = /boot/${kernel} 777 -- then try load with module_path=${kernel} 778 local paths = {"/boot/" .. kernel, kernel} 779 780 for _, v in pairs(paths) do 781 loader.setenv("module_path", v) 782 res = loadBootfile() 783 784 -- succeeded, add path to module_path 785 if res ~= nil then 786 config.kernel_loaded = kernel 787 if module_path ~= nil then 788 loader.setenv("module_path", v .. ";" .. 789 module_path) 790 loader.setenv("kernel_path", v) 791 end 792 return true 793 end 794 end 795 796 -- failed to load with ${kernel} as a directory 797 -- try as a file 798 res = tryLoad(kernel) 799 if res ~= nil then 800 config.kernel_loaded = kernel 801 return true 802 else 803 print(MSG_KERNFAIL:format(kernel)) 804 return false 805 end 806 end 807end 808 809function config.selectKernel(kernel) 810 config.kernel_selected = kernel 811end 812 813function config.load(file, reloading) 814 if not file then 815 file = "/boot/defaults/loader.conf" 816 end 817 818 config.readConf(file) 819 820 checkNextboot() 821 822 local verbose = loader.getenv("verbose_loading") or "no" 823 config.verbose = verbose:lower() == "yes" 824 if not reloading then 825 hook.runAll("config.loaded") 826 end 827end 828 829-- Reload configuration 830function config.reload(file) 831 modules = {} 832 restoreEnv() 833 config.load(file, true) 834 hook.runAll("config.reloaded") 835end 836 837function config.loadelf() 838 local xen_kernel = loader.getenv('xen_kernel') 839 local kernel = config.kernel_selected or config.kernel_loaded 840 local status 841 842 if xen_kernel ~= nil then 843 print(MSG_XENKERNLOADING) 844 if cli_execute_unparsed('load ' .. xen_kernel) ~= 0 then 845 print(MSG_XENKERNFAIL:format(xen_kernel)) 846 return false 847 end 848 end 849 print(MSG_KERNLOADING) 850 if not config.loadKernel(kernel) then 851 return false 852 end 853 hook.runAll("kernel.loaded") 854 855 print(MSG_MODLOADING) 856 status = loadModule(modules, not config.verbose) 857 hook.runAll("modules.loaded") 858 return status 859end 860 861function config.enableModule(modname) 862 if modules[modname] == nil then 863 modules[modname] = {} 864 elseif modules[modname].load == "YES" then 865 modules[modname].force = true 866 return true 867 end 868 869 modules[modname].load = "YES" 870 modules[modname].force = true 871 return true 872end 873 874function config.disableModule(modname) 875 if modules[modname] == nil then 876 return false 877 elseif modules[modname].load ~= "YES" then 878 return true 879 end 880 881 modules[modname].load = "NO" 882 modules[modname].force = nil 883 return true 884end 885 886function config.isModuleEnabled(modname) 887 local mod = modules[modname] 888 if not mod or mod.load ~= "YES" then 889 return false 890 end 891 892 if mod.force then 893 return true 894 end 895 896 local blacklist = getBlacklist() 897 return not blacklist[modname] 898end 899 900function config.getModuleInfo() 901 return { 902 modules = modules, 903 blacklist = getBlacklist() 904 } 905end 906 907hook.registerType("config.buildenv") 908hook.registerType("config.loaded") 909hook.registerType("config.reloaded") 910hook.registerType("kernel.loaded") 911hook.registerType("modules.loaded") 912return config 913