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
复制
# 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 /swapfile2. Enable the Swap File
bash
复制
# Activate immediately
sudo swapon /swapfile
# Verify it's working
swapon --show
free -h3. Make It Permanent
Edit
/etc/fstab to add:plain
复制
/swapfile none swap sw 0 0Configuring 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
复制
# Find the physical offset
sudo filefrag -v /swapfile | head -20Look for the first physical offset number (e.g.,
38912).2. Find Your Root Filesystem UUID
bash
复制
# Get the UUID of the partition containing /swapfile
findmnt -no UUID -T /swapfile3. Configure Kernel Boot Parameters
Edit
/etc/default/grub and modify GRUB_CMDLINE_LINUX:bash
复制
# Example (replace with your actual UUID and offset):
GRUB_CMDLINE_LINUX="resume=UUID=your-root-uuid resume_offset=38912"Then regenerate GRUB config:
bash
复制
sudo grub2-mkconfig -o /boot/grub2/grub.cfg4. Configure initramfs
Create or edit
/etc/dracut.conf.d/resume.conf:plain
复制
add_dracutmodules+=" resume "Then regenerate initramfs:
bash
复制
sudo dracut -f5. Enable Hibernate in Systemd
bash
复制
# Check current sleep modes
cat /sys/power/state
# Test hibernation (save work first!)
systemctl hibernateImportant Considerations
复制
| Factor | Recommendation |
|---|---|
| Swap Size | For hibernation, swap file must be ≥ RAM size |
| Encryption | If using LUKS, resume offset calculation differs |
| Btrfs | If / is Btrfs, you need a swapfile on a dedicated subvolume or use a swap partition instead |
Quick Check Script
bash
复制
#!/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_offsetvalue 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
复制
| Feature | Zswap | ZRAM |
|---|---|---|
| What it does | Compresses pages before swapping to disk | Creates compressed RAM disk for swap |
| Needs disk swap? | Yes (requires backing swap device) | No (self-contained in RAM) |
| Memory pressure | Frees RAM under pressure | Consumes RAM for swap storage |
| Hibernation support | ✅ Yes (with disk swap file/partition) | ❌ No (data lost on shutdown) |
| Best for | Systems with slow disks/SSDs | Systems with ample RAM, no hibernation |
| Performance gain | Reduces I/O, faster than disk | Fastest 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
复制
# 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
复制
options zswap enabled=1
options zswap compressor=zstd # Best compression/speed ratio
options zswap zpool=zsmalloc # Good for small objectsOr use kernel parameters in
/etc/default/grub:bash
复制
GRUB_CMDLINE_LINUX="zswap.enabled=1 zswap.compressor=zstd zswap.zpool=zsmalloc resume=UUID=xxx resume_offset=yyy"Then regenerate GRUB and reboot:
bash
复制
sudo grub2-mkconfig -o /boot/grub2/grub.cfg
sudo reboot3. Verify After Reboot
bash
复制
# Check compression ratio and stats
grep -r . /sys/kernel/debug/zswap/ 2>/dev/nullZRAM (Alternative - No Hibernation)
If you decide against hibernation, ZRAM provides better performance:
bash
复制
# 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 = zstdTrade-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
复制
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 systemsPerformance Tips
复制
| Parameter | Recommended Value | Why |
|---|---|---|
compressor | zstd | Best balance of speed/ratio |
zpool | zsmalloc | Efficient for small pages |
max_pool_percent | 20-30 | Limit Zswap memory usage |
Would you like help tuning Zswap parameters or setting up the hybrid approach?