-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLVM.py
184 lines (165 loc) · 9.81 KB
/
LVM.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
import subprocess
import shutil
import os
import colorama
from colorama import Fore
import random
colors = list(vars(colorama.Fore).values())
class LVM():
def __init__(self, size=0, disk_name="", source_name="", mount_folder="", increase_size=0, decrease_size=0, hd_name=""):
self.__size = size
self.__disk_name = disk_name
self.__source_name = source_name
self.__mount_folder = mount_folder
self.__increase_size = increase_size
self.__decrease_size = decrease_size
self.__hd_name = hd_name
print("\n\n\n")
print(random.choice(colors)+"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print(random.choice(colors)+"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print(random.choice(colors)+"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print(random.choice(colors)+"++++ ++++")
print(random.choice(colors)+"++++ +++ +++ +++ ++++++ ++++++ ++++")
print(random.choice(colors)+"++++ +++ +++ +++ +++ +++ +++ +++ ++++")
print(random.choice(colors)+"++++ +++ +++ +++ +++ +++ +++ +++ ++++")
print(random.choice(colors)+"++++ +++ +++ +++ +++ ++++++ +++ ++++")
print(random.choice(colors)+"++++ +++ +++ +++ +++ +++ ++++")
print(random.choice(colors)+"++++ ++++++++++ ++ + ++ +++ +++ ++++")
print(random.choice(colors)+"++++ ++++++++++ ++++++ +++ +++ ++++")
print(random.choice(colors)+"++++ ++++")
print(random.choice(colors)+"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print(random.choice(colors)+"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print(random.choice(colors)+"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print("\n\n\n")
def showUnusedStorage(self):
print("---------------------------------------SHOWING PARTITIONS------------------------------------------")
print("Showing all partitions in your OS....")
subprocess.run(["fdisk","-l"], check=True)
def createPhysicalPartition(self):
print("------------------------------------CREATING PHYSICAL PARTITION-------------------------------------")
print("What is the name of partition")
self.__hd_name = input()
name = "/dev/"+ self.__hd_name
subprocess.run(["pvcreate" , name],check=True)
output = subprocess.run(["pvdisplay", name], check=True)
print(output)
def createVisualPartition(self):
print("------------------------------------CREATING VIRTUAL PARTITION-------------------------------------")
print("What is name of the first hard disk")
storage1 = input()
print("What is name of the second hard disk")
storage2 = input()
print("What is the name of virtual hard disk")
self.__source_name = input()
name1 = "/dev/" + storage1
name2 = "/dev/" + storage2
subprocess.run(["vgcreate" , self.__source_name , name1 , name2], check=True)
output = subprocess.run(["vgdisplay" ,self.__source_name], check=True)
print(output)
def createLVMPartition(self):
print("----------------------------- CREATING LVM PARTITION ------------------------------------------")
self.__size = int(input("What is the size of partition which you want to create?"))
print("What is the disk name ")
size = str(self.__size) + "G"
self.__disk_name = input()
print("What is the source disk name ")
self.__source_name = input()
name = self.__source_name + "/" + self.__disk_name
subprocess.run(["lvcreate", "--size" ,size,"--name", self.__disk_name ,self.__source_name],check=True)
output = subprocess.run(["lvdisplay", name], check=True)
print(output)
print("------------------------------ FORMATTING PARTITION ------------------------------------------")
LVM.formatPartition(self)
def formatPartition(self):
print("Now Formatting Your Partition")
name = "/dev/" + self.__source_name + "/" + self.__disk_name
output = subprocess.run(["mkfs.ext4", name], check=True)
print(output)
print("------------------------------ MOUNT PARTITION ----------------------------------------------")
LVM.mountPartition(self)
def mountPartition(self):
print("Now Mount the Partition")
print("What is the name of mount folder")
self.__mount_folder = input()
name = "/dev/" + self.__source_name + "/" + self.__disk_name
foldername = "/" + self.__mount_folder
try:
subprocess.run(["mkdir", foldername], check=True)
print("Mounting Folder successfully created")
except:
print("Mounting Folder successfully found")
output = subprocess.run(["mount", name, foldername], check=True)
print(output)
print("--------------------- SUCCESSFULLY DONE ALL THREE STEPS ------------------------------------")
def increasePartition(self):
print("-----------------------------INCREASING PARTITION-----------------------------")
self.__increase_size = int(input("How much you want to extend the partition?... "))
size = "+" + str(self.__increase_size) + "G"
self.__source_name = input("Enter source partition name... ")
self.__disk_name = input("Enter disk name.. ")
name = "/dev/" + self.__source_name + "/" + self.__disk_name
print("------------------------------EXTEND FILE SYSTEM-------------------------")
subprocess.run(["lvextend" , "--size", size , name], check=True)
print("-----------------------------CHECK FILE SYSTEM--------------------------")
subprocess.run(["resize2fs",name], check=True)
def decreasePartition(self):
print("----------------------------DECREASING PARTITION---------------------")
#STEPS
#unmount the file system for reducing.
#check the file system after unmount.
#Reduce the file system.
#Reduce the Logical Volume size than Current size.
#Recheck the file system for error.
#Remount the file-system back to stage.
self.__decrease_size = int(input("How much do you want to reduce the partition?... "))
print("-----------------------UNMOUNTING FILE-SYSTEM---------------------")
foldername = "/" + self.__mount_folder
subprocess.run(["umount","-v", foldername],check=True)
name = "/dev/" + self.__source_name + "/" + self.__disk_name
print("------------------------CHECK FILE SYSTEM------------------------")
subprocess.run(["e2fsck","-ff" , name],check=True)
size = str(self.__size - self.__decrease_size) + "G"
print("-------------------------REDUCE FILE SYSTEM-----------------------")
subprocess.run(["resize2fs",name,size],check=True)
decreasedsize = "-" + str(self.__decrease_size) + "G"
print("-------------------------REDUCE LOGICAL VOLUME---------------------")
subprocess.run(["lvreduce", "-L",decreasedsize , name ],check=True)
subprocess.run(["lvdisplay",self.__disk_name],check=True)
print("--------------------------RECHECK FILE SYSTEM----------------------")
subprocess.run(["resize2fs", name],check=True)
print("--------------------------MOUNT FILE-SYSTEM-------------------------")
subprocess.run(["mount", name , foldername],check=True)
subprocess.run(["lvdisplay",self.__disk_name],check=True)
def showPartition(self):
print("Partitions are as following")
output = subprocess.run(["df", "-h"],check=True)
print(output)
if __name__ == '__main__':
l = LVM()
while True:
print(random.choice(colors) + "Select What do you want....." + random.choice(colors) + "\n1.Show all Partitions\n" + random.choice(colors) + "2.Create Physical Partition\n" + random.choice(colors) + "3.Create Visual Partition\n" + random.choice(colors) + "4.Create LVM Partition\n" + random.choice(colors) + "5.Increase Partition Size\n" + random.choice(colors) +"6.Decrease Partition Size\n" + random.choice(colors) +"7.Show LVM Partition\n" + random.choice(colors) + "8.Exit\n" + random.choice(colors) + "Enter Choice...." + random.choice(colors))
choice = int(input())
if choice == 1:
print(random.choice(colors))
l.showUnusedStorage()
elif choice == 2:
print(random.choice(colors))
l.createPhysicalPartition()
elif choice == 3:
print(random.choice(colors))
l.createVisualPartition()
elif choice == 4:
print(random.choice(colors))
l.createLVMPartition()
elif choice == 5:
print(random.choice(colors))
l.increasePartition()
elif choice == 6:
print(random.choice(colors))
l.decreasePartition()
elif choice == 7:
print(random.choice(colors))
l.showPartition()
elif choice == 8:
print(random.choice(colors) + "----------------------------------------EXITING--------------------------------------")
break