From 86467b14d7d2906549788f79b4eb57729c587811 Mon Sep 17 00:00:00 2001 From: Rishabh Dave Date: Wed, 14 Oct 2020 16:51:36 +0530 Subject: [PATCH] qa/cephfs: refactor KernelMount.mount method Make KernelMount.mount() such that it can LocalKernelMount can reuse it instead of duplicating it. For this, move subtasks required for mounting Ceph FS (like creating mountpoint directory) to a different method, so that these methods can be overriden in LocalKernelMount as per requirement. Signed-off-by: Rishabh Dave --- qa/tasks/cephfs/kernel_mount.py | 81 +++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 34 deletions(-) diff --git a/qa/tasks/cephfs/kernel_mount.py b/qa/tasks/cephfs/kernel_mount.py index fc6e97dc50a..c4cd1591fb5 100644 --- a/qa/tasks/cephfs/kernel_mount.py +++ b/qa/tasks/cephfs/kernel_mount.py @@ -29,6 +29,8 @@ class KernelMount(CephFSMount): self.rbytes = config.get('rbytes', False) self.inst = None self.addr = None + self._mount_bin = ['adjust-ulimits', 'ceph-coverage', self.test_dir +\ + '/archive/coverage', '/bin/mount', '-t', 'ceph'] def mount(self, mntopts=[], check_status=True, **kwargs): self.update_attrs(**kwargs) @@ -39,33 +41,43 @@ class KernelMount(CephFSMount): if not self.cephfs_mntpt: self.cephfs_mntpt = '/' - stderr = StringIO() - try: - self.client_remote.run(args=['mkdir', '-p', self.hostfs_mntpt], - timeout=(5*60), stderr=stderr) - except CommandFailedError: - if 'file exists' not in stderr.getvalue().lower(): - raise + self._create_mntpt() retval = self._run_mount_cmd(mntopts, check_status) if retval: return retval + self._set_filemode_on_mntpt() + + self.mounted = True + + def _create_mntpt(self): stderr = StringIO() try: - self.client_remote.run( - args=['sudo', 'chmod', '1777', self.hostfs_mntpt], - stderr=stderr, timeout=(5*60)) + self.client_remote.run(args=['mkdir', '-p', self.hostfs_mntpt], + timeout=(5*60), stderr=stderr) except CommandFailedError: - # the client does not have write permissions in the caps it holds - # for the Ceph FS that was just mounted. - if 'permission denied' in stderr.getvalue().lower(): - pass + if 'file exists' not in stderr.getvalue().lower(): + raise + def _run_mount_cmd(self, mntopts, check_status): + mount_cmd = self._get_mount_cmd(mntopts) + mountcmd_stdout, mountcmd_stderr = StringIO(), StringIO() - self.mounted = True + try: + self.client_remote.run(args=mount_cmd, timeout=(30*60), + stdout=mountcmd_stdout, + stderr=mountcmd_stderr, omit_sudo=False) + except CommandFailedError as e: + log.info('mount command failed') + if check_status: + raise + else: + return (e, mountcmd_stdout.getvalue(), + mountcmd_stderr.getvalue()) + log.info('mount command passed') - def _run_mount_cmd(self, mntopts, check_status): + def _get_mount_cmd(self, mntopts): opts = 'norequire_active_mds' if self.client_id: opts += ',name=' + self.client_id @@ -82,27 +94,28 @@ class KernelMount(CephFSMount): if mntopts: opts += ',' + ','.join(mntopts) + mount_cmd = ['sudo'] + self._nsenter_args mount_dev = ':' + self.cephfs_mntpt - prefix = ['sudo', 'adjust-ulimits', 'ceph-coverage', - self.test_dir + '/archive/coverage', - 'nsenter', - '--net=/var/run/netns/{0}'.format(self.netns_name)] - cmdargs = prefix + ['/bin/mount', '-t', 'ceph', mount_dev, - self.hostfs_mntpt, '-v', '-o', opts] + mount_cmd += self._mount_bin + [mount_dev, self.hostfs_mntpt, '-v', + '-o', opts] - mountcmd_stdout, mountcmd_stderr = StringIO(), StringIO() + return mount_cmd + + @property + def _nsenter_args(self): + return ['nsenter', f'--net=/var/run/netns/{self.netns_name}'] + + def _set_filemode_on_mntpt(self): + stderr = StringIO() try: - self.client_remote.run(args=cmdargs, timeout=(30*60), - stdout=mountcmd_stdout, - stderr=mountcmd_stderr) - except CommandFailedError as e: - log.info('mount command failed') - if check_status: - raise - else: - return (e, mountcmd_stdout.getvalue(), - mountcmd_stderr.getvalue()) - log.info('mount command passed') + self.client_remote.run( + args=['sudo', 'chmod', '1777', self.hostfs_mntpt], + stderr=stderr, timeout=(5*60)) + except CommandFailedError: + # the client does not have write permissions in the caps it holds + # for the Ceph FS that was just mounted. + if 'permission denied' in stderr.getvalue().lower(): + pass def umount(self, force=False): if not self.is_mounted(): -- 2.39.5