@@ -4,83 +4,142 @@ Smart dynamic swap management for Linux, written in Rust.
44
55## Features
66
7- - ** Auto-detection** : Automatically chooses best swap strategy for your system
8- - ** btrfs optimized** : Uses zswap + swap files on btrfs
9- - ** Universal fallback** : Uses zram on non-btrfs systems
10- - ** Low memory** : ~ 250 KB vs ~ 10 MB Python version
7+ - ** Auto-detection** : Automatically chooses optimal swap strategy for your filesystem
8+ - ** Zswap + SwapFC** : Compressed RAM cache with dynamic btrfs swap files
9+ - ** Zram + SwapFC** : Alternative mode with zram as primary swap
10+ - ** Zram writeback** : Move idle pages from zram to disk (kernel 5.4+)
11+ - ** Btrfs optimized** : Sparse files with optional compression
12+ - ** Lightweight** : ~ 250 KB binary vs ~ 10 MB Python version
13+
14+ ## Swap Modes
15+
16+ | Mode | Description | Best For |
17+ | ------| -------------| ----------|
18+ | ` auto ` | Auto-detect: btrfs → zswap+swapfc, other → zram | Most users |
19+ | ` zswap+swapfc ` | Zswap cache + dynamic swap files | ** Desktop (btrfs)** |
20+ | ` zram+swapfc ` | Zram primary + swap files overflow | Memory-constrained |
21+ | ` zram ` | Zram only (no disk swap) | Non-btrfs systems |
22+ | ` manual ` | Use explicit settings | Advanced users |
23+
24+ ### How Each Mode Works
25+
26+ ** zswap + swapfc (default for btrfs)** :
27+ - Zswap compresses pages in RAM before writing to swap
28+ - SwapFC creates swap files dynamically as memory pressure increases
29+ - Best desktop performance with efficient memory usage
30+
31+ ** zram + swapfc** :
32+ - Zram creates compressed block device in RAM (highest priority)
33+ - SwapFC provides overflow to disk when zram is full
34+ - Optional: zram writeback moves idle pages to disk
35+
36+ ** zram only** :
37+ - All swap in compressed RAM
38+ - No disk I/O, ideal for Live USB
1139
12- ## How It Works
13-
14- | Filesystem | Strategy | Description |
15- | ------------| ----------| -------------|
16- | ** btrfs** | zswap + swapfc | Compressed RAM cache with dynamic swap files |
17- | ** other** | zram only | Compressed RAM disk |
40+ ## Installation
1841
19- ### Why This Choice?
42+ ``` bash
43+ # Build (requires Rust 1.70+)
44+ cargo build --release
2045
21- ** btrfs systems** : zswap compresses pages in RAM before writing to disk. Combined with dynamic swap files (swapfc), this provides efficient memory management with disk backing when needed.
46+ # Install
47+ sudo make install
2248
23- ** non-btrfs systems** : zram creates a compressed swap device entirely in RAM. This is ideal when swap files aren't supported or practical.
49+ # Enable
50+ sudo systemctl enable --now systemd-swap
51+ ```
2452
25- ## Installation
53+ ### Arch Linux / BigLinux
2654
2755``` bash
28- # Arch Linux / BigLinux
2956cd pkgbuild
3057makepkg -si
3158```
3259
3360## Usage
3461
3562``` bash
36- # Enable and start
37- sudo systemctl enable --now systemd-swap
38-
3963# Check status
4064systemd-swap status
4165
42- # View compression algorithms
66+ # View available compression algorithms
4367systemd-swap compression
68+
69+ # Restart after config changes
70+ sudo systemctl restart systemd-swap
71+
72+ # View logs
73+ journalctl -u systemd-swap -f
4474```
4575
4676## Configuration
4777
4878Edit ` /etc/systemd/swap.conf ` :
4979
5080``` ini
51- # Swap mode (default: auto)
52- # auto - Auto-detect filesystem
53- # zswap+swapfc - Force zswap with swap files
54- # zram - Force zram only
55- # manual - Use explicit settings
81+ # ###############################################################################
82+ # Swap Mode
83+ # ###############################################################################
5684swap_mode =auto
5785
58- # Zswap settings (for btrfs mode)
59- zswap_compressor =zstd
60- zswap_max_pool_percent =35
61- zswap_zpool =zsmalloc
62-
63- # Zram settings (for non-btrfs mode)
64- zram_size =$RAM_SIZE
65- zram_alg =zstd
66- zram_prio =32767
67-
68- # SwapFC settings (for btrfs mode)
69- swapfc_chunk_size =512M
70- swapfc_max_count =32
71- swapfc_path =/swapfc/swapfile # Can be on different partition
86+ # ###############################################################################
87+ # Zswap (used in zswap+swapfc mode)
88+ # ###############################################################################
89+ zswap_compressor =zstd # lzo lz4 zstd lzo-rle lz4hc
90+ zswap_max_pool_percent =45 # Max % of RAM for pool
91+ zswap_zpool =zsmalloc # Memory allocator
92+ zswap_shrinker_enabled =1 # Move cold pages to disk
93+ zswap_accept_threshold =80 # Accept threshold after pool full
94+
95+ # ###############################################################################
96+ # Zram (used in zram modes)
97+ # ###############################################################################
98+ zram_size =50% # 50%, 1G, 512M, 100%
99+ zram_alg =zstd # Compression algorithm
100+ zram_prio =32767 # Swap priority
101+
102+ # Zram writeback (requires CONFIG_ZRAM_WRITEBACK)
103+ zram_writeback =0 # 0=disabled, 1=enabled
104+ zram_writeback_dev = # Partition or empty for auto loop
105+ zram_writeback_size =1G # Auto backing file size
106+
107+ # ###############################################################################
108+ # SwapFC - Dynamic swap files (btrfs)
109+ # ###############################################################################
110+ swapfc_chunk_size =512M # Size of each swap file
111+ swapfc_max_count =32 # Maximum swap files
112+ swapfc_free_ram_perc =35 # Create when free RAM < this %
113+ swapfc_free_swap_perc =25 # Create more when free swap < this %
114+ swapfc_path =/swapfc/swapfile # Path (must be btrfs)
115+
116+ # Btrfs compression (experimental)
117+ swapfc_use_btrfs_compression =0 # Uses loop device over sparse file
72118```
73119
74120## Custom Swap Location
75121
76- You can create swap files on a different partition by setting ` swapfc_path ` :
122+ You can place swap files on a different btrfs partition :
77123
78124``` ini
79- # Use swap on separate btrfs partition
80125swapfc_path =/mnt/swap-drive/swapfile
81126```
82127
83- The path must be on a btrfs filesystem.
128+ ## Zram Writeback
129+
130+ Move idle/incompressible pages from zram to disk:
131+
132+ ``` ini
133+ # Enable with auto loop device
134+ zram_writeback =1
135+ zram_writeback_size =2G
136+
137+ # Or use dedicated partition
138+ zram_writeback =1
139+ zram_writeback_dev =/dev/sda5
140+ ```
141+
142+ Requires kernel compiled with ` CONFIG_ZRAM_WRITEBACK ` .
84143
85144## File Locations
86145
@@ -89,7 +148,15 @@ The path must be on a btrfs filesystem.
89148| ` /usr/bin/systemd-swap ` | Main binary |
90149| ` /etc/systemd/swap.conf ` | User configuration |
91150| ` /usr/share/systemd-swap/swap-default.conf ` | Default configuration |
92- | ` /run/systemd/swap/ ` | Runtime data |
151+ | ` /run/systemd-swap/ ` | Runtime data |
152+ | ` /swapfc/ ` | Swap files (default) |
153+
154+ ## Requirements
155+
156+ - Linux kernel 5.0+
157+ - Rust 1.70+ (build only)
158+ - btrfs-progs (for btrfs features)
159+ - util-linux (zramctl, losetup)
93160
94161## License
95162
0 commit comments