zramraid-install 6.5 KB

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