From: Owen Synge <osynge@suse.com>
Date: Tue, 12 Aug 2014 10:00:08 +0200
Subject: [PATCH] Initial systemd enablement

This is a squash of the following four commits:
0e8635704a62d53d4876f5f461e16ba84557f4f6 Switch off systemd detection
a5d9ee110a132d80043cc2369ee50e7ed4743ecf init: add systemd service files
7479f030dd97c94b3db6fb74f3c3f0344dc9e615 Added a systemd target for ceph
b552baad2413ed01115fa408099604fc62610f52 rcceph: wrapper for ceph systemd

Signed-off-by: Owen Synge <osynge@suse.com>
Signed-off-by: Nathan Cutler <ncutler@suse.com>
(cherry picked from commit df592fedaadc6a1158e0f7f4ed15702f126c5163)
---
 ceph.spec.in              |  1 +
 src/Makefile.am           |  4 +++
 src/ceph-osd-prestart.sh  | 50 +++++++++++++++++++++++++++++++++++
 src/init-ceph.in          |  2 ++
 src/upstart/ceph-osd.conf | 31 +---------------------
 systemd/ceph              | 66 +++++++++++++++++++++++++++++++++++++++++++++++
 systemd/ceph-mds@.service | 13 ++++++++++
 systemd/ceph-mon@.service | 18 +++++++++++++
 systemd/ceph-osd@.service | 14 ++++++++++
 systemd/ceph.target       |  4 +++
 10 files changed, 173 insertions(+), 30 deletions(-)
 create mode 100644 src/ceph-osd-prestart.sh
 create mode 100644 systemd/ceph
 create mode 100644 systemd/ceph-mds@.service
 create mode 100644 systemd/ceph-mon@.service
 create mode 100644 systemd/ceph-osd@.service
 create mode 100644 systemd/ceph.target

diff --git a/ceph.spec.in b/ceph.spec.in
index 67a3825..248000b 100644
--- a/ceph.spec.in
+++ b/ceph.spec.in
@@ -453,6 +453,7 @@ fi
 %endif
 %dir %{_libdir}/ceph
 %{_libdir}/ceph/ceph_common.sh
+%{_libexecdir}/ceph/ceph-osd-prestart.sh
 %dir %{_libdir}/rados-classes
 %{_libdir}/rados-classes/libcls_rbd.so*
 %{_libdir}/rados-classes/libcls_hello.so*
diff --git a/src/Makefile.am b/src/Makefile.am
index 9c394e8..5c893ef 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -147,6 +147,7 @@ EXTRA_DIST += \
 	$(srcdir)/vstart.sh \
 	$(srcdir)/stop.sh \
 	ceph-run \
+	$(srcdir)/ceph-osd-prestart.sh \
 	$(srcdir)/ceph_common.sh \
 	$(srcdir)/init-radosgw \
 	$(srcdir)/init-radosgw.sysv \
@@ -217,6 +218,9 @@ doc_DATA = $(srcdir)/sample.ceph.conf sample.fetch_config
 shell_commondir = $(libdir)/ceph
 shell_common_SCRIPTS = ceph_common.sh
 
+ceph_libexecdir = $(libexecdir)/ceph
+ceph_libexec_SCRIPTS = ceph-osd-prestart.sh
+
 bash_completiondir = $(sysconfdir)/bash_completion.d
 bash_completion_DATA = $(srcdir)/bash_completion/ceph \
                $(srcdir)/bash_completion/rados \
diff --git a/src/ceph-osd-prestart.sh b/src/ceph-osd-prestart.sh
new file mode 100644
index 0000000..77153c9
--- /dev/null
+++ b/src/ceph-osd-prestart.sh
@@ -0,0 +1,50 @@
+#!/bin/sh
+
+eval set -- "$(getopt -o i: --long id:,cluster: -- $@)"
+
+while true ; do
+	case "$1" in
+		-i|--id) id=$2; shift 2 ;;
+		--cluster) cluster=$2; shift 2 ;;
+		--) shift ; break ;;
+	esac
+done
+
+if [ -z "$id"  ]; then
+    echo "Usage: $0 [OPTIONS]"
+    echo "--id/-i ID        set ID portion of my name"
+    echo "--cluster NAME    set cluster name (default: ceph)"
+    exit 1;
+fi
+
+update="$(ceph-conf --cluster=${cluster:-ceph} --name=osd.$id --lookup osd_crush_update_on_start || :)"
+
+if [ "${update:-1}" = "1" -o "${update:-1}" = "true" ]; then
+    # update location in crush
+    hook="$(ceph-conf --cluster=${cluster:-ceph} --name=osd.$id --lookup osd_crush_location_hook || :)"
+    if [ -z "$hook" ]; then
+        hook="/usr/bin/ceph-crush-location"
+    fi
+    location="$($hook --cluster ${cluster:-ceph} --id $id --type osd)"
+    weight="$(ceph-conf --cluster=${cluster:-ceph} --name=osd.$id --lookup osd_crush_initial_weight || :)"
+    defaultweight=`df -P -k /var/lib/ceph/osd/${cluster:-ceph}-$id/ | tail -1 | awk '{ d= $2/1073741824 ; r = sprintf("%.2f", d); print r }'`
+    ceph \
+        --cluster="${cluster:-ceph}" \
+        --name="osd.$id" \
+        --keyring="/var/lib/ceph/osd/${cluster:-ceph}-$id/keyring" \
+        osd crush create-or-move \
+        -- \
+        "$id" \
+        "${weight:-${defaultweight:-1}}" \
+        $location
+fi
+
+journal="/var/lib/ceph/osd/${cluster:-ceph}-$id/journal"
+if [ -L "$journal" -a ! -e "$journal" ]; then
+    udevadm settle --timeout=5 || :
+    if [ -L "$journal" -a ! -e "$journal" ]; then
+        echo "ceph-osd($UPSTART_INSTANCE): journal not present, not starting yet." 1>&2
+        stop
+        exit 0
+    fi
+fi
diff --git a/src/init-ceph.in b/src/init-ceph.in
index 5fb8d6d..6506c56 100644
--- a/src/init-ceph.in
+++ b/src/init-ceph.in
@@ -48,6 +48,8 @@ EXIT_STATUS=0
 # detect systemd
 SYSTEMD=0
 grep -qs systemd /proc/1/comm && SYSTEMD=1
+# dont detect on SLE12
+SYSTEMD=0
 
 signal_daemon() {
     name=$1
diff --git a/src/upstart/ceph-osd.conf b/src/upstart/ceph-osd.conf
index 6fa97ed..b61d4b3 100644
--- a/src/upstart/ceph-osd.conf
+++ b/src/upstart/ceph-osd.conf
@@ -15,36 +15,7 @@ pre-start script
 
     install -d -m0755 /var/run/ceph
 
-    update="$(ceph-conf --cluster=${cluster:-ceph} --name=osd.$id --lookup osd_crush_update_on_start || :)"
-    if [ "${update:-1}" = "1" -o "${update:-1}" = "true" ]; then
-        # update location in crush
-	hook="$(ceph-conf --cluster=${cluster:-ceph} --name=osd.$id --lookup osd_crush_location_hook || :)"
-	if [ -z "$hook" ]; then
-	    hook="/usr/bin/ceph-crush-location"
-	fi
-	location="$($hook --cluster ${cluster:-ceph} --id $id --type osd)"
-	weight="$(ceph-conf --cluster=${cluster:-ceph} --name=osd.$id --lookup osd_crush_initial_weight || :)"
-	defaultweight=`df -P -k /var/lib/ceph/osd/${cluster:-ceph}-$id/ | tail -1 | awk '{ d= $2/1073741824 ; r = sprintf("%.2f", d); print r }'`
-	ceph \
-            --cluster="${cluster:-ceph}" \
-            --name="osd.$id" \
-            --keyring="/var/lib/ceph/osd/${cluster:-ceph}-$id/keyring" \
-            osd crush create-or-move \
-	    -- \
-            "$id" \
-	    "${weight:-${defaultweight:-1}}" \
-	    $location
-    fi
-
-    journal="/var/lib/ceph/osd/${cluster:-ceph}-$id/journal"
-    if [ -L "$journal" -a ! -e "$journal" ]; then
-        udevadm settle --timeout=5 || :
-	if [ -L "$journal" -a ! -e "$journal" ]; then
-            echo "ceph-osd($UPSTART_INSTANCE): journal not present, not starting yet." 1>&2
-	    stop
-	    exit 0
-	fi
-    fi
+    /usr/libexec/ceph/ceph-osd-prestart.sh --cluster="${cluster:-ceph}" -i "$id"
 end script
 
 instance ${cluster:-ceph}/$id
diff --git a/systemd/ceph b/systemd/ceph
new file mode 100644
index 0000000..e60dec6
--- /dev/null
+++ b/systemd/ceph
@@ -0,0 +1,66 @@
+#! /bin/bash
+
+### BEGIN INIT INFO
+# Provides:       ceph ceph-mon ceph-osd
+# Required-Start: $network $remote_fs
+# Required-Stop:  $network $remote_fs
+# Should-Start: network-remotefs
+# Should-Stop: network-remotefs
+# Default-Start:  3 5
+# Default-Stop:   0 1 2 6
+# Short-Description: Ceph is a distributed object, and block, storage platform
+# Description:    Ceph is a distributed object, block, and file storage platform
+### END INIT INFO
+
+SYSTEMD_NO_WRAP=1 . /etc/rc.status
+rc_reset
+
+action=$1 ; shift
+cluster="ceph"
+config=$1 ; shift
+
+# Shared variables by many actions
+dir_mon="/var/lib/ceph/mon/"
+dir_osd="/var/lib/ceph/osd/"
+if test -d ${dir_mon} ; then
+lmon=`ls ${dir_mon} | grep ${cluster}`
+fi
+if test -d ${dir_osd} ; then
+losd=`ls ${dir_osd} | grep ${cluster}`
+fi
+prefix="${cluster}-"
+
+if test -n "$config" ; then
+	systemctl "${action}" "ceph-mon@${config}.service"
+else
+	case $action in
+    start | stop | status | enable | disable | restart | is-active | is-failed | show | kill | reset-failed  )
+        n=0
+        if test -n ${lmon} ; then
+            for s in ${lmon#=${prefix}} ; do
+                systemctl "${action}" ceph-mon@${s#$prefix}.service
+                rc_check
+                ((++n))
+            done
+        fi
+        if test -n ${lmon} ; then
+            for s in ${losd#=${prefix}} ; do
+                systemctl "${action}" ceph-osd@${s#$prefix}.service
+                rc_check
+                ((++n))
+            done
+        fi
+        if test $n -gt 0 ; then
+			rc_status
+		else
+			rc_status -u
+		fi
+    ;;
+	*)
+		echo "Invalid paramter : $action"
+        echo "Valid paramters  : start | stop | status | enable | disable | restart | is-active | is-failed | show | kill | reset-failed"
+	;;
+	esac
+fi
+rc_exit
+
diff --git a/systemd/ceph-mds@.service b/systemd/ceph-mds@.service
new file mode 100644
index 0000000..22a787d
--- /dev/null
+++ b/systemd/ceph-mds@.service
@@ -0,0 +1,13 @@
+[Unit]
+Description=Ceph metadata server daemon
+After=network-online.target local-fs.target
+Wants=network-online.target local-fs.target
+PartOf=ceph.target
+
+[Service]
+EnvironmentFile=-/etc/sysconfig/ceph
+Environment=CLUSTER=ceph
+ExecStart=/usr/bin/ceph-mds -f --cluster ${CLUSTER} --id %i
+
+[Install]
+WantedBy=ceph.target
diff --git a/systemd/ceph-mon@.service b/systemd/ceph-mon@.service
new file mode 100644
index 0000000..437ef4d
--- /dev/null
+++ b/systemd/ceph-mon@.service
@@ -0,0 +1,18 @@
+[Unit]
+Description=Ceph cluster monitor daemon
+
+# According to:
+#   http://www.freedesktop.org/wiki/Software/systemd/NetworkTarget
+# these can be removed once ceph-mon will dynamically change network
+# configuration.
+After=network-online.target local-fs.target
+Wants=network-online.target local-fs.target
+PartOf=ceph.target
+
+[Service]
+EnvironmentFile=-/etc/sysconfig/ceph
+Environment=CLUSTER=ceph
+ExecStart=/usr/bin/ceph-mon -f --cluster ${CLUSTER} --id %i
+
+[Install]
+WantedBy=ceph.target
diff --git a/systemd/ceph-osd@.service b/systemd/ceph-osd@.service
new file mode 100644
index 0000000..edb24ad
--- /dev/null
+++ b/systemd/ceph-osd@.service
@@ -0,0 +1,14 @@
+[Unit]
+Description=Ceph object storage daemon
+After=network-online.target local-fs.target
+Wants=network-online.target local-fs.target
+PartOf=ceph.target
+
+[Service]
+EnvironmentFile=-/etc/sysconfig/ceph
+Environment=CLUSTER=ceph
+ExecStart=/usr/bin/ceph-osd -f --cluster ${CLUSTER} --id %i
+ExecStartPre=/usr/libexec/ceph/ceph-osd-prestart.sh --cluster ${CLUSTER} --id %i
+
+[Install]
+WantedBy=ceph.target
diff --git a/systemd/ceph.target b/systemd/ceph.target
new file mode 100644
index 0000000..60734ba
--- /dev/null
+++ b/systemd/ceph.target
@@ -0,0 +1,4 @@
+[Unit]
+Description=ceph target allowing to start/stop all ceph*@.service instances at once
+[Install]
+WantedBy=multi-user.target
