diff options
author | szaimen <szaimen@e.mail.de> | 2020-05-09 09:10:54 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-09 09:10:54 +0300 |
commit | 981e6c2c6cb95e9a9d60e8558ab218bd9df0f240 (patch) | |
tree | 3bb2abd8b101d47175c56f8415ae7587ae58b54f /apps/smbmount.sh | |
parent | a71ef7206002664203bdce534af245df9d3ff44b (diff) |
add change_mount option (#1231)
Diffstat (limited to 'apps/smbmount.sh')
-rw-r--r-- | apps/smbmount.sh | 187 |
1 files changed, 175 insertions, 12 deletions
diff --git a/apps/smbmount.sh b/apps/smbmount.sh index ec9f5741..d0245424 100644 --- a/apps/smbmount.sh +++ b/apps/smbmount.sh @@ -19,7 +19,7 @@ root_check # Variables MAX_COUNT=16 SMBSHARES="/mnt/smbshares" -SMBSHARES_SED="\/mnt\/smbshares" +SMBSHARES_SED=${SMBSHARES//\//\\/} # Check MAX_COUNT if ! [ $MAX_COUNT -gt 0 ] @@ -50,6 +50,7 @@ fi # Functions add_mount() { + # Check if mounting slots are available count=1 while [ $count -le $MAX_COUNT ] @@ -66,6 +67,7 @@ then msg_box "All $MAX_COUNT slots are occupied. No mounting slots available. Please delete one of the SMB-mounts.\nIf you really want to mount more, you can simply download the smb-mount script directly and edit the variable 'MAX_COUNT' to a higher value than $MAX_COUNT by running:\n'curl -sLO https://raw.githubusercontent.com/nextcloud/vm/master/apps/smbmount.sh /var/scripts'\n'sudo nano /var/scripts/smbmount.sh' # Edit MAX_COUNT=$MAX_COUNT to your likings and save the file\n'sudo bash /var/scripts/smbmount.sh' # Execute the script." return fi + # Enter SMB-server and Share-name while true do @@ -78,6 +80,7 @@ do break fi done + # Enter the SMB-user while true do @@ -89,10 +92,11 @@ do break fi done + # Enter the password of the SMB-user while true do - SMB_PASSWORD=$(whiptail --inputbox "Please enter the password of the SMB-user $SMB_USER" "$WT_HEIGHT" "$WT_WIDTH" 3>&1 1>&2 2>&3) + SMB_PASSWORD=$(whiptail --inputbox "Please enter the password of the SMB-user $SMB_USER.\nPlease note, that comma as a character in the password is not supported." "$WT_HEIGHT" "$WT_WIDTH" 3>&1 1>&2 2>&3) if [[ "no" == $(ask_yes_or_no "Is this correct? $SMB_PASSWORD") ]] then msg_box "It seems like your weren't satisfied by the password for the SMB-user you entered. Please try again." @@ -100,6 +104,7 @@ do break fi done + # Write everything to /etc/fstab, mount and connect external storage count=1 while [ $count -le $MAX_COUNT ] @@ -111,6 +116,7 @@ do echo "$SERVER_SHARE_NAME $SMBSHARES/$count cifs username=$SMB_USER,password=$SMB_PASSWORD,vers=3.0,uid=www-data,gid=www-data,file_mode=0770,dir_mode=0770,nounix,noserverino 0 0" >> /etc/fstab mkdir -p "$SMBSHARES/$count" mount "$SMBSHARES/$count" + # Check if mounting was successful if ! mountpoint -q $SMBSHARES/$count then @@ -124,11 +130,13 @@ do then install_and_enable_app files_external fi + # Create and mount external storage to the admin group - MOUNT_ID=$(occ_command files_external:create "SMB$count" local null::null -c datadir="$SMBSHARES/$count" ) + MOUNT_ID=$(occ_command_no_check files_external:create "SMB$count" local null::null -c datadir="$SMBSHARES/$count" ) MOUNT_ID=${MOUNT_ID//[!0-9]/} - occ_command files_external:applicable --add-group=admin "$MOUNT_ID" -q - occ_command files_external:option "$MOUNT_ID" filesystem_check_changes 1 + occ_command_no_check files_external:applicable --add-group=admin "$MOUNT_ID" -q + occ_command_no_check files_external:option "$MOUNT_ID" filesystem_check_changes 1 + # Inform the user that mounting was successful msg_box "Your mount was successful, congratulations!\nIt's now accessible in your root directory under $SMBSHARES/$count.\nYou are now using the Nextcloud external storage app to access files there. The Share has been mounted to the Nextcloud admin-group.\nYou can now access 'https://yourdomain-or-ipaddress/settings/admin/externalstorages' to rename 'SMB$count' to whatever you like or e.g. enable sharing." break @@ -140,6 +148,7 @@ return } mount_shares() { + # Check if any SMB-share is created if ! grep -q "$SMBSHARES" /etc/fstab then @@ -166,8 +175,10 @@ then msg_box "No existing SMB-mount-entry is unmounted. So nothing to mount." return fi + args=(whiptail --title "Mount SMB-shares" --checklist "This option let you mount SMB-shares to connect to network-shares from the host-computer or other machines in the local network.\nChoose which one you want to mount.\nSelect or unselect by pressing the spacebar" "$WT_HEIGHT" "$WT_WIDTH" 4) count=1 + # Find out which SMB-shares are available while [ $count -le $MAX_COUNT ] do @@ -177,9 +188,11 @@ do fi count=$((count+1)) done + # Let the user choose which SMB-shares he wants to mount selected_options=$("${args[@]}" 3>&1 1>&2 2>&3) count=1 + # Mount selected SMB-shares while [ $count -le $MAX_COUNT ] do @@ -199,14 +212,16 @@ return } show_all_mounts() { + # If no entry created, nothing to show if ! grep -q "$SMBSHARES" /etc/fstab then msg_box "You haven't created any SMB-mount. So nothing to show." return fi + # Find out which SMB-shares are available -args=(whiptail --title "List SMB-shares" --checklist "This option let you show detailed information about your SMB-shares.\nChoose which one you want to show.\nSelect or unselect by pressing the spacebar" "$WT_HEIGHT" "$WT_WIDTH" 4) +args=(whiptail --title "List SMB-shares" --checklist "This option let you show detailed information about your SMB-shares.\nChoose which one you want to change.\nSelect or unselect by pressing the spacebar" "$WT_HEIGHT" "$WT_WIDTH" 4) count=1 while [ $count -le $MAX_COUNT ] do @@ -216,8 +231,10 @@ do fi count=$((count+1)) done + # Let the user choose which details he wants to see selected_options=$("${args[@]}" 3>&1 1>&2 2>&3) + # Show selected Shares count=1 while [ $count -le $MAX_COUNT ] @@ -231,7 +248,145 @@ done return } +change_mount() { + +# If no entry created, nothing to show +if ! grep -q "$SMBSHARES" /etc/fstab +then + msg_box "You haven't created any SMB-mount. So nothing to change." + return +fi + +# Find out which SMB-shares are available +args=(whiptail --title "Change a SMB-mount" --radiolist "This option let you change the password, the username and/or the network-share of one of your SMB-mounts.\nChoose which one you want to show.\nSelect one with the [ARROW] keys and select with the [SPACE] key. Confirm by pressing [ENTER]" "$WT_HEIGHT" "$WT_WIDTH" 4) +count=1 +while [ $count -le $MAX_COUNT ] +do + if grep -q "$SMBSHARES/$count " /etc/fstab + then + args+=("$SMBSHARES/$count " "$(grep "$SMBSHARES/$count " /etc/fstab | awk '{print $1}')" OFF) + fi + count=$((count+1)) +done + +# Let the user choose which mount he wants to change +selected_option=$("${args[@]}" 3>&1 1>&2 2>&3) + +if [[ "$selected_option" == "" ]] +then + return +fi + +# Store fstab entry for later in a variable +fstab_entry=$(grep "$selected_option" /etc/fstab) +SERVER_SHARE_NAME=$(echo "$fstab_entry" | awk '{print $1}') +SMB_USER=${fstab_entry##*username=} +SMB_USER=${SMB_USER%%,*} +SMB_PASSWORD=${fstab_entry##*password=} +SMB_PASSWORD=${SMB_PASSWORD%%,*} + + +# Let the user choose which entries he wants to change +choice=$(whiptail --title "Change a SMB-mount" --checklist "$fstab_entry\n\nChoose which option you want to change.\nSelect or unselect by pressing the spacebar" "$WT_HEIGHT" "$WT_WIDTH" 4 \ +"Password" "(change the password of the SMB-user)" OFF \ +"Username" "(change the username of the SMB-user)" OFF \ +"Share" "(change the SMB-share to use the same mount directory)" OFF 3>&1 1>&2 2>&3) + +case "$choice" in + *"Share"*) + clear + # Enter SMB-server and Share-name + while true + do + SERVER_SHARE_NAME=$(whiptail --inputbox "Please enter the server and Share-name like this:\n//Server/Share\nor\n//IP-address/Share" "$WT_HEIGHT" "$WT_WIDTH" 3>&1 1>&2 2>&3) + if [[ "no" == $(ask_yes_or_no "Is this correct? $SERVER_SHARE_NAME") ]] + then + msg_box "It seems like your weren't satisfied by the PATH you entered. Please try again." + else + SERVER_SHARE_NAME=${SERVER_SHARE_NAME// /\\040} + break + fi + done + ;;& + *"Username"*) + clear + # Enter the SMB-user + while true + do + SMB_USER=$(whiptail --inputbox "Please enter the username of the SMB-user" "$WT_HEIGHT" "$WT_WIDTH" 3>&1 1>&2 2>&3) + if [[ "no" == $(ask_yes_or_no "Is this correct? $SMB_USER") ]] + then + msg_box "It seems like your weren't satisfied by the SMB-user you entered. Please try again." + else + break + fi + done + ;;& + *"Password"*) + clear + # Enter the password of the SMB-user + while true + do + SMB_PASSWORD=$(whiptail --inputbox "Please enter the password of the SMB-user $SMB_USER.\nPlease note, that comma as a character in the password is not supported." "$WT_HEIGHT" "$WT_WIDTH" 3>&1 1>&2 2>&3) + if [[ "no" == $(ask_yes_or_no "Is this correct? $SMB_PASSWORD") ]] + then + msg_box "It seems like your weren't satisfied by the password for the SMB-user you entered. Please try again." + else + break + fi + done + ;;& + "") + return + ;;& + *) + ;; +esac + +# Test if SMB-share is still mounted and unmount if yes +if mountpoint -q "$selected_option" +then + umount "$selected_option" + was_mounted=yes + if mountpoint -q "$selected_option" + then + msg_box "It seems like the unmount of $selected_option wasn't successful while trying to change the mount. Please try again." + return + fi +fi + +# Remove that line from fstab +selected_option_sed=${selected_option//\//\\/} +sed -i "/$selected_option_sed/d" /etc/fstab + +# Write changed line to /etc/fstab and mount +echo "$SERVER_SHARE_NAME $selected_option cifs username=$SMB_USER,password=$SMB_PASSWORD,vers=3.0,uid=www-data,gid=www-data,file_mode=0770,dir_mode=0770,nounix,noserverino 0 0" >> /etc/fstab +mount "$selected_option" + +# Check if mounting was successful +if ! mountpoint -q "$selected_option" +then + # If not remove this line from fstab + msg_box "It seems like the mount of the changed configuration wasn't successful. It will get deleted now. The old config will get restored now. Please try again to change the mount." + sed -i "/$selected_option_sed/d" /etc/fstab + echo "$fstab_entry" >> /etc/fstab + if [[ $was_mounted == yes ]] + then + mount "$selected_option" + if ! mountpoint -q "$selected_option" + then + msg_box "Your old configuration couldn't get mounted but is restored to /etc/fstab." + fi + fi +else + # Inform the user that mounting was successful + msg_box "Your change of the mount was successful, congratulations!" +fi + +} + unmount_shares() { + # Check if any SMB-shares are available for unmounting count=1 while [ $count -le $MAX_COUNT ] @@ -248,6 +403,7 @@ then msg_box "You haven't mounted any SMB-mount. So nothing to unmount" return fi + # Find out which SMB-shares are available args=(whiptail --title "Unmount SMB-shares" --checklist "This option let you unmount SMB-shares to disconnect network-shares from the host-computer or other machines in the local network.\nChoose what you want to do.\nSelect or unselect by pressing the spacebar" "$WT_HEIGHT" "$WT_WIDTH" 4) count=1 @@ -259,6 +415,7 @@ do fi count=$((count+1)) done + # Let the user select which SMB-shares he wants to unmount selected_options=$("${args[@]}" 3>&1 1>&2 2>&3) count=1 @@ -280,12 +437,14 @@ return } delete_mounts() { + # Check if any SMB-share is available if ! grep -q "$SMBSHARES" /etc/fstab then msg_box "You haven't created any SMB-mount, nothing to delete." return fi + # Check which SMB-shares are available args=(whiptail --title "Delete SMB-mounts" --checklist "This option let you delete SMB-shares to disconnect and remove network-shares from the Nextcloud VM.\nChoose what you want to do.\nSelect or unselect by pressing the spacebar" "$WT_HEIGHT" "$WT_WIDTH" 4) count=1 @@ -297,8 +456,10 @@ do fi count=$((count+1)) done + # Let the user choose which SMB-shares he wants to delete selected_options=$("${args[@]}" 3>&1 1>&2 2>&3) + # Delete the selected SMB-shares count=1 while [ $count -le $MAX_COUNT ] @@ -313,13 +474,11 @@ do msg_box "It seems like the unmount of $SMBSHARES/$count wasn't successful during the deletion. Please try again." else sed -i "/$SMBSHARES_SED\/$count /d" /etc/fstab - if grep -q "$SMBSHARES/$count " /etc/fstab - then - msg_box "Something went wrong during deletion of $SMBSHARES/$count. Please try again." - else - msg_box "Your deletion of $SMBSHARES/$count was successful!" - fi + msg_box "Your deletion of $SMBSHARES/$count was successful!" fi + else + sed -i "/$SMBSHARES_SED\/$count /d" /etc/fstab + msg_box "Your deletion of $SMBSHARES/$count was successful!" fi fi count=$((count+1)) @@ -335,6 +494,7 @@ do "Add a SMB-mount" "(and mount/connect it)" ON \ "Mount SMB-shares" "(connect SMB-shares)" OFF \ "Show all SMB-mounts" "(show detailed information about the SMB-mounts)" OFF \ + "Change a SMB-mount" "(change password, username &/or share of a mount)" OFF \ "Unmount SMB-shares" "(disconnect SMB-shares)" OFF \ "Delete SMB-mounts" "(and unmount/disconnect them)" OFF \ "Exit SMB-share" "(exit this script)" OFF 3>&1 1>&2 2>&3) @@ -349,6 +509,9 @@ do "Show all SMB-mounts") show_all_mounts ;; + "Change a SMB-mount") + change_mount + ;; "Unmount SMB-shares") unmount_shares ;; |