Here are the common Debian Swap recovery scenarios and steps:
1. Check Swap Status
Before starting recovery, verify the current Swap configuration using either:
sudo swapon --show(shows enabled Swap partitions/files and their sizes)cat /etc/fstab(displays system-configured Swap entries for auto-mount at boot).
This helps identify whether you’re dealing with a Swap partition (e.g.,/dev/sda2) or a Swap file (e.g.,/swapfile) and its current status.
2. Recover a Swap Partition
If your Swap partition is corrupted, deleted, or not mounting automatically:
- Create a New Swap Partition (if the original is permanently lost):
Usefdiskorgpartedto create a new partition on the target disk (e.g.,/dev/sda). Set the partition type to Linux Swap (ID82)—this is critical for the system to recognize it as Swap space. Save changes and exit the tool. - Format the Partition:
Runsudo mkswap /dev/sdXY(replacesdXYwith your actual partition, e.g.,/dev/sda2) to format the partition as Swap. This initializes the partition with the correct file system structure for Swap use. - Enable the Swap Partition:
Executesudo swapon /dev/sdXYto activate the newly formatted partition. This makes the Swap space available immediately for system use. - Update
/etc/fstabfor Persistence:
Edit the file withsudo nano /etc/fstaband add an entry to ensure the Swap partition mounts automatically at boot:
Save changes (Ctrl+O → Enter → Ctrl+X in/dev/sdXY none swap sw 0 0nano). The system will now enable the Swap partition on every restart.
3. Recover a Swap File
If your Swap file (commonly /swapfile) is missing, deleted, or corrupted:
- Restore from Backup (recommended if you have a backup):
Copy the backup file to the original location. For example, if your backup is/path/to/backup/swapfile:
If the backup is compressed (e.g.,sudo cp /path/to/backup/swapfile /swapfileswapfile.img.gz), decompress it first:sudo gunzip /path/to/backup/swapfile.img.gz sudo cp /path/to/backup/swapfile.img /swapfile - Recreate the Swap File (if no backup exists):
Useddto create a new Swap file of the desired size (e.g., 4GB). Replace/swapfilewith your preferred path:sudo fallocate -l 4G /swapfile # Faster alternative to dd sudo chmod 600 /swapfile # Set strict permissions (root-only access) sudo mkswap /swapfile # Format the file as Swap - Enable the Swap File:
Activate the Swap file withsudo swapon /swapfile. Verify it’s enabled usingsudo swapon --show.
4. Fix Existing but Non-Functional Swap Partitions
If the Swap partition exists but isn’t working (e.g., due to corruption), try repairing it:
- Run
sudo fsck -f /dev/sdXY(replacesdXYwith your partition) to check and fix file system errors. The-fflag forces a check even if the partition appears clean. - After repair, remount the Swap partition using
sudo swapon /dev/sdXY.
5. Post-Recovery Verification
Regardless of the recovery method, always confirm the Swap is active:
- Run
sudo swapon --showto list all enabled Swap spaces. You should see your recovered partition or file listed with its size and usage. - Use
free -hto check total Swap space available. The output should reflect the restored Swap capacity.