zramraid-config 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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="25.06.18";
  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. # test enable file messages
  197. if [ ! -f $zmsg ]
  198. then
  199. reports=();
  200. reports[${#reports[@]}]="$zmsg - file not found!";
  201. makeErr;
  202. fi
  203. # read msg
  204. if [[ ! $(cat $zmsg|grep "$lang"|wc -m) == 0 ]]
  205. then
  206. index_msg="$(cat $zmsg | jq '."'$lang'".msg[]'|wc -l)";
  207. if [[ ! "$index_msg" == "0" ]]||[[ "$index_msg" == "${#set_msg[@]}" ]]
  208. then
  209. for (( m_index=0; m_index != $index_msg; m_index++))
  210. do
  211. eval st_in_msg="(" ${set_msg[$m_index]} ")";
  212. if [ "${st_in_msg[1]}" != "0" ]
  213. then
  214. read_msg=$(cat $zmsg | jq '."'$lang'".msg['$m_index']'| sed 's/\"//g');
  215. msg[$m_index]="$read_msg";
  216. else
  217. msg[$m_index]="";
  218. fi
  219. done
  220. else
  221. reports=();
  222. reports[${#reports[@]}]="$zmsg - Incorrect file format or empty!";
  223. makeErr;
  224. fi
  225. fi
  226. }
  227. ##--@F step operation
  228. function readConf() {
  229. # test enable file zramraid.conf
  230. if [ ! -f $zpath/$zconfig ]
  231. then
  232. reports=();
  233. reports[${#reports[@]}]=${msg[7]};
  234. makeErr;
  235. fi
  236. # read config
  237. for (( rd_index=0; rd_index !=${#zdm_vars[@]}; rd_index++))
  238. do
  239. read_key=$(cat $zpath/$zconfig | jq '.md[].'${json_key[$rd_index]}| sed 's/\"//g');
  240. eval ${zdm_vars[$rd_index]}="(" $read_key ")";
  241. done
  242. log=$(cat $zpath/$zconfig | jq '.log'| sed 's/\"//g');
  243. ## test free memory
  244. value_mem=$(cat $zpath/$zconfig | jq '.limit_free_mem.value_size'| sed 's/\"//g');
  245. value_in="$value_mem";
  246. iFs=( "$(echo -n $value_in|wc -m) == 0"
  247. "$(echo -n $value_in|sed 's/%//g'|wc -m) == 0"
  248. "$(echo -n $value_in|sed 's/Kb//g'|wc -m) == 0" );
  249. logic=( '"" "lmem_Err"'
  250. '"" "lmem_Per"'
  251. '"lmem_Err" "lmem_Kb"' );
  252. lmem_Err() {
  253. reports=();
  254. reports[${#reports[@]}]="${msg[29]} $value_mem /or null - ${msg[10]}(${msg[30]}:Kb,%)";
  255. makeErr;
  256. }
  257. lmem_Per() {
  258. lEnd=0;
  259. test_read_mem=0;
  260. input_unit=( "$free_mem" "test_read_mem" );
  261. Kb;
  262. 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}');
  263. ## -"
  264. if (( "$math_unit" < 0 ))||[[ "$math_unit" == "" ]]||[[ ! "$math_unit" =~ ^([0-9]+)$ ]]
  265. then
  266. reports=();
  267. reports[${#reports[@]}]="${msg[29]} $value_mem /or null - ${msg[10]}(${msg[30]}:Kb,%)";
  268. makeErr;
  269. else
  270. limit_mem=$math_unit;
  271. input_unit=( "$math_unit" "info_limit_mem" );
  272. Byte;
  273. math_unit=$info_limit_mem;
  274. input_unit=( "$math_unit" "info_limit_mem" "1" );
  275. Kb;
  276. fi
  277. }
  278. lmem_Kb() {
  279. lEnd=0;
  280. input_unit=( "$(cat $zpath/$zconfig | jq '.limit_free_mem.size'| sed 's/\"//g')" "math_unit" );
  281. Kb;
  282. limit_mem=$math_unit;
  283. input_unit=( "$math_unit" "info_limit_mem");
  284. Mb;
  285. }
  286. eXlogic;
  287. }
  288. function testMd() {
  289. # test the md number parameters for errors and busy in system for add md
  290. if (( "$set_id" < 0 ))||[[ "$set_id" == "" ]]||[[ ! "$set_id" =~ ^([0-9]+)$ ]]
  291. then
  292. reports=();
  293. reports[${#reports[@]}]="${msg[21]}$set_id - ${msg[10]}";
  294. makeErr;
  295. fi
  296. if [ "$($zr_blkid /dev/md$set_id|grep $set_id)" ]
  297. then
  298. reports=();
  299. reports[${#reports[@]}]="${msg[21]}$set_id - ${msg[11]}";
  300. makeErr;
  301. fi
  302. # test the duble number md from config
  303. test_dub=( $(echo -e ${id_zmd[@]}|tr " " "\n"|sort|tr "\n" " ") );
  304. for (( dub_index=0; dub_index != ${#id_zmd[@]}; dub_index++ ))
  305. do
  306. dubtest_index=$(($dub_index+1));
  307. if [[ ! $dubtest_index > $((${#id_zmd[@]}-1)) ]]
  308. then
  309. if [[ $((${test_dub[$dubtest_index]}-${test_dub[$dub_index]})) == 0 ]];
  310. then
  311. ## duble from config
  312. reports=();
  313. reports[${#reports[@]}]="${msg[21]}${id_zmd[$(($dub_index+1))]} - ${msg[12]}";
  314. makeErr;
  315. fi
  316. fi
  317. if [[ $((${test_dub[$dub_index]}-$set_id)) == 0 ]]
  318. then
  319. ## duble input end from config
  320. reports=();
  321. reports[${#reports[@]}]="${msg[21]}$set_id - ${msg[12]}";
  322. makeErr;
  323. fi
  324. done
  325. }
  326. function testImage() {
  327. total_size_images=0;
  328. eval tmp_date="(" $(echo -e $set_image|sed 's,/,\, ,g'|cut -c 2-) ")";
  329. test_path=$(echo $set_image|sed 's,'${tmp_date[$((${#tmp_date[@]}-1))]}',,g');
  330. # test enable file images
  331. if [ ! -f $test_path${tmp_date[$((${#tmp_date[@]}-1))]} ]
  332. then
  333. reports=();
  334. reports[${#reports[@]}]="${msg[22]}$set_image - ${msg[13]}";
  335. makeErr;
  336. fi
  337. function new_add() {
  338. test_size_images=$(stat -c%s $test_path${tmp_date[$((${#tmp_date[@]}-1))]});
  339. }
  340. function sum_img() {
  341. #echo "summ images...";
  342. ## the sum size images
  343. for (( image_index=0; image_index != ${#image_zmd[@]}; image_index++ ))
  344. do
  345. if [ ! -f ${image_zmd[$image_index]} ]
  346. then reports[${#reports[@]}]="${msg[22]}${image_zmd[$image_index]} - ${msg[13]}";
  347. makeErr;
  348. fi
  349. size_zmd[$image_index]=$(stat -c%s ${image_zmd[$image_index]});
  350. done
  351. # test size image end size zram ...
  352. for (( im_index=0; im_index != ${#image_zmd[@]}; im_index++ ))
  353. do
  354. total_size_images=$(($total_size_images+${size_zmd[$im_index]}));
  355. done
  356. size_new_image=$(stat -c%s $test_path${tmp_date[$((${#tmp_date[@]}-1))]});
  357. test_size_images=$(($total_size_images+$size_new_image));
  358. }
  359. function add_false() {
  360. input_unit=( "$test_size_images" "math_unit" "1" );
  361. Kb;
  362. img_msg=$math_unit;
  363. input_unit=( "$limit_mem" "math_unit" "1" );
  364. Kb;
  365. mem_msg=$math_unit;
  366. reports=();
  367. reports[${#reports[@]}]="${msg[22]}$set_image - ${msg[14]}";
  368. reports[${#reports[@]}]="${msg[23]}$img_msg""Kb > ${msg[20]}$mem_msg""Kb";
  369. makeErr;
  370. }
  371. iFs=( "$(echo -n ${#image_zmd[@]}) > 0" );
  372. logic=( '"new_add" "sum_img"' );
  373. eXlogic;
  374. iFs=( "$(echo -n $test_size_images) <= $limit_mem" );
  375. logic=( '"add_false" ""' );
  376. eXlogic;
  377. }
  378. ## input parameters
  379. function inPut() {
  380. function inMd() {
  381. lEnd=0;
  382. eval set_params="(" $(echo -e $params|sed 's/:/ /') ")";
  383. if [ $(($(echo -n ${set_params[0]}|grep -o md|wc -m)-1)) == 2 ]
  384. then
  385. set_id=$(echo -e ${set_params[0]}|awk '{print$1}'|sed 's/\md//g');
  386. if [[ "$set_id" == "" ]]||[[ ! "$set_id" =~ ^([0-9]+)$ ]]||(( "$set_id" < 0 ))
  387. then
  388. reports=();
  389. reports[${#reports[@]}]=${msg[10]};
  390. makeErr;
  391. else
  392. reports=();
  393. set_image=${set_params[1]};
  394. fi
  395. else
  396. reports=();
  397. reports[${#reports[@]}]=${msg[10]};
  398. makeErr;
  399. fi
  400. }
  401. function inLimit() {
  402. lEnd=0;
  403. set_size=$(echo $params|sed 's/%//g'|sed 's/Kb//g');
  404. if [[ "$set_size" == "" ]]||[[ ! "$set_size" =~ ^([0-9]+)$ ]]||(( "$set_size" <= 0 ))
  405. then
  406. reports=();
  407. reports[${#reports[@]}]=${msg[29]};
  408. reports[${#reports[@]}]=${msg[39]};
  409. makeErr;
  410. else
  411. set_type=$(echo $params|sed 's/'"$set_size"'//g');
  412. if [[ "$set_type" == "%" ]]||[[ "$set_type" == "Kb" ]]
  413. then
  414. set_size_limit=$set_size;
  415. set_type_limit=$set_type;
  416. else
  417. reports=();
  418. reports[${#reports[@]}]=${msg[29]};
  419. reports[${#reports[@]}]=${msg[39]};
  420. makeErr;
  421. fi
  422. fi
  423. }
  424. function noSwitch() {
  425. lEnd=0;
  426. reports=();
  427. reports[${#reports[@]}]=${msg[30]};
  428. reports[${#reports[@]}]=${msg[37]};
  429. reports[${#reports[@]}]=${msg[38]};
  430. reports[${#reports[@]}]=${msg[40]};
  431. makeErr;
  432. }
  433. iFs=( "${set_opt[1]} == 1" "${set_opt[2]} == 1" "${set_opt[3]} == 1" "${set_opt[4]} == 1" );
  434. logic=( '"" "inMd"' '"" "inMd"' '"" "inLimit"' '"noSwitch" "inMd"' );
  435. eXlogic;
  436. }
  437. ## create empty config
  438. function confEmpty() {
  439. function confNew(){
  440. echo '{'>$zpath/$zconfig
  441. echo -e '"info":"id - md[id], image - is the path to the media to mount",\n'>>$zpath/$zconfig
  442. echo -e '"log":"/var/log/syslog",\n'>>$zpath/$zconfig
  443. echo '"limit_free_mem":{'>>$zpath/$zconfig
  444. echo '"info":"size - the parameter 10-100% or 1024Kb...; Caution - the limit use free memory!",'>>$zpath/$zconfig
  445. echo '"size":"90",'>>$zpath/$zconfig
  446. echo -e '"value_size":"%"},\n'>>$zpath/$zconfig
  447. echo '"md":['>>$zpath/$zconfig
  448. echo ' ]'>>$zpath/$zconfig
  449. echo -e '}'>>$zpath/$zconfig
  450. }
  451. if [ ! -f $zpath/$zconfig ]
  452. then
  453. confNew;
  454. else
  455. cp $zpath/$zconfig $zpath/$(date +%H_%M)"-zramraid.old">/dev/null;
  456. confNew;
  457. fi
  458. }
  459. function addMd() {
  460. local tdate=$(date +%j);
  461. cat $zpath/$zconfig|jq -c '.md|=.+[{"id":"'$set_id'","info":"","image":"'$set_image'"}]'|
  462. jq '.'>$zpath/$tdate$zconfig && cat $zpath/$tdate$zconfig>$zpath/$zconfig && rm -f $zpath/$tdate$zconfig;
  463. }
  464. ## del md from config
  465. function delMd() {
  466. local tdate=$(date +%j);
  467. local del_value=$(cat $zpath/$zconfig|jq -c '.md[]|select (.id == "'$set_id'")');
  468. #'
  469. if [[ ! "$del_value" ]]
  470. then
  471. reports=();
  472. reports[${#reports[@]}]="${msg[29]}: md$set_id - ${msg[9]}";
  473. makeErr;
  474. else
  475. cat $zpath/$zconfig|jq -c '.md|=.-['$del_value']'|jq '.'>$zpath/$tdate$zconfig && cat $zpath/$tdate$zconfig>$zpath/$zconfig && rm -f $zpath/$tdate$zconfig;
  476. fi
  477. }
  478. ## report config
  479. function reportConf() {
  480. reports=();
  481. for (( report_index=0; report_index != ${#id_zmd[@]}; report_index++ ))
  482. do
  483. reports[${#reports[@]}]="config md: ${id_zmd[$report_index]}";
  484. reports[${#reports[@]}]="info md: ${info_zmd[$report_index]}";
  485. if (( $(stat -c%s ${image_zmd[$report_index]}) > 1024 ))
  486. then
  487. input_unit=( "$(stat -c%s ${image_zmd[$report_index]})" "math_unit" "1" );
  488. Kb;
  489. rep_size="$math_unit""Kb";
  490. else
  491. rep_size="$(stat -c%s ${image_zmd[$report_index]})b";
  492. fi
  493. reports[${#reports[@]}]="size md: $rep_size";
  494. reports[${#reports[@]}]="image md: ${image_zmd[$report_index]}";
  495. reports[${#reports[@]}]="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
  496. done
  497. reports[${#reports[@]}]="${msg[20]} $info_limit_mem Mb";
  498. }
  499. ## setting limit memory
  500. function setLimitMem() {
  501. local tdate=$(date +%j);
  502. test_set_mem=0;
  503. test_free_mem=0;
  504. if [[ "$set_type_limit" == "%" ]]
  505. then
  506. input_unit=( "$free_mem" "math_unit" );
  507. Kb;
  508. 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}');
  509. #-"
  510. fi
  511. if [[ "$set_type_limit" == "Kb" ]]
  512. then
  513. math_unit=$set_size_limit;
  514. input_unit=( "$math_unit" "test_set_mem" );
  515. Kb;
  516. fi
  517. if [[ ! "$test_set_mem" == 0 ]]
  518. then
  519. math_unit=$free_mem;
  520. input_unit=( "$math_unit" "test_free_mem" );
  521. Kb;
  522. else
  523. reports=();
  524. reports[${#reports[@]}]=${msg[29]};
  525. reports[${#reports[@]}]=${msg[39]};
  526. makeErr;
  527. fi
  528. if (( "$test_free_mem" > "$test_set_mem" ))
  529. then
  530. ## add date
  531. local input_date_limit=$(cat $zpath/$zconfig|jq -c '.limit_free_mem|.size="'"$set_size_limit"'"|.value_size="'"$set_type_limit"'"');
  532. cat $zpath/$zconfig|jq -c ' del (.limit_free_mem)'|
  533. jq '.'>$zpath/$tdate$zconfig && cat $zpath/$tdate$zconfig>$zpath/$zconfig && rm -f $zpath/$tdate$zconfig;
  534. ## add
  535. cat $zpath/$zconfig|jq -c '.limit_free_mem|=.+'"$input_date_limit"''|
  536. jq '.'>$zpath/$tdate$zconfig && cat $zpath/$tdate$zconfig>$zpath/$zconfig && rm -f $zpath/$tdate$zconfig;
  537. else
  538. reports=();
  539. reports[${#reports[@]}]="${msg[20]}$set_size_limit$set_type_limit > ${msg[33]}$free_mem""Kb";
  540. reports[${#reports[@]}]=${msg[29]};
  541. reports[${#reports[@]}]=${msg[39]};
  542. makeErr;
  543. fi
  544. }
  545. ## add info device md
  546. function addInfoMd() {
  547. if [[ $(cat $zpath/$zconfig|jq -c '.md[]|select(.id == "'$set_id'")') ]]
  548. #-'
  549. then
  550. local Sinput=();
  551. clear
  552. echo "--------------------------------------------------";
  553. echo "${msg[45]}";
  554. echo "--------------------------------------------------";
  555. read Sinput;
  556. local tdate=$(date +%j);
  557. local data_info="$(cat $zpath/$zconfig|jq -c '.md[]|select(.id == "'$set_id'")|.info ="'"'$(echo ${Sinput[@]})'"'"')";
  558. delMd;
  559. cat $zpath/$zconfig|jq -c '.md|=.+['"$data_info"']'|
  560. jq '.'>$zpath/$tdate$zconfig && cat $zpath/$tdate$zconfig>$zpath/$zconfig && rm -f $zpath/$tdate$zconfig;
  561. else
  562. reports=();
  563. reports[${#reports[@]}]=${msg[9]};
  564. makeErr;
  565. fi
  566. }
  567. ##--@F step operation
  568. function testId() {
  569. if (( ! $(id|grep "root"|wc -m) ))
  570. then
  571. reports=();
  572. reports[${#reports[@]}]="${msg[47]}";
  573. makeErr;
  574. fi
  575. }
  576. ##--@F executor
  577. function executor() {
  578. if [[ ${#execute_func[@]} -eq 0 ]]
  579. then echo "exit";
  580. exit 0;
  581. fi
  582. for ((ex_index=0; ex_index != ${#execute_func[@]}; ex_index++))
  583. do
  584. ## !! debug operation...
  585. ##echo "execution: function ${execute_func[ex_index]}"
  586. ${execute_func[ex_index]};
  587. done
  588. }
  589. ##- Begin zramraid
  590. case "$option" in
  591. ## create default config +
  592. "--config-default" | "--config-default" )
  593. execute_func=( ${operation_createdef[@]} );
  594. executor;
  595. exit 0
  596. ;;
  597. ## add md
  598. "--add" | "--add" )
  599. set_opt[1]="1";
  600. execute_func=( ${operation_add[@]} );
  601. executor;
  602. exit 0
  603. ;;
  604. ## del md
  605. "--del" | "--del" )
  606. set_opt[2]="1";
  607. execute_func=( ${operation_del[@]} );
  608. executor;
  609. exit 0
  610. ;;
  611. ## list md +
  612. "--list" | "--list" )
  613. execute_func=( ${operation_list[@]} );
  614. executor;
  615. exit 0
  616. ;;
  617. ## limit memory
  618. "--limit-mem" | "--limit-mem" )
  619. set_opt[3]="1";
  620. execute_func=( ${operation_mem[@]} );
  621. executor;
  622. exit 0
  623. ;;
  624. ## md info
  625. "--info-md" | "--info-md" )
  626. set_opt[4]="1";
  627. execute_func=( ${operation_mdinfo[@]} );
  628. executor;
  629. exit 0
  630. ;;
  631. ## help +
  632. "--help" | "--help" )
  633. execute_func=( ${operation_help[@]} );
  634. executor;
  635. exit 0
  636. ;;
  637. * )
  638. # selecting defaults. +
  639. execute_func=( ${operation_help[@]} );
  640. executor;
  641. exit 1
  642. ;;
  643. esac