luks.sh 3.4 KB

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