luks.sh 4.0 KB

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