automountntfs.sh 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env bash
  2. # automountntfs.sh
  3. # Mounts all the drives inside a NTFS formatted device automatically.
  4. echo -e "\nWARNING! This script is experimental, so it may not work as expected. Only tested on FreeBSD 13.\n"
  5. if [ -z "$(which ntfs-3g)" ]; then
  6. echo 'ntfs-3g is not found. Please install ntfs-3g and continue.'; exit 6725
  7. fi
  8. if [[ "$1" = /dev/* ]]; then
  9. DEVICE="$1"
  10. DEVICE_ID=$(echo "$1" | sed -r 's|\/dev\/(.*)|\1|')
  11. else
  12. DEVICE_ID="$1"
  13. DEVICE="/dev/${DEVICE_ID}"
  14. fi
  15. if [ -z "$1" ]; then
  16. echo -e "Empty device is not allowed.\nPlease provide a device to mount drives from, such as:\n $0 /dev/ada0"; exit 9482
  17. fi
  18. if [ ! -e "${DEVICE}" ]; then
  19. echo "Device ${DEVICE} doesn't exist"; exit 2529
  20. fi
  21. ls ${DEVICE}s* | while read line
  22. do
  23. echo "Trying to mount $line..."
  24. part_label=$(ntfslabel "${line}")
  25. if [ -z "${part_label}" ]; then # no label
  26. mount_dir=$(echo "${line}" | sed -r 's|\/dev\/(.*)|\1|')
  27. else # has label
  28. mount_dir="${part_label}"
  29. fi
  30. # Check to see if already mounted. This is to avoid multiple duplicate devices showing up.
  31. # This happens when this script is run multiple times.
  32. if [[ ! -z "$(mount | grep "/media/${DEVICE_ID}/${mount_dir} ")" ]]; then
  33. echo "Device ${line} already mounted. Assuming duplicate entry and ignoring..."
  34. else
  35. # Mounting it on /media, instead of /mnt shows it on file manager sidebar.
  36. sudo mkdir -p "/media/${DEVICE_ID}/${mount_dir}"
  37. sudo ntfs-3g "${line}" "/media/${DEVICE_ID}/${mount_dir}"
  38. fi
  39. done