In the last couple of posts, we used DTrace to notify our media servers and perfected our script a bit.

But the script is still not ready to be used on our home servers yet: It requires manual start and stop, not quite the service oriented automatism we're used to in the Oracle Solaris world.

The next step is to wrap our DTrace script inside a Service Management Facility (SMF) service, then wrap everything into a shell script that will easily install or remove the service whenever we need it.

The Joy of SMF

The Solaris Service Management Facility is one of my favorite features even though it undeservedly doesn't get as much attention as ZFS or Zones. Here's why it's so cool:

  • It lets you list, start, stop and monitor all of your daemons and services with a simple command,
  • It stores all information about your services in a single place, in an open data format,
  • It makes the boot process much more organized, and even parallelizes it for you,
  • And it can do much more for you. Read the SMF(5) man page for more.
In this case, we want to turn last post's DTrace script into an SMF service that can be started, stopped and monitored. Then, we want to wrap everything into a simple shell script that keeps everything together, making it easy to install and de-install.

Let's start by looking at the typical components of an SMF service.

Solaris SMF Service Components

To create an SMF service, we just need three things:

  • An SMF Manifest that describes our service to the system,
  • the actual daemon binary or other script that performs the service (in our case it's a DTrace script),
  • one or more start/stop methods that do the actual work of managing the service. This is what used to be /etc/init.d start/stop scripts in the old days, but you can also tell SMF to just start/kill your process instead.
Wrapping a DTrace Script Into an SMF Service

Sam Falkner blogged about how to wrap a DTrace script inside an SMF service some time ago, including a nice manifest that we can use as a starting point.

Another great help for creating your own SMF manifests is "Manifold": An easy-to-use Python script that asks you some simple questions, then creates the whole manifest for you. Neat!

For our purposes, we should include a dependency on zones (since our media services may should run inside them):

Brian Leonhard recently blogged about restricting privileges for Solaris services, which contains a lot of knowledge that we can use to restrict our DTrace script to just the set of privileges it needs.

Here's what I came up with after analyzing the privileges that we actually need:

We also need to tweak the execution methods to fit our DTrace script's (called dirtrap.d) command line options:

(We'll discuss filling in the %{script/*} bits later).

And finally, we'll use our own properties for the service, reflecting the path to our media and the command we need to restart our media server daemon with:

Wrapping an SMF Service Into a Shell Script Using "Here Documents"

Now that we have wrapped our DTrace script into an SMF service by creating a manifest to go along with it, we're ready to install.

But wait: Like all the other home server management scripts we discussed so far, we'd like to have it all in a single script that includes all the install and de-install work for us, not just the bare components of the service.

Wrapping our DTrace script and the manifest is just a matter of using "Here Documents":

cat > $TMPFILE