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