summaryrefslogtreecommitdiffstats
path: root/src/ceph/qa/workunits/ceph-disk/ceph-disk-test.py
blob: 637fa90ebdd231ec299ba79f2f25a61204a8cde1 (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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
#
# Copyright (C) 2015, 2016 Red Hat <contact@redhat.com>
#
# Author: Loic Dachary <loic@dachary.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library Public License for more details.
#
# When debugging these tests (must be root), here are a few useful commands:
#
#  export PATH=.:..:$PATH
#  ceph-disk.sh # run once to prepare the environment as it would be by teuthology
#  ln -sf /home/ubuntu/ceph/src/ceph-disk/ceph_disk/main.py $(which ceph-disk)
#  ln -sf /home/ubuntu/ceph/udev/95-ceph-osd.rules /lib/udev/rules.d/95-ceph-osd.rules
#  ln -sf /home/ubuntu/ceph/systemd/ceph-disk@.service /usr/lib/systemd/system/ceph-disk@.service
#  ceph-disk.conf will be silently ignored if it is a symbolic link or a hard link /var/log/upstart for logs
#  cp /home/ubuntu/ceph/src/upstart/ceph-disk.conf /etc/init/ceph-disk.conf
#  id=3 ; ceph-disk deactivate --deactivate-by-id $id ; ceph-disk destroy --purge --zap --destroy-by-id $id
#  py.test -s -v -k test_activate_dmcrypt_luks ceph-disk-test.py
#
#  CentOS 7
#    udevadm monitor --property & tail -f /var/log/messages
#    udev rules messages are logged in /var/log/messages
#    systemctl stop ceph-osd@2
#    systemctl start ceph-osd@2
#
#  udevadm monitor --property & tail -f /var/log/syslog /var/log/upstart/*  # on Ubuntu 14.04
#  udevadm test --action=add /block/vdb/vdb1 # verify the udev rule is run as expected
#  udevadm control --reload # when changing the udev rules
#  sudo /usr/sbin/ceph-disk -v trigger /dev/vdb1 # activates if vdb1 is data
#
#  integration tests coverage
#  pip install coverage
#  perl -pi -e 's|"ceph-disk |"coverage run --source=/usr/sbin/ceph-disk --append /usr/sbin/ceph-disk |' ceph-disk-test.py
#  rm -f .coverage ; py.test -s -v ceph-disk-test.py
#  coverage report --show-missing
#
import argparse
import json
import logging
import configobj
import os
import pytest
import re
import subprocess
import sys
import tempfile
import time
import uuid

LOG = logging.getLogger('CephDisk')


class CephDisk:

    def __init__(self):
        self.conf = configobj.ConfigObj('/etc/ceph/ceph.conf')

    def save_conf(self):
        self.conf.write(open('/etc/ceph/ceph.conf', 'wb'))

    @staticmethod
    def helper(command):
        command = "ceph-helpers-root.sh " + command
        return CephDisk.sh(command)

    @staticmethod
    def sh(command):
        LOG.debug(":sh: " + command)
        proc = subprocess.Popen(
            args=command,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            shell=True,
            bufsize=1)
        output, _ = proc.communicate()
        if proc.poll():
            LOG.warning(output.decode('utf-8'))
            raise subprocess.CalledProcessError(
                returncode=proc.returncode,
                cmd=command,
                output=output,
            )
        lines = []
        for line in output.decode('utf-8').split('\n'):
            if 'dangerous and experimental' in line:
                LOG.debug('SKIP dangerous and experimental')
                continue
            lines.append(line)
            LOG.debug(line.strip().encode('ascii', 'ignore'))
        return "".join(lines)

    def unused_disks(self, pattern='[vs]d.'):
        names = [x for x in os.listdir("/sys/block") if re.match(pattern, x)]
        if not names:
            return []
        disks = json.loads(
            self.sh("ceph-disk list --format json " + " ".join(names)))
        unused = []
        for disk in disks:
            if 'partitions' not in disk:
                unused.append(disk['path'])
        return unused

    def ensure_sd(self):
        LOG.debug(self.unused_disks('sd.'))
        if self.unused_disks('sd.'):
            return
        modprobe = "modprobe scsi_debug vpd_use_hostno=0 add_host=1 dev_size_mb=300 ; udevadm settle"
        try:
            self.sh(modprobe)
        except:
            self.helper("install linux-image-extra-3.13.0-61-generic")
            self.sh(modprobe)

    def unload_scsi_debug(self):
        self.sh("rmmod scsi_debug || true")

    def get_lockbox(self):
        disks = json.loads(self.sh("ceph-disk list --format json"))
        for disk in disks:
            if 'partitions' in disk:
                for partition in disk['partitions']:
                    if partition.get('type') == 'lockbox':
                        return partition
        raise Exception("no lockbox found " + str(disks))

    def get_osd_partition(self, uuid):
        disks = json.loads(self.sh("ceph-disk list --format json"))
        for disk in disks:
            if 'partitions' in disk:
                for partition in disk['partitions']:
                    if partition.get('uuid') == uuid:
                        return partition
        raise Exception("uuid = " + uuid + " not found in " + str(disks))

    def get_journal_partition(self, uuid):
        return self.get_space_partition('journal', uuid)

    def get_block_partition(self, uuid):
        return self.get_space_partition('block', uuid)

    def get_blockdb_partition(self, uuid):
        return self.get_space_partition('block.db', uuid)

    def get_blockwal_partition(self, uuid):
        return self.get_space_partition('block.wal', uuid)

    def get_space_partition(self, name, uuid):
        data_partition = self.get_osd_partition(uuid)
        space_dev = data_partition[name + '_dev']
        disks = json.loads(self.sh("ceph-disk list --format json"))
        for disk in disks:
            if 'partitions' in disk:
                for partition in disk['partitions']:
                    if partition['path'] == space_dev:
                        if name + '_for' in partition:
                            assert partition[
                                name + '_for'] == data_partition['path']
                        return partition
        raise Exception(
            name + " for uuid = " + uuid + " not found in " + str(disks))

    def destroy_osd(self, uuid):
        id = self.sh("ceph osd create " + uuid).strip()
        self.sh("""
        set -xe
        ceph-disk --verbose deactivate --deactivate-by-id {id}
        ceph-disk --verbose destroy --purge --destroy-by-id {id} --zap
        """.format(id=id))

    def deactivate_osd(self, uuid):
        id = self.sh("ceph osd create " + uuid).strip()
        self.sh("""
        set -xe
        ceph-disk --verbose deactivate --once --deactivate-by-id {id}
        """.format(id=id))

    @staticmethod
    def osd_up_predicate(osds, uuid):
        for osd in osds:
            if osd['uuid'] == uuid and 'up' in osd['state']:
                return True
        return False

    @staticmethod
    def wait_for_osd_up(uuid):
        CephDisk.wait_for_osd(uuid, CephDisk.osd_up_predicate, 'up')

    @staticmethod
    def osd_down_predicate(osds, uuid):
        found = False
        for osd in osds:
            if osd['uuid'] == uuid:
                found = True
                if 'down' in osd['state'] or ['exists'] == osd['state']:
                    return True
        return not found

    @staticmethod
    def wait_for_osd_down(uuid):
        CephDisk.wait_for_osd(uuid, CephDisk.osd_down_predicate, 'down')

    @staticmethod
    def wait_for_osd(uuid, predicate, info):
        LOG.info("wait_for_osd " + info + " " + uuid)
        for delay in (1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024):
            dump = json.loads(CephDisk.sh("ceph osd dump -f json"))
            if predicate(dump['osds'], uuid):
                return True
            time.sleep(delay)
        raise Exception('timeout waiting for osd ' + uuid + ' to be ' + info)

    def check_osd_status(self, uuid, space_name=None):
        data_partition = self.get_osd_partition(uuid)
        assert data_partition['type'] == 'data'
        assert data_partition['state'] == 'active'
        if space_name is not None:
            space_partition = self.get_space_partition(space_name, uuid)
            assert space_partition


class TestCephDisk(object):

    def setup_class(self):
        logging.basicConfig(level=logging.DEBUG)
        c = CephDisk()
        if c.sh("lsb_release -si").strip() == 'CentOS':
            c.helper("install multipath-tools device-mapper-multipath")
        c.conf['global']['pid file'] = '/var/run/ceph/$cluster-$name.pid'
        #
        # Avoid json parsing interference
        #
        c.conf['global']['debug monc'] = 0
        #
        # objecstore
        #
        c.conf['global']['osd journal size'] = 100
        #
        # bluestore
        #
        c.conf['global']['bluestore fsck on mount'] = 'true'
        c.save_conf()

    def setup(self):
        c = CephDisk()
        for key in ('osd objectstore', 'osd dmcrypt type'):
            if key in c.conf['global']:
                del c.conf['global'][key]
        c.save_conf()

    def test_deactivate_reactivate_osd(self):
        c = CephDisk()
        disk = c.unused_disks()[0]
        osd_uuid = str(uuid.uuid1())
        c.sh("ceph-disk --verbose zap " + disk)
        c.sh("ceph-disk --verbose prepare --filestore --osd-uuid " + osd_uuid +
             " " + disk)
        c.wait_for_osd_up(osd_uuid)
        device = json.loads(c.sh("ceph-disk list --format json " + disk))[0]
        assert len(device['partitions']) == 2
        c.check_osd_status(osd_uuid, 'journal')
        data_partition = c.get_osd_partition(osd_uuid)
        c.sh("ceph-disk --verbose deactivate " + data_partition['path'])
        c.wait_for_osd_down(osd_uuid)
        c.sh("ceph-disk --verbose activate " + data_partition['path'] + " --reactivate")
        # check again
        c.wait_for_osd_up(osd_uuid)
        device = json.loads(c.sh("ceph-disk list --format json " + disk))[0]
        assert len(device['partitions']) == 2
        c.check_osd_status(osd_uuid, 'journal')
        c.helper("pool_read_write")
        c.destroy_osd(osd_uuid)

    def test_destroy_osd_by_id(self):
        c = CephDisk()
        disk = c.unused_disks()[0]
        osd_uuid = str(uuid.uuid1())
        c.sh("ceph-disk --verbose prepare --filestore --osd-uuid " + osd_uuid + " " + disk)
        c.wait_for_osd_up(osd_uuid)
        c.check_osd_status(osd_uuid)
        c.destroy_osd(osd_uuid)

    def test_destroy_osd_by_dev_path(self):
        c = CephDisk()
        disk = c.unused_disks()[0]
        osd_uuid = str(uuid.uuid1())
        c.sh("ceph-disk --verbose prepare --filestore --osd-uuid " + osd_uuid + " " + disk)
        c.wait_for_osd_up(osd_uuid)
        partition = c.get_osd_partition(osd_uuid)
        assert partition['type'] == 'data'
        assert partition['state'] == 'active'
        c.sh("ceph-disk --verbose deactivate " + partition['path'])
        c.wait_for_osd_down(osd_uuid)
        c.sh("ceph-disk --verbose destroy --purge " + partition['path'] + " --zap")

    def test_deactivate_reactivate_dmcrypt_plain(self):
        c = CephDisk()
        c.conf['global']['osd dmcrypt type'] = 'plain'
        c.save_conf()
        osd_uuid = self.activate_dmcrypt('ceph-disk-no-lockbox')
        data_partition = c.get_osd_partition(osd_uuid)
        c.sh("ceph-disk --verbose deactivate " + data_partition['path'])
        c.wait_for_osd_down(osd_uuid)
        c.sh("ceph-disk --verbose activate-journal " + data_partition['journal_dev'] +
             " --reactivate" + " --dmcrypt")
        c.wait_for_osd_up(osd_uuid)
        c.check_osd_status(osd_uuid, 'journal')
        c.destroy_osd(osd_uuid)
        c.save_conf()

    def test_deactivate_reactivate_dmcrypt_luks(self):
        c = CephDisk()
        osd_uuid = self.activate_dmcrypt('ceph-disk')
        data_partition = c.get_osd_partition(osd_uuid)
        lockbox_partition = c.get_lockbox()
        c.sh("ceph-disk --verbose deactivate " + data_partition['path'])
        c.wait_for_osd_down(osd_uuid)
        c.sh("ceph-disk --verbose trigger --sync " + lockbox_partition['path'])
        c.sh("ceph-disk --verbose activate-journal " + data_partition['journal_dev'] +
             " --reactivate" + " --dmcrypt")
        c.wait_for_osd_up(osd_uuid)
        c.check_osd_status(osd_uuid, 'journal')
        c.destroy_osd(osd_uuid)

    def test_activate_dmcrypt_plain_no_lockbox(self):
        c = CephDisk()
        c.conf['global']['osd dmcrypt type'] = 'plain'
        c.save_conf()
        osd_uuid = self.activate_dmcrypt('ceph-disk-no-lockbox')
        c.destroy_osd(osd_uuid)
        c.save_conf()

    def test_activate_dmcrypt_luks_no_lockbox(self):
        c = CephDisk()
        osd_uuid = self.activate_dmcrypt('ceph-disk-no-lockbox')
        c.destroy_osd(osd_uuid)

    def test_activate_dmcrypt_luks_with_lockbox(self):
        c = CephDisk()
        osd_uuid = self.activate_dmcrypt('ceph-disk')
        c.destroy_osd(osd_uuid)

    def test_activate_lockbox(self):
        c = CephDisk()
        osd_uuid = self.activate_dmcrypt('ceph-disk')
        lockbox = c.get_lockbox()
        assert lockbox['state'] == 'active'
        c.sh("umount " + lockbox['path'])
        lockbox = c.get_lockbox()
        assert lockbox['state'] == 'prepared'
        c.sh("ceph-disk --verbose trigger " + lockbox['path'])
        success = False
        for delay in (1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024):
            lockbox = c.get_lockbox()
            if lockbox['state'] == 'active':
                success = True
                break
            time.sleep(delay)
        if not success:
            raise Exception('timeout waiting for lockbox ' + lockbox['path'])
        c.destroy_osd(osd_uuid)

    def activate_dmcrypt(self, ceph_disk):
        c = CephDisk()
        disk = c.unused_disks()[0]
        osd_uuid = str(uuid.uuid1())
        journal_uuid = str(uuid.uuid1())
        c.sh("ceph-disk --verbose zap " + disk)
        c.sh(ceph_disk + " --verbose prepare --filestore " +
             " --osd-uuid " + osd_uuid +
             " --journal-uuid " + journal_uuid +
             " --dmcrypt " +
             " " + disk)
        c.wait_for_osd_up(osd_uuid)
        c.check_osd_status(osd_uuid, 'journal')
        return osd_uuid

    def test_trigger_dmcrypt_journal_lockbox(self):
        c = CephDisk()
        osd_uuid = self.activate_dmcrypt('ceph-disk')
        data_partition = c.get_osd_partition(osd_uuid)
        lockbox_partition = c.get_lockbox()
        c.deactivate_osd(osd_uuid)
        c.wait_for_osd_down(osd_uuid)
        with pytest.raises(subprocess.CalledProcessError):
            # fails because the lockbox is not mounted yet
            c.sh("ceph-disk --verbose trigger --sync " + data_partition['journal_dev'])
        c.sh("ceph-disk --verbose trigger --sync " + lockbox_partition['path'])
        c.wait_for_osd_up(osd_uuid)
        c.destroy_osd(osd_uuid)

    def test_trigger_dmcrypt_data_lockbox(self):
        c = CephDisk()
        osd_uuid = self.activate_dmcrypt('ceph-disk')
        data_partition = c.get_osd_partition(osd_uuid)
        lockbox_partition = c.get_lockbox()
        c.deactivate_osd(osd_uuid)
        c.wait_for_osd_down(osd_uuid)
        with pytest.raises(subprocess.CalledProcessError):
            # fails because the lockbox is not mounted yet
            c.sh("ceph-disk --verbose trigger --sync " + data_partition['path'])
        c.sh("ceph-disk --verbose trigger --sync " + lockbox_partition['path'])
        c.wait_for_osd_up(osd_uuid)
        c.destroy_osd(osd_uuid)

    def test_trigger_dmcrypt_lockbox(self):
        c = CephDisk()
        osd_uuid = self.activate_dmcrypt('ceph-disk')
        data_partition = c.get_osd_partition(osd_uuid)
        lockbox_partition = c.get_lockbox()
        c.deactivate_osd(osd_uuid)
        c.wait_for_osd_down(osd_uuid)
        c.sh("ceph-disk --verbose trigger --sync " + lockbox_partition['path'])
        c.wait_for_osd_up(osd_uuid)
        c.destroy_osd(osd_uuid)

    def test_activate_no_journal(self):
        c = CephDisk()
        disk = c.unused_disks()[0]
        osd_uuid = str(uuid.uuid1())
        c.sh("ceph-disk --verbose zap " + disk)
        c.conf['global']['osd objectstore'] = 'memstore'
        c.save_conf()
        c.sh("ceph-disk --verbose prepare --filestore --osd-uuid " + osd_uuid +
             " " + disk)
        c.wait_for_osd_up(osd_uuid)
        device = json.loads(c.sh("ceph-disk list --format json " + disk))[0]
        assert len(device['partitions']) == 1
        partition = device['partitions'][0]
        assert partition['type'] == 'data'
        assert partition['state'] == 'active'
        assert 'journal_dev' not in partition
        c.helper("pool_read_write")
        c.destroy_osd(osd_uuid)
        c.save_conf()

    def test_activate_with_journal_dev_no_symlink(self):
        c = CephDisk()
        disk = c.unused_disks()[0]
        osd_uuid = str(uuid.uuid1())
        c.sh("ceph-disk --verbose zap " + disk)
        c.sh("ceph-disk --verbose prepare --filestore --osd-uuid " + osd_uuid +
             " " + disk)
        c.wait_for_osd_up(osd_uuid)
        device = json.loads(c.sh("ceph-disk list --format json " + disk))[0]
        assert len(device['partitions']) == 2
        c.check_osd_status(osd_uuid, 'journal')
        c.helper("pool_read_write")
        c.destroy_osd(osd_uuid)

    def test_activate_bluestore(self):
        c = CephDisk()
        disk = c.unused_disks()[0]
        osd_uuid = str(uuid.uuid1())
        c.sh("ceph-disk --verbose zap " + disk)
        c.conf['global']['osd objectstore'] = 'bluestore'
        c.save_conf()
        c.sh("ceph-disk --verbose prepare --bluestore --osd-uuid " + osd_uuid +
             " " + disk)
        c.wait_for_osd_up(osd_uuid)
        device = json.loads(c.sh("ceph-disk list --format json " + disk))[0]
        assert len(device['partitions']) == 2
        c.check_osd_status(osd_uuid, 'block')
        c.helper("pool_read_write")
        c.destroy_osd(osd_uuid)
        c.sh("ceph-disk --verbose zap " + disk)

    def test_activate_bluestore_seperated_block_db_wal(self):
        c = CephDisk()
        disk1 = c.unused_disks()[0]
        disk2 = c.unused_disks()[1]
        osd_uuid = str(uuid.uuid1())
        c.sh("ceph-disk --verbose zap " + disk1 + " " + disk2)
        c.conf['global']['osd objectstore'] = 'bluestore'
        c.save_conf()
        c.sh("ceph-disk --verbose prepare --bluestore --osd-uuid " + osd_uuid +
             " " + disk1 + " --block.db " + disk2 + " --block.wal " + disk2)
        c.wait_for_osd_up(osd_uuid)
        device = json.loads(c.sh("ceph-disk list --format json " + disk1))[0]
        assert len(device['partitions']) == 2
        device = json.loads(c.sh("ceph-disk list --format json " + disk2))[0]
        assert len(device['partitions']) == 2
        c.check_osd_status(osd_uuid, 'block')
        c.check_osd_status(osd_uuid, 'block.wal')
        c.check_osd_status(osd_uuid, 'block.db')
        c.helper("pool_read_write")
        c.destroy_osd(osd_uuid)
        c.sh("ceph-disk --verbose zap " + disk1 + " " + disk2)

    def test_activate_bluestore_reuse_db_wal_partition(self):
        c = CephDisk()
        disks = c.unused_disks()
        block_disk = disks[0]
        db_wal_disk = disks[1]
        #
        # Create an OSD with two disks (one for block, 
        # the other for block.db and block.wal ) and then destroy osd.
        #
        osd_uuid1 = str(uuid.uuid1())
        c.sh("ceph-disk --verbose zap " + block_disk + " " + db_wal_disk)
        c.conf['global']['osd objectstore'] = 'bluestore'
        c.save_conf()
        c.sh("ceph-disk --verbose prepare --bluestore --osd-uuid " + 
             osd_uuid1 + " " + block_disk + " --block.db " + db_wal_disk + 
             " --block.wal " + db_wal_disk)
        c.wait_for_osd_up(osd_uuid1)
        blockdb_partition = c.get_blockdb_partition(osd_uuid1)
        blockdb_path = blockdb_partition['path']
        blockwal_partition = c.get_blockwal_partition(osd_uuid1)
        blockwal_path = blockwal_partition['path']
        c.destroy_osd(osd_uuid1)
        c.sh("ceph-disk --verbose zap " + block_disk)
        #
        # Create another OSD with the block.db and block.wal partition 
        # of the previous OSD
        #
        osd_uuid2 = str(uuid.uuid1())
        c.sh("ceph-disk --verbose prepare --bluestore --osd-uuid " + 
             osd_uuid2 + " " + block_disk + " --block.db " + blockdb_path + 
             " --block.wal " + blockwal_path)
        c.wait_for_osd_up(osd_uuid2)
        device = json.loads(c.sh("ceph-disk list --format json " + block_disk))[0]
        assert len(device['partitions']) == 2
        device = json.loads(c.sh("ceph-disk list --format json " + db_wal_disk))[0]
        assert len(device['partitions']) == 2
        c.check_osd_status(osd_uuid2, 'block')
        c.check_osd_status(osd_uuid2, 'block.wal')
        c.check_osd_status(osd_uuid2, 'block.db')
        blockdb_partition = c.get_blockdb_partition(osd_uuid2)
        blockwal_partition = c.get_blockwal_partition(osd_uuid2)
        #
        # Verify the previous OSD partition has been reused
        #
        assert blockdb_partition['path'] == blockdb_path
        assert blockwal_partition['path'] == blockwal_path
        c.destroy_osd(osd_uuid2)
        c.sh("ceph-disk --verbose zap " + block_disk + " " + db_wal_disk)

    def test_activate_with_journal_dev_is_symlink(self):
        c = CephDisk()
        disk = c.unused_disks()[0]
        osd_uuid = str(uuid.uuid1())
        tempdir = tempfile.mkdtemp()
        symlink = os.path.join(tempdir, 'osd')
        os.symlink(disk, symlink)
        c.sh("ceph-disk --verbose zap " + symlink)
        c.sh("ceph-disk --verbose prepare --filestore --osd-uuid " + osd_uuid +
             " " + symlink)
        c.wait_for_osd_up(osd_uuid)
        device = json.loads(c.sh("ceph-disk list --format json " + symlink))[0]
        assert len(device['partitions']) == 2
        data_partition = c.get_osd_partition(osd_uuid)
        assert data_partition['type'] == 'data'
        assert data_partition['state'] == 'active'
        journal_partition = c.get_journal_partition(osd_uuid)
        assert journal_partition
        c.helper("pool_read_write")
        c.destroy_osd(osd_uuid)
        c.sh("ceph-disk --verbose zap " + symlink)
        os.unlink(symlink)
        os.rmdir(tempdir)

    def test_activate_journal_file(self):
        c = CephDisk()
        disks = c.unused_disks()
        data_disk = disks[0]
        #
        # /var/lib/ceph/osd is required otherwise it may violate
        # restrictions enforced by systemd regarding the directories
        # which ceph-osd is allowed to read/write
        #
        tempdir = tempfile.mkdtemp(dir='/var/lib/ceph/osd')
        c.sh("chown ceph:ceph " + tempdir + " || true")
        journal_file = os.path.join(tempdir, 'journal')
        osd_uuid = str(uuid.uuid1())
        c.sh("ceph-disk --verbose prepare --filestore --osd-uuid " + osd_uuid +
             " " + data_disk + " " + journal_file)
        c.wait_for_osd_up(osd_uuid)
        device = json.loads(
            c.sh("ceph-disk list --format json " + data_disk))[0]
        assert len(device['partitions']) == 1
        partition = device['partitions'][0]
        assert journal_file == os.readlink(
            os.path.join(partition['mount'], 'journal'))
        c.check_osd_status(osd_uuid)
        c.helper("pool_read_write 1")  # 1 == pool size
        c.destroy_osd(osd_uuid)
        c.sh("ceph-disk --verbose zap " + data_disk)
        os.unlink(journal_file)
        os.rmdir(tempdir)

    def test_activate_separated_journal(self):
        c = CephDisk()
        disks = c.unused_disks()
        data_disk = disks[0]
        journal_disk = disks[1]
        osd_uuid = self.activate_separated_journal(data_disk, journal_disk)
        c.helper("pool_read_write 1")  # 1 == pool size
        c.destroy_osd(osd_uuid)
        c.sh("ceph-disk --verbose zap " + data_disk + " " + journal_disk)

    def test_activate_separated_journal_dev_is_symlink(self):
        c = CephDisk()
        disks = c.unused_disks()
        data_disk = disks[0]
        journal_disk = disks[1]
        tempdir = tempfile.mkdtemp()
        data_symlink = os.path.join(tempdir, 'osd')
        os.symlink(data_disk, data_symlink)
        journal_symlink = os.path.join(tempdir, 'journal')
        os.symlink(journal_disk, journal_symlink)
        osd_uuid = self.activate_separated_journal(
            data_symlink, journal_symlink)
        c.helper("pool_read_write 1")  # 1 == pool size
        c.destroy_osd(osd_uuid)
        c.sh("ceph-disk --verbose zap " + data_symlink + " " + journal_symlink)
        os.unlink(data_symlink)
        os.unlink(journal_symlink)
        os.rmdir(tempdir)

    def activate_separated_journal(self, data_disk, journal_disk):
        c = CephDisk()
        osd_uuid = str(uuid.uuid1())
        c.sh("ceph-disk --verbose prepare --filestore --osd-uuid " + osd_uuid +
             " " + data_disk + " " + journal_disk)
        c.wait_for_osd_up(osd_uuid)
        device = json.loads(
            c.sh("ceph-disk list --format json " + data_disk))[0]
        assert len(device['partitions']) == 1
        c.check_osd_status(osd_uuid, 'journal')
        return osd_uuid

    #
    # Create an OSD and get a journal partition from a disk that
    # already contains a journal partition which is in use. Updates of
    # the kernel partition table may behave differently when a
    # partition is in use. See http://tracker.ceph.com/issues/7334 for
    # more information.
    #
    def test_activate_two_separated_journal(self):
        c = CephDisk()
        disks = c.unused_disks()
        data_disk = disks[0]
        other_data_disk = disks[1]
        journal_disk = disks[2]
        osd_uuid = self.activate_separated_journal(data_disk, journal_disk)
        other_osd_uuid = self.activate_separated_journal(
            other_data_disk, journal_disk)
        #
        # read/write can only succeed if the two osds are up because
        # the pool needs two OSD
        #
        c.helper("pool_read_write 2")  # 2 == pool size
        c.destroy_osd(osd_uuid)
        c.destroy_osd(other_osd_uuid)
        c.sh("ceph-disk --verbose zap " + data_disk + " " +
             journal_disk + " " + other_data_disk)

    #
    # Create an OSD and reuse an existing journal partition
    #
    def test_activate_reuse_journal(self):
        c = CephDisk()
        disks = c.unused_disks()
        data_disk = disks[0]
        journal_disk = disks[1]
        #
        # Create an OSD with a separated journal and destroy it.
        #
        osd_uuid = self.activate_separated_journal(data_disk, journal_disk)
        journal_partition = c.get_journal_partition(osd_uuid)
        journal_path = journal_partition['path']
        c.destroy_osd(osd_uuid)
        c.sh("ceph-disk --verbose zap " + data_disk)
        osd_uuid = str(uuid.uuid1())
        #
        # Create another OSD with the journal partition of the previous OSD
        #
        c.sh("ceph-disk --verbose prepare --filestore --osd-uuid " + osd_uuid +
             " " + data_disk + " " + journal_path)
        c.helper("pool_read_write 1")  # 1 == pool size
        c.wait_for_osd_up(osd_uuid)
        device = json.loads(
            c.sh("ceph-disk list --format json " + data_disk))[0]
        assert len(device['partitions']) == 1
        c.check_osd_status(osd_uuid)
        journal_partition = c.get_journal_partition(osd_uuid)
        #
        # Verify the previous OSD partition has been reused
        #
        assert journal_partition['path'] == journal_path
        c.destroy_osd(osd_uuid)
        c.sh("ceph-disk --verbose zap " + data_disk + " " + journal_disk)

    def test_activate_multipath(self):
        c = CephDisk()
        if c.sh("lsb_release -si").strip() != 'CentOS':
            pytest.skip(
                "see issue https://bugs.launchpad.net/ubuntu/+source/multipath-tools/+bug/1488688")
        c.ensure_sd()
        #
        # Figure out the name of the multipath device
        #
        disk = c.unused_disks('sd.')[0]
        c.sh("mpathconf --enable || true")
        c.sh("multipath " + disk)
        holders = os.listdir(
            "/sys/block/" + os.path.basename(disk) + "/holders")
        assert 1 == len(holders)
        name = open("/sys/block/" + holders[0] + "/dm/name").read()
        multipath = "/dev/mapper/" + name
        #
        # Prepare the multipath device
        #
        osd_uuid = str(uuid.uuid1())
        c.sh("ceph-disk --verbose zap " + multipath)
        c.sh("ceph-disk --verbose prepare --filestore --osd-uuid " + osd_uuid +
             " " + multipath)
        c.wait_for_osd_up(osd_uuid)
        device = json.loads(
            c.sh("ceph-disk list --format json " + multipath))[0]
        assert len(device['partitions']) == 2
        data_partition = c.get_osd_partition(osd_uuid)
        assert data_partition['type'] == 'data'
        assert data_partition['state'] == 'active'
        journal_partition = c.get_journal_partition(osd_uuid)
        assert journal_partition
        c.helper("pool_read_write")
        c.destroy_osd(osd_uuid)
        c.sh("udevadm settle")
        c.sh("multipath -F")
        c.unload_scsi_debug()


class CephDiskTest(CephDisk):

    def main(self, argv):
        parser = argparse.ArgumentParser(
            'ceph-disk-test',
        )
        parser.add_argument(
            '-v', '--verbose',
            action='store_true', default=None,
            help='be more verbose',
        )
        parser.add_argument(
            '--destroy-osd',
            help='stop, umount and destroy',
        )
        args = parser.parse_args(argv)

        if args.verbose:
            logging.basicConfig(level=logging.DEBUG)

        if args.destroy_osd:
            dump = json.loads(CephDisk.sh("ceph osd dump -f json"))
            osd_uuid = None
            for osd in dump['osds']:
                if str(osd['osd']) == args.destroy_osd:
                    osd_uuid = osd['uuid']
            if osd_uuid:
                self.destroy_osd(osd_uuid)
            else:
                raise Exception("cannot find OSD " + args.destroy_osd +
                                " ceph osd dump -f json")
            return

if __name__ == '__main__':
    sys.exit(CephDiskTest().main(sys.argv[1:]))