zramraid-config 19 KB

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