zramraid-config 19 KB

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