summaryrefslogtreecommitdiff
path: root/test/shell/devicesfile-realdevs.sh
blob: fd328eb32a58d436b0c774f54a07650c2e1320b6 (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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#!/usr/bin/env bash

# Copyright (C) 2020 Red Hat, Inc. All rights reserved.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions
# of the GNU General Public License v.2.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

test_description='devices file with real devs'

. lib/inittest

#
# To use this test, add two or more devices with real device ids,
# e.g. wwids, to a file, e.g.
# $ cat /tmp/devs
# /dev/sdb
# /dev/sdc
# /dev/sdd
#
# Specify this file as LVM_TEST_DEVICE_LIST=/tmp/devs
# when running the test.
#
# This test will wipe these devices.
#

test -e "$LVM_TEST_DEVICE_LIST" || skip

num_devs=$(cat $LVM_TEST_DEVICE_LIST | wc -l)

aux prepare_real_devs

aux lvmconf 'devices/dir = "/dev"'
aux lvmconf 'devices/use_devicesfile = 1'
DFDIR="$LVM_SYSTEM_DIR/devices"
DF="$DFDIR/system.devices"
mkdir $DFDIR
not ls $DF

get_real_devs

wipe_all() {
	for dev in "${REAL_DEVICES[@]}"; do
		wipefs -a $dev
	done
}

wipe_all

# check each dev is added correctly to df

for dev in "${REAL_DEVICES[@]}"; do
	pvcreate $dev

	ls $DF

	pvs -o+uuid $dev
	maj=$(get pv_field "$dev" major)
	min=$(get pv_field "$dev" minor)
	pvid=`pvs $dev --noheading -o uuid | tr -d - | awk '{print $1}'`

	sys_wwid_file="/sys/dev/block/$maj:$min/device/wwid"
	sys_serial_file="/sys/dev/block/$maj:$min/device/serial"
	sys_dm_uuid_file="/sys/dev/block/$maj:$min/dm/uuid"
	sys_md_uuid_file="/sys/dev/block/$maj:$min/md/uuid"
	sys_loop_file="/sys/dev/block/$maj:$min/loop/backing_file"

	if test -e $sys_wwid_file; then
		sys_file=$sys_wwid_file
		idtype="sys_wwid"
	elif test -e $sys_serial_file; then
		sys_file=$sys_serial_file
		idtype="sys_serial"
	elif test -e $sys_dm_uuid_file; then
		sys_file=$sys_dm_uuid_file
		idtype="mpath_uuid"
	elif test -e $sys_md_uuid_file; then
		sys_file=$sys_md_uuid_file
		idtype="md_uuid"
	elif test -e $sys_loop_file; then
		sys_file=$sys_loop_file
		idtype="loop_file"
	else
		echo "no id type for device"
		skip
	fi

	idname=$(< $sys_file)

	rm -f idline
	grep IDNAME=$idname $DF | tee idline
	grep IDTYPE=$idtype idline
	grep DEVNAME=$dev idline
	grep PVID=$pvid idline
done

cp $DF df2

# vgcreate from existing pvs, already in df

vgcreate $vg ${REAL_DEVICES[@]}

vgremove $vg
rm $DF

# vgcreate from existing pvs, adding to df

vgcreate $vg ${REAL_DEVICES[@]}

grep IDNAME $DF > df.ids
grep IDNAME df2 > df2.ids
diff df.ids df2.ids

# check device id metadata fields

for dev in "${REAL_DEVICES[@]}"; do
	grep $dev $DF
	deviceid=`pvs $dev --noheading -o deviceid | awk '{print $1}'`
	deviceidtype=`pvs $dev --noheading -o deviceidtype | awk '{print $1}'`
	grep $dev $DF | grep $deviceid
	grep $dev $DF | grep $deviceidtype
	lvcreate -l1 $vg $dev
done

vgchange -an $vg
vgremove -y $vg

# check pvremove leaves devs in df but without pvid

for dev in "${REAL_DEVICES[@]}"; do
	maj=$(get pv_field "$dev" major)
	min=$(get pv_field "$dev" minor)
	pvid=`pvs $dev --noheading -o uuid | tr -d - | awk '{print $1}'`

	pvremove $dev
	grep $dev $DF
	not grep $pvid $DF
done

# Many of remaining tests require two or three devices
test $num_devs -gt 2 || skip

# check vgextend adds new dev to df, vgreduce leaves dev in df

rm $DF

vgcreate $vg $dev1
vgextend $vg $dev2
grep $dev1 $DF
grep $dev2 $DF
id1=`pvs $dev1 --noheading -o deviceid | awk '{print $1}'`
id2=`pvs $dev2 --noheading -o deviceid | awk '{print $1}'`
grep $id1 $DF
grep $id2 $DF
vgreduce $vg $dev2
grep $dev2 $DF
vgremove $vg

# check devs are not visible to lvm until added to df

rm $DF

# df needs to exist otherwise devicesfile feature turned off
touch $DF

not pvs $dev1
not pvs $dev2
pvs -a |tee all
not grep $dev1 all
not grep $dev2 all
not grep $dev1 $DF
not grep $dev2 $DF

pvcreate $dev1

pvs $dev1
not pvs $dev2
pvs -a |tee all
grep $dev1 all
not grep $dev2 all
grep $dev1 $DF
not grep $dev2 $DF

pvcreate $dev2

pvs $dev1
pvs $dev2
pvs -a |tee all
grep $dev1 all
grep $dev2 all
grep $dev1 $DF
grep $dev2 $DF

vgcreate $vg $dev1

pvs $dev1
pvs $dev2
pvs -a |tee all
grep $dev1 all
grep $dev2 all
grep $dev1 $DF
grep $dev2 $DF

vgextend $vg $dev2

pvs $dev1
pvs $dev2
pvs -a |tee all
grep $dev1 all
grep $dev2 all
grep $dev1 $DF
grep $dev2 $DF

# check vgimportdevices VG

rm $DF
wipe_all

vgcreate $vg ${REAL_DEVICES[@]}
rm $DF
touch $DF

for dev in "${REAL_DEVICES[@]}"; do
	not pvs $dev
done

vgimportdevices $vg

for dev in "${REAL_DEVICES[@]}"; do
	pvs $dev
done

# check vgimportdevices -a

rm $DF
wipe_all

vgcreate $vg1 $dev1
vgcreate $vg2 $dev2

rm $DF

vgimportdevices -a

vgs $vg1
vgs $vg2

pvs $dev1
pvs $dev2

# check vgimportclone --importdevices

rm $DF
wipe_all

vgcreate $vg1 $dev1
vgimportdevices $vg1

dd if=$dev1 of=$dev2 bs=1M count=1

pvs $dev1
not pvs $dev2

grep $dev1 $DF
not grep $dev2 $DF

not vgimportclone $dev2

not grep $dev2 $DF

vgimportclone --basevgname $vg2 --importdevices $dev2

pvid1=`pvs $dev1 --noheading -o uuid | tr -d - | awk '{print $1}'`
pvid2=`pvs $dev2 --noheading -o uuid | tr -d - | awk '{print $1}'`
test "$pvid1" != "$pvid2" || die "same uuid"

id1=`pvs $dev1 --noheading -o deviceid | tr -d - | awk '{print $1}'`
id2=`pvs $dev2 --noheading -o deviceid | tr -d - | awk '{print $1}'`
test "$id1" != "$id2" || die "same device id"

grep $dev1 $DF
grep $dev2 $DF
grep $pvid1 $DF
grep $pvid2 $DF
grep $id1 $DF
grep $id2 $DF

vgs $vg1
vgs $vg2

#
# check lvmdevices
#

wipe_all
rm $DF

# set up pvs and save pvids/deviceids
count=0
for dev in "${REAL_DEVICES[@]}"; do
	pvcreate $dev
	vgcreate ${vg}_${count} $dev
	pvid=`pvs $dev --noheading -o uuid | tr -d - | awk '{print $1}'`
	did=`pvs $dev --noheading -o deviceid | awk '{print $1}'`
	echo dev $dev pvid $pvid did $did
	PVIDS[$count]=$pvid
	DEVICEIDS[$count]=$did
	count=$((  count + 1 ))
done

rm $DF
not lvmdevices
touch $DF
lvmdevices

# check lvmdevices --adddev
count=0
for dev in "${REAL_DEVICES[@]}"; do
	pvid=${PVIDS[$count]}
	did=${DEVICEIDS[$count]}
	not pvs $dev
	lvmdevices --adddev $dev
	lvmdevices |tee out
	grep $dev out |tee idline
	grep $pvid idline
	grep $did idline
	grep $dev $DF
	pvs $dev
	count=$((  count + 1 ))
done

# check lvmdevices --deldev
count=0
for dev in "${REAL_DEVICES[@]}"; do
	pvid=${PVIDS[$count]}
	did=${DEVICEIDS[$count]}
	pvs $dev
	lvmdevices --deldev $dev
	lvmdevices |tee out
	not grep $dev out
	not grep $pvid out
	not grep $did out
	not grep $dev $DF
	not pvs $dev
	count=$((  count + 1 ))
done

# check lvmdevices --addpvid
count=0
for dev in "${REAL_DEVICES[@]}"; do
	pvid=${PVIDS[$count]}
	did=${DEVICEIDS[$count]}
	not pvs $dev
	lvmdevices --addpvid $pvid
	lvmdevices |tee out
	grep $dev out |tee idline
	grep $pvid idline
	grep $did idline
	grep $dev $DF
	pvs $dev
	count=$((  count + 1 ))
done

# check lvmdevices --delpvid
count=0
for dev in "${REAL_DEVICES[@]}"; do
	pvid=${PVIDS[$count]}
	did=${DEVICEIDS[$count]}
	pvs $dev
	lvmdevices --delpvid $pvid
	lvmdevices |tee out
	not grep $dev out
	not grep $pvid out
	not grep $did out
	not grep $dev $DF
	not pvs $dev
	count=$((  count + 1 ))
done

# wrong pvid in df
rm $DF
pvid1=${PVIDS[0]}
pvid2=${PVIDS[1]}
did1=${DEVICEIDS[0]}
did2=${DEVICEIDS[1]}
lvmdevices --adddev $dev1
lvmdevices --adddev $dev2

# test bad pvid
cp $DF $DF.orig
rm $DF
sed "s/$pvid1/badpvid/" "$DF.orig" |tee $DF
not grep $pvid1 $DF
grep $did1 $DF

lvmdevices --check 2>&1|tee out
grep $dev1 out
grep badpvid out
grep $pvid1 out
not grep $dev2 out

lvmdevices |tee out
grep $dev1 out |tee out1
grep badpvid out1
not grep $pvid1 out1
grep $dev2 out

lvmdevices --update

lvmdevices 2>&1|tee out
grep $dev1 out
grep $dev2 out
not grep badpvid
grep $pvid1 out
grep $did1 out
grep $pvid1 $DF
grep $did1 $DF

# wrong deviceid in df
# the devicesfile logic and behavior is based on the idname being
# the primary identifier that we trust over everything else, i.e.
# we'll never assume that the deviceid is wrong and some other
# field is correct, and "fix" the deviceid.  We always assume the
# deviceid correct and other values are wrong (since pvid and devname
# have known, common ways of becoming wrong, but the deviceid doesn't
# really have any known way of becoming wrong apart from random
# file corruption.)
# So, if the deviceid *is* corrupted, as we do here, then standard
# commands won't correct it.  We need to use delpvid/addpvid explicitly
# to say that we are targetting the given pvid.

rm $DF
sed "s/$did1/baddid/" "$DF.orig" |tee $DF

lvmdevices --check 2>&1|tee out
grep $dev1 out
grep baddid out
not grep $dev2 out

lvmdevices 2>&1|tee out
grep $pvid1 out
grep $pvid2 out
grep baddid out
grep $did2 out
grep $dev2 out

lvmdevices --delpvid $pvid1
lvmdevices --addpvid $pvid1

lvmdevices |tee out
grep $dev1 out
grep $dev2 out
not grep baddid
grep $pvid1 out
grep $did1 out
grep $pvid1 $DF
grep $did1 $DF

# wrong devname in df, this is expected to become incorrect regularly
# given inconsistent dev names after reboot

rm $DF
d1=$(basename $dev1)
d3=$(basename $dev3)
sed "s/$d1/$d3/" "$DF.orig" |tee $DF
lvmdevices --check |tee out
grep $dev1 out

lvmdevices --update

lvmdevices |tee out
grep $dev1 out |tee out1
grep $pvid1 out1
grep $did1 out1
grep $dev2 out |tee out2
grep $pvid2 out2
grep $did2 out2

# swap devnames for two existing entries

rm $DF
d1=$(basename $dev1)
d2=$(basename $dev2)
sed "s/$d1/tmp/" "$DF.orig" |tee ${DF}_1
sed "s/$d2/$d1/" "${DF}_1" |tee ${DF}_2
sed "s/tmp/$d2/" "${DF}_2" |tee $DF
rm ${DF}_1 ${DF}_2
lvmdevices --check |tee out
grep $dev1 out
grep $dev2 out

lvmdevices --update

lvmdevices |tee out
grep $dev1 out |tee out1
grep $pvid1 out1
grep $did1 out1
grep $dev2 out |tee out2
grep $pvid2 out2
grep $did2 out2

# ordinary command is not confused by wrong devname and fixes
# the wrong devname in df

rm $DF
d1=$(basename $dev1)
d3=$(basename $dev3)
sed "s/$d1/$d3/" "$DF.orig" |tee $DF
lvmdevices --check |tee out
grep $dev1 out

pvs -o+uuid,deviceid | grep $vg |tee out
grep $dev1 out |tee out1
grep $dev2 out |tee out2
grep $did1 out1
grep $did2 out2
not grep $dev3 out

# same dev info reported after df is fixed
pvs -o+uuid,deviceid | grep $vg |tee out3
diff out out3

pvid=`pvs $dev1 --noheading -o uuid | tr -d - | awk '{print $1}'`
test "$pvid" == "$pvid1" || die "wrong uuid"
pvid=`pvs $dev2 --noheading -o uuid | tr -d - | awk '{print $1}'`
test "$pvid" == "$pvid2" || die "wrong uuid"

lvmdevices |tee out
grep $dev1 out |tee out1
grep $pvid1 out1
grep $did1 out1
grep $dev2 out |tee out2
grep $pvid2 out2
grep $did2 out2

# pvscan --cache doesn't fix wrong devname but still works correctly with
# the correct device

wipe_all
rm $DF