Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # |
| 2 | # While installing a rpm to update kernel on a deployed target, it will update |
| 3 | # the boot area and the boot menu with the kernel as the priority but allow |
| 4 | # you to fall back to the original kernel as well. |
| 5 | # |
| 6 | # - In kernel-image's preinstall scriptlet, it backs up original kernel to avoid |
| 7 | # probable confliction with the new one. |
| 8 | # |
| 9 | # - In kernel-image's postinstall scriptlet, it modifies grub's config file to |
| 10 | # updates the new kernel as the boot priority. |
| 11 | # |
| 12 | |
He Zhe | fe76b1e | 2016-05-25 04:47:16 -0400 | [diff] [blame] | 13 | python __anonymous () { |
| 14 | import re |
| 15 | |
| 16 | preinst = ''' |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 17 | # Parsing confliction |
| 18 | [ -f "$D/boot/grub/menu.list" ] && grubcfg="$D/boot/grub/menu.list" |
| 19 | [ -f "$D/boot/grub/grub.cfg" ] && grubcfg="$D/boot/grub/grub.cfg" |
| 20 | if [ -n "$grubcfg" ]; then |
| 21 | # Dereference symlink to avoid confliction with new kernel name. |
He Zhe | fe76b1e | 2016-05-25 04:47:16 -0400 | [diff] [blame] | 22 | if grep -q "/KERNEL_IMAGETYPE \+root=" $grubcfg; then |
| 23 | if [ -L "$D/boot/KERNEL_IMAGETYPE" ]; then |
| 24 | kimage=`realpath $D/boot/KERNEL_IMAGETYPE 2>/dev/null` |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 25 | if [ -f "$D$kimage" ]; then |
He Zhe | fe76b1e | 2016-05-25 04:47:16 -0400 | [diff] [blame] | 26 | sed -i "s:KERNEL_IMAGETYPE \+root=:${kimage##*/} root=:" $grubcfg |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 27 | fi |
| 28 | fi |
| 29 | fi |
| 30 | |
| 31 | # Rename old kernel if it conflicts with new kernel name. |
He Zhe | fe76b1e | 2016-05-25 04:47:16 -0400 | [diff] [blame] | 32 | if grep -q "/KERNEL_IMAGETYPE-${KERNEL_VERSION} \+root=" $grubcfg; then |
| 33 | if [ -f "$D/boot/KERNEL_IMAGETYPE-${KERNEL_VERSION}" ]; then |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 34 | timestamp=`date +%s` |
He Zhe | fe76b1e | 2016-05-25 04:47:16 -0400 | [diff] [blame] | 35 | kimage="$D/boot/KERNEL_IMAGETYPE-${KERNEL_VERSION}-$timestamp-back" |
| 36 | sed -i "s:KERNEL_IMAGETYPE-${KERNEL_VERSION} \+root=:${kimage##*/} root=:" $grubcfg |
| 37 | mv "$D/boot/KERNEL_IMAGETYPE-${KERNEL_VERSION}" "$kimage" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 38 | fi |
| 39 | fi |
| 40 | fi |
He Zhe | fe76b1e | 2016-05-25 04:47:16 -0400 | [diff] [blame] | 41 | ''' |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 42 | |
He Zhe | fe76b1e | 2016-05-25 04:47:16 -0400 | [diff] [blame] | 43 | postinst = ''' |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 44 | get_new_grub_cfg() { |
| 45 | grubcfg="$1" |
| 46 | old_image="$2" |
He Zhe | fe76b1e | 2016-05-25 04:47:16 -0400 | [diff] [blame] | 47 | title="Update KERNEL_IMAGETYPE-${KERNEL_VERSION}-${PV}" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 48 | if [ "${grubcfg##*/}" = "grub.cfg" ]; then |
| 49 | rootfs=`grep " *linux \+[^ ]\+ \+root=" $grubcfg -m 1 | \ |
He Zhe | fe76b1e | 2016-05-25 04:47:16 -0400 | [diff] [blame] | 50 | sed "s#${old_image}#${old_image%/*}/KERNEL_IMAGETYPE-${KERNEL_VERSION}#"` |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 51 | |
| 52 | echo "menuentry \"$title\" {" |
| 53 | echo " set root=(hd0,1)" |
| 54 | echo "$rootfs" |
| 55 | echo "}" |
| 56 | elif [ "${grubcfg##*/}" = "menu.list" ]; then |
| 57 | rootfs=`grep "kernel \+[^ ]\+ \+root=" $grubcfg -m 1 | \ |
He Zhe | fe76b1e | 2016-05-25 04:47:16 -0400 | [diff] [blame] | 58 | sed "s#${old_image}#${old_image%/*}/KERNEL_IMAGETYPE-${KERNEL_VERSION}#"` |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 59 | |
| 60 | echo "default 0" |
| 61 | echo "timeout 30" |
| 62 | echo "title $title" |
| 63 | echo "root (hd0,0)" |
| 64 | echo "$rootfs" |
| 65 | fi |
| 66 | } |
| 67 | |
| 68 | get_old_grub_cfg() { |
| 69 | grubcfg="$1" |
| 70 | if [ "${grubcfg##*/}" = "grub.cfg" ]; then |
| 71 | cat "$grubcfg" |
| 72 | elif [ "${grubcfg##*/}" = "menu.list" ]; then |
| 73 | sed -e '/^default/d' -e '/^timeout/d' "$grubcfg" |
| 74 | fi |
| 75 | } |
| 76 | |
| 77 | if [ -f "$D/boot/grub/grub.cfg" ]; then |
| 78 | grubcfg="$D/boot/grub/grub.cfg" |
| 79 | old_image=`grep ' *linux \+[^ ]\+ \+root=' -m 1 "$grubcfg" | awk '{print $2}'` |
| 80 | elif [ -f "$D/boot/grub/menu.list" ]; then |
| 81 | grubcfg="$D/boot/grub/menu.list" |
| 82 | old_image=`grep '^kernel \+[^ ]\+ \+root=' -m 1 "$grubcfg" | awk '{print $2}'` |
| 83 | fi |
| 84 | |
| 85 | # Don't update grubcfg at first install while old bzImage doesn't exist. |
| 86 | if [ -f "$D/boot/${old_image##*/}" ]; then |
| 87 | grubcfgtmp="$grubcfg.tmp" |
| 88 | get_new_grub_cfg "$grubcfg" "$old_image" > $grubcfgtmp |
| 89 | get_old_grub_cfg "$grubcfg" >> $grubcfgtmp |
| 90 | mv $grubcfgtmp $grubcfg |
| 91 | echo "Caution! Update kernel may affect kernel-module!" |
| 92 | fi |
He Zhe | fe76b1e | 2016-05-25 04:47:16 -0400 | [diff] [blame] | 93 | ''' |
| 94 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 95 | imagetypes = d.getVar('KERNEL_IMAGETYPES') |
He Zhe | fe76b1e | 2016-05-25 04:47:16 -0400 | [diff] [blame] | 96 | imagetypes = re.sub(r'\.gz$', '', imagetypes) |
| 97 | |
| 98 | for type in imagetypes.split(): |
| 99 | typelower = type.lower() |
| 100 | preinst_append = preinst.replace('KERNEL_IMAGETYPE', type) |
| 101 | postinst_prepend = postinst.replace('KERNEL_IMAGETYPE', type) |
| 102 | d.setVar('pkg_preinst_kernel-image-' + typelower + '_append', preinst_append) |
| 103 | d.setVar('pkg_postinst_kernel-image-' + typelower + '_prepend', postinst_prepend) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 104 | } |
| 105 | |