Extending uShare with RC Scripts

Extending uShare with RC Scripts
In my earlier entry on uShare, I took a look at installing and using uShare. I'll spare you the introduction to uShare and get to the point of this writing. If you're unfamiliar with uShare, then by all means, read our review. In the article, I discussed creating an rc-script for uShare in Gentoo, to automate the process of starting and stopping it. This is beginning of a series of articles on how to package an application for Gentoo's Portage. I'm going to start by creating a more advanced rc-script than what I wrote earlier, then in later entries create an ebuild and show how to put it into portage so you can emerge ushare. Let's begin by looking at the original rc-script.
#!/sbin/runscript
# Copyright 2007 Sean Potter
# Distributed under the terms of the GNU General Public License, v2 or later

depend()
{
  need net
  use logger
}

start() {
        ebegin "Starting uShare with XBox 360 Support"
        start-stop-daemon --start --background --pidfile /var/run/ushare.pid --make-pidfile --exec /usr/bin/ushare -- -i eth1 -x -c /opt -n $HOSTNAME
        eend $?
}

stop () {
        ebegin "Stopping uShare with XBox 360 Support"
        start-stop-daemon --stop --quiet --pidfile=/var/run/ushare.pid
        eend $?
}
I'm not going to go over the basics of the rc-script, you can look at the Gentoo Wiki for that. Instead, let's examine the script more closely. Specifically, I want to look at how the service is started. The script executes /usr/bin/ushare -i eth1 -x -c /opt -n $HOSTNAME. These are some rather fixed items, and unless the person is comfortable with editing an rc-script, then they're going to be stuck with the options I used. The only flexible option here is a name of the server, $HOSTNAME. We can give users more flexibility in configuration by creating a configuration file for the script.Let's start by opening a new file, /etc/conf.d/ushare, in your favorite text editor. In this file, we want to configure the options being passed to uShare when its rc-script is started. Let's create four variables, uNAME, uINT, uPORT, and uOPTS. uNAME is the name of the server, $HOSTNAME by default. uINT specifies the interface we're broadcasting on, as uShare currently only supports one interface per instance. uPORT specifies the port number to run uShare on, and uOPTS will hold any other options we decide to pass. Let's look at how my file is set up.
#/etc/conf.d/ushare

# name of the server
uNAME="$HOSTNAME"

# Interface to run uShare on
uINT="eth1"

# Port to run uShare on
uPORT="49152"

# Additional options, note that each shared folder must have it's own -c option.
uOPTS="-x -c /opt/music -c /opt/movies -n $HOSTNAME"

# Comment the above and uncomment the following for PS3 support
# uOPTS="-d -c /opt -n $HOSTNAME"
Simplistic, and yet efficient. Now we need to make our rc-script work with it. To begin, we need to make sure that the user doesn't have an empty or missing variable. We'll add the checkconfig function to our rc-script to make sure all four variables are present. We can access our variables from the config file the same way we access HOSTNAME, via $variables.
#!/sbin/runscript
# Copyright 2007 Sean Potter
# Distributed under the terms of the GNU General Public License, v2 or later

depend()
{
  need net
  use logger
}

checkconfig() {
        if [ -z "$uNAME" ] || [ -z "$uINT" ] || [ -z "$uOPTS" ] || [ -z "$uPORT" ] ; then
          eerror "You must set config options in /etc/conf.d/ushare first"
          return 1
        fi
}

start() {
        ebegin "Starting uShare with XBox 360 Support"
        checkconfig || return 1
        start-stop-daemon --start --background --pidfile /var/run/ushare.pid --make-pidfile --exec /usr/bin/ushare -- -i $uINT -p $uPORT $uOPTS -n $uNAME
        eend $?
}

stop () {
        ebegin "Stopping uShare with XBox 360 Support"
        start-stop-daemon --stop --quiet --pidfile=/var/run/ushare.pid
        eend $?
}
The end result looks good to me. We're checking for required configuration options, then starting the service with the options that we configured. If you're into more advanced scripting, you could probably extend this further so you would be able to declare all your shared directories in a single variable, then let the script take care of passing each one to uShare. If you've got the knowledge to write that kind of script, please reply join the discussion in our forums. For this post's purposes, this simple configuration is all that is needed. In my next article on uShare, we'll look at creating an ebuild of the file to insert into portage. I hope you enjoyed this short how-to. In a future article, I'll show you how to package up uShare's sourcecode and our custom RC scripts into an eBuild for Gentoo's portage. Stay tuned!