Browse Source

the new version zramraid

root 5 years ago
commit
f73f365c59

File diff suppressed because it is too large
+ 65 - 0
readme.md


+ 5 - 0
zramraid.deb/etc/default/zramraid

@@ -0,0 +1,5 @@
1
+## service start
2
+# mode - startup parameter zraid system in manual or automatic mode, examples: mode="auto" or mode="manual"
3
+# if "manual" zraid does not start automatically at startup, dafult - "manual" 
4
+#
5
+mode="manual";

+ 70 - 0
zramraid.deb/etc/init.d/zramraid-manager

@@ -0,0 +1,70 @@
1
+#!/bin/bash
2
+### BEGIN INIT INFO
3
+# Provides:          zraid-manager
4
+# Required-Start:    udev $local_fs $remote_fs $time
5
+# Required-Stop:     $local_fs $remote_fs
6
+# Default-Start:     2 3 4 5
7
+# Default-Stop:      0 1 6
8
+# Short-Description: start the zraid
9
+# Description:       starts zraid using start-stop-daemon
10
+### END INIT INFO
11
+## version =25.06.18
12
+PARMS=$1
13
+export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
14
+DESC="Zram Raid"
15
+NAME="zramraid"
16
+SCRIPTNAME="zramraid-maker";
17
+ZPATH="/etc/zramraid"
18
+MODE="/etc/defaults/zramraid";
19
+##
20
+
21
+if [[ "$PARMS" = '' ]];
22
+ then
23
+    echo "/etc/init.d/zramraid-manager {start|stop|restart|status}"
24
+ exit 0
25
+fi
26
+
27
+function zramraidBoot() {
28
+if [ $(echo $(($(echo $USER|wc -m)-1))) == 0 ]
29
+    then
30
+    if [ $(cat $MODE |grep mode|grep -v '#'|sed 's/\mode=//g'|sed 's/\"//g;s/\;//g'|grep auto|wc -m) == 0 ]
31
+	then
32
+	    exit 0;
33
+	else
34
+	$ZPATH/$SCRIPTNAME --on
35
+    fi
36
+    echo "$NAME:Sorry.. The only root privileges!"
37
+    exit 0;
38
+fi
39
+}
40
+
41
+### begin
42
+case "$PARMS" in
43
+"start" | "start" )
44
+zramraidBoot;
45
+exit 0
46
+;;
47
+
48
+"stop" | "stop" )
49
+$ZPATH/$SCRIPTNAME --off
50
+exit 0
51
+;;
52
+
53
+"restart" | "restart" )
54
+$ZPATH/$SCRIPTNAME --off
55
+sleep 3
56
+zramraidBoot;
57
+exit 0
58
+;;
59
+
60
+"status" | "status" )
61
+$ZPATH/$SCRIPTNAME --status
62
+exit 0
63
+;;
64
+
65
+* )
66
+echo
67
+echo "no input parameters."
68
+exit 1
69
+;;
70
+esac

+ 119 - 0
zramraid.deb/etc/zramraid/messages.dat

@@ -0,0 +1,119 @@
1
+{
2
+"ru_RU.UTF-8":{
3
+	    "msg":[  
4
+	    "Система zraid не запущена!",
5
+	    "Нет параметров для zraid-maker!",
6
+	    "Полный запуск системы zraid: zraid-maker --on",
7
+	    "Полная остановка системы zraid: zraid-maker --off",
8
+	    "Состояние системы zraid: zraid-maker --status",
9
+	    "Проверка конфигрурации системы zraid: zraid-maker --test",
10
+	    "В системе отсутствует пакет jq!",
11
+	    "Отсутствует файл конфигурации zraid.conf!",
12
+	    "В системе отсутствует пакет mdadm!",
13
+	    "В конфигурации нет указанных md устройств!",
14
+	    "Неверный параметр в конфигурации!",
15
+	    "Занято системой!",
16
+	    "В конфигурации эти значения дублируются!",
17
+	    "Не найден файл!",
18
+	    "Общий обьем образов превышает установленный лимит памяти!",
19
+	    "Это устройство занято системой",
20
+	    "Старт системы zraid...",
21
+	    "Стоп системы zraid...",
22
+	    "zraid - включен и активированы устройства:",
23
+	    "zraid - отключен",
24
+	    "Лимит памяти:",
25
+	    "Ошибка устройства md:",
26
+	    "Ошибка образа:",
27
+	    "Общий размер образов:",
28
+	    "Чтение конфигурации - успешно",
29
+	    "Проверка устройств md в системе - успешно",
30
+	    "Проверка образов - успешно",
31
+	    "Не обнаружен модуль zram в ядре системы!",
32
+	    "Извините, старая версия ядра, рекомендуется 3.14.x и выше!",
33
+	    "Ошибочное значение",
34
+	    "Допускается только",
35
+	    "Помомощь: zraid-maker --help",
36
+	    "Памяти всего:",
37
+	    "Памяти свободно:",
38
+	    "Нет параметров для zraid-config!",
39
+	    "Создание пустого zraid.conf: zraid-config --config-default",
40
+	    "Добавление устройства MD: zraid-config --add md[x]:image - пример: md1:/home/zraid/disk1.img",
41
+	    "Удаление устройства MD: zraid-config --del md[x] - пример: md1",
42
+	    "Получение списка MD: zraid-config --list",
43
+	    "Параметр ограничения использования памяти: zraid-config --limit-mem [x] - пример: 90% or 1024Kb",
44
+	    "Параметр описание носителя: zraid-config --info-md md[x] - пример: md1",
45
+	    "Помощь: zraid-config --help",
46
+	    "Операция не выполнена",
47
+	    "Выполнено - успешно.",
48
+	    "Не найдено:",
49
+	    "Введите описаниe для устройства MD и нажмите Enter:",
50
+	    "Описание образов в конфигурации не найдено",
51
+	    "Доступно только с привилегиями root!",
52
+	    "Версия zraid:",
53
+	    "Извините, старая версия ядра, рекомендуется 4.2.x и выше для этого параметра!",
54
+	    "- устройство уже работает...",
55
+	    "версия ядра не позволяет выборочное управление носителями, только полная остановка и запуск, рекомендуется 4.2.x и выше.",
56
+	    "Индивидуальный запуск носителя zraid: zraid-maker --on md[x] - пример:md1",
57
+	    "Индивидульная остановка носителя zraid: zraid-maker --off md[x] - пример:md1"
58
+	    ]
59
+
60
+    },
61
+"C.UTF-8":{
62
+	    "msg":[
63
+	    "Not start zraid system!",
64
+	    "No input parameters for zraid-maker",
65
+	    "Full start arrays zraid: zraid-maker --on",
66
+	    "Full stop array zraid: zraid-maker --off",
67
+	    "State arrays zraid: zraid-maker --status",
68
+	    "Check config zraid arrays: zraid-maker --test",
69
+	    "Not found in the system package jq!",
70
+	    "Not found configuration file zraid.conf",
71
+	    "Not found in the system package mdadm!",
72
+	    "No specified md devices!",
73
+	    "The parameter is incorrect!",
74
+	    "Busy system!",
75
+	    "This number duplicated!",
76
+	    "Not found file!",
77
+	    "The total images exceeds the limit of available memory!",
78
+	    "This device busy a system!",
79
+	    "zraid start...",
80
+	    "zraid stop...",
81
+	    "zraid - on & enable devices:",
82
+	    "zraid - off",
83
+	    "limit mem:",
84
+	    "error: md",
85
+	    "error: image ",
86
+	    "total size images: ",
87
+	    "read config - done.",
88
+	    "test md from system - done.",
89
+	    "test images - done.",
90
+	    "zram module in the kernel can not be found!",
91
+	    "I'm sorry the old kernel version! recommended 3.14 or higher",
92
+	    "Error value",
93
+	    "Allowed only",
94
+	    "help: zraid-maker --help",
95
+	    "Memory total:",
96
+	    "Memory free:",
97
+	    "No input parameters for zraid-config!",
98
+	    "Create empty zraid.conf: zraid-config --config-default",
99
+	    "Add device MD in config: zraid-config --add md[x]:image - example: md1:/home/zraid/disk1.img",
100
+	    "Delete device MD in config: zraid-config --del md[x] - example: md1",
101
+	    "Listing devices MD in config: zraid-config --list",
102
+	    "The memory limit usage: zraid-config --limit-mem [x] - example: 90% or 1024Kb",
103
+	    "The set info for md device: zraid-config --info-md md[x] - example: md1",
104
+	    "Help: zraid-config --help",
105
+	    "Operation not succeded",
106
+	    "Operation succeded",
107
+	    "Not found:",
108
+	    "Enter the data for device MD description and press Enter:",
109
+	    "description of the images in the configuration are not found",
110
+	    "It is only available with root privileges",
111
+	    "version zraid:",
112
+	    "I'm sorry the old kernel version! recommended 4.2 or higher for this option",
113
+	    "-device is already working",
114
+	    "kernel version does not allow selective media management, only a complete stop and start, recommended 4.2 or higher for this option",
115
+	    "Selective startup zraid device: raid-maker --on md[x] - example: md1",
116
+	    "Selective stop zraid device: raid-maker --off md[x] - example: md1"
117
+	    ]
118
+    }
119
+}

+ 710 - 0
zramraid.deb/etc/zramraid/zramraid-config

@@ -0,0 +1,710 @@
1
+#!/bin/bash
2
+## zraid-config
3
+## (c) author's idea and realization: Kleemov A. & Koshuba V.
4
+## script author: Koshuba V - stvixfree@gmail.com
5
+## all parameters
6
+##settime=$(date +%s%3N);
7
+option=$1;
8
+params=$2;
9
+PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/etc/zraid";
10
+version="10.03.18";
11
+## msg [53]
12
+set_msg=( '"0" "0"' '"1" "0"' '"2" "0"' '"3" "0"' '"4" "0"' '"5" "0"' '"6" "1"' '"7" "1"' '"8" "1"' 
13
+	'"9" "1"' '"10" "1"' '"11" "1"' '"12" "1"'  '"13" "1"' '"14" "1"' '"15" "1"' '"16" "0"' 
14
+	'"17" "0"' '"18" "0"' '"19" "0"' '"20" "1"' '"21" "1"' '"22" "1"' '"23" "1"' '"24" "1"'
15
+	'"25" "1"' '"26" "1"' '"27" "1"' '"28" "1"' '"29" "1"' '"30" "1"' '"31" "1"' '"32" "1"'
16
+	'"33" "1"' '"34" "1"' '"35" "1"' '"36" "1"' '"37" "1"' '"38" "1"' '"39" "1"' '"40" "1"'
17
+	'"41" "1"' '"42" "1"' '"43" "1"' '"44" "1"' '"45" "1"' '"46" "1"' '"47" "1"' '"48" "1"'
18
+	'"49" "0"' '"50" "0"' '"51" "0"' '"52" "0"' '"53" "0"' );
19
+msg=();
20
+###
21
+lang=$(locale|grep LANG=|sed 's/\LANG=//g');
22
+rdate=$(date +%c);
23
+##
24
+total_mem=$(cat /proc/meminfo |awk '/^MemTotal:/{print $2}');
25
+free_mem=$(cat /proc/meminfo |awk '/^MemAvailable:/{print $2}');
26
+limit_mem=0;
27
+info_limit_mem=0;
28
+total_size_images=0;
29
+zpath="/etc/zraid";
30
+zconfig="zraid.conf";
31
+zmsg="/etc/zraid/messages.dat";
32
+log="/var/log/zraid.log";
33
+reports=();
34
+id_zmd=();
35
+image_zmd=();
36
+size_zmd=();
37
+json_key=( "id" "info" "image" );
38
+zdm_vars=( "id_zmd" "info_zmd" "image_zmd" );
39
+input_unit=();
40
+math_unit=0;
41
+set_params=();
42
+set_id="";
43
+set_image="";
44
+set_opt=( "0" "0" "0" "0" );
45
+set_size_limit=0;
46
+set_type_limit="";
47
+##
48
+zr_blkid=$(whereis -b blkid|awk '/^blkid:/{print $2}');
49
+zr_jq=$(whereis -b jq|awk '/^jq:/{print $2}');
50
+
51
+## functions & operations
52
+operation_createdef=( "checkDep" "loadMsg" "testId" "confEmpty" );
53
+operation_add=( "checkDep" "loadMsg" "testId" "inPut" "readConf" "testMd" "testImage" "addMd" );
54
+operation_del=( "checkDep" "loadMsg" "testId" "inPut" "readConf" "delMd" );
55
+operation_list=( "clear" "checkDep" "loadMsg" "testId" "readConf" "reportConf" "printInfo" );
56
+operation_help=( "clear" "checkDep" "loadMsg" "printInfo" );
57
+operation_mdinfo=( "clear" "checkDep" "loadMsg" "testId" "inPut" "readConf" "addInfoMd" );
58
+operation_mem=( "checkDep" "loadMsg" "testId" "inPut" "readConf" "setLimitMem" );
59
+execute_func=();
60
+
61
+## logic executor values
62
+iFs=();
63
+logic=();
64
+value_in="";
65
+lEnd=1;
66
+
67
+##--@F math functions
68
+## Byte <> Kb
69
+function Byte(){ if [ ! ${input_unit[2]} ]
70
+		then eval ${input_unit[1]}=$(echo "scale=0; ${input_unit[0]}/1024"|bc -lq);
71
+		else eval ${input_unit[1]}=$(echo ${input_unit[0]}*1024|bc -lq); 
72
+		fi }
73
+## Kb <> Byte
74
+function Kb() { if [ ! ${input_unit[2]} ] 
75
+		then eval ${input_unit[1]}=$(echo ${input_unit[0]}*1024|bc -lq);
76
+		else eval ${input_unit[1]}=$(echo "scale=0; ${input_unit[0]}/1024"|bc -lq);
77
+		fi }
78
+## Mb <> Byte
79
+function Mb() { if [ ! ${input_unit[2]} ]
80
+		then eval ${input_unit[1]}=$(echo ${input_unit[0]}*1024^2|bc -lq);
81
+		else eval ${input_unit[1]}=$(echo "scale=0; ${input_unit[0]}/1024^2"|bc -lq);
82
+		fi }
83
+## Gb <> Byte
84
+function Gb() { if [ ! ${input_unit[2]} ] 
85
+		then eval ${input_unit[1]}=$(echo ${input_unit[0]}*1024^3|bc -lq);
86
+		else eval ${input_unit[1]}=$(echo "scale=0; ${input_unit[0]}/1024^3"|bc -lq);
87
+		fi }
88
+## Tb <> Byte
89
+function Tb() { if [ ! ${input_unit[2]} ] 
90
+		then eval ${input_unit[1]}=$(echo ${input_unit[0]}*1024^4|bc -lq);
91
+		else eval ${input_unit[1]}=$(echo "scale=0; ${input_unit[0]}/1024^4"|bc -lq);
92
+		fi }
93
+
94
+## -@F logic executor
95
+function eXlogic() {
96
+lEnd=1;
97
+if [[ ${#iFs[@]} -eq 0 ]]||[[ ${#iFs[@]} != ${#logic[@]} ]]
98
+    then echo "exit";
99
+         exit 0;
100
+fi
101
+
102
+local exfunc=();
103
+for ((lg_index=0; lg_index != ${#iFs[@]}; lg_index++))
104
+ do
105
+    ## !! debug operation...
106
+    #echo "eXlogic = execution: function ${iFs[$lg_index]} : index=$lg_index";
107
+    local lg=$(echo $((${iFs[$lg_index]})) );
108
+    local exfunc=( ${logic[$lg_index]} );
109
+    local runfunc=$(echo ${exfunc[$lg]}| sed 's/\"//g');
110
+    $runfunc;
111
+    if [[ $lEnd == 0 ]] 
112
+	then lg_index=$((${#iFs[@]}-1)); 
113
+    fi
114
+done
115
+iFs=();
116
+logic=();
117
+value_in="";
118
+}
119
+
120
+##--@F write log events
121
+function writeToLog() {
122
+for ((rpt_index=0; rpt_index != ${#reports[@]}; rpt_index++))
123
+do
124
+    echo  "$rdate zraid message: ${reports[$rpt_index]}">>$log;
125
+done
126
+}
127
+
128
+##--@F make all errors
129
+function makeErr() {
130
+ reports[${#reports[@]}]=${msg[42]};
131
+ printInfo;
132
+ writeToLog;
133
+ exit 0;
134
+}
135
+
136
+function printInfo() {
137
+value_in="$option";
138
+iFs=(	"$(echo -n $value_in|wc -m) == 0"
139
+	"$(echo -n $value_in|sed 's/--help//g'|wc -m) == 0" 
140
+	"$(echo -n $value_in|sed 's/--list//g'|wc -m) == 0"
141
+	"$(echo -n ${#reports[@]}) == 0" );
142
+
143
+logic=( '"" "pInone"'
144
+	'"" "pIhelp"'
145
+	'"" "pIlist"'
146
+	'"pIdf" "pInone"' );
147
+
148
+
149
+function pInone() {
150
+    lEnd=0;
151
+    clear;
152
+    echo;
153
+    echo -e ${msg[34]}"\n"${msg[35]}"\n"${msg[36]}"\n"${msg[37]}"\n"${msg[38]}"\n"${msg[39]};
154
+    echo -e ${msg[40]}"\n"${msg[41]}"\n"${msg[48]} $version;
155
+    exit 0;
156
+}
157
+
158
+function pIhelp() {
159
+    lEnd=0;
160
+    clear;
161
+    echo;
162
+    echo -e ${msg[35]}"\n"${msg[36]}"\n"${msg[37]}"\n"${msg[38]}"\n"${msg[39]};
163
+    echo -e ${msg[40]}"\n"${msg[41]}"\n"${msg[48]} $version;
164
+    exit 0;
165
+}
166
+
167
+function pIlist() {
168
+    lEnd=0;
169
+    info_free_mem=0;
170
+    info_total_mem=0;
171
+    input_unit=( "$free_mem" "info_free_mem" "1" );
172
+    Kb;
173
+    input_unit=( "$total_mem" "info_total_mem" "1" );
174
+    Kb;
175
+    echo "==============================================================...";
176
+    echo "${msg[32]} $info_total_mem""Mb ${msg[33]} $info_free_mem""Mb";
177
+    echo "==============================================================...";
178
+    for ((rpt_index=0; rpt_index != ${#reports[@]}; rpt_index++))
179
+	do
180
+	echo  "${reports[$rpt_index]}";
181
+    done
182
+    echo "--------------------------------------------------------------...";
183
+    exit 0;
184
+}
185
+
186
+## default function last eXlogic..
187
+function pIdf() {
188
+    for ((rpt_index=0; rpt_index != ${#reports[@]}; rpt_index++))
189
+	do
190
+    echo   "${reports[$rpt_index]}";
191
+    done
192
+    exit 0;
193
+    }
194
+eXlogic;
195
+}
196
+
197
+##--@F Check the program dependency
198
+function checkDep() {
199
+# This installation check jq
200
+if [ $(echo $zr_jq|grep jq|wc -m) = 0 ]
201
+    then
202
+    ## lang messages if yes then lang else us...
203
+    reports=();
204
+    reports[${#reports[@]}]=${msg[6]};
205
+    makeErr;
206
+fi
207
+}
208
+
209
+##--@F read locale messages
210
+function loadMsg() {
211
+ # test enable file messages
212
+if [ ! -f $zmsg ]
213
+    then
214
+    reports=();
215
+    reports[${#reports[@]}]="$zmsg - file not found!";
216
+    makeErr;
217
+fi
218
+
219
+# read msg
220
+if [[ ! $(cat $zmsg|grep "$lang"|wc -m) == 0 ]]
221
+    then
222
+        index_msg="$(cat $zmsg | jq '."'$lang'".msg[]'|wc -l)";
223
+        if [[ ! "$index_msg" == "0" ]]||[[ "$index_msg" == "${#set_msg[@]}" ]]
224
+            then
225
+                for (( m_index=0; m_index != $index_msg; m_index++)) 
226
+                do
227
+                eval st_in_msg="(" ${set_msg[$m_index]} ")";
228
+                if [ "${st_in_msg[1]}" != "0" ]
229
+                    then
230
+                    read_msg=$(cat $zmsg | jq '."'$lang'".msg['$m_index']'| sed 's/\"//g');
231
+                    msg[$m_index]="$read_msg";
232
+                    else
233
+                    msg[$m_index]="";
234
+                fi
235
+                done
236
+            else
237
+                reports=();
238
+                reports[${#reports[@]}]="$zmsg - Incorrect file format or empty!";
239
+                makeErr;
240
+        fi
241
+    fi
242
+}
243
+
244
+##--@F step operation
245
+function readConf() {
246
+# test enable file zraid.conf
247
+if [ ! -f $zpath/$zconfig ]
248
+    then
249
+    reports=();
250
+    reports[${#reports[@]}]=${msg[7]};
251
+    makeErr;
252
+fi
253
+# read config
254
+for (( rd_index=0; rd_index !=${#zdm_vars[@]}; rd_index++)) 
255
+ do
256
+    read_key=$(cat $zpath/$zconfig | jq '.md[].'${json_key[$rd_index]}| sed 's/\"//g');
257
+    eval ${zdm_vars[$rd_index]}="(" $read_key ")";
258
+done
259
+log=$(cat $zpath/$zconfig | jq '.log'| sed 's/\"//g');
260
+
261
+## test free memory
262
+value_mem=$(cat $zpath/$zconfig | jq '.limit_free_mem.value_size'| sed 's/\"//g');
263
+
264
+value_in="$value_mem";
265
+iFs=(	"$(echo -n $value_in|wc -m) == 0"
266
+	"$(echo -n $value_in|sed 's/%//g'|wc -m) == 0" 
267
+	"$(echo -n $value_in|sed 's/Kb//g'|wc -m) == 0" );
268
+logic=( '"" "lmem_Err"'
269
+	'"" "lmem_Per"'
270
+	'"lmem_Err" "lmem_Kb"' );
271
+
272
+lmem_Err() {
273
+reports=();
274
+reports[${#reports[@]}]="${msg[29]} $value_mem /or null - ${msg[10]}(${msg[30]}:Kb,%)";
275
+makeErr;
276
+    }
277
+
278
+lmem_Per() {
279
+lEnd=0;
280
+test_read_mem=0;
281
+input_unit=( "$free_mem" "test_read_mem" );
282
+Kb;
283
+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}');
284
+## -"
285
+
286
+if (( "$math_unit" < 0 ))||[[ "$math_unit" == "" ]]||[[ ! "$math_unit" =~ ^([0-9]+)$ ]]
287
+	then
288
+	reports=();
289
+	reports[${#reports[@]}]="${msg[29]} $value_mem /or null - ${msg[10]}(${msg[30]}:Kb,%)";
290
+	makeErr;
291
+	else
292
+	limit_mem=$math_unit;
293
+	input_unit=( "$math_unit" "info_limit_mem" );
294
+	Byte;
295
+	math_unit=$info_limit_mem;
296
+	input_unit=( "$math_unit" "info_limit_mem" "1" );
297
+	Kb;
298
+fi
299
+    }
300
+
301
+lmem_Kb() {
302
+lEnd=0;
303
+input_unit=( "$(cat $zpath/$zconfig | jq '.limit_free_mem.size'| sed 's/\"//g')" "math_unit" );
304
+Kb;
305
+limit_mem=$math_unit;
306
+input_unit=( "$math_unit" "info_limit_mem");
307
+Mb;
308
+    }
309
+eXlogic;
310
+}
311
+
312
+
313
+function testMd() {
314
+# test the md number parameters for errors and busy in system for add md
315
+
316
+if (( "$set_id" < 0 ))||[[ "$set_id" == "" ]]||[[ ! "$set_id" =~ ^([0-9]+)$ ]]
317
+	then
318
+	reports=(); 
319
+	reports[${#reports[@]}]="${msg[21]}$set_id - ${msg[10]}";
320
+	makeErr;
321
+    fi
322
+    if [ "$($zr_blkid /dev/md$set_id|grep $set_id)" ]
323
+	then
324
+	reports=(); 
325
+	reports[${#reports[@]}]="${msg[21]}$set_id - ${msg[11]}";
326
+	makeErr;
327
+    fi
328
+# test  the duble number md from config
329
+test_dub=( $(echo -e  ${id_zmd[@]}|tr " " "\n"|sort|tr "\n" " ") );
330
+
331
+for (( dub_index=0; dub_index != ${#id_zmd[@]}; dub_index++ )) 
332
+ do
333
+	dubtest_index=$(($dub_index+1));
334
+	if [[ ! $dubtest_index > $((${#id_zmd[@]}-1)) ]]
335
+	    then 
336
+		if [[ $((${test_dub[$dubtest_index]}-${test_dub[$dub_index]})) == 0 ]];
337
+		 then
338
+		    ## duble from config
339
+		    reports=(); 
340
+		    reports[${#reports[@]}]="${msg[21]}${id_zmd[$(($dub_index+1))]} - ${msg[12]}";
341
+		    makeErr;
342
+		fi
343
+		
344
+	    fi
345
+
346
+	if [[ $((${test_dub[$dub_index]}-$set_id)) == 0 ]]
347
+	    then
348
+		## duble input end from config
349
+		    reports=(); 
350
+		    reports[${#reports[@]}]="${msg[21]}$set_id - ${msg[12]}";
351
+		    makeErr;
352
+		fi
353
+
354
+done
355
+
356
+}
357
+
358
+function testImage() {
359
+
360
+total_size_images=0;
361
+eval tmp_date="(" $(echo -e $set_image|sed 's,/,\, ,g'|cut -c 2-) ")";
362
+test_path=$(echo $set_image|sed 's,'${tmp_date[$((${#tmp_date[@]}-1))]}',,g');
363
+
364
+# test enable file images
365
+if [ ! -f $test_path${tmp_date[$((${#tmp_date[@]}-1))]} ]
366
+	    then
367
+	    reports=(); 
368
+	    reports[${#reports[@]}]="${msg[22]}$set_image - ${msg[13]}";
369
+	    makeErr;
370
+	fi
371
+
372
+function new_add() {
373
+test_size_images=$(stat -c%s $test_path${tmp_date[$((${#tmp_date[@]}-1))]});
374
+}
375
+
376
+function sum_img() {
377
+#echo "summ images...";
378
+## the sum size images 
379
+for (( image_index=0; image_index != ${#image_zmd[@]}; image_index++ )) 
380
+    do
381
+        if [ ! -f ${image_zmd[$image_index]} ]
382
+            then reports[${#reports[@]}]="${msg[22]}${image_zmd[$image_index]} - ${msg[13]}";
383
+        makeErr;
384
+        fi
385
+    size_zmd[$image_index]=$(stat -c%s ${image_zmd[$image_index]});
386
+    done
387
+# test size image end size zram ...
388
+for (( im_index=0; im_index != ${#image_zmd[@]}; im_index++ )) 
389
+    do
390
+        total_size_images=$(($total_size_images+${size_zmd[$im_index]}));
391
+    done
392
+size_new_image=$(stat -c%s $test_path${tmp_date[$((${#tmp_date[@]}-1))]});
393
+test_size_images=$(($total_size_images+$size_new_image));
394
+}
395
+
396
+function add_false() {
397
+    input_unit=( "$test_size_images" "math_unit" "1" );
398
+    Kb;
399
+    img_msg=$math_unit;
400
+    input_unit=( "$limit_mem" "math_unit" "1" );
401
+    Kb;
402
+    mem_msg=$math_unit;
403
+    reports=(); 
404
+    reports[${#reports[@]}]="${msg[22]}$set_image - ${msg[14]}";
405
+    reports[${#reports[@]}]="${msg[23]}$img_msg""Kb > ${msg[20]}$mem_msg""Kb";
406
+    makeErr;
407
+}
408
+iFs=( "$(echo -n ${#image_zmd[@]}) > 0" );
409
+logic=( '"new_add" "sum_img"' );
410
+eXlogic;
411
+
412
+iFs=( "$(echo -n $test_size_images) <= $limit_mem" );
413
+logic=( '"add_false" ""' );
414
+eXlogic;
415
+
416
+}
417
+
418
+## input parameters
419
+function inPut() {
420
+
421
+function inMd() {
422
+lEnd=0;
423
+eval set_params="(" $(echo -e $params|sed 's/:/ /') ")";
424
+
425
+if [ $(($(echo -n ${set_params[0]}|grep -o md|wc -m)-1)) == 2 ]
426
+    then  
427
+        set_id=$(echo -e ${set_params[0]}|awk '{print$1}'|sed 's/\md//g');
428
+        if [[ "$set_id" == "" ]]||[[ ! "$set_id" =~ ^([0-9]+)$ ]]||(( "$set_id" < 0 ))
429
+            then
430
+                reports=();
431
+                reports[${#reports[@]}]=${msg[10]};
432
+                makeErr;
433
+            else
434
+                reports=();
435
+                set_image=${set_params[1]};
436
+        fi
437
+    else
438
+        reports=();
439
+        reports[${#reports[@]}]=${msg[10]};
440
+        makeErr;
441
+fi
442
+}
443
+
444
+function inLimit() {
445
+lEnd=0;
446
+set_size=$(echo $params|sed 's/%//g'|sed 's/Kb//g');
447
+if [[ "$set_size" == "" ]]||[[ ! "$set_size" =~ ^([0-9]+)$ ]]||(( "$set_size" <= 0 ))
448
+    then 
449
+        reports=();
450
+        reports[${#reports[@]}]=${msg[29]};
451
+	reports[${#reports[@]}]=${msg[39]};
452
+        makeErr;
453
+    else
454
+    set_type=$(echo $params|sed 's/'"$set_size"'//g');
455
+    if [[ "$set_type" == "%" ]]||[[ "$set_type" == "Kb" ]]
456
+        then
457
+	set_size_limit=$set_size;
458
+	set_type_limit=$set_type;
459
+        else
460
+        reports=();
461
+        reports[${#reports[@]}]=${msg[29]};
462
+	reports[${#reports[@]}]=${msg[39]};
463
+        makeErr;
464
+        fi
465
+    fi
466
+}
467
+
468
+function noSwitch() {
469
+lEnd=0;
470
+reports=();
471
+    reports[${#reports[@]}]=${msg[30]};
472
+    reports[${#reports[@]}]=${msg[37]};
473
+    reports[${#reports[@]}]=${msg[38]};
474
+    reports[${#reports[@]}]=${msg[40]};
475
+    makeErr;
476
+}
477
+
478
+iFs=( "${set_opt[1]} == 1" "${set_opt[2]} == 1" "${set_opt[3]} == 1" "${set_opt[4]} == 1" );
479
+logic=( '"" "inMd"' '"" "inMd"' '"" "inLimit"' '"noSwitch" "inMd"' );
480
+eXlogic;
481
+}
482
+
483
+## create empty config
484
+function confEmpty() {
485
+
486
+function confNew(){
487
+        echo  '{'>$zpath/$zconfig
488
+        echo -e '"info":"id - md[id], image - is the path to the media to mount",\n'>>$zpath/$zconfig
489
+        echo -e '"log":"/var/log/syslog",\n'>>$zpath/$zconfig
490
+        echo  '"limit_free_mem":{'>>$zpath/$zconfig
491
+        echo  '"info":"size - the parameter 10-100% or 1024Kb...; Caution - the limit use free memory!",'>>$zpath/$zconfig
492
+        echo  '"size":"90",'>>$zpath/$zconfig
493
+        echo -e '"value_size":"%"},\n'>>$zpath/$zconfig
494
+        echo  '"md":['>>$zpath/$zconfig
495
+        echo  '   ]'>>$zpath/$zconfig
496
+        echo -e '}'>>$zpath/$zconfig
497
+    }
498
+
499
+if [ ! -f $zpath/$zconfig ]
500
+    then
501
+        confNew;
502
+    else
503
+        cp $zpath/$zconfig $zpath/$(date +%H_%M)"-zraid.old">/dev/null;
504
+        confNew;
505
+fi
506
+}
507
+
508
+function addMd() {
509
+local tdate=$(date +%j);
510
+cat $zpath/$zconfig|jq -c '.md|=.+[{"id":"'$set_id'","info":"","image":"'$set_image'"}]'|
511
+jq '.'>$zpath/$tdate$zconfig && cat $zpath/$tdate$zconfig>$zpath/$zconfig && rm -f $zpath/$tdate$zconfig;
512
+}
513
+
514
+## del md from config
515
+function delMd() {
516
+local tdate=$(date +%j);
517
+local del_value=$(cat $zpath/$zconfig|jq -c '.md[]|select (.id == "'$set_id'")');
518
+#'
519
+if [[ ! "$del_value" ]]
520
+	then 
521
+	    reports=();
522
+	reports[${#reports[@]}]="${msg[29]}: md$set_id - ${msg[9]}";
523
+	makeErr;
524
+	else
525
+	cat $zpath/$zconfig|jq -c '.md|=.-['$del_value']'|jq '.'>$zpath/$tdate$zconfig && cat $zpath/$tdate$zconfig>$zpath/$zconfig && rm -f $zpath/$tdate$zconfig;
526
+fi
527
+}
528
+
529
+## report config
530
+function reportConf() {
531
+	reports=();
532
+for (( report_index=0; report_index != ${#id_zmd[@]}; report_index++ ))
533
+ do
534
+    reports[${#reports[@]}]="config md: ${id_zmd[$report_index]}";
535
+    reports[${#reports[@]}]="info md: ${info_zmd[$report_index]}";
536
+    if (( $(stat -c%s ${image_zmd[$report_index]}) > 1024 ))
537
+	then
538
+	    input_unit=( "$(stat -c%s ${image_zmd[$report_index]})" "math_unit" "1" );
539
+	    Kb;
540
+	    rep_size="$math_unit""Kb";
541
+	else
542
+	    rep_size="$(stat -c%s ${image_zmd[$report_index]})b";
543
+	fi
544
+    reports[${#reports[@]}]="size md: $rep_size";
545
+    reports[${#reports[@]}]="image md: ${image_zmd[$report_index]}";
546
+    reports[${#reports[@]}]="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
547
+done
548
+	reports[${#reports[@]}]="${msg[20]} $info_limit_mem Mb";
549
+}
550
+
551
+## setting limit memory
552
+function setLimitMem() {
553
+local tdate=$(date +%j);
554
+test_set_mem=0;
555
+test_free_mem=0;
556
+
557
+if [[ "$set_type_limit" == "%" ]]
558
+    then
559
+	input_unit=( "$free_mem" "math_unit" );
560
+	Kb;
561
+	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}');
562
+	#-"
563
+	fi
564
+if [[ "$set_type_limit" == "Kb" ]]
565
+    then
566
+	math_unit=$set_size_limit;
567
+	input_unit=( "$math_unit" "test_set_mem" );
568
+	Kb;
569
+	fi
570
+if [[ ! "$test_set_mem" == 0 ]]
571
+    then
572
+	math_unit=$free_mem;
573
+	input_unit=( "$math_unit" "test_free_mem" );
574
+	Kb;
575
+    else
576
+	reports=();
577
+        reports[${#reports[@]}]=${msg[29]};
578
+	reports[${#reports[@]}]=${msg[39]};
579
+        makeErr;
580
+    fi
581
+
582
+if ((  "$test_free_mem" > "$test_set_mem" ))
583
+    then
584
+## add date
585
+local input_date_limit=$(cat $zpath/$zconfig|jq -c '.limit_free_mem|.size="'"$set_size_limit"'"|.value_size="'"$set_type_limit"'"');
586
+cat $zpath/$zconfig|jq -c ' del (.limit_free_mem)'|
587
+	jq '.'>$zpath/$tdate$zconfig && cat $zpath/$tdate$zconfig>$zpath/$zconfig && rm -f $zpath/$tdate$zconfig;
588
+## add
589
+cat $zpath/$zconfig|jq -c '.limit_free_mem|=.+'"$input_date_limit"''|
590
+        jq '.'>$zpath/$tdate$zconfig && cat $zpath/$tdate$zconfig>$zpath/$zconfig && rm -f $zpath/$tdate$zconfig;
591
+    else
592
+	reports=();
593
+	reports[${#reports[@]}]="${msg[20]}$set_size_limit$set_type_limit  > ${msg[33]}$free_mem""Kb";
594
+        reports[${#reports[@]}]=${msg[29]};
595
+	reports[${#reports[@]}]=${msg[39]};
596
+        makeErr;
597
+fi
598
+}
599
+
600
+## add info device md
601
+function addInfoMd() {
602
+if [[ $(cat $zpath/$zconfig|jq -c '.md[]|select(.id == "'$set_id'")') ]]
603
+#-'
604
+    then
605
+local Sinput=();
606
+clear
607
+echo "--------------------------------------------------";
608
+echo "${msg[45]}";
609
+echo "--------------------------------------------------";
610
+read Sinput;
611
+local tdate=$(date +%j);
612
+local data_info="$(cat $zpath/$zconfig|jq -c '.md[]|select(.id == "'$set_id'")|.info ="'"'$(echo ${Sinput[@]})'"'"')";
613
+delMd;
614
+cat $zpath/$zconfig|jq -c '.md|=.+['"$data_info"']'|
615
+        jq '.'>$zpath/$tdate$zconfig && cat $zpath/$tdate$zconfig>$zpath/$zconfig && rm -f $zpath/$tdate$zconfig;
616
+    else
617
+    reports=();
618
+    reports[${#reports[@]}]=${msg[9]};
619
+    makeErr;
620
+fi
621
+}
622
+
623
+##--@F step operation
624
+function testId() {
625
+if (( ! $(id|grep "root"|wc -m) ))
626
+        then
627
+        reports=(); 
628
+        reports[${#reports[@]}]="${msg[47]}";
629
+        makeErr;
630
+fi
631
+}
632
+
633
+##--@F executor
634
+function executor() {
635
+if [[ ${#execute_func[@]} -eq 0 ]] 
636
+    then echo "exit";
637
+	 exit 0; 
638
+fi
639
+for ((ex_index=0; ex_index != ${#execute_func[@]}; ex_index++))
640
+ do
641
+    ## !! debug operation...
642
+    ##echo "execution: function ${execute_func[ex_index]}"
643
+ ${execute_func[ex_index]};
644
+done
645
+
646
+}
647
+
648
+##- Begin ZRAID
649
+case "$option" in
650
+
651
+## create default config +
652
+"--config-default" | "--config-default" )
653
+execute_func=( ${operation_createdef[@]} );
654
+executor;
655
+exit 0
656
+;; 
657
+
658
+## add md
659
+"--add" | "--add" )
660
+set_opt[1]="1";
661
+execute_func=( ${operation_add[@]} );
662
+executor;
663
+exit 0
664
+;;
665
+
666
+## del md
667
+"--del" | "--del" )
668
+set_opt[2]="1";
669
+execute_func=( ${operation_del[@]} );
670
+executor;
671
+exit 0
672
+;;
673
+
674
+## list md +
675
+"--list" | "--list" )
676
+execute_func=( ${operation_list[@]} );
677
+executor;
678
+exit 0
679
+;;
680
+
681
+## limit memory
682
+"--limit-mem" | "--limit-mem" )
683
+set_opt[3]="1";
684
+execute_func=( ${operation_mem[@]} );
685
+executor;
686
+exit 0
687
+;;
688
+
689
+## md info
690
+"--info-md" | "--info-md" )
691
+set_opt[4]="1";
692
+execute_func=( ${operation_mdinfo[@]} );
693
+executor;
694
+exit 0
695
+;;
696
+
697
+## help +
698
+"--help" | "--help" )
699
+execute_func=( ${operation_help[@]} );
700
+executor;
701
+exit 0
702
+;;
703
+
704
+* )
705
+# selecting defaults. +
706
+execute_func=( ${operation_help[@]} );
707
+executor;
708
+exit 1
709
+;;
710
+esac

+ 930 - 0
zramraid.deb/etc/zramraid/zramraid-maker

@@ -0,0 +1,930 @@
1
+#!/bin/bash
2
+## zraid-maker
3
+## (c) author's idea and realization: Kleemov A. & Koshuba V.
4
+## script author: Koshuba V - stvixfree@gmail.com
5
+## all parameters
6
+option=$1;
7
+params=$2;
8
+PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/etc/zraid";
9
+version="10.03.18";
10
+## msg [53]
11
+set_msg=( '"0" "1"' '"1" "1"' '"2" "1"' '"3" "1"' '"4" "1"' '"5" "1"' '"6" "1"' '"7" "1"' '"8" "1"' 
12
+        '"9" "1"' '"10" "1"' '"11" "1"' '"12" "1"'  '"13" "1"' '"14" "1"' '"15" "1"' '"16" "1"' 
13
+        '"17" "1"' '"18" "1"' '"19" "1"' '"20" "1"' '"21" "1"' '"22" "1"' '"23" "1"' '"24" "1"'
14
+        '"25" "1"' '"26" "1"' '"27" "1"' '"28" "1"' '"29" "1"' '"30" "1"' '"31" "1"' '"32" "1"'
15
+        '"33" "1"' '"34" "0"' '"35" "0"' '"36" "0"' '"37" "0"' '"38" "0"' '"39" "0"' '"40" "0"'
16
+        '"41" "0"' '"42" "1"' '"43" "1"' '"44" "1"' '"45" "0"' '"46" "1"' '"47" "1"' '"48" "1"'
17
+        '"49" "1"' '"50" "1"' '"51" "1"' '"52" "1"' '"53" "1"' );
18
+msg=();
19
+
20
+##
21
+lang=$(locale|grep LANG=|sed 's/\LANG=//g');
22
+rdate=$(date +%c);
23
+eval kernel_version="(" $(uname -r|sed 's/\./ /g'|awk '{print$1" "$2}') ")";
24
+test_module=$(find /lib/modules/$(uname -r) -name '*.ko'|grep zram|wc -m);
25
+##
26
+total_mem=$(cat /proc/meminfo |awk '/^MemTotal:/{print $2}');
27
+free_mem=$(cat /proc/meminfo |awk '/^MemAvailable:/{print $2}');
28
+limit_mem=0;
29
+info_limit_mem=0;
30
+zpath="/etc/zraid";
31
+zconfig="/etc/zraid/zraid.conf";
32
+zmsg="/etc/zraid/messages.dat";
33
+log="/var/log/zraid.log";
34
+##
35
+reports=();
36
+id_zmd=();
37
+image_zmd=();
38
+size_zmd=();
39
+json_key=( "id" "image" );
40
+zdm_vars=( "id_zmd" "image_zmd" );
41
+input_unit=();
42
+math_unit=0;
43
+total_size_images=0;
44
+test_status=0;
45
+kernel_status=();									# [0]-kernel3x,[1]-kernel4x
46
+##
47
+set_md_id="";
48
+set_zram_id="";
49
+set_md_status=();									# [0]-on\off,[1]-load zram,[2]-in config
50
+set_prepare="";										# 0 - new md, 1 - md assemble
51
+##
52
+zr_blkid=$(whereis -b blkid|awk '/^blkid:/{print $2}');
53
+zr_jq=$(whereis -b jq|awk '/^jq:/{print $2}');
54
+zr_mdadm=$(whereis -b mdadm|awk '/^mdadm:/{print $2}');
55
+
56
+## functions & operations
57
+operation_test=( "clear" "checkDep" "loadMsg" "testId" "checkModule" "confRead" "testMd" "testImage" "reportConf" "printInfo" );
58
+operation_on=( "checkDep" "loadMsg" "testId" "confRead" "checkModule" "testMd" "testImage" "statusZraid" "createZraid" );
59
+operation_sign_on=(  "checkDep" "loadMsg" "testId" "confRead" "checkModule" "testImage" "inParams" "singleTestMd" "singleStart" );
60
+operation_sign_off=(  "checkDep" "loadMsg" "testId" "confRead" "checkModule" "testImage" "inParams" "singleTestMd" "singleStop" );
61
+operation_off=( "checkDep" "loadMsg" "testId" "confRead" "checkModule" "offZraid" );
62
+operation_status=( "checkDep" "loadMsg" "testId" "confRead" "statusZraid" );
63
+operation_help=( "clear" "checkDep" "loadMsg" "printInfo" );
64
+execute_func=();
65
+
66
+## logic executor values
67
+iFs=();
68
+logic=();
69
+value_in="";
70
+lEnd=1;
71
+
72
+##--@F math functions
73
+## Byte <> Kb
74
+function Byte(){ if [ ! ${input_unit[2]} ]
75
+		then eval ${input_unit[1]}=$(echo "scale=0; ${input_unit[0]}/1024"|bc -lq);
76
+		else eval ${input_unit[1]}=$(echo ${input_unit[0]}*1024|bc -lq); 
77
+		fi }
78
+## Kb <> Byte
79
+function Kb() { if [ ! ${input_unit[2]} ] 
80
+		then eval ${input_unit[1]}=$(echo ${input_unit[0]}*1024|bc -lq);
81
+		else eval ${input_unit[1]}=$(echo "scale=0; ${input_unit[0]}/1024"|bc -lq);
82
+		fi }
83
+## Mb <> Byte
84
+function Mb() { if [ ! ${input_unit[2]} ]
85
+		then eval ${input_unit[1]}=$(echo ${input_unit[0]}*1024^2|bc -lq);
86
+		else eval ${input_unit[1]}=$(echo "scale=0; ${input_unit[0]}/1024^2"|bc -lq);
87
+		fi }
88
+## Gb <> Byte
89
+function Gb() { if [ ! ${input_unit[2]} ] 
90
+		then eval ${input_unit[1]}=$(echo ${input_unit[0]}*1024^3|bc -lq);
91
+		else eval ${input_unit[1]}=$(echo "scale=0; ${input_unit[0]}/1024^3"|bc -lq);
92
+		fi }
93
+## Tb <> Byte
94
+function Tb() { if [ ! ${input_unit[2]} ] 
95
+		then eval ${input_unit[1]}=$(echo ${input_unit[0]}*1024^4|bc -lq);
96
+		else eval ${input_unit[1]}=$(echo "scale=0; ${input_unit[0]}/1024^4"|bc -lq);
97
+		fi }
98
+
99
+## -@F logic executor
100
+function eXlogic() {
101
+lEnd=1;
102
+if [[ ${#iFs[@]} -eq 0 ]]||[[ ${#iFs[@]} != ${#logic[@]} ]]
103
+    then echo "exit";
104
+         exit 0;
105
+fi
106
+
107
+local exfunc=();
108
+for ((lg_index=0; lg_index != ${#iFs[@]}; lg_index++))
109
+ do
110
+    ## !! debug operation...
111
+    ##echo "eXlogic = execution: function ${iFs[$lg_index]} : index=$lg_index";
112
+    local lg=$(echo $((${iFs[$lg_index]})) );
113
+    local exfunc=( ${logic[$lg_index]} );
114
+    local runfunc=$(echo ${exfunc[$lg]}| sed 's/\"//g');
115
+    $runfunc;
116
+    if [[ $lEnd == 0 ]] 
117
+        then lg_index=$((${#iFs[@]}-1)); 
118
+    fi
119
+done
120
+iFs=();
121
+logic=();
122
+value_in="";
123
+}
124
+
125
+##--@F write log events
126
+function writeToLog() {
127
+for ((rpt_index=0; rpt_index != ${#reports[@]}; rpt_index++))
128
+do
129
+    echo  "$rdate zraid message: ${reports[$rpt_index]}">>$log;
130
+done
131
+}
132
+
133
+##--@F make all errors
134
+function makeErr() {
135
+if [[ "$option" == "--on" ]]
136
+    then
137
+	if [ ! "$params" ]
138
+	    then
139
+	    reports[${#reports[@]}]="${msg[0]}";
140
+	    else
141
+	    reports[${#reports[@]}]=${msg[42]};
142
+	fi
143
+    else
144
+	reports[${#reports[@]}]=${msg[42]};
145
+fi
146
+
147
+ printInfo;
148
+ writeToLog;
149
+ exit 0;
150
+}
151
+
152
+function printInfo() {
153
+value_in="$option";
154
+iFs=(   "$(echo -n $value_in|wc -m) == 0"
155
+        "$(echo -n $value_in|sed 's/--help//g'|wc -m) == 0" 
156
+        "$(echo -n $value_in|sed 's/--test//g'|wc -m) == 0"
157
+        "$(echo -n ${#reports[@]}) == 0" );
158
+
159
+logic=( '"" "pInone"'
160
+        '"" "pIhelp"'
161
+        '"" "pItest"'
162
+        '"pIdf" "pInone"' );
163
+
164
+
165
+function pInone() {
166
+    lEnd=0;
167
+    ##clear
168
+    echo
169
+    echo -e ${msg[1]}"\n"${msg[2]}"\n"${msg[3]}"\n"${msg[4]}"\n"${msg[5]};
170
+    echo -e ${msg[52]}"\n"${msg[53]};
171
+    echo -e ${msg[31]}"\n"${msg[48]} $version;
172
+    exit 0;
173
+}
174
+
175
+function pIhelp() {
176
+    lEnd=0;
177
+    ##clear
178
+    echo
179
+    echo -e ${msg[2]}"\n"${msg[3]}"\n"${msg[4]}"\n"${msg[5]};
180
+    echo -e ${msg[51]}"\n"${msg[52]}
181
+    echo -e ${msg[31]}"\n"${msg[48]}  $version;
182
+    exit 0;
183
+}
184
+
185
+function pItest() {
186
+    lEnd=0;
187
+    info_free_mem=0;
188
+    info_total_mem=0;
189
+    input_unit=( "$free_mem" "info_free_mem" "1" );
190
+    Kb;
191
+    input_unit=( "$total_mem" "info_total_mem" "1" );
192
+    Kb;
193
+    echo "==============================================================...";
194
+    echo "${msg[32]} $info_total_mem""Mb ${msg[33]} $info_free_mem""Mb";
195
+    echo "==============================================================...";
196
+    for ((rpt_index=0; rpt_index != ${#reports[@]}; rpt_index++))
197
+        do
198
+        echo  "${reports[$rpt_index]}";
199
+    done
200
+    echo "--------------------------------------------------------------...";
201
+    exit 0;
202
+}
203
+
204
+## default function last eXlogic..
205
+function pIdf() {
206
+    for ((rpt_index=0; rpt_index != ${#reports[@]}; rpt_index++))
207
+        do
208
+    echo   "${reports[$rpt_index]}";
209
+    done
210
+    }
211
+eXlogic;
212
+}
213
+
214
+##--@F Check the program dependency
215
+function checkDep() {
216
+# This installation check $zr_jq
217
+if [ $(echo $zr_jq|grep jq|wc -m) = 0 ]
218
+    then
219
+    ## lang messages if yes then lang else us...
220
+    reports[${#reports[@]}]=${msg[6]};
221
+    makeErr
222
+fi
223
+# This installation check mdadm
224
+if [ $(echo $zr_mdadm|grep mdadm|wc -m) = 0 ]
225
+    then
226
+    reports[${#reports[@]}]=${msg[8]};
227
+    makeErr
228
+fi
229
+
230
+if [ ! "$lang" ]
231
+    then
232
+    lang="C.UTF-8";
233
+fi
234
+
235
+}
236
+
237
+##--@F read locale messages
238
+function loadMsg() {
239
+ # test enable file messages
240
+if [ ! -f $zmsg ]
241
+    then
242
+    reports=();
243
+    reports[${#reports[@]}]="$zmsg - file not found!";
244
+    makeErr;
245
+fi
246
+
247
+# read msg
248
+if [[ ! $(cat $zmsg|grep "$lang"|wc -m) == 0 ]]
249
+    then
250
+        index_msg="$(cat $zmsg | jq '."'$lang'".msg[]'|wc -l)";
251
+        if [[ ! "$index_msg" == "0" ]]||[[ "$index_msg" == "${#set_msg[@]}" ]]
252
+            then
253
+                for (( m_index=0; m_index != $index_msg; m_index++)) 
254
+                do
255
+                eval st_in_msg="(" ${set_msg[$m_index]} ")";
256
+                if [ "${st_in_msg[1]}" != "0" ]
257
+                    then
258
+                    read_msg=$(cat $zmsg | jq '."'$lang'".msg['$m_index']'| sed 's/\"//g');
259
+                    msg[$m_index]="$read_msg";
260
+                    else
261
+                    msg[$m_index]="";
262
+                fi
263
+                done
264
+            else
265
+                reports=();
266
+                reports[${#reports[@]}]="$zmsg - Incorrect file format or empty!";
267
+                makeErr;
268
+        fi
269
+    fi
270
+}
271
+
272
+##--@F check module zram & kernel version
273
+function checkModule() {
274
+# test kernel version
275
+iFs=(   "${kernel_version[0]} < 3"
276
+        "${kernel_version[0]} == 3" 
277
+	"${kernel_version[0]} == 4" 
278
+        "$test_module == 0" );
279
+logic=( '"" "notVersion"'
280
+        '"" "kernel3"'
281
+        '"" "kernel4"'
282
+	'"" "zramFalse"' );
283
+
284
+function notVersion() {
285
+	lEnd=0;
286
+        reports[${#reports[@]}]="${msg[28]}";
287
+	makeErr;
288
+}
289
+
290
+function kernel3() {
291
+kernel_status[${#kernel_status[@]}]="1";
292
+	    if (( "${kernel_version[1]}" < "14" ))
293
+		then
294
+		lEnd=0;
295
+		reports[${#reports[@]}]="${msg[28]}";
296
+		makeErr;
297
+	    fi
298
+}
299
+
300
+function kernel4() {
301
+kernel_status[${#kernel_status[@]}]="1";
302
+	    if (( "${kernel_version[1]}" > "2" ))
303
+		then
304
+		kernel_status[${#kernel_status[@]}]=1;
305
+	    fi
306
+}
307
+
308
+# test module zram
309
+function zramFalse() {
310
+    lEnd=0;
311
+    reports[${#reports[@]}]=${msg[27]};
312
+    makeErr;
313
+}
314
+
315
+eXlogic;
316
+}
317
+
318
+##--@F step operation
319
+function confRead() {
320
+# test enable file zraid.conf
321
+if [ ! -f $zconfig ]
322
+    then
323
+    reports=();
324
+    reports[${#reports[@]}]=${msg[7]};
325
+    makeErr;
326
+fi
327
+# read config
328
+for (( rd_index=0; rd_index !=${#zdm_vars[@]}; rd_index++)) 
329
+ do
330
+    read_key=$(cat $zconfig | jq '.md[].'${json_key[$rd_index]}| sed 's/\"//g');
331
+    eval ${zdm_vars[$rd_index]}="(" $read_key ")";
332
+done
333
+log=$(cat $zconfig | jq '.log'| sed 's/\"//g');
334
+
335
+## test free memory
336
+value_mem=$(cat $zconfig | jq '.limit_free_mem.value_size'| sed 's/\"//g');
337
+
338
+value_in="$value_mem";
339
+iFs=(   "$(echo -n $value_in|wc -m) == 0"
340
+        "$(echo -n $value_in|sed 's/%//g'|wc -m) == 0" 
341
+        "$(echo -n $value_in|sed 's/Kb//g'|wc -m) == 0" );
342
+logic=( '"" "lmem_Err"'
343
+        '"" "lmem_Per"'
344
+        '"lmem_Err" "lmem_Kb"' );
345
+
346
+lmem_Err() {
347
+reports=();
348
+reports[${#reports[@]}]="${msg[29]} $value_mem /or null - ${msg[10]}(${msg[30]}:Kb,%)";
349
+makeErr;
350
+    }
351
+
352
+lmem_Per() {
353
+lEnd=0;
354
+test_read_mem=0;
355
+input_unit=( "$free_mem" "test_read_mem" );
356
+Kb;
357
+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}');
358
+## -"
359
+
360
+if (( "$math_unit" < 0 ))||[[ "$math_unit" == "" ]]||[[ ! "$math_unit" =~ ^([0-9]+)$ ]]
361
+        then
362
+        reports=();
363
+        reports[${#reports[@]}]="${msg[29]} $value_mem /or null - ${msg[10]}(${msg[30]}:Kb,%)";
364
+        makeErr;
365
+        else
366
+        limit_mem=$math_unit;
367
+        input_unit=( "$math_unit" "info_limit_mem" );
368
+        Byte;
369
+        math_unit=$info_limit_mem;
370
+        input_unit=( "$math_unit" "info_limit_mem" "1" );
371
+        Kb;
372
+fi
373
+    }
374
+
375
+lmem_Kb() {
376
+lEnd=0;
377
+input_unit=( "$(cat $zconfig | jq '.limit_free_mem.size'| sed 's/\"//g')" "math_unit" );
378
+Kb;
379
+limit_mem=$math_unit;
380
+input_unit=( "$math_unit" "info_limit_mem");
381
+Mb;
382
+    }
383
+eXlogic;
384
+}
385
+
386
+##--@F step operation
387
+function testMd() {
388
+# test the md number parameters for errors and busy in system
389
+if [ ${#id_zmd[@]} -eq 0 ]
390
+    then reports[${#reports[@]}]="${msg[21]}${id_zmd[$md_index]} - ${msg[9]}";
391
+    makeErr;
392
+fi
393
+
394
+for (( md_index=0; md_index != ${#id_zmd[@]}; md_index++)) 
395
+ do
396
+    if (( "${id_zmd[$md_index]}" < 0 ))||[[ "${id_zmd[$md_index]}" == "" ]]||[[ ! "${id_zmd[$md_index]}" =~ ^([0-9]+)$ ]]
397
+	then reports[${#reports[@]}]="${msg[21]}${id_zmd[$md_index]} - ${msg[10]}";
398
+	makeErr;
399
+    fi
400
+###
401
+    if [ "$($zr_blkid /dev/md${id_zmd[$md_index]}|grep ${id_zmd[$md_index]})" ]
402
+	then reports[${#reports[@]}]="${msg[21]}${id_zmd[$md_index]} - ${msg[11]}";
403
+	makeErr;
404
+    fi
405
+done
406
+# test  the duble number md
407
+test_dub=( $(echo -e  ${id_zmd[@]}|tr " " "\n"|sort|tr "\n" " ") );
408
+for (( dub_index=0; dub_index != ${#id_zmd[@]}; dub_index++ )) 
409
+ do
410
+	dubtest_index=$(($dub_index+1));
411
+	if [[ ! $dubtest_index > $((${#id_zmd[@]}-1)) ]] 
412
+	    then 
413
+		if [[ $((${test_dub[dubtest_index]}-${test_dub[dub_index]})) == 0 ]];
414
+		 then reports[${#reports[@]}]="${msg[21]}${id_zmd[$dub_index]} - ${msg[12]}";
415
+		    makeErr;
416
+		fi
417
+	    fi
418
+done
419
+## ok test md*
420
+if [ "$option" == "--test" ]
421
+    then
422
+	reports[${#reports[@]}]=${msg[25]};
423
+fi
424
+}
425
+
426
+##--@F step operation
427
+function testImage() {
428
+
429
+function test_true() {
430
+if [ "$option" == "--test" ]
431
+    then
432
+	reports[${#reports[@]}]=${msg[26]};
433
+fi
434
+}
435
+
436
+function test_null() {
437
+if [ "$option" == "--test" ]
438
+    then
439
+reports[${#reports[@]}]=${msg[46]};
440
+fi
441
+}
442
+
443
+function sum_img() {
444
+## the sum size images 
445
+for (( image_index=0; image_index != ${#image_zmd[@]}; image_index++ )) 
446
+    do
447
+        if [ ! -f ${image_zmd[$image_index]} ]
448
+            then reports[${#reports[@]}]="${msg[22]}${image_zmd[$image_index]} - ${msg[13]}";
449
+        makeErr
450
+        fi
451
+    size_zmd[$image_index]=$(stat -c%s ${image_zmd[$image_index]});
452
+    done
453
+# test size image end size zram ...
454
+for (( im_index=0; im_index != ${#image_zmd[@]}; im_index++ )) 
455
+    do
456
+        total_size_images=$(($total_size_images+${size_zmd[$im_index]}));
457
+    done
458
+}
459
+
460
+function test_false() {
461
+    reports=(); 
462
+    reports[${#reports[@]}]="${msg[22]}$set_image - ${msg[14]}";
463
+    makeErr;
464
+}
465
+iFs=( "$(echo -n ${#image_zmd[@]}) > 0" );
466
+logic=( '"test_null" "sum_img"' );
467
+eXlogic;
468
+
469
+iFs=( "$(echo -n $total_size_images) <= $limit_mem" );
470
+logic=( '"test_false" "test_true"' );
471
+eXlogic;
472
+
473
+}
474
+
475
+##--@F step operation
476
+function reportConf() {
477
+    if (( $total_size_images > 1024 ))
478
+        then
479
+            input_unit=( "$total_size_images" "math_unit" "1" );
480
+            Kb;
481
+            rep_size="$math_unit""Kb";
482
+        else
483
+            rep_size="$total_size_images""b";
484
+        fi
485
+    reports[${#reports[@]}]="size images: $rep_size";
486
+    reports[${#reports[@]}]="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
487
+    reports[${#reports[@]}]="${msg[20]} $info_limit_mem Mb";
488
+}
489
+
490
+##--@F step operation
491
+function createZraid(){
492
+local check_start="0"
493
+if [ "$(lsmod |grep zram|wc -m)" == 0 ]
494
+    then
495
+    modprobe zram num_devices=${#id_zmd[@]};
496
+    fi
497
+
498
+for ((crt_index=0; crt_index != ${#id_zmd[@]}; crt_index++))
499
+    do
500
+set_md_id=${id_zmd[$crt_index]};
501
+singleTestMd;
502
+
503
+local tstart_md=$(echo ${set_md_status[@]}|tr -d ' '|sed 's/\"//g');
504
+if [[ "$tstart_md" == "" ]] || [[ ! "$tstart_md" =~ ^([0-9]+)$ ]] || (( "$tstart_md" < "0" ))
505
+	then
506
+	    reports=();
507
+            reports[${#reports[@]}]=${msg[10]};
508
+            makeErr;
509
+fi
510
+if [[ "$tstart_md" == "001" ]]
511
+    then
512
+    check_start=$(echo $(($check_start+1)));
513
+    zblock_dev=$((${size_zmd[$crt_index]}));
514
+    echo $zblock_dev > /sys/block/zram$crt_index/disksize;
515
+    on_loop=$(losetup -f "${image_zmd[$crt_index]}" && losetup -a |grep "${image_zmd[$crt_index]}"|awk '{print$1}'|sed 's/\://g');
516
+#
517
+set_image_name=${image_zmd[$crt_index]};
518
+prepareMd;
519
+#
520
+if [[ $set_prepare == "0" ]]
521
+    then
522
+	echo -e "y"|mdadm --create /dev/md${id_zmd[$crt_index]} --auto=yes --metadata=1.2 --level=raid1 --raid-devices=2 missing $on_loop;
523
+        echo -e "y"|mdadm --add /dev/md${id_zmd[$crt_index]}  /dev/zram$crt_index;
524
+    else
525
+        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;
526
+        echo -e "y"|mdadm --add /dev/md${id_zmd[$crt_index]}  /dev/zram$crt_index;
527
+    fi
528
+fi
529
+done
530
+
531
+if (( "$check_start" >"0" ))
532
+    then
533
+    reports[${#reports[@]}]=${msg[16]};
534
+    writeToLog;
535
+    printInfo;
536
+    else
537
+    reports[${#reports[@]}]=${msg[50]};
538
+    writeToLog;
539
+    printInfo;
540
+fi
541
+}
542
+
543
+##--@F step operation --------
544
+function offZraid(){
545
+local check_stop="0";
546
+for ((off_index=0; off_index != ${#id_zmd[@]}; off_index++))
547
+    do
548
+set_md_id=${id_zmd[$off_index]};
549
+singleTestMd;
550
+local tstop_md=$(echo ${set_md_status[@]}|tr -d ' '|sed 's/\"//g');
551
+
552
+if [[ "$tstop_md" == "" ]] || [[ ! "$tstop_md" =~ ^([0-9]+)$ ]] || (( "$tstop_md" < "0" ))
553
+	then
554
+	    reports=();
555
+            reports[${#reports[@]}]=${msg[10]};
556
+            makeErr;
557
+fi
558
+
559
+if [[ "$tstop_md" == "111" ]]||[[ "$tstop_md" == "101" ]]
560
+    then
561
+check_stop=$(echo $(($check_stop+1)));
562
+local off_set_loop=$(losetup -a |grep "${image_zmd[$off_index]}"|awk '{print$1}'|sed 's/\://g');
563
+local off_set_zraid=$(cat /proc/mdstat |grep "md$set_md_id"|tail -n1|awk '{print$5}'|sed 's/\[2]//g');
564
+mdadm -S /dev/md$set_md_id;
565
+    if [[ "$off_set_loop" ]]
566
+	then
567
+	losetup -d $off_set_loop;
568
+    fi
569
+
570
+local kernel_type=$(echo ${kernel_status[@]}|tr -d ' '|sed 's/\:\"//g');
571
+    if [ "$kernel_type" == "11" ]
572
+	then
573
+	    if [ "$(lsmod|grep zram|wc -m)" != "0" ]
574
+		then
575
+		echo $(echo $off_set_zraid|sed 's/\dev//g'|sed 's/\zram//g'|sed 's/\///g') > /sys/class/zram-control/hot_remove ;
576
+	    fi
577
+    fi
578
+fi
579
+done
580
+
581
+if [ $(lsmod|grep zram|wc -m) ] 
582
+    then  modprobe -r zram; 
583
+fi
584
+if (( "$check_stop" >"0" ))
585
+    then
586
+    reports[${#reports[@]}]=${msg[17]};
587
+    writeToLog;
588
+    printInfo;
589
+    else
590
+    reports[${#reports[@]}]=${msg[19]};
591
+    writeToLog;
592
+    printInfo;
593
+fi
594
+}
595
+
596
+##--@F step operation
597
+function statusZraid(){
598
+status=();
599
+for ((st_index=0; st_index != ${#id_zmd[@]}; st_index++))
600
+	do
601
+	test_loop="$(cat /proc/mdstat |grep md${id_zmd[$st_index]}|awk '{print$6}'|cut -d'[' -f1)";
602
+	if [ "$test_loop" ]
603
+	    then
604
+	    test_zmd="$(sudo losetup -a |grep $test_loop|awk '{print$3}'|grep ${image_zmd[$st_index]}|wc -m)";
605
+	    if [ "$test_zmd" ]
606
+		then status[${#status[@]}]="md${id_zmd[$st_index]}";
607
+	    fi
608
+	fi
609
+done
610
+if [ $test_status != 0 ] 
611
+    then
612
+	test_status=0;
613
+	if [ ! ${#status[@]} == 0 ]
614
+	    then 
615
+		reports[${#reports[@]}]="${msg[18]} ${status[@]}";
616
+		writeToLog;
617
+		printInfo;
618
+		exit 0;
619
+	else
620
+		reports[${#reports[@]}]=${msg[19]};
621
+		writeToLog;
622
+		printInfo;
623
+		exit 0;
624
+	fi
625
+fi
626
+}
627
+
628
+##--@F step operation
629
+function testId() {
630
+if (( ! $(id|grep "root"|wc -m) ))
631
+	then
632
+	reports=(); 
633
+	reports[${#reports[@]}]="${msg[47]}";
634
+	makeErr;
635
+fi
636
+}
637
+
638
+##............................................................................................
639
+function singleStart() {
640
+
641
+local tset_md=$(echo ${set_md_status[@]}|tr -d ' '|sed 's/\"//g');
642
+if [[ "$tset_md" == "" ]] || [[ ! "$tset_md" =~ ^([0-9]+)$ ]] || (( "$tset_md" < "0" ))
643
+	then
644
+	    reports=();
645
+            reports[${#reports[@]}]=${msg[10]};
646
+            makeErr;
647
+fi
648
+iFs=( 	"$tset_md == 001"
649
+	"$tset_md == 111"
650
+	"$tset_md == 100"
651
+	"$tset_md == 000" );
652
+logic=( '"" "startSmd"' 
653
+	'"" "noStartSmd"' 
654
+	'"" "busSmd"' 
655
+	'"" "noConfSmd"' );
656
+
657
+function noStartSmd() {
658
+	lEnd=0;
659
+	reports=();
660
+        reports[${#reports[@]}]="md$set_md_id${msg[50]}";
661
+        printInfo;
662
+	exit 0;
663
+}
664
+
665
+function busSmd() {
666
+	lEnd=0;
667
+	reports=();
668
+        reports[${#reports[@]}]="md$set_md_id${msg[15]}";
669
+        printInfo;
670
+	exit 0;
671
+}
672
+
673
+function noConfSmd() {
674
+	lEnd=0;
675
+	reports=();
676
+        reports[${#reports[@]}]="md$set_md_id : ${msg[9]}";
677
+        printInfo;
678
+	exit 0;
679
+}
680
+
681
+function startSmd () {
682
+lEnd=0;
683
+local on_set_image="$(cat $zconfig|jq -c '.md[]|select(.id == "'$set_md_id'")|.image'|sed 's/\"//g' )";
684
+local on_set_loop="$(losetup -f)";
685
+losetup "$on_set_loop" "$on_set_image";
686
+if (( $(lsmod|grep zram|wc -m) != 0 ))
687
+    then
688
+    cat /sys/class/zram-control/hot_add >/dev/nill ;  ## add zram dev!
689
+    else
690
+    modprobe zram num_devices=1;
691
+fi
692
+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)))";
693
+local zset_block_dev=$(stat -c%s "$on_set_image");
694
+echo $zset_block_dev > /sys/block/zram$set_zram_id/disksize;
695
+#
696
+set_image_name=$on_set_image;
697
+prepareMd;
698
+#
699
+if [[ $set_prepare == "0" ]]
700
+    then
701
+	echo -e "y"|mdadm --create /dev/md$set_md_id --auto=yes --metadata=1.2 --level=raid1 --raid-devices=2 missing $on_set_loop;
702
+        echo -e "y"|mdadm --add /dev/md$set_md_id  /dev/zram$set_zram_id;
703
+    else
704
+        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;
705
+        echo -e "y"|mdadm --add /dev/md$set_md_id  /dev/zram$set_zram_id;
706
+    fi
707
+}
708
+eXlogic;
709
+reports[${#reports[@]}]="${msg[43]} : start - /dev/md$set_md_id";
710
+writeToLog;
711
+echo -e  "${reports[@]}";
712
+}
713
+
714
+##............................................................................................
715
+function singleStop() {
716
+local tset_md=$(echo ${set_md_status[@]}|tr -d ' '|sed 's/\"//g');
717
+if [[ "$tset_md" == "" ]] || [[ ! "$tset_md" =~ ^([0-9]+)$ ]] || (( "$tset_md" < "0" ))
718
+	then
719
+	    reports=();
720
+            reports[${#reports[@]}]=${msg[10]};
721
+            makeErr;
722
+fi
723
+iFs=( 	"$tset_md == 001"
724
+	"$tset_md == 111"
725
+	"$tset_md == 101"
726
+	"$tset_md == 100"
727
+	"$tset_md == 000" );
728
+logic=( '"" "noStopSmd"' 
729
+	'"" "stopSmd"' 
730
+	'"" "stopSmd"' 
731
+	'"" "sBusSmd"' 
732
+	'"" "noConfSmd"' );
733
+
734
+function noStopSmd() {
735
+	lEnd=0;
736
+	reports=();
737
+        reports[${#reports[@]}]="${msg[44]}md$set_md_id";
738
+	reports[${#reports[@]}]="${msg[42]}";
739
+        printInfo;
740
+	exit 0;
741
+}
742
+
743
+function sBusSmd() {
744
+	lEnd=0;
745
+	reports=();
746
+        reports[${#reports[@]}]="md$set_md_id${msg[15]}";
747
+        printInfo;
748
+	exit 0;
749
+}
750
+
751
+function noConfSmd() {
752
+	lEnd=0;
753
+	reports=();
754
+        reports[${#reports[@]}]="md$set_md_id : ${msg[9]}";
755
+        printInfo;
756
+	exit 0;
757
+}
758
+
759
+function stopSmd () {
760
+lEnd=0;
761
+local off_set_loop="/dev/$(ls /sys/block/"md$set_md_id"/md/|grep loop|sed 's/dev-//g')";
762
+local off_set_zraid=$(cat /proc/mdstat |grep "md$set_md_id"|tail -n1|awk '{print$5}'|sed 's/\[2]//g');
763
+mdadm -S /dev/md$set_md_id;
764
+
765
+echo $(echo $off_set_zraid|sed 's/\dev//g'|sed 's/\zram//g'|sed 's/\///g') > /sys/class/zram-control/hot_remove;
766
+if [[ $off_set_loop ]]
767
+    then
768
+    losetup -d $off_set_loop;
769
+fi
770
+}
771
+eXlogic;
772
+reports[${#reports[@]}]="${msg[43]} : stop - /dev/md$set_md_id";
773
+writeToLog;
774
+echo  -e "${reports[@]}";
775
+}
776
+
777
+##...........................................................................................
778
+function singleTestMd() {
779
+local check_md=$(cat /proc/mdstat |grep "md$set_md_id"|tail -n 1|awk '{print$1}'|wc -m);
780
+local check_zram=$(cat /proc/mdstat |grep "md$set_md_id"|grep zram|wc -m);
781
+local check_conf=$(cat $zconfig|jq -c '.md[]."id"'|grep -e "$set_md_id"|sed 's/\"//g'|sed 's/^[ \t]*//;s/[ \t]*$//'|wc -m);
782
+
783
+iFs=(   "$check_md == 0"
784
+        "$check_conf == 0"
785
+        "$check_zram == 0" );
786
+
787
+logic=( '"statusOn" "statusOff"'                                                # test on MD
788
+	'"mDconfOn" "mDconfOff"'                                                # test MD in conf
789
+        '"mDzramOn" "mDzramOff"'                                                # test  zram in MD
790
+        );
791
+
792
+    function statusOn() {
793
+	set_md_status[0]="1";
794
+    }
795
+    
796
+    function statusOff() {
797
+	set_md_status[0]="0";
798
+    }
799
+
800
+    function mDzramOn() {
801
+	set_md_status[1]="1";
802
+    }
803
+
804
+    function mDzramOff() {
805
+	set_md_status[1]="0";
806
+    }
807
+    
808
+    function mDconfOn() {
809
+	set_md_status[2]="1";
810
+    }
811
+    
812
+    function mDconfOff() {
813
+	set_md_status[2]="0";
814
+    }
815
+eXlogic;
816
+}
817
+
818
+##..........................................................................................
819
+function prepareMd() {
820
+local check_loop=$(losetup -a |grep "$set_image_name"|awk '{print$1}'|sed 's/\/dev\/\|s//g;s/\://g');
821
+if (( $(cat /proc/mdstat |grep "$check_loop"|awk '{print$1}'|wc -m) != "0" ))
822
+    then
823
+    mdadm -S /dev/$(cat /proc/mdstat |grep "$check_loop"|awk '{print$1}');
824
+    set_prepare="1";
825
+    else
826
+    set_prepare="0";
827
+fi
828
+}
829
+
830
+##...........................................................................................
831
+function inParams() {
832
+local kernel_type=$(echo ${kernel_status[@]}|tr -d ' ');
833
+if (( "$kernel_type" != "11" ))
834
+    then
835
+    reports=(); 
836
+	reports[${#reports[@]}]="${msg[49]}";
837
+	makeErr;
838
+fi
839
+eval set_params="(" $(echo -e $params|sed 's/\md//g') ")";
840
+if ((  "${#set_params[@]}" >= "1" ))
841
+    then
842
+    set_md_id="${set_params[0]}";
843
+        if [[ "$set_md_id" == "" ]]||[[ ! "$set_md_id" =~ ^([0-9]+)$ ]]||(( "$set_md_id" <= "0" ))
844
+            then
845
+                reports=();
846
+                reports[${#reports[@]}]="${msg[10]}";
847
+                makeErr;
848
+        fi
849
+    else
850
+        reports=();
851
+        reports[${#reports[@]}]="${msg[10]}";
852
+        makeErr;
853
+fi
854
+
855
+}
856
+
857
+
858
+##--@F executor
859
+function executor() {
860
+if [[ ${#execute_func[@]} -eq 0 ]] 
861
+    then echo "exit";
862
+	 exit 0; 
863
+fi
864
+for ((ex_index=0; ex_index != ${#execute_func[@]}; ex_index++))
865
+ do
866
+    ## !! debug operation...
867
+    ##echo "execution: function ${execute_func[ex_index]}"
868
+ ${execute_func[ex_index]};
869
+done
870
+}
871
+
872
+
873
+##- Begin ZRAID
874
+case "$option" in
875
+
876
+## on zraid
877
+"--on" | "--on" )
878
+if [ ! "$params" ]
879
+    then
880
+    execute_func=( ${operation_on[@]} );
881
+    executor;
882
+    else
883
+    execute_func=( ${operation_sign_on[@]} );
884
+    executor;
885
+fi
886
+exit 0;
887
+;; 
888
+
889
+## off zraid
890
+"--off" | "--off" )
891
+if [ ! "$params" ]
892
+    then
893
+    execute_func=( ${operation_off[@]} );
894
+    executor;
895
+    else
896
+    execute_func=( ${operation_sign_off[@]} );
897
+    executor;
898
+fi
899
+exit 0;
900
+;;
901
+
902
+## status zraid
903
+"--status" | "--status" )
904
+test_status=1;
905
+execute_func=( ${operation_status[@]} );
906
+executor;
907
+exit 0;
908
+;;
909
+
910
+## test config
911
+"--test" | "--test" )
912
+execute_func=( ${operation_test[@]} );
913
+executor;
914
+exit 0;
915
+;;
916
+
917
+## help
918
+"--help" | "--help" )
919
+execute_func=( ${operation_help[@]} );
920
+executor;
921
+exit 0;
922
+;;
923
+
924
+* )
925
+# selecting defaults.
926
+execute_func=( ${operation_help[@]} );
927
+executor;
928
+exit 1;
929
+;;
930
+esac

+ 16 - 0
zramraid.deb/etc/zramraid/zramraid.conf

@@ -0,0 +1,16 @@
1
+{
2
+  "info": "id - md[id], image - is the path to the media to mount",
3
+  "log": "/var/log/syslog",
4
+  "limit_free_mem": {
5
+    "info": "size - the parameter 10-100% or 1024Kb...; Caution - the limit use free memory!",
6
+    "size": "90",
7
+    "value_size": "%"
8
+  },
9
+  "md": [
10
+    {
11
+      "id": "1",
12
+      "info": "'test disk 1'",
13
+      "image": "../kvm/disk1.img"
14
+    }
15
+  ]
16
+}

+ 20 - 0
zramraid.deb/lib/systemd/system/zramraid.service

@@ -0,0 +1,20 @@
1
+[Unit]
2
+SourcePath=/etc/zraid/zraid-maker
3
+Description=VirtualBox Linux kernel module
4
+Before=runlevel2.target runlevel3.target runlevel4.target runlevel5.target shutdown.target 
5
+After=
6
+Conflicts=shutdown.target 
7
+
8
+[Service]
9
+Type=forking
10
+Restart=no
11
+TimeoutSec=5min
12
+IgnoreSIGPIPE=no
13
+KillMode=process
14
+GuessMainPID=no
15
+RemainAfterExit=yes
16
+ExecStart=/usr/lib/virtualbox/vboxdrv.sh start
17
+ExecStop=/usr/lib/virtualbox/vboxdrv.sh stop
18
+
19
+[Install]
20
+WantedBy=multi-user.target

+ 273 - 0
zramraid.deb/zramraid-install

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