zramraid-install 6.5 KB

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