1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Mod: helper script for network shares

Discussion in 'HD/HDR-FOX T2 Customised Firmware' started by Drutt, Oct 22, 2011.

  1. Drutt

    Drutt Active Member

    I have knocked up a script that attempts to simplify nfs / smb setup by allowing shares to be configured on the box, using folder names in a special "settings" directory to add and configure shares. It runs in the background, pinging the host(s) and mounting / unmounting the share when applicable. This should solve the problem of the Humax locking up if the computer goes to sleep. Its a bit rough and ready at the moment, and only tested on an HD. I'd appreciate any feedback / improvements / HDR testing, and then I can put it into a nice package :)

    Put the script in a file called /mod/sbin/scanMounts, and create a startup script in /mod/etc/init.d to kick it off (with an "&" after to run in the background). When a new directory is created in smb or nfs, after a few seconds it should create template "options" within it for you to fill in.

    Code:
    #!/bin/sh
    
    TryMount()
    {
        type=$1
        name=$2
    
        host=$(ls "$name" | grep -e "^host" | cut -d'=' -f2 | sed -e 's/_/./g') #convert "_"s to "."s in host
        folder=$(ls "$name" | grep -e "^folder" | cut -d'=' -f2 | sed -e 's/_/\//g') #convert "_"s to "/"s in folder name
    
        ping -c 3 $host > /dev/null
        if [ $? != 0 ]; then                # mount or unmount?
            echo "$host not found.."
            # unmount if mounted
            if [ -d "/media/$name" ]; then
                echo "umount "/TEMP/$name""
                umount "/media/$name" -l
                rmdir "/media/$name"
            fi
        else
            if [ ! -d "/media/$name" ]; then
                echo "$host is on-line - attempting to mount"
                mkdir "/media/$name"        # and in My Videos?
                if [ $type = "smb" ]; then
                    user=$(ls "$name" | grep -e "^user")        # keep the [user|password etc]=... for these as they go into mount int that format
                    password=$(ls "$name" | grep -e "^password")
                    domain=$(ls "$name" | grep -e "^domain")
                    echo "mount -t cifs "//$host/$folder" "/media/$name" -o $user,$password,$domain,$workgroup"
                    mount -t cifs "//$host/$folder" "/media/$name" -o $user,$password,$domain,$workgroup
                elif [ $type = "nfs" ]; then
                    echo "mount -o soft "$host:/$folder" "/media/$name""
                    mount -o soft "$host:/$folder" "/media/$name"
                fi
    
                if [ $? != 0 ]; then                # failed
                    echo "Mount failed..."
                    rmdir "/media/$name"
                fi
            fi
        fi
    }
    
    CreateExample()
    {
        type=$1
        name=$2
        mkdir "$name/host=10_0_1_XX"
        mkdir "$name/folder=ShareFolder (use _ for slash)"
        if [ $type = "smb" ]; then
            mkdir "$name/user=User"
            mkdir "$name/password=Password"
            mkdir "$name/*workgroup=Workgroup (Optional - remove *)"
            mkdir "$name/*domain=Domain (Optional - remove *)"
        fi
    }
    
    exec >/tmp/scanmounts.log 2>&1
    
    cd /mod/settings
    # create some help 1st time
    if [ ! -d smb ]; then
        mkdir -p "smb/*Create folder with mount name (opt+)"
        mkdir -p "smb/*Then edit options in folder"
    fi
    if [ ! -d nfs ]; then
        mkdir -p "nfs/*Create folder with mount name (opt+)"
        mkdir -p "nfs/*Then edit options in folder"
    fi
    
    # set up links to where we can get at the settings folder (a bit messy due to HDR / HD differences)
    if [ -d "/media/My Video" ]; then                # HDR - add link to settings here as it can't get to /mod
        mkdir -p "/media/My Video/*modSettings/smb"
        mkdir -p "/media/My Video/*modSettings/nfs"
        mount /mod/settings/smb "/media/My Video/*modSettings/smb"
        mount /mod/settings/nfs "/media/My Video/*modSettings/nfs"
    fi
    if [ -d "/media/drive1/*ModSettings" ]; then    # HD with *modsettings on root- add link to settings here
        mkdir -p "/media/drive1/*ModSettings/smb"
        mkdir -p "/media/drive1/*ModSettings/nfs"
        mount /mod/settings/smb "/media/drive1/*ModSettings/smb"
        mount /mod/settings/nfs "/media/drive1/*ModSettings/nfs"
    fi
    
    # loop checking the mounts
    while [ 1 ]
    do
        for type in smb nfs; do
            if [ -d $type ]; then
                cd $type
                for mountname in [!*]*; do                                        #ignore "comments" starting with *
                    if [ -d "$mountname" ]; then
                        echo "Checking $mountname"
                        if [ "$(ls -A "$mountname")" != "" ]; then
                            TryMount $type "$mountname"
                        else
                            CreateExample $type "$mountname"                    #Empty dir - create examples
                        fi
                    fi
                done
                cd ..        # back up to settings
            fi
        done
    
        sleep 9
    done
    
    
     
  2. Kev_w

    Kev_w Member

    This looks very usefull! I'll have a go at this tonight on my HDR if I have time. Thanks.
     
  3. Trevor Jasper

    Trevor Jasper Member

    Well done Steve.

    Just need this integrated into the webif and do it all graphically :) - tongue in cheek, but great work none-the-less.

    Haven't got round to this yet, but thinking it ought to be something I do.