summaryrefslogtreecommitdiffstats
path: root/qa/tasks/nvme_loop.py
blob: fdec467a16d247046cedd2c384b8b4ff8b91c993 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import contextlib
import logging
import json

from io import StringIO
from teuthology import misc as teuthology
from teuthology import contextutil
from teuthology.orchestra import run


log = logging.getLogger(__name__)


@contextlib.contextmanager
def task(ctx, config):
    log.info('Setting up nvme_loop on scratch devices...')
    host = 'hostnqn'
    port = '1'
    devs_by_remote = {}
    old_scratch_by_remote = {}
    for remote, roles in ctx.cluster.remotes.items():
        if remote.is_container:
            continue
        devs = teuthology.get_scratch_devices(remote)
        devs_by_remote[remote] = devs
        base = '/sys/kernel/config/nvmet'
        remote.run(
            args=[
                'grep', '^nvme_loop', '/proc/modules', run.Raw('||'),
                'sudo', 'modprobe', 'nvme_loop',
                run.Raw('&&'),
                'sudo', 'mkdir', '-p', f'{base}/hosts/{host}',
                run.Raw('&&'),
                'sudo', 'mkdir', '-p', f'{base}/ports/{port}',
                run.Raw('&&'),
                'echo', 'loop', run.Raw('|'),
                'sudo', 'tee', f'{base}/ports/{port}/addr_trtype',
            ]
        )
        for dev in devs:
            short = dev.split('/')[-1]
            log.info(f'Connecting nvme_loop {remote.shortname}:{dev}...')
            remote.run(
                args=[
                    'sudo', 'mkdir', '-p', f'{base}/subsystems/{short}',
                    run.Raw('&&'),
                    'echo', '1', run.Raw('|'),
                    'sudo', 'tee', f'{base}/subsystems/{short}/attr_allow_any_host',
                    run.Raw('&&'),
                    'sudo', 'mkdir', '-p', f'{base}/subsystems/{short}/namespaces/1',
                    run.Raw('&&'),
                    'echo', '-n', dev, run.Raw('|'),
                    'sudo', 'tee', f'{base}/subsystems/{short}/namespaces/1/device_path',
                    run.Raw('&&'),
                    'echo', '1', run.Raw('|'),
                    'sudo', 'tee', f'{base}/subsystems/{short}/namespaces/1/enable',
                    run.Raw('&&'),
                    'sudo', 'ln', '-s', f'{base}/subsystems/{short}',
                    f'{base}/ports/{port}/subsystems/{short}',
                    run.Raw('&&'),
                    'sudo', 'nvme', 'connect', '-t', 'loop', '-n', short, '-q', host,
                ]
            )

        # identify nvme_loops devices
        old_scratch_by_remote[remote] = remote.read_file('/scratch_devs')

        with contextutil.safe_while(sleep=1, tries=15) as proceed:
            while proceed():
                remote.run(args=['lsblk'], stdout=StringIO())
                p = remote.run(args=['sudo', 'nvme', 'list', '-o', 'json'], stdout=StringIO())
                new_devs = []
                # `nvme list -o json` will return one of the following output:
                '''{
                     "Devices" : [
                       {
                         "DevicePath" : "/dev/nvme0n1",
                         "Firmware" : "8DV101H0",
                         "Index" : 0,
                         "ModelNumber" : "INTEL SSDPEDMD400G4",
                         "ProductName" : "Unknown Device",
                         "SerialNumber" : "PHFT620400WB400BGN"
                       },
                       {
                         "DevicePath" : "/dev/nvme1n1",
                         "Firmware" : "5.15.0-1",
                         "Index" : 1,
                         "ModelNumber" : "Linux",
                         "ProductName" : "Unknown Device",
                         "SerialNumber" : "7672ce414766ba44a8e5"
                       }
                     ]
                   }'''
                '''{
                  "Devices":[
                    {
                      "HostNQN":"nqn.2014-08.org.nvmexpress:uuid:00000000-0000-0000-0000-0cc47ada6ba4",
                      "HostID":"898a0e10-da2d-4a42-8017-d9c445089d0c",
                      "Subsystems":[
                        {
                          "Subsystem":"nvme-subsys0",
                          "SubsystemNQN":"nqn.2014.08.org.nvmexpress:80868086CVFT623300LN400BGN  INTEL SSDPEDMD400G4",
                          "Controllers":[
                            {
                              "Controller":"nvme0",
                              "Cntlid":"0",
                              "SerialNumber":"CVFT623300LN400BGN",
                              "ModelNumber":"INTEL SSDPEDMD400G4",
                              "Firmware":"8DV101H0",
                              "Transport":"pcie",
                              "Address":"0000:02:00.0",
                              "Slot":"2",
                              "Namespaces":[
                                {
                                  "NameSpace":"nvme0n1",
                                  "Generic":"ng0n1",
                                  "NSID":1,
                                  "UsedBytes":400088457216,
                                  "MaximumLBA":781422768,
                                  "PhysicalSize":400088457216,
                                  "SectorSize":512
                                }
                              ],
                              "Paths":[
                              ]
                            }
                          ],
                          "Namespaces":[
                          ]
                        }
                      ]
                    }
                  ]
                }
                '''
                '''{
                  "Devices":[
                    {
                      "HostNQN":"nqn.2014-08.org.nvmexpress:uuid:00000000-0000-0000-0000-0cc47ada6ba4",
                      "HostID":"898a0e10-da2d-4a42-8017-d9c445089d0c",
                      "Subsystems":[
                        {
                          "Subsystem":"nvme-subsys0",
                          "SubsystemNQN":"nqn.2014.08.org.nvmexpress:80868086CVFT534400C2400BGN  INTEL SSDPEDMD400G4",
                          "Controllers":[
                            {
                              "Controller":"nvme0",
                              "Cntlid":"0",
                              "SerialNumber":"CVFT534400C2400BGN",
                              "ModelNumber":"INTEL SSDPEDMD400G4",
                              "Firmware":"8DV101H0",
                              "Transport":"pcie",
                              "Address":"0000:02:00.0",
                              "Slot":"2",
                              "Namespaces":[
                                {
                                  "NameSpace":"nvme0n1",
                                  "Generic":"ng0n1",
                                  "NSID":1,
                                  "UsedBytes":400088457216,
                                  "MaximumLBA":781422768,
                                  "PhysicalSize":400088457216,
                                  "SectorSize":512
                                }
                              ],
                              "Paths":[
                              ]
                            }
                          ],
                          "Namespaces":[
                          ]
                        }
                      ]
                    }
                  ]
                }
                '''
                nvme_list = json.loads(p.stdout.getvalue())
                for device in nvme_list['Devices']:
                    try:
                        # first try format 1 / older format
                        dev = device['DevicePath']
                        vendor = device['ModelNumber']
                        if dev.startswith('/dev/') and vendor == 'Linux':
                            new_devs.append(dev)
                            bluestore_zap(remote, dev)
                    except KeyError:
                        for subsystem in device['Subsystems']:
                            # format 2
                            if 'Namespaces' in subsystem and subsystem['Namespaces']:
                                dev = '/dev/' + subsystem['Namespaces'][0]['NameSpace']
                            # try format 3 last
                            else:
                                dev = '/dev/' + subsystem['Controllers'][0]['Namespaces'][0]['NameSpace']
                            # vendor is the same for format 2 and 3
                            vendor = subsystem['Controllers'][0]['ModelNumber']
                            if vendor == 'Linux':
                                new_devs.append(dev)
                                bluestore_zap(remote, dev)
                log.info(f'new_devs {new_devs}')
                assert len(new_devs) <= len(devs)
                if len(new_devs) == len(devs):
                    break

        remote.write_file(
            path='/scratch_devs',
            data='\n'.join(new_devs) + '\n',
            sudo=True
        )

    try:
        yield

    finally:
        for remote, devs in devs_by_remote.items():
            if remote.is_container:
                continue
            for dev in devs:
                short = dev.split('/')[-1]
                log.info(f'Disconnecting nvme_loop {remote.shortname}:{dev}...')
                remote.run(
                    args=[
                        'sudo', 'nvme', 'disconnect', '-n', short
                    ],
                    check_status=False,
                )
            remote.write_file(
                path='/scratch_devs',
                data=old_scratch_by_remote[remote],
                sudo=True
            )

def bluestore_zap(remote, device: str) -> None:
    for offset in [0, 1073741824, 10737418240]:
        remote.run(args=['sudo', 'dd',
                         'if=/dev/zero', f'of={device}',
                         f'seek={offset}', 'bs=1',
                         'count=4096'], stdout=StringIO())
        remote.run(args=['sudo', 'hexdump', '-n22',
                         '-C', f'-s{offset}', f'{device}'],
                   stdout=StringIO())