-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_osd.bash
executable file
·107 lines (94 loc) · 3.3 KB
/
create_osd.bash
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
#!/usr/bin/env bash
# Creates an OSD and adds it to given Ceph deployment using ceph-deploy.
#
# Specially usefull when need to deploy an OSD over a LUKS device, given that
# ceph-volume deprecated the use of --dmcrypt parameter, thus ceph-deploy
# fails wonderfully when trying to use it, and the same happens when a mapper
# device is given. This script handles any block device by manually creating
# the LVM layer.
##############################################################################
#
# create_osd.bash Copyright © 2018 HacKan (https://hackan.net)
# 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 3 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, see <http://www.gnu.org/licenses/>.
#
##############################################################################
VERSION="0.4"
# /dev/mapper/cryptodisk
# /dev/sdX
BLOCK_DEVICE="$1"
VOLUME_GROUP="${2:-vg-$(cat /proc/sys/kernel/random/uuid)}"
LOGICAL_VOLUME="${3:-lv-$(cat /proc/sys/kernel/random/uuid)}"
bailout() {
error "$*"
exit 1
}
usage() {
echo "$0 <block device> [<volume group name> [<logical volume name>]]"
}
info() {
echo "[INFO ] $*"
}
error() {
echo "[ERROR ] $*"
}
warning() {
echo "[WARNING] $*"
}
info "CreateOSD v${VERSION} by HacKan | GNU GPL v3+"
[[ "$1" == "-h" || "$1" == "--help" ]] && usage && exit 0
[[ -z "$BLOCK_DEVICE" ]] && error "Block device is a mandatory parameter" && usage && exit 1
[[ ! -b "$BLOCK_DEVICE" ]] && bailout "The specified device is not a block device!"
[[ "$(whoami)" != "root" ]] && bailout "This script must be run as root"
info "Deploying at $(hostname)"
info "Checking for active VG/LV"
vg="$(pvs | grep "$BLOCK_DEVICE" | cut -f4 -d' ')"
if [[ -n "$vg" ]]; then
info "Removing VG/LV"
sudo vgremove -yv "$vg" || bailout
fi
info "Zapping $BLOCK_DEVICE"
echo
ceph-volume lvm zap "$BLOCK_DEVICE" || bailout
echo
info "Creating LVM physical volume"
echo
pvcreate "$BLOCK_DEVICE" || bailout
echo
info "Creating LVM volume group $VOLUME_GROUP"
echo
vgcreate "$VOLUME_GROUP" "$BLOCK_DEVICE"
echo
info "Creating LVM logical volume $LOGICAL_VOLUME"
echo
lvcreate -l "100%FREE" -n "$LOGICAL_VOLUME" "$VOLUME_GROUP" || bailout
echo
ecode=1
if type ceph-deploy >/dev/null 2>&1 && [[ -f "./ceph.conf" && -f "./ceph.bootstrap-osd.keyring" ]]; then
info "Running ceph-deploy to add device as OSD"
echo
ceph-deploy osd create --data "${VOLUME_GROUP}/${LOGICAL_VOLUME}" "$(hostname)"
ecode=$?
else
if type ceph-deploy >/dev/null 2>&1; then
warning "I couldn't find ceph config nor keyring, you should run this script from the required ceph-deploy data directory"
else
warning "I couldn't find ceph-deploy executable"
fi
info "All is not lost, just run (at the right directory):"
echo "ceph-deploy osd create --data ${VOLUME_GROUP}/${LOGICAL_VOLUME} $(hostname)"
ecode=2
fi
echo
info "Finished"
exit $ecode