zramraid-maker 24 KB

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