Skip to content

Commit

Permalink
cmdmod: add 'binds' parameter in run_chroot
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aplanas committed Feb 28, 2019
1 parent aeceb1d commit b6a4394
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions salt/modules/cmdmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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::
Expand Down Expand Up @@ -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')):
Expand Down Expand Up @@ -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'))
Expand Down

0 comments on commit b6a4394

Please sign in to comment.