Skip to content

Instantly share code, notes, and snippets.

@maage
Last active November 16, 2018 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maage/e9d2245b1a39f0ee29ebb059f60c5ee0 to your computer and use it in GitHub Desktop.
Save maage/e9d2245b1a39f0ee29ebb059f60c5ee0 to your computer and use it in GitHub Desktop.
Fix %config(noreplace) files without reinstalling package

You can fix %config(noreplace) files without deleting and reinstalling. If package is not essential, it is easiest way. But for every essentia package/config file it is not recommended to remove them.

Run this as root to preserve users and other stuff. Download package. Unpack it. Move files to fix over old ones. You need also to copy selinux context and maybe capabilities. Cpio does not know about them. You need to set them by hand before moving file in place.

You also need to copy file attributes, extended attributes and ACL entries if there is some.

sudo -i
f=<file-wo-first-slash>
pkg="$(rpm -qf /"$f")"
rpm -V "$pkg"
tmp="$(mktemp -d -p .)"; cd "$tmp"
dnf download "$(rpm -q "$pkg")"
rpm2cpio *.rpm|cpio -idm
chcon {--reference=/,}"$f"
cap="$(getcap /"$f"|grep /"$f")";[ $? -ne 0 ] || setcap "$cap" "$f"
diff -u {,/}"$f"
mv {,/}"$f"
rpm -V "$pkg"
rm -rf ../"$tmp";cd

You can fix metadata of other files than %config(noreplace) and %ghost by running:

rpm --restore --noghost <package>

Currently ghost files might (usually) have bogus metadata and should not be restored this way.

To restore other content you can just run:

dnf reinstall -y <package>

Also note some of the packages do define files not in proper way. Like always locally changed files are defined just as %config. They should be defined usually as %config(noreplace). Sometimes packages have files or directories at /run. As it is tmpfs it does not make sense to define anything there at package. Package maintainers should add tmpfiles.d file to define those.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment