freedombone-utils-filesystem 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Filesystem encryption, etc
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. TOMB_REPO="https://github.com/dyne/Tomb"
  31. TOMB_COMMIT='c80ebd6d6ed77980eb5b559757e03ea13a29bdd1'
  32. function mesh_install_tomb {
  33. # shellcheck disable=SC2154
  34. chroot "${rootdir}" apt-get -yq install cryptsetup zsh pinentry-curses
  35. if [ ! -d "${rootdir}/$INSTALL_DIR" ]; then
  36. mkdir -p "${rootdir}/$INSTALL_DIR"
  37. fi
  38. if [ -d /repos/tomb ]; then
  39. mkdir "${rootdir}/$INSTALL_DIR/tomb"
  40. cp -r -p /repos/tomb/. "${rootdir}/$INSTALL_DIR/tomb"
  41. cd "${rootdir}/$INSTALL_DIR/tomb" || exit 3648368
  42. git pull
  43. else
  44. git_clone "$TOMB_REPO" "${rootdir}/$INSTALL_DIR/tomb"
  45. fi
  46. cd "${rootdir}/$INSTALL_DIR/tomb" || exit 7346384
  47. git checkout $TOMB_COMMIT -b $TOMB_COMMIT
  48. chroot "${rootdir}" /bin/bash -x <<EOF
  49. cd $INSTALL_DIR/tomb
  50. make install
  51. EOF
  52. if [ ! -f "${rootdir}/usr/local/bin/tomb" ]; then
  53. exit 93462
  54. fi
  55. }
  56. function install_tomb {
  57. if [ "$INSTALLING_MESH" ]; then
  58. mesh_install_tomb
  59. return
  60. fi
  61. function_check set_repo_commit
  62. set_repo_commit "$INSTALL_DIR/tomb" "tomb commit" "$TOMB_COMMIT" "$TOMB_REPO"
  63. if [ -f "$COMPLETION_FILE" ]; then
  64. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  65. return
  66. fi
  67. fi
  68. apt-get -yq install cryptsetup zsh pinentry-curses
  69. if [ ! -d "$INSTALL_DIR" ]; then
  70. mkdir -p "$INSTALL_DIR"
  71. fi
  72. if [ -d /repos/tomb ]; then
  73. mkdir "$INSTALL_DIR/tomb"
  74. cp -r -p /repos/tomb/. "$INSTALL_DIR/tomb"
  75. cd "$INSTALL_DIR/tomb" || exit 7684638
  76. git pull
  77. else
  78. git_clone "$TOMB_REPO" "$INSTALL_DIR/tomb"
  79. fi
  80. cd "$INSTALL_DIR/tomb" || exit 364863463
  81. git checkout "$TOMB_COMMIT" -b "$TOMB_COMMIT"
  82. set_completion_param "tomb commit" "$TOMB_COMMIT"
  83. make install
  84. if [ ! -f /usr/local/bin/tomb ]; then
  85. exit 93462
  86. fi
  87. mark_completed "${FUNCNAME[0]}"
  88. }
  89. function defrag_filesystem {
  90. fs_type=$(stat -f /)
  91. if [[ "$fs_type" != *"btrfs"* && "$fs_type" != *"ext4"* ]]; then
  92. return
  93. fi
  94. echo $'Defragmenting root directory'
  95. if [[ "$fs_type" == *"btrfs"* ]]; then
  96. btrfs filesystem defragment -r -clzo /
  97. fi
  98. if [[ "$fs_type" == *"ext4"* ]]; then
  99. e4defrag /
  100. fi
  101. echo $'Defragmentation completed'
  102. }
  103. function optimise_filesystem {
  104. if grep -q "btrfs" /etc/fstab; then
  105. if ! grep -q "btrfs defaults,subvol=@,compress=lzo,ssd" /etc/fstab; then
  106. sed -i 's|btrfs subvol=@|btrfs defaults,subvol=@,compress=lzo,ssd|g' /etc/fstab
  107. fi
  108. fi
  109. }
  110. # NOTE: deliberately no exit 0