osdetect.lua 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.
  14. --
  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/>.
  17. --
  18. function enum_device (device, fs, uuid)
  19. local root
  20. local title
  21. local source
  22. local kernels = {}
  23. local kernel_num = 0
  24. local function enum_file (name)
  25. local version
  26. version = string.match (name, "vmlinuz%-(.*)")
  27. if (version ~= nil) then
  28. table.insert (kernels, version)
  29. kernel_num = kernel_num + 1
  30. end
  31. end
  32. local function sort_kernel (first, second)
  33. local a1, a2, a3, a4, b1, b2, b3, b4
  34. a1, a2, a3, a4 = string.match (first, "(%d+)%.?(%d*).?(%d*)%-?(%d*)")
  35. b1, b2, b3, b4 = string.match (second, "(%d+)%.?(%d*).?(%d*)%-?(%d*)")
  36. return (a1 > b1) or (a2 > b2) or (a3 > b3) or (a4 < b4);
  37. end
  38. root = "(" .. device .. ")/"
  39. source = "root (" .. device .. ")\nchainloader +1"
  40. title = nil
  41. if (grub.file_exist (root .. "bootmgr") and
  42. grub.file_exist (root .. "boot/bcd")) then
  43. title = "Windows Vista bootmgr"
  44. elseif (grub.file_exist (root .. "ntldr") and
  45. grub.file_exist (root .. "ntdetect.com") and
  46. grub.file_exist (root .. "boot.ini")) then
  47. title = "Windows NT/2000/XP loader"
  48. elseif (grub.file_exist (root .. "windows/win.com")) then
  49. title = "Windows 98/ME"
  50. elseif (grub.file_exist (root .. "io.sys") and
  51. grub.file_exist (root .. "command.com")) then
  52. title = "MS-DOS"
  53. elseif (grub.file_exist (root .. "kernel.sys")) then
  54. title = "FreeDOS"
  55. elseif (grub.file_exist (root .. "boot/loader") and
  56. grub.file_exist (root .. "boot/device.hints")) then
  57. source = "root (" .. device .. ")\nfreebsd /boot/loader" ..
  58. "\nfreebsd_loadenv /boot/device.hints"
  59. title = "FreeBSD"
  60. else
  61. grub.enum_file (enum_file, root .. "boot")
  62. if kernel_num ~= 0 then
  63. table.sort (kernels, sort_kernel)
  64. for i = 1, kernel_num do
  65. local initrd
  66. title = "Linux " .. kernels[i]
  67. source = "root (" .. device ..
  68. ")\nlinux /boot/vmlinuz-" .. kernels[i] ..
  69. " root=UUID=" .. uuid .. " ro"
  70. if grub.file_exist (root .. "boot/initrd-" ..
  71. kernels[i] .. ".img") then
  72. initrd = "\ninitrd /boot/initrd-" .. kernels[i] .. ".img"
  73. elseif grub.file_exist (root .. "boot/initrd.img-" .. kernels[i]) then
  74. initrd = "\ninitrd /boot/initrd.img-" .. kernels[i]
  75. elseif grub.file_exist (root .. "boot/initrd-" .. kernels[i]) then
  76. initrd = "\ninitrd /boot/initrd-" .. kernels[i]
  77. else
  78. initrd = ""
  79. end
  80. grub.add_menu (source .. initrd, title)
  81. grub.add_menu (source .. " single" .. initrd,
  82. title .. " (single-user mode)")
  83. end
  84. return 0
  85. end
  86. end
  87. if title == nil then
  88. local partition = string.match (device, ".*,(%d+)")
  89. if (partition ~= nil) and (tonumber (partition) > 4) then
  90. return 0
  91. end
  92. title = "Other OS"
  93. end
  94. grub.add_menu (source, title)
  95. return 0
  96. end
  97. grub.enum_device (enum_device)