From 01ae8a528e2f0cd04179a7a66e0487925d9bbf06 Mon Sep 17 00:00:00 2001 From: Christian Kellner Date: Thu, 4 Jun 2020 10:41:20 +0200 Subject: [PATCH] stages/rpm: cleanup /etc/kernel again if oprhaned If the run of dracut has been disabled via installing a link to /dev/null in /etc/kernel/install.d and after the installation of the packages the /etc/kernel directory is not owned by any newly installed packages, remove it again. --- stages/org.osbuild.rpm | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/stages/org.osbuild.rpm b/stages/org.osbuild.rpm index b989e9c3..dd40eb8b 100755 --- a/stages/org.osbuild.rpm +++ b/stages/org.osbuild.rpm @@ -184,6 +184,25 @@ def enable_dracut(masked_files): os.unlink(path) +def remove_unowned_etc_kernel(tree): + # if installed, /etc/kernel is owned by systemd-udev; but + # in case the directory is un-owned, remove it again + res = subprocess.run(["rpm", + "--root", tree, + "-qf", "/etc/kernel"], + stdout=subprocess.PIPE, + check=False) + owner = res.stdout.strip() + + if res == 0 and owner: + print(f"/etc/kernel is owned by {owner}") + return + + with contextlib.suppress(OSError): + os.rmdir("etc/kernel/install.d") + os.rmdir("etc/kernel") + + def parse_input(inputs): packages = inputs["packages"] path = packages["path"] @@ -263,6 +282,7 @@ def main(tree, inputs, options): # re-enabled dracut if no_dracut: enable_dracut(masked_files) + remove_unowned_etc_kernel(tree) # remove temporary machine ID if it was created by us if not machine_id_set_previously: