Let’s suppose that version 1.2 of the software stopped providing
/etc/foo.conf. Instead it provides
/etc/bar.conf because the configuration file got renamed. If you do nothing special, the new conffile will be installed with the default configuration, and the old one will stay around. Any customization made by the administrator are lost in the process (in fact they are not lost, they are still in foo.conf but they are unused).
Of course, you could do
mv /etc/foo.conf /etc/bar.conf in the pre-installation script. But that’s not satisfactory: it will generate a spurious conffile prompt that the end-user will not understand.
In the preinst script, you have to verify if the old conffile has been modified by the administrator. If yes, you want to keep the file around. Otherwise you know you will be able to ditch it once the upgrade is over, and you rename it to
/etc/foo.conf.dpkg-remove to remember this fact.
In the postinst script, you remove
/etc/foo.conf.dpkg-remove. If the old conffile (
/etc/foo.conf) still exists, it’s because it was modified by the administrator. You make a backup of the new conffile in
/etc/bar.conf.dpkg-dist and rename the old one into
In the postrm, when called to abort an upgrade, you move
/etc/foo.conf.dpkg-remove back to its original name.
In practice, use dpkg-maintscript-helper
dpkg-maintscript-helper can automate all those tasks. You just have to put the following snippet in the maintainer scripts (postinst, postrm, preinst):
if dpkg-maintscript-helper supports mv_conffile 2>/dev/null; then dpkg-maintscript-helper mv_conffile /etc/foo.conf /etc/bar.conf 1.1-3 -- "$@" fi
In this example, I assumed that version 1.1-3 was the last version of the package that contained
/etc/foo.conf (i.e. the last version released before 1.2-1 was packaged).
You can avoid the preliminary test if you pre-depend on “dpkg (>= 126.96.36.199)” or if enough time has passed to assume that everybody has a newer version anyway. You can learn all the details in dpkg-maintscript-helper’s manual page.
Found it useful? Be sure to not miss other packaging tips (or lessons), click here to subscribe to my free newsletter and get new articles by email (just check “Send me blog updates”).