-
Notifications
You must be signed in to change notification settings - Fork 7
/
Umount.sh
executable file
·141 lines (123 loc) · 2.99 KB
/
Umount.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
#!/bin/bash
# set -euo pipefail
#
# Script to clean up all devices mounted under $CHROOT
#
#################################################################
PROGNAME=$(basename "$0")
PROGDIR="$( dirname "${0}" )"
CHROOT="${CHROOT:-/mnt/ec2-root}"
DEBUG="${DEBUG:-UNDEF}"
TARGDISK="${TARGDISK:-UNDEF}"
# Import shared error-exit function
source "${PROGDIR}/err_exit.bashlib"
# Ensure appropriate SEL mode is set
source "${PROGDIR}/no_sel.bashlib"
# Print out a basic usage message
function UsageMsg {
local SCRIPTEXIT
SCRIPTEXIT="${1:-1}"
(
echo "Usage: ${0} [GNU long option] [option] ..."
echo " Options:"
printf '\t%-4s%s\n' '-c' 'Where chroot-dev is set up (default: "/mnt/ec2-root")'
printf '\t%-4s%s\n' '-C' 'Device to clean'
printf '\t%-4s%s\n' '-h' 'Print this message'
echo " GNU long options:"
printf '\t%-20s%s\n' '--chroot' 'See "-c" short-option'
printf '\t%-20s%s\n' '--clean' 'See "-C" short-option'
printf '\t%-20s%s\n' '--help' 'See "-h" short-option'
)
exit "${SCRIPTEXIT}"
}
# Do dismount
function UnmountThem {
local BLK
while read -r BLK
do
err_exit "Unmounting ${BLK}" NONE
umount "${BLK}" || \
err_exit "Failed unmounting ${BLK}"
done < <( cut -d " " -f 3 <( mount ) | grep "${CHROOT}" | sort -r )
}
# Clean things up
function DiskCleanup {
local TARGVG
# Look for LVM2 volume-groups on $TARGDISK
TARGVG="$( pvs "${TARGDISK}"2 --no-heading -o vg_name | sed 's/[ ]*//g' )"
# Remove LVM2 volume-groups as needed
if [[ ${TARGVG:-} == "" ]]
then
err_exit "Found no LVM volume-groups to clean" NONE
else
err_exit "Nuking ${TARGVG}" NONE
vgremove -f "${TARGVG}" || \
err_exit "Failed nuking ${TARGVG}"
fi
# Null-out disk vtoc
err_exit "Clearing label from ${TARGDISK}" NONE
dd if=/dev/urandom of="${TARGDISK}" bs=1024 count=10240 2> /dev/null || \
err_exit "Failed clearing label from ${TARGDISK}"
}
######################
## Main program-flow
######################
OPTIONBUFR=$( getopt \
-o C:c:h\
--long chroot:,clean:,help\
-n "${PROGNAME}" -- "$@" )
eval set -- "${OPTIONBUFR}"
###################################
# Parse contents of ${OPTIONBUFR}
###################################
while true
do
case "$1" in
-c|--chroot)
case "$2" in
"")
err_exit "Error: option required but not specified"
shift 2;
exit 1
;;
*)
CHROOT="${2}"
shift 2;
;;
esac
;;
-C|--clean)
case "$2" in
"")
err_exit "Error: option required but not specified"
shift 2;
exit 1
;;
*)
TARGDISK="${2}"
shift 2;
;;
esac
;;
-h|--help)
UsageMsg 0
;;
--)
shift
break
;;
*)
err_exit "Internal error!"
exit 1
;;
esac
done
# Dismount chroot
UnmountThem
# Clean chroot-dev if requested
if [[ ${TARGDISK} == "UNDEF" ]]
then
err_exit "Cleanup option not selected: Done" NONE
else
DiskCleanup
fi