温馨提示×

Debian Swap恢复步骤

小樊
34
2025-11-11 08:32:19
栏目: 智能运维

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):
    Use fdisk or gparted to create a new partition on the target disk (e.g., /dev/sda). Set the partition type to Linux Swap (ID 82)—this is critical for the system to recognize it as Swap space. Save changes and exit the tool.
  • Format the Partition:
    Run sudo mkswap /dev/sdXY (replace sdXY with 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:
    Execute sudo swapon /dev/sdXY to activate the newly formatted partition. This makes the Swap space available immediately for system use.
  • Update /etc/fstab for Persistence:
    Edit the file with sudo nano /etc/fstab and add an entry to ensure the Swap partition mounts automatically at boot:
    /dev/sdXY none swap sw 0 0
    
    Save changes (Ctrl+O → Enter → Ctrl+X in nano). 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:
    sudo cp /path/to/backup/swapfile /swapfile
    
    If the backup is compressed (e.g., swapfile.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):
    Use dd to create a new Swap file of the desired size (e.g., 4GB). Replace /swapfile with 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 with sudo swapon /swapfile. Verify it’s enabled using sudo 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 (replace sdXY with your partition) to check and fix file system errors. The -f flag 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 --show to list all enabled Swap spaces. You should see your recovered partition or file listed with its size and usage.
  • Use free -h to check total Swap space available. The output should reflect the restored Swap capacity.

0