-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvoid-remove-vg
executable file
·300 lines (273 loc) · 8.79 KB
/
void-remove-vg
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
#!/usr/bin/env bash
#shellcheck disable=SC2034,SC2155,SC2154,SC2181
# void-install
# Created: 2023/04/25
# Altered: 2024/03/08
#
# Copyright (c) 2022-2024, Vilmar Catafesta <vcatafesta@gmail.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
############################################################################
export TEXTDOMAINDIR=/usr/share/locale
export TEXTDOMAIN=void-remove-vg
#debug
export PS4='${red}${0##*/}${green}[$FUNCNAME]${pink}[$LINENO]${reset} '
#set -x
#set -e
shopt -s extglob
#system
readonly APP="${0##*/}"
readonly _VERSION_='1.0.01-20240308'
sh_diahora() {
DIAHORA=$(date +"%d%m%Y-%T" | sed 's/://g')
printf "%s\n" "$DIAHORA"
}
sh_setenvironment() {
declare -gA Alocale=([0]=pt_BR [1]=en_US [2]=de_DE [3]=fr_FR [4]=es_ES [5]=it_IT)
tput sgr0 # reset colors
bold=$(tput bold)
reset=$(tput sgr0)
white="${bold}$(tput setaf 7)"
black="${bold}$(tput setaf 0)"
red=$(tput bold)$(tput setaf 196)
green=$(tput setaf 2)
yellow=$(tput bold)$(tput setaf 3)
blue=$(tput setaf 4)
pink=$(tput setaf 5)
cyan=$(tput setaf 6)
orange=$(tput setaf 3)
purple=$(tput setaf 125)
violet=$(tput setaf 61)
force=0
readonly BOOTLOG="/tmp/$APP-$(sh_diahora).log"
COL_NC='\e[0m' # No Color
COL_LIGHT_GREEN='\e[1;32m'
COL_LIGHT_RED='\e[1;31m'
TICK="${white}[${COL_LIGHT_GREEN}✓${COL_NC}${white}]"
CROSS="${white}[${COL_LIGHT_RED}✗${COL_NC}${white}]"
INFO="[i]"
# shellcheck disable=SC2034
DONE="${COL_LIGHT_GREEN} done!${COL_NC}"
OVER="\\r\\033[K"
DOTPREFIX=" ${black}::${reset} "
readonly LOGGER='/dev/tty8'
}
sh_unsetvarcolors() {
unset bold reset cyan red blue white black
unset green yellow orange pink cyan purple violet
}
readconf() {
if [[ $LC_DEFAULT -eq 0 ]]; then
read -r -p "$* [S/n]"
else
read -r -p "$* [Y/n]"
fi
[[ ${REPLY^} == "" ]] && return 0
[[ ${REPLY^} == N ]] && return 1 || return 0
}
readnotconf() {
if [[ $LC_DEFAULT -eq 0 ]]; then
read -r -p "$* [s/N]"
else
read -r -p "$* [y/N]"
fi
[[ ${REPLY^} == "" ]] && return 1
[[ ${REPLY^} == N ]] && return 1 || return 0
}
die() {
local msg=$1
shift
printf " %b %s\\n" "${CROSS}" "${bold}${red}${msg}"
exit 1
}
sh_checkRoot() {
local str="$(gettext "Verificação de usuário root")"
if [[ "$(id -u)" != "0" ]]; then
die "${red}$(gettext "erro: você não pode executar esta operação a menos que seja root")"
fi
printf " %b %s\\n" "${TICK}" "${str}"
return 0
}
log_msg() {
local retval="${PIPESTATUS[0]}"
if [[ $retval -eq 0 ]]; then
printf " %b %s\\n" "${TICK}" "${*}"
else
printf " %b %s\\n" "${CROSS}" "${*}"
fi
}
sh_version() {
printf "%s\n" "${black}${0##*/} v${_VERSION_}${reset}"
printf "\t%s\n\t%s\n\t%s\n\t%s\n" "${bold}${black}Copyright (C) 2023 vcatafesta@gmail.com"\
"$(gettext "Licença GPLv3+: GNU GPL versão 3 ou posterior") <https://gnu.org/licenses/gpl.html>"\
"$(gettext "Este é um software livre: você é livre para alterá-lo e redistribuí-lo.")"\
"$(gettext "NÃO HÁ GARANTIA, em toda a extensão permitida por lei.")$reset"
exit 0
}
sh_usage() {
cat <<EOF
${black}${APP} v$_VERSION_ ${reset}
Usage: ${red}$0 -r|--remove <VG> [-v|--vgs] [-p|--pvs] [-l|--lvs] [-f|--force] [-V|--version] [-n|--nocolor] [-h|--help]
${cyan}[<operations>] ${reset}
${red}-v --vgs${cyan} $(gettext "#listar VGs (grupo de volumes)")
${red}-p --pvs${cyan} $(gettext "#listar PVs (Volumes físicos)")
${red}-l --lvs${cyan} $(gettext "#listar LVs (Volumes lógicos)")
${red}-r --remove${cyan} $(gettext "#remover VG (obrigatório)")
${red}-f --force${cyan} $(gettext "#forçar")
${red}-V --version${cyan} $(gettext "#imprimir versão do aplicativo")
${red}-n --nocolor${cyan} $(gettext "#remover cores da saída")
${red}-h --help${cyan} $(gettext "#esta ajuda")
EOF
exit 0
}
limpa_vm() {
echo -n "${black}"
log_msg "${green}$(gettext "iniciando limpeza")"
umount -Rl /mnt/voidlinux 2>&1 | tee -i -a "$BOOTLOG" >"$LOGGER"
log_msg "${black}$(gettext "Desmontando") FS : ${orange}/mnt/voidlinux"
umount -Rl /dev/"$_vg"/* 2>&1 | tee -i -a "$BOOTLOG" >"$LOGGER"
log_msg "${black}$(gettext "Desmontando") LVs : ${orange}/dev/$_vg/*"
lvremove -ff /dev/"$_vg"/* 2>&1 | tee -i -a "$BOOTLOG" >"$LOGGER"
log_msg "${black}$(gettext "Removendo") LVs : ${orange}/dev/$_vg/*"
vgremove -ff "$_vg" 2>&1 | tee -i -a "$BOOTLOG" >"$LOGGER"
log_msg "${black}$(gettext "Removendo") VG : ${orange}$_vg"
# log_msg "${black}$(gettext "Removendo") PVs (Volumes Fisícos)";
# pvremove -ff ${AConfFde[partition]} 2>&1 | tee -i -a "$BOOTLOG" > "$LOGGER"
log_msg "${green}$(gettext "Ignorando a remoção dos Volumes Físicos, talvez queira recuperar algo")"
log_msg "${green}$(gettext "Feito.")${white}"
vgs -v
echo
pvs -v
echo
lvs -v
: <<'REMARK'
log_msg "${black}Desmontando FS em : ${orange}/mnt/voidlinux";
umount -Rl /mnt/voidlinux >/dev/null 2>&-
log_msg "${black}Desativando partições lógicas";
lvchange -an /dev/$_vg/root >/dev/null 2>&-
lvchange -an /dev/$_vg/swap >/dev/null 2>&-
lvchange -an /dev/$_vg/home >/dev/null 2>&-
vgchange -an $_vg >/dev/null 2>&-
#dmsetup info >/dev/null 2>&-
log_msg "${black}Removendo Volume Group : ${orange}$_vg";
vgremove -ff $_vg >/dev/null 2>&-
log_msg "${black}Removendo Volume lógico : ${orange}/dev/$_vg/root";
lvremove /dev/$_vg/root >/dev/null 2>&-
log_msg "${black}Removendo Volume lógico : ${orange}/dev/$_vg/swap";
lvremove /dev/$_vg/swap >/dev/null 2>&-
log_msg "${black}Removendo Volume lógico : ${orange}/dev/$_vg/home";
lvremove /dev/$_vg/home >/dev/null 2>&-
log_msg "${black}Removendo Volume lógico : ${orange}/dev/$_vg/var";
lvremove /dev/$_vg/var >/dev/null 2>&-
log_msg "${black}Removendo Label Volume físico: ${orange}/dev/mapper/$_vg";
pvremove -ff /dev/mapper/$_vg >/dev/null 2>&-
dmsetup remove $_vg >/dev/null 2>&-
# dmsetup info >/dev/null 2>&-
log_msg "${green}Feito.${white}"
vgs -v
echo
pvs -v
echo
lvs -v
REMARK
}
sh_setenvironment
OPTIONS=r:lvpfVnh
LONGOPTIONS=remove:,vgs,pvs,lvs,force,version,nocolor,help
# Processa as opções
#opts=$(getopt --options=$OPTIONS --longoptions=$LONGOPTIONS --name "$0" -- "$@")
#if [ $? -ne 0 ]; then
# sh_usage
#fi
#eval set -- "$opts"
if ! mapfile -t opts < <(getopt --options=$OPTIONS --longoptions=$LONGOPTIONS --name "$0" -- "$@"); then
sh_usage
fi
eval set -- "${opts[*]}"
while true; do
case "$1" in
-r | --remove)
log_msg "$(gettext "Modo remoção ...")"
_vg="$2"
shift 2
;;
-v | --vgs)
sh_checkRoot
log_msg "$(gettext "Listando VGs ...")"
echo -n "${cyan}"
vgs --noheadings
exit $(($# ? 0 : 1))
;;
-p | --pvs)
sh_checkRoot
log_msg "$(gettext "Listando PVs ...")"
echo -n "${cyan}"
pvs --noheadings
exit $(($# ? 0 : 1))
;;
-l | --lvs)
sh_checkRoot
log_msg "$(gettext "Listando LVs ...")"
echo -n "${cyan}"
lvs --noheadings
exit $(($# ? 0 : 1))
;;
-f | --force)
force=1
log_msg "$(gettext "Ligando force mode")"
shift
;;
-n | --nocolor)
log_msg "$(gettext "Desligando saida em cores")"
sh_unsetvarcolors
shift
;;
-V | --version) sh_version ;;
-h | --help) sh_usage ;;
--)
shift
break
;;
*) sh_usage ;;
esac
done
if [ -z "$_vg" ]; then
sh_usage
fi
sh_checkRoot
VGs="$(vgs --noheadings)"
echo "VGs available : "
echo -n "${cyan}"
vgs --noheadings
echo "${reset}VG to remove : "
echo " ${yellow}$_vg"
echo
if grep -o -q "$_vg " <<<"$VGs"; then
if ! ((force)); then
if ! readnotconf "${red}CAREFUL! ${reset}This procedure is ${red}irreversible! ${reset}continue with the removal of ?"; then
die "Cancelled"
fi
fi
limpa_vm "$@"
else
die "$(gettext "Cancelado!") => '${yellow}$_vg${red}' $(gettext "não encontrado!")"
fi