SlideShare a Scribd company logo
1 of 2
#!/bin/bash 
### BEGIN INIT INFO 
# Provides: zram 
# Required-Start: 
# Required-Stop: 
# Default-Start: 2 3 4 5 
# Default-Stop: 0 1 6 
# Short-Description: Increased Performance In Linux With zRam (Virtual Swap 
Compressed in RAM) 
# Description: Adapted from systemd scripts at 
https://github.com/mystilleef/FedoraZram 
### END INIT INFO 
DEFAULTS="/etc/default/zram" 
# Include defaults if available 
[ -r "$DEFAULTS" ] && . "$DEFAULTS" 
# Get lsb functions 
. /lib/lsb/init-functions 
start() { 
# Add support of defaults file parameter - ZRAM_NDEVS 
if [ -z "$ZRAM_NDEVS" ] || [ "$ZRAM_NDEVS" -le "0" ]; then 
# get the number of CPUs 
num_cpus=$(grep -c processor /proc/cpuinfo) 
else 
# or just use user specified 
num_cpus=$ZRAM_NDEVS 
fi 
# if something goes wrong, assume we have 1 
[ "$num_cpus" != 0 ] || num_cpus=1 
decr_num_cpus=$((num_cpus - 1)) 
# find module and it's param name 
error_file=$(mktemp ${RC_NAME}XXXXXX) # storage for error message 
param_name=$(/usr/bin/env modinfo --parameters zram 2>$error_file | head -1 
| cut -d: -f1) 
error_message=$(<$error_file) 
rm -f $error_file 
mod_found=$? 
# load dependency modules 
if [ "$mod_found" == 0 ]; then 
log_begin_msg "Loading zRAM kernel module" 
modprobe zram "$param_name=$num_cpus" 
log_end_msg $? 
else 
log_failure_msg "Failed to load zRAM kernel module: ${error_message}" 
# shit happens, yep. 
exit $mod_found 
fi 
# get the amount of memory in the machine 
mem_total_kb=$(fgrep MemTotal /proc/meminfo | grep -E --only-matching 
'[[:digit:]]+') 
mem_total=$((mem_total_kb * 1024)) 
# Add support of defaults file parameter - ZRAM_USE_PERCENT 
if [ -z "$ZRAM_USE_PERCENT" ] || [ "$ZRAM_USE_PERCENT" -le "0" ] ||
[ "$ZRAM_USE_PERCENT" -gt "100" ]; then 
# use 100% of ram for zRAM storage (suitable for low-ram laptops) 
use_percent=100 
else 
# or just use user specified 
use_percent=$ZRAM_USE_PERCENT 
fi 
zsize=$((use_percent * mem_total / num_cpus / 100)) 
# initialize the devices 
for i in $(seq 0 $decr_num_cpus); do 
echo $zsize > /sys/block/zram$i/disksize 
done 
# get page size 
page_size=$(/usr/bin/env getconf PAGESIZE || /usr/bin/env getconf PAGE_SIZE) 
# Creating swap filesystems 
for i in $(seq 0 $decr_num_cpus); do 
msg="> Making swap on /dev/zram$i" 
mkswap -p $page_size /dev/zram$i 2>&1 >/dev/null 
[ "$?" -eq "0" ] && log_success_msg $msg || log_failure_msg $msg 
done 
# Switch the swaps on 
for i in $(seq 0 $decr_num_cpus); do 
msg=">> Activating swap on /dev/zram$i" 
swapon -p 100 /dev/zram$i 
[ "$?" -eq "0" ] && log_success_msg $msg || log_failure_msg $msg 
done 
# TODO FIXME need strict error reporting 
# log_end_msg 0 
} 
stop() { 
# Switching off swap ASAP 
for zswap in $(fgrep /dev/zram /proc/swaps | cut -d' ' -f1); do 
swapoff $zswap 
done 
# Custom kernels can contain zram compiled-in 
[ -z "$(fgrep zram /proc/modules)" ] || rmmod zram 
} 
case "$1" in 
start) 
start 
;; 
stop) 
stop 
;; 
restart) 
stop 
sleep 3 
start 
;; 
*) 
echo "Usage: $0 {start|stop|restart}" 
RETVAL=1 
esac

More Related Content

What's hot

Magical WordPress Development with Vagrant
Magical WordPress Development with VagrantMagical WordPress Development with Vagrant
Magical WordPress Development with VagrantChris Olbekson
 
A journey through the years of UNIX and Linux service management
A journey through the years of UNIX and Linux service managementA journey through the years of UNIX and Linux service management
A journey through the years of UNIX and Linux service managementLubomir Rintel
 
Configuration Management with Cfengine
Configuration Management with CfengineConfiguration Management with Cfengine
Configuration Management with CfengineSteven Kreuzer
 
Environments line-up! Vagrant & Puppet 101
Environments line-up! Vagrant & Puppet 101Environments line-up! Vagrant & Puppet 101
Environments line-up! Vagrant & Puppet 101jelrikvh
 
Hubot: a look inside our robot friend
Hubot: a look inside our robot friendHubot: a look inside our robot friend
Hubot: a look inside our robot friendajacksified
 
Ssh2 install
Ssh2 installSsh2 install
Ssh2 installGGclub
 
How to simplify OSGi development using OBR - Peter Kriens
How to simplify OSGi development using OBR - Peter KriensHow to simplify OSGi development using OBR - Peter Kriens
How to simplify OSGi development using OBR - Peter Kriensmfrancis
 
Go初心者がGoでコマンドラインツールの作成に挑戦した話
Go初心者がGoでコマンドラインツールの作成に挑戦した話Go初心者がGoでコマンドラインツールの作成に挑戦した話
Go初心者がGoでコマンドラインツールの作成に挑戦した話dcubeio
 
(WHAT) To Do or Not To Do: Staying Up To Date with PHP Versions
(WHAT) To Do or Not To Do: Staying Up To Date with PHP Versions(WHAT) To Do or Not To Do: Staying Up To Date with PHP Versions
(WHAT) To Do or Not To Do: Staying Up To Date with PHP VersionsAbu Ashraf Masnun
 
Ansible ハンズオン on AWS - DevelopersIO 2017
Ansible ハンズオン on AWS - DevelopersIO 2017Ansible ハンズオン on AWS - DevelopersIO 2017
Ansible ハンズオン on AWS - DevelopersIO 2017Shuji Watanabe
 

What's hot (20)

Magical WordPress Development with Vagrant
Magical WordPress Development with VagrantMagical WordPress Development with Vagrant
Magical WordPress Development with Vagrant
 
A journey through the years of UNIX and Linux service management
A journey through the years of UNIX and Linux service managementA journey through the years of UNIX and Linux service management
A journey through the years of UNIX and Linux service management
 
Advanced Shell Scripting
Advanced Shell ScriptingAdvanced Shell Scripting
Advanced Shell Scripting
 
garry pal ing
garry pal inggarry pal ing
garry pal ing
 
Comets notes
Comets notesComets notes
Comets notes
 
4412 SDRAM
4412 SDRAM4412 SDRAM
4412 SDRAM
 
Configuration Management with Cfengine
Configuration Management with CfengineConfiguration Management with Cfengine
Configuration Management with Cfengine
 
Linux basic3
Linux basic3Linux basic3
Linux basic3
 
Environments line-up! Vagrant & Puppet 101
Environments line-up! Vagrant & Puppet 101Environments line-up! Vagrant & Puppet 101
Environments line-up! Vagrant & Puppet 101
 
Hubot: a look inside our robot friend
Hubot: a look inside our robot friendHubot: a look inside our robot friend
Hubot: a look inside our robot friend
 
Ssh2 install
Ssh2 installSsh2 install
Ssh2 install
 
How to simplify OSGi development using OBR - Peter Kriens
How to simplify OSGi development using OBR - Peter KriensHow to simplify OSGi development using OBR - Peter Kriens
How to simplify OSGi development using OBR - Peter Kriens
 
Thin
ThinThin
Thin
 
Como programar melhor jogando game boy
Como programar melhor jogando game boyComo programar melhor jogando game boy
Como programar melhor jogando game boy
 
Automazione quotidiana in php
Automazione quotidiana in phpAutomazione quotidiana in php
Automazione quotidiana in php
 
Introduction to ansible
Introduction to ansibleIntroduction to ansible
Introduction to ansible
 
Go初心者がGoでコマンドラインツールの作成に挑戦した話
Go初心者がGoでコマンドラインツールの作成に挑戦した話Go初心者がGoでコマンドラインツールの作成に挑戦した話
Go初心者がGoでコマンドラインツールの作成に挑戦した話
 
(WHAT) To Do or Not To Do: Staying Up To Date with PHP Versions
(WHAT) To Do or Not To Do: Staying Up To Date with PHP Versions(WHAT) To Do or Not To Do: Staying Up To Date with PHP Versions
(WHAT) To Do or Not To Do: Staying Up To Date with PHP Versions
 
Ruby 1.9
Ruby 1.9Ruby 1.9
Ruby 1.9
 
Ansible ハンズオン on AWS - DevelopersIO 2017
Ansible ハンズオン on AWS - DevelopersIO 2017Ansible ハンズオン on AWS - DevelopersIO 2017
Ansible ハンズオン on AWS - DevelopersIO 2017
 

Similar to Zram

Shell实现的windows回收站功能的脚本
Shell实现的windows回收站功能的脚本Shell实现的windows回收站功能的脚本
Shell实现的windows回收站功能的脚本Lingfei Kong
 
How to mount and unmount filesystem
How to mount and unmount filesystemHow to mount and unmount filesystem
How to mount and unmount filesystemCOMSATS
 
Configuration Surgery with Augeas
Configuration Surgery with AugeasConfiguration Surgery with Augeas
Configuration Surgery with AugeasPuppet
 
Really useful linux commands
Really useful linux commandsReally useful linux commands
Really useful linux commandsMichael J Geiser
 
php drupal mysql MAMP
php drupal mysql MAMPphp drupal mysql MAMP
php drupal mysql MAMPJing Cheng
 
StackiFest16: Stacki 1600+ Server Journey - Dave Peterson, Salesforce
StackiFest16: Stacki 1600+ Server Journey - Dave Peterson, Salesforce StackiFest16: Stacki 1600+ Server Journey - Dave Peterson, Salesforce
StackiFest16: Stacki 1600+ Server Journey - Dave Peterson, Salesforce StackIQ
 
Virtualization and automation of library software/machines + Puppet
Virtualization and automation of library software/machines + PuppetVirtualization and automation of library software/machines + Puppet
Virtualization and automation of library software/machines + PuppetOmar Reygaert
 
Augeas
AugeasAugeas
Augeaslutter
 
Automatic systems installations and change management wit FAI - Talk for Netw...
Automatic systems installations and change management wit FAI - Talk for Netw...Automatic systems installations and change management wit FAI - Talk for Netw...
Automatic systems installations and change management wit FAI - Talk for Netw...Henning Sprang
 
Introduction to JumpStart
Introduction to JumpStartIntroduction to JumpStart
Introduction to JumpStartScott McDermott
 
Vagrant for real
Vagrant for realVagrant for real
Vagrant for realCodemotion
 

Similar to Zram (20)

Samba 4 - debian instalacao
Samba 4 - debian instalacaoSamba 4 - debian instalacao
Samba 4 - debian instalacao
 
Shell实现的windows回收站功能的脚本
Shell实现的windows回收站功能的脚本Shell实现的windows回收站功能的脚本
Shell实现的windows回收站功能的脚本
 
How to mount and unmount filesystem
How to mount and unmount filesystemHow to mount and unmount filesystem
How to mount and unmount filesystem
 
Solaris_quickref.pdf
Solaris_quickref.pdfSolaris_quickref.pdf
Solaris_quickref.pdf
 
EC2
EC2EC2
EC2
 
Configuration Surgery with Augeas
Configuration Surgery with AugeasConfiguration Surgery with Augeas
Configuration Surgery with Augeas
 
Augeas @RMLL 2012
Augeas @RMLL 2012Augeas @RMLL 2012
Augeas @RMLL 2012
 
Dev ops
Dev opsDev ops
Dev ops
 
Really useful linux commands
Really useful linux commandsReally useful linux commands
Really useful linux commands
 
php drupal mysql MAMP
php drupal mysql MAMPphp drupal mysql MAMP
php drupal mysql MAMP
 
Stacki - The1600+ Server Journey
Stacki - The1600+ Server JourneyStacki - The1600+ Server Journey
Stacki - The1600+ Server Journey
 
StackiFest16: Stacki 1600+ Server Journey - Dave Peterson, Salesforce
StackiFest16: Stacki 1600+ Server Journey - Dave Peterson, Salesforce StackiFest16: Stacki 1600+ Server Journey - Dave Peterson, Salesforce
StackiFest16: Stacki 1600+ Server Journey - Dave Peterson, Salesforce
 
Virtualization and automation of library software/machines + Puppet
Virtualization and automation of library software/machines + PuppetVirtualization and automation of library software/machines + Puppet
Virtualization and automation of library software/machines + Puppet
 
Augeas
AugeasAugeas
Augeas
 
Automatic systems installations and change management wit FAI - Talk for Netw...
Automatic systems installations and change management wit FAI - Talk for Netw...Automatic systems installations and change management wit FAI - Talk for Netw...
Automatic systems installations and change management wit FAI - Talk for Netw...
 
Vagrant for real
Vagrant for realVagrant for real
Vagrant for real
 
Introduction to JumpStart
Introduction to JumpStartIntroduction to JumpStart
Introduction to JumpStart
 
Vyg monitor
Vyg monitorVyg monitor
Vyg monitor
 
ASM
ASMASM
ASM
 
Vagrant for real
Vagrant for realVagrant for real
Vagrant for real
 

Recently uploaded

CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 

Recently uploaded (20)

CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 

Zram

  • 1. #!/bin/bash ### BEGIN INIT INFO # Provides: zram # Required-Start: # Required-Stop: # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Increased Performance In Linux With zRam (Virtual Swap Compressed in RAM) # Description: Adapted from systemd scripts at https://github.com/mystilleef/FedoraZram ### END INIT INFO DEFAULTS="/etc/default/zram" # Include defaults if available [ -r "$DEFAULTS" ] && . "$DEFAULTS" # Get lsb functions . /lib/lsb/init-functions start() { # Add support of defaults file parameter - ZRAM_NDEVS if [ -z "$ZRAM_NDEVS" ] || [ "$ZRAM_NDEVS" -le "0" ]; then # get the number of CPUs num_cpus=$(grep -c processor /proc/cpuinfo) else # or just use user specified num_cpus=$ZRAM_NDEVS fi # if something goes wrong, assume we have 1 [ "$num_cpus" != 0 ] || num_cpus=1 decr_num_cpus=$((num_cpus - 1)) # find module and it's param name error_file=$(mktemp ${RC_NAME}XXXXXX) # storage for error message param_name=$(/usr/bin/env modinfo --parameters zram 2>$error_file | head -1 | cut -d: -f1) error_message=$(<$error_file) rm -f $error_file mod_found=$? # load dependency modules if [ "$mod_found" == 0 ]; then log_begin_msg "Loading zRAM kernel module" modprobe zram "$param_name=$num_cpus" log_end_msg $? else log_failure_msg "Failed to load zRAM kernel module: ${error_message}" # shit happens, yep. exit $mod_found fi # get the amount of memory in the machine mem_total_kb=$(fgrep MemTotal /proc/meminfo | grep -E --only-matching '[[:digit:]]+') mem_total=$((mem_total_kb * 1024)) # Add support of defaults file parameter - ZRAM_USE_PERCENT if [ -z "$ZRAM_USE_PERCENT" ] || [ "$ZRAM_USE_PERCENT" -le "0" ] ||
  • 2. [ "$ZRAM_USE_PERCENT" -gt "100" ]; then # use 100% of ram for zRAM storage (suitable for low-ram laptops) use_percent=100 else # or just use user specified use_percent=$ZRAM_USE_PERCENT fi zsize=$((use_percent * mem_total / num_cpus / 100)) # initialize the devices for i in $(seq 0 $decr_num_cpus); do echo $zsize > /sys/block/zram$i/disksize done # get page size page_size=$(/usr/bin/env getconf PAGESIZE || /usr/bin/env getconf PAGE_SIZE) # Creating swap filesystems for i in $(seq 0 $decr_num_cpus); do msg="> Making swap on /dev/zram$i" mkswap -p $page_size /dev/zram$i 2>&1 >/dev/null [ "$?" -eq "0" ] && log_success_msg $msg || log_failure_msg $msg done # Switch the swaps on for i in $(seq 0 $decr_num_cpus); do msg=">> Activating swap on /dev/zram$i" swapon -p 100 /dev/zram$i [ "$?" -eq "0" ] && log_success_msg $msg || log_failure_msg $msg done # TODO FIXME need strict error reporting # log_end_msg 0 } stop() { # Switching off swap ASAP for zswap in $(fgrep /dev/zram /proc/swaps | cut -d' ' -f1); do swapoff $zswap done # Custom kernels can contain zram compiled-in [ -z "$(fgrep zram /proc/modules)" ] || rmmod zram } case "$1" in start) start ;; stop) stop ;; restart) stop sleep 3 start ;; *) echo "Usage: $0 {start|stop|restart}" RETVAL=1 esac