move project Пн 30 ноя 2026 23:54:40 MSK

This commit is contained in:
master committed 2026-11-30 23:54:40 +03:00
1 parent a6f2ef8362
commit 513c844076
9 files changed
+2216 -2

No files matched your search

+68 -2
View File
@@ -1,3 +1,69 @@
# zramraid
# PROJECT ZRAMRAID
BASH: System for creating RAID arrays from block devices created in RAID
Description<br>
This project is a derivative of all the experiments using virtualization XEN \ KVM, which we started in 2012. Then we did not like the speed of the disk subsystem in HVM mode for WINDOWS 2003-2012 servers.<br>
In the search for solutions remembered zram module and the first thing to make the virtual partition to swop. Of course definitely work in OS WINDOWS, "this" environment has become faster, more accurate became more "responsive", but not for server tasks. That's when we got the idea to make a so-called virtual RAID, which we call the derivative zram -> ZRAID. The idea itself is simple, create a block of the memory device using the module zram core size equal to the image of the disk for the virtual machine, and then combine the two media in a RAID array, and a ready-RAID (MD) give virtual machine XEN or KVM as a carrier for data.<br>
After that, in a virtual machine format, and use for other purposes.<br>
What result? First - the speed of communication with the carrier is almost equal to the speed of memory, and I'll tell you on the SSD will be steeper! Secondly, with regard to feelings of failure, especially in the virtual machine performed emergency shutdown of the system unit and the machine is adequately withstand such "failures". All the same software RAID in LINUX system worthy of respect! In all our experiments on extreme trips as part ZRAID, and emergency stops, the data remained intact, which helped us to solve many production problems in the operation of virtual machines. At the moment, we have a server who worked for 3years and despite occasional failures on nutrition but survived the test for reliability. Certainly our modest project can not qualify for the commercial operation and is rather unusual solution, but I think will help to solve many problems on the servers. In particular I have in this example «ZRAID» excellent health "feels" the mail database. In addition to the description and functional diagram «ZRAID», working code in BASH which is the system on OS LINUX for easier use of the solution.<br>
The code is distributed under the GNU Public License.<br>
<br>
BASH language code consists of 3 scripts:<br>
1. install - installer script installer supports OS Debian<br>
2. zramraid-config - script for configuration zraid media management<br>
3. zramraid-maker - starting and stopping control script zraid media<br>
# How to use:
zramraid-install --install<br>
and..<br>
<hr>
1. Create an image of the required size, consider the size of allocated RAM to mirror your image!<br>
example:<br>
cat /proc/meminfo |grep MemAvailable<br>
MemAvailable: 6638832 kB<br>
or:<br>
zramraid-config --list<br>
2. Create image:<br>
fallocate -l 3G /home/kvm/disk0.img<br>
3. Create zramraid:<br>
zramraid-config --add md0:/home/kvm/disk0.img<br>
4. Check the configuration:<br>
zramraid-config --list<br>
5. Start or restart zramraid:<br>
/etc/init.d/zramraid-manager restart<br>
or:<br>
systemctl restart zramraid-manager<br>
6. Result:<br>
cat /proc/mdstat<br>
Personalities : [raid1]<br>
md0 : active raid1 zram0[2] loop0[1]<br>
3143680 blocks super 1.2 [2/2] [UU]<br>
<br>
unused devices: <none><br>
To automatically start the system, you must correct the configuration file:<br>
editor /etc/defaults/zramraid<br>
...<br>
mode="auto";<br>
<br>
Now, when you start, zramraid will start automatically.<br>
<hr>
Additional control parameters can be found from the parameter --help<br>
example:<br>
zramraid-config --help<br>
zramraid-maker --help<br>
zramraid-install --help<br>
# Recommendations:
If you plan to use zramraid together with KVM then we recommend making a change for the systemd:<br>
editor /lib/systemd/system/libvirt-guests.service<br>
...<br>
Requires=zramraid.service<br>
...<br>
* see example files from /lib/systemd/system
* If an error occurs: "Peer's Certificate issuer is not recognized"
use the option: git -c http.sslVerify=false clone ...
<hr>
version - 18.05.23
<hr>
License: GPLv3
+5
View File
@@ -0,0 +1,5 @@
## service start
# mode - startup parameter zramraid system in manual or automatic mode, examples: mode="auto" or mode="manual"
# if "manual" zramraid does not start automatically at startup, dafult - "manual"
#
mode="manual";
+89
View File
@@ -0,0 +1,89 @@
#!/bin/bash
### BEGIN INIT INFO
# Provides: zramraid-manager
# Required-Start: udev $local_fs $remote_fs $time
# Required-Stop: $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start the zraid
# Description: starts zraid using start-stop-daemon
### END INIT INFO
## version =03.03.21
PARMS=$1
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/etc/zramraid";
DESC="Zram Raid"
NAME="zramraid"
SCRIPTNAME="zramraid-maker";
ZPATH="/etc/zramraid"
MODE="/etc/default/zramraid";
##
if [[ "$PARMS" = '' ]];
then
echo "/etc/init.d/zramraid-manager {start|stop|restart|status}"
exit 0
fi
function zRun() {
if [ ! $(echo $USER|grep root|wc -m) = 0 ]
then
if [ $(cat $MODE |grep mode|grep -v '#'|sed 's/\mode=//g'|sed 's/\"//g;s/\;//g'|grep auto|wc -m) == 0 ]
then
echo "$(date) $NAME: no autostart from boot! mode=manual - /etc/defaults/zramraid">>/var/log/zramraid.log
exit 0;
else
$ZPATH/$SCRIPTNAME --on
fi
exit 0;
fi
if [ ! $(echo $USER|grep '[priv]'|wc -m) = 0 ]
then
if [ $(cat $MODE |grep mode|grep -v '#'|sed 's/\mode=//g'|sed 's/\"//g;s/\;//g'|grep auto|wc -m) == 0 ]
then
echo "$(date) $NAME: no autostart from boot! mode=manual - /etc/defaults/zramraid">>/var/log/zramraid.log
exit 0;
else
$ZPATH/$SCRIPTNAME --on
fi
exit 0;
fi
echo "$NAME:Sorry.. The only root privileges!"
exit 0;
}
### begin
case "$PARMS" in
"start" | "start" )
zRun;
exit 0
;;
"stop" | "stop" )
$ZPATH/$SCRIPTNAME --off
exit 0
;;
"restart" | "restart" )
$ZPATH/$SCRIPTNAME --off
sleep 3
zRun;
exit 0
;;
"status" | "status" )
$ZPATH/$SCRIPTNAME --status
exit 0
;;
"boot" | "boot" )
zBoot;
exit 0
;;
* )
echo
echo "no input parameters."
exit 1
;;
esac
+118
View File
@@ -0,0 +1,118 @@
{
"ru_RU.UTF-8":{
"msg":[
{"0":"Система zramraid не запущена!"},
{"1":"Нет параметров для zramraid-maker!"},
{"2":"Полный запуск системы zramraid: zramraid-maker --on"},
{"3":"Полная остановка системы zramraid: zramraid-maker --off"},
{"4":"Состояние системы zramraid: zramraid-maker --status"},
{"5":"Проверка конфигрурации системы zramraid: zramraid-maker --test"},
{"6":"В системе отсутствует пакет jq!"},
{"7":"Отсутствует файл конфигурации zramraid.conf!"},
{"8":"В системе отсутствует пакет mdadm!"},
{"9":"В конфигурации нет указанных md устройств!"},
{"10":"Неверный параметр в конфигурации!"},
{"11":"Занято системой!"},
{"12":"В конфигурации эти значения дублируются!"},
{"13":"Не найден файл!"},
{"14":"Общий обьем образов превышает установленный лимит памяти!"},
{"15":"Это устройство занято системой"},
{"16":"Старт системы zramraid..."},
{"17":"Стоп системы zramraid..."},
{"18":"zramraid - включен и активированы устройства:"},
{"19":"zramraid - отключен"},
{"20":"Лимит памяти:"},
{"21":"Ошибка устройства md:"},
{"22":"Ошибка образа:"},
{"23":"Общий размер образов:"},
{"24":"Чтение конфигурации - успешно"},
{"25":"Проверка устройств md в системе - успешно"},
{"26":"Проверка образов - успешно"},
{"27":"Не обнаружен модуль zram в ядре системы!"},
{"28":"Извините, старая версия ядра, рекомендуется 3.14.x и выше!"},
{"29":"Ошибочное значение"},
{"30":"Допускается только"},
{"31":"Помомощь: zramraid-maker --help"},
{"32":"Памяти всего:"},
{"33":"Памяти свободно:"},
{"34":"Нет параметров для zramraid-config!"},
{"35":"Создание пустого zramraid.conf: zramraid-config --config-default"},
{"36":"Добавление устройства MD: zramraid-config --add md[x]:image - пример: md1:/home/zramraid/disk1.img"},
{"37":"Удаление устройства MD: zramraid-config --del md[x] - пример: md1"},
{"38":"Получение списка MD: zramraid-config --list"},
{"39":"Параметр ограничения использования памяти: zramraid-config --limit-mem [x] - пример: 90% or 1024Kb"},
{"40":"Параметр описание носителя: zramraid-config --info-md md[x] - пример: md1"},
{"41":"Помощь: zramraid-config --help"},
{"42":"Операция не выполнена"},
{"43":"Выполнено - успешно."},
{"44":"Не найдено:"},
{"45":"Введите описаниe для устройства MD и нажмите Enter:"},
{"46":"Описание образов в конфигурации не найдено"},
{"47":"Доступно только с привилегиями root!"},
{"48":"Версия zramraid:"},
{"49":"Извините, старая версия ядра, рекомендуется 4.2.x и выше для этого параметра!"},
{"50":"- устройство уже работает..."},
{"51":"версия ядра не позволяет выборочное управление носителями, только полная остановка и запуск, рекомендуется 4.2.x и выше."},
{"52":"Индивидуальный запуск носителя zramraid: zramraid-maker --on md[x] - пример:md1"},
{"53":"Индивидульная остановка носителя zramraid: zramraid-maker --off md[x] - пример:md1"}
]
},
"C.UTF-8":{
"msg":[
{"0":"Not start zramraid system!"},
{"1":"No input parameters for zramraid-maker"},
{"2":"Full start arrays zramraid: zramraid-maker --on"},
{"3":"Full stop array zramraid: zramraid-maker --off"},
{"4":"State arrays zramraid: zramraid-maker --status"},
{"5":"Check config zramraid arrays: zramraid-maker --test"},
{"6":"Not found in the system package jq!"},
{"7":"Not found configuration file zramraid.conf"},
{"8":"Not found in the system package mdadm!"},
{"9":"No specified md devices!"},
{"10":"The parameter is incorrect!"},
{"11":"Busy system!"},
{"12":"This number duplicated!"},
{"13":"Not found file!"},
{"14":"The total images exceeds the limit of available memory!"},
{"15":"This device busy a system!"},
{"16":"zramraid start..."},
{"17":"zramraid stop..."},
{"18":"zramraid - on & enable devices:"},
{"19":"zramraid - off"},
{"20":"limit mem:"},
{"21":"error: md"},
{"22":"error: image "},
{"23":"total size images: "},
{"24":"read config - done."},
{"25":"test md from system - done."},
{"26":"test images - done."},
{"27":"zram module in the kernel can not be found!"},
{"28":"I'm sorry the old kernel version! recommended 3.14 or higher"},
{"29":"Error value"},
{"30":"Allowed only"},
{"31":"help: zramraid-maker --help"},
{"32":"Memory total:"},
{"33":"Memory free:"},
{"34":"No input parameters for zramraid-config!"},
{"35":"Create empty zramraid.conf: zramraid-config --config-default"},
{"36":"Add device MD in config: zramraid-config --add md[x]:image - example: md1:/home/zramraid/disk1.img"},
{"37":"Delete device MD in config: zramraid-config --del md[x] - example: md1"},
{"38":"Listing devices MD in config: zramraid-config --list"},
{"39":"The memory limit usage: zramraid-config --limit-mem [x] - example: 90% or 1024Kb"},
{"40":"The set info for md device: zramraid-config --info-md md[x] - example: md1"},
{"41":"Help: zramraid-config --help"},
{"42":"Operation not succeded"},
{"43":"Operation succeded"},
{"44":"Not found:"},
{"45":"Enter the data for device MD description and press Enter:"},
{"46":"description of the images in the configuration are not found"},
{"47":"It is only available with root privileges"},
{"48":"version zramraid:"},
{"49":"I'm sorry the old kernel version! recommended 4.2 or higher for this option"},
{"50":"-device is already working"},
{"51":"kernel version does not allow selective media management, only a complete stop and start, recommended 4.2 or higher for this option"},
{"52":"Selective startup zramraid device: zramraid-maker --on md[x] - example: md1"},
{"53":"Selective stop zramraid device: zramraid-maker --off md[x] - example: md1"}
]
}
}
+705
View File
@@ -0,0 +1,705 @@
#!/bin/bash
## zramraid-config
## (c) author's idea and realization: Kleemov A. & Koshuba V.
## script author: Koshuba V - stvixfree@gmail.com
## License: GPLv3
## all parameters
##settime=$(date +%s%3N);
option=$1;
params=$2;
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/etc/zramraid";
version="18.05.23";
## msg [53]
set_msg=( '"0" "0"' '"1" "0"' '"2" "0"' '"3" "0"' '"4" "0"' '"5" "0"' '"6" "1"' '"7" "1"' '"8" "1"'
'"9" "1"' '"10" "1"' '"11" "1"' '"12" "1"' '"13" "1"' '"14" "1"' '"15" "1"' '"16" "0"'
'"17" "0"' '"18" "0"' '"19" "0"' '"20" "1"' '"21" "1"' '"22" "1"' '"23" "1"' '"24" "1"'
'"25" "1"' '"26" "1"' '"27" "1"' '"28" "1"' '"29" "1"' '"30" "1"' '"31" "1"' '"32" "1"'
'"33" "1"' '"34" "1"' '"35" "1"' '"36" "1"' '"37" "1"' '"38" "1"' '"39" "1"' '"40" "1"'
'"41" "1"' '"42" "1"' '"43" "1"' '"44" "1"' '"45" "1"' '"46" "1"' '"47" "1"' '"48" "1"'
'"49" "0"' '"50" "0"' '"51" "0"' '"52" "0"' '"53" "0"' );
msg=();
###
lang=$(locale|grep LANG=|sed 's/\LANG=//g');
rdate=$(date +%c);
##
total_mem=$(cat /proc/meminfo |awk '/^MemTotal:/{print $2}');
free_mem=$(cat /proc/meminfo |awk '/^MemAvailable:/{print $2}');
limit_mem=0;
info_limit_mem=0;
total_size_images=0;
zpath="/etc/zramraid";
zconfig="zramraid.conf";
zmsg="/etc/zramraid/messages.dat";
log="/var/log/zramraid.log";
reports=();
id_zmd=();
image_zmd=();
size_zmd=();
json_key=( "id" "info" "image" );
zdm_vars=( "id_zmd" "info_zmd" "image_zmd" );
input_unit=();
math_unit=0;
set_params=();
set_id="";
set_image="";
set_opt=( "0" "0" "0" "0" );
set_size_limit=0;
set_type_limit="";
##
zr_blkid=$(whereis -b blkid|awk '/^blkid:/{print $2}');
zr_jq=$(whereis -b jq|awk '/^jq:/{print $2}');
## functions & operations
operation_createdef=( "checkDep" "loadMsg" "testId" "confEmpty" );
operation_add=( "checkDep" "loadMsg" "testId" "inPut" "readConf" "testMd" "testImage" "addMd" );
operation_del=( "checkDep" "loadMsg" "testId" "inPut" "readConf" "delMd" );
operation_list=( "clear" "checkDep" "loadMsg" "testId" "readConf" "reportConf" "printInfo" );
operation_help=( "clear" "checkDep" "loadMsg" "printInfo" );
operation_mdinfo=( "clear" "checkDep" "loadMsg" "testId" "inPut" "readConf" "addInfoMd" );
operation_mem=( "checkDep" "loadMsg" "testId" "inPut" "readConf" "setLimitMem" );
execute_func=();
## logic executor values
iFs=();
logic=();
value_in="";
lEnd=1;
##--@F math functions
## Byte <> Kb
function Byte(){ if [ ! ${input_unit[2]} ]
then eval ${input_unit[1]}=$(echo "scale=0; ${input_unit[0]}/1024"|bc -lq);
else eval ${input_unit[1]}=$(echo ${input_unit[0]}*1024|bc -lq);
fi }
## Kb <> Byte
function Kb() { if [ ! ${input_unit[2]} ]
then eval ${input_unit[1]}=$(echo ${input_unit[0]}*1024|bc -lq);
else eval ${input_unit[1]}=$(echo "scale=0; ${input_unit[0]}/1024"|bc -lq);
fi }
## Mb <> Byte
function Mb() { if [ ! ${input_unit[2]} ]
then eval ${input_unit[1]}=$(echo ${input_unit[0]}*1024^2|bc -lq);
else eval ${input_unit[1]}=$(echo "scale=0; ${input_unit[0]}/1024^2"|bc -lq);
fi }
## Gb <> Byte
function Gb() { if [ ! ${input_unit[2]} ]
then eval ${input_unit[1]}=$(echo ${input_unit[0]}*1024^3|bc -lq);
else eval ${input_unit[1]}=$(echo "scale=0; ${input_unit[0]}/1024^3"|bc -lq);
fi }
## Tb <> Byte
function Tb() { if [ ! ${input_unit[2]} ]
then eval ${input_unit[1]}=$(echo ${input_unit[0]}*1024^4|bc -lq);
else eval ${input_unit[1]}=$(echo "scale=0; ${input_unit[0]}/1024^4"|bc -lq);
fi }
## -@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 message: ${reports[$rpt_index]}">>$log;
done
}
##--@F make all errors
function makeErr() {
reports[${#reports[@]}]=${msg[42]};
printInfo;
writeToLog;
exit 0;
}
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 $value_in|sed 's/--list//g'|wc -m) == 0"
"$(echo -n ${#reports[@]}) == 0" );
logic=( '"" "pInone"'
'"" "pIhelp"'
'"" "pIlist"'
'"pIdf" "pInone"' );
function pInone() {
lEnd=0;
clear;
echo;
echo -e ${msg[34]}"\n"${msg[35]}"\n"${msg[36]}"\n"${msg[37]}"\n"${msg[38]}"\n"${msg[39]};
echo -e ${msg[40]}"\n"${msg[41]}"\n"${msg[48]} $version;
exit 0;
}
function pIhelp() {
lEnd=0;
clear;
echo;
echo -e ${msg[35]}"\n"${msg[36]}"\n"${msg[37]}"\n"${msg[38]}"\n"${msg[39]};
echo -e ${msg[40]}"\n"${msg[41]}"\n"${msg[48]} $version;
exit 0;
}
function pIlist() {
lEnd=0;
info_free_mem=0;
info_total_mem=0;
input_unit=( "$free_mem" "info_free_mem" "1" );
Kb;
input_unit=( "$total_mem" "info_total_mem" "1" );
Kb;
echo "==============================================================...";
echo "${msg[32]} $info_total_mem""Mb ${msg[33]} $info_free_mem""Mb";
echo "==============================================================...";
for ((rpt_index=0; rpt_index != ${#reports[@]}; rpt_index++))
do
echo "${reports[$rpt_index]}";
done
echo "--------------------------------------------------------------...";
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 Check the program dependency
function checkDep() {
# This installation check jq
if [ $(echo $zr_jq|grep jq|wc -m) = 0 ]
then
## lang messages if yes then lang else us...
reports=();
reports[${#reports[@]}]=${msg[6]};
makeErr;
fi
}
##--@F read locale messages
function loadMsg() {
# - msg debug
echo "Please wait - load locales..";
# test enable file messages
if [ ! -f $zmsg ]
then
reports[${#reports[@]}]="$zmsg - file not found!";
makeErr;
fi
# read msg
if [[ ! $(cat $zmsg|grep "$lang"|wc -m) == 0 ]]
then
index_msg="$(cat $zmsg| jq '."'$lang'".msg[]'|grep ':'|sed 's/\://g;s/\"//g'|awk '{print$1}'|tail -n1)";
if [ ! "$index_msg" == "0" ];
then
for (( m_index=0; m_index != $index_msg; m_index++))
do
read_msg=$(cat $zmsg | jq '."'$lang'".msg['$m_index']."'$m_index'"'| sed 's/\"//g');
msg[$m_index]="$read_msg";
done
else
reports[${#reports[@]}]="$zmsg - Incorrect file format or empty!";
makeErr;
fi
fi
}
##--@F step operation
function readConf() {
# test enable file zramraid.conf
if [ ! -f $zpath/$zconfig ]
then
reports=();
reports[${#reports[@]}]=${msg[7]};
makeErr;
fi
# read config
for (( rd_index=0; rd_index !=${#zdm_vars[@]}; rd_index++))
do
read_key=$(cat $zpath/$zconfig | jq '.md[].'${json_key[$rd_index]}| sed 's/\"//g');
eval ${zdm_vars[$rd_index]}="(" $read_key ")";
done
log=$(cat $zpath/$zconfig | jq '.log'| sed 's/\"//g');
## test free memory
value_mem=$(cat $zpath/$zconfig | jq '.limit_free_mem.value_size'| sed 's/\"//g');
value_in="$value_mem";
iFs=( "$(echo -n $value_in|wc -m) == 0"
"$(echo -n $value_in|sed 's/%//g'|wc -m) == 0"
"$(echo -n $value_in|sed 's/Kb//g'|wc -m) == 0" );
logic=( '"" "lmem_Err"'
'"" "lmem_Per"'
'"lmem_Err" "lmem_Kb"' );
lmem_Err() {
reports=();
reports[${#reports[@]}]="${msg[29]} $value_mem /or null - ${msg[10]}(${msg[30]}:Kb,%)";
makeErr;
}
lmem_Per() {
lEnd=0;
test_read_mem=0;
input_unit=( "$free_mem" "test_read_mem" );
Kb;
math_unit=$(echo "scale=2; $(echo "scale=2; $test_read_mem/100"|bc -lq)*$(cat $zpath/$zconfig | jq '.limit_free_mem.size'| sed 's/\"//g')"|bc -lq|sed 's/\./ /g'|awk '{print$1}');
## -"
if (( "$math_unit" < 0 ))||[[ "$math_unit" == "" ]]||[[ ! "$math_unit" =~ ^([0-9]+)$ ]]
then
reports=();
reports[${#reports[@]}]="${msg[29]} $value_mem /or null - ${msg[10]}(${msg[30]}:Kb,%)";
makeErr;
else
limit_mem=$math_unit;
input_unit=( "$math_unit" "info_limit_mem" );
Byte;
math_unit=$info_limit_mem;
input_unit=( "$math_unit" "info_limit_mem" "1" );
Kb;
fi
}
lmem_Kb() {
lEnd=0;
input_unit=( "$(cat $zpath/$zconfig | jq '.limit_free_mem.size'| sed 's/\"//g')" "math_unit" );
Kb;
limit_mem=$math_unit;
input_unit=( "$math_unit" "info_limit_mem");
Mb;
}
eXlogic;
}
function testMd() {
# test the md number parameters for errors and busy in system for add md
if (( "$set_id" < 0 ))||[[ "$set_id" == "" ]]||[[ ! "$set_id" =~ ^([0-9]+)$ ]]
then
reports=();
reports[${#reports[@]}]="${msg[21]}$set_id - ${msg[10]}";
makeErr;
fi
if [ "$($zr_blkid /dev/md$set_id|grep $set_id)" ]
then
reports=();
reports[${#reports[@]}]="${msg[21]}$set_id - ${msg[11]}";
makeErr;
fi
# test the duble number md from config
test_dub=( $(echo -e ${id_zmd[@]}|tr " " "\n"|sort|tr "\n" " ") );
for (( dub_index=0; dub_index != ${#id_zmd[@]}; dub_index++ ))
do
dubtest_index=$(($dub_index+1));
if [[ ! $dubtest_index > $((${#id_zmd[@]}-1)) ]]
then
if [[ $((${test_dub[$dubtest_index]}-${test_dub[$dub_index]})) == 0 ]];
then
## duble from config
reports=();
reports[${#reports[@]}]="${msg[21]}${id_zmd[$(($dub_index+1))]} - ${msg[12]}";
makeErr;
fi
fi
if [[ $((${test_dub[$dub_index]}-$set_id)) == 0 ]]
then
## duble input end from config
reports=();
reports[${#reports[@]}]="${msg[21]}$set_id - ${msg[12]}";
makeErr;
fi
done
}
function testImage() {
total_size_images=0;
eval tmp_date="(" $(echo -e $set_image|sed 's,/,\, ,g'|cut -c 2-) ")";
test_path=$(echo $set_image|sed 's,'${tmp_date[$((${#tmp_date[@]}-1))]}',,g');
# test enable file images
if [ ! -f $test_path${tmp_date[$((${#tmp_date[@]}-1))]} ]
then
reports=();
reports[${#reports[@]}]="${msg[22]}$set_image - ${msg[13]}";
makeErr;
fi
function new_add() {
test_size_images=$(stat -c%s $test_path${tmp_date[$((${#tmp_date[@]}-1))]});
}
function sum_img() {
#echo "summ images...";
## the sum size images
for (( image_index=0; image_index != ${#image_zmd[@]}; image_index++ ))
do
if [ ! -f ${image_zmd[$image_index]} ]
then reports[${#reports[@]}]="${msg[22]}${image_zmd[$image_index]} - ${msg[13]}";
makeErr;
fi
size_zmd[$image_index]=$(stat -c%s ${image_zmd[$image_index]});
done
# test size image end size zram ...
for (( im_index=0; im_index != ${#image_zmd[@]}; im_index++ ))
do
total_size_images=$(($total_size_images+${size_zmd[$im_index]}));
done
size_new_image=$(stat -c%s $test_path${tmp_date[$((${#tmp_date[@]}-1))]});
test_size_images=$(($total_size_images+$size_new_image));
}
function add_false() {
input_unit=( "$test_size_images" "math_unit" "1" );
Kb;
img_msg=$math_unit;
input_unit=( "$limit_mem" "math_unit" "1" );
Kb;
mem_msg=$math_unit;
reports=();
reports[${#reports[@]}]="${msg[22]}$set_image - ${msg[14]}";
reports[${#reports[@]}]="${msg[23]}$img_msg""Kb > ${msg[20]}$mem_msg""Kb";
makeErr;
}
iFs=( "$(echo -n ${#image_zmd[@]}) > 0" );
logic=( '"new_add" "sum_img"' );
eXlogic;
iFs=( "$(echo -n $test_size_images) <= $limit_mem" );
logic=( '"add_false" ""' );
eXlogic;
}
## input parameters
function inPut() {
function inMd() {
lEnd=0;
eval set_params="(" $(echo -e $params|sed 's/:/ /') ")";
if [ $(($(echo -n ${set_params[0]}|grep -o md|wc -m)-1)) == 2 ]
then
set_id=$(echo -e ${set_params[0]}|awk '{print$1}'|sed 's/\md//g');
if [[ "$set_id" == "" ]]||[[ ! "$set_id" =~ ^([0-9]+)$ ]]||(( "$set_id" < 0 ))
then
reports=();
reports[${#reports[@]}]=${msg[10]};
makeErr;
else
reports=();
set_image=${set_params[1]};
fi
else
reports=();
reports[${#reports[@]}]=${msg[10]};
makeErr;
fi
}
function inLimit() {
lEnd=0;
set_size=$(echo $params|sed 's/%//g'|sed 's/Kb//g');
if [[ "$set_size" == "" ]]||[[ ! "$set_size" =~ ^([0-9]+)$ ]]||(( "$set_size" <= 0 ))
then
reports=();
reports[${#reports[@]}]=${msg[29]};
reports[${#reports[@]}]=${msg[39]};
makeErr;
else
set_type=$(echo $params|sed 's/'"$set_size"'//g');
if [[ "$set_type" == "%" ]]||[[ "$set_type" == "Kb" ]]
then
set_size_limit=$set_size;
set_type_limit=$set_type;
else
reports=();
reports[${#reports[@]}]=${msg[29]};
reports[${#reports[@]}]=${msg[39]};
makeErr;
fi
fi
}
function noSwitch() {
lEnd=0;
reports=();
reports[${#reports[@]}]=${msg[30]};
reports[${#reports[@]}]=${msg[37]};
reports[${#reports[@]}]=${msg[38]};
reports[${#reports[@]}]=${msg[40]};
makeErr;
}
iFs=( "${set_opt[1]} == 1" "${set_opt[2]} == 1" "${set_opt[3]} == 1" "${set_opt[4]} == 1" );
logic=( '"" "inMd"' '"" "inMd"' '"" "inLimit"' '"noSwitch" "inMd"' );
eXlogic;
}
## create empty config
function confEmpty() {
function confNew(){
echo '{'>$zpath/$zconfig
echo -e '"info":"id - md[id], image - is the path to the media to mount",\n'>>$zpath/$zconfig
echo -e '"log":"/var/log/syslog",\n'>>$zpath/$zconfig
echo '"limit_free_mem":{'>>$zpath/$zconfig
echo '"info":"size - the parameter 10-100% or 1024Kb...; Caution - the limit use free memory!",'>>$zpath/$zconfig
echo '"size":"90",'>>$zpath/$zconfig
echo -e '"value_size":"%"},\n'>>$zpath/$zconfig
echo '"md":['>>$zpath/$zconfig
echo ' ]'>>$zpath/$zconfig
echo -e '}'>>$zpath/$zconfig
}
if [ ! -f $zpath/$zconfig ]
then
confNew;
else
cp $zpath/$zconfig $zpath/$(date +%H_%M)"-zramraid.old">/dev/null;
confNew;
fi
}
function addMd() {
local tdate=$(date +%j);
cat $zpath/$zconfig|jq -c '.md|=.+[{"id":"'$set_id'","info":"","image":"'$set_image'"}]'|
jq '.'>$zpath/$tdate$zconfig && cat $zpath/$tdate$zconfig>$zpath/$zconfig && rm -f $zpath/$tdate$zconfig;
}
## del md from config
function delMd() {
local tdate=$(date +%j);
local del_value=$(cat $zpath/$zconfig|jq -c '.md[]|select (.id == "'$set_id'")');
#'
if [[ ! "$del_value" ]]
then
reports=();
reports[${#reports[@]}]="${msg[29]}: md$set_id - ${msg[9]}";
makeErr;
else
cat $zpath/$zconfig|jq -c '.md|=.-['$del_value']'|jq '.'>$zpath/$tdate$zconfig && cat $zpath/$tdate$zconfig>$zpath/$zconfig && rm -f $zpath/$tdate$zconfig;
fi
}
## report config
function reportConf() {
reports=();
for (( report_index=0; report_index != ${#id_zmd[@]}; report_index++ ))
do
reports[${#reports[@]}]="config md: ${id_zmd[$report_index]}";
reports[${#reports[@]}]="info md: ${info_zmd[$report_index]}";
if (( $(stat -c%s ${image_zmd[$report_index]}) > 1024 ))
then
input_unit=( "$(stat -c%s ${image_zmd[$report_index]})" "math_unit" "1" );
Kb;
rep_size="$math_unit""Kb";
else
rep_size="$(stat -c%s ${image_zmd[$report_index]})b";
fi
reports[${#reports[@]}]="size md: $rep_size";
reports[${#reports[@]}]="image md: ${image_zmd[$report_index]}";
reports[${#reports[@]}]="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
done
reports[${#reports[@]}]="${msg[20]} $info_limit_mem Mb";
}
## setting limit memory
function setLimitMem() {
local tdate=$(date +%j);
test_set_mem=0;
test_free_mem=0;
if [[ "$set_type_limit" == "%" ]]
then
input_unit=( "$free_mem" "math_unit" );
Kb;
test_set_mem=$(echo "scale=2; $(echo "scale=2; $math_unit/100"|bc -lq)*$set_size_limit"|bc -lq|sed 's/\./ /g'|awk '{print$1}');
#-"
fi
if [[ "$set_type_limit" == "Kb" ]]
then
math_unit=$set_size_limit;
input_unit=( "$math_unit" "test_set_mem" );
Kb;
fi
if [[ ! "$test_set_mem" == 0 ]]
then
math_unit=$free_mem;
input_unit=( "$math_unit" "test_free_mem" );
Kb;
else
reports=();
reports[${#reports[@]}]=${msg[29]};
reports[${#reports[@]}]=${msg[39]};
makeErr;
fi
if (( "$test_free_mem" > "$test_set_mem" ))
then
## add date
local input_date_limit=$(cat $zpath/$zconfig|jq -c '.limit_free_mem|.size="'"$set_size_limit"'"|.value_size="'"$set_type_limit"'"');
cat $zpath/$zconfig|jq -c ' del (.limit_free_mem)'|
jq '.'>$zpath/$tdate$zconfig && cat $zpath/$tdate$zconfig>$zpath/$zconfig && rm -f $zpath/$tdate$zconfig;
## add
cat $zpath/$zconfig|jq -c '.limit_free_mem|=.+'"$input_date_limit"''|
jq '.'>$zpath/$tdate$zconfig && cat $zpath/$tdate$zconfig>$zpath/$zconfig && rm -f $zpath/$tdate$zconfig;
else
reports=();
reports[${#reports[@]}]="${msg[20]}$set_size_limit$set_type_limit > ${msg[33]}$free_mem""Kb";
reports[${#reports[@]}]=${msg[29]};
reports[${#reports[@]}]=${msg[39]};
makeErr;
fi
}
## add info device md
function addInfoMd() {
if [[ $(cat $zpath/$zconfig|jq -c '.md[]|select(.id == "'$set_id'")') ]]
#-'
then
local Sinput=();
clear
echo "--------------------------------------------------";
echo "${msg[45]}";
echo "--------------------------------------------------";
read Sinput;
local tdate=$(date +%j);
local data_info="$(cat $zpath/$zconfig|jq -c '.md[]|select(.id == "'$set_id'")|.info ="'"'$(echo ${Sinput[@]})'"'"')";
delMd;
cat $zpath/$zconfig|jq -c '.md|=.+['"$data_info"']'|
jq '.'>$zpath/$tdate$zconfig && cat $zpath/$tdate$zconfig>$zpath/$zconfig && rm -f $zpath/$tdate$zconfig;
else
reports=();
reports[${#reports[@]}]=${msg[9]};
makeErr;
fi
}
##--@F step operation
function testId() {
if (( ! $(id|grep "root"|wc -m) ))
then
reports=();
reports[${#reports[@]}]="${msg[47]}";
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
## create default config +
"--config-default" | "--config-default" )
execute_func=( ${operation_createdef[@]} );
executor;
exit 0
;;
## add md
"--add" | "--add" )
set_opt[1]="1";
execute_func=( ${operation_add[@]} );
executor;
exit 0
;;
## del md
"--del" | "--del" )
set_opt[2]="1";
execute_func=( ${operation_del[@]} );
executor;
exit 0
;;
## list md +
"--list" | "--list" )
execute_func=( ${operation_list[@]} );
executor;
exit 0
;;
## limit memory
"--limit-mem" | "--limit-mem" )
set_opt[3]="1";
execute_func=( ${operation_mem[@]} );
executor;
exit 0
;;
## md info
"--info-md" | "--info-md" )
set_opt[4]="1";
execute_func=( ${operation_mdinfo[@]} );
executor;
exit 0
;;
## help +
"--help" | "--help" )
execute_func=( ${operation_help[@]} );
executor;
exit 0
;;
* )
# selecting defaults. +
execute_func=( ${operation_help[@]} );
executor;
exit 1
;;
esac
+925
View File
@@ -0,0 +1,925 @@
#!/bin/bash
## zramraid-maker
## (c) author's idea and realization: Kleemov A. & Koshuba V.
## script author: Koshuba V - stvixfree@gmail.com
## License: GPLv3
## all parameters
option=$1;
params=$2;
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/etc/zramraid";
version="18.05.23";
## msg [53]
set_msg=( '"0" "1"' '"1" "1"' '"2" "1"' '"3" "1"' '"4" "1"' '"5" "1"' '"6" "1"' '"7" "1"' '"8" "1"'
'"9" "1"' '"10" "1"' '"11" "1"' '"12" "1"' '"13" "1"' '"14" "1"' '"15" "1"' '"16" "1"'
'"17" "1"' '"18" "1"' '"19" "1"' '"20" "1"' '"21" "1"' '"22" "1"' '"23" "1"' '"24" "1"'
'"25" "1"' '"26" "1"' '"27" "1"' '"28" "1"' '"29" "1"' '"30" "1"' '"31" "1"' '"32" "1"'
'"33" "1"' '"34" "0"' '"35" "0"' '"36" "0"' '"37" "0"' '"38" "0"' '"39" "0"' '"40" "0"'
'"41" "0"' '"42" "1"' '"43" "1"' '"44" "1"' '"45" "0"' '"46" "1"' '"47" "1"' '"48" "1"'
'"49" "1"' '"50" "1"' '"51" "1"' '"52" "1"' '"53" "1"' );
msg=();
##
lang=$(locale|grep LANG=|sed 's/\LANG=//g');
rdate=$(date +%c);
eval kernel_version="(" $(uname -r|sed 's/\./ /g'|awk '{print$1" "$2}') ")";
test_module=$(find /lib/modules/$(uname -r) -name '*.ko'|grep zram|wc -m);
##
total_mem=$(cat /proc/meminfo |awk '/^MemTotal:/{print $2}');
free_mem=$(cat /proc/meminfo |awk '/^MemAvailable:/{print $2}');
limit_mem=0;
info_limit_mem=0;
zpath="/etc/zramraid";
zconfig="/etc/zramraid/zramraid.conf";
zmsg="/etc/zramraid/messages.dat";
log="/var/log/zramraid.log";
##
reports=();
id_zmd=();
image_zmd=();
size_zmd=();
json_key=( "id" "image" );
zdm_vars=( "id_zmd" "image_zmd" );
input_unit=();
math_unit=0;
total_size_images=0;
test_status=0;
kernel_status=(); # [0]-kernel3x,[1]-kernel4x
##
set_md_id="";
set_zram_id="";
set_md_status=(); # [0]-on\off,[1]-load zram,[2]-in config
set_prepare=""; # 0 - new md, 1 - md assemble
##
zr_blkid=$(whereis -b blkid|awk '/^blkid:/{print $2}');
zr_jq=$(whereis -b jq|awk '/^jq:/{print $2}');
zr_mdadm=$(whereis -b mdadm|awk '/^mdadm:/{print $2}');
## functions & operations
operation_test=( "clear" "checkDep" "loadMsg" "testId" "checkModule" "confRead" "testMd" "testImage" "reportConf" "printInfo" );
operation_on=( "checkDep" "loadMsg" "testId" "confRead" "checkModule" "testMd" "testImage" "statusZraid" "createZraid" );
operation_sign_on=( "checkDep" "loadMsg" "testId" "confRead" "checkModule" "testImage" "inParams" "singleTestMd" "singleStart" );
operation_sign_off=( "checkDep" "loadMsg" "testId" "confRead" "checkModule" "testImage" "inParams" "singleTestMd" "singleStop" );
operation_off=( "checkDep" "loadMsg" "testId" "confRead" "checkModule" "offZraid" );
operation_status=( "checkDep" "loadMsg" "testId" "confRead" "statusZraid" );
operation_help=( "clear" "checkDep" "loadMsg" "printInfo" );
execute_func=();
## logic executor values
iFs=();
logic=();
value_in="";
lEnd=1;
##--@F math functions
## Byte <> Kb
function Byte(){ if [ ! ${input_unit[2]} ]
then eval ${input_unit[1]}=$(echo "scale=0; ${input_unit[0]}/1024"|bc -lq);
else eval ${input_unit[1]}=$(echo ${input_unit[0]}*1024|bc -lq);
fi }
## Kb <> Byte
function Kb() { if [ ! ${input_unit[2]} ]
then eval ${input_unit[1]}=$(echo ${input_unit[0]}*1024|bc -lq);
else eval ${input_unit[1]}=$(echo "scale=0; ${input_unit[0]}/1024"|bc -lq);
fi }
## Mb <> Byte
function Mb() { if [ ! ${input_unit[2]} ]
then eval ${input_unit[1]}=$(echo ${input_unit[0]}*1024^2|bc -lq);
else eval ${input_unit[1]}=$(echo "scale=0; ${input_unit[0]}/1024^2"|bc -lq);
fi }
## Gb <> Byte
function Gb() { if [ ! ${input_unit[2]} ]
then eval ${input_unit[1]}=$(echo ${input_unit[0]}*1024^3|bc -lq);
else eval ${input_unit[1]}=$(echo "scale=0; ${input_unit[0]}/1024^3"|bc -lq);
fi }
## Tb <> Byte
function Tb() { if [ ! ${input_unit[2]} ]
then eval ${input_unit[1]}=$(echo ${input_unit[0]}*1024^4|bc -lq);
else eval ${input_unit[1]}=$(echo "scale=0; ${input_unit[0]}/1024^4"|bc -lq);
fi }
## -@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 message: ${reports[$rpt_index]}">>$log;
done
}
##--@F make all errors
function makeErr() {
if [[ "$option" == "--on" ]]
then
if [ ! "$params" ]
then
reports[${#reports[@]}]="${msg[0]}";
else
reports[${#reports[@]}]=${msg[42]};
fi
else
reports[${#reports[@]}]=${msg[42]};
fi
printInfo;
writeToLog;
exit 0;
}
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 $value_in|sed 's/--test//g'|wc -m) == 0"
"$(echo -n ${#reports[@]}) == 0" );
logic=( '"" "pInone"'
'"" "pIhelp"'
'"" "pItest"'
'"pIdf" "pInone"' );
function pInone() {
lEnd=0;
##clear
echo
echo -e ${msg[1]}"\n"${msg[2]}"\n"${msg[3]}"\n"${msg[4]}"\n"${msg[5]};
echo -e ${msg[52]}"\n"${msg[53]};
echo -e ${msg[31]}"\n"${msg[48]} $version;
exit 0;
}
function pIhelp() {
lEnd=0;
##clear
echo
echo -e ${msg[2]}"\n"${msg[3]}"\n"${msg[4]}"\n"${msg[5]};
echo -e ${msg[51]}"\n"${msg[52]}
echo -e ${msg[31]}"\n"${msg[48]} $version;
exit 0;
}
function pItest() {
lEnd=0;
info_free_mem=0;
info_total_mem=0;
input_unit=( "$free_mem" "info_free_mem" "1" );
Kb;
input_unit=( "$total_mem" "info_total_mem" "1" );
Kb;
echo "==============================================================...";
echo "${msg[32]} $info_total_mem""Mb ${msg[33]} $info_free_mem""Mb";
echo "==============================================================...";
for ((rpt_index=0; rpt_index != ${#reports[@]}; rpt_index++))
do
echo "${reports[$rpt_index]}";
done
echo "--------------------------------------------------------------...";
exit 0;
}
## default function last eXlogic..
function pIdf() {
for ((rpt_index=0; rpt_index != ${#reports[@]}; rpt_index++))
do
echo "${reports[$rpt_index]}";
done
}
eXlogic;
}
##--@F Check the program dependency
function checkDep() {
# This installation check $zr_jq
if [ $(echo $zr_jq|grep jq|wc -m) = 0 ]
then
## lang messages if yes then lang else us...
reports[${#reports[@]}]=${msg[6]};
makeErr
fi
# This installation check mdadm
if [ $(echo $zr_mdadm|grep mdadm|wc -m) = 0 ]
then
reports[${#reports[@]}]=${msg[8]};
makeErr
fi
if [ ! "$lang" ]
then
lang="C.UTF-8";
fi
}
##--@F read locale messages
function loadMsg() {
# - msg debug
echo "Please wait - load locales..";
# test enable file messages
if [ ! -f $zmsg ]
then
reports[${#reports[@]}]="$zmsg - file not found!";
makeErr;
fi
# read msg
if [[ ! $(cat $zmsg|grep "$lang"|wc -m) == 0 ]]
then
index_msg="$(cat $zmsg| jq '."'$lang'".msg[]'|grep ':'|sed 's/\://g;s/\"//g'|awk '{print$1}'|tail -n1)";
if [ ! "$index_msg" == "0" ];
then
for (( m_index=0; m_index != $index_msg; m_index++))
do
read_msg=$(cat $zmsg | jq '."'$lang'".msg['$m_index']."'$m_index'"'| sed 's/\"//g');
msg[$m_index]="$read_msg";
done
else
reports[${#reports[@]}]="$zmsg - Incorrect file format or empty!";
makeErr;
fi
fi
}
##--@F check module zram & kernel version
function checkModule() {
# test kernel version
iFs=( "${kernel_version[0]} < 3"
"${kernel_version[0]} == 3"
"${kernel_version[0]} == 4"
"$test_module == 0" );
logic=( '"" "notVersion"'
'"" "kernel3"'
'"" "kernel4"'
'"" "zramFalse"' );
function notVersion() {
lEnd=0;
reports[${#reports[@]}]="${msg[28]}";
makeErr;
}
function kernel3() {
kernel_status[${#kernel_status[@]}]="1";
if (( "${kernel_version[1]}" < "14" ))
then
lEnd=0;
reports[${#reports[@]}]="${msg[28]}";
makeErr;
fi
}
function kernel4() {
kernel_status[${#kernel_status[@]}]="1";
if (( "${kernel_version[1]}" > "2" ))
then
kernel_status[${#kernel_status[@]}]=1;
fi
}
# test module zram
function zramFalse() {
lEnd=0;
reports[${#reports[@]}]=${msg[27]};
makeErr;
}
eXlogic;
}
##--@F step operation
function confRead() {
# test enable file zramraid.conf
if [ ! -f $zconfig ]
then
reports=();
reports[${#reports[@]}]=${msg[7]};
makeErr;
fi
# read config
for (( rd_index=0; rd_index !=${#zdm_vars[@]}; rd_index++))
do
read_key=$(cat $zconfig | jq '.md[].'${json_key[$rd_index]}| sed 's/\"//g');
eval ${zdm_vars[$rd_index]}="(" $read_key ")";
done
log=$(cat $zconfig | jq '.log'| sed 's/\"//g');
## test free memory
value_mem=$(cat $zconfig | jq '.limit_free_mem.value_size'| sed 's/\"//g');
value_in="$value_mem";
iFs=( "$(echo -n $value_in|wc -m) == 0"
"$(echo -n $value_in|sed 's/%//g'|wc -m) == 0"
"$(echo -n $value_in|sed 's/Kb//g'|wc -m) == 0" );
logic=( '"" "lmem_Err"'
'"" "lmem_Per"'
'"lmem_Err" "lmem_Kb"' );
lmem_Err() {
reports=();
reports[${#reports[@]}]="${msg[29]} $value_mem /or null - ${msg[10]}(${msg[30]}:Kb,%)";
makeErr;
}
lmem_Per() {
lEnd=0;
test_read_mem=0;
input_unit=( "$free_mem" "test_read_mem" );
Kb;
math_unit=$(echo "scale=2; $(echo "scale=2; $test_read_mem/100"|bc -lq)*$(cat $zconfig | jq '.limit_free_mem.size'| sed 's/\"//g')"|bc -lq|sed 's/\./ /g'|awk '{print$1}');
## -"
if (( "$math_unit" < 0 ))||[[ "$math_unit" == "" ]]||[[ ! "$math_unit" =~ ^([0-9]+)$ ]]
then
reports=();
reports[${#reports[@]}]="${msg[29]} $value_mem /or null - ${msg[10]}(${msg[30]}:Kb,%)";
makeErr;
else
limit_mem=$math_unit;
input_unit=( "$math_unit" "info_limit_mem" );
Byte;
math_unit=$info_limit_mem;
input_unit=( "$math_unit" "info_limit_mem" "1" );
Kb;
fi
}
lmem_Kb() {
lEnd=0;
input_unit=( "$(cat $zconfig | jq '.limit_free_mem.size'| sed 's/\"//g')" "math_unit" );
Kb;
limit_mem=$math_unit;
input_unit=( "$math_unit" "info_limit_mem");
Mb;
}
eXlogic;
}
##--@F step operation
function testMd() {
# test the md number parameters for errors and busy in system
if [ ${#id_zmd[@]} -eq 0 ]
then reports[${#reports[@]}]="${msg[21]}${id_zmd[$md_index]} - ${msg[9]}";
makeErr;
fi
for (( md_index=0; md_index != ${#id_zmd[@]}; md_index++))
do
if (( "${id_zmd[$md_index]}" < 0 ))||[[ "${id_zmd[$md_index]}" == "" ]]||[[ ! "${id_zmd[$md_index]}" =~ ^([0-9]+)$ ]]
then reports[${#reports[@]}]="${msg[21]}${id_zmd[$md_index]} - ${msg[10]}";
makeErr;
fi
###
if [ "$($zr_blkid /dev/md${id_zmd[$md_index]}|grep ${id_zmd[$md_index]})" ]
then reports[${#reports[@]}]="${msg[21]}${id_zmd[$md_index]} - ${msg[11]}";
makeErr;
fi
done
# test the duble number md
test_dub=( $(echo -e ${id_zmd[@]}|tr " " "\n"|sort|tr "\n" " ") );
for (( dub_index=0; dub_index != ${#id_zmd[@]}; dub_index++ ))
do
dubtest_index=$(($dub_index+1));
if [[ ! $dubtest_index > $((${#id_zmd[@]}-1)) ]]
then
if [[ $((${test_dub[dubtest_index]}-${test_dub[dub_index]})) == 0 ]];
then reports[${#reports[@]}]="${msg[21]}${id_zmd[$dub_index]} - ${msg[12]}";
makeErr;
fi
fi
done
## ok test md*
if [ "$option" == "--test" ]
then
reports[${#reports[@]}]=${msg[25]};
fi
}
##--@F step operation
function testImage() {
function test_true() {
if [ "$option" == "--test" ]
then
reports[${#reports[@]}]=${msg[26]};
fi
}
function test_null() {
if [ "$option" == "--test" ]
then
reports[${#reports[@]}]=${msg[46]};
fi
}
function sum_img() {
## the sum size images
for (( image_index=0; image_index != ${#image_zmd[@]}; image_index++ ))
do
if [ ! -f ${image_zmd[$image_index]} ]
then reports[${#reports[@]}]="${msg[22]}${image_zmd[$image_index]} - ${msg[13]}";
makeErr
fi
size_zmd[$image_index]=$(stat -c%s ${image_zmd[$image_index]});
done
# test size image end size zram ...
for (( im_index=0; im_index != ${#image_zmd[@]}; im_index++ ))
do
total_size_images=$(($total_size_images+${size_zmd[$im_index]}));
done
}
function test_false() {
reports=();
reports[${#reports[@]}]="${msg[22]}$set_image - ${msg[14]}";
makeErr;
}
iFs=( "$(echo -n ${#image_zmd[@]}) > 0" );
logic=( '"test_null" "sum_img"' );
eXlogic;
iFs=( "$(echo -n $total_size_images) <= $limit_mem" );
logic=( '"test_false" "test_true"' );
eXlogic;
}
##--@F step operation
function reportConf() {
if (( $total_size_images > 1024 ))
then
input_unit=( "$total_size_images" "math_unit" "1" );
Kb;
rep_size="$math_unit""Kb";
else
rep_size="$total_size_images""b";
fi
reports[${#reports[@]}]="size images: $rep_size";
reports[${#reports[@]}]="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
reports[${#reports[@]}]="${msg[20]} $info_limit_mem Mb";
}
##--@F step operation
function createZraid(){
local check_start="0"
if [ "$(lsmod |grep zram|wc -m)" == 0 ]
then
modprobe zram num_devices=${#id_zmd[@]};
fi
for ((crt_index=0; crt_index != ${#id_zmd[@]}; crt_index++))
do
set_md_id=${id_zmd[$crt_index]};
singleTestMd;
local tstart_md=$(echo ${set_md_status[@]}|tr -d ' '|sed 's/\"//g');
if [[ "$tstart_md" == "" ]] || [[ ! "$tstart_md" =~ ^([0-9]+)$ ]] || (( "$tstart_md" < "0" ))
then
reports=();
reports[${#reports[@]}]=${msg[10]};
makeErr;
fi
if [[ "$tstart_md" == "001" ]]
then
check_start=$(echo $(($check_start+1)));
zblock_dev=$((${size_zmd[$crt_index]}));
echo $zblock_dev > /sys/block/zram$crt_index/disksize;
on_loop=$(losetup -f "${image_zmd[$crt_index]}" && losetup -a |grep "${image_zmd[$crt_index]}"|awk '{print$1}'|sed 's/\://g');
#
set_image_name=${image_zmd[$crt_index]};
prepareMd;
#
if [[ $set_prepare == "0" ]]
then
echo -e "y"|mdadm --create /dev/md${id_zmd[$crt_index]} --auto=yes --metadata=1.2 --level=raid1 --raid-devices=2 missing $on_loop;
echo -e "y"|mdadm --add /dev/md${id_zmd[$crt_index]} /dev/zram$crt_index;
else
echo -e "y"|mdadm --create /dev/md${id_zmd[$crt_index]} --assume-clean --auto=yes --metadata=1.2 --level=raid1 --raid-devices=2 missing $on_loop;
echo -e "y"|mdadm --add /dev/md${id_zmd[$crt_index]} /dev/zram$crt_index;
fi
fi
done
if (( "$check_start" >"0" ))
then
reports[${#reports[@]}]=${msg[16]};
writeToLog;
printInfo;
else
reports[${#reports[@]}]=${msg[50]};
writeToLog;
printInfo;
fi
}
##--@F step operation --------
function offZraid(){
local check_stop="0";
for ((off_index=0; off_index != ${#id_zmd[@]}; off_index++))
do
set_md_id=${id_zmd[$off_index]};
singleTestMd;
local tstop_md=$(echo ${set_md_status[@]}|tr -d ' '|sed 's/\"//g');
if [[ "$tstop_md" == "" ]] || [[ ! "$tstop_md" =~ ^([0-9]+)$ ]] || (( "$tstop_md" < "0" ))
then
reports=();
reports[${#reports[@]}]=${msg[10]};
makeErr;
fi
if [[ "$tstop_md" == "111" ]]||[[ "$tstop_md" == "101" ]]
then
check_stop=$(echo $(($check_stop+1)));
local off_set_loop=$(losetup -a |grep "${image_zmd[$off_index]}"|awk '{print$1}'|sed 's/\://g');
local off_set_zraid=$(cat /proc/mdstat |grep "md$set_md_id"|tail -n1|awk '{print$5}'|sed 's/\[2]//g');
mdadm -S /dev/md$set_md_id;
if [[ "$off_set_loop" ]]
then
losetup -d $off_set_loop;
fi
local kernel_type=$(echo ${kernel_status[@]}|tr -d ' '|sed 's/\:\"//g');
if [ "$kernel_type" == "11" ]
then
if [ "$(lsmod|grep zram|wc -m)" != "0" ]
then
echo $(echo $off_set_zraid|sed 's/\dev//g'|sed 's/\zram//g'|sed 's/\///g') > /sys/class/zram-control/hot_remove ;
fi
fi
fi
done
if [ $(lsmod|grep zram|wc -m) ]
then modprobe -r zram;
fi
if (( "$check_stop" >"0" ))
then
reports[${#reports[@]}]=${msg[17]};
writeToLog;
printInfo;
else
reports[${#reports[@]}]=${msg[19]};
writeToLog;
printInfo;
fi
}
##--@F step operation
function statusZraid(){
status=();
for ((st_index=0; st_index != ${#id_zmd[@]}; st_index++))
do
test_loop="$(cat /proc/mdstat |grep md${id_zmd[$st_index]}|awk '{print$6}'|cut -d'[' -f1)";
if [ "$test_loop" ]
then
test_zmd="$(sudo losetup -a |grep $test_loop|awk '{print$3}'|grep ${image_zmd[$st_index]}|wc -m)";
if [ "$test_zmd" ]
then status[${#status[@]}]="md${id_zmd[$st_index]}";
fi
fi
done
if [ $test_status != 0 ]
then
test_status=0;
if [ ! ${#status[@]} == 0 ]
then
reports[${#reports[@]}]="${msg[18]} ${status[@]}";
writeToLog;
printInfo;
exit 0;
else
reports[${#reports[@]}]=${msg[19]};
writeToLog;
printInfo;
exit 0;
fi
fi
}
##--@F step operation
function testId() {
if (( ! $(id|grep "root"|wc -m) ))
then
reports=();
reports[${#reports[@]}]="${msg[47]}";
makeErr;
fi
}
##............................................................................................
function singleStart() {
local tset_md=$(echo ${set_md_status[@]}|tr -d ' '|sed 's/\"//g');
if [[ "$tset_md" == "" ]] || [[ ! "$tset_md" =~ ^([0-9]+)$ ]] || (( "$tset_md" < "0" ))
then
reports=();
reports[${#reports[@]}]=${msg[10]};
makeErr;
fi
iFs=( "$tset_md == 001"
"$tset_md == 111"
"$tset_md == 100"
"$tset_md == 000" );
logic=( '"" "startSmd"'
'"" "noStartSmd"'
'"" "busSmd"'
'"" "noConfSmd"' );
function noStartSmd() {
lEnd=0;
reports=();
reports[${#reports[@]}]="md$set_md_id${msg[50]}";
printInfo;
exit 0;
}
function busSmd() {
lEnd=0;
reports=();
reports[${#reports[@]}]="md$set_md_id${msg[15]}";
printInfo;
exit 0;
}
function noConfSmd() {
lEnd=0;
reports=();
reports[${#reports[@]}]="md$set_md_id : ${msg[9]}";
printInfo;
exit 0;
}
function startSmd () {
lEnd=0;
local on_set_image="$(cat $zconfig|jq -c '.md[]|select(.id == "'$set_md_id'")|.image'|sed 's/\"//g' )";
local on_set_loop="$(losetup -f)";
losetup "$on_set_loop" "$on_set_image";
if (( $(lsmod|grep zram|wc -m) != 0 ))
then
cat /sys/class/zram-control/hot_add >/dev/nill ; ## add zram dev!
else
modprobe zram num_devices=1;
fi
local set_zram_id="$(echo $(($(ls /dev/zram*|sed 's/\dev//g'|sed 's/\zram//g'|sed 's/\///g'|tr -d '\r\n'|wc -c)-1)))";
local zset_block_dev=$(stat -c%s "$on_set_image");
echo $zset_block_dev > /sys/block/zram$set_zram_id/disksize;
#
set_image_name=$on_set_image;
prepareMd;
#
if [[ $set_prepare == "0" ]]
then
echo -e "y"|mdadm --create /dev/md$set_md_id --auto=yes --metadata=1.2 --level=raid1 --raid-devices=2 missing $on_set_loop;
echo -e "y"|mdadm --add /dev/md$set_md_id /dev/zram$set_zram_id;
else
echo -e "y"|mdadm --create /dev/md$set_md_id --assume-clean --auto=yes --metadata=1.2 --level=raid1 --raid-devices=2 missing $on_set_loop;
echo -e "y"|mdadm --add /dev/md$set_md_id /dev/zram$set_zram_id;
fi
}
eXlogic;
reports[${#reports[@]}]="${msg[43]} : start - /dev/md$set_md_id";
writeToLog;
echo -e "${reports[@]}";
}
##............................................................................................
function singleStop() {
local tset_md=$(echo ${set_md_status[@]}|tr -d ' '|sed 's/\"//g');
if [[ "$tset_md" == "" ]] || [[ ! "$tset_md" =~ ^([0-9]+)$ ]] || (( "$tset_md" < "0" ))
then
reports=();
reports[${#reports[@]}]=${msg[10]};
makeErr;
fi
iFs=( "$tset_md == 001"
"$tset_md == 111"
"$tset_md == 101"
"$tset_md == 100"
"$tset_md == 000" );
logic=( '"" "noStopSmd"'
'"" "stopSmd"'
'"" "stopSmd"'
'"" "sBusSmd"'
'"" "noConfSmd"' );
function noStopSmd() {
lEnd=0;
reports=();
reports[${#reports[@]}]="${msg[44]}md$set_md_id";
reports[${#reports[@]}]="${msg[42]}";
printInfo;
exit 0;
}
function sBusSmd() {
lEnd=0;
reports=();
reports[${#reports[@]}]="md$set_md_id${msg[15]}";
printInfo;
exit 0;
}
function noConfSmd() {
lEnd=0;
reports=();
reports[${#reports[@]}]="md$set_md_id : ${msg[9]}";
printInfo;
exit 0;
}
function stopSmd () {
lEnd=0;
local off_set_loop="/dev/$(ls /sys/block/"md$set_md_id"/md/|grep loop|sed 's/dev-//g')";
local off_set_zraid=$(cat /proc/mdstat |grep "md$set_md_id"|tail -n1|awk '{print$5}'|sed 's/\[2]//g');
mdadm -S /dev/md$set_md_id;
echo $(echo $off_set_zraid|sed 's/\dev//g'|sed 's/\zram//g'|sed 's/\///g') > /sys/class/zram-control/hot_remove;
if [[ $off_set_loop ]]
then
losetup -d $off_set_loop;
fi
}
eXlogic;
reports[${#reports[@]}]="${msg[43]} : stop - /dev/md$set_md_id";
writeToLog;
echo -e "${reports[@]}";
}
##...........................................................................................
function singleTestMd() {
local check_md=$(cat /proc/mdstat |grep "md$set_md_id"|tail -n 1|awk '{print$1}'|wc -m);
local check_zram=$(cat /proc/mdstat |grep "md$set_md_id"|grep zram|wc -m);
local check_conf=$(cat $zconfig|jq -c '.md[]."id"'|grep -e "$set_md_id"|sed 's/\"//g'|sed 's/^[ \t]*//;s/[ \t]*$//'|wc -m);
iFs=( "$check_md == 0"
"$check_conf == 0"
"$check_zram == 0" );
logic=( '"statusOn" "statusOff"' # test on MD
'"mDconfOn" "mDconfOff"' # test MD in conf
'"mDzramOn" "mDzramOff"' # test zram in MD
);
function statusOn() {
set_md_status[0]="1";
}
function statusOff() {
set_md_status[0]="0";
}
function mDzramOn() {
set_md_status[1]="1";
}
function mDzramOff() {
set_md_status[1]="0";
}
function mDconfOn() {
set_md_status[2]="1";
}
function mDconfOff() {
set_md_status[2]="0";
}
eXlogic;
}
##..........................................................................................
function prepareMd() {
local check_loop=$(losetup -a |grep "$set_image_name"|awk '{print$1}'|sed 's/\/dev\/\|s//g;s/\://g');
if (( $(cat /proc/mdstat |grep "$check_loop"|awk '{print$1}'|wc -m) != "0" ))
then
mdadm -S /dev/$(cat /proc/mdstat |grep "$check_loop"|awk '{print$1}');
set_prepare="1";
else
set_prepare="0";
fi
}
##...........................................................................................
function inParams() {
local kernel_type=$(echo ${kernel_status[@]}|tr -d ' ');
if (( "$kernel_type" != "11" ))
then
reports=();
reports[${#reports[@]}]="${msg[49]}";
makeErr;
fi
eval set_params="(" $(echo -e $params|sed 's/\md//g') ")";
if (( "${#set_params[@]}" >= "1" ))
then
set_md_id="${set_params[0]}";
if [[ "$set_md_id" == "" ]]||[[ ! "$set_md_id" =~ ^([0-9]+)$ ]]||(( "$set_md_id" <= "0" ))
then
reports=();
reports[${#reports[@]}]="${msg[10]}";
makeErr;
fi
else
reports=();
reports[${#reports[@]}]="${msg[10]}";
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
## on zramraid
"--on" | "--on" )
if [ ! "$params" ]
then
execute_func=( ${operation_on[@]} );
executor;
else
execute_func=( ${operation_sign_on[@]} );
executor;
fi
exit 0;
;;
## off zramraid
"--off" | "--off" )
if [ ! "$params" ]
then
execute_func=( ${operation_off[@]} );
executor;
else
execute_func=( ${operation_sign_off[@]} );
executor;
fi
exit 0;
;;
## status zramraid
"--status" | "--status" )
test_status=1;
execute_func=( ${operation_status[@]} );
executor;
exit 0;
;;
## test config
"--test" | "--test" )
execute_func=( ${operation_test[@]} );
executor;
exit 0;
;;
## help
"--help" | "--help" )
execute_func=( ${operation_help[@]} );
executor;
exit 0;
;;
* )
# selecting defaults.
execute_func=( ${operation_help[@]} );
executor;
exit 1;
;;
esac
+16
View File
@@ -0,0 +1,16 @@
{
"info": "id - md[id], image - is the path to the media to mount",
"log": "/var/log/syslog",
"limit_free_mem": {
"info": "size - the parameter 10-100% or 1024Kb...; Caution - the limit use free memory!",
"size": "90",
"value_size": "%"
},
"md": [
{
"id": "1",
"info": "'test disk 1'",
"image": "../kvm/disk1.img"
}
]
}
@@ -0,0 +1,21 @@
[Unit]
SourcePath=/etc/zramraid/zramraid-maker
Description=zram raid system
Before=libvirt-guests.service libvirtd.service runlevel2.target runlevel3.target runlevel4.target runlevel5.target shutdown.target
After=dbus.service iscsid.service apparmor.service local-fs.target
Conflicts=shutdown.target
[Service]
User=root
Group=root
Type=oneshot
GuessMainPID=no
RemainAfterExit=yes
WorkingDirectory=/etc/zramraid/
ExecStart=/etc/init.d/zramraid-manager start
ExecStop=/etc/init.d/zramraid-manager stop
ExecReload=/etc/init.d/zramraid-manager restart
TimeoutSec=5min
[Install]
WantedBy=multi-user.target
+269
View File
@@ -0,0 +1,269 @@
#!/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: GPLv3
option=$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 & 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() {
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
## lang messages if yes then lang else us...
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 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 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
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;
}
##--@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