-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathlibtest.sh
More file actions
executable file
·171 lines (145 loc) · 3.7 KB
/
libtest.sh
File metadata and controls
executable file
·171 lines (145 loc) · 3.7 KB
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
#!/bin/bash
create_partition_sfdisk(){
local dev="$1" size
# Use a single partition of a whole device
# TODO:
# * Consider gpt, or unpartitioned volumes
# * Error handling when partition(s) already exist
# * Deal with loop/nbd device names. See growpart code
size=$(( $( awk "\$4 ~ /"$( basename $dev )"/ { print \$3 }" /proc/partitions ) * 2 - 2048 ))
cat <<EOF | sfdisk $dev
unit: sectors
start= 2048, size= ${size}, Id=8e
EOF
}
create_partition_parted(){
local dev="$1"
parted $dev --script mklabel msdos mkpart primary 0% 100% set 1 lvm on
}
# Partition a block device.
create_partition() {
local dev="$1" part
if [ -x "/usr/sbin/parted" ]; then
create_partition_parted $dev
else
create_partition_sfdisk $dev
fi
# Sometimes on slow storage it takes a while for partition device to
# become available. Wait for device node to show up.
if ! udevadm settle;then
return 1
fi
return 0
}
# Tests if the volume group vg_name exists
vg_exists() {
local vg vg_name="$1"
for vg in $(vgs --noheadings -o vg_name); do
if [ "$vg" == "$vg_name" ]; then
return 0
fi
done
return 1
}
# Tests if the physical volume exists
pv_exists() {
pvs $1 >/dev/null 2>&1
}
# Tests if the logical volume lv_name exists
lv_exists() {
local vg_name=$1
local lv_name=$2
lvs $vg_name/$lv_name > /dev/null 2>&1 && return 0
return 1
}
# Tests if the logical volume lv_name is active
lv_is_active() {
local vg_name=$1
local lv_name=$2
local lv_attr is_active
lv_attr=$(lvs -o lv_attr --no-headings $vg_name/$lv_name)
lv_attr=${lv_attr# }
is_active=${lv_attr:4:1}
[ "$is_active" == "a" ] && return 0
return 1
}
remove_pvs() {
local dev devs=$1 pv
for dev in $devs; do
pv=$(lsblk -npl -o NAME "$dev" | tail -n +2 | head -1)
# If lsblk output physical volume (pv) name, pv exists on partition.
if [ -n "$pv" ]; then
pvremove -y ${pv} >> $LOGS 2>&1
# If lsblk output nothing, there might be a pv on block device.
# pv name would be same as block device name in this case.
elif pv_exists "$dev"; then
pvremove -y ${dev} >> $LOGS 2>&1
fi
done
}
parted_del_partition() {
local dev=$1
parted ${dev} rm 1 >> $LOGS 2>&1
}
sfdisk_del_partition() {
local dev=$1
sfdisk --delete ${dev} 1 >> $LOGS 2>&1
}
remove_partitions() {
local dev devs=$1
local use_parted=false
if [ -x "/usr/sbin/parted" ]; then
use_parted=true
fi
for dev in $devs; do
if [ "$use_parted" == "true" ]; then
parted_del_partition "$dev"
else
sfdisk_del_partition "$dev"
fi
done
}
# Wipe all signatures on devices
wipe_signatures() {
local dev devs=$1
for dev in $devs; do
wipefs -f -a $dev >> $LOGS 2>&1
done
}
cleanup() {
local vg_name=$1
local devs=$2
local infile=/etc/sysconfig/docker-storage-setup
local outfile=/etc/sysconfig/docker-storage
if [ $# -eq 4 ]; then
infile=$3
outfile=$4
fi
if vg_exists "$vg_name"; then
vgremove -y $vg_name >> $LOGS 2>&1
fi
remove_pvs "$devs"
remove_partitions "$devs"
# After removing partitions let udev settle down. In some
# cases it has been observed that udev rule kept the device
# busy.
udevadm settle
rm -f $infile $outfile
wipe_signatures "$devs"
[ -d "$CSS_METADATA_DIR" ] && rm -rf "$CSS_METADATA_DIR"
}
cleanup_mount_file() {
local mount_path=$1
local mount_filename=$(echo $mount_path|sed 's/\//-/g'|cut -c 2-)
if [ -f "/etc/systemd/system/$mount_filename.mount" ];then
systemctl disable $mount_filename.mount >/dev/null 2>&1
rm /etc/systemd/system/$mount_filename.mount >/dev/null 2>&1
systemctl daemon-reload
fi
}
cleanup_soft_links() {
local dev devs=$1
for dev in $devs; do
rm $dev
done
}