To move the dphys-swapfile from a Btrfs volume to a magnetic disk, you must first disable and remove the existing swapfile using dphys-swapfile swapoff and dphys-swapfile purge. Next, configure the new path in /etc/dphys-swapfile, recreate the file on the HDD, and enable it with dphys-swapfile swapon. This ensures proper cleanup of the old Btrfs-based swap space.
Understanding Swapfile Limitations on Btrfs
Thedphys-swapfile utility is designed to manage a single swapfile located at /var/swap. While modern versions of Btrfs support swapfiles, many systems, particularly older Raspberry Pi distributions or specific embedded Linux setups, rely on dphys-swapfile for simplicity. However, running swap on a Btrfs filesystem can sometimes lead to performance issues or compatibility warnings because Btrfs uses copy-on-write (COW) semantics that may interfere with swap operations if not explicitly disabled for the swapfile location.
Moving the swapfile to a magnetic disk (HDD) is often recommended for systems where the primary storage is an SSD or SD card, as it reduces write amplification on the faster media. Magnetic disks handle large sequential writes differently than flash storage, making them suitable for the heavy read/write cycles associated with swapping memory pages. Before proceeding, ensure your target magnetic disk is mounted and accessible. For instance, if you are moving to an external USB drive, verify its mount point using mount | grep sdX or check /etc/fstab for the correct device identifier.
Step 1: Disable and Remove the Existing Swapfile
The most critical step is to properly deactivate the current swapfile. Many users mistakenly delete the file directly usingrm /var/swap. This approach is dangerous because it leaves the system in an inconsistent state if the swap daemon has already unmapped the file but not fully released resources, or if the deletion fails midway. Instead, you must use the dphys-swapfile commands to handle the lifecycle correctly.
First, stop the usage of the swapfile by running:
sudo dphys-swapfile swapoff
This command tells the kernel to stop using the current swapfile and removes the mapping. It does not delete the file itself. Next, you must remove the actual file and reset the configuration state. Run:
sudo dphys-swapfile purge
The purge command deletes the swapfile at /var/swap and resets the configuration file /etc/dphys-swapfile to its default values. This ensures that no residual references to the old Btrfs-based swap location remain active. If you skip this step and simply create a new file, you might end up with two swapfiles or encounter permission errors because the system expects the default location to be managed exclusively by dphys-swapfile.
Step 2: Configure the New Swapfile Location
With the old swapfile removed, you need to instructdphys-swapfile to create the new file on your magnetic disk. Open the configuration file using a text editor such as nano or vim:
sudo nano /etc/dphys-swapfile
Look for the line starting with CONF_SWAPFILE=. By default, it will be set to /var/swap. Change this path to the desired location on your magnetic disk. For example, if your magnetic disk is mounted at /mnt/hdd, you might set:
CONF_SWAPFILE=/mnt/hdd/swapfile
Ensure that the directory /mnt/hdd exists and has sufficient free space. The swapfile size is determined by CONF_SWAPSIZE, which defaults to 100MB if not specified. You can adjust this value based on your RAM size, but keeping it reasonable (e.g., 512MB or 1GB) is usually sufficient for most desktop and server workloads. Save the file and exit the editor.
Step 3: Create and Enable the New Swapfile
Now that the configuration is updated, you can generate the new swapfile. Run:sudo dphys-swapfile setup
This command reads /etc/dphys-swapfile and creates the file at the specified path with the correct permissions (typically 600) and size. Once the file is created, activate it by running:
sudo dphys-swapfile swapon
Verify that the new swapfile is active by checking the output of swapon --show or free -h. You should see the new path listed under the NAME column. If the command fails, check the system logs using dmesg | tail for any permission denied errors or I/O faults. Ensure that the magnetic disk is not suspended or disconnected during this process.
Verifying Stability and Performance
After enabling the new swapfile, monitor the system for stability. Usevmstat 1 to observe page in/out activity. If the system becomes unresponsive, consider reducing the swappiness value in /etc/sysctl.conf to discourage excessive swapping. Additionally, since you are now using a magnetic disk, be aware that swap access times will be significantly slower than on SSDs or RAM. This is acceptable for occasional overflow but not for high-performance computing tasks.
For related information on managing filesystem transitions, you might find How to Convert ext4 Partition on RAID 1 to btrfs useful if you are planning further storage optimizations. Understanding how different filesystems handle swap can prevent future conflicts.
Frequently Asked Questions
Can I have multiple swapfiles on different disks?
Yes, Linux supports multiple swap files or partitions. However, dphys-swapfile is designed to manage only one swapfile at a time. To add additional swap spaces manually, you would need to use mkswap and swapon directly, bypassing dphys-swapfile for those specific entries.
What happens if the magnetic disk is disconnected?
If the magnetic disk containing the swapfile is disconnected while the system is running, the kernel may throw I/O errors, potentially leading to system instability or crashes. It is advisable to use nofail in /etc/fstab for non-critical swap drives to allow booting even if the disk is missing.
Why did my previous attempt fail after deleting /var/swap?
Directly deleting the file bypasses the kernel's swap unmapping process. The system still thinks the swapfile is active until you run dphys-swapfile swapoff. Skipping this step leaves the swap entry in the kernel's memory management tables, causing errors when you try to create a new file or re-enable swap.
Is it safe to keep swap on Btrfs?
While newer kernels support swapfiles on Btrfs, it requires disabling Copy-On-Write (COW) for the specific file using chattr +C. dphys-swapfile does not automatically apply this attribute, which is why moving to a standard filesystem like ext4 on a magnetic disk is often more reliable for this specific tool.
How do I revert to the default Btrfs swapfile?
To revert, edit /etc/dphys-swapfile and change CONF_SWAPFILE back to /var/swap. Then run sudo dphys-swapfile setup followed by sudo dphys-swapfile swapon. This will recreate the default swapfile in the original location.