How cloud-init systemd service is activated on Ubuntu 16.04 (xenial)

2016-09-23

Recently I’ve trace how cloud-init is being run. First, I try to find service file in /etc/system/system or /lib/systemd/system. Although I can locate the unit files, I can’t see any systemd target that will bring up these cloud-init services. I did’t see how cloud-init.target is activated by systemd.

Finally, I found that cloud-init project utilize the systemd generator to generate the unit files dynamically. The dynamic unit files is located at /run/system/generator.early/multi-user.target.wants/cloud-init.target. With this unit file, when the multi-user.target is activated by systemd, the cloud-init.target will also be activated. Then the systemd will further activate related cloud-init service in /etc/systemd/system/cloud-init.target.wants/.

Below is the dynamic unit file.

$ ll /run/systemd/generator.early/multi-user.target.wants/cloud-init.target
lrwxrwxrwx 1 root root 37 Jul 14 07:52 /run/systemd/generator.early/multi-user.target.wants/cloud-init.target -> /lib/systemd/system/cloud-init.target

If I want to disable cloud-init, there are several ways to do it. 1. Mask the default cloud-init systemd generator.

ln -sf /dev/null /etc/systemd/system-generator/cloud-init-generator
  1. Add kernel parameter cloud-init=disabled or touch /etc/cloud/cloud-init.disabled. This kernel parameter will be processed by cloud-init systemd generator. See http://bazaar.launchpad.net/~cloud-init-dev/cloud-init/trunk/view/head:/systemd/cloud-init.target

Ref: https://www.freedesktop.org/software/systemd/man/systemd.generator.html