2009-07-07 Pavel Roskin <proski@gnu.org>
[grub2/bean.git] / util / osdetect.lua
blob38a90993dacf1b479c950d1c3f0d3a83dbe2a83f
1 #!lua
2 --
3 -- Copyright (C) 2009 Free Software Foundation, Inc.
4 --
5 -- GRUB is free software: you can redistribute it and/or modify
6 -- it under the terms of the GNU General Public License as published by
7 -- the Free Software Foundation, either version 3 of the License, or
8 -- (at your option) any later version.
9 --
10 -- GRUB is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
15 -- You should have received a copy of the GNU General Public License
16 -- along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 function enum_device (device, fs, uuid)
20 local root
21 local title
22 local source
23 local kernels = {}
24 local kernel_num = 0
26 local function enum_file (name)
27 local version
29 version = string.match (name, "vmlinuz%-(.*)")
30 if (version ~= nil) then
31 table.insert (kernels, version)
32 kernel_num = kernel_num + 1
33 end
34 end
36 local function sort_kernel (first, second)
37 local a1, a2, a3, a4, b1, b2, b3, b4
39 a1, a2, a3, a4 = string.match (first, "(%d+)%.?(%d*).?(%d*)%-?(%d*)")
40 b1, b2, b3, b4 = string.match (second, "(%d+)%.?(%d*).?(%d*)%-?(%d*)")
41 return (a1 > b1) or (a2 > b2) or (a3 > b3) or (a4 < b4);
42 end
44 root = "(" .. device .. ")/"
45 source = "root (" .. device .. ")\nchainloader +1"
46 title = nil
47 if (grub.file_exist (root .. "bootmgr") and
48 grub.file_exist (root .. "boot/bcd")) then
49 title = "Windows Vista bootmgr"
50 elseif (grub.file_exist (root .. "ntldr") and
51 grub.file_exist (root .. "ntdetect.com") and
52 grub.file_exist (root .. "boot.ini")) then
53 title = "Windows NT/2000/XP loader"
54 elseif (grub.file_exist (root .. "windows/win.com")) then
55 title = "Windows 98/ME"
56 elseif (grub.file_exist (root .. "io.sys") and
57 grub.file_exist (root .. "command.com")) then
58 title = "MS-DOS"
59 elseif (grub.file_exist (root .. "kernel.sys")) then
60 title = "FreeDOS"
61 elseif (grub.file_exist (root .. "boot/loader") and
62 grub.file_exist (root .. "boot/device.hints")) then
63 source = "root (" .. device .. ")\nfreebsd /boot/loader" ..
64 "\nfreebsd_loadenv /boot/device.hints"
65 title = "FreeBSD"
66 else
67 grub.enum_file (enum_file, root .. "boot")
68 if kernel_num ~= 0 then
69 table.sort (kernels, sort_kernel)
70 for i = 1, kernel_num do
71 local initrd
73 title = "Linux " .. kernels[i]
74 source = "root (" .. device ..
75 ")\nlinux /boot/vmlinuz-" .. kernels[i] ..
76 " root=UUID=" .. uuid .. " ro"
78 if grub.file_exist (root .. "boot/initrd-" ..
79 kernels[i] .. ".img") then
80 initrd = "\ninitrd /boot/initrd-" .. kernels[i] .. ".img"
81 elseif grub.file_exist (root .. "boot/initrd.img-" .. kernels[i]) then
82 initrd = "\ninitrd /boot/initrd.img-" .. kernels[i]
83 elseif grub.file_exist (root .. "boot/initrd-" .. kernels[i]) then
84 initrd = "\ninitrd /boot/initrd-" .. kernels[i]
85 else
86 initrd = ""
87 end
89 grub.add_menu (source .. initrd, title)
90 grub.add_menu (source .. " single" .. initrd,
91 title .. " (single-user mode)")
92 end
93 return 0
94 end
95 end
97 if title == nil then
98 local partition = string.match (device, ".*,(%d+)")
100 if (partition ~= nil) and (tonumber (partition) > 4) then
101 return 0
104 title = "Other OS"
107 grub.add_menu (source, title)
108 return 0
111 grub.enum_device (enum_device)