luks.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/bin/sh
  2. ERR () {
  3. printf '[ERROR] %s' "$1"
  4. test -n "$2" && exit "$2"
  5. }
  6. file_path="$1"
  7. test -n "$file_path" || { echo 'No file given'; exit 1; }
  8. file="$(basename "$file_path")"
  9. name="$(echo "$file" | cut -d. -f1)"
  10. _name="__${name}__"
  11. ext="$(echo "$file" | cut -d. -f2)"
  12. block_dev="/dev/mapper/${_name}"
  13. cur_uid="$(id -u)"
  14. cur_gid="$(id -g)"
  15. sudo_prog='sudo --prompt=Sudo_Password:'
  16. test "$ext" = 'luks' || ERR "File $file_path does not have extencion .luks." 1
  17. if test -e "$file_path"
  18. then
  19. test -f "$file_path" || ERR "${file_path} Is not a regular file" 1
  20. else
  21. echo -n "File ${file_path} does not exist. Want to create? (Y/n) "
  22. read choose
  23. case "$choose" in
  24. N|n) exit 1 ;;
  25. esac
  26. echo -n "What size? (10G) "
  27. read _size
  28. if test "$choose" = ''
  29. then
  30. _size='10G'
  31. fi
  32. truncate --size="$_size" "$file_path" || ERR "Failed to allocate file: ${file_path}" 1
  33. cryptsetup luksFormat -c aes-xts-plain64 -s 512 -y "$file_path" || ERR "Failed to format file: ${file_path}" 1
  34. echo "Successifuly create encrypted file: ${file_path}"
  35. fi
  36. if test -e ./"$name"
  37. then
  38. test -d "./${name}" || ERR "File ${name} already exist in current directory. And is not a directory." 1
  39. if test -b "$block_dev"
  40. then
  41. echo -n "File ${file_path} Already open. Want to close? (Y/n) "
  42. read choose
  43. case "$choose" in
  44. N|n) exit 0 ;;
  45. esac
  46. echo "Unmounting ./${name}"
  47. $sudo_prog umount ./"$name" || ERR "Failed to unmount ./${name}." 1
  48. echo "Closing luks block dev ${block_dev}"
  49. $sudo_prog cryptsetup close "$block_dev" || ERR "Failed to close luks file: ${file_path}." 1
  50. echo "Succesfully Closed ${file_path}."
  51. echo -n "Delete directory ./${name}? (Y/n) "
  52. read choose
  53. case "$choose" in
  54. N|n) exit 0 ;;
  55. esac
  56. rmdir ./$name || ERR "Failed to remove directory: ./${name}" 1
  57. exit 0
  58. fi
  59. else
  60. mkdir ./"$name"
  61. fi
  62. echo "Opening luks file: ${file_path} as ${_name}."
  63. $sudo_prog cryptsetup luksOpen "$file_path" "$_name" || ERR "Failed to open luks file: ${file_path}." 1
  64. echo "Mounting ${block_dev} into ./${name}"
  65. $sudo_prog mount "$block_dev" ./"$name" || ERR "Failed to mound ${block_dev} on ./${name}." 1
  66. dir_perm="$(stat -c '%u:%g' "$file_path")"
  67. cur_perm="${cur_uid}:${cur_gid}"
  68. if ! test "$dir_perm" = "$cur_perm"
  69. then
  70. (
  71. echo -n "Directory ./${name} not owned by current user. Want to change direcory owner? (Y/n) "
  72. read choose
  73. case "$choose" in
  74. N|n) exit 0 ;;
  75. esac
  76. echo "Changing ownership of ./${name} from ${dir_perm} to ${cur_perm}."
  77. $sudo_prog chown "$cur_perm" ./"$name" || ERR "Failed to change permission of directory ./${name} from ${dir_perm} to ${cur_perm}." 1
  78. ) || exit 1
  79. fi
  80. echo "Opened ${file_path} on ./${name}"
  81. exit 0