YUM更新内部机制:重叠文件处理与异常终止防护技术问询
yum update Handles File Overwrites and Interruption Safety Great question! Let’s break this down clearly—since yum’s update logic relies on the RPM package manager under the hood, that’s where we’ll focus to answer your two key concerns.
1. RPM’s Transactional Approach: The Core of Safety
First, it’s critical to know that all package operations (updates, installs, deletes) in RPM are transactional. That means every change is treated as a single, atomic unit: either the entire update completes successfully, or RPM rolls back every partial change to leave your system exactly as it was before you started. This is why you never end up with missing files if you hit Ctrl+C mid-update.
2. The Actual Update Order (Why Old Packages Are Removed Last)
Your observation from Fedora’s docs is spot-on: when updating a package like hello, the old package’s "removal" happens after the new one is fully installed. Here’s a simplified walkthrough for your example:
- yum first downloads the new
helloRPM, verifies its signature and integrity to make sure it’s legitimate. - RPM starts a transaction for the update.
- It runs any pre-install scripts (
%prescriptlets) from the new package (these run before any files are touched, for setup tasks like stopping services if needed). - RPM installs all files from the new package:
- For overlapping files (like
/etc/hello.confand/usr/bin/hello), it overwrites the old file—but first saves a copy of the old version in its internal database, just in case rollback is needed. - Any new files unique to the new package are added to the system.
- For overlapping files (like
- Once all new files are installed and verified, RPM cleans up: it removes the old package’s entry from its database, and deletes any files that were part of the old package but aren’t included in the new one (this is the "old package removal" step you read about).
- Finally, it runs post-install scripts (
%postscriptlets) and marks the transaction as complete.
This order ensures there’s never a gap where critical files are missing. If the new package fails to install for any reason, the old package is still fully registered in RPM’s database, and your system can revert instantly.
3. What Happens When You Interrupt the Update?
If you hit Ctrl+C (or the process crashes) mid-update, RPM’s transaction system kicks into rollback mode:
- Any partial new files that were installed are deleted.
- Overwritten old files are restored from RPM’s backup copies.
- The old package’s database entry remains intact, so your system acts like the update never started.
No half-installed packages, no missing files—just your original system state.
4. How Overlapping Files Are Handled Safely
For files that exist in both old and new versions, RPM adds extra safeguards:
- It uses atomic file operations when possible. For example, it might write the new file to a temporary location first, then rename it to the target path. This ensures the file is never in a half-written, unusable state.
- For config files (like
/etc/hello.conf), RPM checks if you’ve modified the old version. If you have, it will prompt you to keep your custom version, overwrite it with the new default, or view the differences between the two—so you don’t lose your changes accidentally.
内容的提问来源于stack exchange,提问作者Stephen

