#!/bin/bash



HOST=`hostname -s`
DEV=$1
if [ "$DEV" == "" ] ; then
	echo "DEV not set"
	exit 1
fi
#DEV=/dev/sdx

	echo "----------------------------------------------"
	echo "Removing current data!"
	echo "----------------------------------------------"

	parted ${DEV} rm 1
	parted ${DEV} rm 2


	echo "----------------------------------------------"
	echo "Creating new OSD!"
	echo "----------------------------------------------"

	OSDID=`ceph --cluster cephsml osd create`
	echo "New OSD.${OSDID} created"

	parted -s -a optimal $DEV mklabel gpt || failed "mklabel $DEV"
	echo "Creating osd device $OSDID data label"
	parted -s -a optimal $DEV mkpart osd-device-$OSDID-data 0G 32G || failed "mkpart $OSDID-data"
	parted -s -a optimal $DEV mkpart osd-device-$OSDID-block 32G 100% || failed "mkpart $OSDID-block"

	sleep 3
	
	mkdir -p /var/lib/ceph/osd/cephsml-${OSDID} ;

	mkfs.xfs -f /dev/disk/by-partlabel/osd-device-${OSDID}-data
	mount -o rw,noatime,attr2,inode64,noquota  /dev/disk/by-partlabel/osd-device-${OSDID}-data /var/lib/ceph/osd/cephsml-${OSDID} ;

	echo "Init OSD.${OSDID}	"
	ceph-osd --cluster cephsml -i ${OSDID} --mkfs --mkkey ;
	chown -R ceph:ceph /var/lib/ceph/osd/cephsml-${OSDID} ;

	echo "Activating OSD.${OSDID}..."
	ceph --cluster cephsml auth add osd.${OSDID} osd 'allow *' mon 'allow rwx' -i /var/lib/ceph/osd/cephsml-${OSDID}/keyring
	echo "ok" > /var/lib/ceph/osd/cephsml-${OSDID}/active
	echo "Done !"
	
	ceph --cluster cephsml osd crush add ${OSDID} 1.818 host=${HOST}

	rm -f /var/log/ceph/cephsml-osd.${OSDID}.lo*

	sgdisk -t 1:4fbd7e29-9d25-41b8-afd0-062c0ceff05d -t 2:cafecafe-9b03-4f30-b4c6-b4b80ceff106 ${DEV}

	chown ceph:ceph ${DEV}1
	chown ceph:ceph ${DEV}2
	
	echo "ok" > /var/lib/ceph/osd/cephsml-${OSDID}/active

	systemctl start ceph-osd@${OSDID}
	sleep 1
#	tail -f /var/log/ceph/cephsml-osd.${OSDID}.log
echo ""

