9

As part of my custom (in-house) .deb file that will be deployed to Ubuntu 12.04 devices, I need to add several PPA repositories, run apt-get update, and then install packages from those PPAs.

Can this be done within the .preinst or .postinst scripts in my .deb file?

I assume that when my .deb file is installed (sudo dpkg -i testing.deb) that some locks or mechanisms likely exist which would prevent me from calling certain commands such as add-apt-repository, apt-get update, and apt-get install.

Is this a correct assumption?

1

2 Answers 2

7

You are correct. Maintainer scripts are not supposed to make any changes to the apt or dpkg states, other than the changes that will be made when installing the package anyway.

Installing a file into /etc/apt/sources.list.d, though, is fine. Most packages that fill a role like "install this package to add the APT repository X to your system" simply do that (as opposed to calling add-apt-repository). It's also common to drop the GPG public key used to sign the repo into /etc/apt/trusted.gpg.d.

If you want to make it simple for the user to perform the additional installation steps for your software, you might just want to ship a script which does the job, and tell the user to run the script manually.

But if you really, really want to call apt-get update or apt-get install, etc automatically, and you don't mind that your package will be wholly unacceptable in Debian or Ubuntu proper, and your users are ok with the package acting that way, then you could perhaps put something in /etc/cron.d which checks for any existing apt or dpkg locks, and if none are held, performs your additional installation steps and arranges for those steps not to be performed again. I don't recommend that approach.

0

Yes, you should not do this -- but if you have to -- here is how you could call apt-get from inside preinst script. The same approach would work for calling dpkg directly.

The idea is to launch a subshell which patiently waits in the background until all install activity has ended and then does its thing.

((
   # wait for install to finish
   while pgrep -x 'dpkg|apt|apt-get' > /dev/null; do sleep 1; done
   export DEBIAN_FRONTEND=noninteractive
   # make sure no one else is locking the dpkg database
   flock --exclusive --close /var/lib/dpkg/lock \
       apt-get install --assume-yes my-fancy-package
) 2>&1 >/dev/null </dev/null &)

Obviously this will cause problems if there are multiple packages which try this trick. So as I said above, you should really not do this.

You must log in to answer this question.

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