Advertisement
Guest User

Untitled

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