[virt-bootstrap] [PATCH v6 12/26] Make set_root_password_in_rootfs reusable

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Make the code for setting password hash in the content of shadow file
reusable and hence can be used with qcow2 output format.
---
 src/virtBootstrap/utils.py | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/src/virtBootstrap/utils.py b/src/virtBootstrap/utils.py
index 7604766..9ee83e4 100644
--- a/src/virtBootstrap/utils.py
+++ b/src/virtBootstrap/utils.py
@@ -363,6 +363,19 @@ def str2float(element):
         return None
 
 
+def set_password_in_shadow_content(shadow_content, password, user='root'):
+    """
+    Find a user the content of shadow file and set a hash of the password.
+    """
+    for index, line in enumerate(shadow_content):
+        if line.startswith(user):
+            line_split = line.split(':')
+            line_split[1] = passlib.hosts.linux_context.hash(password)
+            shadow_content[index] = ':'.join(line_split)
+            break
+    return shadow_content
+
+
 def set_root_password_in_rootfs(rootfs, password):
     """
     Set password on the root user within root filesystem
@@ -377,15 +390,10 @@ def set_root_password_in_rootfs(rootfs, password):
         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
+        new_content = set_password_in_shadow_content(shadow_content, password)
 
         with open(shadow_file, "w") as new_file:
-            new_file.write('\n'.join(shadow_content))
+            new_file.write('\n'.join(new_content))
 
     except Exception:
         raise
-- 
2.13.5

_______________________________________________
virt-tools-list mailing list
virt-tools-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/virt-tools-list



[Index of Archives]     [Linux Virtualization]     [KVM Development]     [CentOS Virtualization]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite Forum]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]     [Video 4 Linux]

  Powered by Linux