#!/bin/bash

# Copyright 2014 Intel Corporation, All Rights Reserved.

# Licensed under the Apache License, Version 2.0 (the"License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#  http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

ceph-osd -i $1 --mkfs --mkkey
cp -rf /var/lib/ceph/osd/osd$1/keyring /etc/ceph/keyring.osd.$1

id ceph >/dev/null 2>&1 && ceph_user=1
if test "$ceph_user"; then
    chown -R ceph:ceph /var/lib/ceph
    chown -R ceph:ceph /etc/ceph
fi

ceph auth add osd.$1 osd 'allow *' mon 'allow profile osd' -i /etc/ceph/keyring.osd.$1
ceph osd crush add osd.$1 1.0 `hostname`

systemctl --version >/dev/null 2>&1 && systemctl=1
if test "$systemctl"; then
    if [[ -f /usr/lib/systemd/system/ceph.target ]]; then
        systemctl start ceph-osd@$1
    else
        service ceph start osd.$1
    fi
else
    service ceph start osd.$1
fi