Skip to content

Instantly share code, notes, and snippets.

@fajarnugraha
Last active November 7, 2016 09:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fajarnugraha/5d46109250ed3bf16c93 to your computer and use it in GitHub Desktop.
Save fajarnugraha/5d46109250ed3bf16c93 to your computer and use it in GitHub Desktop.
Grub lua script to enable automatic creation of grub menu entries on zfs root. Download to /rpool/grub/zfsroot.lua
-- Change this two variables with your actual setup
rpool="rpool"
zfs_rootdir="ROOT"
-- End of user-editable section
ds = ""
snap = ""
snap_dir = ""
function find_kernel_pair (name)
kernel_ver = string.match (name, "vmlinuz%-(.+)")
if (kernel_ver) then
local kernel = snap_dir .. "/boot/vmlinuz-" .. kernel_ver
local initrd = snap_dir .. "/boot/initrd.img-" .. kernel_ver
local root = "root=ZFS=" .. rpool .. "/" .. zfs_rootdir .. "/" .. ds .. snap
local source = "linux " .. kernel .. " " .. root .. " ro boot=zfs" ..
"\ninitrd " .. initrd
grub.add_menu (source, "Boot linux " .. kernel_ver .. " on " .. rpool .. "/" .. zfs_rootdir .. "/" .. ds .. snap)
end
end
function find_kernel_in_snapshot (name)
snap = string.match (name, "(@.+)")
if (snap) then
snap_dir = "/" .. zfs_rootdir .. "/" .. ds .. "/" .. snap
grub.enum_file (find_kernel_pair, snap_dir .. "/boot")
end
end
function find_ds_with_cfg (name)
ds = string.match (name, "(...+)")
if (ds) then
local ds_dir = "/" .. zfs_rootdir .. "/" .. ds
local cfg = ds_dir .. "/@/boot/grub/grub.cfg"
if (grub.file_exist(cfg)) then
return ds,ds_dir,cfg
end
end
return nil,nil,nil
end
function find_bootable_snapshot (name)
ds,ds_dir,cfg = find_ds_with_cfg(name)
if (ds) then
grub.enum_file (find_kernel_in_snapshot, ds_dir)
end
end
function find_bootable_ds (name)
ds,ds_dir,cfg = find_ds_with_cfg(name)
if (ds) then
local source = "configfile " .. cfg
grub.add_menu (source, "Boot menu from " .. rpool .. "/" .. zfs_rootdir .. "/" .. ds)
end
end
grub.enum_file (find_bootable_ds, zfs_rootdir)
grub.enum_file (find_bootable_snapshot, zfs_rootdir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment