@@ -101,22 +101,16 @@ install_maint_scripts () {
101101 hookdirs="\$ {hookdirs# }"
102102 test -n "\$ hookdirs" && run-parts --arg="${KERNELRELEASE} " --arg="/${installed_image_path} " \$ hookdirs
103103
104- # After successful installation, update canonical symlinks to point
105- # to the newest installed kernel image and its DTB directory.
104+ # After successful installation, update symlinks to point
105+ # to currently installed kernel image and its DTB directory.
106106 if [ -d /boot ]; then
107- latest_vmlinuz=\$ (ls -1 /boot/vmlinuz-* 2>/dev/null | sort -V | tail -n1 || true)
108- if [ -n "\$ latest_vmlinuz" ]; then
109- ln -sf "\$ latest_vmlinuz" /boot/vmlinuz || true
110- else
111- [ -L /boot/vmlinuz ] && rm -f /boot/vmlinuz || true
107+ if [ -n "/${installed_image_path} " ]; then
108+ ln -sf "/${installed_image_path} " /boot/vmlinuz || true
109+ fi
110+ linux_dtb_dir="/usr/lib/linux-image-${KERNELRELEASE} "
111+ if [ -d "\$ linux_dtb_dir" ]; then
112+ ln -sfnT "\$ linux_dtb_dir" /boot/dtb || true
112113 fi
113- fi
114-
115- latest_image_dir=\$ (ls -1d /usr/lib/linux-image-* 2>/dev/null | sort -V | tail -n1 || true)
116- if [ -n "\$ latest_image_dir" ] && [ -d "\$ latest_image_dir" ]; then
117- ln -sf "\$ latest_image_dir" /boot/dtb || true
118- else
119- [ -L /boot/dtb ] && rm -f /boot/dtb || true
120114 fi
121115
122116 exit 0
@@ -143,22 +137,35 @@ install_maint_scripts () {
143137 hookdirs="\$ {hookdirs# }"
144138 test -n "\$ hookdirs" && run-parts --arg="${KERNELRELEASE} " --arg="/${installed_image_path} " \$ hookdirs
145139
146- # After removal, recompute canonical symlinks so they point to the
147- # newest remaining kernel image (or are removed if none left).
148- if [ -d /boot ]; then
149- latest_vmlinuz=\$ (ls -1 /boot/vmlinuz-* 2>/dev/null | sort -V | tail -n1 || true)
150- if [ -n "\$ latest_vmlinuz" ]; then
151- ln -sf "\$ latest_vmlinuz" /boot/vmlinuz || true
152- else
153- [ -L /boot/vmlinuz ] && rm -f /boot/vmlinuz || true
140+ # After removal, link vmlinuz and dtb to the newest
141+ # remianing kernel (only consider version and subversion, ignore commit hash)
142+ # if the current kernel/dtb are linked to this kernel
143+ if [ -r /boot/ ]; then
144+ if [ -L /boot/vmlinuz ] && [ ! -e /boot/vmlinuz ]; then
145+ if [ "\$ (readlink -f /boot/vmlinuz)" = "/boot/vmlinuz-${KERNELRELEASE} " ]; then
146+ # recreate symbol link
147+ newest_image=\$ (sh -c 'tmp=\$ (mktemp) || exit 1; trap "rm -f \"\$ tmp\"" EXIT; for f in /boot/vmlinuz-*; do [ -e "\$ f" ] || continue; bn=\$ {f##*/}; fn=\$ {bn#vmlinuz-}; ver=\$ (printf "%s\n" "\$ fn" | sed -n "s/^\([0-9]\+\(\.[0-9]\+\)*\).*/\1/p"); [ -n "\$ ver" ] || ver="\$ fn"; if printf "%s\n" "\$ fn" | grep -q -- "-rc[0-9]\+"; then is_nonrc=0; rcnum=\$ (printf "%s\n" "\$ fn" | sed -n "s/.*-rc\([0-9]\+\).*/\1/p"); else is_nonrc=1; rcnum=0; fi; printf "%s|%d|%d|%s\n" "\$ ver" "\$ is_nonrc" "\$ rcnum" "\$ f" >> "\$ tmp"; done; [ -s "\$ tmp" ] || exit 0; sort -t"|" -k1,1V -k2,2n -k3,3n "\$ tmp" | tail -n1 | cut -d"|" -f4-')
148+ if [ -n \$ newest_image ]; then
149+ ln -sf \$ newest_image /boot/vmlinuz
150+ else
151+ # Can not find a image (kernel may not boot after reboot)
152+ rm /boot/vmlinuz
153+ fi
154+ fi
155+ fi
156+
157+ if [ -L /boot/dtb ] && [ ! -e /boot/dtb ]; then
158+ if [ "\$ (readlink -f /boot/dtb)" = "/usr/lib/linux-image-${KERNELRELEASE} " ]; then
159+ # recreate symbol link
160+ newest_dtb=\$ (sh -c 'tmp=\$ (mktemp) || exit 1; trap "rm -f \"\$ tmp\"" EXIT; for f in /usr/lib/linux-image-*; do [ -e "\$ f" ] || continue; bn=\$ {f##*/}; fn=\$ {bn#vmlinuz-}; ver=\$ (printf "%s\n" "\$ fn" | sed -n "s/^\([0-9]\+\(\.[0-9]\+\)*\).*/\1/p"); [ -n "\$ ver" ] || ver="\$ fn"; if printf "%s\n" "\$ fn" | grep -q -- "-rc[0-9]\+"; then is_nonrc=0; rcnum=\$ (printf "%s\n" "\$ fn" | sed -n "s/.*-rc\([0-9]\+\).*/\1/p"); else is_nonrc=1; rcnum=0; fi; printf "%s|%d|%d|%s\n" "\$ ver" "\$ is_nonrc" "\$ rcnum" "\$ f" >> "\$ tmp"; done; [ -s "\$ tmp" ] || exit 0; sort -t"|" -k1,1V -k2,2n -k3,3n "\$ tmp" | tail -n1 | cut -d"|" -f4-')
161+ if [ -n \$ newest_dtb ]; then
162+ ln -sf \$ newest_dtb /boot/dtb
163+ else
164+ # Can not find dtb (kernel may not boot after reboot)
165+ rm /boot/dtb
166+ fi
167+ fi
154168 fi
155- fi
156-
157- latest_image_dir=\$ (ls -1d /usr/lib/linux-image-* 2>/dev/null | sort -V | tail -n1 || true)
158- if [ -n "\$ latest_image_dir" ] && [ -d "\$ latest_image_dir/dtb" ]; then
159- ln -sf "\$ latest_image_dir/dtb" /boot/dtb || true
160- else
161- [ -L /boot/dtb ] && rm -f /boot/dtb || true
162169 fi
163170
164171 exit 0
0 commit comments