51b5b9
From 88d6ceb18e17c5a18bafb5092ae0c22241b212df Mon Sep 17 00:00:00 2001
51b5b9
From: Mark Reynolds <mreynolds@redhat.com>
51b5b9
Date: Mon, 1 Nov 2021 14:01:11 -0400
51b5b9
Subject: [PATCH 08/12] Issue 4978 - make installer robust
51b5b9
51b5b9
Description:  When run in a container the server can fail to start
51b5b9
              because the installer sets the db_home_dir to /dev/shm,
51b5b9
              but in containers the default size of /dev/shm is too
51b5b9
              small for libdb. We should detect if we are in a
51b5b9
              container and not set db_home_dir to /dev/shm.
51b5b9
51b5b9
              During instance removal, if an instance was not properly
51b5b9
              created then it can not be removed either. Make the
51b5b9
              uninstall more robust to accept some errors and continue
51b5b9
              removing the instance.
51b5b9
51b5b9
relates: https://github.com/389ds/389-ds-base/issues/4978
51b5b9
51b5b9
Reviewed by: firstyear & tbordaz(Thanks!)
51b5b9
---
51b5b9
 src/lib389/lib389/instance/setup.py | 9 +++++++++
51b5b9
 src/lib389/lib389/utils.py          | 5 ++++-
51b5b9
 2 files changed, 13 insertions(+), 1 deletion(-)
51b5b9
51b5b9
diff --git a/src/lib389/lib389/instance/setup.py b/src/lib389/lib389/instance/setup.py
51b5b9
index be6854af8..7b0147cf9 100644
51b5b9
--- a/src/lib389/lib389/instance/setup.py
51b5b9
+++ b/src/lib389/lib389/instance/setup.py
51b5b9
@@ -731,6 +731,15 @@ class SetupDs(object):
51b5b9
             for line in template_dse.readlines():
51b5b9
                 dse += line.replace('%', '{', 1).replace('%', '}', 1)
51b5b9
 
51b5b9
+        # Check if we are in a container, if so don't use /dev/shm for the db home dir
51b5b9
+        # as containers typically don't allocate enough space for dev/shm and we don't
51b5b9
+        # want to unexpectedly break the server after an upgrade
51b5b9
+        container_result = subprocess.run(["systemd-detect-virt", "-c"], capture_output=True)
51b5b9
+        if container_result.returncode == 0:
51b5b9
+            # In a container, set the db_home_dir to the db path
51b5b9
+            self.log.debug("Container detected setting db home directory to db directory.")
51b5b9
+            slapd['db_home_dir'] = slapd['db_dir']
51b5b9
+
51b5b9
         with open(os.path.join(slapd['config_dir'], 'dse.ldif'), 'w') as file_dse:
51b5b9
             dse_fmt = dse.format(
51b5b9
                 schema_dir=slapd['schema_dir'],
51b5b9
diff --git a/src/lib389/lib389/utils.py b/src/lib389/lib389/utils.py
51b5b9
index 5ba0c6676..c63b4d0ee 100644
51b5b9
--- a/src/lib389/lib389/utils.py
51b5b9
+++ b/src/lib389/lib389/utils.py
51b5b9
@@ -266,6 +266,8 @@ def selinux_label_port(port, remove_label=False):
51b5b9
     :type remove_label: boolean
51b5b9
     :raises: ValueError: Error message
51b5b9
     """
51b5b9
+    if port is None:
51b5b9
+        return
51b5b9
     try:
51b5b9
         import selinux
51b5b9
     except ImportError:
51b5b9
@@ -662,7 +664,8 @@ def isLocalHost(host_name):
51b5b9
         Uses gethostbyname()
51b5b9
     """
51b5b9
     # first see if this is a "well known" local hostname
51b5b9
-    if host_name == 'localhost' or \
51b5b9
+    if host_name is None or \
51b5b9
+       host_name == 'localhost' or \
51b5b9
        host_name == 'localhost.localdomain' or \
51b5b9
        host_name == socket.gethostname():
51b5b9
         return True
51b5b9
-- 
51b5b9
2.31.1
51b5b9