-
Notifications
You must be signed in to change notification settings - Fork 4
/
docker_thinpool_setup.sh
executable file
·187 lines (159 loc) · 4.96 KB
/
docker_thinpool_setup.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
#!/bin/bash
set -e
# check to see if you're running this as root
if [ "${UID}" -ne 0 ]
then
echo "ERROR: you must run this script as root"
exit 1
fi
# get block device name
BLOCK_DEVICE="${1:-}"
# set block device to second argument when running headless
if [ "${1}" = "--headless" ]
then
BLOCK_DEVICE="${2:-}"
fi
# verify a block device was provided
if [ -z "${BLOCK_DEVICE}" ]
then
echo "ERROR: no block device specified"
echo "Usage: ${0} /path/to/block/device"
echo "Example: ${0} /dev/sdb"
echo "Headless example: ${0} --headless /dev/sdb"
exit 1
fi
# check to see if the block device is mounted
if mount | grep "${BLOCK_DEVICE}" > /dev/null 2>&1
then
echo "Error: the specified block device ${BLOCK_DEVICE} is currently mounted! This should be an unused, unmounted device."
exit 1
fi
# check to see if the specified device is a block device
if [ ! -b "${BLOCK_DEVICE}" ]
then
echo "Error: ${BLOCK_DEVICE} is not a block device"
exit 1
fi
if [ "${1}" != "--headless" ]
then
# give the user one last chance to quit
echo "Running this script will remove the following:"
echo " * All data in /var/lib/docker"
echo " * All data on ${BLOCK_DEVICE}"
echo ""
read -rp "Do you with to continue (y/n)? " choice
case "$choice" in
y|Y|[yY][eE][sS])
;;
*)
echo "Aborting"
exit 1
;;
esac
fi
# check to see if we need to install lvm2
if ! pvdisplay > /dev/null 2>&1
then
echo -e "\nInstalling lvm2"
yum install -y lvm2
fi
# source the deferred deletion check support functions
# shellcheck disable=SC1091 source=/dev/null
. "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"/deferred_deletion_check/check_for_deferred_deletion.sh
# run and check result of deferred deletion functions
if platform_supports_deferred_deletion
then
echo -e "\nDeferred deletion is supported"
DEFERRED_DELETION="true"
else
echo -e "\nDeferred deletion is not supported"
DEFERRED_DELETION="false"
fi
# stop docker
echo -e "\nEnsuring docker is stopped"
systemctl stop docker
# check to see if /var/lib/docker exists
if [ -d "/var/lib/docker" ]
then
# remove everything in /var/lib/docker
echo -e "\nRemoving all files in /var/lib/docker"
rm -rf /var/lib/docker/*
fi
# check to see if there is a physical device setup
if pvs "${BLOCK_DEVICE}" > /dev/null 2>&1
then
echo -e "\nRemoving exiting LVM setup on ${BLOCK_DEVICE}"
# get a list of volume groups
VGS="$(pvdisplay -c "${BLOCK_DEVICE}"* 2> /dev/null | awk -F ':' '{print $2}')"
for VG in ${VGS}
do
# get a list of logical volumes
LVS="$(lvdisplay -c "${VG}" | awk -F ':' '{print $1}')"
for LV in ${LVS}
do
# remove logical volume
lvremove -f "${LV}"
done
# remove volume group
vgremove -f "${VG}"
done
# check to see if there are multiple physical volumes (shouldn't be because this script isn't configured to properly handle this!)
for PV in $(pvs --noheadings | awk '{print $1}')
do
# remove physical volume
pvremove -f "${PV}"
done
fi
# create a new physical volume
echo -e "\nCreating physical volume"
pvcreate -y "${BLOCK_DEVICE}" || (lsblk "${BLOCK_DEVICE}" || true; echo -e "\nERROR: Unable to create physical device; is the device already in use?"; exit 1)
# create a new volume group
echo -e "\nCreating volume group"
vgcreate docker "${BLOCK_DEVICE}"
# create a new thin pool for docker to use
echo -e "\nCreating thin pool"
lvcreate --wipesignatures y -n thinpool docker -l 95%VG
lvcreate --wipesignatures y -n thinpoolmeta docker -l 1%VG
lvconvert -y --zero n -c 512K --thinpool docker/thinpool --poolmetadata docker/thinpoolmeta
# create an autoextend profile
echo -e "\nCreating auto extension profile"
cat << EOF > /etc/lvm/profile/docker-thinpool.profile
activation {
thin_pool_autoextend_threshold=80
thin_pool_autoextend_percent=20
}
EOF
# enable monitoring to ensure autoextend executes
echo -e "\nEnabling monitoring of the thin pool for auto extension"
lvchange --metadataprofile docker-thinpool docker/thinpool
# show user the monitoring is enabled
echo -e "\nVerifying monitoring was successfully enabled"
lvs -o+seg_monitor
# check to see if /etc/docker exists
if [ ! -d "/etc/docker" ]
then
echo -e "\nCreating directory '/etc/docker'"
mkdir /etc/docker
fi
# check to see if a daemon.json exists
if [ -f "/etc/docker/daemon.json" ]
then
# create backup of daemon.json
DATETIME="$(date +'%s')"
echo -e "\nExisting /etc/docker/daemon.json found; moving to daemon.json.${DATETIME}.bak"
mv /etc/docker/daemon.json /etc/docker/daemon.json."${DATETIME}".bak
fi
# write devicemapper config to daemon.json
echo -e "\nWriting devicemapper configuration to /etc/docker/daemon.json"
cat << EOF > /etc/docker/daemon.json
{
"storage-driver": "devicemapper",
"storage-opts": [
"dm.thinpooldev=/dev/mapper/docker-thinpool",
"dm.use_deferred_removal=true",
"dm.use_deferred_deletion=${DEFERRED_DELETION}"
]
}
EOF
# finished!
echo -e "\nThin pool setup complete. Start the docker daemon to begin using the thin pool"