From b3120f7fefbb772b8fd5f5e8d32ee5377d4aa5cf Mon Sep 17 00:00:00 2001
From: sxt1001 <shixuantong1@huawei.com>
Date: Wed, 13 Nov 2024 23:15:39 +0800
Subject: [PATCH] chore: set recursive=False for ensure_dir if parent path is
 "/" (#5816)

---
 cloudinit/util.py | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/cloudinit/util.py b/cloudinit/util.py
index 8025f4d51..e2f04a402 100644
--- a/cloudinit/util.py
+++ b/cloudinit/util.py
@@ -1884,7 +1884,11 @@ def ensure_dir(path, mode=None, user=None, group=None):
         # Get non existed parent dir first before they are created.
         non_existed_parent_dir = get_non_exist_parent_dir(path)
         # Make the dir and adjust the mode
-        with SeLinuxGuard(os.path.dirname(path), recursive=True):
+        dir_name = os.path.dirname(path)
+        selinux_recursive = True
+        if dir_name == "/":
+            selinux_recursive = False
+        with SeLinuxGuard(dir_name, recursive=selinux_recursive):
             os.makedirs(path)
         chmod(path, mode)
         # Change the ownership
-- 
2.27.0

