diff options
author | Radostin Stoyanov <rstoyanov1@gmail.com> | 2017-07-24 09:14:05 +0100 |
---|---|---|
committer | Radostin Stoyanov <rstoyanov1@gmail.com> | 2017-07-24 15:01:36 +0100 |
commit | 5853bf681d2b18bf51f997526b3d3934345d0b4c (patch) | |
tree | 97ee528d6b34945a9ef916e38411927049f7203f /src/virtBootstrap/utils.py | |
parent | f4abf17432983c4ce539bbd4cadf0645a7c4154a (diff) | |
download | virt-bootstrap.git-5853bf681d2b18bf51f997526b3d3934345d0b4c.tar.gz virt-bootstrap.git-5853bf681d2b18bf51f997526b3d3934345d0b4c.tar.xz virt-bootstrap.git-5853bf681d2b18bf51f997526b3d3934345d0b4c.zip |
Add support for setting root passowrd in qcow2
For containers bootstrapped in qcow2 format use the tool 'virt-edit'
to insert the hashed root password in the shadow file of the last layer
Diffstat (limited to 'src/virtBootstrap/utils.py')
-rw-r--r-- | src/virtBootstrap/utils.py | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/src/virtBootstrap/utils.py b/src/virtBootstrap/utils.py index e1e681c..2c79d6b 100644 --- a/src/virtBootstrap/utils.py +++ b/src/virtBootstrap/utils.py @@ -30,6 +30,7 @@ import os import sys import tempfile import logging +import re from subprocess import CalledProcessError, PIPE, Popen import passlib.hosts @@ -332,7 +333,7 @@ def str2float(element): return None -def set_root_password(rootfs, password): +def set_root_password_in_rootfs(rootfs, password): """ Set password on the root user within root filesystem """ @@ -364,6 +365,29 @@ def set_root_password(rootfs, password): os.chmod(shadow_file, shadow_file_permissions) +def set_root_password_in_image(image, password): + """ + Set password on the root user within image + """ + password_hash = passlib.hosts.linux_context.hash(password) + execute(['virt-edit', + '-a', image, '/etc/shadow', + '-e', 's,^root:.*?:,root:%s:,' % re.escape(password_hash)]) + + +def set_root_password(fmt, dest, root_password): + """ + Set root password + """ + if fmt == "dir": + set_root_password_in_rootfs(dest, root_password) + elif fmt == "qcow2": + layers = [layer for layer in os.listdir(dest) + if layer.startswith('layer-')] + set_root_password_in_image(os.path.join(dest, max(layers)), + root_password) + + def write_progress(prog): """ Write progress output to console |