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.

(cherry picked from commit b6a4394)
  • Loading branch information
aplanas committed Dec 3, 2019
1 parent daf7d4b commit 933fba5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
20 changes: 20 additions & 0 deletions salt/modules/cmdmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -2904,6 +2904,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 @@ -2963,6 +2964,11 @@ 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.
.. versionadded:: Sodium
:param dict env: Environment variables to be set prior to execution.
.. note::
Expand Down Expand Up @@ -3059,6 +3065,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 @@ -3109,6 +3124,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
30 changes: 30 additions & 0 deletions tests/unit/modules/test_cmdmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,3 +433,33 @@ def test_run_all_output_encoding(self):
ret = cmdmod.run_all('some command', output_encoding='latin1')

self.assertEqual(ret['stdout'], stdout)

def test_run_chroot_mount(self):
'''
Test cmdmod.run_chroot mount / umount balance
'''
mock_mount = MagicMock()
mock_umount = MagicMock()
mock_run_all = MagicMock()
with patch.dict(cmdmod.__salt__, {
'mount.mount': mock_mount,
'mount.umount': mock_umount}):
with patch('salt.modules.cmdmod.run_all', mock_run_all):
cmdmod.run_chroot('/mnt', 'cmd')
self.assertEqual(mock_mount.call_count, 3)
self.assertEqual(mock_umount.call_count, 3)

def test_run_chroot_mount_bind(self):
'''
Test cmdmod.run_chroot mount / umount balance with bind mount
'''
mock_mount = MagicMock()
mock_umount = MagicMock()
mock_run_all = MagicMock()
with patch.dict(cmdmod.__salt__, {
'mount.mount': mock_mount,
'mount.umount': mock_umount}):
with patch('salt.modules.cmdmod.run_all', mock_run_all):
cmdmod.run_chroot('/mnt', 'cmd', binds=['/var'])
self.assertEqual(mock_mount.call_count, 4)
self.assertEqual(mock_umount.call_count, 4)

0 comments on commit 933fba5

Please sign in to comment.