blob: f7dcc0715a546a7da2bb9ec049171514ba5d6426 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#
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 Zhefe76b1e2016-05-25 04:47:16 -040013python __anonymous () {
14 import re
15
16 preinst = '''
Patrick Williamsc124f4f2015-09-15 14:41:29 -050017 # 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 Zhefe76b1e2016-05-25 04:47:16 -040022 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 Williamsc124f4f2015-09-15 14:41:29 -050025 if [ -f "$D$kimage" ]; then
He Zhefe76b1e2016-05-25 04:47:16 -040026 sed -i "s:KERNEL_IMAGETYPE \+root=:${kimage##*/} root=:" $grubcfg
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027 fi
28 fi
29 fi
30
31 # Rename old kernel if it conflicts with new kernel name.
He Zhefe76b1e2016-05-25 04:47:16 -040032 if grep -q "/KERNEL_IMAGETYPE-${KERNEL_VERSION} \+root=" $grubcfg; then
33 if [ -f "$D/boot/KERNEL_IMAGETYPE-${KERNEL_VERSION}" ]; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -050034 timestamp=`date +%s`
He Zhefe76b1e2016-05-25 04:47:16 -040035 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 Williamsc124f4f2015-09-15 14:41:29 -050038 fi
39 fi
40 fi
He Zhefe76b1e2016-05-25 04:47:16 -040041'''
Patrick Williamsc124f4f2015-09-15 14:41:29 -050042
He Zhefe76b1e2016-05-25 04:47:16 -040043 postinst = '''
Patrick Williamsc124f4f2015-09-15 14:41:29 -050044 get_new_grub_cfg() {
45 grubcfg="$1"
46 old_image="$2"
He Zhefe76b1e2016-05-25 04:47:16 -040047 title="Update KERNEL_IMAGETYPE-${KERNEL_VERSION}-${PV}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050048 if [ "${grubcfg##*/}" = "grub.cfg" ]; then
49 rootfs=`grep " *linux \+[^ ]\+ \+root=" $grubcfg -m 1 | \
He Zhefe76b1e2016-05-25 04:47:16 -040050 sed "s#${old_image}#${old_image%/*}/KERNEL_IMAGETYPE-${KERNEL_VERSION}#"`
Patrick Williamsc124f4f2015-09-15 14:41:29 -050051
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 Zhefe76b1e2016-05-25 04:47:16 -040058 sed "s#${old_image}#${old_image%/*}/KERNEL_IMAGETYPE-${KERNEL_VERSION}#"`
Patrick Williamsc124f4f2015-09-15 14:41:29 -050059
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 Zhefe76b1e2016-05-25 04:47:16 -040093'''
94
95 imagetypes = d.getVar('KERNEL_IMAGETYPES', True)
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 Williamsc124f4f2015-09-15 14:41:29 -0500104}
105