30_os-prober.in 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #! /bin/sh
  2. set -e
  3. # grub-mkconfig helper script.
  4. # Copyright (C) 2006,2007,2008,2009 Free Software Foundation, Inc.
  5. #
  6. # GRUB is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # GRUB is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. prefix="@prefix@"
  19. exec_prefix="@exec_prefix@"
  20. datarootdir="@datarootdir@"
  21. export TEXTDOMAIN=@PACKAGE@
  22. export TEXTDOMAINDIR="@localedir@"
  23. . "$pkgdatadir/grub-mkconfig_lib"
  24. if [ "x${GRUB_DISABLE_OS_PROBER}" = "xtrue" ]; then
  25. grub_warn "$(gettext_printf "os-prober will not be executed to detect other bootable partitions.\nSystems on them will not be added to the GRUB boot configuration.\nCheck GRUB_DISABLE_OS_PROBER documentation entry.")"
  26. exit 0
  27. fi
  28. if ! command -v os-prober > /dev/null || ! command -v linux-boot-prober > /dev/null ; then
  29. # missing os-prober and/or linux-boot-prober
  30. exit 0
  31. fi
  32. grub_warn "$(gettext_printf "os-prober will be executed to detect other bootable partitions.\nIts output will be used to detect bootable binaries on them and create new boot entries.")"
  33. OSPROBED="`os-prober | tr ' ' '^' | paste -s -d ' '`"
  34. if [ -z "${OSPROBED}" ] ; then
  35. # empty os-prober output, nothing doing
  36. exit 0
  37. fi
  38. osx_entry() {
  39. if [ x$2 = x32 ]; then
  40. # TRANSLATORS: it refers to kernel architecture (32-bit)
  41. bitstr="$(gettext "(32-bit)")"
  42. else
  43. # TRANSLATORS: it refers to kernel architecture (64-bit)
  44. bitstr="$(gettext "(64-bit)")"
  45. fi
  46. # TRANSLATORS: it refers on the OS residing on device %s
  47. onstr="$(gettext_printf "(on %s)" "${DEVICE}")"
  48. cat << EOF
  49. menuentry '$(echo "${LONGNAME} $bitstr $onstr" | grub_quote)' --class osx --class darwin --class os \$menuentry_id_option 'osprober-xnu-$2-$(grub_get_device_id "${DEVICE}")' {
  50. EOF
  51. save_default_entry | grub_add_tab
  52. prepare_grub_to_access_device ${DEVICE} | grub_add_tab
  53. cat << EOF
  54. load_video
  55. set do_resume=0
  56. if [ /var/vm/sleepimage -nt10 / ]; then
  57. if xnu_resume /var/vm/sleepimage; then
  58. set do_resume=1
  59. fi
  60. fi
  61. if [ \$do_resume = 0 ]; then
  62. xnu_uuid ${OSXUUID} uuid
  63. if [ -f /Extra/DSDT.aml ]; then
  64. acpi -e /Extra/DSDT.aml
  65. fi
  66. if [ /kernelcache -nt /System/Library/Extensions ]; then
  67. $1 /kernelcache boot-uuid=\${uuid} rd=*uuid
  68. elif [ -f /System/Library/Kernels/kernel ]; then
  69. $1 /System/Library/Kernels/kernel boot-uuid=\${uuid} rd=*uuid
  70. xnu_kextdir /System/Library/Extensions
  71. else
  72. $1 /mach_kernel boot-uuid=\${uuid} rd=*uuid
  73. if [ /System/Library/Extensions.mkext -nt /System/Library/Extensions ]; then
  74. xnu_mkext /System/Library/Extensions.mkext
  75. else
  76. xnu_kextdir /System/Library/Extensions
  77. fi
  78. fi
  79. if [ -f /Extra/Extensions.mkext ]; then
  80. xnu_mkext /Extra/Extensions.mkext
  81. fi
  82. if [ -d /Extra/Extensions ]; then
  83. xnu_kextdir /Extra/Extensions
  84. fi
  85. if [ -f /Extra/devprop.bin ]; then
  86. xnu_devprop_load /Extra/devprop.bin
  87. fi
  88. if [ -f /Extra/splash.jpg ]; then
  89. insmod jpeg
  90. xnu_splash /Extra/splash.jpg
  91. fi
  92. if [ -f /Extra/splash.png ]; then
  93. insmod png
  94. xnu_splash /Extra/splash.png
  95. fi
  96. if [ -f /Extra/splash.tga ]; then
  97. insmod tga
  98. xnu_splash /Extra/splash.tga
  99. fi
  100. fi
  101. }
  102. EOF
  103. }
  104. used_osprober_linux_ids=
  105. if [ "x$GRUB_TOP_LEVEL_OS_PROBER" != x ]; then
  106. OSPROBED=$(grub_move_to_front "$GRUB_TOP_LEVEL_OS_PROBER" ${OSPROBED})
  107. fi
  108. for OS in ${OSPROBED} ; do
  109. DEVICE="`echo ${OS} | cut -d ':' -f 1`"
  110. LONGNAME="`echo ${OS} | cut -d ':' -f 2 | tr '^' ' '`"
  111. LABEL="`echo ${OS} | cut -d ':' -f 3 | tr '^' ' '`"
  112. BOOT="`echo ${OS} | cut -d ':' -f 4`"
  113. if UUID="`${grub_probe} --target=fs_uuid --device ${DEVICE%@*}`"; then
  114. EXPUUID="$UUID"
  115. if [ x"${DEVICE#*@}" != x ] ; then
  116. EXPUUID="${EXPUUID}@${DEVICE#*@}"
  117. fi
  118. if [ "x${GRUB_OS_PROBER_SKIP_LIST}" != "x" ] && [ "x`echo ${GRUB_OS_PROBER_SKIP_LIST} | grep -i -e '\b'${EXPUUID}'\b'`" != "x" ] ; then
  119. echo "Skipped ${LONGNAME} on ${DEVICE} by user request." >&2
  120. continue
  121. fi
  122. fi
  123. BTRFS="`echo ${OS} | cut -d ':' -f 5`"
  124. if [ "x$BTRFS" = "xbtrfs" ]; then
  125. BTRFSuuid="`echo ${OS} | cut -d ':' -f 6`"
  126. BTRFSsubvol="`echo ${OS} | cut -d ':' -f 7`"
  127. fi
  128. if [ -z "${LONGNAME}" ] ; then
  129. LONGNAME="${LABEL}"
  130. fi
  131. # os-prober returns text string followed by optional counter
  132. CLASS="--class $(echo "${LABEL}" | LC_ALL=C sed 's,[[:digit:]]*$,,' | cut -d' ' -f1 | tr 'A-Z' 'a-z' | LC_ALL=C sed 's,[^[:alnum:]_],_,g')"
  133. gettext_printf "Found %s on %s\n" "${LONGNAME}" "${DEVICE}" >&2
  134. case ${BOOT} in
  135. chain)
  136. onstr="$(gettext_printf "(on %s)" "${DEVICE}")"
  137. cat << EOF
  138. menuentry '$(echo "${LONGNAME} $onstr" | grub_quote)' $CLASS --class os \$menuentry_id_option 'osprober-chain-$(grub_get_device_id "${DEVICE}")' {
  139. EOF
  140. save_default_entry | grub_add_tab
  141. prepare_grub_to_access_device ${DEVICE} | grub_add_tab
  142. if [ x"`${grub_probe} --device ${DEVICE} --target=partmap`" = xmsdos ]; then
  143. cat << EOF
  144. parttool \${root} hidden-
  145. EOF
  146. fi
  147. case ${LONGNAME} in
  148. Windows\ Vista*|Windows\ 7*|Windows\ Server\ 2008*)
  149. ;;
  150. *)
  151. cat << EOF
  152. drivemap -s (hd0) \${root}
  153. EOF
  154. ;;
  155. esac
  156. cat <<EOF
  157. chainloader +1
  158. }
  159. EOF
  160. ;;
  161. efi)
  162. EFIPATH=${DEVICE#*@}
  163. DEVICE=${DEVICE%@*}
  164. onstr="$(gettext_printf "(on %s)" "${DEVICE}")"
  165. cat << EOF
  166. menuentry '$(echo "${LONGNAME} $onstr" | grub_quote)' $CLASS --class os \$menuentry_id_option 'osprober-efi-$(grub_get_device_id "${DEVICE}")' {
  167. EOF
  168. save_default_entry | sed -e "s/^/\t/"
  169. prepare_grub_to_access_device ${DEVICE} | sed -e "s/^/\t/"
  170. cat <<EOF
  171. chainloader ${EFIPATH}
  172. }
  173. EOF
  174. ;;
  175. linux)
  176. if [ "x$BTRFS" = "xbtrfs" ]; then
  177. LINUXPROBED="`linux-boot-prober btrfs ${BTRFSuuid} ${BTRFSsubvol} 2> /dev/null | tr ' ' '^' | paste -s -d ' '`"
  178. else
  179. LINUXPROBED="`linux-boot-prober ${DEVICE} 2> /dev/null | tr ' ' '^' | paste -s -d ' '`"
  180. fi
  181. prepare_boot_cache=
  182. boot_device_id=
  183. is_top_level=true
  184. title_correction_code=
  185. OS="${LONGNAME}"
  186. for LINUX in ${LINUXPROBED} ; do
  187. LROOT="`echo ${LINUX} | cut -d ':' -f 1`"
  188. LBOOT="`echo ${LINUX} | cut -d ':' -f 2`"
  189. LLABEL="`echo ${LINUX} | cut -d ':' -f 3 | tr '^' ' '`"
  190. LKERNEL="`echo ${LINUX} | cut -d ':' -f 4`"
  191. LINITRD="`echo ${LINUX} | cut -d ':' -f 5 | tr '^' ' '`"
  192. LPARAMS="`echo ${LINUX} | cut -d ':' -f 6- | tr '^' ' '`"
  193. if [ -z "${LLABEL}" ] ; then
  194. LLABEL="${LONGNAME}"
  195. fi
  196. if [ "${LROOT}" != "${LBOOT}" ]; then
  197. LKERNEL="${LKERNEL#/boot}"
  198. LINITRD="${LINITRD#/boot}"
  199. fi
  200. onstr="$(gettext_printf "(on %s)" "${DEVICE}")"
  201. recovery_params="$(echo "${LPARAMS}" | grep single)" || true
  202. counter=1
  203. while echo "$used_osprober_linux_ids" | grep 'osprober-gnulinux-$LKERNEL-${recovery_params}-$counter-$boot_device_id' > /dev/null; do
  204. counter=$((counter+1));
  205. done
  206. if [ -z "$boot_device_id" ]; then
  207. boot_device_id="$(grub_get_device_id "${DEVICE}")"
  208. fi
  209. used_osprober_linux_ids="$used_osprober_linux_ids 'osprober-gnulinux-$LKERNEL-${recovery_params}-$counter-$boot_device_id'"
  210. if [ -z "${prepare_boot_cache}" ]; then
  211. prepare_boot_cache="$(prepare_grub_to_access_device ${LBOOT} | grub_add_tab)"
  212. fi
  213. # The GRUB_DISABLE_SUBMENU option used to be different than others since it was
  214. # mentioned in the documentation that has to be set to 'y' instead of 'true' to
  215. # enable it. This caused a lot of confusion to users that set the option to 'y',
  216. # 'yes' or 'true'. This was fixed but all of these values must be supported now.
  217. if [ "x${GRUB_DISABLE_SUBMENU}" = xyes ] || [ "x${GRUB_DISABLE_SUBMENU}" = xy ]; then
  218. GRUB_DISABLE_SUBMENU="true"
  219. fi
  220. if [ "x$is_top_level" = xtrue ] && [ "x${GRUB_DISABLE_SUBMENU}" != xtrue ]; then
  221. cat << EOF
  222. menuentry '$(echo "$OS $onstr" | grub_quote)' $CLASS --class gnu-linux --class gnu --class os \$menuentry_id_option 'osprober-gnulinux-simple-$boot_device_id' {
  223. EOF
  224. save_default_entry | grub_add_tab
  225. printf '%s\n' "${prepare_boot_cache}"
  226. cat << EOF
  227. linux ${LKERNEL} ${LPARAMS}
  228. EOF
  229. if [ -n "${LINITRD}" ] ; then
  230. cat << EOF
  231. initrd ${LINITRD}
  232. EOF
  233. fi
  234. cat << EOF
  235. }
  236. EOF
  237. echo "submenu '$(gettext_printf "Advanced options for %s" "${OS} $onstr" | grub_quote)' \$menuentry_id_option 'osprober-gnulinux-advanced-$boot_device_id' {"
  238. is_top_level=false
  239. fi
  240. title="${LLABEL} $onstr"
  241. cat << EOF
  242. menuentry '$(echo "$title" | grub_quote)' --class gnu-linux --class gnu --class os \$menuentry_id_option 'osprober-gnulinux-$LKERNEL-${recovery_params}-$boot_device_id' {
  243. EOF
  244. save_default_entry | sed -e "s/^/$grub_tab$grub_tab/"
  245. printf '%s\n' "${prepare_boot_cache}" | grub_add_tab
  246. cat << EOF
  247. linux ${LKERNEL} ${LPARAMS}
  248. EOF
  249. if [ -n "${LINITRD}" ] ; then
  250. cat << EOF
  251. initrd ${LINITRD}
  252. EOF
  253. fi
  254. cat << EOF
  255. }
  256. EOF
  257. if [ x"$title" = x"$GRUB_ACTUAL_DEFAULT" ] || [ x"Previous Linux versions>$title" = x"$GRUB_ACTUAL_DEFAULT" ]; then
  258. replacement_title="$(echo "Advanced options for ${OS} $onstr" | sed 's,>,>>,g')>$(echo "$title" | sed 's,>,>>,g')"
  259. quoted="$(echo "$GRUB_ACTUAL_DEFAULT" | grub_quote)"
  260. title_correction_code="${title_correction_code}if [ \"x\$default\" = '$quoted' ]; then default='$(echo "$replacement_title" | grub_quote)'; fi;"
  261. grub_warn "$(gettext_printf "Please don't use old title \`%s' for GRUB_DEFAULT, use \`%s' (for versions before 2.00) or \`%s' (for 2.00 or later)" "$GRUB_ACTUAL_DEFAULT" "$replacement_title" "gnulinux-advanced-$boot_device_id>gnulinux-$version-$type-$boot_device_id")"
  262. fi
  263. done
  264. if [ x"$is_top_level" != xtrue ]; then
  265. echo '}'
  266. fi
  267. echo "$title_correction_code"
  268. ;;
  269. macosx)
  270. if [ "${UUID}" ]; then
  271. OSXUUID="${UUID}"
  272. osx_entry xnu_kernel 32
  273. osx_entry xnu_kernel64 64
  274. fi
  275. ;;
  276. hurd)
  277. onstr="$(gettext_printf "(on %s)" "${DEVICE}")"
  278. cat << EOF
  279. menuentry '$(echo "${LONGNAME} $onstr" | grub_quote)' --class hurd --class gnu --class os \$menuentry_id_option 'osprober-gnuhurd-/boot/gnumach.gz-false-$(grub_get_device_id "${DEVICE}")' {
  280. EOF
  281. save_default_entry | grub_add_tab
  282. prepare_grub_to_access_device ${DEVICE} | grub_add_tab
  283. grub_device="`${grub_probe} --device ${DEVICE} --target=drive`"
  284. mach_device="`echo "${grub_device}" | sed -e 's/(\(hd.*\),msdos\(.*\))/\1s\2/'`"
  285. grub_fs="`${grub_probe} --device ${DEVICE} --target=fs`"
  286. case "${grub_fs}" in
  287. *fs) hurd_fs="${grub_fs}" ;;
  288. *) hurd_fs="${grub_fs}fs" ;;
  289. esac
  290. cat << EOF
  291. multiboot /boot/gnumach.gz root=device:${mach_device}
  292. module /hurd/${hurd_fs}.static ${hurd_fs} --readonly \\
  293. --multiboot-command-line='\${kernel-command-line}' \\
  294. --host-priv-port='\${host-port}' \\
  295. --device-master-port='\${device-port}' \\
  296. --exec-server-task='\${exec-task}' -T typed '\${root}' \\
  297. '\$(task-create)' '\$(task-resume)'
  298. module /lib/ld.so.1 exec /hurd/exec '\$(exec-task=task-create)'
  299. }
  300. EOF
  301. ;;
  302. minix)
  303. cat << EOF
  304. menuentry "${LONGNAME} (on ${DEVICE}, Multiboot)" {
  305. EOF
  306. save_default_entry | sed -e "s/^/\t/"
  307. prepare_grub_to_access_device ${DEVICE} | sed -e "s/^/\t/"
  308. cat << EOF
  309. multiboot /boot/image_latest
  310. }
  311. EOF
  312. ;;
  313. *)
  314. # TRANSLATORS: %s is replaced by OS name.
  315. gettext_printf "%s is not yet supported by grub-mkconfig.\n" " ${LONGNAME}" >&2
  316. ;;
  317. esac
  318. done