zramraid-install 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. if [ -f "/lib/systemd/system/libvirt-guests.service" ];
  142. then
  143. if [ $(cat /lib/systemd/system/libvirt-guests.service|grep zramraid|wc -m ) = 0];
  144. then
  145. sed -i '/Requires=virt-guest-shutdown.target/a \'"Requires=zramraid.service" /lib/systemd/system/libvirt-guests.service;
  146. fi
  147. fi
  148. fi
  149. reports[${#reports[@]}]="zramraid install susseful";
  150. reports[${#reports[@]}]="please setup config for autostart from boot: /etc/default/zramraid.";
  151. writeToLog;
  152. printInfo;
  153. }
  154. function uninstall() {
  155. function zrDel() {
  156. if [[ $deb_release < 8 ]];
  157. then
  158. /etc/init.d/zramraid-manager stop;
  159. update-rc.d -n zramraid-manager remove;
  160. else
  161. /bin/systemctl stop zramraid-manager;
  162. /bin/systemctl disable zramraid.service;
  163. rm /lib/systemd/system/zramraid.service;
  164. if [ -f "/lib/systemd/system/libvirt-guests.service" ];
  165. then
  166. if [ $(cat /lib/systemd/system/libvirt-guests.service|grep zramraid|wc -m ) = 1];
  167. then
  168. sed -i -e '/'"Requires=zramraid.service"'/d' /lib/systemd/system/libvirt-guests.service;
  169. fi
  170. fi
  171. fi
  172. rm -f /usr/local/bin/zramraid-maker;
  173. rm -f /usr/local/bin/zramraid-config;
  174. rm -f /etc/zramraid/messages.dat;
  175. rm -f /etc/zramraid/zramraid-config;
  176. rm -f /etc/zramraid/zramraid-maker;
  177. rm -f /etc/init.d/zramraid-manager;
  178. rm -f /etc/default/zramraid;
  179. }
  180. if [ ! -f "/etc/zramraid/zramraid.conf" ]
  181. then
  182. zrDel;
  183. rm -rf /etc/zramraid;
  184. else
  185. zrDel;
  186. fi
  187. reports[${#reports[@]}]="zramraid uninstall susseful";
  188. writeToLog;
  189. printInfo;
  190. }
  191. ##--@F step operation
  192. function testId() {
  193. if (( ! $(id|grep "root"|wc -m) ))
  194. then
  195. reports=();
  196. reports[${#reports[@]}]="It is only available with root privileges";
  197. makeErr;
  198. fi
  199. }
  200. ##--@F executor
  201. function executor() {
  202. if [[ ${#execute_func[@]} -eq 0 ]]
  203. then echo "exit";
  204. exit 0;
  205. fi
  206. for ((ex_index=0; ex_index != ${#execute_func[@]}; ex_index++))
  207. do
  208. ## !! debug operation...
  209. ##echo "execution: function ${execute_func[ex_index]}"
  210. ${execute_func[ex_index]};
  211. done
  212. }
  213. ##- Begin zramraid
  214. case "$option" in
  215. ## install zramraid
  216. "--install" | "--install" )
  217. execute_func=( ${operation_install[@]} );
  218. executor;
  219. exit 0
  220. ;;
  221. ## uninstall zramraid
  222. "--uninstall" | "--uninstall" )
  223. execute_func=( ${operation_uninstall[@]} );
  224. executor;
  225. exit 0
  226. ;;
  227. ## help
  228. "--help" | "--help" )
  229. execute_func=( ${operation_help[@]} );
  230. executor;
  231. exit 0
  232. ;;
  233. * )
  234. # selecting defaults.
  235. execute_func=( ${operation_help[@]} );
  236. executor;
  237. exit 1
  238. ;;
  239. esac