summaryrefslogtreecommitdiffstats
path: root/src/virtBootstrap/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/virtBootstrap/utils.py')
-rw-r--r--src/virtBootstrap/utils.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/virtBootstrap/utils.py b/src/virtBootstrap/utils.py
index a65d3f5..e1e681c 100644
--- a/src/virtBootstrap/utils.py
+++ b/src/virtBootstrap/utils.py
@@ -32,6 +32,7 @@ import tempfile
import logging
from subprocess import CalledProcessError, PIPE, Popen
+import passlib.hosts
# pylint: disable=invalid-name
# Create logger
@@ -331,6 +332,38 @@ def str2float(element):
return None
+def set_root_password(rootfs, password):
+ """
+ Set password on the root user within root filesystem
+ """
+ shadow_file = os.path.join(rootfs, "etc/shadow")
+
+ shadow_file_permissions = os.stat(shadow_file)[0]
+ # Set read-write permissions to shadow file
+ # 438 = 0110110110 = -rw-rw-rw-
+ os.chmod(shadow_file, 438)
+ try:
+ with open(shadow_file) as orig_file:
+ shadow_content = orig_file.read().split('\n')
+
+ for index, line in enumerate(shadow_content):
+ if line.startswith('root'):
+ line_split = line.split(':')
+ line_split[1] = passlib.hosts.linux_context.hash(password)
+ shadow_content[index] = ':'.join(line_split)
+ break
+
+ with open(shadow_file, "w") as new_file:
+ new_file.write('\n'.join(shadow_content))
+
+ except Exception:
+ raise
+
+ finally:
+ # Restore original permissions
+ os.chmod(shadow_file, shadow_file_permissions)
+
+
def write_progress(prog):
"""
Write progress output to console