zramraid-install 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #!/bin/bash
  2. ## zramraid-install (Default for Debian & Ubuntu)
  3. ## (c) author's idea and realization: Kleemov A. & Koshuba V.
  4. ## script author: Koshuba V - stvixfree@gmail.com
  5. ## version = 26.06.18
  6. option=$1;
  7. rdate=$(date +%c);
  8. reports=();
  9. getpkg=( "jq" "mdadm" "lsb-release" );
  10. test_module=$(find /lib/modules/$kernel_version -name '*.ko'|grep zram|wc -m);
  11. log="/var/log/syslog";
  12. eval kernel_version="(" $(uname -r|sed 's/\./ /g') ")";
  13. deb_release=""
  14. ## functions & operations
  15. operation_install=( "clear" "testId" "checkDep" "checkMod" "install" "printInfo" );
  16. operation_uninstall=( "clear" "testId" "checkDep" "checkMod" "uninstall" "printInfo" );
  17. operation_help=( "clear" "printInfo" );
  18. execute_func=();
  19. ## -@F logic executor
  20. function eXlogic() {
  21. lEnd=1;
  22. if [[ ${#iFs[@]} -eq 0 ]]||[[ ${#iFs[@]} != ${#logic[@]} ]]
  23. then echo "exit";
  24. exit 0;
  25. fi
  26. local exfunc=();
  27. for ((lg_index=0; lg_index != ${#iFs[@]}; lg_index++))
  28. do
  29. ## !! debug operation...
  30. #echo "eXlogic = execution: function ${iFs[$lg_index]} : index=$lg_index";
  31. local lg=$(echo $((${iFs[$lg_index]})) );
  32. local exfunc=( ${logic[$lg_index]} );
  33. local runfunc=$(echo ${exfunc[$lg]}| sed 's/\"//g');
  34. $runfunc;
  35. if [[ $lEnd == 0 ]]
  36. then lg_index=$((${#iFs[@]}-1));
  37. fi
  38. done
  39. iFs=();
  40. logic=();
  41. value_in="";
  42. }
  43. ##--@F write log events
  44. function writeToLog() {
  45. for ((rpt_index=0; rpt_index != ${#reports[@]}; rpt_index++))
  46. do
  47. echo "$rdate zramraid install message: ${reports[$rpt_index]}">>$log;
  48. done
  49. }
  50. function printInfo() {
  51. value_in="$option";
  52. iFs=( "$(echo -n $value_in|wc -m) == 0"
  53. "$(echo -n $value_in|sed 's/--help//g'|wc -m) == 0"
  54. "$(echo -n ${#reports[@]}) == 0" );
  55. logic=( '"" "pIhelp"'
  56. '"" "pIhelp"'
  57. '"pIdf" "pIhelp"' );
  58. function pIhelp() {
  59. lEnd=0;
  60. clear
  61. echo
  62. echo -e "install: zramraid-install --install";
  63. echo -e "uninstall: zramraid-install --uninstall";
  64. echo -e "help: zramraid-install --help";
  65. exit 0;
  66. }
  67. ## default function last eXlogic..
  68. function pIdf() {
  69. for ((rpt_index=0; rpt_index != ${#reports[@]}; rpt_index++))
  70. do
  71. echo "${reports[$rpt_index]}";
  72. done
  73. exit 0;
  74. }
  75. eXlogic;
  76. }
  77. ##--@F make all errors
  78. function makeErr() {
  79. reports[${#reports[@]}]="Operation not succeded";
  80. printInfo;
  81. writeToLog;
  82. exit 0;
  83. }
  84. ##@F-tests
  85. function checkMod() {
  86. if (( "${kernel_version[0]}" >= "3" ))
  87. then
  88. if (( ! "${kernel_version[1]}" >= "14" ))
  89. then
  90. reports[${#reports[@]}]="Sorry Kernel version < 3.14.x";
  91. makeErr;
  92. exit 0;
  93. fi
  94. else
  95. reports[${#reports[@]}]="Sorry Kernel version < 3.x";
  96. makeErr;
  97. exit 0;
  98. fi
  99. # test module zram
  100. if [ $test_module == 0 ]
  101. then
  102. reports[${#reports[@]}]="The current kernel does not find zram module";
  103. makeErr;
  104. fi
  105. }
  106. ##--@F Check the program dependency
  107. function checkDep() {
  108. if [[ ! $(apt-cache policy ${getpkg[@]} | grep status|wc -l) = $(echo -e ${#getpkg[@]}) ]];
  109. then apt-get install $(echo ${getpkg[@]});
  110. fi
  111. for ((ipkg=0; ipkg != ${#getpkg[@]}; ipkg++))
  112. do
  113. if [ ! "$(apt-cache policy ${getpkg[$ipkg]} | grep status)" ]
  114. then
  115. ## lang messages if yes then lang else us...
  116. reports[${#reports[@]}]="Do not set ${getpkg[$ipkg]} package";
  117. makeErr;
  118. fi
  119. done
  120. local lsbtools=$(whereis -b lsb_release|awk '/^lsb_release:/{print $2}');
  121. deb_release=$( $lsbtools -r|awk '/^Release:/{print $2}'|sed 's/\./ /g'|awk '{print$1}' );
  122. }
  123. function install() {
  124. if [ ! -d "/etc/zramraid" ]
  125. then
  126. mkdir -p /etc/zramraid;
  127. fi
  128. cp -f $PWD/etc/zramraid/messages.dat /etc/zramraid/messages.dat;
  129. cp -f $PWD/etc/zramraid/zramraid-config /etc/zramraid/zramraid-config;
  130. cp -f $PWD/etc/zramraid/zramraid-maker /etc/zramraid/zramraid-maker;
  131. cp -f $PWD/etc/init.d/zramraid-manager /etc/init.d/zramraid-manager;
  132. cp -f $PWD/etc/default/zramraid /etc/default/zramraid;
  133. ln -s /etc/zramraid/zramraid-maker /usr/local/bin/zramraid-maker;
  134. ln -s /etc/zramraid/zramraid-config /usr/local/bin/zramraid-config;
  135. if [[ $deb_release < 8 ]];
  136. then
  137. update-rc.d -n zramraid-manager defaults;
  138. else
  139. cp -f $PWD/lib/systemd/system/zramraid.service /lib/systemd/system/zramraid.service;
  140. /bin/systemctl enable zramraid.service;
  141. fi
  142. reports[${#reports[@]}]="zramraid install susseful";
  143. reports[${#reports[@]}]="please setup config for autostart from boot: /etc/default/zramraid.";
  144. writeToLog;
  145. printInfo;
  146. }
  147. function uninstall() {
  148. function zrDel() {
  149. if [[ $deb_release < 8 ]];
  150. then
  151. /etc/init.d/zramraid-manager stop;
  152. update-rc.d -n zramraid-manager remove;
  153. else
  154. /bin/systemctl stop zramraid-manager;
  155. /bin/systemctl disable zramraid.service;
  156. rm /lib/systemd/system/zramraid.service;
  157. fi
  158. rm -f /usr/local/bin/zramraid-maker;
  159. rm -f /usr/local/bin/zramraid-config;
  160. rm -f /etc/zramraid/messages.dat;
  161. rm -f /etc/zramraid/zramraid-config;
  162. rm -f /etc/zramraid/zramraid-maker;
  163. rm -f /etc/init.d/zramraid-manager;
  164. rm -f /etc/default/zramraid;
  165. }
  166. if [ ! -f "/etc/zramraid/zramraid.conf" ]
  167. then
  168. zrDel;
  169. rm -rf /etc/zramraid;
  170. else
  171. zrDel;
  172. fi
  173. reports[${#reports[@]}]="zramraid uninstall susseful";
  174. writeToLog;
  175. printInfo;
  176. }
  177. ##--@F step operation
  178. function testId() {
  179. if (( ! $(id|grep "root"|wc -m) ))
  180. then
  181. reports=();
  182. reports[${#reports[@]}]="It is only available with root privileges";
  183. makeErr;
  184. fi
  185. }
  186. ##--@F executor
  187. function executor() {
  188. if [[ ${#execute_func[@]} -eq 0 ]]
  189. then echo "exit";
  190. exit 0;
  191. fi
  192. for ((ex_index=0; ex_index != ${#execute_func[@]}; ex_index++))
  193. do
  194. ## !! debug operation...
  195. ##echo "execution: function ${execute_func[ex_index]}"
  196. ${execute_func[ex_index]};
  197. done
  198. }
  199. ##- Begin zramraid
  200. case "$option" in
  201. ## install zramraid
  202. "--install" | "--install" )
  203. execute_func=( ${operation_install[@]} );
  204. executor;
  205. exit 0
  206. ;;
  207. ## uninstall zramraid
  208. "--uninstall" | "--uninstall" )
  209. execute_func=( ${operation_uninstall[@]} );
  210. executor;
  211. exit 0
  212. ;;
  213. ## help
  214. "--help" | "--help" )
  215. execute_func=( ${operation_help[@]} );
  216. executor;
  217. exit 0
  218. ;;
  219. * )
  220. # selecting defaults.
  221. execute_func=( ${operation_help[@]} );
  222. executor;
  223. exit 1
  224. ;;
  225. esac