Correct way to create /etc/machine-id from python
Question: I want to create systemd's `/etc/machine-id` from a python script instead of
using `systemd-machine-id-setup`. I am preparing a rootfs and I don't want to
depend on systemd being installed on the host.

My current code looks like this:

    
    
    from uuid import uuid4
    f = open("machine-id", "w")
    f.write(uuid4().hex + "\n")
    f.close()
    

Is this the correct way of doing it? Is it the correct byteorder? Did I miss
anything?

If needed, please provide correct code and/or references.


Answer: Yes. See the
[Documentation](http://www.freedesktop.org/software/systemd/man/machine-
id.html). Its a text file so byteorder is not relevant here. Also see the code
that handles/generates it: The `make_v4_uuid` function and the place where it
is called from at line 221 in
<https://github.com/systemd/systemd/blob/master/src/libsystemd/sd-id128/sd-
id128.c>

Although personally I would make it:

    
    
    from uuid import uuid4
    with open("machine-id", "w") as f:
        f.write(uuid4().hex + "\n")
    

