4

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.

Community
  • 1
  • 1
smitelli
  • 6,835
  • 3
  • 31
  • 53

1 Answers1

6

You're on the right track with apt-daily service, at least when I experienced the same problem. But apt-daily is immediately triggering unattended-upgrades, which is probably why your sleep loop didn't do anything. Couple of ideas:

Option 1

If you are building your own vagrant box, here's how to solve this problem:

Create a new file on your base box:

/etc/systemd/system/apt-daily.timer.d/apt-daily.timer.conf

[Timer]
Persistent=false

This will override the default systemd timer that triggers apt-daily to run immediately on boot (the default persistent = true means that if the job was missed while the system was down, then it will run immediately on boot). This file will override the default settings in this file:

/lib/systemd/system/apt-daily.timer

This is what I did because I built my own vagrant box and I can confirm it works.

Option 2

Use this xenial image

https://github.com/geerlingguy/packer-ubuntu-1604

Geerlingguy solved the problem by disabling unattended-upgrades. You can see the discussion here

https://github.com/geerlingguy/packer-ubuntu-1604/issues/3#issue-154560190

agileafro
  • 61
  • 1
  • Two years later now - has any new workaround been discovered for this issue? I'm trying to do the same as OP but this box: https://app.vagrantup.com/arnemertz/boxes/Xubuntu16.04 – Taylor Liss Apr 30 '18 at 17:26