luks.sh 3.9 KB

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