Advertisement
Guest User

Untitled

a guest
May 7th, 2017
550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 40.70 KB | None | 0 0
  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 2007, 2008 Canonical Ltd.
  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 2 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 this program; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. #
  21. # Contributors:
  22. # Jason Thomas <jason@debian.org>
  23. # David B.Harris <dbarclay10@yahoo.ca>
  24. # Marc Haber <mh@zugschlus.de>
  25. # Crispin Flowerday <crispin@zeus.com>
  26. # Steve Langasek <steve.langasek@canonical.com>
  27.  
  28. # Abort on errors
  29. set -e
  30.  
  31. # load debconf first, since this re-execs the script
  32. . /usr/share/debconf/confmodule
  33.  
  34. host_os=`uname -s | tr '[A-Z]' '[a-z]'`
  35.  
  36. abort() {
  37. message=$@
  38.  
  39. echo >&2
  40. printf '%s\n' "$message" >&2
  41. echo >&2
  42. exit 1
  43. }
  44.  
  45. find_grub_dir ()
  46. {
  47. echo -n "Searching for GRUB installation directory ... " >&2
  48.  
  49. for d in $grub_dirs ; do
  50. if [ -d "$d" ] ; then
  51. grub_dir="$d"
  52. break
  53. fi
  54. done
  55.  
  56. if [ -z "$grub_dir" ] ; then
  57. abort "No GRUB directory found.
  58. To create a template run 'mkdir /boot/grub' first.
  59. To install grub, install it manually or try the 'grub-install' command.
  60. ### Warning, grub-install is used to change your MBR. ###"
  61. else
  62. echo "found: $grub_dir" >&2
  63. fi
  64.  
  65. echo $grub_dir
  66. }
  67.  
  68. find_device ()
  69. {
  70. mount_point=$1
  71.  
  72. # Autodetect current root device
  73. device=
  74. if [ -f /etc/fstab ] ; then
  75. device=$(awk '$1!~/^#/{
  76. if ($2 == "'"$mount_point"'"){
  77. print $1;
  78. }
  79. }' /etc/fstab | tail -n 1)
  80. fi
  81.  
  82. if [ -n "$device" ] ; then
  83. case "$device" in
  84. LABEL=* | UUID=*)
  85. device=`readlink -f "$(findfs $device)"`
  86. ;;
  87. *)
  88. device=`readlink -f "$device"`
  89. ;;
  90. esac
  91. fi
  92.  
  93. echo $device
  94. }
  95.  
  96. find_root_device ()
  97. {
  98. device=$(find_device "/")
  99.  
  100. if [ -z "$device" ]; then
  101. echo "Cannot determine root device. Assuming /dev/hda1" >&2
  102. echo "This error is probably caused by an invalid /etc/fstab" >&2
  103. device=/dev/hda1
  104. fi
  105.  
  106. echo $device
  107. }
  108.  
  109. # Usage: convert_raid1 os_device
  110. # Checks if os_device is a software raid1.
  111. # If so, converts to first physical device in array.
  112. convert_raid1 ()
  113. {
  114. case $1 in
  115. /dev/md[0-9])
  116. : ;; # Continue
  117. *)
  118. return 1 ;;
  119. esac
  120.  
  121. [ -x /sbin/mdadm ] || return 1
  122.  
  123. # Check that the raid device is raid1
  124. raidlevel=$(mdadm -D -b $1 | grep "^ARRAY" | \
  125. sed "s/^.*level=//" | cut -d" " -f1)
  126. [ "$raidlevel" = "raid1" ] || return 1
  127.  
  128. # Take only the first device that makes up the raid
  129. raiddev=$(mdadm -D $1 | grep -A1 "Number" | grep "dev" \
  130. | sed "s/^.*\(\/dev\/.*\)$/\1/")
  131. [ -n "$raiddev" ] || return 1
  132.  
  133. echo $raiddev
  134. return 0
  135. }
  136.  
  137. # Usage: convert os_device
  138. # Convert an OS device to the corresponding GRUB drive.
  139. # This part is OS-specific.
  140. convert () {
  141. # First, check if the device file exists.
  142. if test -e "$1"; then
  143. :
  144. else
  145. echo "$1: Not found or not a block device." 1>&2
  146. exit 1
  147. fi
  148.  
  149. host_os=`uname -s | tr '[[:upper:]]' '[[:lower:]]'`
  150.  
  151. # Break the device name into the disk part and the partition part.
  152. case "$host_os" in
  153. linux)
  154. tmp_disk=`echo "$1" | sed -e 's%\([sh]d[[:lower:]]\)[0-9]*$%\1%' \
  155. -e 's%\(fd[0-9]*\)$%\1%' \
  156. -e 's%/part[0-9]*$%/disc%' \
  157. -e 's%\(c[0-7]d[0-9]*\).*$%\1%'`
  158. tmp_part=`echo "$1" | sed -e 's%.*/[sh]d[[:lower:]]\([0-9]*\)$%\1%' \
  159. -e 's%.*/fd[0-9]*$%%' \
  160. -e 's%.*/floppy/[0-9]*$%%' \
  161. -e 's%.*/\(disc\|part\([0-9]*\)\)$%\2%' \
  162. -e 's%.*c[0-7]d[0-9]*p*%%'`
  163. ;;
  164. gnu)
  165. tmp_disk=`echo "$1" | sed 's%\([sh]d[0-9]*\).*%\1%'`
  166. tmp_part=`echo "$1" | sed "s%$tmp_disk%%"` ;;
  167. freebsd|*/kfreebsd)
  168. tmp_disk=`echo "$1" | sed 's%r\{0,1\}\([saw]d[0-9]*\).*$%\1%' \
  169. | sed 's%r\{0,1\}\(da[0-9]*\).*$%\1%'`
  170. tmp_part=`echo "$1" \
  171. | sed "s%.*/r\{0,1\}[saw]d[0-9]\(s[0-9]*[a-h]\)%\1%" \
  172. | sed "s%.*/r\{0,1\}da[0-9]\(s[0-9]*[a-h]\)%\1%"`
  173. ;;
  174. netbsd|*/knetbsd)
  175. tmp_disk=`echo "$1" | sed 's%r\{0,1\}\([sw]d[0-9]*\).*$%r\1d%' \
  176. | sed 's%r\{0,1\}\(fd[0-9]*\).*$%r\1a%'`
  177. tmp_part=`echo "$1" \
  178. | sed "s%.*/r\{0,1\}[sw]d[0-9]\([abe-p]\)%\1%"`
  179. ;;
  180. *)
  181. echo "update-grub does not support your OS yet." 1>&2
  182. exit 1 ;;
  183. esac
  184.  
  185. # Get the drive name.
  186. tmp_drive=`grep -v '^#' $device_map | grep "$tmp_disk *$" \
  187. | sed 's%.*\(([hf]d[0-9][a-z0-9,]*)\).*%\1%'`
  188.  
  189. # If not found, print an error message and exit.
  190. if test "x$tmp_drive" = x; then
  191. echo "$1 does not have any corresponding BIOS drive." 1>&2
  192. exit 1
  193. fi
  194.  
  195. if test "x$tmp_part" != x; then
  196. # If a partition is specified, we need to translate it into the
  197. # GRUB's syntax.
  198. case "$host_os" in
  199. linux)
  200. echo "$tmp_drive" | sed "s%)$%,`expr $tmp_part - 1`)%" ;;
  201. gnu)
  202. if echo $tmp_part | grep "^s" >/dev/null; then
  203. tmp_pc_slice=`echo $tmp_part \
  204. | sed "s%s\([0-9]*\)[a-z]*$%\1%"`
  205. tmp_drive=`echo "$tmp_drive" \
  206. | sed "s%)%,\`expr "$tmp_pc_slice" - 1\`)%"`
  207. fi
  208. if echo $tmp_part | grep "[a-z]$" >/dev/null; then
  209. tmp_bsd_partition=`echo "$tmp_part" \
  210. | sed "s%[^a-z]*\([a-z]\)$%\1%"`
  211. tmp_drive=`echo "$tmp_drive" \
  212. | sed "s%)%,$tmp_bsd_partition)%"`
  213. fi
  214. echo "$tmp_drive" ;;
  215. freebsd|*/kfreebsd)
  216. if echo $tmp_part | grep "^s" >/dev/null; then
  217. tmp_pc_slice=`echo $tmp_part \
  218. | sed "s%s\([0-9]*\)[a-h]*$%\1%"`
  219. tmp_drive=`echo "$tmp_drive" \
  220. | sed "s%)%,\`expr "$tmp_pc_slice" - 1\`)%"`
  221. fi
  222. if echo $tmp_part | grep "[a-h]$" >/dev/null; then
  223. tmp_bsd_partition=`echo "$tmp_part" \
  224. | sed "s%s\{0,1\}[0-9]*\([a-h]\)$%\1%"`
  225. tmp_drive=`echo "$tmp_drive" \
  226. | sed "s%)%,$tmp_bsd_partition)%"`
  227. fi
  228. echo "$tmp_drive" ;;
  229. netbsd|*/knetbsd)
  230. if echo $tmp_part | grep "^[abe-p]$" >/dev/null; then
  231. tmp_bsd_partition=`echo "$tmp_part" \
  232. | sed "s%\([a-p]\)$%\1%"`
  233. tmp_drive=`echo "$tmp_drive" \
  234. | sed "s%)%,$tmp_bsd_partition)%"`
  235. fi
  236. echo "$tmp_drive" ;;
  237. esac
  238. else
  239. # If no partition is specified, just print the drive name.
  240. echo "$tmp_drive"
  241. fi
  242. }
  243.  
  244. # Usage: convert_default os_device
  245. # Convert an OS device to the corresponding GRUB drive.
  246. # Calls OS-specific convert, and returns a default of
  247. # (hd0,0) if anything goes wrong
  248. convert_default () {
  249. # Check if device is software raid1 array
  250. if tmp_dev=$(convert_raid1 $1 2>/dev/null) ; then
  251. : # Use device returned by convert_raid1
  252. else
  253. tmp_dev=$1
  254. fi
  255.  
  256. if tmp=$(convert $tmp_dev 2>/dev/null) ; then
  257. echo $tmp
  258. else
  259. echo "(hd0,0)"
  260. fi
  261. }
  262.  
  263. is_removable () {
  264. removabledevice="$(echo "$1" | sed -e 's%\([sh]d[a-z]\)[0-9]*$%\1%' -e 's%\(fd[0-9]*\)$%\1%' -e 's%/part[0-9]*$%/disc%' -e 's%\(c[0-7]d[0-9]*\).*$%\1%' -e 's%^/dev/%%g')"
  265. if [ -e "/sys/block/$removabledevice/removable" ]; then
  266. if [ "$(cat /sys/block/$removabledevice/removable)" != "0" ]; then
  267. echo "/dev/$removabledevice"
  268. return
  269. fi
  270. fi
  271. echo ""
  272. }
  273.  
  274. convert_to_uuid()
  275. {
  276. local dev; dev=$1
  277.  
  278. convert=false
  279. case "$dev" in
  280. /dev/disk/*)
  281. ;;
  282. /dev/mapper/*)
  283. ;;
  284. /dev/evms/[hs]d[a-z][0-9]*)
  285. convert=:
  286. ;;
  287. /dev/evms/*)
  288. ;;
  289. /dev/md[0-9]*)
  290. ;;
  291. /dev/*)
  292. convert=:
  293. ;;
  294. esac
  295. if $convert; then
  296. if [ -b "$dev" ]; then
  297. uuid=$(/sbin/vol_id -u "$dev" || true)
  298. fi
  299. fi
  300.  
  301. echo "$uuid"
  302. }
  303.  
  304. convert_kopt_to_uuid()
  305. {
  306. local kopt; kopt=$1
  307.  
  308. convert=false
  309. root=$(echo "$kopt" | sed 's/.*root=//;s/ .*//')
  310. case "$root" in
  311. UUID=*|LABEL=*)
  312. ;;
  313. /dev/disk/*)
  314. ;;
  315. /dev/mapper/*)
  316. ;;
  317. /dev/evms/[hs]d[a-z][0-9]*)
  318. convert=:
  319. ;;
  320. /dev/evms/*)
  321. ;;
  322. /dev/md[0-9]*)
  323. ;;
  324. /dev/*)
  325. convert=:
  326. ;;
  327. esac
  328. if $convert; then
  329. if [ -L "$DEV" ] && readlink "$DEV" | grep -q "^/dev/mapper/"
  330. then
  331. :
  332. elif [ -b "$root" ]; then
  333. uuid=$(/sbin/vol_id -u "$root" || true)
  334. if [ -n "$uuid" ]; then
  335. kopt=$(echo "$kopt" | sed "s/\(.*root=\)[^ ]*/\1UUID=$uuid/")
  336. fi
  337. fi
  338. fi
  339.  
  340. echo "$kopt"
  341. }
  342.  
  343.  
  344. ## Configuration Options
  345. # directory's to look for the grub installation and the menu file
  346. grub_dirs="/boot/grub /boot/boot/grub"
  347.  
  348. # The grub installation directory
  349. grub_dir=$(find_grub_dir)
  350.  
  351. # Full path to the menu.lst
  352. menu_file_basename=menu.lst
  353. menu_file=$grub_dir/$menu_file_basename
  354.  
  355. # Full path to the menu.lst fragment used for ucf management
  356. ucf_menu_file=/var/run/grub/$menu_file_basename
  357.  
  358. # Full path to the default file
  359. default_file_basename=default
  360. default_file=$grub_dir/$default_file_basename
  361.  
  362. # the device for the / filesystem
  363. root_device=$(find_root_device)
  364.  
  365. # the device for the /boot filesystem
  366. boot_device=$(find_device "/boot")
  367.  
  368. # Full path to the device.map
  369. device_map=$grub_dir/device.map
  370.  
  371. # Default kernel options, overidden by the kopt statement in the menufile.
  372. loop_file=$(awk '$2=="/" && $4~"loop" {print $1}' /etc/fstab)
  373. if [ -n "$loop_file" ]; then
  374. dev_mountpoint=$(awk '"'${loop_file}'"~"^"$2 && $2!="/" {print $1";"$2}' /proc/mounts|tail -n 1)
  375. host_device="${dev_mountpoint%;*}"
  376. host_mountpoint="${dev_mountpoint#*;}"
  377. fi
  378. if [ -n "$host_device" ]; then
  379. boot_device=
  380. root_device="$host_device"
  381. default_kopt="root=$host_device loop=${loop_file#$host_mountpoint} ro"
  382. else
  383. default_kopt="root=$root_device ro"
  384. fi
  385. default_kopt="$(convert_kopt_to_uuid "$default_kopt")"
  386. kopt="$default_kopt"
  387.  
  388. # Title
  389. title=$(lsb_release --short --description 2>/dev/null) || title="Ubuntu"
  390.  
  391. # should update-grub remember the default entry
  392. updatedefaultentry="false"
  393.  
  394. # Drive(in GRUB terms) where the kernel is located. Overridden by the
  395. # kopt statement in menufile.
  396. # if we don't have a device.map then we can't use the convert function.
  397.  
  398. # Try to use a UUID instead of the GRUB device name.
  399. if test -z "$boot_device" ; then
  400. uuid=$(convert_to_uuid "$root_device")
  401. else
  402. uuid=$(convert_to_uuid "$boot_device")
  403. fi
  404.  
  405. if [ -n "$uuid" ]; then
  406. grub_root_device="$uuid"
  407. fi
  408.  
  409. check_removable=""
  410. if test -z "$uuid"; then
  411. if test -f "$device_map"; then
  412. if test -z "$boot_device" ; then
  413. grub_root_device=$(convert_default "$root_device")
  414. check_removable="$(is_removable "$root_device")"
  415. else
  416. grub_root_device=$(convert_default "$boot_device")
  417. check_removable="$(is_removable "$boot_device")"
  418. fi
  419. else
  420. grub_root_device="(hd0,0)"
  421. fi
  422. fi
  423.  
  424. # If the root/boot device is on a removable target, we need to override
  425. # the grub_root_device to (hd0,X). This is a requirement since the BIOS
  426. # will change device mapping dynamically if we switch boot device.
  427.  
  428. if test -n "$check_removable" ; then
  429. grub_root_device="$(echo "$grub_root_device" | sed -e 's/d.*,/d0,/g')"
  430. fi
  431.  
  432. # should grub create the alternative boot options in the menu
  433. alternative="true"
  434.  
  435. # should grub lock the alternative boot options in the menu
  436. lockalternative="false"
  437.  
  438. # additional options to use with the default boot option, but not with the
  439. # alternatives
  440. defoptions="quiet splash"
  441.  
  442. # should grub lock the old kernels
  443. lockold="false"
  444.  
  445. # Xen hypervisor options to use with the default Xen boot option
  446. xenhopt=""
  447.  
  448. # Xen Linux kernel options to use with the default Xen boot option
  449. xenkopt="console=tty0"
  450.  
  451. # options to use with the alternative boot options
  452. altoptions="(recovery mode) single"
  453.  
  454. # controls howmany kernels are listed in the config file,
  455. # this does not include the alternative kernels
  456. howmany="all"
  457.  
  458. # should grub create a memtest86 entry
  459. memtest86="true"
  460.  
  461. # should grub add "savedefault" to default boot options
  462. savedefault="false"
  463.  
  464. # stores the command line arguments
  465. command_line_arguments=$1
  466.  
  467. # does this version of grub support the quiet option?
  468. if [ -f ${grub_dir}/installed-version ] && dpkg --compare-versions `cat ${grub_dir}/installed-version` ge 0.97-11ubuntu4; then
  469. supports_quiet=true
  470. else
  471. supports_quiet=false
  472. fi
  473.  
  474. # read user configuration
  475. if test -f "/etc/default/grub" ; then
  476. . /etc/default/grub
  477. fi
  478.  
  479. # Default options to use in a new config file. This will only be used if $menu_file
  480. # doesn't already exist. Only edit the lines between the two "EOF"s. The others are
  481. # part of the script.
  482. newtemplate=$(tempfile)
  483. cat >> "$newtemplate" <<EOF
  484. # $menu_file_basename - See: grub(8), info grub, update-grub(8)
  485. # grub-install(8), grub-floppy(8),
  486. # grub-md5-crypt, /usr/share/doc/grub
  487. # and /usr/share/doc/grub-doc/.
  488.  
  489. ## default num
  490. # Set the default entry to the entry number NUM. Numbering starts from 0, and
  491. # the entry number 0 is the default if the command is not used.
  492. #
  493. # You can specify 'saved' instead of a number. In this case, the default entry
  494. # is the entry saved with the command 'savedefault'.
  495. # WARNING: If you are using dmraid do not use 'savedefault' or your
  496. # array will desync and will not let you boot your system.
  497. default 0
  498.  
  499. ## timeout sec
  500. # Set a timeout, in SEC seconds, before automatically booting the default entry
  501. # (normally the first entry defined).
  502. timeout 3
  503.  
  504. ## hiddenmenu
  505. # Hides the menu by default (press ESC to see the menu)
  506. hiddenmenu
  507.  
  508. # Pretty colours
  509. #color cyan/blue white/blue
  510.  
  511. ## password ['--md5'] passwd
  512. # If used in the first section of a menu file, disable all interactive editing
  513. # control (menu entry editor and command-line) and entries protected by the
  514. # command 'lock'
  515. # e.g. password topsecret
  516. # password --md5 \$1\$gLhU0/\$aW78kHK1QfV3P2b2znUoe/
  517. # password topsecret
  518.  
  519. #
  520. # examples
  521. #
  522. # title Windows 95/98/NT/2000
  523. # root (hd0,0)
  524. # makeactive
  525. # chainloader +1
  526. #
  527. # title Linux
  528. # root (hd0,1)
  529. # kernel /vmlinuz root=/dev/hda2 ro
  530. #
  531.  
  532. #
  533. # Put static boot stanzas before and/or after AUTOMAGIC KERNEL LIST
  534.  
  535. EOF
  536. ## End Configuration Options
  537.  
  538. echo -n "Searching for default file ... " >&2
  539. if [ -f "$default_file" ] ; then
  540. echo "found: $default_file" >&2
  541. else
  542. echo "Generating $default_file file and setting the default boot entry to 0" >&2
  543. grub-set-default 0
  544. fi
  545.  
  546. # Make sure we use the standard sorting order
  547. LC_COLLATE=C
  548. # Magic markers we use
  549. start="### BEGIN AUTOMAGIC KERNELS LIST"
  550. end="### END DEBIAN AUTOMAGIC KERNELS LIST"
  551.  
  552. startopt="## ## Start Default Options ##"
  553. endopt="## ## End Default Options ##"
  554.  
  555. # path to grub2
  556. grub2name="/boot/grub/core.img"
  557.  
  558. # Extract options from config file
  559. ExtractMenuOpt()
  560. {
  561. opt=$1
  562.  
  563. sed -ne "/^$start\$/,/^$end\$/ {
  564. /^$startopt\$/,/^$endopt\$/ {
  565. /^# $opt=/ {
  566. s/^# $opt=\(.*\)\$/\1/
  567. p
  568. }
  569. }
  570. }" $menu
  571. }
  572.  
  573. GetMenuOpts()
  574. {
  575. opt=$1
  576.  
  577. sed -ne "/^$start\$/,/^$end\$/ {
  578. /^$startopt\$/,/^$endopt\$/ {
  579. /^# $opt=/ {
  580. p
  581. }
  582. }
  583. }" $menu
  584. }
  585.  
  586. ExtractMenuOpts()
  587. {
  588. opt=$1
  589.  
  590. GetMenuOpts $opt | sed "s/^# $opt=\(.*\)\$/\1=\"\2\"/"
  591. }
  592.  
  593. GetMenuOpt()
  594. {
  595. opt=$1
  596. value=$2
  597.  
  598. [ -z "$(GetMenuOpts "$opt")" ] || value=$(ExtractMenuOpt "$opt")
  599.  
  600. echo $value
  601. }
  602.  
  603. # Compares two version strings A and B
  604. # Returns -1 if A<B
  605. # 0 if A==B
  606. # 1 if A>B
  607. # This compares version numbers of the form
  608. # 2.4.14.2 > 2.4.14
  609. # 2.4.14random = 2.4.14-random > 2.4.14-ac10 > 2.4.14 > 2.4.14-pre2 >
  610. # 2.4.14-pre1 > 2.4.13-ac99
  611. CompareVersions()
  612. {
  613. #Changes the line something-x.y.z into somthing-x.y.z.q
  614. #This is to ensure that kernels with a .q is treated as higher than the ones without
  615. #First a space is put after the version number
  616. v1=$(echo $1 | sed -e 's!^\(.*-\([0-9]\+\.\)\{2,3\}[0-9]\+\)\(.*\)!\1 \3!g')
  617. v2=$(echo $2 | sed -e 's!^\(.*-\([0-9]\+\.\)\{2,3\}[0-9]\+\)\(.*\)!\1 \3!g')
  618. #If the version number only has 3 digits then put in another .0
  619. v1=$(echo $v1 | sed -e 's!^\(.*-\([0-9]\+\.\)\{2\}[0-9]\+\)\( .*\|$\)!\1.0 \3!g')
  620. v2=$(echo $v2 | sed -e 's!^\(.*-\([0-9]\+\.\)\{2\}[0-9]\+\)\( .*\|$\)!\1.0 \3!g')
  621.  
  622. # Then split the version number and remove any '.' 's or dashes
  623. v1=$(echo $v1 | sed -e 's![-\.]\+! !g' -e 's!\([0-9]\)\([[:alpha:]]\)!\1 \2!')
  624. v2=$(echo $v2 | sed -e 's![-\.]\+! !g' -e 's!\([0-9]\)\([[:alpha:]]\)!\1 \2!')
  625.  
  626. # we weight different kernel suffixes here
  627. # ac = 50
  628. # pre = -50
  629. # rc = -40
  630. # test = -60
  631. # others are given 99
  632. v1=$(echo $v1 | sed -e 's! k7! 786 !g' -e 's! ac! 50 !g' -e 's! rc! -40 !g' -e 's! pre! -50 !g' -e 's! test! -60 !g' -e 's![^ ]*[^-0-9 ][^ ]*!99!g')
  633.  
  634. v2=$(echo $v2 | sed -e 's! k7! 786 !g' -e 's! ac! 50 !g' -e 's! rc! -40 !g' -e 's! pre! -50 !g' -e 's! test! -60 !g' -e 's![^ ]*[^-0-9 ][^ ]*!99!g')
  635.  
  636. result=0; v1finished=0; v2finished=0;
  637. while [ $result -eq 0 ] && [ $v1finished -eq 0 ] && [ $v2finished -eq 0 ];
  638. do
  639. if [ "$v1" = "" ]; then
  640. v1comp=0; v1finished=1
  641. else
  642. set -- $v1; v1comp=$1; shift; v1=$*
  643. fi
  644.  
  645. if [ "$v2" = "" ]; then
  646. v2comp=0; v2finished=1
  647. else
  648. set -- $v2; v2comp=$1; shift; v2=$*
  649. fi
  650.  
  651. set +e
  652. result=`expr $v1comp - $v2comp`
  653. result=`expr substr $result 1 2`
  654. set -e
  655.  
  656. if [ $result -gt 0 ]; then result=1
  657. elif [ $result -lt 0 ]; then result=-1
  658. fi
  659. done
  660.  
  661. # finally return the result
  662. echo $result
  663. }
  664.  
  665. # looks in the directory specified for an initrd image with the version specified
  666. FindInitrdName()
  667. {
  668. # strip trailing slashes
  669. directory=$(echo $1 | sed -e 's#/*$##')
  670. version=$2
  671.  
  672. # initrd
  673. # initrd.img
  674. # initrd-lvm
  675. # .*.gz
  676.  
  677. initrdName=""
  678. names="initrd initrd.img initrd-lvm"
  679. compressed="gz"
  680.  
  681. for n in $names ; do
  682. # make sure we haven't already found it
  683. if [ -z "$initrdName" ] ; then
  684. if [ -f "$directory/$n$version" ] ; then
  685. initrdName="$n$version"
  686. break
  687. else
  688. for c in $compressed ; do
  689. if [ -f "$directory/$n$version.$c" ] ; then
  690. initrdName="$n$version.$c"
  691. break
  692. fi
  693. done
  694. fi
  695. else
  696. break
  697. fi
  698. done
  699.  
  700. # return the result
  701. echo $initrdName
  702. }
  703.  
  704. FindXenHypervisorVersions ()
  705. {
  706. version=$1
  707.  
  708. if [ -f "/var/lib/linux-image-$version/xen-versions" ]; then
  709. ret="$(cat /var/lib/linux-image-$version/xen-versions)"
  710. fi
  711.  
  712. echo $ret
  713. }
  714.  
  715. get_kernel_opt()
  716. {
  717. kernel_version=$1
  718.  
  719. version=$(echo $kernel_version | sed 's/^[^0-9]*//')
  720. version=$(echo $version | sed 's/[-\+\.]/_/g')
  721. if [ -n "$version" ] ; then
  722. while [ -n "$version" ] ; do
  723. currentOpt="$(eval "echo \${kopt_$version}")"
  724. if [ -n "$currentOpt" ] ; then
  725. break
  726. fi
  727.  
  728. oldversion="$version"
  729. version=$(echo $version | sed 's/_\?[^_]*$//')
  730. if [ "$version" = "$oldversion" ] ; then
  731. # Break infinite loop, if the version isn't what we expect
  732. break
  733. fi
  734. done
  735. fi
  736.  
  737. if [ -z "$currentOpt" ] ; then
  738. currentOpt=$kopt
  739. fi
  740.  
  741. echo $currentOpt
  742. }
  743.  
  744. write_kernel_entry()
  745. {
  746. local kernel_version; kernel_version=$1; shift
  747. local recovery_desc; recovery_desc=$1; shift
  748. local lock_alternative; lock_alternative=$1; shift
  749. local grub_root_device; grub_root_device=$1; shift
  750. local kernel; kernel=$1; shift
  751. local kernel_options; kernel_options=$1; shift
  752. local recovery_suffix; recovery_suffix=$1; shift
  753. local initrd; initrd=$1; shift
  754. local savedefault; savedefault=$1; shift
  755. local lockold; lockold=$1; shift
  756. local dapper_upgrade; dapper_upgrade=$1; shift
  757. local hypervisor
  758. if [ -n "$1" ]; then
  759. # Hypervisor.
  760. hypervisor=$1; shift
  761. local hypervisor_image; hypervisor_image=$1; shift
  762. local hypervisor_version; hypervisor_version=$1; shift
  763. local hypervisor_options; hypervisor_options=$1; shift
  764. fi
  765.  
  766. echo -n "title " >> $buffer
  767.  
  768. if [ -n "$hypervisor" ]; then
  769. echo -n "$hypervisor $hypervisor_version / " >> $buffer
  770. fi
  771.  
  772. echo -n "$title" >> $buffer
  773. if [ -n "$kernel_version" ]; then
  774. echo -n ", " >> $buffer
  775. # memtest86 is not strictly a kernel
  776. if ! echo "$kernel_version" | grep -q ^memtest86; then
  777. echo -n "kernel " >> $buffer
  778. fi
  779. echo -n "$kernel_version" >> $buffer
  780. fi
  781. if [ -n "$recovery_desc" ]; then
  782. echo -n " $recovery_desc" >> $buffer
  783. fi
  784. echo >> $buffer
  785.  
  786. # lock the alternative options
  787. if test x"$lock_alternative" = x"true" ; then
  788. echo "lock" >> $buffer
  789. fi
  790. # lock the old entries
  791. if test x"$lockold" = x"true" ; then
  792. echo "lock" >> $buffer
  793. fi
  794.  
  795. case "$grub_root_device" in
  796. [^A-Za-z0-9]*)
  797. echo "root $grub_root_device" >> $buffer
  798. ;;
  799. *)
  800. echo "uuid $grub_root_device" >> $buffer
  801. ;;
  802. esac
  803.  
  804. echo -n "kernel " >> $buffer
  805. if [ -n "$hypervisor" ]; then
  806. echo -n "$hypervisor_image" >> $buffer
  807. if [ -n "$hypervisor_options" ]; then
  808. echo -n " $hypervisor_options" >> $buffer
  809. fi
  810. echo >> $buffer
  811. echo -n "module " >> $buffer
  812. fi
  813. echo -n "$kernel" >> $buffer
  814. if [ -n "$kernel_options" ]; then
  815. echo -n " $kernel_options" >> $buffer
  816. fi
  817. if [ -n "$recovery_desc" ]; then
  818. echo -n " $recovery_suffix" >> $buffer
  819. fi
  820. if [ -n "$dapper_upgrade" -a -z "$kernel_options$recovery_desc" ]; then
  821. echo -n " " >> $buffer
  822. fi
  823. echo >> $buffer
  824.  
  825. if [ -n "$initrd" ]; then
  826. if [ -n "$hypervisor" ]; then
  827. echo -n "module " >> $buffer
  828. else
  829. echo -n "initrd " >> $buffer
  830. fi
  831. echo "$initrd" >> $buffer
  832. fi
  833.  
  834. if [ ! -n "$recovery_desc" -a x"$supports_quiet" = x"true" -a -z "$dapper_upgrade" ]; then
  835. echo "quiet" >> $buffer
  836. fi
  837.  
  838. if test x"$savedefault" = x"true" ; then
  839. echo "savedefault" >> $buffer
  840. fi
  841. if test x"$dapper_upgrade" != x ; then
  842. echo "boot" >> $buffer
  843. fi
  844. echo >> $buffer
  845. }
  846.  
  847. ## write out the kernel entries
  848. output_kernel_list() {
  849. counter=0
  850.  
  851. # Xen entries first.
  852. for kern in $xenKernels ; do
  853. if test ! x"$howmany" = x"all" ; then
  854. if [ $counter -gt $howmany ] ; then
  855. break
  856. fi
  857. fi
  858.  
  859. kernelName=$(basename $kern)
  860. kernelVersion=$(echo $kernelName | sed -e 's/vmlinuz//')
  861.  
  862. initrdName=$(FindInitrdName "/boot" "$kernelVersion")
  863. initrd=""
  864.  
  865. kernel=$kernel_dir/$kernelName
  866. if [ -n "$initrdName" ] ; then
  867. initrd=$kernel_dir/$initrdName
  868. fi
  869.  
  870. kernelVersion=$(echo $kernelVersion | sed -e 's/^-//')
  871. currentOpt=$(get_kernel_opt $kernelVersion)
  872.  
  873. hypervisorVersions=$(FindXenHypervisorVersions "$kernelVersion")
  874.  
  875. found=
  876. for hypervisorVersion in $hypervisorVersions; do
  877. hypervisor="$kernel_dir/xen-$hypervisorVersion.gz"
  878. if [ -e "$hypervisor" ]; then
  879. found=1
  880.  
  881. echo "Found Xen hypervisor $hypervisorVersion, kernel: $kernel" >&2
  882.  
  883. write_kernel_entry "$kernelVersion" '' '' "$grub_root_device" \
  884. "$kernel" "$currentOpt $xenkopt" '' "$initrd" "$savedefault" '' "$dapper_upgrade" \
  885. Xen "$hypervisor" "$hypervisorVersion" "$xenhopt"
  886. counter=$(($counter + 1))
  887. fi
  888. done
  889.  
  890. if [ -z $found ]; then
  891. for hypervisor in $hypervisors; do
  892. hypVersion=`basename "$hypervisor" .gz | sed s%xen-%%`
  893.  
  894. echo "Found Xen hypervisor $hypVersion, kernel: $kernel" >&2
  895.  
  896. write_kernel_entry "$kernelVersion" '' '' "$grub_root_device" \
  897. "$kernel" "$currentOpt $xenkopt" '' "$initrd" "$savedefault" '' "$dapper_upgrade" \
  898. Xen "$kernel_dir/$hypervisor" "$hypVersion" "$xenhopt"
  899. counter=$(($counter + 1))
  900. done
  901. fi
  902. done
  903.  
  904. for kern in $sortedKernels ; do
  905. counter=$(($counter + 1))
  906. if test ! x"$howmany" = x"all" ; then
  907. if [ $counter -gt $howmany ] ; then
  908. break
  909. fi
  910. fi
  911. kernelName=$(basename $kern)
  912. initrdName=""
  913. initrd=""
  914. extra_opts=""
  915.  
  916. if [ "$kern" = "/boot/last-good-boot/vmlinuz" ]; then
  917. kernelVersion="Last successful boot"
  918. if [ -e "/boot/last-good-boot/initrd.img" ]; then
  919. initrdName="last-good-boot/initrd.img"
  920. fi
  921. kernelName="last-good-boot/vmlinuz"
  922. extra_opts="$extra_opts last-good-boot"
  923. else
  924. kernelVersion=$(echo $kernelName | sed -e 's/vmlinuz//')
  925. initrdName=$(FindInitrdName "/boot" "$kernelVersion")
  926. if [ -x "/usr/bin/makedumpfile" ] && [ -x "/sbin/kexec" ]; then
  927. extra_opts="$extra_opts crashkernel=384M-2G:64M@16M,2G-:128M@16M"
  928. fi
  929. fi
  930.  
  931. kernel=$kernel_dir/$kernelName
  932.  
  933. if [ -n "$initrdName" ] ; then
  934. initrd=$kernel_dir/$initrdName
  935. fi
  936.  
  937. echo "Found kernel: $kernel" >&2
  938.  
  939. if [ "$kernelName" = "vmlinuz" ]; then
  940. if [ -L "/boot/$kernelName" ]; then
  941. kernelVersion=`readlink -f "/boot/$kernelName"`
  942. kernelVersion=$(echo $kernelVersion | sed -e 's/.*vmlinuz-//')
  943. kernelVersion="$kernelVersion Default"
  944. else
  945. kernelVersion="Default"
  946. fi
  947. fi
  948. if [ "$kernelName" = "vmlinuz.old" ]; then
  949. if [ -L "/boot/$kernelName" ]; then
  950. kernelVersion=`readlink -f "/boot/$kernelName"`
  951. kernelVersion=$(echo $kernelVersion | sed -e 's/.*vmlinuz-//')
  952. kernelVersion="$kernelVersion Previous"
  953. else
  954. kernelVersion="Previous"
  955. fi
  956. fi
  957.  
  958. kernelVersion=$(echo $kernelVersion | sed -e 's/^-//')
  959.  
  960. currentOpt=$(get_kernel_opt $kernelVersion)
  961.  
  962. do_lockold=$lockold
  963. # do not lockold for the first entry
  964. [ $counter -eq 1 ] && do_lockold=false
  965.  
  966. if [ "$kernelName" = "last-good-boot/vmlinuz" ]; then
  967. if [ -e /boot/last-good-boot/cmdline ]; then
  968. cmdline="$(cat /boot/last-good-boot/cmdline) last-good-boot"
  969. else
  970. cmdline="$currentOpt $defoptions $extra_opts"
  971. fi
  972. write_kernel_entry "$kernelVersion" "" "" "$grub_root_device" "$kernel" \
  973. "$cmdline" "" "$initrd" "$savedefault" "$do_lockold" \
  974. "$dapper_upgrade"
  975. else
  976. write_kernel_entry "$kernelVersion" "" "" "$grub_root_device" "$kernel" \
  977. "$currentOpt $defoptions $extra_opts" "" "$initrd" "$savedefault" \
  978. "$do_lockold" "$dapper_upgrade"
  979. fi
  980.  
  981. # insert the alternative boot options
  982. if test ! x"$alternative" = x"false" && \
  983. test ! x"$kernelName" = x"last-good-boot/vmlinuz"; then
  984. # for each altoptions line do this stuff
  985. sed -ne 's/# altoptions=\(.*\)/\1/p' $buffer | while read line; do
  986. descr=$(echo $line | sed -ne 's/\(([^)]*)\)[[:space:]]\(.*\)/\1/p')
  987. suffix=$(echo $line | sed -ne 's/\(([^)]*)\)[[:space:]]\(.*\)/\2/p')
  988.  
  989. test x"$lockalternative" = x"true" && do_lockold=false
  990. write_kernel_entry "$kernelVersion" "$descr" "$lockalternative" \
  991. "$grub_root_device" "$kernel" "$currentOpt $extra_opts" \
  992. "$suffix" "$initrd" "false" "$do_lockold" \
  993. "$dapper_upgrade"
  994. done
  995. fi
  996. done
  997.  
  998. if test -f $grub2name ; then
  999. echo "Found GRUB 2: $grub2name" >&2
  1000. cat >> $buffer << EOF
  1001. title Chainload into GRUB 2
  1002. root $grub_root_device
  1003. kernel $grub2name
  1004. EOF
  1005. if test x"$savedefault" = x"true" ; then
  1006. echo "savedefault" >> $buffer
  1007. fi
  1008. echo >> $buffer
  1009. fi
  1010.  
  1011. memtest86names="memtest86 memtest86+"
  1012.  
  1013. if test ! x"$memtest86" = x"false" ; then
  1014. for name in $memtest86names ; do
  1015. if test -f "/boot/$name.bin" ; then
  1016. kernelVersion="$name"
  1017. kernel="$kernel_dir/$name.bin"
  1018. currentOpt=
  1019. initrd=
  1020.  
  1021. echo "Found kernel: $kernel" >&2
  1022.  
  1023. write_kernel_entry "$kernelVersion" "" "" "$grub_root_device" \
  1024. "$kernel" "$currentOpt" "" "$initrd" "false" "" "$dapper_upgrade"
  1025. fi
  1026. done
  1027. fi
  1028.  
  1029. echo $end >> $buffer
  1030. }
  1031.  
  1032. ucf_update_kernels() {
  1033. local target; target="$1"
  1034. local buffer; buffer="$2"
  1035.  
  1036. sed -ni -e"/$endopt/,/$end/p" "$buffer"
  1037.  
  1038. if [ "x$initialconfig" = "x" ]; then
  1039. sed -n -e"/$endopt/,/$end/p" < $menu > $ucf_menu_file
  1040. else
  1041. cat $buffer > $ucf_menu_file
  1042. fi
  1043.  
  1044. db_x_loadtemplatefile /var/lib/dpkg/info/grub.templates grub
  1045.  
  1046. ucf --debconf-ok \
  1047. --debconf-template grub/update_grub_changeprompt_threeway \
  1048. --three-way "$buffer" $ucf_menu_file
  1049. rm "$buffer"
  1050.  
  1051. # now re-merge the ucf results with the target file
  1052. sed -i -e "/^$endopt/,/^$end/ {
  1053. /^$endopt/r $ucf_menu_file
  1054. d
  1055. }
  1056. " $target
  1057.  
  1058. rm -f $ucf_menu_file ${ucf_menu_file}.ucf-old
  1059. }
  1060.  
  1061.  
  1062. echo -n "Testing for an existing GRUB $menu_file_basename file ... " >&2
  1063.  
  1064. # Test if our menu file exists
  1065. if [ -f "$menu_file" ] ; then
  1066. menu="$menu_file"
  1067. rm -f $newtemplate
  1068. unset newtemplate
  1069. echo "found: $menu_file" >&2
  1070. cp -f "$menu_file" "$menu_file~"
  1071. else
  1072. # if not ask user if they want us to create one
  1073. initialconfig=1
  1074. menu="$menu_file"
  1075. echo >&2
  1076. echo >&2
  1077. echo -n "Could not find $menu_file file. " >&2
  1078. if [ "-y" = "$command_line_arguments" ] ; then
  1079. echo >&2
  1080. echo "Generating $menu_file" >&2
  1081. answer=y
  1082. else
  1083. echo -n "Would you like $menu_file generated for you? " >&2
  1084. echo -n "(y/N) " >&2
  1085. read answer <&2
  1086. fi
  1087.  
  1088. case "$answer" in
  1089. y* | Y*)
  1090. cat "$newtemplate" > $menu_file
  1091. rm -f $newtemplate
  1092. unset newtemplate
  1093. ;;
  1094. *)
  1095. abort "Not creating $menu_file as you wish"
  1096. ;;
  1097. esac
  1098. fi
  1099.  
  1100. # Extract the kernel options to use
  1101. kopt=$(GetMenuOpt "kopt" "$kopt")
  1102.  
  1103. # Extract options for specific kernels
  1104. opts="$(ExtractMenuOpts "\(kopt_[[:alnum:]_]\+\)")"
  1105. test -z "$opts" || eval "$opts"
  1106. CustomKopts=$(GetMenuOpts "\(kopt_[[:alnum:]_]\+\)" | \
  1107. grep -v "^# kopt_2_6=" || true)
  1108.  
  1109. # Set the kernel 2.6 option only for fresh install (but convert it to
  1110. # mount-by-UUID on upgrade)
  1111. test -z "$kopt_2_6" && test -z "$(GetMenuOpt "kopt" "")" && \
  1112. kopt_2_6="$default_kopt"
  1113.  
  1114. # Extract the grub root
  1115. grub_root_device=$(GetMenuOpt "groot" "$grub_root_device")
  1116.  
  1117. # Extract the old recovery value
  1118. alternative=$(GetMenuOpt "recovery" "$alternative")
  1119.  
  1120. # Extract the alternative value
  1121. alternative=$(GetMenuOpt "alternative" "$alternative")
  1122.  
  1123. # Extract the lockalternative value
  1124. lockalternative=$(GetMenuOpt "lockalternative" "$lockalternative")
  1125.  
  1126. # Extract the additional default options
  1127. # Check nonaltoptions too for compatibility with Ubuntu <= 5.10
  1128. defoptions=$(GetMenuOpt "nonaltoptions" "$defoptions")
  1129. defoptions=$(GetMenuOpt "defoptions" "$defoptions")
  1130.  
  1131. # Extract the lockold value
  1132. lockold=$(GetMenuOpt "lockold" "$lockold")
  1133.  
  1134. # Extract Xen hypervisor options
  1135. xenhopt=$(GetMenuOpt "xenhopt" "$xenhopt")
  1136.  
  1137. # Extract Xen Linux kernel options
  1138. xenkopt=$(GetMenuOpt "xenkopt" "$xenkopt")
  1139.  
  1140. # Extract the howmany value
  1141. howmany=$(GetMenuOpt "howmany" "$howmany")
  1142.  
  1143. # Extract the memtest86 value
  1144. memtest86=$(GetMenuOpt "memtest86" "$memtest86")
  1145.  
  1146.  
  1147. # Extract the updatedefaultentry option
  1148. updatedefaultentry=$(GetMenuOpt "updatedefaultentry" "$updatedefaultentry")
  1149.  
  1150. # If "default saved" is in use, set the default to true
  1151. grep -q "^default.*saved" $menu && savedefault=true
  1152. # Extract the savedefault option
  1153. savedefault=$(GetMenuOpt "savedefault" "$savedefault")
  1154.  
  1155. # Generate the menu options we want to insert
  1156. buffer=$(tempfile)
  1157. echo $start >> $buffer
  1158. echo "## lines between the AUTOMAGIC KERNELS LIST markers will be modified" >> $buffer
  1159. echo "## by the debian update-grub script except for the default options below" >> $buffer
  1160. echo >> $buffer
  1161. echo "## DO NOT UNCOMMENT THEM, Just edit them to your needs" >> $buffer
  1162. echo >> $buffer
  1163. echo "## ## Start Default Options ##" >> $buffer
  1164.  
  1165. echo "## default kernel options" >> $buffer
  1166. echo "## default kernel options for automagic boot options" >> $buffer
  1167. echo "## If you want special options for specific kernels use kopt_x_y_z" >> $buffer
  1168. echo "## where x.y.z is kernel version. Minor versions can be omitted." >> $buffer
  1169. echo "## e.g. kopt=root=/dev/hda1 ro" >> $buffer
  1170. echo "## kopt_2_6_8=root=/dev/hdc1 ro" >> $buffer
  1171. echo "## kopt_2_6_8_2_686=root=/dev/hdc2 ro" >> $buffer
  1172. echo "# kopt=$kopt" >> $buffer
  1173. if [ -n "$kopt_2_6" ] && [ "$kopt" != "$kopt_2_6" ]; then
  1174. echo "# kopt_2_6=$kopt_2_6" >> $buffer
  1175. fi
  1176. if [ -n "$CustomKopts" ] ; then
  1177. echo "$CustomKopts" >> $buffer
  1178. fi
  1179. echo >> $buffer
  1180.  
  1181. echo "## default grub root device" >> $buffer
  1182. echo "## e.g. groot=(hd0,0)" >> $buffer
  1183. echo "# groot=$grub_root_device" >> $buffer
  1184. echo >> $buffer
  1185.  
  1186. echo "## should update-grub create alternative automagic boot options" >> $buffer
  1187. echo "## e.g. alternative=true" >> $buffer
  1188. echo "## alternative=false" >> $buffer
  1189. echo "# alternative=$alternative" >> $buffer
  1190. echo >> $buffer
  1191.  
  1192. echo "## should update-grub lock alternative automagic boot options" >> $buffer
  1193. echo "## e.g. lockalternative=true" >> $buffer
  1194. echo "## lockalternative=false" >> $buffer
  1195. echo "# lockalternative=$lockalternative" >> $buffer
  1196. echo >> $buffer
  1197.  
  1198. echo "## additional options to use with the default boot option, but not with the" >> $buffer
  1199. echo "## alternatives" >> $buffer
  1200. echo "## e.g. defoptions=vga=791 resume=/dev/hda5" >> $buffer
  1201. echo "# defoptions=$defoptions" >> $buffer
  1202. echo >> $buffer
  1203.  
  1204. echo "## should update-grub lock old automagic boot options" >> $buffer
  1205. echo "## e.g. lockold=false" >> $buffer
  1206. echo "## lockold=true" >> $buffer
  1207. echo "# lockold=$lockold" >> $buffer
  1208. echo >> $buffer
  1209.  
  1210. echo "## Xen hypervisor options to use with the default Xen boot option" >> $buffer
  1211. echo "# xenhopt=$xenhopt" >> $buffer
  1212. echo >> $buffer
  1213.  
  1214. echo "## Xen Linux kernel options to use with the default Xen boot option" >> $buffer
  1215. echo "# xenkopt=$xenkopt" >> $buffer
  1216. echo >> $buffer
  1217.  
  1218. echo "## altoption boot targets option" >> $buffer
  1219. echo "## multiple altoptions lines are allowed" >> $buffer
  1220. echo "## e.g. altoptions=(extra menu suffix) extra boot options" >> $buffer
  1221. echo "## altoptions=(recovery) single" >> $buffer
  1222.  
  1223. if ! grep -q "^# altoptions" $menu ; then
  1224. echo "# altoptions=$altoptions" >> $buffer
  1225. else
  1226. grep "^# altoptions" $menu >> $buffer
  1227. fi
  1228. echo >> $buffer
  1229.  
  1230. echo "## controls how many kernels should be put into the $menu_file_basename" >> $buffer
  1231. echo "## only counts the first occurence of a kernel, not the" >> $buffer
  1232. echo "## alternative kernel options" >> $buffer
  1233. echo "## e.g. howmany=all" >> $buffer
  1234. echo "## howmany=7" >> $buffer
  1235. echo "# howmany=$howmany" >> $buffer
  1236. echo >> $buffer
  1237.  
  1238.  
  1239. echo "## should update-grub create memtest86 boot option" >> $buffer
  1240. echo "## e.g. memtest86=true" >> $buffer
  1241. echo "## memtest86=false" >> $buffer
  1242. echo "# memtest86=$memtest86" >> $buffer
  1243. echo >> $buffer
  1244.  
  1245. echo "## should update-grub adjust the value of the default booted system" >> $buffer
  1246. echo "## can be true or false" >> $buffer
  1247. echo "# updatedefaultentry=$updatedefaultentry" >> $buffer
  1248. echo >> $buffer
  1249.  
  1250. echo "## should update-grub add savedefault to the default options" >> $buffer
  1251. echo "## can be true or false" >> $buffer
  1252. echo "# savedefault=$savedefault" >> $buffer
  1253. echo >> $buffer
  1254.  
  1255. echo "## ## End Default Options ##" >> $buffer
  1256. echo >> $buffer
  1257.  
  1258. echo -n "Searching for splash image ... " >&2
  1259. current_splash=`grep '^splashimage=' ${menu_file} || true`
  1260. splashimage_path="splashimage=${grub_root_device}${grub_dir##${boot_device:+/boot}}/splash.xpm.gz"
  1261. if [ `sed -e "/^$start/,/^$end/d" $menu_file | grep -c '^splashimage='` != "0" ] ; then
  1262. #checks for splashscreen defined outside the autoupdated part
  1263. splashimage=$(grep '^splashimage=' ${menu_file})
  1264. echo "found: ${splashimage##*=}" >&2
  1265. echo >&2
  1266. elif [ -f "${grub_dir}/splash.xpm.gz" ] && [ "$current_splash" = "" ]; then
  1267. echo "found: /boot/grub/splash.xpm.gz" >&2
  1268. echo "$splashimage_path" >> $buffer
  1269. echo >> $buffer
  1270. elif [ -f "${grub_dir}/splash.xpm.gz" ] && [ "$current_splash" = "$splashimage_path" ]; then
  1271. echo "found: /boot/grub/splash.xpm.gz" >&2
  1272. echo "$splashimage_path" >> $buffer
  1273. echo >> $buffer
  1274. elif [ "$current_splash" != "" ] && [ "$current_splash" != "$splashimage_path" ]; then
  1275. echo "found but preserving previous setting: $(grep '^splashimage=' ${menu_file})" >&2
  1276. echo "$current_splash" >> $buffer
  1277. echo >> $buffer
  1278. else
  1279. echo "none found, skipping ..." >&2
  1280. fi
  1281.  
  1282.  
  1283. hypervisors=""
  1284. for hyp in /boot/xen-*.gz; do
  1285. if [ ! -h "$hyp" ] && [ -f "$hyp" ]; then
  1286. hypervisors="$hypervisors `basename "$hyp"`"
  1287. fi
  1288. done
  1289.  
  1290. # figure out where grub looks for the kernels at boot time
  1291. kernel_dir=/boot
  1292. if [ -n "$boot_device" ] ; then
  1293. kernel_dir=
  1294. fi
  1295.  
  1296.  
  1297. # We need a static path to use for the ucf registration; since we're not
  1298. # using the full menu.lst file (maybe we should, just copying it around?
  1299. # C.f. discussion with Manoj), create a directory in a fixed location
  1300. # even though we're not treating the file in that location as
  1301. # persistent.
  1302. mkdir -p /var/run/grub
  1303.  
  1304. # The first time ucf sees the file, we can only assume any difference
  1305. # between the magic comments and the kernel options is a result of local
  1306. # mods, so this will result in a ucf prompt for anyone whose first
  1307. # invocation of update-grub is as a result of updating the magic comments.
  1308. if ! ucfq grub | grep -q $ucf_menu_file; then
  1309. otherbuffer=$(tempfile)
  1310. cat $buffer > $otherbuffer
  1311.  
  1312. sortedKernels=`sed -n -e "
  1313. /$endopt/,/$end/ {
  1314. s/^kernel[[:space:]]\+\([^[:space:]]\+\).*/\1/p
  1315. }" < $menu | grep -vE "memtest86|$grub2name|xen" | uniq`
  1316. xenKernels=`sed -n -e "
  1317. /$endopt/,/$end/ {
  1318. s/^module[[:space:]]\+\([^[:space:]]*vmlinuz[^[:space:]]\+\).*/\1/p
  1319. }" < $menu | uniq`
  1320.  
  1321. savebuffer="$buffer"
  1322. buffer="$otherbuffer"
  1323. savetitle="$title"
  1324. title="$(sed -n -e "/$endopt/,/$end/ {
  1325. s/^title[[:space:]]\+\(.*\),.*/\1/p
  1326. }" < $menu | head -n 1)"
  1327. if [ -z "$title" ]; then
  1328. title="$savetitle"
  1329. fi
  1330.  
  1331. # Hack: the kernel list output in Ubuntu 6.06 was different than
  1332. # in the current version, so to support smooth upgrades we need to
  1333. # properly detect a config generated by this old version of
  1334. # update-grub and mimic it for the initial ucf registration
  1335. dapper_upgrade=`sed -n -e "
  1336. /$endopt/,/$end/ {
  1337. /^boot/p
  1338. }" < $menu`
  1339. save_savedefault="$savedefault"
  1340. if [ -n "$dapper_upgrade" ]; then
  1341. savedefault=true
  1342. fi
  1343.  
  1344. output_kernel_list
  1345.  
  1346. savedefault="$save_savedefault"
  1347. dapper_upgrade=""
  1348. buffer="$savebuffer"
  1349. title="$savetitle"
  1350.  
  1351. ucf_update_kernels "$menu" "$otherbuffer"
  1352.  
  1353. # all done, now register it
  1354. ucfr grub $ucf_menu_file
  1355. fi
  1356.  
  1357.  
  1358. xenKernels=""
  1359. for ver in `grep -l CONFIG_XEN_PRIVILEGED_GUEST=y /boot/config* | sed -e s%/boot/config-%%`; do
  1360. # ver is a kernel version
  1361. kern="/boot/vmlinuz-$ver"
  1362. if [ -r $kern ] ; then
  1363. newerKernels=""
  1364. for i in $xenKernels ; do
  1365. res=$(CompareVersions "$kern" "$i")
  1366. if [ "$kern" != "" ] && [ "$res" -gt 0 ] ; then
  1367. newerKernels="$newerKernels $kern $i"
  1368. kern=""
  1369. else
  1370. newerKernels="$newerKernels $i"
  1371. fi
  1372. done
  1373. if [ "$kern" != "" ] ; then
  1374. newerKernels="$newerKernels $kern"
  1375. fi
  1376. xenKernels="$newerKernels"
  1377. fi
  1378. done
  1379.  
  1380. in_domU=
  1381. if [ -e /proc/xen/capabilities ] && ! grep -q "control_d" /proc/xen/capabilities; then
  1382. in_domU=1
  1383. fi
  1384.  
  1385. sortedKernels=""
  1386. for kern in $(/bin/ls -1vr /boot | grep -v "dpkg-*" | grep "^vmlinuz-") ; do
  1387. if `echo "$xenKernels" | grep -q "$kern "` || `echo "$kern" | grep -q "xen"`; then
  1388. is_xen=1
  1389. else
  1390. is_xen=
  1391. fi
  1392.  
  1393. if [ ! "$in_domU" ] && [ "$is_xen" ]; then
  1394. # skip xen kernels
  1395. continue
  1396. elif [ "$in_domU" ] && ! [ "$is_xen" ]; then
  1397. # skip non-xen kernels
  1398. continue
  1399. fi
  1400. kern="/boot/$kern"
  1401. newerKernels=""
  1402. for i in $sortedKernels ; do
  1403. res=$(CompareVersions "$kern" "$i")
  1404. if [ "$kern" != "" ] && [ "$res" -gt 0 ] ; then
  1405. newerKernels="$newerKernels $kern $i"
  1406. kern=""
  1407. else
  1408. newerKernels="$newerKernels $i"
  1409. fi
  1410. done
  1411. if [ "$kern" != "" ] ; then
  1412. newerKernels="$newerKernels $kern"
  1413. fi
  1414. sortedKernels="$newerKernels"
  1415. done
  1416.  
  1417. if test -f "/boot/vmlinuz.old" ; then
  1418. sortedKernels="/boot/vmlinuz.old $sortedKernels"
  1419. fi
  1420. if test -f "/boot/vmlinuz" ; then
  1421. sortedKernels="/boot/vmlinuz $sortedKernels"
  1422. fi
  1423.  
  1424. # Add our last-good-boot kernel, second in list. We always add it, because
  1425. # it can appear out of nowhere.
  1426. newerKernels=""
  1427. last_good="/boot/last-good-boot/vmlinuz"
  1428. if [ -e "$last_good" ]; then
  1429. for i in $sortedKernels ; do
  1430. if [ "$last_good" != "" ]; then
  1431. newerKernels="$i $last_good"
  1432. last_good=""
  1433. else
  1434. newerKernels="$newerKernels $i"
  1435. fi
  1436. done
  1437. # Shouldn't happen, unless someone removed all the kernels
  1438. if [ "$last_good" != "" ]; then
  1439. newerKernels="$newerKernels $last_good"
  1440. fi
  1441. sortedKernels="$newerKernels"
  1442. fi
  1443.  
  1444. #Finding the value the default line
  1445. use_grub_set_default="false"
  1446. if test "$updatedefaultentry" = "true" ; then
  1447. defaultEntryNumber=$(sed -ne 's/^[[:blank:]]*default[[:blank:]]*\(.*\).*/\1/p' $menu)
  1448.  
  1449. if [ "$defaultEntryNumber" = "saved" ] ; then
  1450. defaultEntryNumber=$(sed 'q' "$grub_dir/default")
  1451. use_grub_set_default="true"
  1452. fi
  1453.  
  1454. if test -n "$defaultEntryNumber"; then
  1455. defaultEntryNumberPlusOne=$(expr $defaultEntryNumber \+ 1);
  1456. defaultEntry=$(grep "^[[:blank:]]*title" $menu | sed -ne "${defaultEntryNumberPlusOne}p" | sed -ne ";s/^[[:blank:]]*title[[:blank:]]*//p")
  1457. defaultEntry=$(echo $defaultEntry | sed -e "s/[[:blank:]]*$//") # don't trust trailing blanks
  1458. else
  1459. notChangeDefault="yes"
  1460. fi
  1461. else
  1462. notChangeDefault="yes"
  1463. fi
  1464.  
  1465. output_kernel_list
  1466.  
  1467. otherbuffer=$(tempfile)
  1468. cat $buffer > $otherbuffer
  1469.  
  1470. ucf_update_kernels "$buffer" "$otherbuffer"
  1471.  
  1472. echo -n "Updating $menu ... " >&2
  1473. # Insert the new options into the menu
  1474. if ! grep -q "^$start" $menu ; then
  1475. cat $buffer >> $menu
  1476. rm -f $buffer
  1477. else
  1478. umask 077
  1479. sed -e "/^$start/,/^$end/{
  1480. /^$start/r $buffer
  1481. d
  1482. }
  1483. " $menu > $menu.new
  1484. cat $menu.new > $menu
  1485. rm -f $buffer $menu.new
  1486. fi
  1487.  
  1488. # Function to update the default value
  1489. set_default_value() {
  1490. if [ "$use_grub_set_default" = "true" ] ; then
  1491. grub-set-default $1
  1492. else
  1493. value="$1"
  1494. newmenu=$(tempfile)
  1495. sed -e "s/^[[:blank:]]*default[[:blank:]]*[[:digit:]]*\(.*\)/default ${value}\1/;b" $menu > $newmenu
  1496. cat $newmenu > $menu
  1497. rm -f $newmenu
  1498. unset newmenu
  1499. fi
  1500. }
  1501.  
  1502. #Updating the default number
  1503. if test -z "$notChangeDefault"; then
  1504. newDefaultNumberPlusOne=$(grep "^[[:blank:]]*title[[:blank:]]*" $menu | grep -n "${defaultEntry}" | cut -f1 -d ":" | sed -ne "1p")
  1505. if test -z "$newDefaultNumberPlusOne"; then
  1506. echo "Previous default entry removed, resetting to 0">&2
  1507. set_default_value "0"
  1508. elif test -z "$defaultEntry"; then
  1509. echo "Value of default value matches no entry, resetting to 0" >&2
  1510. set_default_value "0"
  1511. else
  1512. if test "$newDefaultNumberPlusOne" = "1"; then
  1513. newDefaultNumber="0"
  1514. else
  1515. newDefaultNumber=$(expr $newDefaultNumberPlusOne - 1)
  1516. fi
  1517. echo "Updating the default booting kernel">&2
  1518. set_default_value "$newDefaultNumber"
  1519. fi
  1520. fi
  1521.  
  1522. echo "done" >&2
  1523. echo >&2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement