From b6a4394002574696011f67232ef36536c48bef5d Mon Sep 17 00:00:00 2001 From: Alberto Planas Date: Wed, 27 Feb 2019 16:49:43 +0100 Subject: [PATCH] cmdmod: add 'binds' parameter in run_chroot Sometimes we want to export directories from the system to the chroot environment. For example, some services will require the presence of /run inside the chroot, to request some PID information. This patch add a new 'binds' parameter in the run_chroot function, that will export those system directories into the chroot. --- salt/modules/cmdmod.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/salt/modules/cmdmod.py b/salt/modules/cmdmod.py index d31cd62376de..75bfd94d5122 100644 --- a/salt/modules/cmdmod.py +++ b/salt/modules/cmdmod.py @@ -3069,6 +3069,7 @@ def run_chroot(root, group=None, shell=DEFAULT_SHELL, python_shell=True, + binds=None, env=None, clean_env=False, template=None, @@ -3130,6 +3131,9 @@ def run_chroot(root, arguments. Set to True to use shell features, such as pipes or redirection. + :param list binds: List of directories that will be exported inside + the chroot with the bind option. + :param dict env: Environment variables to be set prior to execution. .. note:: @@ -3240,6 +3244,15 @@ def run_chroot(root, 'sysfs', fstype='sysfs') + binds = binds if binds else [] + for bind_exported in binds: + bind_exported_to = os.path.relpath(bind_exported, os.path.sep) + bind_exported_to = os.path.join(root, bind_exported_to) + __salt__['mount.mount']( + bind_exported_to, + bind_exported, + opts='default,bind') + # Execute chroot routine sh_ = '/bin/sh' if os.path.isfile(os.path.join(root, 'bin/bash')): @@ -3292,6 +3305,11 @@ def run_chroot(root, log.error('Processes running in chroot could not be killed, ' 'filesystem will remain mounted') + for bind_exported in binds: + bind_exported_to = os.path.relpath(bind_exported, os.path.sep) + bind_exported_to = os.path.join(root, bind_exported_to) + __salt__['mount.umount'](bind_exported_to) + __salt__['mount.umount'](os.path.join(root, 'sys')) __salt__['mount.umount'](os.path.join(root, 'proc')) __salt__['mount.umount'](os.path.join(root, 'dev'))