New release
Jonathan Carter
1 year, 3 months ago
0 | Copyright (c) 2014-2019, Jonathan Carter <jcarter@linux.com> | |
1 | ||
2 | Permission to use, copy, modify, and/or distribute this software for any | |
3 | purpose with or without fee is hereby granted, provided that the above | |
4 | copyright notice and this permission notice appear in all copies. | |
5 | ||
6 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
7 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
8 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
9 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
10 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
11 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT | |
12 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
0 | zram-tools | |
1 | ========== | |
2 | ||
3 | Scripts for managing zram device, currently only for zramswap, | |
4 | but more tools could be implemented in the future, such as managing | |
5 | /tmp on zram. | |
6 | ||
7 | zramswap start | |
8 | -------------- | |
9 | ||
10 | Sets up zram devices and initializes swap. | |
11 | ||
12 | zramswap stop | |
13 | ------------- | |
14 | ||
15 | Removes all current zram swap space and device. | |
16 | ||
17 | zramswap status | |
18 | --------------- | |
19 | ||
20 | Shows information on data stored in zram swap. | |
21 | ||
22 | /etc/default/zramswap | |
23 | --------------------- | |
24 | ||
25 | Configuration file for zramswap. |
0 | # Compression algorithm selection | |
1 | # speed: lz4 > zstd > lzo | |
2 | # compression: zstd > lzo > lz4 | |
3 | # This is not inclusive of all that is available in latest kernels | |
4 | # See /sys/block/zram0/comp_algorithm (when zram module is loaded) to see | |
5 | # what is currently set and available for your kernel[1] | |
6 | # [1] https://github.com/torvalds/linux/blob/master/Documentation/blockdev/zram.txt#L86 | |
7 | #ALGO=lz4 | |
8 | ||
9 | # Specifies the amount of RAM that should be used for zram | |
10 | # based on a percentage the total amount of available memory | |
11 | # This takes precedence and overrides SIZE below | |
12 | #PERCENT=50 | |
13 | ||
14 | # Specifies a static amount of RAM that should be used for | |
15 | # the ZRAM devices, this is in MiB | |
16 | #SIZE=256 | |
17 | ||
18 | # Specifies the priority for the swap devices, see swapon(2) | |
19 | # for more details. Higher number = higher priority | |
20 | # This should probably be higher than hdd/ssd swaps. | |
21 | #PRIORITY=100 |
0 | #!/bin/bash | |
1 | ||
2 | # This script does the following: | |
3 | # zramswap start: | |
4 | # Space is assigned to the zram device, then swap is initialized and enabled. | |
5 | # zramswap stop: | |
6 | # Somewhat potentially dangerous, removes zram module at the end | |
7 | ||
8 | # https://github.com/torvalds/linux/blob/master/Documentation/blockdev/zram.txt | |
9 | ||
10 | readonly CONFIG="/etc/default/zramswap" | |
11 | readonly SWAP_DEV="/dev/zram0" | |
12 | ||
13 | if command -v logger >/dev/null; then | |
14 | function elog { | |
15 | logger -s "Error: $*" | |
16 | exit 1 | |
17 | } | |
18 | ||
19 | function wlog { | |
20 | logger -s "$*" | |
21 | } | |
22 | else | |
23 | function elog { | |
24 | echo "Error: $*" | |
25 | exit 1 | |
26 | } | |
27 | ||
28 | function wlog { | |
29 | echo "$*" | |
30 | } | |
31 | fi | |
32 | ||
33 | function start { | |
34 | wlog "Starting Zram" | |
35 | ||
36 | # Load config | |
37 | test -r "${CONFIG}" || wlog "Cannot read config from ${CONFIG} continuing with defaults." | |
38 | source "${CONFIG}" 2>/dev/null | |
39 | ||
40 | # Set defaults if not specified | |
41 | : "${ALGO:=lz4}" "${SIZE:=256}" "${PRIORITY:=100}" | |
42 | ||
43 | SIZE=$((SIZE * 1024 * 1024)) # convert amount from MiB to bytes | |
44 | ||
45 | # Prefer percent if it is set | |
46 | if [ -n "${PERCENT}" ]; then | |
47 | readonly TOTAL_MEMORY=$(awk '/MemTotal/{print $2}' /proc/meminfo) # in KiB | |
48 | readonly SIZE="$((TOTAL_MEMORY * 1024 * PERCENT / 100))" | |
49 | fi | |
50 | ||
51 | modprobe zram || elog "inserting the zram kernel module" | |
52 | echo -n "${ALGO}" > /sys/block/zram0/comp_algorithm || elog "setting compression algo to ${ALGO}" | |
53 | echo -n "${SIZE}" > /sys/block/zram0/disksize || elog "setting zram device size to ${SIZE}" | |
54 | mkswap "${SWAP_DEV}" || elog "initialising swap device" | |
55 | swapon -p "${PRIORITY}" "${SWAP_DEV}" || elog "enabling swap device" | |
56 | } | |
57 | ||
58 | function status { | |
59 | test -x "$(which zramctl)" || elog "install zramctl for this feature" | |
60 | test -b "${SWAP_DEV}" || elog "${SWAP_DEV} doesn't exist" | |
61 | # old zramctl doesn't have --output-all | |
62 | #zramctl --output-all | |
63 | zramctl "${SWAP_DEV}" | |
64 | } | |
65 | ||
66 | function stop { | |
67 | wlog "Stopping Zram" | |
68 | test -b "${SWAP_DEV}" || wlog "${SWAP_DEV} doesn't exist" | |
69 | swapoff "${SWAP_DEV}" 2>/dev/null || wlog "disabling swap device: ${SWAP_DEV}" | |
70 | modprobe -r zram || elog "removing zram module from kernel" | |
71 | } | |
72 | ||
73 | function usage { | |
74 | cat << EOF | |
75 | ||
76 | Usage: | |
77 | zramswap (start|stop|restart|status) | |
78 | ||
79 | EOF | |
80 | } | |
81 | ||
82 | case "$1" in | |
83 | start) start;; | |
84 | stop) stop;; | |
85 | restart) stop && start;; | |
86 | status) status;; | |
87 | "") usage;; | |
88 | *) elog "Unknown option $1";; | |
89 | esac |
0 | .TH "zramswap" 1 "2018-10-04" "zramswap" | |
1 | .SH NAME | |
2 | zramswap \- configure swap on zram | |
3 | .SH SYNOPSIS | |
4 | .B zramswap | |
5 | [OPTION] | |
6 | .SH DESCRIPTION | |
7 | zram is a Linux feature that allows a user to create compressed ramdisks. | |
8 | ||
9 | zramswap sets up swap in zram, which effectively allows you to compress | |
10 | memory. | |
11 | ||
12 | Common use cases: | |
13 | ||
14 | On machines with low memory, mitigates the tedious situation where the | |
15 | machine is out of memory and then it starts swapping to disk only to lose | |
16 | a large amount of disk bandwidth. | |
17 | ||
18 | On machines where the main swap file is on flash memory, zramswap may only | |
19 | provide marginal performance increase (especially on fast SSD media), | |
20 | however, it will prevent some amount of wear on your flash memory. zramswap | |
21 | is especially beneficial if your main flash is on SD/MMC/CF card. | |
22 | ||
23 | On servers that run many virtual machines or containers, zramswap allows you to | |
24 | optimise memory usage by swapping out data that's not often accessed, but when | |
25 | a user needs to access it, it will be available fast. It allows you to | |
26 | overcommit on memory with a negligible hit on your application performance | |
27 | (and often an improvement in performance where you can use more main memory | |
28 | for filesystem cache). | |
29 | ||
30 | .SH OPTIONS | |
31 | Usage: zramswap [OPTION] | |
32 | ||
33 | .B ZRAMSWAP Options | |
34 | ||
35 | \fB\ start\fR | |
36 | .RS | |
37 | Sets up zram swap. | |
38 | .RE | |
39 | ||
40 | \fB\ stop\fR | |
41 | .RS | |
42 | Tears down zram swap. | |
43 | .RE | |
44 | ||
45 | \fB\ status\fR | |
46 | .RS | |
47 | Displays zram swap usage and compression ratio. | |
48 | .RE | |
49 | ||
50 | .SH REPORTING BUGS | |
51 | Please file issues at: https://salsa.debian.org/jcc/zram-tools/issues | |
52 | ||
53 | .SH AUTHORS | |
54 | This manual page was written by Jonathan Carter <jcarter@linux.com> |
0 | [Unit] | |
1 | Description=Linux zramswap setup | |
2 | Documentation=man:zramswap(8) | |
3 | ||
4 | [Service] | |
5 | EnvironmentFile=-/etc/default/zramswap | |
6 | ExecStart=/usr/sbin/zramswap start | |
7 | ExecStop=/usr/sbin/zramswap stop | |
8 | ExecReload=/usr/sbin/zramswap restart | |
9 | Type=oneshot | |
10 | RemainAfterExit=true | |
11 | ||
12 | [Install] | |
13 | WantedBy=multi-user.target |