-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlvm-helper.sh
637 lines (607 loc) · 20.5 KB
/
lvm-helper.sh
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
#!/bin/bash
# LVM Helper Script
# Disclaimer:
# This script is provided for educational purposes only. Use it at your own risk.
# The author is not responsible for any consequences or damages resulting from the use of this script.
# If you choose to run this script, make sure you understand the code and its implications.
VERSION="v0.0.1"
GR=$(tput setaf 2)
CY=$(tput setaf 6)
NC=$(tput sgr 0)
CHECK_MARK="\033[0;32m\xE2\x9C\x94\033[0m"
REQUIRED_UTILITIES=("pvcreate" "vgcreate" "lvcreate" "mkfs.ext4" "blkid")
TMP_LOG="lvm-helper.tmp.log"
# Check if the script is being run as root
echo -ne "Check if the script is being run with root privileges...\033[0K\r"
sleep 0.5
if [[ $(id -u) -ne 0 ]]; then
echo "To create LVM requires root privileges. Please run it as root user or use sudo."
exit 1
else
echo -e "\\r${CHECK_MARK} Script run with root privileges.\033[0K\r"
sleep 0.5
fi
# Check if the required utilities are installed
echo -ne "Check required utilities...\033[0K\r"
sleep 0.5
for utility in "${REQUIRED_UTILITIES[@]}"; do
if ! command -v "$utility" &>/dev/null; then
echo "$utility is not installed. Please install the required tools."
exit 1
fi
done
# If all required utilities are installed, continue operations.
echo -e "\\r${CHECK_MARK} All required utilities are installed. Proceed script operations...\033[0K\r"
sleep 0.5
# Create a tmp log file
if [ -e $TMP_LOG ]; then
>$TMP_LOG
fi
touch "$TMP_LOG"
# Display Main Menu
show_main_menu() {
clear
echo "|--------------------------------- ${CY}LVM Helper${NC} ---------------------------------|"
echo
echo "1. Create"
echo "2. Delete"
echo "3. Extend Size"
echo "4. Reduce Size"
echo "5. Show Info"
echo "6. Quit"
echo
echo "Note:"
echo "- This script can perform tasks such as creating, deleting, and resizing."
echo "- Extend and Reduce size only Logical Volume (LV) supported."
echo
echo "This script is provided for educational purposes only. Use it at your own risk."
echo
echo "|----------------------------------- ${GR}$VERSION${NC} -----------------------------------|"
}
# Display submenu 1
show_submenu1() {
clear
echo "|--------------------------------- ${CY}LVM Helper${NC} ---------------------------------|"
echo
echo "1. Create Physical Volumes (PV)"
echo "2. Create Volume Groups (VG)"
echo "3. Create Logical Volumes (LV)"
echo "4. Back to Main Menu"
echo
echo "|----------------------------------- ${GR}$VERSION${NC} -----------------------------------|"
}
# Display submenu 2
show_submenu2() {
clear
echo "|--------------------------------- ${CY}LVM Helper${NC} ---------------------------------|"
echo
echo "1. Delete Physical Volumes (PV)"
echo "2. Delete Volume Groups (VG)"
echo "3. Delete Logical Volumes (LV)"
echo "4. Back to Main Menu"
echo
echo "|----------------------------------- ${GR}$VERSION${NC} -----------------------------------|"
}
# Display submenu 3
show_submenu3() {
clear
echo "|--------------------------------- ${CY}LVM Helper${NC} ---------------------------------|"
echo
echo "1. Extend Logical Volumes (LV)"
echo "2. Back to Main Menu"
echo
echo "|----------------------------------- ${GR}$VERSION${NC} -----------------------------------|"
}
# Display submenu 4
show_submenu4() {
clear
echo "|--------------------------------- ${CY}LVM Helper${NC} ---------------------------------|"
echo
echo "1. Reduce Logical Volumes (LV)"
echo "2. Back to Main Menu"
echo
echo "|----------------------------------- ${GR}$VERSION${NC} -----------------------------------|"
}
# Confirmation function
confirm() {
while true; do
read -p "$1 [y/n]: " choice
case "$choice" in
[Yy]*)
break
;;
[Nn]*)
return 1
;;
*)
echo "Please enter 'y' for yes or 'n' for no."
;;
esac
done
return 0
}
# Get LVM Info
update_log() {
echo -ne "Updating LVM info...\033[0K\r"
lvm_info >$TMP_LOG 2>&1
sleep 0.5
echo -e "\\r${CHECK_MARK} LVM info updated!\033[0K\r"
}
# Create PV
create_pv() {
echo "Create Physical Volumes. Please fill required input."
while true; do
read -p 'Disk Path: ' VAR_DISK_PATH
if [[ -n "$VAR_DISK_PATH" ]]; then
break
else
echo "Input cannot be empty. Please try again."
fi
done
pvcreate $VAR_DISK_PATH
update_log
}
# Create VG
create_vg() {
echo "Create Volume Groups. Please fill required input."
while true; do
read -p 'PV Name: ' VAR_PV_NAME
if [[ -n "$VAR_PV_NAME" ]]; then
break
else
echo "Input cannot be empty. Please try again."
fi
done
while true; do
read -p 'VG Name: ' VAR_VG_NAME
if [[ -n "$VAR_VG_NAME" ]]; then
break
else
echo "Input cannot be empty. Please try again."
fi
done
vgcreate $VAR_VG_NAME $VAR_PV_NAME
update_log
}
# Create LV
create_lv() {
echo "Create Logical Volumes. Please fill required input."
while true; do
read -p 'VG Name: ' VAR_VG_NAME
if [[ -n "$VAR_VG_NAME" ]]; then
break
else
echo "Input cannot be empty. Please try again."
fi
done
while true; do
read -p 'LV Name: ' VAR_LV_NAME
if [[ -n "$VAR_LV_NAME" ]]; then
break
else
echo "Input cannot be empty. Please try again."
fi
done
while true; do
read -p 'LV Size: ' VAR_LV_SIZE
if [[ -n "$VAR_LV_SIZE" ]]; then
break
else
echo "Input cannot be empty. Please try again."
fi
done
while true; do
read -p 'Mountpoint: ' VAR_MOUNTPOINT
if [[ -n "$VAR_MOUNTPOINT" ]]; then
break
else
echo "Input cannot be empty. Please try again."
fi
done
lvcreate -L $VAR_LV_SIZE -n $VAR_LV_NAME $VAR_VG_NAME
mkfs.ext4 /dev/$VAR_VG_NAME/$VAR_LV_NAME
if [[ ! -d $VAR_MOUNTPOINT ]]; then
mkdir -p $VAR_MOUNTPOINT
fi
UUID=$(blkid -o value -s UUID /dev/$VAR_VG_NAME/$VAR_LV_NAME)
echo "UUID=$UUID $VAR_MOUNTPOINT ext4 defaults 0 2" >>/etc/fstab
mount -a
update_log
}
# Delete PV
delete_pv() {
echo "Delete Physical Volumes. Please fill required input."
while true; do
read -p 'PV Name: ' VAR_PV_NAME
if [[ -n "$VAR_PV_NAME" ]]; then
break
else
echo "Input cannot be empty. Please try again."
fi
done
pvremove $VAR_PV_NAME
update_log
}
# Delete VG
delete_vg() {
echo "Delete Volume Groups. Please fill required input."
while true; do
read -p 'VG Name: ' VAR_VG_NAME
if [[ -n "$VAR_VG_NAME" ]]; then
break
else
echo "Input cannot be empty. Please try again."
fi
done
vgremove $VAR_VG_NAME
update_log
}
# Delete LV
delete_lv() {
echo "Delete Logical Volumes. Please fill required input."
while true; do
read -p 'VG Name: ' VAR_VG_NAME
if [[ -n "$VAR_VG_NAME" ]]; then
break
else
echo "Input cannot be empty. Please try again."
fi
done
while true; do
read -p 'LV Name: ' VAR_LV_NAME
if [[ -n "$VAR_LV_NAME" ]]; then
break
else
echo "Input cannot be empty. Please try again."
fi
done
while true; do
read -p 'Mountpoint: ' VAR_MOUNTPOINT
if [[ -n "$VAR_MOUNTPOINT" ]]; then
break
else
echo "Input cannot be empty. Please try again."
fi
done
if mountpoint -q "$VAR_MOUNTPOINT"; then
umount "$VAR_MOUNTPOINT"
if [ $? -eq 0 ]; then
echo "Unmounted successfully."
else
echo "Unmounting failed."
fi
fi
UUID=$(blkid -o value -s UUID /dev/$VAR_VG_NAME/$VAR_LV_NAME)
line_to_delete="UUID=$UUID $VAR_MOUNTPOINT ext4 defaults 0 2"
tmpfile=$(mktemp)
grep -v "$line_to_delete" /etc/fstab >"$tmpfile"
mv "$tmpfile" /etc/fstab
wipefs -a /dev/$VAR_VG_NAME/$VAR_LV_NAME
lvchange -an /dev/$VAR_VG_NAME/$VAR_LV_NAME
lvremove /dev/$VAR_VG_NAME/$VAR_LV_NAME
if confirm "Do you want to delete mountpoint directory?"; then
rm -rf $VAR_MOUNTPOINT
fi
update_log
}
# Extend LV
extend_lv() {
echo "Extend Logical Volumes Size. Please fill required input."
while true; do
read -p 'VG Name: ' VAR_VG_NAME
if [[ -n "$VAR_VG_NAME" ]]; then
break
else
echo "Input cannot be empty. Please try again."
fi
done
while true; do
read -p 'LV Name: ' VAR_LV_NAME
if [[ -n "$VAR_LV_NAME" ]]; then
break
else
echo "Input cannot be empty. Please try again."
fi
done
while true; do
read -p 'Size Increase: ' VAR_INCR_SIZE
if [[ -n "$VAR_INCR_SIZE" ]]; then
break
else
echo "Input cannot be empty. Please try again."
fi
done
lvextend -r -L +$VAR_INCR_SIZE /dev/$VAR_VG_NAME/$VAR_LV_NAME
update_log
}
# Reduce LV
reduce_lv() {
echo "Reduce Logical Volumes Size. Please fill required input."
while true; do
read -p 'VG Name: ' VAR_VG_NAME
if [[ -n "$VAR_VG_NAME" ]]; then
break
else
echo "Input cannot be empty. Please try again."
fi
done
while true; do
read -p 'LV Name: ' VAR_LV_NAME
if [[ -n "$VAR_LV_NAME" ]]; then
break
else
echo "Input cannot be empty. Please try again."
fi
done
while true; do
read -p 'Size Decrease: ' VAR_DECR_SIZE
if [[ -n "$VAR_DECR_SIZE" ]]; then
break
else
echo "Input cannot be empty. Please try again."
fi
done
while true; do
read -p 'Mountpoint: ' VAR_MOUNTPOINT
if [[ -n "$VAR_MOUNTPOINT" ]]; then
break
else
echo "Input cannot be empty. Please try again."
fi
done
if mountpoint -q "$VAR_MOUNTPOINT"; then
umount "$VAR_MOUNTPOINT"
if [ $? -eq 0 ]; then
echo "Unmounted successfully."
else
echo "Unmounting failed."
fi
fi
fsck -fy /dev/$VAR_VG_NAME/$VAR_LV_NAME
lvreduce -r -L -$VAR_DECR_SIZE /dev/$VAR_VG_NAME/$VAR_LV_NAME
mount -a
update_log
}
# Perform Get LVM info function
lvm_info() {
if [ -e "$TMP_LOG" ]; then
>"$TMP_LOG"
fi
IFS=$'\n'
export IFS
phydis=$(pvdisplay | grep -E "PV Name|VG Name|PV Size" | awk 'NR%3{printf "%s,",$0;next}{print;}' | sed 's/PV Name//g' | sed 's/VG Name//g' | sed 's/PV Size//g' | sed 's/ //g')
if [ -f /etc/redhat-release ]; then
if [[ $(cat /etc/redhat-release | awk -F " " '{print$4}' | awk -F "." '{print$1}') -eq 7 ]]; then
logdis=$(lvdisplay | grep -E "LV Path|LV Size" | awk 'NR%2{printf "%s,",$0;next}{print;}' | sed 's/LV Path//g' | sed 's/LV Size//g' | sed 's/ //g' | sort)
else
logdis=$(lvdisplay | grep -E "LV Name|LV Size" | awk 'NR%2{printf "%s,",$0;next}{print;}' | sed 's/LV Name//g' | sed 's/LV Size//g' | sed 's/ //g' | sort)
fi
else
logdis=$(lvdisplay | grep -E "LV Name|LV Size" | awk 'NR%2{printf "%s,",$0;next}{print;}' | sed 's/LV Name//g' | sed 's/LV Size//g' | sed 's/ //g' | sort)
flag="norh"
fi
phyparttrans=""
print_heading() {
echo "| ${GR}PV Name${NC} | ${GR}PV Size${NC} | ${GR}VG Name${NC} | ${GR}VG Size${NC} | ${GR}LV Name${NC} | ${GR}LV Size${NC} | ${GR}Used${NC} | ${GR}Avail${NC} | ${GR}Mounted on${NC} |"
}
print_separator() {
echo "|-----------|-----------|--------------|-----------|---------------------------|-----------|--------|--------|-----------------------------|"
}
print_line() {
printf '%1s %-9s %1s %-9s %1s %-12s %1s %-9s %1s %-25s %1s %-9s %1s %-6s %1s %-6s %1s %-27s %1s\n' \
"|" "$1" "|" "$2" "|" "$3" "|" "$4" "|" "$5" "|" "$6" "|" "$7" "|" "$8" "|" "$9" "|"
}
echo "LVM Information:"
print_separator
print_heading
for phy in ${phydis}; do
phypart1=$(echo "${phy}" | awk -F "," '{print$1}')
phypart2=$(echo "${phy}" | awk -F "," '{print$2}')
phypart3=$(echo "${phy}" | awk -F "," '{print$3}' | cut -f1 -d"/" | tr -d '<')
if [ -z "$logdis" ]; then
volsize=$(vgdisplay "$phypart2" 2>/dev/null | awk '/VG Size/ {gsub("<", "", $3); print $3 $NF}')
print_separator
print_line "$phypart1" "$phypart3" "$phypart2" "$volsize" "" "" "" ""
fi
if [ "$phypart2" != "$phyparttrans" ]; then
phyparttrans="$phypart2"
counter=1
for log in ${logdis}; do
volsize=$(vgdisplay "$phypart2" 2>/dev/null | awk '/VG Size/ {gsub("<", "", $3); print $3 $NF}')
logpart1=$(echo "${log}" | awk -F "," '{print$1}')
logpart2=$(echo "${log}" | awk -F "," '{print$2}')
logpartmatch=$(echo "${logpart1}" | awk -F "/" '{print$3}')
if [ $counter -eq 1 ]; then
if [ "${phypart2}" == "${logpartmatch}" ]; then
mountpoint1=$(echo "$logpart1" | awk -F "/" '{print$3}')
mountpoint2=$(echo "$logpart1" | awk -F "/" '{print$4}')
df_output=$(df -h /dev/mapper/${mountpoint1}-${mountpoint2} 2>/dev/null | tail -1)
Used=$(echo "$df_output" | awk -F " " '{print$3}')
Avail=$(echo "$df_output" | awk -F " " '{print$2}')
Mounted=$(echo "$df_output" | awk -F " " '{print$3$NF}')
print_separator
if [ -e /dev/mapper/${mountpoint1}-${mountpoint2} ]; then
print_line "$phypart1" "$phypart3" "$phypart2" "$volsize" "$logpart1" "$logpart2" "$Used" "$Avail" "$Mounted"
else
print_line "$phypart1" "$phypart3" "$phypart2" "$volsize" "" "" "" "" ""
fi
counter=$(($counter + 1))
elif [[ "${flag}" == "norh" ]]; then
df_output=$(df -h /dev/${phypart2}/${logpart1} 2>/dev/null | tail -1)
Used=$(echo "$df_output" | awk -F " " '{print$3}')
Avail=$(echo "$df_output" | awk -F " " '{print$2}')
Mounted=$(echo "$df_output" | awk -F " " '{print$NF}')
print_separator
if [ -e /dev/mapper/${phypart2}-${logpart1} ]; then
print_line "$phypart1" "$phypart3" "$phypart2" "$volsize" "$logpart1" "$logpart2" "$Used" "$Avail" "$Mounted"
else
print_line "$phypart1" "$phypart3" "$phypart2" "$volsize" "" "" "" "" ""
fi
counter=$(($counter + 1))
fi
else
if [ "${phypart2}" == "${logpartmatch}" ]; then
mountpoint1=$(echo "$logpart1" | awk -F "/" '{print$3}')
mountpoint2=$(echo "$logpart1" | awk -F "/" '{print$4}')
df_output=$(df -h /dev/mapper/${mountpoint1}-${mountpoint2} 2>/dev/null | tail -1)
Used=$(echo "$df_output" | awk -F " " '{print$3}')
Avail=$(echo "$df_output" | awk -F " " '{print$2}')
Mounted=$(echo "$df_output" | awk -F " " '{print$NF}')
if [ -e /dev/mapper/${mountpoint1}-${mountpoint2} ]; then
print_line "" "" "" "" "$logpart1" "$logpart2" "$Used" "$Avail" "$Mounted"
fi
counter=$(($counter + 1))
elif [[ "${flag}" == "norh" ]]; then
df_output=$(df -h /dev/${phypart2}/${logpart1} 2>/dev/null | tail -1)
Used=$(echo "$df_output" | awk -F " " '{print$3}')
Avail=$(echo "$df_output" | awk -F " " '{print$2}')
Mounted=$(echo "$df_output" | awk -F " " '{print$NF}')
if [ -e /dev/mapper/${phypart2}-${logpart1} ]; then
print_line "" "" "" "" "$logpart1" "$logpart2" "$Used" "$Avail" "$Mounted"
fi
counter=$(($counter + 1))
fi
fi
done
# else
# print_line "|" "$phypart1" "|" "$phypart3" "|" "$phypart2" "|" "$volsize" "|" " " "|" " " "|" " " "|" " " "|" " " "|"
fi
done
print_separator
}
# Main Menu loop
while true; do
show_main_menu
if [ -s $TMP_LOG ]; then
cat $TMP_LOG
fi
echo
read -p "Enter your choice (1/2/3/etc...): " main_choice
case "$main_choice" in
1)
while true; do
show_submenu1
if [ -s $TMP_LOG ]; then
cat $TMP_LOG
fi
echo
read -p "Enter your choice (1/2/3/etc...): " submenu1_choice
case "$submenu1_choice" in
1)
create_pv
read -p "Press Enter to continue..."
;;
2)
create_vg
read -p "Press Enter to continue..."
;;
3)
create_lv
read -p "Press Enter to continue..."
;;
4)
break # Go back to the main menu
;;
*)
echo "Invalid choice. Please select a valid option (1/2/3/etc...)"
read -p "Press Enter to continue..."
;;
esac
done
;;
2)
while true; do
show_submenu2
if [ -s $TMP_LOG ]; then
cat $TMP_LOG
fi
echo
read -p "Enter your choice (1/2/3/etc...): " submenu2_choice
case "$submenu2_choice" in
1)
delete_pv
read -p "Press Enter to continue..."
;;
2)
delete_vg
read -p "Press Enter to continue..."
;;
3)
delete_lv
read -p "Press Enter to continue..."
;;
4)
break # Go back to the main menu
;;
*)
echo "Invalid choice. Please select a valid option (1/2/3/etc...)"
read -p "Press Enter to continue..."
;;
esac
done
;;
3)
while true; do
show_submenu3
if [ -s $TMP_LOG ]; then
cat $TMP_LOG
fi
echo
read -p "Enter your choice (1/2/3/etc...): " submenu3_choice
case "$submenu3_choice" in
1)
extend_lv
read -p "Press Enter to continue..."
;;
2)
break # Go back to the main menu
;;
*)
echo "Invalid choice. Please select a valid option (1/2/3/etc...)"
read -p "Press Enter to continue..."
;;
esac
done
;;
4)
while true; do
show_submenu4
if [ -s $TMP_LOG ]; then
cat $TMP_LOG
fi
echo
read -p "Enter your choice (1/2/3/etc...): " submenu4_choice
case "$submenu4_choice" in
1)
reduce_lv
read -p "Press Enter to continue..."
;;
2)
break # Go back to the main menu
;;
*)
echo "Invalid choice. Please select a valid option (1/2/3/etc...)"
read -p "Press Enter to continue..."
;;
esac
done
;;
5)
update_log
read -p "Press Enter to continue..."
;;
6)
rm -rf $TMP_LOG
echo -ne "Exiting script...\033[0K\r"
sleep 0.5
echo -e "Byebye!\033[0K\r"
sleep 0.5
clear
exit 0
;;
*)
echo "Invalid choice. Please select a valid option (1/2/3/etc..)"
read -p "Press Enter to continue..."
;;
esac
done