[syslinux] LUA script to emulate PXE boot on USB

Laurent Licour
Sat, 28 Aug 2010 04:50:27 -0000

Hello

Here is an attempt to reproduce PXE-like booting (having different config per
MAC address) on
other bootable media (ex :  USB)
It's a lua script that analyze  dmi information and load a config file. As
MAC address is not available
in DMI (no driver has been loaded), selection is made by UUID, serial, product,
manufacturer. Otherwise, default.cfg
is loaded
It can be usefull to have a USB key with several template of boot, depending a
the hardware

Laurent



-- dmiconfig.lua
-- ============--
-- Boot syslinux with config depending of host hardware (similar to PXE)
-- Search for config file in following subdirectories (in this order) of
'syslinux.cfg'
--    uuid/
--    serial-number/
--    product-name/
--    manufacturer/
-- Finaly, if no config file exists, use 'default' one
--
-- Warning : Due to lua limitation, filenames must have no space, and / must be
replaced with _
--  (shell equivalent : touch `dmidecocde -s system-serial | tr -d " "
| tr / _` )
--


path = "syslinux.cfg"
path_uuid         = path .. "/uuid"
path_serial       = path .. "/serial-number"
path_product      = path .. "/product-name"
path_manufacturer = path .. "/manufacturer"

-- Verify if file exist
function exists(n)
  local f=io.open(n)
  if f then
    io.close(f)
  end
  return f
end

-- Suppress spaces (bug : lua does not support spaces in filenames)
-- Replace / (directory delimiter)
--  (shell equivalent : touch `dmidecocde -s system-serial | tr -d " "
| tr / _` )
function cleanup(s)
  return s:gsub("%s", ""):gsub("/", "_")
end

if (dmi.supported()) then

  dmitable = dmi.gettable()

  manufacturer = cleanup(dmitable["system.manufacturer"])
  product      = cleanup(dmitable["system.product_name"])
  serial       = cleanup(dmitable["system.serial"])
  uuid         = cleanup(dmitable["system.uuid"])

  print("DMI informations :")
  print(" Manufacturer : " .. manufacturer)
  print(" Product Name : " .. product)
  print(" Serial       : " .. serial)
  print(" UUID         : " .. uuid)

  -- Search for config file (in this order) :
  --   UUID, SerialNumber, ProductName, Manufacturer
  -- Otherwise : use default
  if ( exists(path_uuid .. "/" .. uuid) ) then
    config = path_uuid .. "/" .. uuid
  elseif ( exists(path_serial .. "/" .. serial) ) then
    config = path_serial .. "/" .. serial
  elseif ( exists(path_product .. "/" .. product) ) then
    config = path_product .. "/" .. product
  elseif ( exists(path_manufacturer .. "/" .. manufacturer) ) then
    config = path_manufacturer .. "/" .. manufacturer
  else
    config = path .. "/default"
  end

  -- And now, reload syslinux with specific config
  print(" -> Config    : " .. config)
  syslinux.run_command("config.c32 " .. config)

else
  -- No DMI : boot on default configuration

  print("DMI not supported")
  config = path .. "/default"
  print(" -> Config    : " .. config)
  syslinux.run_command("config.c32 " .. config)
end