-
Notifications
You must be signed in to change notification settings - Fork 0
/
lvmautomation.py
257 lines (232 loc) · 6.5 KB
/
lvmautomation.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
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
import subprocess
import time
print()
print()
print("Welcome to the program for LVM Automation")
#step2
ch='y'
pvdisks=''
while(ch=='y'or ch=='Y'):
print('''Following is the list of devices available to be used as LVM:
/sdf : 1G
/sdg : 2G
/sdh : 3G
/sdi : 4G
/sdc : 20G
/sde : 30G
/sdd : 40G''')
x=input('Enter the disk you want to attach(1,2,3,4,2...) :')
if x=='1':
subprocess.getstatusoutput('pvcreate /dev/sdf')
pvdisks=pvdisks+' '+'/dev/sdf'
elif x=='2':
subprocess.getstatusoutput('pvcreate /dev/sdg')
pvdisks=pvdisks+' '+'/dev/sdg'
elif x=='3':
subprocess.getstatusoutput('pvcreate /dev/sdh')
pvdisks=pvdisks+' '+'/dev/sdh'
elif x=='4':
subprocess.getstatusoutput('pvcreate /dev/sdi')
pvdisks=pvdisks+' '+'/dev/sdi'
elif x=='20':
subprocess.getstatusoutput('pvcreate /dev/sdc')
pvdisks=pvdisks+' '+'/dev/sdc'
elif x=='30':
subprocess.getstatusoutput('pvcreate /dev/sde')
pvdisks=pvdisks+' '+'/dev/sde'
elif x=='40':
subprocess.getstatusoutput('pvcreate /dev/sdd')
pvdisks=pvdisks+' '+'/dev/sdd'
else:
print('Please enter a valid data')
exit()
ch=input('Do you want to enter more devices?(y/n)')
#print(pvdisks)
print('')
vgname=input('Enter the name you want to give to you Volume Group: ')
cmdforvgcreate='vgcreate {}{}'.format(vgname,pvdisks)
#print('command for vg create:',cmdforvgcreate)
try:
vgcreated=subprocess.getstatusoutput(cmdforvgcreate) #systemcommand
if vgcreated[0]==0:
print('Executing command...')
time.sleep(1)
print('Volume Group created successfully!')
else:
print('Unsuccessful')
exit()
except:
print('System error')
exit()
print('')
lvname=input('Enter the name of logical volume you want to create: ')
lvsize=input('Enter the size of the logical volume(e.g 500M, 1G): ')
cmdforlvcreate='lvcreate --size {} --name {} {}'.format(lvsize,lvname,vgname)
#print('command for lv create:', cmdforlvcreate)
try:
lvcreated=subprocess.getstatusoutput(cmdforlvcreate) #systemcommand
if lvcreated[0]==0:
print('Executing command...')
time.sleep(1)
print('Logical Volume created successfully!')
else:
print('Unsuccessful')
exit()
except:
print('System error')
exit()
print('')
fstype=input('Enter the format type for your logical volume:')
cmdforformatting='mkfs.{} /dev/{}/{}'.format(fstype,vgname,lvname)
#print('Command for formatting:',cmdforformatting)
try:
formatted=subprocess.getstatusoutput(cmdforformatting) #systemcommand
if formatted[0]==0:
print('Executing command...')
time.sleep(1)
print('Formatting successfully!')
else:
print('Unsuccessful')
exit()
except:
print('System error')
exit()
print('')
dirname=input('Enter the name of directory for mounting your logical volume:')
cmdformkdir='mkdir /{}'.format(dirname)
try:
dircreated=subprocess.getstatusoutput(cmdformkdir) #systemcommand
if dircreated[0]==0:
print('Executing command...')
time.sleep(1)
print('Directory created successfully!')
else:
print('Unsuccessful')
exit()
except:
print('System error')
exit()
cmdformounting='mount /dev/{}/{} /{}'.format(vgname,lvname,dirname)
#print('Command for mounting: ',cmdformounting)
try:
mounted=subprocess.getstatusoutput(cmdformounting) #systemcommand
if mounted[0]==0:
print('Executing command...')
time.sleep(1)
print('Formatting successfully!')
else:
print('Unsuccessful')
exit()
except:
print('System error')
exit()
print('')
inc=input('Do you want to increase the size of logical volume?(y/n)')
if inc=='n':
exit()
incrementsize=input('Enter the increment size (e.g 500M, 800M, 1G, 2G:): ')
cmdforlvextend='lvextend --size +{} /dev/{}/{}'.format(incrementsize,vgname,lvname)
print('Command for lvextend: ',cmdforlvextend)
try:
lvextended=subprocess.getstatusoutput(cmdforlvextend) #systemcommand
if lvextended[0]==0:
print('Executing command...')
time.sleep(1)
print('Logical volume extended successfully!')
else:
print('Unsuccessful')
exit()
except:
print('System error')
exit()
print('')
cmdforfsresizing='resize2fs /dev/{}/{}'.format(vgname,lvname)
#print('command for resizing file system:', cmdforfsresizing)
try:
fsresized=subprocess.getstatusoutput(cmdforfsresizing) #systemcommand
if fsresized[0]==0:
print('Executing command...')
time.sleep(1)
print('File system resized successfully!')
else:
print('Unsuccessful')
exit()
except:
print('System error')
exit()
print('')
choice=input('Do you want to add more storage device to your volume group?(y/n)')
if choice=='n':
exit()
y=input('''Enter the disk you want to attach(1,2,3,4,2...)
/sdf : 1G
/sdg : 2G
/sdh : 3G
/sdi : 4G
/sdc : 20G
/sde : 30G
/sdd : 40G: ''')
if y=='1':
vgextenddevice='/dev/sdf'
elif y=='2':
vgextenddevice='/dev/sdg'
elif y=='3':
vgextenddevice='/dev/sdh'
elif y=='4':
vgextenddevice='/dev/sdi'
elif y=='20':
vgextenddevice='/dev/sdc'
elif y=='30':
vgextenddevice='/dev/sde'
elif y=='40':
vgextenddevice='/dev/sdd'
else:
print('Please enter a valid data')
exit()
cmdforvgextend='vgextend {} {}'.format(vgname,vgextenddevice)
try:
vgextened=subprocess.getstatusoutput(cmdforvgextend) #systemcommand
if vgextened[0]==0:
print('Executing command...')
time.sleep(1)
print('Volume group extended successfully!')
else:
print('Unsuccessful')
exit()
except:
print('System error')
exit()
print('')
inc=input('Do you want to add more space to your logical volume now?(y/n)')
if choice=='n':
exit()
lvextendsize=input('''Enter size of logical volume you want to increase (e.g 500M, 800M, 1G, 2G): ''')
cmdforlvextend='lvextend --size +{} /dev/{}/{}'.format(lvextendsize,vgname,lvname)
#print('Command for lvextend: ',cmdforlvextend)
try:
lvextenedagain=subprocess.getstatusoutput(cmdforlvextend) #systemcommand
if lvextenedagain[0]==0:
print('Executing command...')
time.sleep(1)
print('Logical volume extended successfully!')
else:
print('Unsuccessful')
exit()
except:
print('System error')
exit()
print('')
cmdforfsresizing='resize2fs /dev/{}/{}'.format(vgname,lvname)
#print('command for resizing file system:', cmdforfsresizing)
try:
fsresizedagain=subprocess.getstatusoutput(cmdforfsresizing) #systemcommand
if fsresizedagain[0]==0:
print('Executing command...')
time.sleep(1)
print('File system resized successfully!')
else:
print('Unsuccessful')
exit()
except:
print('System error')
exit()