]> git.proxmox.com Git - ceph.git/blob - ceph/qa/tasks/kclient.py
be75286bd0dc81be6181eb15cd8a64e0af0b7bce
[ceph.git] / ceph / qa / tasks / kclient.py
1 """
2 Mount/unmount a ``kernel`` client.
3 """
4 import contextlib
5 import logging
6
7 from teuthology.misc import deep_merge
8 from teuthology.orchestra.run import CommandFailedError
9 from teuthology import misc
10 from teuthology.contextutil import MaxWhileTries
11 from tasks.cephfs.kernel_mount import KernelMount
12
13 log = logging.getLogger(__name__)
14
15 @contextlib.contextmanager
16 def task(ctx, config):
17 """
18 Mount/unmount a ``kernel`` client.
19
20 The config is optional and defaults to mounting on all clients. If
21 a config is given, it is expected to be a list of clients to do
22 this operation on. This lets you e.g. set up one client with
23 ``ceph-fuse`` and another with ``kclient``.
24
25 ``brxnet`` should be a Private IPv4 Address range, default range is
26 [192.168.0.0/16]
27
28 Example that mounts all clients::
29
30 tasks:
31 - ceph:
32 - kclient:
33 - interactive:
34 - brxnet: [192.168.0.0/16]
35
36 Example that uses both ``kclient` and ``ceph-fuse``::
37
38 tasks:
39 - ceph:
40 - ceph-fuse: [client.0]
41 - kclient: [client.1]
42 - interactive:
43
44
45 Pass a dictionary instead of lists to specify per-client config:
46
47 tasks:
48 -kclient:
49 client.0:
50 debug: true
51 mntopts: ["nowsync"]
52
53 :param ctx: Context
54 :param config: Configuration
55 """
56 log.info('Mounting kernel clients...')
57
58 if config is None:
59 ids = misc.all_roles_of_type(ctx.cluster, 'client')
60 client_roles = [f'client.{id_}' for id_ in ids]
61 config = dict([r, dict()] for r in client_roles)
62 elif isinstance(config, list):
63 client_roles = config
64 config = dict([r, dict()] for r in client_roles)
65 elif isinstance(config, dict):
66 client_roles = filter(lambda x: 'client.' in x, config.keys())
67 else:
68 raise ValueError(f"Invalid config object: {config} ({config.__class__})")
69 log.info(f"config is {config}")
70
71 clients = list(misc.get_clients(ctx=ctx, roles=client_roles))
72
73 test_dir = misc.get_testdir(ctx)
74
75 for id_, remote in clients:
76 KernelMount.cleanup_stale_netnses_and_bridge(remote)
77
78 mounts = {}
79 overrides = ctx.config.get('overrides', {}).get('kclient', {})
80 top_overrides = dict(filter(lambda x: 'client.' not in x[0], overrides.items()))
81 for id_, remote in clients:
82 entity = f"client.{id_}"
83 client_config = config.get(entity)
84 if client_config is None:
85 client_config = {}
86 # top level overrides
87 for k, v in top_overrides.items():
88 if v is not None:
89 client_config[k] = v
90 # mount specific overrides
91 client_config_overrides = overrides.get(entity)
92 deep_merge(client_config, client_config_overrides)
93 log.info(f"{entity} config is {client_config}")
94
95 cephfs_name = client_config.get("cephfs_name")
96 if config.get("disabled", False) or not client_config.get('mounted', True):
97 continue
98
99 kernel_mount = KernelMount(
100 ctx=ctx,
101 test_dir=test_dir,
102 client_id=id_,
103 client_remote=remote,
104 brxnet=ctx.teuthology_config.get('brxnet', None),
105 config=client_config,
106 cephfs_name=cephfs_name)
107
108 mounts[id_] = kernel_mount
109
110 if client_config.get('debug', False):
111 remote.run(args=["sudo", "bash", "-c", "echo 'module ceph +p' > /sys/kernel/debug/dynamic_debug/control"])
112 remote.run(args=["sudo", "bash", "-c", "echo 'module libceph +p' > /sys/kernel/debug/dynamic_debug/control"])
113
114 kernel_mount.mount(mntopts=client_config.get('mntopts', []))
115
116 def umount_all():
117 log.info('Unmounting kernel clients...')
118
119 forced = False
120 for mount in mounts.values():
121 if mount.is_mounted():
122 try:
123 mount.umount()
124 except (CommandFailedError, MaxWhileTries):
125 log.warning("Ordinary umount failed, forcing...")
126 forced = True
127 mount.umount_wait(force=True)
128
129 for id_, remote in clients:
130 KernelMount.cleanup_stale_netnses_and_bridge(remote)
131
132 return forced
133
134 ctx.mounts = mounts
135 try:
136 yield mounts
137 except:
138 umount_all() # ignore forced retval, we are already in error handling
139 finally:
140
141 forced = umount_all()
142 if forced:
143 # The context managers within the kclient manager worked (i.e.
144 # the test workload passed) but for some reason we couldn't
145 # umount, so turn this into a test failure.
146 raise RuntimeError("Kernel mounts did not umount cleanly")