Skip to content

Commit

Permalink
Add zram role
Browse files Browse the repository at this point in the history
  • Loading branch information
ikaruswill committed Aug 3, 2023
1 parent c06f4d5 commit db760d7
Show file tree
Hide file tree
Showing 8 changed files with 276 additions and 0 deletions.
13 changes: 13 additions & 0 deletions zram.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
- hosts: all
roles:
- zram
vars:
zram_log:
alg: lzo-rle
mem_limit: 64M
disk_size: 192M
zram_swap:
alg: lzo-rle
mem_limit: 512M
disk_size: 1024M
64 changes: 64 additions & 0 deletions zram/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
ZRAM
=========

Install and configure `zram-config`. Optionally upgrade zram-config if a newer version is available.

Requirements
------------

None

Role Variables
--------------

| Variable | Description | Default |
| ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------- |
| zram_upgrade | Should zram-swap be upgraded if a newer version is available? | false |
| zram_swap_default_swap_priority | Default swap priority if none specified | 75 |
| zram_swap_default_page_cluster | Default page cluster if none specified | 0 |
| zram_swap_default_swappiness | Default swappiness if none specified | 150 |
| zram_log_default_target_dir | Default target_dir if none specified | /var/log |
| zram_log_default_bind_dir | Default bind_dir if none specified | /opt/zram/log.bind |
| zram_log_default_oldlog_dir | Default oldlog_dir if none specified | /opt/zram/oldlog |
| <zram_device>.alg | Compression algorithm to use | |
| <zram_device>.mem_limit | Maximum physical memory that the swap may occupy | |
| <zram_device>.disk_size | Maximum size to advertise after factoring in compression | |
| zram_swap | Swap device configuration attributes, if mapping is omitted, no swap device will be created. | |
| zram_swap.swap_priority | Priority of the zram swap device over other swap devices | zram_swap_default_swap_priority |
| zram_swap.page_cluster | Number of memory pages up to which consecutive pages are read in from swap. Set to 0 for zram for low latency. Values are taken as powers of 2. | zram_swap_default_page_cluster |
| zram_swap.swappiness | How often should the swap device be utilized? For zram, value should be > 100. Can be set to 200 for improved performance in high memory pressure | zram_swap_default_swappiness |
| zram_log | zram log device configuration attributes, if mapping is omitted, no zram log device will be created. | |
| zram_log.target_dir | Target directory with which to mount the zram device | zram_log_default_target_dir |
| zram_log.bind_dir | Path where the original directory will be mounted for persistence | zram_log_default_bind_dir |
| zram_log.oldlog_dir | Path where the rotated logs will be stored | zram_log_default_oldlog_dir |

Dependencies
------------

None

Example Playbook
----------------
```yaml
- hosts: all
roles:
- zram
vars:
zram_log:
alg: lzo-rle
mem_limit: 64M
disk_size: 192M
zram_swap:
alg: lzo-rle
mem_limit: 512M
disk_size: 1024M
```
License
-------
BSD
Author Information
------------------
ikaruswill
53 changes: 53 additions & 0 deletions zram/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
zram_repo: https://github.com/ecdye/zram-config.git
zram_clone_path: /tmp/zram-config
zram_config_path: /etc/ztab
zram_uninstall_path: /usr/local/share/zram-config/uninstall.bash
zram_upgrade: false

zram_swap_default_swap_priority: 75
zram_swap_default_page_cluster: 0
zram_swap_default_swappiness: 150

zram_log_default_target_dir: /var/log
zram_log_default_bind_dir: /opt/zram/log.bind
zram_log_default_oldlog_dir: /opt/zram/oldlog

# Example configuration
# zram_swap:
# alg: lz4
# mem_limit: 250M
# disk_size: 750M
# swap_priority: 75 # Optional
# page_cluster: 0 # Optional
# swappiness: 80 # Optional

# zram_log:
# alg: lz4
# mem_limit: 50M
# disk_size: 150M
# target_dir: /var/log # Optional
# bind_dir: /log.bind # Optional
# oldlog_dir: /opt/zram/oldlog # Optional

# zram_extra_devices:
# - type: swap
# alg: lz4
# mem_limit: 250M
# disk_size: 750M
# swap_priority: 75 # Optional
# page_cluster: 0 # Optional
# swappiness: 80 # Optional
# - type: log
# alg: lz4
# mem_limit: 50M
# disk_size: 150M
# target_dir: /var/log # Optional
# bind_dir: /log.bind # Optional
# oldlog_dir: /opt/zram/oldlog # Optional
# - type: dir
# alg: lz4
# mem_limit: 50M
# disk_size: 150M
# target_dir: /home/pi
# bind_dir: /pi.bind
14 changes: 14 additions & 0 deletions zram/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
- name: Check if zram-config is installed
shell: ls /etc/systemd/system | grep zram-config.service
register: zram_installed_result
changed_when: false
failed_when: false

- include: zram-uninstall.yml
when: zram_installed_result.rc == 0 and zram_upgrade

- include: zram-install.yml
when: zram_installed_result.rc == 1 or (zram_installed_result.rc == 0 and zram_upgrade)

- include: zram-config.yml
18 changes: 18 additions & 0 deletions zram/tasks/zram-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
- name: Stop zram-config
become: yes
service:
name: zram-config
state: stopped

- name: Configure zram-config
become: yes
template:
src: '../templates/ztab.j2'
dest: '{{ zram_config_path }}'

- name: Start zram-config
become: yes
service:
name: zram-config
state: started
28 changes: 28 additions & 0 deletions zram/tasks/zram-install.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
- name: Clone zram-config repository
git:
repo: '{{ zram_repo }}'
dest: '{{ zram_clone_path }}'
depth: '1'
force: yes

- name: Enable execution of install script
file:
path: '{{ zram_clone_path }}/install.bash'
mode: 755

- name: Run install script
become: yes
command: './install.bash'
args:
chdir: '{{ zram_clone_path }}'
register: zram_config_install

- debug: msg="{{ zram_config_install.stdout_lines }}"
- debug: msg="{{ zram_config_install.stderr_lines }}"

- name: Cleanup
become: yes
file:
path: '{{ zram_clone_path }}'
state: absent
14 changes: 14 additions & 0 deletions zram/tasks/zram-uninstall.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
- name: Enable execution of uninstall script
become: yes
file:
path: '{{ zram_uninstall_path }}'
mode: 755

- name: Run uninstall script
become: yes
command: '{{ zram_uninstall_path }}'
register: zram_config_uninstall

- debug: msg="{{ zram_config_uninstall.stdout_lines }}"
- debug: msg="{{ zram_config_uninstall.stderr_lines }}"
72 changes: 72 additions & 0 deletions zram/templates/ztab.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Use '#' to comment out any line, add new drives with the first column
# providing the drive type and then drive details separated by tab characters.
#
# All algorithms in /proc/crypto are supported but only lzo-rle, lzo, lz4, and
# zstd have zramctl text strings; lzo-rle is the fastest with zstd having much
# better text compression.
#
# mem_limit is the compressed memory limit and will set a hard memory limit for
# the system admin. Set to 0 to disable the mem_limit.
#
# disk_size is the maximum size of the uncompressed memory. It should be set to
# roughly 150% of mem_limit depending on the algorithm and how compressible the
# input files are. Don't make it much higher than the compression algorithm
# (and the additional zram overhead) is capable of because there is a ~0.1%
# memory overhead when empty.
#
# swap_priority will set zram over alternative swap devices.
#
# page-cluster 0 means tuning to singular pages rather than the default 3 which
# caches 8 for HDD tuning, which can lower latency.
#
# swappiness 150 because the improved performance of zram allows more usage
# without any adverse affects from the default of 60. It can be raised up to 200
# which will improve performance in high memory pressure situations.
#
# target_dir is the directory you wish to hold in zram, and the original will be
# moved to a bind mount 'bind_dir' and is synchronized on start, stop, and write
# commands.
#
# bind_dir is the directory where the original directory will be mounted for
# sync purposes. Usually in '/opt' or '/var', name optional.
#
# oldlog_dir will enable log-rotation to an off device directory while retaining
# only live logs in zram. Usually in '/opt' or '/var', name optional.
#
# If you need multiple zram swaps or zram directories, just create another entry
# in this file. To do this simply add the new entries to this file, if you need
# to edit an active zram device you must stop zram with
# 'sudo systemctl stop zram-config.service' on Debian or
# 'sudo rc-service zram-config stop' on Alpine and then edit any entries you
# need to. Once finished, start zram using
# 'sudo systemctl start zram-config.service' or
# 'sudo rc-service zram-config start' which will only add the new entries if
# zram is already running.

{% if zram_swap is defined %}
# swap alg mem_limit disk_size swap_priority page-cluster swappiness
swap {{ zram_swap.alg }} {{ zram_swap.mem_limit }} {{ zram_swap.disk_size }} {{ zram_swap.swap_priority | default(zram_swap_default_swap_priority) }} {{ zram_swap.page_cluster | default(zram_swap_default_page_cluster) }} {{ zram_swap.swappiness | default(zram_swap_default_swappiness) }}
{% endif %}

{% if zram_log is defined %}
# log alg mem_limit disk_size target_dir bind_dir oldlog_dir
log {{ zram_log.alg }} {{ zram_log.mem_limit }} {{ zram_log.disk_size }} {{ zram_log.target_dir | default(zram_log_default_target_dir) }} {{ zram_log.bind_dir | default(zram_log_default_bind_dir) }} {{ zram_log.oldlog_dir | default(zram_log_default_oldlog_dir) }}
{% endif %}

{% if zram_extra_devices is defined %}
{% for dev in zram_extra_devices %}
{% if dev.type == 'swap' %}
# swap alg mem_limit disk_size swap_priority page-cluster swappiness
swap {{ dev.alg }} {{ dev.mem_limit }} {{ dev.disk_size }} {{ dev.swap_priority | default(zram_swap_default_swap_priority) }} {{ dev.page_cluster | default(zram_swap_default_page_cluster) }} {{ dev.swappiness | default(zram_swap_default_swappiness) }}

{% elif dev.type == 'log' %}
# log alg mem_limit disk_size target_dir bind_dir oldlog_dir
log {{ dev.alg }} {{ dev.mem_limit }} {{ dev.disk_size }} {{ dev.target_dir | default(zram_log_default_target_dir) }} {{ dev.bind_dir | default(zram_log_default_bind_dir) }} {{ dev.oldlog_dir | default(zram_log_default_oldlog_dir) }}

{% elif dev.type == 'dir' %}
# dir alg mem_limit disk_size target_dir bind_dir
dir {{ dev.alg }} {{ dev.mem_limit }} {{ dev.disk_size }} {{ dev.target_dir }} {{ dev.bind_dir }}

{% endif %}
{% endfor %}
{% endif %}

0 comments on commit db760d7

Please sign in to comment.