kernel-grub.bbclass 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #
  2. # Copyright OpenEmbedded Contributors
  3. #
  4. #
  5. # While installing a rpm to update kernel on a deployed target, it will update
  6. # the boot area and the boot menu with the kernel as the priority but allow
  7. # you to fall back to the original kernel as well.
  8. #
  9. # - In kernel-image's preinstall scriptlet, it backs up original kernel to avoid
  10. # probable confliction with the new one.
  11. #
  12. # - In kernel-image's postinstall scriptlet, it modifies grub's config file to
  13. # updates the new kernel as the boot priority.
  14. #
  15. python __anonymous () {
  16. import re
  17. preinst = '''
  18. # Parsing confliction
  19. [ -f "$D/boot/grub/menu.list" ] && grubcfg="$D/boot/grub/menu.list"
  20. [ -f "$D/boot/grub/grub.cfg" ] && grubcfg="$D/boot/grub/grub.cfg"
  21. if [ -n "$grubcfg" ]; then
  22. # Dereference symlink to avoid confliction with new kernel name.
  23. if grep -q "/KERNEL_IMAGETYPE \+root=" $grubcfg; then
  24. if [ -L "$D/boot/KERNEL_IMAGETYPE" ]; then
  25. kimage=`realpath $D/boot/KERNEL_IMAGETYPE 2>/dev/null`
  26. if [ -f "$D$kimage" ]; then
  27. sed -i "s:KERNEL_IMAGETYPE \+root=:${kimage##*/} root=:" $grubcfg
  28. fi
  29. fi
  30. fi
  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. postinst = '''
  43. get_new_grub_cfg() {
  44. grubcfg="$1"
  45. old_image="$2"
  46. title="Update KERNEL_IMAGETYPE-${KERNEL_VERSION}-${PV}"
  47. if [ "${grubcfg##*/}" = "grub.cfg" ]; then
  48. rootfs=`grep " *linux \+[^ ]\+ \+root=" $grubcfg -m 1 | \
  49. sed "s#${old_image}#${old_image%/*}/KERNEL_IMAGETYPE-${KERNEL_VERSION}#"`
  50. echo "menuentry \"$title\" {"
  51. echo " set root=(hd0,1)"
  52. echo "$rootfs"
  53. echo "}"
  54. elif [ "${grubcfg##*/}" = "menu.list" ]; then
  55. rootfs=`grep "kernel \+[^ ]\+ \+root=" $grubcfg -m 1 | \
  56. sed "s#${old_image}#${old_image%/*}/KERNEL_IMAGETYPE-${KERNEL_VERSION}#"`
  57. echo "default 0"
  58. echo "timeout 30"
  59. echo "title $title"
  60. echo "root (hd0,0)"
  61. echo "$rootfs"
  62. fi
  63. }
  64. get_old_grub_cfg() {
  65. grubcfg="$1"
  66. if [ "${grubcfg##*/}" = "grub.cfg" ]; then
  67. cat "$grubcfg"
  68. elif [ "${grubcfg##*/}" = "menu.list" ]; then
  69. sed -e '/^default/d' -e '/^timeout/d' "$grubcfg"
  70. fi
  71. }
  72. if [ -f "$D/boot/grub/grub.cfg" ]; then
  73. grubcfg="$D/boot/grub/grub.cfg"
  74. old_image=`grep ' *linux \+[^ ]\+ \+root=' -m 1 "$grubcfg" | awk '{print $2}'`
  75. elif [ -f "$D/boot/grub/menu.list" ]; then
  76. grubcfg="$D/boot/grub/menu.list"
  77. old_image=`grep '^kernel \+[^ ]\+ \+root=' -m 1 "$grubcfg" | awk '{print $2}'`
  78. fi
  79. # Don't update grubcfg at first install while old bzImage doesn't exist.
  80. if [ -f "$D/boot/${old_image##*/}" ]; then
  81. grubcfgtmp="$grubcfg.tmp"
  82. get_new_grub_cfg "$grubcfg" "$old_image" > $grubcfgtmp
  83. get_old_grub_cfg "$grubcfg" >> $grubcfgtmp
  84. mv $grubcfgtmp $grubcfg
  85. echo "Caution! Update kernel may affect kernel-module!"
  86. fi
  87. '''
  88. imagetypes = d.getVar('KERNEL_IMAGETYPES')
  89. imagetypes = re.sub(r'\.gz$', '', imagetypes)
  90. for type in imagetypes.split():
  91. typelower = type.lower()
  92. preinst_append = preinst.replace('KERNEL_IMAGETYPE', type)
  93. postinst_prepend = postinst.replace('KERNEL_IMAGETYPE', type)
  94. d.setVar('pkg_preinst:kernel-image-' + typelower + ':append', preinst_append)
  95. d.setVar('pkg_postinst:kernel-image-' + typelower + ':prepend', postinst_prepend)
  96. }