freedombone-utils-filesystem 3.6KB

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