← Back to Dashboard

Hybrid Swap Expansion Guide

Scenario: You increased the Disk Offering in CloudStack, but growpart expanded the root partition (/) to consume all space. Since filesystems cannot be shrunk, we will use a Hybrid Swap approach.

1. Resource Assessment

Verify your current disk layout and swap usage before starting.

2. Create the Swap File

We will use the dd utility to pre-allocate 4GB of space.

# Allocate the file (1024 * 4 = 4096)
sudo dd if=/dev/zero of=/swapfile bs=1M count=4096

# Restrict Permissions
sudo chmod 600 /swapfile

# Format as Swap
sudo mkswap /swapfile

3. Activation and Persistence

# Enable the file
sudo swapon /swapfile

# Verify total swap (should show ~5GB total)
free -h

# Configure permanent mounting
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

4. Optimization (Swappiness)

Adjust "swappiness" to ensure the kernel only uses swap when necessary.

# Check current value (Default 60)
cat /proc/sys/vm/swappiness

# Set to 10 and make permanent
sudo sysctl vm.swappiness=10
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf

Troubleshooting & FAQ

Can I delete it later?
Yes. Run sudo swapoff /swapfile, remove the line from /etc/fstab, and sudo rm /swapfile.

Is it slower than LVM?
No. On modern Linux kernels, there is no measurable performance difference between a swap partition and a swap file.