123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- #!/bin/bash
- option=$1;
- rdate=$(date +%c);
- reports=();
- getpkg=( "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=""
- operation_install=( "clear" "testId" "checkDep" "checkMod" "install" "printInfo" );
- operation_uninstall=( "clear" "testId" "checkDep" "checkMod" "uninstall" "printInfo" );
- operation_help=( "clear" "printInfo" );
- execute_func=();
- 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
-
-
- 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="";
- }
- 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;
- }
- function pIdf() {
- for ((rpt_index=0; rpt_index != ${#reports[@]}; rpt_index++))
- do
- echo "${reports[$rpt_index]}";
- done
- exit 0;
- }
- eXlogic;
- }
- function makeErr() {
- reports[${#reports[@]}]="Operation not succeded";
- printInfo;
- writeToLog;
- exit 0;
- }
- 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
- if [ $test_module == 0 ]
- then
- reports[${#reports[@]}]="The current kernel does not find zram module";
- makeErr;
- fi
- }
- function checkDep() {
- if [[ ! $(apt-cache policy ${getpkg[@]} | grep status|wc -l) = $(echo -e ${#getpkg[@]}) ]];
- then apt-get install $(echo ${getpkg[@]});
- fi
- for ((ipkg=0; ipkg != ${#getpkg[@]}; ipkg++))
- do
- if [ ! "$(apt-cache policy ${getpkg[$ipkg]} | grep status)" ]
- then
-
- reports[${#reports[@]}]="Do not set ${getpkg[$ipkg]} package";
- makeErr;
- fi
- done
- local 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;
- 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;
- if [[ $deb_release < 8 ]];
- then
- update-rc.d -n 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
- fi
- fi
- reports[${#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 -n zramraid-manager remove;
- else
- /bin/systemctl stop zramraid-manager;
- /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 ) = 1];
- then
- sed -i -e '/'"Requires=zramraid.service"'/d' /lib/systemd/system/libvirt-guests.service;
- fi
- fi
- 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;
- }
- function testId() {
- if (( ! $(id|grep "root"|wc -m) ))
- then
- reports=();
- reports[${#reports[@]}]="It is only available with root privileges";
- makeErr;
- fi
- }
- function executor() {
- if [[ ${#execute_func[@]} -eq 0 ]]
- then echo "exit";
- exit 0;
- fi
- for ((ex_index=0; ex_index != ${#execute_func[@]}; ex_index++))
- do
-
-
- ${execute_func[ex_index]};
- done
- }
- case "$option" in
- "--install" | "--install" )
- execute_func=( ${operation_install[@]} );
- executor;
- exit 0
- ;;
- "--uninstall" | "--uninstall" )
- execute_func=( ${operation_uninstall[@]} );
- executor;
- exit 0
- ;;
- "--help" | "--help" )
- execute_func=( ${operation_help[@]} );
- executor;
- exit 0
- ;;
- * )
- execute_func=( ${operation_help[@]} );
- executor;
- exit 1
- ;;
- esac
|