New Chat
Ctrl
K
Kimi Code 3x Boost
Chat History
Mobile App

Swap File, ZRAM, Zswap

I installed Fedora and I forgot to create a swap partition. How can I enable a swap file? Can I configure hibernation with such a swap file?
I'll help you set up a swap file on Fedora and configure hibernation. This is a common scenario and entirely fixable without reinstalling.

Creating a Swap File

1. Create the Swap File

bash
Copy
# Create a 4GB swap file (adjust count for different sizes)
sudo dd if=/dev/zero of=/swapfile bs=1G count=4 status=progress

# Set secure permissions
sudo chmod 600 /swapfile

# Format as swap
sudo mkswap /swapfile

2. Enable the Swap File

bash
Copy
# Activate immediately
sudo swapon /swapfile

# Verify it's working
swapon --show
free -h

3. Make It Permanent

Edit /etc/fstab to add:
plain
Copy
/swapfile none swap sw 0 0

Configuring Hibernation with Swap File

Yes, you can use a swap file for hibernation, but it requires additional configuration since Fedora needs to know exactly where to resume from.

1. Determine Your Swap File Offset

You need the physical offset of the swap file for the kernel resume parameter:
bash
Copy
# Find the physical offset
sudo filefrag -v /swapfile | head -20
Look for the first physical offset number (e.g., 38912).

2. Find Your Root Filesystem UUID

bash
Copy
# Get the UUID of the partition containing /swapfile
findmnt -no UUID -T /swapfile

3. Configure Kernel Boot Parameters

Edit /etc/default/grub and modify GRUB_CMDLINE_LINUX:
bash
Copy
# Example (replace with your actual UUID and offset):
GRUB_CMDLINE_LINUX="resume=UUID=your-root-uuid resume_offset=38912"
Then regenerate GRUB config:
bash
Copy
sudo grub2-mkconfig -o /boot/grub2/grub.cfg

4. Configure initramfs

Create or edit /etc/dracut.conf.d/resume.conf:
plain
Copy
add_dracutmodules+=" resume "
Then regenerate initramfs:
bash
Copy
sudo dracut -f

5. Enable Hibernate in Systemd

bash
Copy
# Check current sleep modes
cat /sys/power/state

# Test hibernation (save work first!)
systemctl hibernate

Important Considerations

Table
Copy
FactorRecommendation
Swap SizeFor hibernation, swap file must be ≥ RAM size
EncryptionIf using LUKS, resume offset calculation differs
BtrfsIf / is Btrfs, you need a swapfile on a dedicated subvolume or use a swap partition instead

Quick Check Script

bash
Copy
#!/bin/bash
echo "=== Swap Status ==="
swapon --show
echo ""
echo "=== Memory Info ==="
free -h
echo ""
echo "=== Resume Parameters ==="
cat /proc/cmdline | grep -o "resume=[^ ]*"

Troubleshooting

  • Hibernation fails to resume? Double-check the resume_offset value and UUID
  • Swap file too small? Create a larger one: sudo swapoff /swapfile, delete it, and recreate with larger size
  • Slow hibernation? Normal for large RAM systems; SSDs help significantly
Would you like me to elaborate on any specific step, such as the Btrfs-specific procedure or encrypted swap setup?
How about enabling Zswap or ZRAM? Which one is to be preferred?
Great question! Both Zswap and ZRAM are excellent performance optimizations, but they serve different purposes. Here's a detailed comparison to help you decide:

Quick Comparison

Table
Copy
FeatureZswapZRAM
What it doesCompresses pages before swapping to diskCreates compressed RAM disk for swap
Needs disk swap?Yes (requires backing swap device)No (self-contained in RAM)
Memory pressureFrees RAM under pressureConsumes RAM for swap storage
Hibernation support✅ Yes (with disk swap file/partition)❌ No (data lost on shutdown)
Best forSystems with slow disks/SSDsSystems with ample RAM, no hibernation
Performance gainReduces I/O, faster than diskFastest possible swap (RAM speed)

My Recommendation

For your use case (Fedora with swap file + hibernation): Use Zswap
Since you want hibernation, Zswap is the clear winner. ZRAM cannot support hibernation because swap data is stored in volatile RAM.

Enabling Zswap (Recommended for You)

Zswap is often already enabled by default on Fedora, but let's verify and optimize it:

1. Check Current Status

bash
Copy
# Check if Zswap is enabled
dmesg | grep -i zswap
cat /sys/module/zswap/parameters/enabled

# View current stats
cat /sys/kernel/debug/zswap/stats 2>/dev/null || echo "Debugfs not mounted"

2. Enable/Optimize Zswap (if not active)

Create /etc/modprobe.d/zswap.conf:
plain
Copy
options zswap enabled=1
options zswap compressor=zstd      # Best compression/speed ratio
options zswap zpool=zsmalloc       # Good for small objects
Or use kernel parameters in /etc/default/grub:
bash
Copy
GRUB_CMDLINE_LINUX="zswap.enabled=1 zswap.compressor=zstd zswap.zpool=zsmalloc resume=UUID=xxx resume_offset=yyy"
Then regenerate GRUB and reboot:
bash
Copy
sudo grub2-mkconfig -o /boot/grub2/grub.cfg
sudo reboot

3. Verify After Reboot

bash
Copy
# Check compression ratio and stats
grep -r . /sys/kernel/debug/zswap/ 2>/dev/null

ZRAM (Alternative - No Hibernation)

If you decide against hibernation, ZRAM provides better performance:
bash
Copy
# Install and enable
sudo dnf install zram-generator-defaults
sudo systemctl enable --now [email protected]

# Configure in /etc/systemd/zram-generator.conf
[zram0]
zram-size = ram / 2      # Use 50% of RAM
compression-algorithm = zstd
Trade-off: With ZRAM, you lose hibernation capability entirely.

Hybrid Approach (Advanced)

Some users run both:
  • ZRAM for light swap pressure (fast, no disk wear)
  • Zswap + disk swap for heavy pressure and hibernation
But this requires careful tuning to avoid conflicts. For most users, Zswap alone is sufficient.

Summary Decision Tree

plain
Copy
Do you need hibernation?
├── YES → Use Zswap + swap file (your current path)
│         └── Benefits: Hibernation works, reduces SSD wear,
│             compression before disk write
│
└── NO  → Use ZRAM (simpler, faster)
          └── Benefits: No disk I/O at all, maximum speed,
              good for RAM-rich systems

Performance Tips

Table
Copy
ParameterRecommended ValueWhy
compressorzstdBest balance of speed/ratio
zpoolzsmallocEfficient for small pages
max_pool_percent20-30Limit Zswap memory usage
Would you like help tuning Zswap parameters or setting up the hybrid approach?