Skip to content

Commit

Permalink
Create fake machine id within the runner
Browse files Browse the repository at this point in the history
Create a fake machine id before running the runner
command to avoid the annoying "Faled to resolve
specifiers in '/var/log/journal/%m': No such file or
directory" errors
  • Loading branch information
mmartinv committed Nov 14, 2023
1 parent 8cae8f5 commit 598af46
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 19 deletions.
25 changes: 25 additions & 0 deletions osbuild/util/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,31 @@ def sysusers():
sys.exit(1)


def create_machine_id_if_needed(tree=""):
"""Create a machine-id with a fake machine id if it does not exist"""
path = f"{tree}/etc/machine-id"
if os.path.exists(path):
return False

os.makedirs(f"{tree}/etc", exist_ok=True)
with open(path, "w", encoding="utf8") as f:
# create a fake machine ID to improve reproducibility
f.write("ffffffffffffffffffffffffffffffff\n")
os.fchmod(f.fileno(), 0o400)

return True


def remove_machine_id(tree=""):
"""Create a machine-id with a fake machine id if it does not exist"""
path = f"{tree}/etc/machine-id"
if not os.path.exists(path):
return
machine_id_file = pathlib.Path(path)
machine_id_file.unlink()
machine_id_file.touch(mode=0o444)


def tmpfiles():
# Allow systemd-tmpfiles to return non-0. Some packages want to create
# directories owned by users that are not set up with systemd-sysusers.
Expand Down
3 changes: 3 additions & 0 deletions runners/org.osbuild.centos9
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ from osbuild.util import runners
if __name__ == "__main__":
with api.exception_handler():
runners.ldconfig()
created_machine_id = runners.create_machine_id_if_needed()
runners.sysusers()
runners.tmpfiles()
runners.nsswitch()

r = subprocess.run(sys.argv[1:], check=False)
if created_machine_id:
runners.remove_machine_id()

sys.exit(r.returncode)
3 changes: 3 additions & 0 deletions runners/org.osbuild.fedora30
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ from osbuild.util import runners
if __name__ == "__main__":
with api.exception_handler():
runners.ldconfig()
created_machine_id = runners.create_machine_id_if_needed()
runners.sysusers()
runners.tmpfiles()
runners.nsswitch()

r = subprocess.run(sys.argv[1:], check=False)
if created_machine_id:
runners.remove_machine_id()

sys.exit(r.returncode)
3 changes: 3 additions & 0 deletions runners/org.osbuild.fedora38
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ from osbuild.util import runners
if __name__ == "__main__":
with api.exception_handler():
runners.ldconfig()
created_machine_id = runners.create_machine_id_if_needed()
runners.sysusers()
runners.tmpfiles()
runners.nsswitch()
runners.sequoia()

r = subprocess.run(sys.argv[1:], check=False)
if created_machine_id:
runners.remove_machine_id()
sys.exit(r.returncode)
3 changes: 3 additions & 0 deletions runners/org.osbuild.rhel81
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ def os_release():
if __name__ == "__main__":
with api.exception_handler():
runners.ldconfig()
created_machine_id = runners.create_machine_id_if_needed()
runners.sysusers()
runners.tmpfiles()
runners.nsswitch()
os_release()
runners.python_alternatives()

r = subprocess.run(sys.argv[1:], check=False)
if created_machine_id:
runners.remove_machine_id()
sys.exit(r.returncode)
3 changes: 3 additions & 0 deletions runners/org.osbuild.rhel82
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ from osbuild.util import runners
if __name__ == "__main__":
with api.exception_handler():
runners.ldconfig()
created_machine_id = runners.create_machine_id_if_needed()
runners.sysusers()
runners.tmpfiles()
runners.nsswitch()
Expand All @@ -18,5 +19,7 @@ if __name__ == "__main__":

r = subprocess.run(sys.argv[1:], env=env, check=False)

if created_machine_id:
runners.remove_machine_id()

sys.exit(r.returncode)
21 changes: 2 additions & 19 deletions stages/org.osbuild.rpm
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ This stage will return the following metadata via the osbuild API:
import contextlib
import json
import os
import pathlib
import subprocess
import sys
import tempfile
from operator import itemgetter

from osbuild import api
from osbuild.util.mnt import mount
from osbuild.util.runners import create_machine_id_if_needed, remove_machine_id

SCHEMA = """
"additionalProperties": false,
Expand Down Expand Up @@ -263,21 +263,6 @@ def parse_input(inputs):
return path, files


def create_machine_id_if_needed(tree):
"""Create a machine-id with a fake machine id if it does not exist"""
path = f"{tree}/etc/machine-id"
if os.path.exists(path):
return False

os.makedirs(f"{tree}/etc", exist_ok=True)
with open(path, "w", encoding="utf8") as f:
# create a fake machine ID to improve reproducibility
f.write("ffffffffffffffffffffffffffffffff\n")
os.fchmod(f.fileno(), 0o400)

return True


# pylint: disable=too-many-branches
def main(tree, inputs, options):
pkgpath, packages = parse_input(inputs)
Expand Down Expand Up @@ -380,9 +365,7 @@ def main(tree, inputs, options):
# remove temporary machine ID if it was created by us
if machine_id_created:
print("deleting the fake machine id")
machine_id_file = pathlib.Path(f"{tree}/etc/machine-id")
machine_id_file.unlink()
machine_id_file.touch(mode=0o444)
remove_machine_id(tree)

if ostree_booted:
os.unlink(ostree_booted)
Expand Down

0 comments on commit 598af46

Please sign in to comment.