dpkg Does Not Resolve the Graph
In one line
apt is a tool that decides what to install, and dpkg is a tool that installs what has been decided. Once you know this division of labor, you immediately understand why dpkg -i stops with a dependency error.
Why this exists
If you download one .deb file and install it with dpkg -i, you get a message like this.
dpkg: dependency problems prevent configuration of labhub-extra:
labhub-extra depends on labhub-tool (>= 1.2.0); however:
Package labhub-tool is not installed.
dpkg does not know the repository. It does not even know where to fetch what from. It merely unpacks the file it is given, and if the dependencies are not satisfied, leaves it in a "not configured" state. That is why apt-get -f install (fix broken) exists as its partner. It means "apt, look at the repository and finish resolving the unresolved state dpkg left behind."
How it works
A .deb file is actually an ar archive. It holds three pieces.
| Piece | Contents |
|---|---|
debian-binary |
Format version (usually 2.0) |
control.tar.* |
The control file and maintainer scripts (preinst/postinst/prerm/postrm), conffiles, md5sums |
data.tar.* |
The files that are actually unpacked onto the filesystem |
Installation has two steps, unpack → configure. unpack extracts the data, and configure runs postinst to do finishing work such as registering the service. The first two characters of the dpkg -l output show this state.
| Mark | Meaning |
|---|---|
ii |
Install requested + install complete (normal) |
iU |
Install requested + only unpacked (configure failed) |
rc |
Removal requested + only configuration files remain |
pn |
Not installed (purged) |
It is common to overlook the rc state. The answer to "I removed it, so why is the configuration still there?" is here.
dpkg's query commands go in three directions.
dpkg -l # 무엇이 설치돼 있나
dpkg -L coreutils # 이 패키지가 어떤 파일을 깔았나
dpkg -S /usr/bin/ls # 이 파일은 누가 깔았나
dpkg -s coreutils # 이 패키지의 메타데이터는 무엇인가
dpkg-query -W -f='${Package} ${Version}\n'
The third (-S) is the one used most often in practice. When you meet a binary of unknown origin on an unfamiliar server, it separates in one second whether a package installed it or a person placed it by hand. If dpkg -S finds nothing, that file is outside package management, and such files tend not to disappear on upgrades and not to be captured in backups.
What it looks like in the field
A configuration file conflict. During an upgrade, a Configuration file '/etc/foo.conf' ... What would you like to do? prompt appears. dpkg remembers the md5 of files registered in conffiles, so it can tell whether a person edited them. If you carelessly choose "package maintainer's version" at this prompt, the tuning you put in by hand disappears entirely. In automation scripts you have to state the policy explicitly with -o Dpkg::Options::="--force-confold".
Configuration files are treated specially
The files listed in conffiles inside a .deb follow different rules from the rest. If you do not
know these rules, you meet situations where the configuration vanished after an update or, conversely, the new configuration never came in,
and both have the same cause.
When it updates, dpkg compares three things: the content the package originally put in, the content on disk now, and the content the new package wants to put in.
- If we did not edit it, it quietly replaces it with the new one.
- If we edited it and it is identical to the new one, nothing happens.
- If we edited it and it also differs from the new one, it asks. If it is not interactive, following the default behavior
it keeps ours and places the new file next to it as
.dpkg-dist.
The third case is where the problem lies. In an automated update nobody sees that question, so
the service runs with the configuration entries the new version needs missing from our file.
It quietly runs on defaults and later shows up as strange symptoms. That is why you need a procedure that, after an update,
scans for whether .dpkg-dist, .dpkg-new, or .ucf-dist files have appeared.
The relationship between the rc state and configuration files is explained here too. As we saw earlier, the reason configuration remains even after you delete a package is that remove does not touch conffiles,
and purge deletes those as well. So if you reinstall the same package, the old configuration comes back to life,
and "I reinstalled cleanly but get the same problem" comes from here.
Finally, if you plan to manage a configuration file outside the package, it is better to put it in a separate file that is not in
conffiles at all. This is why most packages provide a directory such as conf.d/. The files in it are not owned by the package, so an update does not touch
them, and the three-way comparison itself never happens.
Half-installed packages: where it stopped is the cause
You put several .deb files from a vendor in at once with dpkg -i, an error came up, and since then apt stops with "Unmet dependencies" whatever it does. There is one thing to do in this state: check, from the two status letters and the error lines, at which stage dpkg stopped for each package. Even the same "the install did not finish" splits into three places where it stopped.
iF: it failed during configuration (half-configured). All the files were unpacked and postinst ran, but that script exited with a nonzero value. The error line is "post-installation script subprocess returned error exit status 1", and right above it is the real reason the script left (a configuration file is missing, an account is missing). The prescription is to provide what the script required and then rerun only the configuration stage. Editing the script to pass with exit 0 is not a fix: the package becomes "installed" without what the script was meant to do (registering the service, preparing directories).
iU: unpacked, but could not enter configuration (unpacked). A dependency package is missing, so dpkg never started the configuration stage. The error line is "depends on … however: Package … is not installed". If you type apt-get -f install here, apt looks for that dependency in the repository, and if it cannot find it, it proposes removing the iU package instead. The broken state disappears, so from apt's point of view it is fixed, but the package we wanted disappears too. If the dependency was in the same delivery, install it and rerun the configuration.
It could not even unpack: someone else's file. "trying to overwrite '/usr/bin/…', which is also in package …" means that another package already owns that path. dpkg allows only one owner per file, so it rolls back the unpacking and stops (status in: wanted to install, but not installed). If the new package replaces the old one, the right thing is to take the old one out first, and the new package should originally have declared that with Replaces and Conflicts. If you force the overwrite, a state remains in which two packages claim the same file, and the moment you later remove the old package, the new package's file disappears with it.
The order matters too. If the three are mixed, look at the whole with dpkg --audit and dpkg -l first, fix the ones with different causes separately, and finally rerun the configuration stage once.
What you will do in the next lab
You query the system with dpkg -L/-S/-s, and build a .deb yourself from a fixture directory and even install it. Once you have built a package, you know for certain what is inside it. At the end, you finish off a server where three vendor packages each stopped at one of the three places above.