Advertisement
Guest User

rc.zram

a guest
Aug 16th, 2017
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.54 KB | None | 0 0
  1. #!/bin/sh
  2. #################################################################################################################################################
  3. #                                                                                                                                               #
  4. # Previous standard setting                                                                                                                     #
  5. # SIZE=1024                                                                                                                                     #
  6. # echo $SIZE*1024*1024 | bc > /sys/block/zram0/disksize                                                                                         #
  7. # default 1GB                                                                                                                                   #
  8. # Script to start zRam (Virtual Swap Compressed in RAM)                                                                                         #
  9. # https://github.com/otzy007/enable-zRam-in-Slackware                                                                                           #
  10. #                                                                                                                                               #
  11. #################################################################################################################################################
  12. #                                                                                                                                               #
  13. # http://forum.linuxvillage.org/index.php?topic=92.0                                                                                            #
  14. # https://sergioprado.org/economizando-memoria-com-o-zram/                                                                                      #
  15. # http://www.linuxquestions.org/questions/linux-kernel-70/zram-compression-ratio-4175588385/                                                    #
  16. # https://www.ibm.com/developerworks/community/blogs/752a690f-8e93-4948-b7a3-c060117e8665/entry/ative_a_compactacao_de_memoria_com_zram?lang=en #
  17. #                                                                                                                                               #
  18. # Jean Zonta                                                                                                                                    #
  19. # Slackware 14.2 x64                                                                                                                            #
  20. # Genéric Kernel 4.9.41                                                                                                                         #
  21. # wiki.anon                                                                                                                                     #
  22. # echo "}oqo4gtutF_gnuu4ius4hx" | perl -pe \ 's/(.)/chr(ord($1)-2*3)/ge'                                                                        #
  23. #                                                                                                                                               #
  24. # My added news                                                                                                                                 #
  25. #                                                                                                                                               #
  26. # echo 1G > /sys/block/zram0/disksize                                                                                                           #
  27. # config                                                                                                                                        #                                                                                                                                                
  28. # fstab                                                                                                                                         #
  29. #                                                                                                                                               #
  30. #################################################################################################################################################
  31.  
  32. # Null
  33. NULL=/dev/null
  34.  
  35. # Check for existence  /dev/zram0
  36. ZRAM=$(ls -l /dev/zram0 | awk '{print $10}')
  37.  
  38. # Definition priority for RAM
  39. PRIORITYRAM="vm.swappiness=5"
  40.  
  41. # Definition priority for ZRAM
  42. PRIORITYZRAM="vm.swappiness=100"
  43.  
  44. # File sysctl.conf
  45. SYSCTL=/etc/sysctl.conf
  46.  
  47. # Check for existence of parameter
  48. SWAPPINESS=$( grep -ri "vm.swappiness" $SYSCTL)
  49.  
  50. # File fstab
  51. FSTAB=/etc/fstab
  52.  
  53. # Check configured partition
  54. STRZRAM=$( grep -ri "/dev/zram0" $FSTAB | awk '{print $1}')
  55.  
  56.  
  57. # Start ZRAM
  58. start()
  59. {
  60.  echo 2G > /sys/block/zram0/disksize
  61.   mkswap /dev/zram0
  62.  swapon -p 50 /dev/zram0
  63. }
  64.  
  65. # Config Parameters
  66. config()
  67. {
  68.  
  69. if [[ $ZRAM < $NULL && ! -f $SYSCTL ]]; then
  70.  touch $SYSCTL
  71.   echo -e "# Top priority for RAM usage\n$PRIORITYRAM" >> $SYSCTL
  72.    echo -e "\nArquivo $SYSCTL e $PRIORITYRAM Criada com sucesso ..!!!\n"
  73.    
  74. elif [[ $ZRAM > $NULL && ! -f $SYSCTL ]]; then
  75.  touch $SYSCTL
  76.   echo -e "# Top priority for ZRAM usage\n$PRIORITYZRAM" >> $SYSCTL
  77.    echo -e "\nArquivo $SYSCTL e $PRIORITYZRAM Criada com sucesso ..!!!\n"
  78.  
  79. elif [[ $ZRAM < $NULL && $SWAPPINESS < $NULL ]]; then
  80.  echo -e "# Top priority for RAM usage\n$PRIORITYRAM" >> $SYSCTL
  81.   echo -e "\n$PRIORITYRAM Criada com sucesso ..!!!\n"
  82.  
  83. elif [[ $ZRAM > $NULL && $SWAPPINESS < $NULL ]]; then
  84.  echo -e "# Top priority for ZRAM usage\n$PRIORITYZRAM" >> $SYSCTL
  85.   echo -e "\n$PRIORITYZRAM Criada com sucesso ..!!!\n"
  86.    
  87. elif [[ $ZRAM < $NULL && $SWAPPINESS > $NULL && $SWAPPINESS != $PRIORITYRAM ]]; then
  88.    MODIFICA=$(sed -i 's/# Top priority for ZRAM usage/# Top priority for RAM usage/g;s/'"^$SWAPPINESS"'/'"$PRIORITYRAM"'/g' $SYSCTL)
  89.     echo -e "\n# Top priority for RAM usage\n$PRIORITYRAM\nModificada com sucesso ..!!!\n"
  90.    
  91. elif [[ $ZRAM > $NULL && $SWAPPINESS > $NULL && $SWAPPINESS != $PRIORITYZRAM ]]; then
  92.   MODIFICA=$(sed -i 's/# Top priority for RAM usage/# Top priority for ZRAM usage/g;s/'"^$SWAPPINESS"'/'"$PRIORITYZRAM"'/g' $SYSCTL)
  93.    echo -e "\n# Top priority for ZRAM usage\n$PRIORITYZRAM\nModificada com sucesso ..!!!\n"
  94.     fi
  95. }
  96.  
  97. # Config ZRAM fstab
  98. fstab()
  99. {
  100.  if [[ $ZRAM < $NULL && $STRZRAM < $NULL ]]; then
  101.   echo -e "\n# Top priority for $PRIORITYRAM configured\n"
  102.  
  103.  elif [[ $ZRAM > $NULL && $STRZRAM > $NULL ]]; then
  104.   echo -e "\n# Top priority for $STRZRAM configured\n"
  105.  
  106.  elif [[ $ZRAM > $NULL && $STRZRAM < $NULL && $SWAPPINESS == $PRIORITYZRAM ]]; then
  107.   cp $FSTAB $FSTAB.old
  108.    echo "/dev/zram0       none             swap        sw,pri=50        0   0" > $FSTAB
  109.     cat $FSTAB.old >> $FSTAB
  110.  
  111.   MODIFICA=$(sed -i 's/defaults/noatime,barrier=0,commit=60/g' $FSTAB)      
  112.    rm -rf $FSTAB.old
  113.     cat $FSTAB
  114.      echo -e "/dev/zram0 configured sucessfull\n"
  115.      
  116.  elif [[ $ZRAM < $NULL && $STRZRAM > $NULL && $SWAPPINESS == $PRIORITYRAM ]]; then
  117.   RM=$(sed -i '1d' $FSTAB)
  118.     cat $FSTAB
  119.      echo -e "\n# Top priority for /dev/zram0 not supported\n$FSTAB reconfigured sucessfull\n"
  120.  fi
  121. }
  122.  
  123. # Stop ZRAM
  124. stop()
  125. {
  126.   swapoff /dev/zram0
  127. }
  128.  
  129. case "$1" in
  130.  
  131.   start)
  132.     start
  133.   ;;
  134.    
  135.  config)
  136.    config
  137.   ;;
  138.  
  139.   fstab)
  140.     fstab
  141.   ;;
  142.  
  143.   stop)
  144.     stop
  145.   ;;
  146.  
  147.   restart)
  148.     echo 1 > /sys/block/zram0/reset
  149.   ;;
  150.  
  151.   *)
  152.   echo "Usage: $0 (start|config|fstab|stop|restart)"
  153. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement