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