Followup-For: Bug #822671 Control: tag -1 patch Hi Ben,
attached is a patch that improves the handling of initramfs.conf That's a lot of code - but most of it was stolen from dpkg-maintscript-helper :-) (preinst/postrm from rm_conffile, postinst from from mv_conffile with slight modifications) It's currently set to be very verbose: mvverbose=-v that should be changed to mvverbose=#-v for production. There is also a 1-line .maintscript file being added that takes care of /etc/bash_completion.d/initramfs-tools conffile removal This passed a piuparts jessie -> stretch upgrade (unmodified conffile) and a manual jessie -> stretch upgrade with a modified conffile. Andreas
diff -Nru initramfs-tools-0.125/debian/initramfs-tools-core.postinst initramfs-tools-0.125+nmu1/debian/initramfs-tools-core.postinst --- initramfs-tools-0.125/debian/initramfs-tools-core.postinst 2016-04-17 21:39:22.000000000 +0200 +++ initramfs-tools-0.125+nmu1/debian/initramfs-tools-core.postinst 2016-04-30 20:41:44.000000000 +0200 @@ -7,15 +7,57 @@ > /etc/initramfs-tools/modules fi +mvverbose=-v + +# from dpkg-maintscript-helper +ensure_package_owns_file() { + local PACKAGE="$1" + local FILE="$2" + + if ! dpkg-query -L "$PACKAGE" | grep -q -x "$FILE"; then + debug "File '$FILE' not owned by package " \ + "'$PACKAGE', skipping $command" + return 1 + fi + return 0 +} + +# from dpkg-maintscript-helper +debug() { + if [ -n "$DPKG_DEBUG" ]; then + echo "DEBUG: $PROGNAME: $*" >&2 + fi +} + +# from dpkg-maintscript-helper, modified +finish_mv_conffile() { + local OLDCONFFILE="$1" + local NEWCONFFILE="$2" + local PACKAGE="$3" + + rm $mvverbose -f $NEWCONFFILE.dpkg-remove + + [ -e "$OLDCONFFILE" ] || return 0 + #ensure_package_owns_file "$PACKAGE" "$OLDCONFFILE" || return 0 + ensure_package_owns_file "$PACKAGE" "$NEWCONFFILE" || return 0 + + echo "Preserving user changes to $NEWCONFFILE (renamed from $OLDCONFFILE)..." + if [ -e "$NEWCONFFILE" ]; then + mv $mvverbose -f "$NEWCONFFILE" "$NEWCONFFILE.dpkg-new" + fi + mv $mvverbose -f "$OLDCONFFILE" "$NEWCONFFILE" +} + # When installing as a new dependency of initramfs-tools, or upgrading # from <0.123, we need to move initramfs.conf back into place. -if [ "$1" = configure ] && dpkg --compare-versions "$2" lt 0.123~ && \ - [ -f /etc/initramfs-tools/initramfs.conf.dpkg-backup ]; then - # On fresh installation ($2 is empty), overwrite the shipped - # initramfs.conf. Otherwise use -n to avoid overwriting user - # configuration if somehow both files exist. - mv ${2:+-n} /etc/initramfs-tools/initramfs.conf.dpkg-backup \ - /etc/initramfs-tools/initramfs.conf +LASTVERSION=0.123~ +CONFFILE=/etc/initramfs-tools/initramfs.conf +PACKAGE=initramfs-tools-core +OLDCONFFILE="$CONFFILE.dpkg-backup" +NEWCONFFILE="$CONFFILE" +if [ "$1" = "configure" ] && + dpkg --compare-versions "$2" le "$LASTVERSION"; then + finish_mv_conffile "$OLDCONFFILE" "$NEWCONFFILE" "$PACKAGE" fi #DEBHELPER# diff -Nru initramfs-tools-0.125/debian/initramfs-tools.maintscript initramfs-tools-0.125+nmu1/debian/initramfs-tools.maintscript --- initramfs-tools-0.125/debian/initramfs-tools.maintscript 1970-01-01 01:00:00.000000000 +0100 +++ initramfs-tools-0.125+nmu1/debian/initramfs-tools.maintscript 2016-04-30 20:41:44.000000000 +0200 @@ -0,0 +1 @@ +rm_conffile /etc/bash_completion.d/initramfs-tools 0.126~ diff -Nru initramfs-tools-0.125/debian/initramfs-tools.postinst initramfs-tools-0.125+nmu1/debian/initramfs-tools.postinst --- initramfs-tools-0.125/debian/initramfs-tools.postinst 2016-04-17 21:39:22.000000000 +0200 +++ initramfs-tools-0.125+nmu1/debian/initramfs-tools.postinst 2016-04-30 20:41:44.000000000 +0200 @@ -2,12 +2,57 @@ set -e +mvverbose=-v + +# from dpkg-maintscript-helper +ensure_package_owns_file() { + local PACKAGE="$1" + local FILE="$2" + + if ! dpkg-query -L "$PACKAGE" | grep -q -x "$FILE"; then + debug "File '$FILE' not owned by package " \ + "'$PACKAGE', skipping $command" + return 1 + fi + return 0 +} + +# from dpkg-maintscript-helper +debug() { + if [ -n "$DPKG_DEBUG" ]; then + echo "DEBUG: $PROGNAME: $*" >&2 + fi +} + +# from dpkg-maintscript-helper, modified +finish_mv_conffile() { + local OLDCONFFILE="$1" + local NEWCONFFILE="$2" + local PACKAGE="$3" + + rm $mvverbose -f $NEWCONFFILE.dpkg-remove + + [ -e "$OLDCONFFILE" ] || return 0 + #ensure_package_owns_file "$PACKAGE" "$OLDCONFFILE" || return 0 + ensure_package_owns_file "$PACKAGE" "$NEWCONFFILE" || return 0 + + echo "Preserving user changes to $NEWCONFFILE (renamed from $OLDCONFFILE)..." + if [ -e "$NEWCONFFILE" ]; then + mv $mvverbose -f "$NEWCONFFILE" "$NEWCONFFILE.dpkg-new" + fi + mv $mvverbose -f "$OLDCONFFILE" "$NEWCONFFILE" +} + # If initramfs-tools-core was fully upgraded to 0.123 before our # preinst ran, we need to move initramfs.conf back into place. -if [ "$1" = configure ] && [ -n "$2" ] && dpkg --compare-versions "$2" lt 0.123~ && \ - [ -f /etc/initramfs-tools/initramfs.conf.dpkg-backup ]; then - mv -n /etc/initramfs-tools/initramfs.conf.dpkg-backup \ - /etc/initramfs-tools/initramfs.conf +LASTVERSION=0.123~ +CONFFILE=/etc/initramfs-tools/initramfs.conf +PACKAGE=initramfs-tools-core +OLDCONFFILE="$CONFFILE.dpkg-backup" +NEWCONFFILE="$CONFFILE" +if [ "$1" = "configure" ] && [ -n "$2" ] && + dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then + finish_mv_conffile "$OLDCONFFILE" "$NEWCONFFILE" "$PACKAGE" fi # Regenerate initramfs whenever we go to dpkg state `installed' diff -Nru initramfs-tools-0.125/debian/initramfs-tools.postrm initramfs-tools-0.125+nmu1/debian/initramfs-tools.postrm --- initramfs-tools-0.125/debian/initramfs-tools.postrm 2016-04-17 21:39:22.000000000 +0200 +++ initramfs-tools-0.125+nmu1/debian/initramfs-tools.postrm 2016-04-30 20:41:44.000000000 +0200 @@ -2,12 +2,54 @@ set -e +mvverbose=-v + +# from dpkg-maintscript-helper +ensure_package_owns_file() { + local PACKAGE="$1" + local FILE="$2" + + if ! dpkg-query -L "$PACKAGE" | grep -q -x "$FILE"; then + debug "File '$FILE' not owned by package " \ + "'$PACKAGE', skipping $command" + return 1 + fi + return 0 +} + +# from dpkg-maintscript-helper +debug() { + if [ -n "$DPKG_DEBUG" ]; then + echo "DEBUG: $PROGNAME: $*" >&2 + fi +} + +# from dpkg-maintscript-helper +abort_rm_conffile() { + local CONFFILE="$1" + local PACKAGE="$2" + + ensure_package_owns_file "$PACKAGE" "$CONFFILE" || return 0 + + if [ -e "$CONFFILE.dpkg-remove" ]; then + echo "Reinstalling $CONFFILE that was moved away" + mv $mvverbose "$CONFFILE.dpkg-remove" "$CONFFILE" + fi + if [ -e "$CONFFILE.dpkg-backup" ]; then + echo "Reinstalling $CONFFILE that was backupped" + mv $mvverbose "$CONFFILE.dpkg-backup" "$CONFFILE" + fi +} + # If aborting an upgrade from <0.123, we need to move initramfs.conf back # into place. -if [ "$1" = abort-upgrade ] && dpkg --compare-versions "$2" lt 0.123~ && \ - [ -f /etc/initramfs-tools/initramfs.conf.dpkg-backup ]; then - mv -n /etc/initramfs-tools/initramfs.conf.dpkg-backup \ - /etc/initramfs-tools/initramfs.conf +LASTVERSION=0.123~ +CONFFILE=/etc/initramfs-tools/initramfs.conf +PACKAGE=initramfs-tools +if [ "$1" = "abort-install" -o "$1" = "abort-upgrade" ] && + [ -n "$2" ] && + dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then + abort_rm_conffile "$CONFFILE" "$PACKAGE" fi #DEBHELPER# diff -Nru initramfs-tools-0.125/debian/initramfs-tools.preinst initramfs-tools-0.125+nmu1/debian/initramfs-tools.preinst --- initramfs-tools-0.125/debian/initramfs-tools.preinst 2016-04-17 21:39:22.000000000 +0200 +++ initramfs-tools-0.125+nmu1/debian/initramfs-tools.preinst 2016-04-30 20:41:44.000000000 +0200 @@ -2,14 +2,56 @@ set -e +mvverbose=-v + +# from dpkg-maintscript-helper +ensure_package_owns_file() { + local PACKAGE="$1" + local FILE="$2" + + if ! dpkg-query -L "$PACKAGE" | grep -q -x "$FILE"; then + debug "File '$FILE' not owned by package " \ + "'$PACKAGE', skipping $command" + return 1 + fi + return 0 +} + +# from dpkg-maintscript-helper +debug() { + if [ -n "$DPKG_DEBUG" ]; then + echo "DEBUG: $PROGNAME: $*" >&2 + fi +} + +# from dpkg-maintscript-helper +prepare_rm_conffile() { + local CONFFILE="$1" + local PACKAGE="$2" + + [ -e "$CONFFILE" ] || return 0 + ensure_package_owns_file "$PACKAGE" "$CONFFILE" || return 0 + + local md5sum="$(md5sum $CONFFILE | sed -e 's/ .*//')" + local old_md5sum="$(dpkg-query -W -f='${Conffiles}' $PACKAGE | \ + sed -n -e "\' $CONFFILE ' { s/ obsolete$//; s/.* //; p }")" + if [ "$md5sum" != "$old_md5sum" ]; then + mv $mvverbose -f "$CONFFILE" "$CONFFILE.dpkg-backup" + else + mv $mvverbose -f "$CONFFILE" "$CONFFILE.dpkg-remove" + fi +} + # When upgrading from <0.123, we need to move initramfs.conf away so # dpkg will stop remembering it as an 'obsolete' conffile for us. The # postinst of either initramfs-tools or initramfs-tools-core will move # it back. -if [ "$1" = upgrade ] && dpkg --compare-versions "$2" lt 0.123~ && \ - [ -f /etc/initramfs-tools/initramfs.conf ]; then - mv /etc/initramfs-tools/initramfs.conf \ - /etc/initramfs-tools/initramfs.conf.dpkg-backup +LASTVERSION=0.123~ +CONFFILE=/etc/initramfs-tools/initramfs.conf +PACKAGE=initramfs-tools +if [ "$1" = "install" -o "$1" = "upgrade" ] && [ -n "$2" ] && + dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then + prepare_rm_conffile "$CONFFILE" "$PACKAGE" fi #DEBHELPER#