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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
#!/usr/bin/python
# Copyright 2015 Sam Yaple
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This module has been relicensed from the source below:
# https://github.com/SamYaple/yaodu/blob/master/ansible/library/ceph_osd_list
import json
import pyudev
import re
import subprocess # nosec
from ansible.module_utils.basic import AnsibleModule
DOCUMENTATION = '''
---
module: find_disks
short_description: Return list of devices containing a specfied name or label
description:
- This will return a list of all devices with either GPT partition name
or filesystem label of the name specified.
options:
match_mode:
description:
- Label match mode, either strict or prefix
default: 'strict'
required: False
choices: [ "strict", "prefix" ]
type: str
name:
description:
- Partition name or filesystem label
required: True
type: str
aliases: [ 'partition_name' ]
use_udev:
description:
- When True, use Linux udev to read disk info such as partition labels,
uuid, etc. Some older host operating systems have issues using udev to
get the info this module needs. Set to False to fall back to more low
level commands such as blkid to retrieve this information. Most users
should not need to change this.
default: True
required: False
type: bool
author: Sam Yaple
'''
EXAMPLES = '''
- hosts: ceph-osd
tasks:
- name: Return all valid formated devices with the name KOLLA_CEPH_OSD
find_disks:
name: 'KOLLA_CEPH_OSD'
register: osds
- hosts: swift-object-server
tasks:
- name: Return all valid devices with the name KOLLA_SWIFT
find_disks:
name: 'KOLLA_SWIFT'
register: swift_disks
- hosts: swift-object-server
tasks:
- name: Return all valid devices with wildcard name 'swift_d*'
find_disks:
name: 'swift_d' match_mode: 'prefix'
register: swift_disks
'''
PREFERRED_DEVICE_LINK_ORDER = [
'/dev/disk/by-uuid',
'/dev/disk/by-partuuid',
'/dev/disk/by-parttypeuuid',
'/dev/disk/by-label',
'/dev/disk/by-partlabel'
]
def get_id_part_entry_name(dev, use_udev):
if use_udev:
dev_name = dev.get('ID_PART_ENTRY_NAME', '')
else:
part = re.sub(r'.*[^\d]', '', dev.device_node)
parent = dev.find_parent('block').device_node
# NOTE(Mech422): Need to use -i as -p truncates the partition name
out = subprocess.Popen(['/usr/sbin/sgdisk', '-i', part, # nosec
parent],
stdout=subprocess.PIPE).communicate()
match = re.search(r'Partition name: \'(\w+)\'', out[0])
if match:
dev_name = match.group(1)
else:
dev_name = ''
return dev_name
def get_id_fs_uuid(dev, use_udev):
if use_udev:
id_fs_uuid = dev.get('ID_FS_UUID', '')
else:
out = subprocess.Popen(['/usr/sbin/blkid', '-o', 'export', # nosec
dev.device_node],
stdout=subprocess.PIPE).communicate()
match = re.search(r'\nUUID=([\w-]+)', out[0])
if match:
id_fs_uuid = match.group(1)
else:
id_fs_uuid = ''
return id_fs_uuid
def is_dev_matched_by_name(dev, name, mode, use_udev):
if dev.get('DEVTYPE', '') == 'partition':
dev_name = get_id_part_entry_name(dev, use_udev)
else:
dev_name = dev.get('ID_FS_LABEL', '')
if mode == 'strict':
return dev_name == name
elif mode == 'prefix':
return dev_name.startswith(name)
else:
return False
def find_disk(ct, name, match_mode, use_udev):
for dev in ct.list_devices(subsystem='block'):
if is_dev_matched_by_name(dev, name, match_mode, use_udev):
yield dev
def get_device_link(dev):
for preferred_link in PREFERRED_DEVICE_LINK_ORDER:
for link in dev.device_links:
if link.startswith(preferred_link):
return link
return dev.device_node
def extract_disk_info(ct, dev, name, use_udev):
if not dev:
return
kwargs = dict()
kwargs['fs_uuid'] = get_id_fs_uuid(dev, use_udev)
kwargs['fs_label'] = dev.get('ID_FS_LABEL', '')
if dev.get('DEVTYPE', '') == 'partition':
kwargs['partition_label'] = name
kwargs['device'] = dev.find_parent('block').device_node
kwargs['partition'] = dev.device_node
kwargs['partition_num'] = re.sub(r'.*[^\d]', '', dev.device_node)
if is_dev_matched_by_name(dev, name, 'strict', use_udev):
kwargs['external_journal'] = False
# NOTE(jeffrey4l): this is only used for bootstrap osd stage and
# there is no journal partion at all. So it is OK to use
# device_node directly.
kwargs['journal'] = dev.device_node[:-1] + '2'
kwargs['journal_device'] = kwargs['device']
kwargs['journal_num'] = 2
else:
kwargs['external_journal'] = True
journal_name = get_id_part_entry_name(dev, use_udev) + '_J'
for journal in find_disk(ct, journal_name, 'strict', use_udev):
kwargs['journal'] = get_device_link(journal)
kwargs['journal_device'] = \
journal.find_parent('block').device_node
kwargs['journal_num'] = \
re.sub(r'.*[^\d]', '', journal.device_node)
break
if 'journal' not in kwargs:
# NOTE(SamYaple): Journal not found, not returning info
return
else:
kwargs['device'] = dev.device_node
yield kwargs
def extract_disk_info_bs(ct, dev, name, use_udev):
if not dev:
return
kwargs = dict(bs_blk_label='', bs_blk_device='', bs_db_label='',
bs_db_device='', bs_wal_label='', bs_wal_device='',
bs_wal_partition_num='', bs_db_partition_num='',
bs_blk_partition_num='', partition='', partition_label='',
partition_num='', device='', partition_usage='')
kwargs['fs_uuid'] = get_id_fs_uuid(dev, use_udev)
kwargs['fs_label'] = dev.get('ID_FS_LABEL', '')
if dev.get('DEVTYPE', '') == 'partition':
actual_name = get_id_part_entry_name(dev, use_udev)
if (('BOOTSTRAP_BS' in name or 'DATA_BS' in name)
and name in actual_name):
if actual_name.endswith("_B"):
kwargs['partition_usage'] = 'block'
kwargs['bs_blk_partition_num'] = \
re.sub(r'.*[^\d]', '', dev.device_node)
kwargs['bs_blk_device'] = dev.find_parent('block').device_node
kwargs['bs_blk_label'] = actual_name
return kwargs
if actual_name.endswith("_D"):
kwargs['partition_usage'] = 'block.db'
kwargs['bs_db_partition_num'] = \
re.sub(r'.*[^\d]', '', dev.device_node)
kwargs['bs_db_device'] = dev.find_parent('block').device_node
kwargs['bs_db_label'] = actual_name
return kwargs
if actual_name.endswith("_W"):
kwargs['partition_usage'] = 'block.wal'
kwargs['bs_wal_partition_num'] = \
re.sub(r'.*[^\d]', '', dev.device_node)
kwargs['bs_wal_device'] = dev.find_parent('block').device_node
kwargs['bs_wal_label'] = actual_name
return kwargs
if '_BS' in actual_name:
kwargs['partition_usage'] = 'osd'
kwargs['partition'] = dev.find_parent('block').device_node
kwargs['partition_label'] = actual_name
kwargs['partition_num'] = \
re.sub(r'.*[^\d]', '', dev.device_node)
kwargs['device'] = dev.find_parent('block').device_node
return kwargs
return 0
def nb_of_osd(disks):
osd_info = dict()
osd_info['block_label'] = list()
nb_of_osds = 0
for item in disks:
if item['partition_usage'] == 'osd':
osd_info['block_label'].append(item['partition_label'])
nb_of_osds += 1
osd_info['nb_of_osd'] = nb_of_osds
return osd_info
def combine_info(disks):
info = list()
osds = nb_of_osd(disks)
osd_id = 0
while osd_id < osds['nb_of_osd']:
final = dict()
idx = 0
idx_osd = idx_blk = idx_wal = idx_db = -1
for item in disks:
if (item['partition_usage'] == 'osd' and
item['partition_label'] == osds['block_label'][osd_id]):
idx_osd = idx
elif (item['partition_usage'] == 'block' and
item['bs_blk_label'] ==
osds['block_label'][osd_id] + "_B"):
idx_blk = idx
elif (item['partition_usage'] == 'block.wal' and
item['bs_wal_label'] ==
osds['block_label'][osd_id] + "_W"):
idx_wal = idx
elif (item['partition_usage'] == 'block.db' and
item['bs_db_label'] ==
osds['block_label'][osd_id] + "_D"):
idx_db = idx
idx = idx + 1
# write the information of block.db and block.wal to block item
# if block.db and block.wal are found
if idx_blk != -1:
disks[idx_osd]['bs_blk_device'] = disks[idx_blk]['bs_blk_device']
disks[idx_osd]['bs_blk_label'] = disks[idx_blk]['bs_blk_label']
disks[idx_osd]['bs_blk_partition_num'] = \
disks[idx_blk]['bs_blk_partition_num']
disks[idx_blk]['partition_usage'] = ''
if idx_wal != -1:
disks[idx_osd]['bs_wal_device'] = disks[idx_wal]['bs_wal_device']
disks[idx_osd]['bs_wal_partition_num'] = \
disks[idx_wal]['bs_wal_partition_num']
disks[idx_osd]['bs_wal_label'] = disks[idx_wal]['bs_wal_label']
disks[idx_wal]['partition_usage'] = ''
if idx_db != -1:
disks[idx_osd]['bs_db_device'] = disks[idx_db]['bs_db_device']
disks[idx_osd]['bs_db_partition_num'] = \
disks[idx_db]['bs_db_partition_num']
disks[idx_osd]['bs_db_label'] = disks[idx_db]['bs_db_label']
disks[idx_db]['partition_usage'] = ''
final['fs_uuid'] = disks[idx_osd]['fs_uuid']
final['fs_label'] = disks[idx_osd]['fs_label']
final['bs_blk_device'] = disks[idx_osd]['bs_blk_device']
final['bs_blk_label'] = disks[idx_osd]['bs_blk_label']
final['bs_blk_partition_num'] = disks[idx_osd]['bs_blk_partition_num']
final['bs_db_device'] = disks[idx_osd]['bs_db_device']
final['bs_db_partition_num'] = disks[idx_osd]['bs_db_partition_num']
final['bs_db_label'] = disks[idx_osd]['bs_db_label']
final['bs_wal_device'] = disks[idx_osd]['bs_wal_device']
final['bs_wal_partition_num'] = disks[idx_osd]['bs_wal_partition_num']
final['bs_wal_label'] = disks[idx_osd]['bs_wal_label']
final['device'] = disks[idx_osd]['device']
final['partition'] = disks[idx_osd]['partition']
final['partition_label'] = disks[idx_osd]['partition_label']
final['partition_num'] = disks[idx_osd]['partition_num']
final['external_journal'] = False
final['journal'] = ''
final['journal_device'] = ''
final['journal_num'] = 0
info.append(final)
disks[idx_osd]['partition_usage'] = ''
osd_id += 1
return info
def main():
argument_spec = dict(
match_mode=dict(required=False, choices=['strict', 'prefix'],
default='strict'),
name=dict(aliases=['partition_name'], required=True, type='str'),
use_udev=dict(required=False, default=True, type='bool')
)
module = AnsibleModule(argument_spec)
match_mode = module.params.get('match_mode')
name = module.params.get('name')
use_udev = module.params.get('use_udev')
try:
ret = list()
ct = pyudev.Context()
for dev in find_disk(ct, name, match_mode, use_udev):
if '_BS' in name:
info = extract_disk_info_bs(ct, dev, name, use_udev)
if info:
ret.append(info)
else:
for info in extract_disk_info(ct, dev, name, use_udev):
if info:
ret.append(info)
if '_BS' in name and len(ret) > 0:
ret = combine_info(ret)
module.exit_json(disks=json.dumps(ret))
except Exception as e:
module.exit_json(failed=True, msg=repr(e))
if __name__ == '__main__':
main()
|