Announcement

Collapse
No announcement yet.

Announcement

Collapse
No announcement yet.

[WIP] Shell script to automatically create NFS / SMB mount points

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    [WIP] Shell script to automatically create NFS / SMB mount points

    A while ago I have written a script to ease up mounting NFS shares on linux boxes. Out of academic interest I tried to use this under Android and ended up with completely rewriting the script as Android lacks some fundamental shell commands and tools, e.g. the nfs tool "showmounts" to enumerate NFS shares.

    So what does this script do:
    1. Query the user for the NFS server IP
    2. List all NFS shares this server exposes (using nmap for this)
    3. Ask for the default mount path. Default is set to /mnt/
    4. Ask for each share that is found, if this share should be mounted and to which folder it should be mounted
    5. Create an init.d file to automatically mount these shares at boot


    Code:
    #!/system/bin/sh
    
    #Default Values nfs
    nfs_options="rw,nosuid,nodev,nolock,rsize=32768,wsize=32768,intr,noatime"
    mount_path="/mnt/"
    wait_network="5"
    file_initd="/etc/init.d/97NFS"
    
    # Paths and busybox commands
    awk_cmd="busybox awk"
    stat_cmd="busybox stat"
    printf_cmd="busybox printf"
    nmap_bin="/sbin/nmap"
    nmap_data="/sdcard/Download/nmap-5.61TEST4/share/nmap/"
    nmap_dns="--dns-servers 8.8.8.8"
    
    $printf_cmd "Please enter your nfs server IP: \c"
    read nfs_server
    
    nfs_available=($($nmap_bin --script=nfs-showmount.nse $nfs_server $nmap_dns --datadir $nmap_data | grep -e "|   " -e "|_  " | $awk_cmd '{print $2;}'))
    
    # List all found nfs shares
    let i=0
    $printf_cmd "The server $nfs_server exposes following shares:\n"
    while (( ${#nfs_available[@]} > i )); do
        $printf_cmd "$i) ${nfs_available[i++]}\n"
    done
    $printf_cmd "####################\n\n"
    
    # Query user for the default base mount path
    $printf_cmd "Please enter your base path for mounting the shares.\n"
    $printf_cmd "If you are unsure leave the default value given.\n"
    $printf_cmd "Note that the path has to end with /!\n"
    $printf_cmd "Base path for mounting the nfs shares [$mount_path]: \c"
    read mount_path_new
    [ -n "$mount_path_new" ] && mount_path=$mount_path_new
    $printf_cmd "####################\n\n"
    
    #Collect all shares and corresponding folders in arrays
    let i=0
    while (( ${#nfs_available[@]} > i )); do
        $printf_cmd "Do you want to mount the share "${nfs_available[i++]}"?\n"
        select yn in "Yes" "No"; do
        case $yn in
            Yes ) 
                $printf_cmd "Please enter the name of the local folder to mount this share: \c";
                read local_folder;
                if [ -d "$mount_path$local_folder" ]; then
                    folder_type=$($stat_cmd -f $mount_path$local_folder | grep -e "Type" | $awk_cmd '{print $6;}')
                    $printf_cmd "The path $mount_path$local_folder already exists\n"
                    $printf_cmd "Folder type of $mount_path$local_folder is: $folder_type\n"
                    $printf_cmd "Do you really want to use this folder?\n"
                    select yn in "Yes" "No"; do
                    case $yn in
                        Yes )
                            to_mount+=("busybox mount -o $nfs_options $nfs_server:${nfs_available[i++]} $mount_path$local_folder\n");
                            break;;
                        No )
                            $printf_cmd "Please enter a new name of the local folder to mount this share: \c";
                            read local_folder;
                            to_create+=("mkdir $mount_path$local_folder\n");
                            to_mount+=("busybox mount -o $nfs_options $nfs_server:${nfs_available[i++]} $mount_path$local_folder\n");
                            break;;
                    esac
                    done
                else
                    to_create+=("mkdir $mount_path$local_folder\n");
                    to_mount+=("busybox mount -o $nfs_options $nfs_server:${nfs_available[i++]} $mount_path$local_folder\n");
                fi
                break;;
            No ) break;;
        esac
        done
    done
    
    let i=0
    while (( ${#to_create[@]} > i )); do
        $printf_cmd "$i) ${to_create[i++]}\n"
    done
    let i=0
    while (( ${#to_mount[@]} > i )); do
        $printf_cmd "$i) ${to_mount[i++]}\n"
    done
    
    # Create the init.d file
    if [ -f "$file_initd" ]
    then
      rm "$file_initd"
      touch "$file_initd"
    else
      touch "$file_initd"
    fi
    
    (
    $printf_cmd "#!/system/bin/sh\n"
    $printf_cmd "\n"
    $printf_cmd "# Make sure networking is up before mounting\n"
    $printf_cmd "busybox sleep $wait_network\n"
    $printf_cmd "\n"
    $printf_cmd "# Switch rootfs to read / write\n"
    $printf_cmd "# Be careful after this. Misplaced commands may\n"
    $printf_cmd "# brick your system\n"
    $printf_cmd "busybox mount -o rw,remount /\n"
    $printf_cmd "\n"
    $printf_cmd "# Create Mount Points\n"
    ) >> "$file_initd"
    let i=0
    while (( ${#to_create[@]} > i )); do
        $printf_cmd "${to_create[i++]}" >> "$file_initd"
    done
    let i=0
    $printf_cmd '\n' >> "$file_initd"
    $printf_cmd '# Mount nfs shares\n' >> "$file_initd"
    while (( ${#to_mount[@]} > i )); do
        $printf_cmd "${to_mount[i++]}" >> "$file_initd"
    done
    $printf_cmd '\n# Switch rootfs to read only\n' >> "$file_initd"
    $printf_cmd 'busybox mount -o ro,remount /\n' >> "$file_initd"
    
    
    # Make init.d file executeable and make sure it is root owned
    chmod 0755 $file_initd 
    
    # Execute the new init.d
    "$file_initd"
    This script requires nmap for Android, which you can download here: http://ftp.linux.hr/android/nmap/nma...rm-bin.tar.bz2

    The nmap /bin files and /share files have to be installed somewhere with permission to execute and the first few lines of the shell script modified to reflect this.

    Please note that this script is not really intended to be setup by the end-user. It is harder to setup this script and nmap than to manually edit a init.d file.

    If ROM Devs are interested, they could provide it preconfigured alongside with their ROMs and then it would be an easy way for the end-user to configure his NFS shares, e.g. by providing a shortcut from the launcher

    If there is interest, i could extend it to provide the same functionality for SMB shares as well, otherwise I leave it as an excerise to dust of my shell skills.

    Side note on KitKat:
    Kit Kat seems to use some kind of "emulated" folders like
    • /mnt/shell/emulated/
    • /sdcard/
    • /storage/

    and others depending on the ROM. These folder are likely to be causing empty mounts.
    In multi-user environment the path /data/media/0/ seems to be a safe bet.
    Last edited by SoulStyle; 03 June 2014, 21:29.

    #2
    Originally posted by SoulStyle View Post
    A while ago I have written a script to ease up mounting NFS shares on linux boxes. Out of academic interest I tried to use this under Android and ended up with completely rewriting the script as Android lacks some fundamental shell commands and tools, e.g. the nfs tool "showmounts" to enumerate NFS shares.

    So what does this script do:
    1. Query the user for the NFS server IP
    2. List all NFS shares this server exposes (using nmap for this)
    3. Ask for the default mount path. Default is set to /mnt/
    4. Ask for each share that is found, if this share should be mounted and to which folder it should be mounted
    5. Create an init.d file to automatically mount these shares at boot


    Code:
    #!/system/bin/sh
    
    #Default Values nfs
    nfs_options="rw,nosuid,nodev,nolock,rsize=32768,wsize=32768,intr,noatime"
    mount_path="/mnt/"
    wait_network="5"
    file_initd="/etc/init.d/97NFS"
    
    # Paths and busybox commands
    awk_cmd="busybox awk"
    stat_cmd="busybox stat"
    printf_cmd="busybox printf"
    nmap_bin="/sbin/nmap"
    nmap_data="/sdcard/Download/nmap-5.61TEST4/share/nmap/"
    nmap_dns="--dns-servers 8.8.8.8"
    
    $printf_cmd "Please enter your nfs server IP: \c"
    read nfs_server
    
    nfs_available=($($nmap_bin --script=nfs-showmount.nse $nfs_server $nmap_dns --datadir $nmap_data | grep -e "|   " -e "|_  " | $awk_cmd '{print $2;}'))
    
    # List all found nfs shares
    let i=0
    $printf_cmd "The server $nfs_server exposes following shares:\n"
    while (( ${#nfs_available[@]} > i )); do
        $printf_cmd "$i) ${nfs_available[i++]}\n"
    done
    $printf_cmd "####################\n\n"
    
    # Query user for the default base mount path
    $printf_cmd "Please enter your base path for mounting the shares.\n"
    $printf_cmd "If you are unsure leave the default value given.\n"
    $printf_cmd "Note that the path has to end with /!\n"
    $printf_cmd "Base path for mounting the nfs shares [$mount_path]: \c"
    read mount_path_new
    [ -n "$mount_path_new" ] && mount_path=$mount_path_new
    $printf_cmd "####################\n\n"
    
    #Collect all shares and corresponding folders in arrays
    let i=0
    while (( ${#nfs_available[@]} > i )); do
        $printf_cmd "Do you want to mount the share "${nfs_available[i++]}"?\n"
        select yn in "Yes" "No"; do
        case $yn in
            Yes ) 
                $printf_cmd "Please enter the name of the local folder to mount this share: \c";
                read local_folder;
                if [ -d "$mount_path$local_folder" ]; then
                    folder_type=$($stat_cmd -f $mount_path$local_folder | grep -e "Type" | $awk_cmd '{print $6;}')
                    $printf_cmd "The path $mount_path$local_folder already exists\n"
                    $printf_cmd "Folder type of $mount_path$local_folder is: $folder_type\n"
                    $printf_cmd "Do you really want to use this folder?\n"
                    select yn in "Yes" "No"; do
                    case $yn in
                        Yes )
                            to_mount+=("busybox mount -o $nfs_options $nfs_server:${nfs_available[i++]} $mount_path$local_folder\n");
                            break;;
                        No )
                            $printf_cmd "Please enter a new name of the local folder to mount this share: \c";
                            read local_folder;
                            to_create+=("mkdir $mount_path$local_folder\n");
                            to_mount+=("busybox mount -o $nfs_options $nfs_server:${nfs_available[i++]} $mount_path$local_folder\n");
                            break;;
                    esac
                    done
                else
                    to_create+=("mkdir $mount_path$local_folder\n");
                    to_mount+=("busybox mount -o $nfs_options $nfs_server:${nfs_available[i++]} $mount_path$local_folder\n");
                fi
                break;;
            No ) break;;
        esac
        done
    done
    
    let i=0
    while (( ${#to_create[@]} > i )); do
        $printf_cmd "$i) ${to_create[i++]}\n"
    done
    let i=0
    while (( ${#to_mount[@]} > i )); do
        $printf_cmd "$i) ${to_mount[i++]}\n"
    done
    
    # Create the init.d file
    if [ -f "$file_initd" ]
    then
      rm "$file_initd"
      touch "$file_initd"
    else
      touch "$file_initd"
    fi
    
    (
    $printf_cmd "#!/system/bin/sh\n"
    $printf_cmd "\n"
    $printf_cmd "# Make sure networking is up before mounting\n"
    $printf_cmd "busybox sleep $wait_network\n"
    $printf_cmd "\n"
    $printf_cmd "# Switch rootfs to read / write\n"
    $printf_cmd "# Be careful after this. Misplaced commands may\n"
    $printf_cmd "# brick your system\n"
    $printf_cmd "busybox mount -o rw,remount /\n"
    $printf_cmd "\n"
    $printf_cmd "# Create Mount Points\n"
    ) >> "$file_initd"
    let i=0
    while (( ${#to_create[@]} > i )); do
        $printf_cmd "${to_create[i++]}" >> "$file_initd"
    done
    let i=0
    $printf_cmd '\n' >> "$file_initd"
    $printf_cmd '# Mount nfs shares\n' >> "$file_initd"
    while (( ${#to_mount[@]} > i )); do
        $printf_cmd "${to_mount[i++]}" >> "$file_initd"
    done
    $printf_cmd '\n# Switch rootfs to read only\n' >> "$file_initd"
    $printf_cmd 'busybox mount -o ro,remount /\n' >> "$file_initd"
    
    
    # Make init.d file executeable and make sure it is root owned
    chmod 0755 $file_initd 
    
    # Execute the new init.d
    "$file_initd"
    This script requires nmap for Android, which you can download here: http://ftp.linux.hr/android/nmap/nma...rm-bin.tar.bz2

    The nmap /bin files and /share files have to be installed somewhere with permission to execute and the first few lines of the shell script modified to reflect this.

    Please note that this script is not really intended to be setup by the end-user. It is harder to setup this script and nmap than to manually edit a init.d file.

    If ROM Devs are interested, they could provide it preconfigured alongside with their ROMs and then it would be an easy way for the end-user to configure his NFS shares, e.g. by providing a shortcut from the launcher

    If there is interest, i could extend it to provide the same functionality for SMB shares as well, otherwise I leave it as an excerise to dust of my shell skills.

    Side note on KitKat:
    Kit Kat seems to use some kind of "emulated" folders like
    • /mnt/shell/emulated/
    • /sdcard/
    • /storage/

    and others depending on the ROM. These folder are likely to be causing empty mounts.
    In multi-user environment the path /data/media/0/ seems to be a safe bet.
    looks very good to me oO.
    great thx from my side
    i think many people will be thankful to you because i think about 50% or maybe more user are using nfs ^^

    and yes,youre right,maybe the devs can add this little piece of gold into the roms.
    would be very nice
    so BBBIIIGGGGG thx

    Comment


      #3
      Originally posted by SoulStyle View Post
      A while ago I have written a script to ease up mounting NFS shares on linux boxes. Out of academic interest I tried to use this under Android and ended up with completely rewriting the script as Android lacks some fundamental shell commands and tools, e.g. the nfs tool "showmounts" to enumerate NFS shares.

      So what does this script do:
      1. Query the user for the NFS server IP
      2. List all NFS shares this server exposes (using nmap for this)
      3. Ask for the default mount path. Default is set to /mnt/
      4. Ask for each share that is found, if this share should be mounted and to which folder it should be mounted
      5. Create an init.d file to automatically mount these shares at boot

      Code:
      #!/system/bin/sh
      
      #Default Values nfs
      nfs_options="rw,nosuid,nodev,nolock,rsize=32768,wsize=32768,intr,noatime"
      mount_path="/mnt/"
      wait_network="5"
      file_initd="/etc/init.d/97NFS"
      
      # Paths and busybox commands
      awk_cmd="busybox awk"
      stat_cmd="busybox stat"
      printf_cmd="busybox printf"
      nmap_bin="/sbin/nmap"
      nmap_data="/sdcard/Download/nmap-5.61TEST4/share/nmap/"
      nmap_dns="--dns-servers 8.8.8.8"
      
      $printf_cmd "Please enter your nfs server IP: \c"
      read nfs_server
      
      nfs_available=($($nmap_bin --script=nfs-showmount.nse $nfs_server $nmap_dns --datadir $nmap_data | grep -e "|   " -e "|_  " | $awk_cmd '{print $2;}'))
      
      # List all found nfs shares
      let i=0
      $printf_cmd "The server $nfs_server exposes following shares:\n"
      while (( ${#nfs_available[@]} > i )); do
          $printf_cmd "$i) ${nfs_available[i++]}\n"
      done
      $printf_cmd "####################\n\n"
      
      # Query user for the default base mount path
      $printf_cmd "Please enter your base path for mounting the shares.\n"
      $printf_cmd "If you are unsure leave the default value given.\n"
      $printf_cmd "Note that the path has to end with /!\n"
      $printf_cmd "Base path for mounting the nfs shares [$mount_path]: \c"
      read mount_path_new
      [ -n "$mount_path_new" ] && mount_path=$mount_path_new
      $printf_cmd "####################\n\n"
      
      #Collect all shares and corresponding folders in arrays
      let i=0
      while (( ${#nfs_available[@]} > i )); do
          $printf_cmd "Do you want to mount the share "${nfs_available[i++]}"?\n"
          select yn in "Yes" "No"; do
          case $yn in
              Yes ) 
                  $printf_cmd "Please enter the name of the local folder to mount this share: \c";
                  read local_folder;
                  if [ -d "$mount_path$local_folder" ]; then
                      folder_type=$($stat_cmd -f $mount_path$local_folder | grep -e "Type" | $awk_cmd '{print $6;}')
                      $printf_cmd "The path $mount_path$local_folder already exists\n"
                      $printf_cmd "Folder type of $mount_path$local_folder is: $folder_type\n"
                      $printf_cmd "Do you really want to use this folder?\n"
                      select yn in "Yes" "No"; do
                      case $yn in
                          Yes )
                              to_mount+=("busybox mount -o $nfs_options $nfs_server:${nfs_available[i++]} $mount_path$local_folder\n");
                              break;;
                          No )
                              $printf_cmd "Please enter a new name of the local folder to mount this share: \c";
                              read local_folder;
                              to_create+=("mkdir $mount_path$local_folder\n");
                              to_mount+=("busybox mount -o $nfs_options $nfs_server:${nfs_available[i++]} $mount_path$local_folder\n");
                              break;;
                      esac
                      done
                  else
                      to_create+=("mkdir $mount_path$local_folder\n");
                      to_mount+=("busybox mount -o $nfs_options $nfs_server:${nfs_available[i++]} $mount_path$local_folder\n");
                  fi
                  break;;
              No ) break;;
          esac
          done
      done
      
      let i=0
      while (( ${#to_create[@]} > i )); do
          $printf_cmd "$i) ${to_create[i++]}\n"
      done
      let i=0
      while (( ${#to_mount[@]} > i )); do
          $printf_cmd "$i) ${to_mount[i++]}\n"
      done
      
      # Create the init.d file
      if [ -f "$file_initd" ]
      then
        rm "$file_initd"
        touch "$file_initd"
      else
        touch "$file_initd"
      fi
      
      (
      $printf_cmd "#!/system/bin/sh\n"
      $printf_cmd "\n"
      $printf_cmd "# Make sure networking is up before mounting\n"
      $printf_cmd "busybox sleep $wait_network\n"
      $printf_cmd "\n"
      $printf_cmd "# Switch rootfs to read / write\n"
      $printf_cmd "# Be careful after this. Misplaced commands may\n"
      $printf_cmd "# brick your system\n"
      $printf_cmd "busybox mount -o rw,remount /\n"
      $printf_cmd "\n"
      $printf_cmd "# Create Mount Points\n"
      ) >> "$file_initd"
      let i=0
      while (( ${#to_create[@]} > i )); do
          $printf_cmd "${to_create[i++]}" >> "$file_initd"
      done
      let i=0
      $printf_cmd '\n' >> "$file_initd"
      $printf_cmd '# Mount nfs shares\n' >> "$file_initd"
      while (( ${#to_mount[@]} > i )); do
          $printf_cmd "${to_mount[i++]}" >> "$file_initd"
      done
      $printf_cmd '\n# Switch rootfs to read only\n' >> "$file_initd"
      $printf_cmd 'busybox mount -o ro,remount /\n' >> "$file_initd"
      
      
      # Make init.d file executeable and make sure it is root owned
      chmod 0755 $file_initd 
      
      # Execute the new init.d
      "$file_initd"
      This script requires nmap for Android, which you can download here: http://ftp.linux.hr/android/nmap/nma...rm-bin.tar.bz2

      The nmap /bin files and /share files have to be installed somewhere with permission to execute and the first few lines of the shell script modified to reflect this.

      Please note that this script is not really intended to be setup by the end-user. It is harder to setup this script and nmap than to manually edit a init.d file.

      If ROM Devs are interested, they could provide it preconfigured alongside with their ROMs and then it would be an easy way for the end-user to configure his NFS shares, e.g. by providing a shortcut from the launcher

      If there is interest, i could extend it to provide the same functionality for SMB shares as well, otherwise I leave it as an excerise to dust of my shell skills.

      Side note on KitKat:
      Kit Kat seems to use some kind of "emulated" folders like
      • /mnt/shell/emulated/
      • /sdcard/
      • /storage/
      and others depending on the ROM. These folder are likely to be causing empty mounts.
      In multi-user environment the path /data/media/0/ seems to be a safe bet.

      Very good work!!
      There are 10 types of people in the world: Those who understand binary, and those who don't...

      If you want to buy me a beer, please use this -> https://www.paypalobjects.com/en_US/..._donate_SM.gif

      Comment


        #4
        Please find below a version of the script that also offers to create SMB / CIFS shares
        Code:
        #!/system/bin/sh
        #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### 
        # Developer Notes:
        #         Needs nmap: http://ftp.linux.hr/android/nmap/nmap-5.61TEST4-android-arm-bin.tar.bz2
        #  
        # License: 
        #        GNU Affero General Public License, version 3.
        #        See <http://www.gnu.org/licenses/>
        #
        # Version history:
        #         2014-05-22  first version by SoulStyle
        #        2014-06-03    Added SMB mounts, improved if up handling, 
        #                   added umount script
        #
        #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### ####
         
        #Default Values nfs
        nfs_options="rw,nosuid,nodev,nolock,rsize=32768,wsize=32768,intr,noatime"
        initd_nfs="/etc/init.d/97NFS"
        
        #Default Values for SMB/CIFS
        smb_options="iocharset=utf8,noserverino,nounix,file_mode=0777,dir_mode=0777"
        smb_port="445"
        initd_smb="/etc/init.d/98SMB"
        use_shutd="0"
        shutd_smb="/etc/shut.d/umount_SMB"
        
        # Paths and busybox commands
        awk_cmd="busybox awk"
        stat_cmd="busybox stat"
        printf_cmd="busybox printf"
        nmap_bin="/sbin/nmap"
        nmap_data="/sdcard/Download/nmap-5.61TEST4/share/nmap/"
        nmap_dns="--dns-servers 8.8.8.8"
        mount_path="/mnt/"
        
        $printf_cmd "Choose type of shares to mount:\n"
        select yn in "NFS" "SMB/CIFS"; do
        case $yn in
            NFS ) 
                share_type="NFS";
                file_initd=$initd_nfs;
                break;;
            SMB/CIFS ) 
                share_type="SMB";
                file_initd=$initd_smb;
                break;;
        esac
        done
        
        # Query user for share type
        $printf_cmd "Please enter your $share_type server IP: \c"
        read server_ip
        $printf_cmd "\n####################\n\n"
        
        # Query user for base path of the mount points
        $printf_cmd "Please enter your base path for mounting the shares.\n"
        $printf_cmd "If you are unsure leave the default value given.\n"
        $printf_cmd "Note that the path has to end with /!\n"
        $printf_cmd "Base path for mounting the nfs shares [$mount_path]: \c"
        read mount_path_new
        [ -n "$mount_path_new" ] && mount_path=$mount_path_new
        $printf_cmd "\n####################\n\n"
        
        if [[ $share_type = "NFS" ]]; then
            #NFS
            nfs_available=($($nmap_bin --script=nfs-showmount.nse $server_ip $nmap_dns --datadir $nmap_data | grep -e "|   " -e "|_  " | $awk_cmd '{print $2;}'))
            # List all found nfs shares
            let i=0
            $printf_cmd "The server $server_ip exposes following shares:\n"
            while (( ${#nfs_available[@]} > i )); do
                $printf_cmd "$i) ${nfs_available[i++]}\n"
            done
            $printf_cmd "\n####################\n\n"
            
            #Collect all shares and corresponding folders in arrays
            let i=0
            while (( ${#nfs_available[@]} > i )); do
                $printf_cmd "Do you want to mount the share "${nfs_available[i]}"?\n"
                select yn in "Yes" "No"; do
                case $yn in
                    Yes ) 
                        $printf_cmd "Please enter the name of the local folder to mount this share: \c";
                        read local_folder;
                        if [ -d "$mount_path$local_folder" ]; then
                            folder_type=$($stat_cmd -f $mount_path$local_folder | grep -e "Type" | $awk_cmd '{print $6;}')
                            $printf_cmd "The path $mount_path$local_folder already exists\n"
                            $printf_cmd "Folder type of $mount_path$local_folder is: $folder_type\n"
                            $printf_cmd "Do you really want to use this folder?\n"
                            select yn in "Yes" "No"; do
                            case $yn in
                                Yes )
                                    to_mount+=("busybox mount -o $nfs_options $server_ip:${nfs_available[i]} $mount_path$local_folder\n");
                                    break;;
                                No )
                                    $printf_cmd "Please enter a new name of the local folder to mount this share: \c";
                                    read local_folder;
                                    to_create+=("$mount_path$local_folder\n");
                                    to_mount+=("busybox mount -o $nfs_options $server_ip:${nfs_available[i]} $mount_path$local_folder\n");
                                    break;;
                            esac
                            done
                        else
                            to_create+=("$mount_path$local_folder\n");
                            to_mount+=("busybox mount -o $nfs_options $server_ip:${nfs_available[i]} $mount_path$local_folder\n");
                        fi
                        break;;
                    No ) break;;
                esac
                done
            let i++
            done
        elif [[ $share_type = "SMB" ]]; then
            #SMB
            # Query user for smb user
            $printf_cmd "Please enter your SMB user: \c"
            read smb_user
            # Query user for smb password
            $printf_cmd "Please enter your SMB password: \c"
            read smb_pass
            $printf_cmd "\n####################\n\n"
            
            oldGLOBIGNORE=$GLOBIGNORE
            oldIFS=$IFS
            nmap_cmd="$nmap_bin -T4 --script smb-enum-shares --script-args smbuser=$smb_user,smbpass=$smb_pass -p$smb_port $server_ip $nmap_dns --datadir $nmap_data"
            IFS=$'\r\n' GLOBIGNORE='*' :; nmap_smb=($(eval $nmap_cmd))
            IFS=$oldIFS
            GLOBIGNORE=$oldGLOBIGNORE
            
            smb_access=($($printf_cmd -- '%s\n' "${nmap_smb[@]}" | grep -e "\|     Current user" -e "\|_    Current user" | $awk_cmd '{print $6}'))
            smb_shares=($($printf_cmd -- '%s\n' "${nmap_smb[@]}" | grep -e "\|   [^[:space:]].*" | $awk_cmd '{print $2}'))
        
            let i=0
            $printf_cmd "The server $server_ip exposes following SMB shares:\n\n"
            $printf_cmd "%-20s %-30s\n" "Share name:" "Access Rights for $smb_user"
            while (( ${#smb_shares[@]} > i )); do
                $printf_cmd "%-20s %-30s\n" "${smb_shares[i]}" "${smb_access[i++]}"
            done
            $printf_cmd "\n####################\n\n"
            
            #Collect all shares and corresponding folders in arrays
            let i=0
            while (( ${#smb_shares[@]} > i )); do
                $printf_cmd "Do you want to mount the share "${smb_shares[i]}"?\n"
                select yn in "Yes" "No"; do
                case $yn in
                    Yes ) 
                        $printf_cmd "Please enter the name of the local folder to mount this share: \c";
                        read local_folder;
                        if [ -d "$mount_path$local_folder" ]; then
                            folder_type=$($stat_cmd -f $mount_path$local_folder | grep -e "Type" | $awk_cmd '{print $6;}')
                            $printf_cmd "The path $mount_path$local_folder already exists\n"
                            $printf_cmd "Folder type of $mount_path$local_folder is: $folder_type\n"
                            $printf_cmd "Do you really want to use this folder?\n"
                            select yn in "Yes" "No"; do
                            case $yn in
                                Yes )
                                    to_mount+=("busybox mount -t cifs -o username=$smb_user,password=$smb_pass,$smb_options //$server_ip/"${smb_shares[i]}" $mount_path$local_folder\n");
                                    break;;
                                No )
                                    $printf_cmd "Please enter a new name of the local folder to mount this share: \c";
                                    read local_folder;
                                    to_create+=("$mount_path$local_folder\n");
                                    to_mount+=("busybox mount -t cifs -o username=$smb_user,password=$smb_pass,$smb_options //$server_ip/"${smb_shares[i]}" $mount_path$local_folder\n");
                                    break;;
                            esac
                            done
                        else
                            to_create+=("$mount_path$local_folder\n");
                            to_mount+=("busybox mount -t cifs -o username=$smb_user,password=$smb_pass,$smb_options //$server_ip/"${smb_shares[i]}" $mount_path$local_folder\n");
                        fi
                        break;;
                    No ) break;;
                esac
                done
            let i++
            done
        fi
        
        $printf_cmd "Creating init.d file....."
        
        # Create the init.d file
        if [ -f "$file_initd" ]; then
          rm "$file_initd"
          touch "$file_initd"
        else
          touch "$file_initd"
        fi
        
        (
        $printf_cmd "#!/system/bin/sh\n"
        $printf_cmd "\n"
        $printf_cmd "# Make sure networking is up before mounting\n"
        $printf_cmd "while :\n"
        $printf_cmd "do\n"
        $printf_cmd "   check_if_up=(\$(netcfg | grep -e "eth0" -e "wlan0" | busybox awk '{print \$2}'))\n"
        $printf_cmd "   if [[ ("\${check_if_up[0]}" = "UP") || ("\${check_if_up[1]}" = "UP") ]]; then\n"
        $printf_cmd "    break\n"
        $printf_cmd "   fi\n" 
        $printf_cmd "   sleep 1\n"
        $printf_cmd "done\n"
        $printf_cmd "sleep 1\n"
        $printf_cmd "\n"
        $printf_cmd "# Switch rootfs to read / write\n"
        $printf_cmd "# Be careful after this. Misplaced commands may\n"
        $printf_cmd "# brick your system\n"
        $printf_cmd "busybox mount -o rw,remount /\n"
        $printf_cmd "\n"
        $printf_cmd "# Create Mount Points\n"
        ) >> "$file_initd"
        let i=0
        while (( ${#to_create[@]} > i )); do
            $printf_cmd "mkdir ${to_create[i++]}" >> "$file_initd"
        done
        let i=0
        $printf_cmd '\n' >> "$file_initd"
        $printf_cmd '# Mount $share_type shares\n' >> "$file_initd"
        while (( ${#to_mount[@]} > i )); do
            $printf_cmd "${to_mount[i++]}" >> "$file_initd"
        done
        $printf_cmd "\n# Switch rootfs to read only\n" >> "$file_initd"
        $printf_cmd "busybox mount -o ro,remount /\n" >> "$file_initd"
        
        
        # Make init.d file executeable
        chmod 0755 "$file_initd" 
        
        $printf_cmd "done\n"
        
        # Create umount script upon shutdown for SMB shares
        if ([ "$share_type" = "SMB" ] && [ "$use_shutd" = "1" ]); then
            if [ -f "$shutd_smb" ]; then
                rm "$shutd_smb"
                touch "$shutd_smb"
            else
                touch "$shutd_smb"
            fi
            
            (
            $printf_cmd "#!/system/bin/sh\n"
            $printf_cmd "\n"
            $printf_cmd "# Make sure SMB shares are umount'ed before shutdown\n"
            ) >> "$shutd_smb"
            
            let i=0
            while (( ${#to_create[@]} > i )); do
                $printf_cmd "busybox umount -f -l ${to_create[i++]}" >> "$shutd_smb"
            done
            chmod 0755 "$shutd_smb"
        fi
        
        # Execute the new init.d
        $printf_cmd "Executing init.d file....."
        "$file_initd"
        $printf_cmd "done\n"
        
        $printf_cmd "Script finished. Your mount points should be active\n"
        A note on SMB/CIFS:
        These filesystems can behave very badly on client shutdown when there are still open mount points. It is strongly reccomended to umount SMB share before shutting down the client otherwise errors might pop up that are greatly delaying the shutdown process or even data may be lost.

        I have added an automatic creation of an umount script. You can activate it by setting use_shutd="0" to use_shutd="1".
        For this to work in Android some further modifications are needed. Check this thread on XDA: http://forum.xda-developers.com/show....php?t=1387692
        One problem is that this seems not to work under Android 4.4 since some modifications would be needed. I'm not much of an Android expert so I leave this exercise to the more professional.

        Comment


          #5
          Originally posted by SoulStyle View Post
          Please find below a version of the script that also offers to create SMB / CIFS shares
          Code:
          #!/system/bin/sh
          #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### 
          # Developer Notes:
          #         Needs nmap: http://ftp.linux.hr/android/nmap/nmap-5.61TEST4-android-arm-bin.tar.bz2
          #  
          # License: 
          #        GNU Affero General Public License, version 3.
          #        See <http://www.gnu.org/licenses/>
          #
          # Version history:
          #         2014-05-22  first version by SoulStyle
          #        2014-06-03    Added SMB mounts, improved if up handling, 
          #                   added umount script
          #
          #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### ####
           
          #Default Values nfs
          nfs_options="rw,nosuid,nodev,nolock,rsize=32768,wsize=32768,intr,noatime"
          initd_nfs="/etc/init.d/97NFS"
          
          #Default Values for SMB/CIFS
          smb_options="iocharset=utf8,noserverino,nounix,file_mode=0777,dir_mode=0777"
          smb_port="445"
          initd_smb="/etc/init.d/98SMB"
          use_shutd="0"
          shutd_smb="/etc/shut.d/umount_SMB"
          
          # Paths and busybox commands
          awk_cmd="busybox awk"
          stat_cmd="busybox stat"
          printf_cmd="busybox printf"
          nmap_bin="/sbin/nmap"
          nmap_data="/sdcard/Download/nmap-5.61TEST4/share/nmap/"
          nmap_dns="--dns-servers 8.8.8.8"
          mount_path="/mnt/"
          
          $printf_cmd "Choose type of shares to mount:\n"
          select yn in "NFS" "SMB/CIFS"; do
          case $yn in
              NFS ) 
                  share_type="NFS";
                  file_initd=$initd_nfs;
                  break;;
              SMB/CIFS ) 
                  share_type="SMB";
                  file_initd=$initd_smb;
                  break;;
          esac
          done
          
          # Query user for share type
          $printf_cmd "Please enter your $share_type server IP: \c"
          read server_ip
          $printf_cmd "\n####################\n\n"
          
          # Query user for base path of the mount points
          $printf_cmd "Please enter your base path for mounting the shares.\n"
          $printf_cmd "If you are unsure leave the default value given.\n"
          $printf_cmd "Note that the path has to end with /!\n"
          $printf_cmd "Base path for mounting the nfs shares [$mount_path]: \c"
          read mount_path_new
          [ -n "$mount_path_new" ] && mount_path=$mount_path_new
          $printf_cmd "\n####################\n\n"
          
          if [[ $share_type = "NFS" ]]; then
              #NFS
              nfs_available=($($nmap_bin --script=nfs-showmount.nse $server_ip $nmap_dns --datadir $nmap_data | grep -e "|   " -e "|_  " | $awk_cmd '{print $2;}'))
              # List all found nfs shares
              let i=0
              $printf_cmd "The server $server_ip exposes following shares:\n"
              while (( ${#nfs_available[@]} > i )); do
                  $printf_cmd "$i) ${nfs_available[i++]}\n"
              done
              $printf_cmd "\n####################\n\n"
              
              #Collect all shares and corresponding folders in arrays
              let i=0
              while (( ${#nfs_available[@]} > i )); do
                  $printf_cmd "Do you want to mount the share "${nfs_available[i]}"?\n"
                  select yn in "Yes" "No"; do
                  case $yn in
                      Yes ) 
                          $printf_cmd "Please enter the name of the local folder to mount this share: \c";
                          read local_folder;
                          if [ -d "$mount_path$local_folder" ]; then
                              folder_type=$($stat_cmd -f $mount_path$local_folder | grep -e "Type" | $awk_cmd '{print $6;}')
                              $printf_cmd "The path $mount_path$local_folder already exists\n"
                              $printf_cmd "Folder type of $mount_path$local_folder is: $folder_type\n"
                              $printf_cmd "Do you really want to use this folder?\n"
                              select yn in "Yes" "No"; do
                              case $yn in
                                  Yes )
                                      to_mount+=("busybox mount -o $nfs_options $server_ip:${nfs_available[i]} $mount_path$local_folder\n");
                                      break;;
                                  No )
                                      $printf_cmd "Please enter a new name of the local folder to mount this share: \c";
                                      read local_folder;
                                      to_create+=("$mount_path$local_folder\n");
                                      to_mount+=("busybox mount -o $nfs_options $server_ip:${nfs_available[i]} $mount_path$local_folder\n");
                                      break;;
                              esac
                              done
                          else
                              to_create+=("$mount_path$local_folder\n");
                              to_mount+=("busybox mount -o $nfs_options $server_ip:${nfs_available[i]} $mount_path$local_folder\n");
                          fi
                          break;;
                      No ) break;;
                  esac
                  done
              let i++
              done
          elif [[ $share_type = "SMB" ]]; then
              #SMB
              # Query user for smb user
              $printf_cmd "Please enter your SMB user: \c"
              read smb_user
              # Query user for smb password
              $printf_cmd "Please enter your SMB password: \c"
              read smb_pass
              $printf_cmd "\n####################\n\n"
              
              oldGLOBIGNORE=$GLOBIGNORE
              oldIFS=$IFS
              nmap_cmd="$nmap_bin -T4 --script smb-enum-shares --script-args smbuser=$smb_user,smbpass=$smb_pass -p$smb_port $server_ip $nmap_dns --datadir $nmap_data"
              IFS=$'\r\n' GLOBIGNORE='*' :; nmap_smb=($(eval $nmap_cmd))
              IFS=$oldIFS
              GLOBIGNORE=$oldGLOBIGNORE
              
              smb_access=($($printf_cmd -- '%s\n' "${nmap_smb[@]}" | grep -e "\|     Current user" -e "\|_    Current user" | $awk_cmd '{print $6}'))
              smb_shares=($($printf_cmd -- '%s\n' "${nmap_smb[@]}" | grep -e "\|   [^[:space:]].*" | $awk_cmd '{print $2}'))
          
              let i=0
              $printf_cmd "The server $server_ip exposes following SMB shares:\n\n"
              $printf_cmd "%-20s %-30s\n" "Share name:" "Access Rights for $smb_user"
              while (( ${#smb_shares[@]} > i )); do
                  $printf_cmd "%-20s %-30s\n" "${smb_shares[i]}" "${smb_access[i++]}"
              done
              $printf_cmd "\n####################\n\n"
              
              #Collect all shares and corresponding folders in arrays
              let i=0
              while (( ${#smb_shares[@]} > i )); do
                  $printf_cmd "Do you want to mount the share "${smb_shares[i]}"?\n"
                  select yn in "Yes" "No"; do
                  case $yn in
                      Yes ) 
                          $printf_cmd "Please enter the name of the local folder to mount this share: \c";
                          read local_folder;
                          if [ -d "$mount_path$local_folder" ]; then
                              folder_type=$($stat_cmd -f $mount_path$local_folder | grep -e "Type" | $awk_cmd '{print $6;}')
                              $printf_cmd "The path $mount_path$local_folder already exists\n"
                              $printf_cmd "Folder type of $mount_path$local_folder is: $folder_type\n"
                              $printf_cmd "Do you really want to use this folder?\n"
                              select yn in "Yes" "No"; do
                              case $yn in
                                  Yes )
                                      to_mount+=("busybox mount -t cifs -o username=$smb_user,password=$smb_pass,$smb_options //$server_ip/"${smb_shares[i]}" $mount_path$local_folder\n");
                                      break;;
                                  No )
                                      $printf_cmd "Please enter a new name of the local folder to mount this share: \c";
                                      read local_folder;
                                      to_create+=("$mount_path$local_folder\n");
                                      to_mount+=("busybox mount -t cifs -o username=$smb_user,password=$smb_pass,$smb_options //$server_ip/"${smb_shares[i]}" $mount_path$local_folder\n");
                                      break;;
                              esac
                              done
                          else
                              to_create+=("$mount_path$local_folder\n");
                              to_mount+=("busybox mount -t cifs -o username=$smb_user,password=$smb_pass,$smb_options //$server_ip/"${smb_shares[i]}" $mount_path$local_folder\n");
                          fi
                          break;;
                      No ) break;;
                  esac
                  done
              let i++
              done
          fi
          
          $printf_cmd "Creating init.d file....."
          
          # Create the init.d file
          if [ -f "$file_initd" ]; then
            rm "$file_initd"
            touch "$file_initd"
          else
            touch "$file_initd"
          fi
          
          (
          $printf_cmd "#!/system/bin/sh\n"
          $printf_cmd "\n"
          $printf_cmd "# Make sure networking is up before mounting\n"
          $printf_cmd "while :\n"
          $printf_cmd "do\n"
          $printf_cmd "   check_if_up=(\$(netcfg | grep -e "eth0" -e "wlan0" | busybox awk '{print \$2}'))\n"
          $printf_cmd "   if [[ ("\${check_if_up[0]}" = "UP") || ("\${check_if_up[1]}" = "UP") ]]; then\n"
          $printf_cmd "    break\n"
          $printf_cmd "   fi\n" 
          $printf_cmd "   sleep 1\n"
          $printf_cmd "done\n"
          $printf_cmd "sleep 1\n"
          $printf_cmd "\n"
          $printf_cmd "# Switch rootfs to read / write\n"
          $printf_cmd "# Be careful after this. Misplaced commands may\n"
          $printf_cmd "# brick your system\n"
          $printf_cmd "busybox mount -o rw,remount /\n"
          $printf_cmd "\n"
          $printf_cmd "# Create Mount Points\n"
          ) >> "$file_initd"
          let i=0
          while (( ${#to_create[@]} > i )); do
              $printf_cmd "mkdir ${to_create[i++]}" >> "$file_initd"
          done
          let i=0
          $printf_cmd '\n' >> "$file_initd"
          $printf_cmd '# Mount $share_type shares\n' >> "$file_initd"
          while (( ${#to_mount[@]} > i )); do
              $printf_cmd "${to_mount[i++]}" >> "$file_initd"
          done
          $printf_cmd "\n# Switch rootfs to read only\n" >> "$file_initd"
          $printf_cmd "busybox mount -o ro,remount /\n" >> "$file_initd"
          
          
          # Make init.d file executeable
          chmod 0755 "$file_initd" 
          
          $printf_cmd "done\n"
          
          # Create umount script upon shutdown for SMB shares
          if ([ "$share_type" = "SMB" ] && [ "$use_shutd" = "1" ]); then
              if [ -f "$shutd_smb" ]; then
                  rm "$shutd_smb"
                  touch "$shutd_smb"
              else
                  touch "$shutd_smb"
              fi
              
              (
              $printf_cmd "#!/system/bin/sh\n"
              $printf_cmd "\n"
              $printf_cmd "# Make sure SMB shares are umount'ed before shutdown\n"
              ) >> "$shutd_smb"
              
              let i=0
              while (( ${#to_create[@]} > i )); do
                  $printf_cmd "busybox umount -f -l ${to_create[i++]}" >> "$shutd_smb"
              done
              chmod 0755 "$shutd_smb"
          fi
          
          # Execute the new init.d
          $printf_cmd "Executing init.d file....."
          "$file_initd"
          $printf_cmd "done\n"
          
          $printf_cmd "Script finished. Your mount points should be active\n"
          A note on SMB/CIFS:
          These filesystems can behave very badly on client shutdown when there are still open mount points. It is strongly reccomended to umount SMB share before shutting down the client otherwise errors might pop up that are greatly delaying the shutdown process or even data may be lost.

          I have added an automatic creation of an umount script. You can activate it by setting use_shutd="0" to use_shutd="1".
          For this to work in Android some further modifications are needed. Check this thread on XDA: http://forum.xda-developers.com/show....php?t=1387692
          One problem is that this seems not to work under Android 4.4 since some modifications would be needed. I'm not much of an Android expert so I leave this exercise to the more professional.
          big thx
          maybe in the future we will have THE script for all the shares ->noobstyle
          would be very nice

          Comment

          Working...
          X