zramraid-maker 24 KB

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