xref: /spdk/scripts/ceph/start.sh (revision 12fbe739a31b09aff0d05f354d4f3bbef99afc55)
1#!/usr/bin/env bash
2#  SPDX-License-Identifier: BSD-3-Clause
3#  Copyright (C) 2017 Intel Corporation
4#  All rights reserved.
5#
6# create mon
7
8set -x
9set -e
10
11script_dir=$(readlink -f $(dirname $0))
12
13base_dir=/var/tmp/ceph
14mon_ip=$1
15mon_dir=${base_dir}/mon.a
16pid_dir=${base_dir}/pid
17ceph_conf=${base_dir}/ceph.conf
18mnt_dir=${base_dir}/mnt
19image=/var/tmp/ceph_raw.img
20dev=/dev/loop200
21
22modprobe loop
23umount ${dev}p2 || true
24losetup -d $dev || true
25
26# partition osd
27if [ -d $base_dir ]; then
28	rm -rf $base_dir
29fi
30mkdir ${base_dir}
31cp ${script_dir}/ceph.conf $ceph_conf
32
33if [ ! -e $image ]; then
34	fallocate -l 4G $image
35fi
36
37mknod ${dev} b 7 200 || true
38losetup ${dev} ${image} || true
39
40PARTED="parted -s"
41SGDISK="sgdisk"
42
43echo "Partitioning ${dev}"
44${PARTED} ${dev} mktable gpt
45sleep 2
46
47${PARTED} ${dev} mkpart primary 0% 2GiB
48${PARTED} ${dev} mkpart primary 2GiB 100%
49
50partno=0
51echo "Setting name on ${dev}"
52${SGDISK} -c 1:osd-device-${partno}-journal ${dev}
53${SGDISK} -c 2:osd-device-${partno}-data ${dev}
54kpartx ${dev}
55
56# later versions of ceph-12 have a lot of changes, to compatible with the new
57# version of ceph-deploy.
58ceph_version=$(ceph -v | awk '{print $3}')
59ceph_maj=${ceph_version%%.*}
60if [ $ceph_maj -gt 12 ]; then
61	update_config=true
62	rm -f /var/log/ceph/ceph-mon.a.log || true
63	set_min_mon_release="--set-min-mon-release 14"
64	ceph_osd_extra_config="--check-needs-journal --no-mon-config"
65else
66	update_config=false
67	set_min_mon_release=""
68	ceph_osd_extra_config=""
69fi
70
71# prep osds
72
73mnt_pt=${mnt_dir}/osd-device-0-data
74mkdir -p ${mnt_pt}
75mkfs.xfs -f /dev/disk/by-partlabel/osd-device-0-data
76mount /dev/disk/by-partlabel/osd-device-0-data ${mnt_pt}
77cat << EOL >> $ceph_conf
78osd data = ${mnt_pt}
79osd journal = /dev/disk/by-partlabel/osd-device-0-journal
80
81# add mon address
82[mon.a]
83mon addr = ${mon_ip}:12046
84EOL
85
86# create mon
87rm -rf "${mon_dir:?}/"*
88mkdir -p ${mon_dir}
89mkdir -p ${pid_dir}
90rm -f /etc/ceph/ceph.client.admin.keyring
91
92ceph-authtool --create-keyring --gen-key --name=mon. ${base_dir}/keyring --cap mon 'allow *'
93ceph-authtool --gen-key --name=client.admin --cap mon 'allow *' --cap osd 'allow *' --cap mds 'allow *' --cap mgr 'allow *' ${base_dir}/keyring
94
95monmaptool --create --clobber --add a ${mon_ip}:12046 --print ${base_dir}/monmap $set_min_mon_release
96
97sh -c "ulimit -c unlimited && exec ceph-mon --mkfs -c ${ceph_conf} -i a --monmap=${base_dir}/monmap --keyring=${base_dir}/keyring --mon-data=${mon_dir}"
98
99if [ $update_config = true ]; then
100	sed -i 's/mon addr = /mon addr = v2:/g' $ceph_conf
101fi
102
103cp ${base_dir}/keyring ${mon_dir}/keyring
104
105cp $ceph_conf /etc/ceph/ceph.conf
106
107cp ${base_dir}/keyring /etc/ceph/keyring
108cp ${base_dir}/keyring /etc/ceph/ceph.client.admin.keyring
109chmod a+r /etc/ceph/ceph.client.admin.keyring
110
111ceph-run sh -c "ulimit -n 16384 && ulimit -c unlimited && exec ceph-mon -c ${ceph_conf} -i a --keyring=${base_dir}/keyring --pid-file=${base_dir}/pid/root@$(hostname).pid --mon-data=${mon_dir}" || true
112
113# after ceph-mon creation, ceph -s should work.
114if [ $update_config = true ]; then
115	# start to get whole log.
116	ceph-conf --name mon.a --show-config-value log_file
117
118	# add fsid to ceph config file.
119	fsid=$(ceph -s | grep id | awk '{print $2}')
120	sed -i 's/perf = true/perf = true\n\tfsid = '$fsid' \n/g' $ceph_conf
121
122	# unify the filesystem with the old versions.
123	sed -i 's/perf = true/perf = true\n\tosd objectstore = filestore\n/g' $ceph_conf
124	cat ${ceph_conf}
125fi
126
127# create osd
128
129i=0
130
131mkdir -p ${mnt_dir}
132
133uuid=$(uuidgen)
134ceph -c ${ceph_conf} osd create ${uuid} $i
135ceph-osd -c ${ceph_conf} -i $i --mkfs --mkkey --osd-uuid ${uuid} ${ceph_osd_extra_config}
136ceph -c ${ceph_conf} osd crush add osd.${i} 1.0 host=$(hostname) root=default
137ceph -c ${ceph_conf} -i ${mnt_dir}/osd-device-${i}-data/keyring auth add osd.${i} osd "allow *" mon "allow profile osd" mgr "allow *"
138
139class_dir=/$(ceph -c "$ceph_conf" config get osd osd_class_dir)
140[[ -e $class_dir ]]
141ceph -c "$ceph_conf" config set osd osd_class_dir "$class_dir"
142
143# start osd
144pkill -9 ceph-osd || true
145sleep 2
146
147mkdir -p ${pid_dir}
148env -i TCMALLOC_MAX_TOTAL_THREAD_CACHE_BYTES=134217728 ceph-osd -c ${ceph_conf} -i 0 --pid-file=${pid_dir}/ceph-osd.0.pid
149