zramraid-maker 24 KB

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