#!/bin/bash ## zramraid-install (Default for Debian & Ubuntu) ## (c) author's idea and realization: Kleemov A. & Koshuba V. ## script author: Koshuba V - stvixfree@gmail.com ## version = 10.03.18 option=$1; rdate=$(date +%c); reports=(); test_module=$(find /lib/modules/$kernel_version -name '*.ko'|grep zram|wc -m); test_jq=$(apt-cache policy jq | grep status); test_mdadm=$(apt-cache policy mdadm | grep status); test_lsb_release=$(apt-cache policy lsb-release | grep status); log="/var/log/syslog"; eval kernel_version="(" $(uname -r|sed 's/\./ /g') ")"; debversion=$(lsb_release -c); ## functions & operations operation_install=( "clear" "testId" "checkDep" "checkMod" "install" "printInfo" ); operation_uninstall=( "clear" "testId" "checkDep" "checkMod" "uninstall" "printInfo" ); operation_help=( "clear" "printInfo" ); execute_func=(); ## -@F logic executor function eXlogic() { lEnd=1; if [[ ${#iFs[@]} -eq 0 ]]||[[ ${#iFs[@]} != ${#logic[@]} ]] then echo "exit"; exit 0; fi local exfunc=(); for ((lg_index=0; lg_index != ${#iFs[@]}; lg_index++)) do ## !! debug operation... #echo "eXlogic = execution: function ${iFs[$lg_index]} : index=$lg_index"; local lg=$(echo $((${iFs[$lg_index]})) ); local exfunc=( ${logic[$lg_index]} ); local runfunc=$(echo ${exfunc[$lg]}| sed 's/\"//g'); $runfunc; if [[ $lEnd == 0 ]] then lg_index=$((${#iFs[@]}-1)); fi done iFs=(); logic=(); value_in=""; } ##--@F write log events function writeToLog() { for ((rpt_index=0; rpt_index != ${#reports[@]}; rpt_index++)) do echo "$rdate zramraid install message: ${reports[$rpt_index]}">>$log; done } function printInfo() { value_in="$option"; iFs=( "$(echo -n $value_in|wc -m) == 0" "$(echo -n $value_in|sed 's/--help//g'|wc -m) == 0" "$(echo -n ${#reports[@]}) == 0" ); logic=( '"" "pIhelp"' '"" "pIhelp"' '"pIdf" "pIhelp"' ); function pIhelp() { lEnd=0; clear echo echo -e "install: zramraid-install --install"; echo -e "uninstall: zramraid-install --uninstall"; echo -e "help: zramraid-install --help"; exit 0; } ## default function last eXlogic.. function pIdf() { for ((rpt_index=0; rpt_index != ${#reports[@]}; rpt_index++)) do echo "${reports[$rpt_index]}"; done exit 0; } eXlogic; } ##--@F make all errors function makeErr() { reports[${#reports[@]}]="Operation not succeded"; printInfo; writeToLog; exit 0; } ##@F-tests function checkMod() { if (( "${kernel_version[0]}" >= "3" )) then if (( ! "${kernel_version[1]}" >= "14" )) then reports[${#reports[@]}]="Sorry Kernel version < 3.14.x"; makeErr; exit 0; fi else reports[${#reports[@]}]="Sorry Kernel version < 3.x"; makeErr; exit 0; fi # test module zram if [ $test_module == 0 ] then reports[${#reports[@]}]="The current kernel does not find zram module"; makeErr; fi } ##--@F Check the program dependency function checkDep() { # This installation check jq if [ ! "$test_jq" ] then ## lang messages if yes then lang else us... reports[${#reports[@]}]="Do not set jq package"; makeErr; fi # This installation check mdadm if [ ! "$test_mdadm" ] then reports[${#reports[@]}]="Do not set mdadm package"; makeErr; fi # This installation check mdadm if [ ! "$test_lsb_release" ] then reports[${#reports[@]}]="Do not set lsb_release package"; makeErr; fi ## check Debian or Ubuntu... ## } function install() { if [ ! -d "/etc/zramraid" ] then mkdir -p /etc/zramraid; fi cp -f $PWD/etc/zramraid/messages.dat /etc/zramraid/messages.dat; cp -f $PWD/etc/zramraid/zramraid-config /etc/zramraid/zramraid-config; cp -f $PWD/etc/zramraid/zramraid-maker /etc/zramraid/zramraid-maker; cp -f $PWD/etc/init.d/zramraid-manager /etc/init.d/zramraid-manager; cp -f $PWD/etc/default/zramraid /etc/default/zramraid; ln -s /etc/zramraid/zramraid-maker /usr/local/bin/zramraid-maker; ln -s /etc/zramraid/zramraid-config /usr/local/bin/zramraid-config; ## 4 deb 8.x end up if [ $(echo $debversion|grep jessie|wc -m) != 0 ] then cp -f $PWD/lib/systemd/system/zramraid-start.service /lib/systemd/system/zramraid-start.service; cp -f $PWD/lib/systemd/system/zramraid-monitor.service /lib/systemd/system/zramraid-monitor.service; /bin/systemctl enable zramraid-start.service; /bin/systemctl enable zramraid-monitor.service; fi if [ $(echo $debversion|grep wheezy|wc -m) != 0 ] then update-rc.d -n zramraid-manager defaults; fi ## update-rc.0 .. reports[${#reports[@]}]="zramraid install susseful"; reports[${#reports[@]}]="please setup config for autostart from boot: /etc/default/zramraid."; writeToLog; printInfo; } function uninstall() { function zrDel() { if [ $(echo $debversion|grep jessie|wc -m) != 0 ] then /bin/systemctl stop zramraid-manager; /bin/systemctl disable zramraid-start.service; /bin/systemctl disable zramraid-monitor.service; rm /lib/systemd/system/zramraid-start.service; rm /lib/systemd/system/zramraid-monitor.service; fi if [ $(echo $debversion|grep wheezy|wc -m) != 0 ] then /etc/init.d/zramraid-manager stop; update-rc.d -n zramraid-manager remove; fi rm -f /usr/local/bin/zramraid-maker; rm -f /usr/local/bin/zramraid-config; rm -f /etc/zramraid/messages.dat; rm -f /etc/zramraid/zramraid-config; rm -f /etc/zramraid/zramraid-maker; rm -f /etc/init.d/zramraid-manager; rm -f /etc/default/zramraid; } if [ ! -f "/etc/zramraid/zramraid.conf" ] then zrDel; rm -rf /etc/zramraid; else zrDel; fi reports[${#reports[@]}]="zramraid uninstall susseful"; writeToLog; printInfo; } ##--@F step operation function testId() { if (( ! $(id|grep "root"|wc -m) )) then reports=(); reports[${#reports[@]}]="It is only available with root privileges"; makeErr; fi } ##--@F executor function executor() { if [[ ${#execute_func[@]} -eq 0 ]] then echo "exit"; exit 0; fi for ((ex_index=0; ex_index != ${#execute_func[@]}; ex_index++)) do ## !! debug operation... ##echo "execution: function ${execute_func[ex_index]}" ${execute_func[ex_index]}; done } ##- Begin zramraid case "$option" in ## install zramraid "--install" | "--install" ) execute_func=( ${operation_install[@]} ); executor; exit 0 ;; ## uninstall zramraid "--uninstall" | "--uninstall" ) execute_func=( ${operation_uninstall[@]} ); executor; exit 0 ;; ## help "--help" | "--help" ) execute_func=( ${operation_help[@]} ); executor; exit 0 ;; * ) # selecting defaults. execute_func=( ${operation_help[@]} ); executor; exit 1 ;; esac