for some reference, i booted a vm with a modified cloud-init-
local.service and an additional file /usr/bin/python-import-logger .
see below for those files.

The first line in the file below is the 'cp /proc/uptime' that runs
before the python program (meant to show the uptime before python starts
at all). The thing to note is that PYTHONHASHSEED does improve immediate
startup, but 'import random' basically causes a huge delay even if it is
set.  The reason to point that out is that cloud-init uses tempfile and
tempfile imports random.

after boot, catting /run/python-logger.log shows:
4.74 1.23
[4.84] start sys
[4.84] end sys (0.00)
[4.84] start random
[31.20] end random (26.36)
[31.20] start os
[31.20] end os (0.00)
[31.20] start tempfile
[31.22] end tempfile (0.02)
That was with args: /usr/bin/python-import-logger
  PYTHONHASHSEED=0

and

4.68 1.23
[27.13] start sys
[27.13] end sys (0.00)
[27.13] start random
[27.16] end random (0.03)
[27.16] start os
[27.16] end os (0.00)
[27.16] start tempfile
[27.18] end tempfile (0.02)
That was with args: /usr/bin/python-import-logger
  PYTHONHASHSEED=none-set

$ cat /lib/systemd/system/cloud-init-local.service
[Unit]
Description=Initial cloud-init job (pre-networking)
DefaultDependencies=no
Wants=local-fs.target
Wants=network-pre.target
After=local-fs.target
Conflicts=shutdown.target
Before=network-pre.target
Before=shutdown.target

[Service]
Type=oneshot
#Environment=PYTHONHASHSEED=0
ExecStart=/bin/cp /proc/uptime /run/python-logger.log
ExecStart=/usr/bin/python3 /usr/bin/python-import-logger
ExecStart=/usr/bin/cloud-init init --local
ExecStart=/bin/touch /run/cloud-init/network-config-ready
RemainAfterExit=yes
TimeoutSec=0

# Output needs to appear in instance console output
StandardOutput=journal+console

[Install]
WantedBy=cloud-init.target

$ cat /usr/bin/python-import-logger
#!/usr/bin/python3

LOG="/run/python-logger.log"
MODS=("sys", "random", "os", "tempfile")
start_fmt = "[%3.2f] start %s"
end_fmt = "[%3.2f] end %s (%2.2f)"

def read_up():
    with open("/proc/uptime", "r") as ufp:
        return float(ufp.read().partition(" ")[0])

def msg(comment, fp=None):
    if fp:
        fp.write(comment + "\n")
    print(comment)

with open(LOG, "a") as fp:
    s = read_up()
    msg(start_fmt % (s, "baseline"))
    e = read_up()
    msg(end_fmt % (e, "baseline", e - s))
    for name in MODS:
        s = read_up()
        msg(start_fmt % (read_up(), name), fp)
        __import__(name)
        e = read_up()
        msg(end_fmt % (e, name, e - s), fp)

    import sys
    import os
    msg("That was with args: %s" % ' '.join(sys.argv), fp)
    msg("  PYTHONHASHSEED=%s" % os.environ.get("PYTHONHASHSEED", "none-set"), 
fp)

-- 
You received this bug notification because you are a member of Ubuntu
Bugs, which is subscribed to Ubuntu.
https://bugs.launchpad.net/bugs/1584147

Title:
  cloud-init hangs on boot as Python waits for sufficient randomness to
  start

To manage notifications about this bug go to:
https://bugs.launchpad.net/cloud-images/+bug/1584147/+subscriptions

-- 
ubuntu-bugs mailing list
[email protected]
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs

Reply via email to