Mittwoch, 22. Februar 2012

mythtv and NAS storage for recordings

Since 0.22 mythtv introduced the Storage Group feature: multiple single drives are used to store the recordings or archived movies. The system uses some clever routines to utilize the least utilized (in terms of IOPS or free space) for new recordings and other IO activity.

A loss of a single drive will only impact the recordings on this drive. It's not like a RAID array where a loss of a drive may impact RAID performance or redundancy.

This system is nice and clever for recordings, which can be re-recorded when a show is again in the schedule. More drives allow smooth recordings of multiple recordings at once.

But for archived recordings I prefer an external RAID array - Synology, QNAP - whatever in RAID6 mode. This solution allows to loose two drives out of the array and still have your data available - which does not replace a backup, but protects against the loss of a single drive. Using mythnuv2mkv.sh script I can archive recordings (with an impact on my power bill, as the sytem takes ages to archive HDTV recordings) on the NAS together with my pictures.

But you do not want to wake up the NAS system for every recording on a mixed frontend/backend system. The NAS spindles down when not in use - so I need a solution which actives the NAS system when I start mythfrontend via mythwelcome, but keeps it idle when just recording a show and goes back to sleep.

To do this I modified /usr/share/mythtv/mythfrontend.sh (mythbuntu 11.04) system. As this file is being overwritten with the daily nightly updates, I need a solution to keep my changes to the file.

So each change is being backuped in the same directory and named as mythfrontend.sh.YYYYMMDD (Year, Month, Day without spaces).

The following two lines in /etc/rc.local will replace with each reboot the standard script with the latest backup:

# Replace mythfrontend.sh with our version
# Backup files have .YYYYMMDD as suffix - use latest
cp -f `ls /usr/share/mythtv/mythfrontend.sh.2* | sort -r | head -1` \
      /usr/share/mythtv/mythfrontend.sh
Now I just need to have one backup file with my changes to mount the NAS filer when mythfrontend will be started via lirc and mythwelcome.

The actual start of mythfrontend.real is being initiated in the last if-block in mythfrontend.sh. Use vim +95 mythfrontend.sh to change the code to:
    if [ "$IGNORE_NOT" = "0" ]; then
        /usr/share/mythtv/mythmount.sh
        /usr/bin/mythfrontend.real "$@"
        /usr/share/mythtv/mythunmount.sh
    fi
and backup the modified mythfrontend.sh to mythfrontend.sh.YYYMMDD (replace of course YYYY, MM and DD with the actual date).

mythmount.sh and mythunmount.sh are two new scripts which mount and unmount the NAS shares to /mnt. /mnt has four prepared mount points, FSK00, FSK06, FSK12 and FSK16 (which are the age classifications in Germany, without, six years, twelve years, sixteen years).

Now create /usr/share/mythtv/mythmount.sh:
#!/bin/sh
# Mount NFS shares
for SHARE in FSK00 FSK06 FSK12 FSK16 music pictures FSK00 FSK06; do
    # Check if already mounted (multiple starts of mythfrontend)
    MOUNTED=`mount | grep "/mnt/${SHARE}" | uniq`
    # Mount if not already mounted
    [ "$MOUNTED" ] || mount /mnt/${SHARE}
done
make it executable for everyone and change ownership to mythtv:mythtv. You see that the first two shares are again in the for-list at the end - sometimes the NAS requires too long to spindle up and become available that first two mount actions will time out. Adjust as needed.

Create mythunmount.sh as well:
#!/bin/sh
# umount NFS shares
for SHARE in FSK00 FSK06 FSK12 FSK16 music pictures; do
    # Check if already mounted (multiple starts of mythfrontend)
    MOUNTED=`mount | grep "/mnt/${SHARE}" | uniq`
    # umount if already mounted
    [ "$MOUNTED" ] && umount /mnt/${SHARE}
done
 and change again ownership and executable-bits.

To automatically mount the shares you also need to prepare /etc/fstab and add a line for each share:
//192.168.1.17/movies-fsk0    /mnt/FSK00    cifs    username=mythtv,password=XXX,uid=107,gid=112,iocharset=utf8,user,noauto    0    0
//192.168.1.17/movies-fsk6    /mnt/FSK06    cifs    username=mythtv,password=XXX,uid=107,gid=112,iocharset=utf8,user,noauto    0    0
//192.168.1.17/movies-fsk12    /mnt/FSK12    cifs    username=mythtv,password=XXX,uid=107,gid=112,iocharset=utf8,user,noauto    0    0
//192.168.1.17/movies-fsk16    /mnt/FSK16    cifs    username=mythtv,password=XXX,uid=107,gid=112,iocharset=utf8,user,noauto    0    0
Of course this requires an user account named mythtv with password XXX on the NAS solution. Adjust uid and gid settings to your system and change the IP address to the one from your NAS device.

Now the shares are mounted and unmounted, but mythtv does not see any archived recordings from the NAS, as mythtv searches for files in /var/lib/mythtv/videos, not in /mnt. Just add a symlink for each share into that folder:
lrwxrwxrwx  1 root   mythtv   10 2011-03-31 22:36 FSK00 -> /mnt/FSK00
lrwxrwxrwx  1 root   mythtv   10 2011-03-31 22:36 FSK06 -> /mnt/FSK06
lrwxrwxrwx  1 root   mythtv   10 2011-03-31 22:36 FSK12 -> /mnt/FSK12
lrwxrwxrwx  1 root   mythtv   10 2011-03-31 22:36 FSK16 -> /mnt/FSK16
lrwxrwxrwx  1 root   mythtv   10 2011-03-31 22:36 FSK18 -> /mnt/FSK18
Now after a reboot you should have a working combination of NAS external storage, accessing organized archived recordings while the NAS stays quit when the mixed frontend/backend is recording a new show. Whenever a human being starts mythfrontend via mythwelcome, NAS is being mounted and archived recordings become available.