[elbe-devel] [PATCH 7/7] elbepack: efilesystem: clean up custom /usr/sbin/policy-rc.d

Thomas Weißschuh thomas.weissschuh at linutronix.de
Tue Dec 17 13:24:30 CET 2024


Use the excursion machinery to clean up the injected policy-rc.d script.

Closes: https://github.com/Linutronix/elbe/issues/423
Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 elbepack/efilesystem.py               | 24 ++++++++++++++++++++++--
 newsfragments/+policy-rc.bugfix.rst   |  1 +
 tests/simple-validation-image-test.py | 13 +++++++++++++
 tests/simple-validation-image.xml     |  1 +
 4 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/elbepack/efilesystem.py b/elbepack/efilesystem.py
index 171ff939629b3681a43efba9efc7547484682a25..ec5eb6a9fa026617e5c9945f18bffc2381c39c30 100644
--- a/elbepack/efilesystem.py
+++ b/elbepack/efilesystem.py
@@ -10,6 +10,7 @@ import os
 import pathlib
 import shutil
 import subprocess
+import tempfile
 import time
 
 from elbepack.filesystem import Filesystem
@@ -313,6 +314,23 @@ class _ExcursionContext:
             self._ended = True
 
 
+class _SimpleTmpFile:
+    def __init__(self, contents, mode):
+        self.contents = contents
+        self.mode = mode
+
+    def __enter__(self):
+        handle, path = tempfile.mkstemp()
+        self.path = pathlib.Path(path)
+        self.path.write_text(self.contents)
+        self.path.chmod(self.mode)
+
+        return self.path
+
+    def __exit__(self, typ, value, traceback):
+        self.path.unlink()
+
+
 class ChRootFilesystem(ElbeFilesystem):
 
     def __init__(self, path, interpreter=None, clean=False):
@@ -327,15 +345,17 @@ class ChRootFilesystem(ElbeFilesystem):
     def __enter__(self):
         self._exitstack = contextlib.ExitStack()
 
+        policy_rc_d = self._exitstack.enter_context(
+                _SimpleTmpFile('#!/bin/sh\nexit 101\n', 0o755))
+
         excursions = [
             Excursion('/etc/resolv.conf'),
             Excursion('/etc/apt/apt.conf'),
-            Excursion('/usr/sbin/policy-rc.d'),
+            Excursion(str(policy_rc_d), dst='/usr/sbin/policy-rc.d'),
         ]
 
         self.mkdir_p('usr/bin')
         self.mkdir_p('usr/sbin')
-        self.write_file('usr/sbin/policy-rc.d', 0o755, '#!/bin/sh\nexit 101\n')
 
         if self.interpreter:
             ui = '/usr/share/elbe/qemu-elbe/' + self.interpreter
diff --git a/newsfragments/+policy-rc.bugfix.rst b/newsfragments/+policy-rc.bugfix.rst
new file mode 100644
index 0000000000000000000000000000000000000000..6ae14c2ee85079f08fcfb2e25a3912b3ee86e5b1
--- /dev/null
+++ b/newsfragments/+policy-rc.bugfix.rst
@@ -0,0 +1 @@
+Remove /usr/sbin/policy-rc.d which is used at build time from final image.
diff --git a/tests/simple-validation-image-test.py b/tests/simple-validation-image-test.py
index c20e7c4ff8a6b6a6c4d112e826458d80b447efa6..0728e8a87a772837484999d23f3afe2976460880 100755
--- a/tests/simple-validation-image-test.py
+++ b/tests/simple-validation-image-test.py
@@ -205,6 +205,18 @@ def _test_archive(root):
     assert archive_file_stat.st_gid == 0
 
 
+def _test_excursions(root):
+    assert not root.joinpath('usr', 'sbin', 'policy-rc.d').exists()
+
+    copy = root.joinpath('usr', 'sbin', 'policy-rc.d.copy')
+    assert copy.exists()
+    assert copy.stat().st_mode & 0o777 == 0o755
+    assert copy.read_text().strip() == textwrap.dedent("""
+    #!/bin/sh
+    exit 101
+    """).strip()
+
+
 def _test_rfs_partition(build_dir, part):
     assert part.number == 1
     assert part.start == 1 * 1024 * 1024
@@ -260,6 +272,7 @@ def _test_rfs_partition(build_dir, part):
         _test_generated_elbe_files(build_dir, root)
         _test_finetuning(root)
         _test_archive(root)
+        _test_excursions(root)
 
 
 def test_image(build_dir):
diff --git a/tests/simple-validation-image.xml b/tests/simple-validation-image.xml
index 2fe79ea932efb665cc124a97ccb0ddfbb1e38645..9fe51497ecdf419354a072d14d7ce62b595a3c35 100644
--- a/tests/simple-validation-image.xml
+++ b/tests/simple-validation-image.xml
@@ -79,6 +79,7 @@ SPDX-FileCopyrightText: Linutronix GmbH
 
 			<command>cat /etc/hosts | cat -n > /etc/hosts4</command>
 			<raw_cmd>cp /etc/hosts /etc/hosts5</raw_cmd>
+			<command>cp /usr/sbin/policy-rc.d /usr/sbin/policy-rc.d.copy</command>
 		</finetuning>
 
 		<project-finetuning>

-- 
2.47.1



More information about the elbe-devel mailing list