| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 | #!/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 = 03.03.21## License: GPLv3option=$1;rdate=$(date +%c);reports=();getpkg=( "bc" "jq" "mdadm" "lsb-release" );test_module=$(find /lib/modules/$kernel_version -name '*.ko'|grep zram|wc -m);log="/var/log/syslog";eval kernel_version="(" $(uname -r|sed 's/\./ /g') ")";deb_release=""## functions & operationsoperation_install=( "clear" "testId" "checkDep" "checkMod" "install" "printInfo" );operation_uninstall=( "clear" "testId" "checkDep" "checkMod" "uninstall" "printInfo" );operation_help=( "clear" "printInfo" );execute_func=();## -@F logic executorfunction eXlogic() {lEnd=1;if [[ ${#iFs[@]} -eq 0 ]]||[[ ${#iFs[@]} != ${#logic[@]} ]]    then echo "exit";         exit 0;filocal 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));     fidoneiFs=();logic=();value_in="";}##--@F write log eventsfunction 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 errorsfunction makeErr() { reports[${#reports[@]}]="Operation not succeded"; printInfo; writeToLog; exit 0;}##@F-testsfunction 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 zramif [ $test_module == 0 ]     then    reports[${#reports[@]}]="The current kernel does not find zram module";    makeErr;fi}##--@F Check the program dependencyfunction checkDep() {if [[ ! $(apt-cache policy ${getpkg[@]} | grep status|wc -l) = $(echo -e ${#getpkg[@]}) ]];    then apt-get install $(echo ${getpkg[@]});fifor ((ipkg=0; ipkg != ${#getpkg[@]}; ipkg++)) do    if [ ! "$(apt-cache policy ${getpkg[$ipkg]} | grep status)" ]    then    ## lang messages if yes then lang else us...    reports[${#reports[@]}]="Do not set ${getpkg[$ipkg]} package";    makeErr;fidonelocal lsbtools=$(whereis -b lsb_release|awk '/^lsb_release:/{print $2}');deb_release=$( $lsbtools -r|awk '/^Release:/{print $2}'|sed 's/\./ /g'|awk '{print$1}' );}function install() {if [ ! -d "/etc/zramraid" ]    then	mkdir -p /etc/zramraid;ficp -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;if [[ ! $deb_release < 8 ]];      then	update-rc.d zramraid-manager defaults;     else	cp -f $PWD/lib/systemd/system/zramraid.service /lib/systemd/system/zramraid.service;	/bin/systemctl enable zramraid.service;	if [ -f "/lib/systemd/system/libvirt-guests.service" ];	    then		if [ $(cat /lib/systemd/system/libvirt-guests.service|grep zramraid|wc -m ) = 0 ];		    then			sed -i '/Requires=virt-guest-shutdown.target/a \'"Requires=zramraid.service" /lib/systemd/system/libvirt-guests.service;		fi	fifireports[${#reports[@]}]="zramraid install susseful";reports[${#reports[@]}]="please setup config for autostart from boot: /etc/default/zramraid.";writeToLog;printInfo;}function uninstall() {function zrDel() {if [[ $deb_release < 8 ]];      then	/etc/init.d/zramraid-manager stop;	update-rc.d zramraid-manager remove;     else    /bin/systemctl stop zramraid.service;    /bin/systemctl disable zramraid.service;    rm /lib/systemd/system/zramraid.service;    if [ -f "/lib/systemd/system/libvirt-guests.service" ];	    then		if [ ! $(cat /lib/systemd/system/libvirt-guests.service|grep zramraid|wc -m ) = 0 ];		    then			sed -i -e '/'"Requires=zramraid.service"'/d' /lib/systemd/system/libvirt-guests.service;		fi	fifi	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;fireports[${#reports[@]}]="zramraid uninstall susseful";writeToLog;printInfo;}##--@F step operationfunction testId() {if (( ! $(id|grep "root"|wc -m) ))        then        reports=();         reports[${#reports[@]}]="It is only available with root privileges";        makeErr;fi}##--@F executorfunction executor() {if [[ ${#execute_func[@]} -eq 0 ]]     then echo "exit";         exit 0; fifor ((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 zramraidcase "$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
 |