Great thing about NAS4Free is the opportunity to do really powerful scripting. You can do automation scripts on par with full-blown *nix distributions. Better still, you can even configure them to run at desired times giving you full hand-off experience.
With all that power and possibility of scripts running left and right, it pays off to do some sanity checking. Most common one is disabling multiple instances (particularly important for backup script).
Since NAS4Free is FreeBSD-based, some things might be a bit different than your everyday Linux but rough code would be:
SCRIPT_NAME=`basename $0`PID_FILE="/var/tmp/.$SCRIPT_NAME.pid"PID_LAST=`cat $PID_FILE 2> /dev/null`if[-n"$PID_LAST"];thenPID_ACTIVE=`ps $PID_LAST |grep"^$PID_LAST"|grep"${SCRIPT_NAME}\$"`if[-n"$PID_ACTIVE"];thenecho"ERROR: Script is already running!">&2exit255fifiecho$$>$PID_FILE# DO SOMETHING USEFULrm$PID_FILE
When script is started for first time, process identifier (PID) is written to /var/tmp/.script.sh.pid (where script.sh is name of our script). This is followed by some useful action (insert your code here). When the job is done, /var/tmp/.script.sh.pid is simply removed and everything is fresh for a new start.
If we start second instance of script before first one has completed, script will find its temporary file. While it might stop with an error at this point, I prefer it to run additional check. Previous script might have crashed and thus temporary file might be just a leftover and there is nothing really running.
So it reads content of a temporary file in order to find previously used PID. That PID is then found in the list of processes and compared against a script name (maybe some other script is running under same PID - rare but happens). If process with same number and name is found, script throws an error and exits.
Simple and works 99% of time.
Script does have some issues. You cannot have two scripts with same name using the same trick because temporary file is based on a name only. Similar problem might be a false positive if another script with same name accidentally gets same PID as our previously running script (although this is highly unlikely). Solution for both would be using realpath command and basing temporary file and PID search on it.
Another issue might be how easy is to trick script into a parallel execution. Just remove temporary PID file and script will be no wiser. And there is no real way around this. Intention of this code is to prevent accidental parallel execution - if user wants to shoot at his foot, he will do it.
You can really make this as complicated or as simple as you wish. I found my sweet spot in the code above.
[2018-07-22: NAS4Free has been renamed to XigmaNAS as of July 2018]
As I was building my media server, I decided I must have support for both network file sharing and DAAP protocol. That way I could (comfortably) listen to my media collection using both my PC and my mobile phone. That simple requirement and fact that I had some experience with FreeNAS 0.7 drove me to NAS4Free.
I installed it in VirtualBox with two virtual drives; one 512 MB and another 2 GB. On smaller one I did embedded install of NAS4Free. Second one I formatted as ZFS (a.k.a. single *nix file system that doesn’t suck on power loss) and assigned to a single ZFS virtual device which was in turn assigned to a single ZFS pool. Both DAAP and samba were then forwarded to this share for their consumables.
Since I naively allocated only 2 GB I soon stumbled upon topic of this blog post. How do I increase my ZFS volume?
Increasing virtual disk is really easy and there is probably handful tools for every disk type (VHD in my case). I decided to use VirtualBox built-in vboxmanage tool (found in VirtualBox’s directory; don’t forget to turn off virtual machine):
While this was simple enough, it didn’t resolve anything. Next boot-up showed that ZFS still assumed 2 GB as size of a pool (at Disks, ZFS, Pools). If there was only a way for ZFS to recognize disk was bigger…
Fortunately there was just such a command. I went to Advanced, Command and executed following line:
zpool online -e Media-Pool ada1
Media-Pool was name of ZFS pool I was increasing and ada1 was actual physical disk that pool was located on. And that was it, my disk was increased painlessly and without any need for copying data around (except for backup purpose, of course). While it was nowhere close to comfort of using mouse to perform this task in Disk Management, it wasn’t too bad either.
PS: This post assumes that you know how to use NAS4Free. If you don’t use it, do try it out. You’ll love it.
PPS: Essentially same procedure works for FreeNAS or any other situation where you have virtualized disk drive with ZFS pool on it.