Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / ceph-volume / ceph_volume / devices / lvm / zap.py
1 import argparse
2 import logging
3
4 from textwrap import dedent
5
6 from ceph_volume import decorators, terminal, process
7 from ceph_volume.api import lvm as api
8
9 logger = logging.getLogger(__name__)
10
11
12 def wipefs(path):
13     """
14     Removes the filesystem from an lv or partition.
15     """
16     process.run([
17         'wipefs',
18         '--all',
19         path
20     ])
21
22
23 def zap_data(path):
24     """
25     Clears all data from the given path. Path should be
26     an absolute path to an lv or partition.
27
28     10M of data is written to the path to make sure that
29     there is no trace left of any previous Filesystem.
30     """
31     process.run([
32         'dd',
33         'if=/dev/zero',
34         'of={path}'.format(path=path),
35         'bs=1M',
36         'count=10',
37     ])
38
39
40 class Zap(object):
41
42     help = 'Removes all data and filesystems from a logical volume or partition.'
43
44     def __init__(self, argv):
45         self.argv = argv
46
47     @decorators.needs_root
48     def zap(self, args):
49         device = args.device
50         lv = api.get_lv_from_argument(device)
51         if lv:
52             # we are zapping a logical volume
53             path = lv.lv_path
54         else:
55             # we are zapping a partition
56             #TODO: ensure device is a partition
57             path = device
58
59         logger.info("Zapping: %s", path)
60         terminal.write("Zapping: %s" % path)
61
62         wipefs(path)
63         zap_data(path)
64
65         if lv:
66             # remove all lvm metadata
67             lv.clear_tags()
68
69         terminal.success("Zapping successful for: %s" % path)
70
71     def main(self):
72         sub_command_help = dedent("""
73         Zaps the given logical volume or partition. If given a path to a logical
74         volume it must be in the format of vg/lv. Any filesystems present
75         on the given lv or partition will be removed and all data will be purged.
76
77         However, the lv or partition will be kept intact.
78
79         Example calls for supported scenarios:
80
81           Zapping a logical volume:
82
83               ceph-volume lvm zap {vg name/lv name}
84
85           Zapping a partition:
86
87               ceph-volume lvm zap /dev/sdc1
88
89         """)
90         parser = argparse.ArgumentParser(
91             prog='ceph-volume lvm zap',
92             formatter_class=argparse.RawDescriptionHelpFormatter,
93             description=sub_command_help,
94         )
95
96         parser.add_argument(
97             'device',
98             metavar='DEVICE',
99             nargs='?',
100             help='Path to an lv (as vg/lv) or to a partition like /dev/sda1'
101         )
102         if len(self.argv) == 0:
103             print(sub_command_help)
104             return
105         args = parser.parse_args(self.argv)
106         self.zap(args)