update-grub 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. #!/bin/bash
  2. #
  3. # Insert a list of installed kernels in a grub config file
  4. # Copyright 2001 Wichert Akkerman <wichert@linux.com>
  5. # Copyright (C) 2007,2008 Free Software Foundation, Inc.
  6. #
  7. # This file is free software; you can redistribute it and/or modify it
  8. # under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful, but
  13. # WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. # General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. # Contributors:
  21. # Jason Thomas <jason@debian.org>
  22. # David B.Harris <dbarclay10@yahoo.ca>
  23. # Marc Haber <mh+debian-packages@zugschlus.de>
  24. # Crispin Flowerday <crispin@zeus.com>
  25. # Abort on errors
  26. set -e
  27. host_os=`uname -s | tr '[A-Z]' '[a-z]'`
  28. abort() {
  29. message=$@
  30. echo >&2
  31. echo -e "$message" >&2
  32. echo >&2
  33. exit 1
  34. }
  35. find_grub_dir ()
  36. {
  37. echo -n "Searching for GRUB installation directory ... " >&2
  38. for d in /boot/grub /boot/boot/grub ; do
  39. if [ -d "$d" ] ; then
  40. grub_dir="$d"
  41. break
  42. fi
  43. done
  44. if [ -z "$grub_dir" ] ; then
  45. abort "No GRUB directory found.\n To create a template run 'mkdir /boot/grub' first.\n To install grub, install it manually or try the 'grub-install' command.\n ### Warning, grub-install is used to change your MBR. ###"
  46. else
  47. echo "found: $grub_dir" >&2
  48. fi
  49. echo $grub_dir
  50. }
  51. # This function was borrowed from grub2/util/update-grub_lib.in
  52. make_system_path_relative_to_its_root ()
  53. {
  54. path=$1
  55. # abort if file doesn't exist
  56. if test -e $path ; then : ;else
  57. return 1
  58. fi
  59. # canonicalize
  60. if path=`readlink -f $path` ; then : ; else
  61. return 1
  62. fi
  63. # if not a directory, climb up to the directory containing it
  64. if test -d $path ; then
  65. dir=$path
  66. else
  67. dir=`echo $path | sed -e "s,/[^/]*$,,g"`
  68. fi
  69. num=`stat -c %d $dir`
  70. # this loop sets $dir to the root directory of the filesystem we're inspecting
  71. while : ; do
  72. parent=`readlink -f $dir/..`
  73. if [ "x`stat -c %d $parent`" = "x$num" ] ; then : ; else
  74. # $parent is another filesystem; we found it.
  75. break
  76. fi
  77. if [ "x$dir" = "x/" ] ; then
  78. # / is our root.
  79. break
  80. fi
  81. dir=$parent
  82. done
  83. # This function never prints trailing slashes (so that its output can be
  84. # appended a slash unconditionally). Each slash in $dir is considered a
  85. # preceding slash, and therefore the root directory is an empty string.
  86. if [ "$dir" = "/" ] ; then
  87. dir=""
  88. fi
  89. echo $path | sed -e "s,^$dir,,g"
  90. }
  91. # The grub installation directory
  92. grub_dir=$(find_grub_dir)
  93. # Full path to the device.map
  94. device_map=$grub_dir/device.map
  95. find_device ()
  96. {
  97. if ! test -e ${device_map} ; then
  98. echo quit | grub --batch --no-floppy --device-map=${device_map} > /dev/null
  99. fi
  100. grub-probe --device-map=${device_map} -t device $1 2> /dev/null
  101. }
  102. # Usage: convert_raid1 os_device
  103. # Checks if os_device is a software raid1.
  104. # If so, converts to first physical device in array.
  105. convert_raid1 ()
  106. {
  107. case $1 in
  108. /dev/md[0-9] | /dev/md/[0-9])
  109. : ;; # Continue
  110. *)
  111. return 1 ;;
  112. esac
  113. [ -x /sbin/mdadm ] || return 1
  114. # Check that the raid device is raid1
  115. raidlevel=$(mdadm -D -b $1 | grep "^ARRAY" | \
  116. sed "s/^.*level=//" | cut -d" " -f1)
  117. [ "$raidlevel" = "raid1" ] || return 1
  118. # Take only the first device that makes up the raid
  119. raiddev=$(mdadm -D $1 | grep -A1 "Number" | grep "dev" \
  120. | sed "s/^.*\(\/dev\/.*\)$/\1/")
  121. [ -n "$raiddev" ] || return 1
  122. echo $raiddev
  123. return 0
  124. }
  125. # Usage: convert os_device
  126. # Convert an OS device to the corresponding GRUB drive.
  127. convert () {
  128. if ! test -e ${device_map} ; then
  129. echo quit | grub --batch --no-floppy --device-map=${device_map} > /dev/null
  130. fi
  131. GRUB_LEGACY_0_BASED_PARTITIONS=1 grub-probe --device-map=${device_map} -t drive -d "$1" 2> /dev/null || {
  132. echo "warning: grub-probe can't find drive for $1." >&2
  133. tmp_map=$(mktemp -t device.map.XXXXXXXX)
  134. grub-mkdevicemap --device-map=${tmp_map} --no-floppy >/dev/null 2>&1 || true
  135. GRUB_LEGACY_0_BASED_PARTITIONS=1 grub-probe --device-map=${tmp_map} -t drive -d "$1" || {
  136. rm -f ${tmp_map}
  137. return 1
  138. }
  139. echo "Please check ${device_map}, you might have to regenerate it with grub-mkdevicemap." >&2
  140. rm -f ${tmp_map}
  141. }
  142. }
  143. # Usage: convert_default os_device
  144. # Convert an OS device to the corresponding GRUB drive.
  145. convert_default () {
  146. # Check if device is software raid1 array
  147. if tmp_dev=$(convert_raid1 $1 2>/dev/null) ; then
  148. : # Use device returned by convert_raid1
  149. else
  150. tmp_dev=$1
  151. fi
  152. convert $tmp_dev
  153. }
  154. ## Configuration Options
  155. # Full path to the menu.lst
  156. menu_file_basename=menu.lst
  157. menu_file=$grub_dir/$menu_file_basename
  158. # Full path to the default file
  159. default_file_basename=default
  160. default_file=$grub_dir/$default_file_basename
  161. # the device for the / filesystem
  162. root_device=$(find_device "/")
  163. # loop-AES arranges things so that /dev/loop/X can be our root device, but
  164. # the initrds that Linux uses don't like that.
  165. case ${root_device} in
  166. /dev/loop/*|/dev/loop[0-9])
  167. root_device=`losetup ${root_device} | sed -e "s/^[^(]*(\([^)]\+\)).*/\1/"`
  168. ;;
  169. esac
  170. # the device for the /boot filesystem
  171. boot_device=$(find_device "/boot")
  172. # where grub looks for the kernels at boot time
  173. kernel_dir=`make_system_path_relative_to_its_root /boot`
  174. # the "-t abstraction" check is a workaround untill #484297 is fixed
  175. if abstraction=`grub-probe -t abstraction --device ${root_device} 2> /dev/null` && [ "$abstraction" = "" ] && \
  176. root_uuid=`grub-probe --device-map=${device_map} --device ${root_device} --target=fs_uuid 2> /dev/null` && \
  177. test -e "/dev/disk/by-uuid/${root_uuid}" ; then
  178. linux_root_device=UUID=${root_uuid}
  179. else
  180. linux_root_device=${root_device}
  181. fi
  182. # Default kernel options, overidden by the kopt statement in the menufile.
  183. kopt="root=$linux_root_device ro"
  184. # Title
  185. title=$(lsb_release --short --description 2>/dev/null) || title="Ubuntu"
  186. # should update-grub remember the default entry
  187. updatedefaultentry="false"
  188. # Drive(in GRUB terms) where the kernel is located. Overridden by the
  189. # groot statement in menufile.
  190. grub_root_device=$(convert_default "$boot_device")
  191. # should grub create the alternative boot options in the menu
  192. alternative="true"
  193. # should grub lock the alternative boot options in the menu
  194. lockalternative="false"
  195. # additional options to use with the default boot option, but not with the
  196. # alternatives
  197. defoptions="quiet splash"
  198. # should grub lock the old kernels
  199. lockold="false"
  200. # Xen hypervisor options to use with the default Xen boot option
  201. xenhopt=""
  202. # Xen Linux kernel options to use with the default Xen boot option
  203. xenkopt="console=tty0"
  204. # options to use with the alternative boot options
  205. altoptions="(recovery mode) single"
  206. # controls howmany kernels are listed in the config file,
  207. # this does not include the alternative kernels
  208. howmany="all"
  209. # should grub create a memtest86 entry
  210. memtest86="true"
  211. # should grub add "savedefault" to default boot options
  212. savedefault="false"
  213. # stores the command line arguments
  214. command_line_arguments=$1
  215. # does this version of grub support the quiet option?
  216. if [ -f ${grub_dir}/installed-version ] && dpkg --compare-versions `cat ${grub_dir}/installed-version` ge 0.97-11ubuntu4; then
  217. supports_quiet=true
  218. else
  219. supports_quiet=false
  220. fi
  221. # read user configuration
  222. if test -f "/etc/default/grub" ; then
  223. . /etc/default/grub
  224. fi
  225. # Default options to use in a new config file. This will only be used if $menu_file
  226. # doesn't already exist. Only edit the lines between the two "EOF"s. The others are
  227. # part of the script.
  228. newtemplate=$(tempfile)
  229. cat > "$newtemplate" <<EOF
  230. # $menu_file_basename - See: grub(8), info grub, update-grub(8)
  231. # grub-install(8), grub-floppy(8),
  232. # grub-md5-crypt, /usr/share/doc/grub
  233. # and /usr/share/doc/grub-legacy-doc/.
  234. ## default num
  235. # Set the default entry to the entry number NUM. Numbering starts from 0, and
  236. # the entry number 0 is the default if the command is not used.
  237. #
  238. # You can specify 'saved' instead of a number. In this case, the default entry
  239. # is the entry saved with the command 'savedefault'.
  240. # WARNING: If you are using dmraid do not change this entry to 'saved' or your
  241. # array will desync and will not let you boot your system.
  242. default 0
  243. ## timeout sec
  244. # Set a timeout, in SEC seconds, before automatically booting the default entry
  245. # (normally the first entry defined).
  246. timeout 5
  247. # Pretty colours
  248. color cyan/blue white/blue
  249. ## password ['--md5'] passwd
  250. # If used in the first section of a menu file, disable all interactive editing
  251. # control (menu entry editor and command-line) and entries protected by the
  252. # command 'lock'
  253. # e.g. password topsecret
  254. # password --md5 \$1\$gLhU0/\$aW78kHK1QfV3P2b2znUoe/
  255. # password topsecret
  256. #
  257. # examples
  258. #
  259. # title Windows 95/98/NT/2000
  260. # root (hd0,0)
  261. # makeactive
  262. # chainloader +1
  263. #
  264. # title Linux
  265. # root (hd0,1)
  266. # kernel /vmlinuz root=/dev/hda2 ro
  267. #
  268. #
  269. # Put static boot stanzas before and/or after AUTOMAGIC KERNEL LIST
  270. EOF
  271. ## End Configuration Options
  272. echo -n "Searching for default file ... " >&2
  273. if [ -f "$default_file" ] ; then
  274. echo "found: $default_file" >&2
  275. else
  276. echo "Generating $default_file file and setting the default boot entry to 0" >&2
  277. if [ -f /usr/lib/grub-legacy/grub-set-default ] ; then
  278. /usr/lib/grub-legacy/grub-set-default 0
  279. else
  280. grub-set-default 0
  281. fi
  282. fi
  283. # Make sure we use the standard sorting order
  284. LC_COLLATE=C
  285. # Magic markers we use
  286. start="### BEGIN AUTOMAGIC KERNELS LIST"
  287. end="### END DEBIAN AUTOMAGIC KERNELS LIST"
  288. startopt="## ## Start Default Options ##"
  289. endopt="## ## End Default Options ##"
  290. # Extract options from config file
  291. ExtractMenuOpt()
  292. {
  293. opt=$1
  294. sed -ne "/^$start\$/,/^$end\$/ {
  295. /^$startopt\$/,/^$endopt\$/ {
  296. /^# $opt=/ {
  297. s/^# $opt=\(.*\)\$/\1/
  298. p
  299. }
  300. }
  301. }" $menu
  302. }
  303. GetMenuOpts()
  304. {
  305. opt=$1
  306. sed -ne "/^$start\$/,/^$end\$/ {
  307. /^$startopt\$/,/^$endopt\$/ {
  308. /^# $opt=/ {
  309. p
  310. }
  311. }
  312. }" $menu
  313. }
  314. ExtractMenuOpts()
  315. {
  316. opt=$1
  317. GetMenuOpts $opt | sed "s/^# $opt=\(.*\)\$/\1=\"\2\"/"
  318. }
  319. GetMenuOpt()
  320. {
  321. opt=$1
  322. value=$2
  323. [ -z "$(GetMenuOpts "$opt")" ] || value=$(ExtractMenuOpt "$opt")
  324. echo $value
  325. }
  326. # Compares two version strings A and B
  327. # Returns -1 if A<B
  328. # 0 if A==B
  329. # 1 if A>B
  330. # This compares version numbers of the form
  331. # 2.4.14.2 > 2.4.14
  332. # 2.4.14random = 2.4.14-random > 2.4.14-ac10 > 2.4.14 > 2.4.14-pre2 >
  333. # 2.4.14-pre1 > 2.4.13-ac99
  334. CompareVersions()
  335. {
  336. local sedexp="s,.*/vmlinu[zx]-,,g;s/[._-]\(pre\|rc\|test\|git\|trunk\)/~\1/g"
  337. local a=`echo $1 | sed -e "$sedexp"`
  338. local b=`echo $2 | sed -e "$sedexp"`
  339. if [ "$a" = "$b" ] ; then
  340. echo 0
  341. elif dpkg --compare-versions "$a" gt "$b" ; then
  342. echo 1
  343. else
  344. echo -1
  345. fi
  346. }
  347. # looks in the directory specified for an initrd image with the version specified
  348. FindInitrdName()
  349. {
  350. # strip trailing slashes
  351. directory=$(echo $1 | sed -e 's#/*$##')
  352. version=$2
  353. # initrd
  354. # initrd.img
  355. # initrd-lvm
  356. # .*.gz
  357. initrdName=""
  358. names="initrd initrd.img initrd-lvm"
  359. compressed="gz"
  360. for n in $names ; do
  361. # make sure we haven't already found it
  362. if [ -z "$initrdName" ] ; then
  363. if [ -f "$directory/$n$version" ] ; then
  364. initrdName="$n$version"
  365. break
  366. else
  367. for c in $compressed ; do
  368. if [ -f "$directory/$n$version.$c" ] ; then
  369. initrdName="$n$version.$c"
  370. break
  371. fi
  372. done
  373. fi
  374. else
  375. break
  376. fi
  377. done
  378. # return the result
  379. echo $initrdName
  380. }
  381. FindXenHypervisorVersions ()
  382. {
  383. version=$1
  384. if [ -f "/var/lib/linux-image-$version/xen-versions" ]; then
  385. ret="$(cat /var/lib/linux-image-$version/xen-versions)"
  386. fi
  387. echo $ret
  388. }
  389. get_kernel_opt()
  390. {
  391. kernel_version=$1
  392. version=$(echo $kernel_version | sed 's/^[^0-9]*//')
  393. version=$(echo $version | sed 's/[-\+\.]/_/g')
  394. if [ -n "$version" ] ; then
  395. while [ -n "$version" ] ; do
  396. currentOpt="$(eval "echo \${kopt_$version}")"
  397. if [ -n "$currentOpt" ] ; then
  398. break
  399. fi
  400. version=$(echo $version | sed 's/_\?[^_]*$//')
  401. done
  402. fi
  403. if [ -z "$currentOpt" ] ; then
  404. currentOpt=$kopt
  405. fi
  406. echo $currentOpt
  407. }
  408. write_kernel_entry()
  409. {
  410. local kernel_version; kernel_version=$1; shift
  411. local recovery_desc; recovery_desc=$1; shift
  412. local lock_alternative; lock_alternative=$1; shift
  413. local grub_root_device; grub_root_device=$1; shift
  414. local kernel; kernel=$1; shift
  415. local kernel_options; kernel_options=$1; shift
  416. local recovery_suffix; recovery_suffix=$1; shift
  417. local initrd; initrd=$1; shift
  418. local savedefault; savedefault=$1; shift
  419. local lockold; lockold=$1; shift
  420. local hypervisor
  421. if [ -n "$1" ]; then
  422. # Hypervisor.
  423. hypervisor=$1; shift
  424. local hypervisor_image; hypervisor_image=$1; shift
  425. local hypervisor_version; hypervisor_version=$1; shift
  426. local hypervisor_options; hypervisor_options=$1; shift
  427. fi
  428. local grub_root_type
  429. case "$grub_root_device" in
  430. [^A-Za-z0-9]*) grub_root_type='root' ;;
  431. *) grub_root_type='uuid' ;;
  432. esac
  433. echo -n "title " >> $buffer
  434. if [ -n "$hypervisor" ]; then
  435. echo -n "$hypervisor $hypervisor_version / " >> $buffer
  436. fi
  437. echo -n "$title" >> $buffer
  438. if [ -n "$kernel_version" ]; then
  439. echo -n ", " >> $buffer
  440. # memtest86 is not strictly a kernel
  441. if ! echo "$kernel_version" | grep -q ^memtest86; then
  442. echo -n "kernel " >> $buffer
  443. fi
  444. echo -n "$kernel_version" >> $buffer
  445. fi
  446. if [ -n "$recovery_desc" ]; then
  447. echo -n " $recovery_desc" >> $buffer
  448. fi
  449. echo >> $buffer
  450. # lock the alternative options
  451. if test x"$lock_alternative" = x"true" ; then
  452. echo "lock" >> $buffer
  453. fi
  454. # lock the old entries
  455. if test x"$lockold" = x"true" ; then
  456. echo "lock" >> $buffer
  457. fi
  458. echo "$grub_root_type $grub_root_device" >> $buffer
  459. echo -n "kernel " >> $buffer
  460. if [ -n "$hypervisor" ]; then
  461. echo -n "$hypervisor_image" >> $buffer
  462. if [ -n "$hypervisor_options" ]; then
  463. echo -n " $hypervisor_options" >> $buffer
  464. fi
  465. echo >> $buffer
  466. echo -n "module " >> $buffer
  467. fi
  468. echo -n "$kernel" >> $buffer
  469. if [ -n "$kernel_options" ]; then
  470. echo -n " $kernel_options" >> $buffer
  471. fi
  472. if [ -n "$recovery_desc" ]; then
  473. echo -n " $recovery_suffix" >> $buffer
  474. fi
  475. echo >> $buffer
  476. if [ -n "$initrd" ]; then
  477. if [ -n "$hypervisor" ]; then
  478. echo -n "module " >> $buffer
  479. else
  480. echo -n "initrd " >> $buffer
  481. fi
  482. echo "$initrd" >> $buffer
  483. fi
  484. if [ ! -n "$recovery_desc" -a x"$supports_quiet" = x"true" ]; then
  485. echo "quiet" >> $buffer
  486. fi
  487. if test x"$savedefault" = x"true" ; then
  488. echo "savedefault" >> $buffer
  489. fi
  490. echo >> $buffer
  491. }
  492. echo -n "Testing for an existing GRUB $menu_file_basename file ... " >&2
  493. # Test if our menu file exists
  494. if [ -f "$menu_file" ] ; then
  495. menu="$menu_file"
  496. rm -f $newtemplate
  497. unset newtemplate
  498. echo "found: $menu_file" >&2
  499. cp -f "$menu_file" "$menu_file~"
  500. else
  501. # if not ask user if they want us to create one
  502. menu="$menu_file"
  503. echo >&2
  504. echo >&2
  505. if [ "-y" = "$command_line_arguments" ] ; then
  506. echo "Warning: ignoring deprecated -y option." >&2
  507. fi
  508. echo >&2
  509. echo "Generating $menu_file" >&2
  510. cat "$newtemplate" > $menu_file
  511. rm -f $newtemplate
  512. unset newtemplate
  513. fi
  514. # Extract the kernel options to use
  515. kopt=$(GetMenuOpt "kopt" "$kopt")
  516. # Set the kernel 2.6 option only for fresh install
  517. test -z "$(GetMenuOpt "kopt" "")" && kopt_2_6="root=$linux_root_device ro"
  518. # Extract options for specific kernels
  519. opts="$(ExtractMenuOpts "\(kopt_[[:alnum:]_]\+\)")"
  520. test -z "$opts" || eval "$opts"
  521. CustomKopts=$(GetMenuOpts "\(kopt_[[:alnum:]_]\+\)")
  522. # Extract the grub root
  523. grub_root_device=$(GetMenuOpt "groot" "$grub_root_device")
  524. # Extract the old recovery value
  525. alternative=$(GetMenuOpt "recovery" "$alternative")
  526. # Extract the alternative value
  527. alternative=$(GetMenuOpt "alternative" "$alternative")
  528. # Extract the lockalternative value
  529. lockalternative=$(GetMenuOpt "lockalternative" "$lockalternative")
  530. # Extract the additional default options
  531. defoptions=$(GetMenuOpt "defoptions" "$defoptions")
  532. # Extract the lockold value
  533. lockold=$(GetMenuOpt "lockold" "$lockold")
  534. # Extract Xen hypervisor options
  535. xenhopt=$(GetMenuOpt "xenhopt" "$xenhopt")
  536. # Extract Xen Linux kernel options
  537. xenkopt=$(GetMenuOpt "xenkopt" "$xenkopt")
  538. # Extract the howmany value
  539. howmany=$(GetMenuOpt "howmany" "$howmany")
  540. # Extract the memtest86 value
  541. memtest86=$(GetMenuOpt "memtest86" "$memtest86")
  542. # Extract the updatedefaultentry option
  543. updatedefaultentry=$(GetMenuOpt "updatedefaultentry" "$updatedefaultentry")
  544. # Extract the savedefault option
  545. savedefault=$(GetMenuOpt "savedefault" "$savedefault")
  546. # Generate the menu options we want to insert
  547. buffer=$(tempfile)
  548. echo $start >> $buffer
  549. echo "## lines between the AUTOMAGIC KERNELS LIST markers will be modified" >> $buffer
  550. echo "## by the debian update-grub script except for the default options below" >> $buffer
  551. echo >> $buffer
  552. echo "## DO NOT UNCOMMENT THEM, Just edit them to your needs" >> $buffer
  553. echo >> $buffer
  554. echo "## ## Start Default Options ##" >> $buffer
  555. echo "## default kernel options" >> $buffer
  556. echo "## default kernel options for automagic boot options" >> $buffer
  557. echo "## If you want special options for specific kernels use kopt_x_y_z" >> $buffer
  558. echo "## where x.y.z is kernel version. Minor versions can be omitted." >> $buffer
  559. echo "## e.g. kopt=root=/dev/hda1 ro" >> $buffer
  560. echo "## kopt_2_6_8=root=/dev/hdc1 ro" >> $buffer
  561. echo "## kopt_2_6_8_2_686=root=/dev/hdc2 ro" >> $buffer
  562. echo "# kopt=$kopt" >> $buffer
  563. if [ -n "$CustomKopts" ] ; then
  564. echo "$CustomKopts" >> $buffer
  565. elif [ -n "$kopt_2_6" ] && [ "$kopt" != "$kopt_2_6" ]; then
  566. echo "# kopt_2_6=$kopt_2_6" >> $buffer
  567. fi
  568. echo >> $buffer
  569. echo "## default grub root device" >> $buffer
  570. echo "## e.g. groot=(hd0,0)" >> $buffer
  571. echo "# groot=$grub_root_device" >> $buffer
  572. echo >> $buffer
  573. echo "## should update-grub create alternative automagic boot options" >> $buffer
  574. echo "## e.g. alternative=true" >> $buffer
  575. echo "## alternative=false" >> $buffer
  576. echo "# alternative=$alternative" >> $buffer
  577. echo >> $buffer
  578. echo "## should update-grub lock alternative automagic boot options" >> $buffer
  579. echo "## e.g. lockalternative=true" >> $buffer
  580. echo "## lockalternative=false" >> $buffer
  581. echo "# lockalternative=$lockalternative" >> $buffer
  582. echo >> $buffer
  583. echo "## additional options to use with the default boot option, but not with the" >> $buffer
  584. echo "## alternatives" >> $buffer
  585. echo "## e.g. defoptions=vga=791 resume=/dev/hda5" >> $buffer
  586. echo "# defoptions=$defoptions" >> $buffer
  587. echo >> $buffer
  588. echo "## should update-grub lock old automagic boot options" >> $buffer
  589. echo "## e.g. lockold=false" >> $buffer
  590. echo "## lockold=true" >> $buffer
  591. echo "# lockold=$lockold" >> $buffer
  592. echo >> $buffer
  593. echo "## Xen hypervisor options to use with the default Xen boot option" >> $buffer
  594. echo "# xenhopt=$xenhopt" >> $buffer
  595. echo >> $buffer
  596. echo "## Xen Linux kernel options to use with the default Xen boot option" >> $buffer
  597. echo "# xenkopt=$xenkopt" >> $buffer
  598. echo >> $buffer
  599. echo "## altoption boot targets option" >> $buffer
  600. echo "## multiple altoptions lines are allowed" >> $buffer
  601. echo "## e.g. altoptions=(extra menu suffix) extra boot options" >> $buffer
  602. echo "## altoptions=(recovery) single" >> $buffer
  603. if ! grep -q "^# altoptions" $menu ; then
  604. echo "# altoptions=$altoptions" >> $buffer
  605. else
  606. grep "^# altoptions" $menu >> $buffer
  607. fi
  608. echo >> $buffer
  609. echo "## controls how many kernels should be put into the $menu_file_basename" >> $buffer
  610. echo "## only counts the first occurence of a kernel, not the" >> $buffer
  611. echo "## alternative kernel options" >> $buffer
  612. echo "## e.g. howmany=all" >> $buffer
  613. echo "## howmany=7" >> $buffer
  614. echo "# howmany=$howmany" >> $buffer
  615. echo >> $buffer
  616. echo "## should update-grub create memtest86 boot option" >> $buffer
  617. echo "## e.g. memtest86=true" >> $buffer
  618. echo "## memtest86=false" >> $buffer
  619. echo "# memtest86=$memtest86" >> $buffer
  620. echo >> $buffer
  621. echo "## should update-grub adjust the value of the default booted system" >> $buffer
  622. echo "## can be true or false" >> $buffer
  623. echo "# updatedefaultentry=$updatedefaultentry" >> $buffer
  624. echo >> $buffer
  625. echo "## should update-grub add savedefault to the default options" >> $buffer
  626. echo "## can be true or false" >> $buffer
  627. echo "# savedefault=$savedefault" >> $buffer
  628. echo >> $buffer
  629. echo "## ## End Default Options ##" >> $buffer
  630. echo >> $buffer
  631. echo -n "Searching for splash image ... " >&2
  632. current_splash=`grep '^splashimage=' ${menu_file} || true`
  633. grub_dir_rel=`make_system_path_relative_to_its_root $grub_dir`
  634. splashimage_path="splashimage=${grub_root_device}/${grub_dir_rel##${kernel_dir}}/splash.xpm.gz"
  635. if [ `sed -e "/^$start/,/^$end/d" $menu_file | grep -c '^splashimage='` != "0" ] ; then
  636. #checks for splashscreen defined outside the autoupdated part
  637. splashimage=$(grep '^splashimage=' ${menu_file})
  638. echo "found: ${splashimage##*=}" >&2
  639. echo >&2
  640. elif [ -f "${grub_dir}/splash.xpm.gz" ] && [ "$current_splash" = "" ]; then
  641. echo "found: /boot/grub/splash.xpm.gz" >&2
  642. echo "$splashimage_path" >> $buffer
  643. echo >> $buffer
  644. elif [ -f "${grub_dir}/splash.xpm.gz" ] && [ "$current_splash" = "$splashimage_path" ]; then
  645. echo "found: /boot/grub/splash.xpm.gz" >&2
  646. echo "$splashimage_path" >> $buffer
  647. echo >> $buffer
  648. elif [ "$current_splash" != "" ] && [ "$current_splash" != "$splashimage_path" ]; then
  649. echo "found but preserving previous setting: $(grep '^splashimage=' ${menu_file})" >&2
  650. echo "$current_splash" >> $buffer
  651. echo >> $buffer
  652. else
  653. echo "none found, skipping ..." >&2
  654. fi
  655. xen0Kernels=""
  656. # First kernels with xen0 support.
  657. for ver in `grep -l CONFIG_XEN=y /boot/config* | sed -e s%/boot/config-%%`; do
  658. if ! grep -q CONFIG_XEN_PRIVILEGED_GUEST=y /boot/config-$ver ; then
  659. continue
  660. fi
  661. # ver is a kernel version
  662. kern="/boot/vmlinuz-$ver"
  663. if [ -r $kern ] ; then
  664. newerKernels=""
  665. for i in $xen0Kernels ; do
  666. res=$(CompareVersions "$kern" "$i")
  667. if [ "$kern" != "" ] && [ "$res" -gt 0 ] ; then
  668. newerKernels="$newerKernels $kern $i"
  669. kern=""
  670. else
  671. newerKernels="$newerKernels $i"
  672. fi
  673. done
  674. if [ "$kern" != "" ] ; then
  675. newerKernels="$newerKernels $kern"
  676. fi
  677. xen0Kernels="$newerKernels"
  678. fi
  679. done
  680. sortedKernels=""
  681. for kern in $(/bin/ls -1vr /boot | grep -v "dpkg-*" | grep "^vmlinuz-") ; do
  682. kern="/boot/$kern"
  683. newerKernels=""
  684. for i in $sortedKernels ; do
  685. res=$(CompareVersions "$kern" "$i")
  686. if [ "$kern" != "" ] && [ "$res" -gt 0 ] ; then
  687. newerKernels="$newerKernels $kern $i"
  688. kern=""
  689. else
  690. newerKernels="$newerKernels $i"
  691. fi
  692. done
  693. if [ "$kern" != "" ] ; then
  694. newerKernels="$newerKernels $kern"
  695. fi
  696. sortedKernels="$newerKernels"
  697. done
  698. if test -f "/boot/vmlinuz.old" ; then
  699. sortedKernels="/boot/vmlinuz.old $sortedKernels"
  700. fi
  701. if test -f "/boot/vmlinuz" ; then
  702. sortedKernels="/boot/vmlinuz $sortedKernels"
  703. fi
  704. hypervisors=""
  705. for hyp in /boot/xen-*.gz; do
  706. if [ ! -h "$hyp" ] && [ -f "$hyp" ]; then
  707. hypervisors="$hypervisors `basename "$hyp"`"
  708. fi
  709. done
  710. #Finding the value the default line
  711. use_grub_set_default="false"
  712. if test "$updatedefaultentry" = "true" ; then
  713. defaultEntryNumber=$(sed -ne 's/^[[:blank:]]*default[[:blank:]]*\(.*\).*/\1/p' $menu)
  714. if [ "$defaultEntryNumber" = "saved" ] ; then
  715. defaultEntryNumber=$(sed 'q' "$grub_dir/default")
  716. use_grub_set_default="true"
  717. fi
  718. if test -n "$defaultEntryNumber"; then
  719. defaultEntryNumberPlusOne=$(expr $defaultEntryNumber \+ 1);
  720. defaultEntry=$(grep "^[[:blank:]]*title" $menu | sed -ne "${defaultEntryNumberPlusOne}p" | sed -ne ";s/^[[:blank:]]*title[[:blank:]]*//p")
  721. defaultEntry=$(echo $defaultEntry | sed -e "s/[[:blank:]]*$//") # don't trust trailing blanks
  722. else
  723. notChangeDefault="yes"
  724. fi
  725. else
  726. notChangeDefault="yes"
  727. fi
  728. ## heres where we start writing out the kernel entries
  729. counter=0
  730. case "$grub_root_device" in
  731. [^A-Za-z0-9]*) grub_root_type='root' ;;
  732. *) grub_root_type='uuid' ;;
  733. esac
  734. grub2name="${kernel_dir}/grub/core.img"
  735. if [ "$LET_US_TRY_GRUB_2" = "true" ] \
  736. && test -f /boot/grub/core.img ; then
  737. echo "Found GRUB 2: $grub2name" >&2
  738. cat >> $buffer << EOF
  739. title Chainload into GRUB 2
  740. $grub_root_type $grub_root_device
  741. kernel $grub2name
  742. title `echo ───────────────────────────────────────────────────────────────────── | iconv -f utf-8 -t cp437`
  743. root
  744. title When you have verified GRUB 2 works, you can use this command to
  745. root
  746. title complete the upgrade: upgrade-from-grub-legacy
  747. root
  748. title `echo ───────────────────────────────────────────────────────────────────── | iconv -f utf-8 -t cp437`
  749. root
  750. EOF
  751. fi
  752. # Xen entries first.
  753. for kern in $xen0Kernels ; do
  754. if test ! x"$howmany" = x"all" ; then
  755. if [ $counter -gt $howmany ] ; then
  756. break
  757. fi
  758. fi
  759. kernelName=$(basename $kern)
  760. kernelVersion=$(echo $kernelName | sed -e 's/vmlinuz//')
  761. initrdName=$(FindInitrdName "/boot" "$kernelVersion")
  762. initrd=""
  763. kernel=$kernel_dir/$kernelName
  764. if [ -n "$initrdName" ] ; then
  765. initrd=$kernel_dir/$initrdName
  766. fi
  767. kernelVersion=$(echo $kernelVersion | sed -e 's/^-//')
  768. currentOpt=$(get_kernel_opt $kernelVersion)
  769. hypervisorVersions=$(FindXenHypervisorVersions "$kernelVersion")
  770. found=
  771. for hypervisorVersion in $hypervisorVersions; do
  772. hypervisor="$kernel_dir/xen-$hypervisorVersion.gz"
  773. if [ -e "$hypervisor" ]; then
  774. found=1
  775. echo "Found Xen hypervisor $hypervisorVersion, kernel: $kernel" >&2
  776. write_kernel_entry "$kernelVersion" '' '' "$grub_root_device" \
  777. "$kernel" "$currentOpt $xenkopt" '' "$initrd" "$savedefault" '' \
  778. Xen "$hypervisor" "$hypervisorVersion" "$xenhopt"
  779. counter=$(($counter + 1))
  780. fi
  781. done
  782. if [ -z $found ]; then
  783. for hypervisor in $hypervisors; do
  784. hypVersion=`basename "$hypervisor" .gz | sed s%xen-%%`
  785. echo "Found Xen hypervisor $hypVersion, kernel: $kernel" >&2
  786. write_kernel_entry "$kernelVersion" '' '' "$grub_root_device" \
  787. "$kernel" "$currentOpt $xenkopt" '' "$initrd" "$savedefault" '' \
  788. Xen "$kernel_dir/$hypervisor" "$hypVersion" "$xenhopt"
  789. counter=$(($counter + 1))
  790. done
  791. fi
  792. done
  793. for kern in $sortedKernels ; do
  794. counter=$(($counter + 1))
  795. if test ! x"$howmany" = x"all" ; then
  796. if [ $counter -gt $howmany ] ; then
  797. break
  798. fi
  799. fi
  800. kernelName=$(basename $kern)
  801. kernelVersion=$(echo $kernelName | sed -e 's/vmlinuz//')
  802. initrdName=$(FindInitrdName "/boot" "$kernelVersion")
  803. initrd=""
  804. kernel=$kernel_dir/$kernelName
  805. if [ -n "$initrdName" ] ; then
  806. initrd=$kernel_dir/$initrdName
  807. fi
  808. echo "Found kernel: $kernel" >&2
  809. if [ "$kernelName" = "vmlinuz" ]; then
  810. if [ -L "/boot/$kernelName" ]; then
  811. kernelVersion=`readlink -f "/boot/$kernelName"`
  812. kernelVersion=$(echo $kernelVersion | sed -e 's/.*vmlinuz-//')
  813. kernelVersion="$kernelVersion Default"
  814. else
  815. kernelVersion="Default"
  816. fi
  817. fi
  818. if [ "$kernelName" = "vmlinuz.old" ]; then
  819. if [ -L "/boot/$kernelName" ]; then
  820. kernelVersion=`readlink -f "/boot/$kernelName"`
  821. kernelVersion=$(echo $kernelVersion | sed -e 's/.*vmlinuz-//')
  822. kernelVersion="$kernelVersion Previous"
  823. else
  824. kernelVersion="Previous"
  825. fi
  826. fi
  827. kernelVersion=$(echo $kernelVersion | sed -e 's/^-//')
  828. currentOpt=$(get_kernel_opt $kernelVersion)
  829. do_lockold=$lockold
  830. # do not lockold for the first entry
  831. [ $counter -eq 1 ] && do_lockold=false
  832. write_kernel_entry "$kernelVersion" "" "" "$grub_root_device" "$kernel" \
  833. "$currentOpt $defoptions" "" "$initrd" "$savedefault" "$do_lockold"
  834. # insert the alternative boot options
  835. if test ! x"$alternative" = x"false" ; then
  836. # for each altoptions line do this stuff
  837. sed -ne 's/# altoptions=\(.*\)/\1/p' $buffer | while read line; do
  838. descr=$(echo $line | sed -ne 's/\(([^)]*)\)[[:space:]]\(.*\)/\1/p')
  839. suffix=$(echo $line | sed -ne 's/\(([^)]*)\)[[:space:]]\(.*\)/\2/p')
  840. test x"$lockalternative" = x"true" && do_lockold=false
  841. write_kernel_entry "$kernelVersion" "$descr" "$lockalternative" \
  842. "$grub_root_device" "$kernel" "$currentOpt" "$suffix" "$initrd" \
  843. "$savedefault" "$do_lockold"
  844. done
  845. fi
  846. done
  847. memtest86names="memtest86 memtest86+"
  848. if test ! x"$memtest86" = x"false" ; then
  849. for name in $memtest86names ; do
  850. if test -f "/boot/$name.bin" ; then
  851. kernelVersion="$name"
  852. kernel="$kernel_dir/$name.bin"
  853. currentOpt=
  854. initrd=
  855. echo "Found kernel: $kernel" >&2
  856. write_kernel_entry "$kernelVersion" "" "" "$grub_root_device" \
  857. "$kernel" "$currentOpt" "" "$initrd" "false" ""
  858. fi
  859. done
  860. fi
  861. echo $end >> $buffer
  862. echo -n "Updating $menu ... " >&2
  863. # Insert the new options into the menu
  864. if ! grep -q "^$start" $menu ; then
  865. cat $buffer >> $menu
  866. rm -f $buffer
  867. else
  868. umask 077
  869. sed -e "/^$start/,/^$end/{
  870. /^$start/r $buffer
  871. d
  872. }
  873. " $menu > $menu.new
  874. cat $menu.new > $menu
  875. rm -f $buffer $menu.new
  876. fi
  877. # Function to update the default value
  878. set_default_value() {
  879. if [ "$use_grub_set_default" = "true" ] ; then
  880. if [ -f /usr/lib/grub-legacy/grub-set-default ] ; then
  881. /usr/lib/grub-legacy/grub-set-default $1
  882. else
  883. grub-set-default $1
  884. fi
  885. else
  886. value="$1"
  887. newmenu=$(tempfile)
  888. sed -e "s/^[[:blank:]]*default[[:blank:]]*[[:digit:]]*\(.*\)/default ${value}\1/;b" $menu > $newmenu
  889. cat $newmenu > $menu
  890. rm -f $newmenu
  891. unset newmenu
  892. fi
  893. }
  894. #Updating the default number
  895. if [ "$LET_US_TRY_GRUB_2" = "true" ] && test -f /boot/grub/core.img ; then
  896. set_default_value "0"
  897. elif test -z "$notChangeDefault"; then
  898. newDefaultNumberPlusOne=$(grep "^[[:blank:]]*title[[:blank:]]*" $menu | grep -n "${defaultEntry}" | cut -f1 -d ":" | sed -ne "1p")
  899. if test -z "$newDefaultNumberPlusOne"; then
  900. echo "Previous default entry removed, resetting to 0">&2
  901. set_default_value "0"
  902. elif test -z "$defaultEntry"; then
  903. echo "Value of default value matches no entry, resetting to 0" >&2
  904. set_default_value "0"
  905. else
  906. if test "$newDefaultNumberPlusOne" = "1"; then
  907. newDefaultNumber="0"
  908. else
  909. newDefaultNumber=$(expr $newDefaultNumberPlusOne - 1)
  910. fi
  911. echo "Updating the default booting kernel">&2
  912. set_default_value "$newDefaultNumber"
  913. fi
  914. fi
  915. echo "done" >&2
  916. echo >&2