-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_protection.py
77 lines (63 loc) · 2.42 KB
/
data_protection.py
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
from logging import debug
from libutils import run_cmd
def data_protection_setup(devices, raid_name, raid_level):
"""creates a raid with given set of drives.
Args:
devices: list of the devices that needs to be included in the raid
raid_name: The name of the logical unit of raid that will be created
raid_level: level of raid that needs to be implemented.
Returns:
A tuple: return code, stderr, stdout"""
data_protection_reset(raid_name, devices)
debug("Creating raid {} level {} devices: {} ".format(raid_name, raid_level,
','.join(devices)))
cmd = "yes | sudo mdadm --create " + raid_name + " --level=" + str(raid_level) + \
" --raid-devices=" + str(len(devices)) + " " + ' '.join(devices) + ""
return run_cmd(cmd)
def data_protection_stop(raid_name):
"""stops a raid.
Args:
raid_name: The name of the raid to be stopped
Returns:
A tuple: return code, stderr, stdout"""
debug("Stopping raid {}".format(raid_name))
raid_dev = data_protection_get_dev(raid_name)
if not raid_dev:
debug("Raid "+raid_name+" not running")
return 0, "", ""
cmd = "sudo mdadm --stop " + raid_name + ""
return run_cmd(cmd)
def data_protection_start():
"""scans for raid devices and activates them
Args:
Returns:
A tuple: return code, stderr, stdout"""
debug("Starting raid")
cmd = "sudo mdadm --assemble --scan"
rc, err, out = run_cmd(cmd)
if rc:
cmd = "sudo mdadm --examine --scan | sudo tee /etc/mdadm/mdadm.conf"
return run_cmd(cmd)
return rc,err,out
def data_protection_reset(raid_device, devices):
"""resets all the devices in raid.
Args:
devices: list of devices that needs to be reset.
partition_number: Number of the partition to be delete
Returns:
A tuple: return code, stderr, stdout"""
debug("Resetting raid on devices {}".format(','.join(devices)))
return_code = 0
out_msg = ""
err_msg = ""
cmd="sudo mdadm --stop "+raid_device
run_cmd(cmd)
cmd = "sudo mdadm --zero-superblock " + ' '.join(devices) + ""
return run_cmd(cmd)
def data_protection_get_dev(raid_name):
cmd = "ls -l " +raid_name
rc, out, err = run_cmd(cmd)
if rc:
return ""
dev = "/dev/"+out.split("/")[-1]
return dev.rstrip("\n")