Debian

Generic Fix For Broken Packages In Debian Based Systems

So, you’ve just finished installing a bunch of updates in your system and let it restart as you go to get a cup of coffee. You take your time and get back with a cup in hand and ready to start your work for the day. As you sit down, a blank screen stares right back at you! You try typing something, but it doesn’t work. You hard reset the system and see that the boot screen shows up just fine, but then it just freezes and goes blank again. In a state of panic, you go into the safe mode and try to drop into a root shell. No dice. In a last ditch effort, you try connecting from a remote host. And voila! ssh works. Just barely. You are able to access all your files and enter commands normally.

Feeling a bit relieved, you try the normal troubleshooting methods and after some investigation, you realise that your update did not go well and you have a bunch of broken packages and a hot mess on your hands. So, what do you do? First, you’ve got to find out what the broken packages are by name. Fortunately, debian has a log file that has information about each instance that a package was installed, upgraded or removed. This is located here: /var/log/dpkg.log

Here’s a short example of what it might look like:

2019-09-28 11:40:33 status installed hicolor-icon-theme:all 0.17-2
2019-09-28 11:40:33 trigproc gnome-menus:amd64 3.32.0-1ubuntu1
2019-09-28 11:40:33 status half-configured gnome-menus:amd64 3.32.0-1ubuntu1
2019-09-28 11:40:33 status installed gnome-menus:amd64 3.32.0-1ubuntu1
2019-09-28 12:25:26 startup packages configure
2019-09-28 12:25:26 startup packages purge
2019-09-28 12:25:26 purge snapd:amd64 2.39.2+19.04
2019-09-28 12:25:26 status config-files snapd:amd64 2.39.2+19.04
2019-09-28 12:25:32 status not-installed snapd:amd64
2019-09-28 12:25:32 startup packages configure

You may notice a bunch of packages that are logged as “half-configured” or “half-installed”. These are our likely suspects. Now, we can’t search the whole darn list line by line to note down each half-configured package manually. We need to use some tool to pick up the lines we are interested in. Its a good thing that the log is well structured and follows the common log format. So, we can probably just grep our keyword “half-configured” and that would get us the list of packages to reinstall.

cat /var/log/dpkg.log | grep "half-configured"

The output of this command might look something like this:

2019-09-28 11:31:26 status half-configured mime-support:all 3.60ubuntu1
2019-09-28 11:31:26 status half-configured gnome-menus:amd64 3.32.0-1ubuntu1
2019-09-28 11:31:26 status half-configured libglib2.0-0:amd64 2.60.4-0ubuntu0.19.04.1
2019-09-28 11:31:26 status half-configured libglib2.0-0:i386 2.60.4-0ubuntu0.19.04.1
2019-09-28 11:31:26 status half-configured libc-bin:amd64 2.29-0ubuntu2
2019-09-28 11:31:27 status half-configured man-db:amd64 2.8.5-2
2019-09-28 11:31:31 status half-configured ca-certificates:all 20190110
2019-09-28 11:31:31 status half-configured install-info:amd64 6.5.0.dfsg.1-4build1
2019-09-28 11:31:32 status half-configured desktop-file-utils:amd64 0.23-4ubuntu1
2019-09-28 11:40:32 status half-configured sublime-text:amd64 3207
2019-09-28 11:40:32 status half-configured desktop-file-utils:amd64 0.23-4ubuntu1
2019-09-28 11:40:32 status half-configured mime-support:all 3.60ubuntu1
2019-09-28 11:40:33 status half-configured hicolor-icon-theme:all 0.17-2
2019-09-28 11:40:33 status half-configured gnome-menus:amd64 3.32.0-1ubuntu1

Now, we can’t go typing out the names of all these packages. That would take a lot of time and besides, its probably just one or two of these packages that are causing the boot issue. We could analyse each package and see which specific packages need to be reinstalled, but this is, again too much effort! Instead, lets just reinstall them all!

Now, to filter just the package names, lets use awk. Awk is a Unix utility that is used commonly for manipulating text data and picking up regular expressions and patterns. It works quite well for our purpose of picking up data from a log file. From the grep output above, we see that the package name is listed in the last (5th) column. So, lets just pick that up using awk:

cat /var/log/dpkg.log | grep "half-configured" | awk '{print " "$5""}'

The output of this command might look something like this:

mime-support:all
hicolor-icon-theme:all
gnome-menus:amd64
libxapian30:amd64
libsigc++-2.0-0v5:amd64
aptitude-common:all
libboost-iostreams1.67.0:amd64
libcwidget3v5:amd64
aptitude:amd64
man-db:amd64
libc-bin:amd64

Now, before we go ahead and plug this into our apt-get command, lets take this opportunity to ditch apt-get and go for aptitude. This is a high level package manager and can do a lot more that apt-get. Interestingly, aptitude also has a text-based interactive UI. Putting all together now:

sudo aptitude reinstall `cat /var/log/dpkg.log | grep "half-configured" | awk '{print " "$5""}'`

Now, after a few minutes, the reinstall should be done and with the next system reboot, the GUI should work just fine and the system will boot into the desktop.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s