-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmount-volumes.py
executable file
·84 lines (69 loc) · 2.36 KB
/
mount-volumes.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
78
79
80
81
82
83
84
#!/usr/bin/python3
#
# Setup and mount a RAID0 array
#
import requests
import boto3
import sys
import time
import subprocess
import platform
import json
volumes = json.loads(sys.stdin.read())
mountpoint=sys.argv[1]
print('Volumes: ' + str(volumes))
print('Mountpoint: ' + str(mountpoint))
r = requests.get("http://169.254.169.254/latest/dynamic/instance-identity/document")
response_json = r.json()
region = response_json.get('region')
availability_zone = response_json.get('availabilityZone')
instance_id = response_json.get('instanceId')
print('instance_id=' + instance_id)
def run_command(command):
p = subprocess.Popen(command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
return iter(p.stdout.readline, b'')
ec2 = boto3.resource('ec2',region_name=region)
client = boto3.client('ec2',region_name=region)
print("Creating the RAID0 Array...Please wait...")
cmd = "mdadm --create --verbose /dev/md0 --level=0 --name=MY_RAID --raid-devices=" + str(len(volumes)) + " " + " ".join(volumes)
print(cmd)
command_user = cmd.split()
subprocess.run(command_user)
time.sleep(30)
comm=('mdadm --detail --scan').split()
for line in run_command(comm):
print(line)
with open('/etc/mdadm/mdadm.conf', 'a') as fd:
fd.write(line.decode('utf-8'))
print("Creating filesystem ...")
comm2 = ("mkfs.ext4 -L MY_RAID /dev/md0").split()
run_command(comm2)
time.sleep(10)
comm_mkdir=("mkdir -p "+ mountpoint).split()
subprocess.run(comm_mkdir)
print("Mounting " + mountpoint + " and adding to FSTAB")
comm_mount=("mount /dev/md0 " +mountpoint).split()
subprocess.run(comm_mount)
comm=("lsblk /dev/md0 -J -o UUID").split()
r = subprocess.check_output(comm)
response_json = json.loads(r.decode('utf-8'))
md_UUID='UUID='+response_json.get('blockdevices')[0].get('uuid')
file = open('/etc/fstab','r')
found=0
for line in file:
if md_UUID in line:
if mountpoint in line:
found = 1
if found !=1:
file2=open('/etc/fstab','a')
# line_to_add = md_UUID +" " + mountpoint + " ext4 defaults,nofail 0 2\n"
line_to_add = "LABEL=MY_RAID " + mountpoint + " ext4 defaults,nofail 0 2\n"
file2.write(line_to_add)
file2.close()
file.close()
print("Updating bootimg...")
comm=("update-initramfs -u").split()
run_command(comm)
time.sleep(5)