Hi,

I've written two lua scripts that can automatically detect and boot
LiveCD iso files. I'm using these scripts on my USB-Stick, so I can just
put the LiveCD iso file on it and use it and don't have to use the
different LiveCD to USB tools, which often not allow more than one Live
System on one USB-Stick.

The first script listisos.lua lists all iso files in a specific folder.
The folder can be specified with an environment variable "isofolder" or
if it is not set it looks in "/boot/isos".

The second script bootiso.lua actually boots an iso file specified with
the "isofile" environment variable and is used for the menuentries that
the listisos.lua script creates. The following LiveCD types are
currently supported:

      * Grml
      * Sidux
      * Ubuntu
#!lua
--
-- Copyright (C) 2009 Free Software Foundation, Inc.
--
-- GRUB is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- GRUB is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with GRUB. If not, see <http://www.gnu.org/licenses/>.
--

isofolder = grub.getenv ("isofolder")
if (isofolder == nil) then
  isofolder = "/boot/isos"
end

function enum_file (name)
  local title = string.match (name, "(.*)%.[iI][sS][oO]")

  if (title) then
    local source = "set isofile=" .. isofolder .. "/" .. name ..
      "\nsource /boot/grub/bootiso.lua"

    grub.add_menu (source, title)
  end
end

grub.enum_file (enum_file, isofolder)
#!lua
--
-- Copyright (C) 2009 Free Software Foundation, Inc.
--
-- GRUB is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- GRUB is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with GRUB. If not, see <http://www.gnu.org/licenses/>.
--

function bootiso (isofile)
  local err_no
  local err_msg

  local function print_error (msg)
    print ("Error: " .. msg)
    grub.run ("read")
  end

  local function find_file (folder, match)
    local filename

    local function enum_file (name)
      if (filename == nil) then
        filename = string.match (name, match)
      end
    end

    grub.enum_file (enum_file, folder)

    if (filename) then
      return folder .. "/" .. filename
    else
      return nil
    end
  end

  local function try_boot_generic ()
    local grubcfg = "(loop)/boot/grub/grub.cfg"

    if (grub.file_exist (grubcfg)) then
      grub.run ("configfile " .. grubcfg)
      return true
    end
    return false
  end

  local function try_boot_grml ()
    local linux = "(loop)/boot/grml/linux26"
    local initrd = "(loop)/boot/grml/initrd.gz"
    local params = "findiso=" .. isofile .. 
      " apm=power-off vga=791 quiet boot=live nomce"

    if (grub.file_exist ("(loop)/GRML/grml-version") and 
        grub.file_exist (linux) and 
        grub.file_exist (initrd)) then
      grub.run ("linux " .. linux .. " " .. params)
      grub.run ("initrd " .. initrd)
      return true
    end
    return false
  end

  local function try_boot_sidux ()
    local linux = find_file ("(loop)/boot", "vmlinuz%-.*%-sidux%-.*")
    local initrd = find_file ("(loop)/boot", "initrd%.img%-.*%-sidux%-.*")
    local params = "fromiso=" .. isofile .. " boot=fll quiet vga=791"

    if (linux and grub.file_exist (linux) and 
        initrd and grub.file_exist (initrd)) then
      grub.run ("linux " .. linux .. " " .. params)
      grub.run ("initrd " .. initrd)
      return true
    end
    return false
  end

  local function try_boot_ubuntu ()
    local linux = "(loop)/casper/vmlinuz"
    local initrd = "(loop)/casper/initrd.gz"
    local params = "boot=casper iso-scan/filename=" .. isofile .. 
      " quiet splash noprompt --"

    if (grub.file_exist ("(loop)/preseed/ubuntu.seed") and 
        grub.file_exist (linux) and 
        grub.file_exist (initrd)) then
      grub.run ("linux " .. linux .. " " .. params)
      grub.run ("initrd " .. initrd)
      return true
    end
    return false
  end

  if (isofile == nil) then
    print_error ("variable 'isofile' is undefined")
  elseif (not grub.file_exist (isofile)) then
    print_error ("file '" .. isofile .. "' does not exist")
  else
    err_no,err_msg = grub.run ("loopback loop " .. isofile)
    if (err_no ~= 0) then
      print_error ("Cannot load ISO: " .. err_msg)
    else
      if (try_boot_grml ()) then
      elseif (try_boot_sidux ()) then
      elseif (try_boot_ubuntu ()) then
      elseif (try_boot_generic ()) then
      else
        print_error ("Unsupported ISO type")
      end
    end
  end
end

bootiso (grub.getenv ("isofile"))
_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/grub-devel

Reply via email to