-
Notifications
You must be signed in to change notification settings - Fork 2
/
lvm_create
executable file
·164 lines (152 loc) · 3.24 KB
/
lvm_create
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
#!/bin/bash
#
# Copyright (C) 2022 David Valin [email protected]
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
lv_disks=""
lv_total_size=0
lv_disk_count=0;
devices_to_use=""
devices_list=""
lvm_vol=""
lvm_grp=""
wipefs=""
usage()
{
echo "Usage: $1"
echo " --devices_to_use: comma separated list of devices to use"
echo " --lvm_grp: name of the lvm group creating"
echo " --lvm_vol: name of the lvm volume creating"
echo " --wipefs: wipe the device before doing the lvm operations"
exit 0
}
#
# Verify the passed data is correct.
#
validate_data()
{
if [[ $lvm_vol = "" ]]; then
echo Need to designate an lvm_vol name.
exit -1
fi
if [[ $lvm_grp = "" ]]; then
echo Need to designate an lvm_grp name.
exit -1
fi
if [[ $devices_to_use = "" ]]; then
echo Need to designate an lvm_grp name.
exit -1
fi
for i in $devices_to_use;
do
if [[ ! -e $i ]]; then
echo Error, device $i not found.
exit -1
fi
done
}
#
# Define options
#
ARGUMENT_LIST=(
"devices"
"lvm_vol"
"lvm_grp"
)
NO_ARGUMENTS=(
"wipefs"
"usage"
)
# read arguments
opts=$(getopt \
--longoptions "$(printf "%s:," "${ARGUMENT_LIST[@]}")" \
--longoptions "$(printf "%s," "${NO_ARGUMENTS[@]}")" \
--name "$(basename "$0")" \
--options "h" \
-- "$@"
)
if [ $? -ne 0 ]; then
exit
fi
eval set --$opts
while [[ $# -gt 0 ]]; do
case "$1" in
--devices)
devices_list=${2}
shift 2
;;
--lvm_vol)
lvm_vol=${2}
shift 2
;;
--lvm_grp)
lvm_grp=${2}
shift 2
;;
--usage)
usage $0
;;
--wipefs)
wipefs=1
shift 1
;;
-h)
usage $0
;;
--)
break;
;;
*)
echo option not found $1
usage $0
;;
esac
done
devices_to_use=`echo $devices_list | sed "s/,/ /g"`
validate_data
seper=""
for i in $devices_to_use; do
lv_disks=${i}${seper}${lv_disks}
size=`fdisk -l $i | grep bytes | grep Disk | cut -d' ' -f 5`
gb=`echo "$size/(1024*1024*1024)" | bc`
let "lv_total_size=$lv_total_size+$gb"
let "lv_disk_count=$lv_disk_count+1"
seper=" "
if [ $wipefs -eq 1 ]; then
wipefs -a $i
fi
done
lvremove -f $lvm_vol/$lvm_grp
vgremove -f $lvm_vol
let "total_size=$lv_total_size-200"
pvcreate -ff $lv_disks
if [ $? -ne 0 ]; then
echo pvcreate -ff $lv_disks failed.
exit -1
fi
sleep 60
vgcreate $lvm_vol $lv_disks
if [ $? -ne 0 ]; then
echo vgcreate $lvm_vol $lv_disks failed
exit -1
fi
sleep 60
echo y | lvcreate -Zy -Wy --yes -i $lv_disk_count -L ${total_size}G -n $lvm_vol $lvm_grp
if [ $? -ne 0 ]; then
echo lvcreate -Zy -Wy --yes -i $lv_disk_count -L ${total_size}G -n $lvm_vol $lvm_grp failed.
exit -1
fi
wipefs -f /dev/$lvm_vol/$lvm_grp