blob: a63f482a912474c4a1b0202f4dc9974cb516c30e [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
13pkg_preinst_kernel-image_append () {
14 # Parsing confliction
15 [ -f "$D/boot/grub/menu.list" ] && grubcfg="$D/boot/grub/menu.list"
16 [ -f "$D/boot/grub/grub.cfg" ] && grubcfg="$D/boot/grub/grub.cfg"
17 if [ -n "$grubcfg" ]; then
18 # Dereference symlink to avoid confliction with new kernel name.
19 if grep -q "/${KERNEL_IMAGETYPE} \+root=" $grubcfg; then
20 if [ -L "$D/boot/${KERNEL_IMAGETYPE}" ]; then
21 kimage=`realpath $D/boot/${KERNEL_IMAGETYPE} 2>/dev/null`
22 if [ -f "$D$kimage" ]; then
23 sed -i "s:${KERNEL_IMAGETYPE} \+root=:${kimage##*/} root=:" $grubcfg
24 fi
25 fi
26 fi
27
28 # Rename old kernel if it conflicts with new kernel name.
29 if grep -q "/${KERNEL_IMAGETYPE}-${KERNEL_VERSION} \+root=" $grubcfg; then
30 if [ -f "$D/boot/${KERNEL_IMAGETYPE}-${KERNEL_VERSION}" ]; then
31 timestamp=`date +%s`
32 kimage="$D/boot/${KERNEL_IMAGETYPE}-${KERNEL_VERSION}-$timestamp-back"
33 sed -i "s:${KERNEL_IMAGETYPE}-${KERNEL_VERSION} \+root=:${kimage##*/} root=:" $grubcfg
34 mv "$D/boot/${KERNEL_IMAGETYPE}-${KERNEL_VERSION}" "$kimage"
35 fi
36 fi
37 fi
38}
39
40pkg_postinst_kernel-image_prepend () {
41 get_new_grub_cfg() {
42 grubcfg="$1"
43 old_image="$2"
44 title="Update ${KERNEL_IMAGETYPE}-${KERNEL_VERSION}-${PV}"
45 if [ "${grubcfg##*/}" = "grub.cfg" ]; then
46 rootfs=`grep " *linux \+[^ ]\+ \+root=" $grubcfg -m 1 | \
47 sed "s#${old_image}#${old_image%/*}/${KERNEL_IMAGETYPE}-${KERNEL_VERSION}#"`
48
49 echo "menuentry \"$title\" {"
50 echo " set root=(hd0,1)"
51 echo "$rootfs"
52 echo "}"
53 elif [ "${grubcfg##*/}" = "menu.list" ]; then
54 rootfs=`grep "kernel \+[^ ]\+ \+root=" $grubcfg -m 1 | \
55 sed "s#${old_image}#${old_image%/*}/${KERNEL_IMAGETYPE}-${KERNEL_VERSION}#"`
56
57 echo "default 0"
58 echo "timeout 30"
59 echo "title $title"
60 echo "root (hd0,0)"
61 echo "$rootfs"
62 fi
63 }
64
65 get_old_grub_cfg() {
66 grubcfg="$1"
67 if [ "${grubcfg##*/}" = "grub.cfg" ]; then
68 cat "$grubcfg"
69 elif [ "${grubcfg##*/}" = "menu.list" ]; then
70 sed -e '/^default/d' -e '/^timeout/d' "$grubcfg"
71 fi
72 }
73
74 if [ -f "$D/boot/grub/grub.cfg" ]; then
75 grubcfg="$D/boot/grub/grub.cfg"
76 old_image=`grep ' *linux \+[^ ]\+ \+root=' -m 1 "$grubcfg" | awk '{print $2}'`
77 elif [ -f "$D/boot/grub/menu.list" ]; then
78 grubcfg="$D/boot/grub/menu.list"
79 old_image=`grep '^kernel \+[^ ]\+ \+root=' -m 1 "$grubcfg" | awk '{print $2}'`
80 fi
81
82 # Don't update grubcfg at first install while old bzImage doesn't exist.
83 if [ -f "$D/boot/${old_image##*/}" ]; then
84 grubcfgtmp="$grubcfg.tmp"
85 get_new_grub_cfg "$grubcfg" "$old_image" > $grubcfgtmp
86 get_old_grub_cfg "$grubcfg" >> $grubcfgtmp
87 mv $grubcfgtmp $grubcfg
88 echo "Caution! Update kernel may affect kernel-module!"
89 fi
90}
91