-
Notifications
You must be signed in to change notification settings - Fork 0
/
LVM_operations.sh
587 lines (429 loc) · 17.6 KB
/
LVM_operations.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
#!/bin/bash
#Hernán De León
NUMARGS=$#
OPTION="$1"
VOLUMEGROUP="0"
FREEVG="0"
DUMPPATH="/recovery"
#Colors
red=$'\e[1;31m'
grn=$'\e[1;32m'
yel=$'\e[1;33m'
blu=$'\e[1;34m'
mag=$'\e[1;35m'
cyn=$'\e[1;36m'
end=$'\e[0m'
LVreduce(){
printf " $cyn The actual FREE space inside $VOLUMEGROUP is: $end"
vgs --units m | grep $VOLUMEGROUP | awk '{print $7}'
printf "\n\n $cyn Available Logical Volumes inside $VOLUMEGROUP are: \n $end"
printf "LV LSize\n"
lvs | grep $VOLUMEGROUP | awk {'print $1 " " $4'}
printf "\n $cyn Insert the LV Name: $end "
read VOLUMENAME
#check if Volume Name exist
lvdisplay | grep 'LV Name' | grep $VOLUMENAME || clear
lvdisplay | grep 'LV Name' | grep $VOLUMENAME || printf "\n\nLogical Volume does not exist inside $VOLUMEGROUP, please put another name: \n\n\n\n"
lvdisplay | grep 'LV Name' | grep $VOLUMENAME || LVreduce
USED=$(df -h | grep $VOLUMENAME | awk '{print $3}')
AVAILABLE=$(df -h | grep $VOLUMENAME | awk '{print $4}')
###
#LOGICAL VOLUME PATH = /dev/VolumeGroup/Volumename
DEV="/dev/$VOLUMEGROUP/$VOLUMENAME"
# equivale a
#DEVICE MAPPER LV PATH = /dev/mapper/VolumeGroup-Volumename
DEV_MAP="/dev/mapper/$VOLUMEGROUP-$VOLUMENAME"
#PHYSICAL DEVICE PATH = /dev/mapper/VolumeGroup-Volumename
# PV_DEV=$(/dev/sdb1)
clear
# exit if the device is not mounted
mount | grep $VOLUMENAME || printf "$red \n\n The selcted device is not mounted." &&
mount | grep $VOLUMENAME || printf "$red Please MOUNT $VOLUMENAME before run this script \n\n." &&
lsblk | grep $VOLUMENAME | grep SWAP || mount | grep $VOLUMENAME || exit 0
clear
###### GET THE PARTITION TYPE ! SWAP - EXT4 - XFS
#swap or no swap
lsblk | grep $VOLUMENAME | grep SWAP && TYPE="swap"
if [[ "$TYPE" = "swap" ]]; then
clear
printf "$yel The device is mounted as SWAP $end"
USED=$(lvdisplay $VOLUMEGROUP/$VOLUMENAME | grep Size | awk '{print $3}')
printf "\n $cyn Insert the final size that you want to achieve in $VOLUMENAME"
printf "\n $yel Please also type the letter
$blu m $end or $blu G $end : "
read NEWSIZE
else
# GET MOUNT POINT
MOUNTPOINT=$(df | grep "$VOLUMENAME" | awk '{print $6}')
#GET PARTITION TYPE
type=$(mount | grep $MOUNTPOINT | awk '{printf $5}')
df -h | grep --color Used &&
df -h | grep $VOLUMENAME | grep --color "$USED"
printf "\n $cyn Partition type is $type "
printf "\n $cyn Insert the final size that you want to achieve in $VOLUMENAME"
printf "\n $yel Should be more than used $USED, also type the letter
$blu m $end or $blu G $end : "
read NEWSIZE
fi
# check required size
# if [ $USED -gt $NEWSIZE ]; then
# echo "The final scope $NEWSIZE is smaller than $USED, that is acctually used by $VOLUMENAME "
# LVreduce
# fi
}
generate_dump(){
## Generate dump from existing filesystem
##################################################install required
printf "\n $mag Creating Dump path in $DUMPPATH $end"
if [ ! -d "$DUMPPATH" ]; then
mkdir -p $DUMPPATH && printf "\n $mag Creating Dump path in $DUMPPATH $end"
fi
DUMMPATH=$("$DUMPPATH"/"$VOLUMENAME"_FILESYSTEM_DUMP_`date +%F`.dump)
printf "The mount point of $VOLUMENAME is $MOUNTPOINT "
#ext4 - xfs health check
if [[ "$TYPE" = "ext4" ]]; then
printf "\n $mag Installing required dump \n "
# REDHAT xfsdump -I || yum -y install xfsdump >/dev/null 2>&1
#SUSE zypper
xfsdump -I || zypper install dump
if [ $? -eq 0 ]; then
echo "Package xfsdump exist \n $end"
else
echo " $red Canceling as cannot install dump $end"
exit 1
fi
dump -0uf $DUMPPATH $DEV_MAP
if [ $? -eq 0 ]; then
echo "$grn Succesful dump generated at "$DUMPPATH"/"$VOLUMENAME"_FILESYSTEM_DUMP.dump $end"
else
echo "$red Canceling, dump incorrect $end"
exit 1
fi
else if [[ "$TYPE" = "xfs" ]]; then
printf "\n $mag Installing required xfsdump \n "
# REDHAT
xfsdump -I || yum -y install xfsdump >/dev/null 2>&1
#SUSE - zypper xfsdump -I || zypper install xfsdump
if [ $? -eq 0 ]; then
echo "Package xfsdump exist \n $end"
else
echo " $red Canceling as cannot install xfsdump $end"
exit 1
fi
xfsdump -J - $MOUNTPOINT > $DUMPPATH >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "$grn Succesful dump generated at "$DUMPPATH"/"$VOLUMENAME"_FILESYSTEM_DUMP.dump $end"
else
echo "$red Canceling, dump incorrect $end"
exit 1
fi
fi
fi
}
resize_force_umount(){
## Umount current filesystem
printf "\n $yel The used space in $VOLUMENAME is $USED Used and the size scope is $NEWSIZE \n"
#ext4 - xfs health check
if [[ "$TYPE" = "ext4" ]]; then
fsck -f $DEV || printf "$red The device selected has errors.\n $end" && exit 1
else if [[ "$TYPE" = "xfs" ]]; then
xfs_check $DEV fsck -f $DEV || printf "$red The device selected has errors.\n $end" && exit 1
fi
fi
printf " $yel We are about to umount, reduce and mount the $VOLUMENAME in $MOUNTPOINT $end \n "
are_you_sure
fuser -ck $MOUNTPOINT && >/dev/null 2>&1
umount $MOUNTPOINT >/dev/null 2>&1
if [ $? -eq 0 ]; then
printf "$grn Succesful umount $end \n\n"
else
printf "$red Unsuccesful umount.\n"
printf " trying to remount the $VOLUMENAME in $MOUNTPOINT $end "
mount $DEV $MOUNTPOINT #To try to remount if issue
df | grep $DEV_MAP | awk '{print $6}' && printf "$grn Succesfully Re-mounted $end \n\n"
exit 1
fi
}
## Destroy previous filesystem
# resize_destroy_lvm(){
# printf "$yel Removing previous filesystem $end "
# lvremove -f $DEV >/dev/null 2>&1
# if [ $? -ne 0 ]; then
# printf "$red Error on removal. Please check $VOLUMENAME status . $end"
# lvdisplay $VOLUMENAME
# exit 1
# fi
#}
## Check that there-s enought space free on the volumegrup previous to create any new per requirement
LVreduce2(){
# mountpoint
printf "The used space in $VOLUMENAME is $USED and the Available is $AVAILABLE \n"
#ext4 - xfs health check
if [[ "$TYPE" = "ext4" ]]; then
fsck -f $DEV || printf "$red The device selected has errors.\n $end" && exit 1
else if [[ "$TYPE" = "xfs" ]]; then
xfs_check $DEV fsck -f $DEV || printf "$red The device selected has errors.\n $end" && exit 1
fi
fi
# e2fsck -f $DEV &&
# resize2fs $DEV $NEWSIZE &&
# fsck -f $DEV &&
lvreduce --size $NEWSIZE $DEV
mount $DEV_MAP $MOUNTPOINT
lvdisplay $VOLUMEGROUP/$VOLUMENAME
printf "\n The used space in $VOLUMENAME is $USED Used and the Available is $AVAILABLE \n"
printf "\n --> NEW $VOLUMENAME size"
lvdisplay $DEV | grep "LV Size"
df | grep $DEV_MAP | awk '{print $6}' && printf "$grn Succesfully Re-mounted $end \n\n"
}
check_vg_free_space()
{
vgdisplay | grep 'VG Name' | awk '{print $3}'
printf "\n $cyn Insert an existing Volume Group name from below list: $end "
read VOLUMEGROUP
#checking if VG Name is wrong
vgdisplay | grep $VOLUMEGROUP >/dev/null 2>&1 || check_vg_free_space
# if VG Name is ok
FREEVG=$(vgdisplay --noheadings --columns $VOLUMEGROUP |awk '{print $7}')
}
## Create lvm
LVcreate(){
printf "\n $cyn Please select the partition type to create [ ext4 / xfs / swap ] \n "
read -p "\n For swap you can also use the option --swapfile : $end" TYPE
echo $TYPE | grep -E 'ext4|xfs|swap' || printf " $yel Please insert a valid option $end \n "
echo $TYPE | grep -E 'ext4|xfs|swap' || LVcreate
printf "$yel The actual FREE space inside $VOLUMEGROUP is: $end "
vgs --units m | grep $VOLUMEGROUP | awk '{print $7}'
printf "\nAvailable Logical Volumes inside $VOLUMEGROUP are: \n"
printf "LV LSize\n"
lvs | grep $VOLUMEGROUP | awk {'print $1 " " $4'}
printf "\n$cyn Insert the required Logical Volume Name: $end "
read VOLUMENAME
# if $VOLMENAME already exist in LVcreate and lvextend
if [[ "$STEP" = "lvextend" ]]; then
lvdisplay | grep 'LV Name' | grep $VOLUMENAME || printf "\n\n $yel Logical Volume to extend does not exist, please put a name from the list: \n $end "
lvdisplay | grep 'LV Name' | grep $VOLUMENAME || LVcreate
fi
if [[ "$STEP" = "lvcreate" ]]; then
lvdisplay | grep 'LV Name' | grep $VOLUMENAME &&
printf "\n\n $yel Logical Volume already exist, please put another name: \n\n\n\n $end"
lvdisplay | grep 'LV Name' | grep $VOLUMENAME && LVcreate
fi
clear
###
#LOGICAL VOLUME PATH = /dev/VolumeGroup/Volumename
DEV="/dev/$VOLUMEGROUP/$VOLUMENAME"
# equivale a
#DEVICE MAPPER LV PATH = /dev/mapper/VolumeGroup-Volumename
DEV_MAP="/dev/mapper/$VOLUMEGROUP-$VOLUMENAME"
#PHYSICAL DEVICE PATH = /dev/mapper/VolumeGroup-Volumename
# PV_DEV=$(/dev/sdb1)
# for CREATE SWAP option --createswap
if [[ "$TYPE" = "swap" ]]; then
echo ""
else
printf "\n $cyn Insert the size that you want to add to the Logical Volume $VOLUMENAME \n "
printf " $cyn Should be $FREEVG or less, use the same format, input the letter \n:
$blu m $end or $blu G :$end "
read NEWSIZE
echo " newsize $NEWSIZE - freevg $FREEVG "
# m as egabytes
# G as Gigabytes
lvcreate -L "$NEWSIZE" --yes -n $VOLUMENAME $VOLUMEGROUP
# printf "\n Succesful create $VOLUMENAME in $VOLUMEGROUP with $NEWSIZE \n"
fi
}
cleandumps(){
## Restore lvm data
cat $DUMPPATH/FILESYSTEM_DUMP.dump | xfsrestore -J - $MOUNTPOINT >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "Dump restore succesful"
rm -rf $DUMPPATH/FILESYSTEM_DUMP.dump
else
echo "Dump generation incorrect"
fi
}
mountpoint(){
if [[ "$STEP" = "lvreduce" ]]; then
are_you_sure
mount $DEV $MOUNTPOINT >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "$grn Failed to mount $VOLUMENAME in $MOUNTPOINT $end"
exit 1
else
"\n $grn Logical Filesystem $VOLUMENAME has been succesfully re-mounted in $MOUNTPOINT \n $end "
fi
fi
# for ext4 LVcreate option
if [[ "$TYPE" = "xfs" ]]; then
printf "\n We are about to MOUNT this $VOLUMENAME, "
are_you_sure
printf "\n Insert a new or existing Mount Point \n "
printf "should be an empty folder that starts and ends with / : "
read MOUNTPOINT
ls $MOUNTPOINT || mkdir -p $MOUNTPOINT ## only create the folder if it does not exist.
printf "\n The Volume $DEV will be formatted as XFS and all the data inside will be lost. \n "
are_you_sure
mkfs.xfs $DEV &&
mount $DEV $MOUNTPOINT >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Failed to mount $VOLUMENAME in $MOUNTPOINT"
exit 1
fi
printf "\n Logical Filesystem $VOLUMENAME has been Mounted in $MOUNTPOINT \n "
fi
# for xfs LVcreate option
if [[ "$TYPE" = "ext4" ]]; then
printf "\n We are about to MOUNT this $VOLUMENAME, "
are_you_sure
printf "\n Insert a new or existing Mount Point \n "
printf "should be an empty folder that starts and ends with / : "
read MOUNTPOINT
ls $MOUNTPOINT || mkdir -p $MOUNTPOINT ## only create the folder if it does not exist.
printf "\n The Volume $DEV will be formatted as EXT4 and all the data inside will be lost!!. \n "
are_you_sure
mkfs.ext4 $DEV &&
mount $DEV $MOUNTPOINT >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Failed to mount $VOLUMENAME in $MOUNTPOINT"
exit 1
fi
printf "\n Logical Filesystem $VOLUMENAME has been Mounted in $MOUNTPOINT \n "
fi
# for LVcreate and SWAP option
if [[ "$TYPE" = "swap" ]]; then
printf "\n The Volume $DEV will be formatted as SWAP and all the data inside will be lost. \n "
are_you_sure
mkswap $DEV &&
# echo "$DEV swap swap defaults 0 0 #added by script" >> /etc/fstab
printf "\n Old swap size \n"
cat /proc/swaps
echo ""
#Enable the extended logical volume:
swapon $DEV_MAP
#Test that the logical volume has been extended properly:
printf "\n\n\n NEW swap size \n"
cat /proc/swaps
fi
}
are_you_sure()
{
y=0
read -p "Continue? [y/n/] (y) " y
# echo "${y,,}" #converto string to lowercase
if [[ "${y,,}" != "y" ]]; then
echo ""
exit 1
fi
}
############################################ MENU
menu(){
case "$OPTION" in
--lvcreate)
STEP=lvcreate
clear
check_vg_free_space
LVcreate
mountpoint
;;
--swapfile)
# --swapfile \e[96mDEVICE \e[97mSWAPFILEPATH \e[95mSIZE\033[0m \033[0m
TYPE="swap"
clear
check_vg_free_space
LVcreate
mountpoint
lsblk | grep $VOLUMENAME
;;
--vgextend)
clear
echo -en "\e[1m resize provided VolumeGroup with the selected space \033[0m\n"
check_vg_free_space
lsblk | grep part | grep -v sda
printf "\n Please select an empty lvm device to join it into the Volume Group $VOLUMEGROUP : "
read DEVICE
printf "\n Old "
vgdisplay | grep "VG Size"
echo ""
vgextend $VOLUMEGROUP /dev/$DEVICE
printf "\n NEW "
vgdisplay | grep "VG Size"
;;
--lvextend)
clear
STEP=lvextend
check_vg_free_space
LVcreate
# lvextend -l +$FREEVG $VOLUMENAME
clear
printf "\n --> Old $VOLUMENAME size"
lvdisplay $DEV | grep "LV Size"
printf "\n"
lvextend -L+"$NEWSIZE" $DEV
printf "\n"
resize2fs $DEV >/dev/null 2>&1
xfs_growfs $DEV >/dev/null 2>&1
printf "\n --> NEW $VOLUMENAME size"
lvdisplay $DEV | grep "LV Size"
;;
--lvreduceTEST) #COMMENTED FOR TEST,
clear
STEP="lvreduce"
printf " \n\n\n $red Shrinking a logical volume is likely to destroy any filesystem
located on that volume if you do not make appropriate preparations.
If you want to preserve existing files then you will need to:
\n 1. Reduce the size of the filesystem by the required amount, then
\n 2. Reduce the size of the underlying block device (the logical volume)
to match that of the filesystem.
This script will support you to reduce a Logical Volume safetly. $end \n\n "
are_you_sure
clear
check_vg_free_space
LVreduce
if [[ "$TYPE" = "swap" ]]; then
printf "There is no data in a SWAP partition"
swapoff $DEV
lvreduce --size $NEWSIZE $DEV
mkswap $DEV
swapon $DEV
else
generate_dump
resize_force_umount
LVreduce2
fi
;;
--cleandumps)
check_vg_free_space
printf " $yel We are about to delete the dump file in "$DUMPPATH"/"$VOLUMENAME"_FILESYSTEM_DUMP.dump "
are_you_sure
resize_destroy_lvm
;;
--help)
echo -en "\e[1mSelected option $OPTION is not valid \033[0m\n"
menu_help
;;
*)
menu_help
esac
}
## Help Menu
menu_help(){
clear
echo -en "\e[1mValid options are: \033[0m
\e[94m --vgextend :\033[0m Resize Volume Group to a bigger size.
\e[94m --lvcreate :\033[0m Create an LVM device that belongs to the provided volumegroup to an specific size.
\e[94m --lvextend :\033[0m Resize the provided LUN to a bigger size.
\e[94m --swapfile :\033[0m Create a swapfile from an existing Logical Volume
\e[94m --cleandumps :\033[0m Clean all dumps. Used in case of not enought space available.
\e[1m Reminder :\033[0m
\e[92mVOLUME GROUP NAME\033[0m Name of the volumegroup associate. For Example VolGroup001, vgroot.
\e[93mLVM NAME\033[0m Logical Volume Group name. For example root, var_log, LogicalVolume01.
\e[94mMOUNT POINT\033[0m Mountpoint for the existing and or future mountpoint.
\e[95mSIZE\033[0m Size for the new or existing filesystem. Value on Gigabytes
\e[96mDEVICE\033[0m Physical device to be added. Examples should be /dev/xvdf /dev/xvdf1.
\e[97mSWAP FILE PATH\033[0m Swap File path.
"
exit 2
}
#########BEGIN
menu