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 13python __anonymous () { 14 import re 15 16 preinst = ''' 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. 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` 25 if [ -f "$D$kimage" ]; then 26 sed -i "s:KERNEL_IMAGETYPE \+root=:${kimage##*/} root=:" $grubcfg 27 fi 28 fi 29 fi 30 31 # Rename old kernel if it conflicts with new kernel name. 32 if grep -q "/KERNEL_IMAGETYPE-${KERNEL_VERSION} \+root=" $grubcfg; then 33 if [ -f "$D/boot/KERNEL_IMAGETYPE-${KERNEL_VERSION}" ]; then 34 timestamp=`date +%s` 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" 38 fi 39 fi 40 fi 41''' 42 43 postinst = ''' 44 get_new_grub_cfg() { 45 grubcfg="$1" 46 old_image="$2" 47 title="Update KERNEL_IMAGETYPE-${KERNEL_VERSION}-${PV}" 48 if [ "${grubcfg##*/}" = "grub.cfg" ]; then 49 rootfs=`grep " *linux \+[^ ]\+ \+root=" $grubcfg -m 1 | \ 50 sed "s#${old_image}#${old_image%/*}/KERNEL_IMAGETYPE-${KERNEL_VERSION}#"` 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 | \ 58 sed "s#${old_image}#${old_image%/*}/KERNEL_IMAGETYPE-${KERNEL_VERSION}#"` 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 93''' 94 95 imagetypes = d.getVar('KERNEL_IMAGETYPES') 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) 104} 105 106