Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to list all the packages that uses a given file. I'm willing to use any tool (such as dpkg, dlocate, apt-file etc.), Please do mind when I say 'use' I don't mean necessarily only owns/installs the file, I'd like to know which package also reference it, link against it etc.

here's my use case:

I have installed both KVM and VirtualBox and ended up using only VirtualBox, as it turns out VirtualBox and KVM competes for resources of some sort, fortunately enough for me I don't use KVM so I just uninstalled it.

by issuing:

sudo apt-get purge kvm

But it wasn't enough and when I tried to run VirtualBox on a subsequent boot I got the following error:

Error: failed to start machine. Error message: VirtualBox can't operate in VMX root mode. Please disable the KVM kernel extension, recompile your kernel and reboot (VERR_VMX_IN_VMX_ROOT_MODE)

looking at the loaded modules revealed that the kvm kernel modules were still loaded

mcradle:~> lsmod | grep -i kvm
kvm_intel              39416  0
kvm                   244969  1 kvm_intel

tuned to a web search which suggested to purge qemu-kvm

sudo apt-get purge qemu-kvm

this did the trick, now my question is: how could I have gotten to this conclusion systematically without the web-magic-dust? In particular It would be nice to know how can one tell which package depends on a given file?

Please do mind that dlocate -S is not good enough because it shows the file's owner package and I wanted to find the package that causes 'kvm.ko' to be loaded!

mcradle:/etc# dlocate -S kvm.ko
linux-image-2.6.32-25-generic-pae: /lib/modules/2.6.32-25-generic-pae/kernel/arch/x86/kvm/kvm.ko

if 'apt-rdepends -r' would have accepted files instead of packages that could have been exactly what I'm after.

Thanks in advance and I hope the question is clear.

share|improve this question
this question might be a better fit on askubuntu – RobotHumans Nov 28 '10 at 16:20
@aking1012: This question is not specific to Ubuntu; the same package management tools exist on all Debian derivatives, and the underlying issue applies to any Linux distribution and beyond. So while it is on-topic on Ask Ubuntu, it's a better fit here. – Gilles Nov 28 '10 at 18:46
up vote 1 down vote accepted

There is no general way of finding packages that use a given file. You can combine dpkg -S or dlocate to find what package contains a file, with apt-rdepends to find what packages depend on that package. But that won't cover all cases, and it won't tell you which files each package depends on (not that this is really meaningful: dependencies apply to packages as a whole, and often they can't be pinpointed to a particular file in the depended-upon package).

One case you won't be able to find through dependencies is dependencies on essential packages (those for which the metadata includes Essential: yes). Every system must have the complete collection of essential packages, or if you like every package is assumed to depend on every essential package. Case in point: kvm.ko is a kernel module, belonging to the kernel package, which is essential.

Another case you can't always find through dependencies is plug-ins. Many programs will load whatever plug-ins they find installed, and will run even if the plug-in is not there, so the program usually declares no dependency on the plug-in. (The plug-in may declare a dependency on some kind of plug-in framework that the package providing the main program executable depends on, but that's not always the case.) Case in point: the kvm kernel module is an optional component of the kvm virtual machine; you can run /usr/bin/kvm even if the kvm kernel module is not loaded, it'll just be slower.

Here the message from VirtualBox is so generic and playing-it-safe that it's misleading. The message assumes that the KVM extension is compiled into the kernel, but on your system it's really a module that can be unloaded. Merely unloading the module is enough to satisfy VirtualBox: just run rmmod kvm_amd kvm_intel kvm. You can also keep the qemu-kvm package from loading the kvm module by editing /etc/init.d/qemu-kvm or /etc/init/qemu-kvm.conf (depending whether your Ubuntu release has a SysVinit script or an Upstart script) and commenting out the module-loading line, or preventing the service from starting at boot time.

share|improve this answer
Yes I know how to solve the problem presented (in my case purging kvm and qemu-kvm was good enough), I wanted to understand what is that I don't know and equip myself with the tools should I face a similar problem in the future, and I'm still not sure for example how to tell which package causes a module to load (perhaps this should have been my question) – Mcradle Nov 29 '10 at 9:29
@Mcradle: The main point of my answer is that there is no fully general way. Any program could potentially cause a module to load at any point (for example, if qemu had been engineered slightly differently, it might have loaded the kvm module from the qemu executable instead of from a boot script). – Gilles Nov 29 '10 at 19:35
I agree, but why does qemu even assumes that the kvm modules are compiled and available? the kernel package may not be even enabling the drivers at all. – Mcradle Nov 29 '10 at 22:32
@Mcradle: It doesn't assume that the modules are there, it just tries to load them (doing nothing if they aren't there or aren't supported by the hardware). – Gilles Nov 29 '10 at 22:51
so this fully demonstrates your point of 'opportunistic usage' of other packages files without registering this as a dependency anywhere. I totally get it. – Mcradle Nov 29 '10 at 22:56

Web pixie dust? By their powers combined, a team of package tools will shine a blinding light and make the darkness recoil!

  1. use dlocate -S to find the owner package
  2. use aptitude remove -sy $pkg to find out all dependent packages (-syD to add detail; aptitude why to find just one dependency chain).

Or in one step:

  1. aptitude remove -sy $(dlocate -S /path/to/file |sed 's#:.*##' |uniq )
share|improve this answer
thanks. I don't think it's going to work for the case I've presented because 'dlocate -S' returns 'linux-image-2.6.32-25-generic-pae' which is the kernel package that I don't want to uninstall (one would imagine this is either impossible or bares severe consequences) – Mcradle Nov 29 '10 at 9:24
-sy is in fact simulating an uninstall, and tells you what depends on the package. It runs without root privileges. – Tobu Nov 29 '10 at 18:21
pretty cool trick thanks! – Mcradle Nov 29 '10 at 22:34

apt-file should work for that. I haven’t tested that

share|improve this answer
Thanks for the answe. I think that 'apt-file search /lib/modules/2.6.32-25-generic-pae/kernel/arch/x86/kvm/kvm.ko' is only showing the package that installed the file, apt-file does not seem to have other action that would be of interest. – Mcradle Nov 28 '10 at 17:01
Sorry I was in hurt and doesn’t read well – zzeroo Nov 28 '10 at 20:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .