This wasn't much of a problem under Ubuntu 14.04, but since switching to 16.04 things have gotten a bit messy:
When I use a tool to start a new VM and run a provisioning script (think Vagrant or Packer) one of the first things the script does is an apt-get update
/upgrade
/install
dance. I've noticed since switching to Ubuntu Xenial that these scripts are throwing a lot of non-deterministic errors like
Reading package lists...
E: Could not get lock /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable)
E: Unable to lock directory /var/lib/apt/lists/
and
E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?
If I throw a sleep 30
or similar at the beginning of the script it seems to settle down. It also works if I try each command in a loop that retries until the exit status is 0
. Best I can figure, there is some first-boot task that runs apt-get at the same time my provisioner does.
I thought it was something obvious, like the apt-daily service. But adding the following wait loop has no effect; it never waits for anything:
while systemctl status apt-daily.service > /dev/null 2>&1; do
sleep 0.5
done
I know I can follow the ideas in this question to check for an apt lock specifically, but I'm more interested if there is a more general-purpose and bulletproof way to determine when a new VM has come up fully. Doing a blind sleep
seems like way too fragile a hack.