How to Migrate Linux Swap Partition from /dev/hda3 to /dev/hda4: A Step-by-Step Guide


10 views

Before modifying swap partitions, it's crucial to understand their role in Linux systems. Swap space acts as virtual memory when physical RAM is fully utilized. The process involves:

  1. Identifying current swap usage with free -h or swapon --show
  2. Checking partition types using lsblk -f
  3. Verifying filesystem with blkid /dev/hda3

First, ensure your system meets these requirements:


# Check current swap space
sudo swapon --show

# Verify partition table
sudo fdisk -l /dev/hda

# Create backup of fstab
sudo cp /etc/fstab /etc/fstab.bak

Format /dev/hda4 as swap space:


# Set partition type (if not already Linux swap)
sudo fdisk /dev/hda
> t
> 4
> 82
> w

# Format as swap
sudo mkswap /dev/hda4

# Verify swap signature
sudo blkid /dev/hda4 | grep -i swap

The migration process requires careful sequencing:


# Disable current swap
sudo swapoff /dev/hda3

# Enable new swap
sudo swapon /dev/hda4

# Verify activation
free -h

Make the change persistent across reboots:


# Edit fstab
sudo nano /etc/fstab

# Replace or comment old entry
# /dev/hda3 none swap sw 0 0
/dev/hda4 none swap sw 0 0

If you want to repurpose /dev/hda3:


# Clear swap signature
sudo wipefs -a /dev/hda3

# Create new filesystem
sudo mkfs.ext4 /dev/hda3

Confirm the migration was successful:


# Check active swap
cat /proc/swaps

# Test under load
stress --vm 1 --vm-bytes $(free -g | awk '/Mem:/ {print $2}')G --vm-keep

Watch for these potential problems:

  • Incorrect permissions on /dev/hda4
  • Missing fstab entries causing boot failures
  • Memory pressure during swap transition

For emergency recovery, boot with a Live CD and check /etc/fstab.


Before migrating swap partitions, let's clarify what swap space does. Swap acts as virtual memory extension when physical RAM is exhausted. Linux uses dedicated partitions (like /dev/hda3) or swap files for this purpose. The process involves:

  • Identifying current swap usage with free -h or swapon --show
  • Checking partition types using lsblk -f

First, verify your current setup and prepare the new partition:


# Check current swap devices:
sudo swapon --show

# Verify partition types:
sudo fdisk -l /dev/hda

# Prepare new partition (if not already done):
sudo fdisk /dev/hda
# Within fdisk: n (new), p (primary), 4 (partition number), set type to 82 (Linux swap)

Here's the complete process to transition from /dev/hda3 to /dev/hda4:


# Step 1: Disable current swap
sudo swapoff /dev/hda3

# Step 2: Format new partition
sudo mkswap /dev/hda4

# Step 3: Update fstab (backup first!)
sudo cp /etc/fstab /etc/fstab.bak
sudo nano /etc/fstab
# Replace /dev/hda3 with /dev/hda4, keeping other parameters same

# Step 4: Enable new swap
sudo swapon /dev/hda4

# Step 5: Verify activation
free -h

Common challenges and their solutions:


# If swap isn't activating:
sudo blkid /dev/hda4  # Verify UUID
sudo swapon -a  # Activate all swaps in fstab

# For systems using UUID references:
sudo findmnt -no UUID -T /dev/hda4
# Update fstab with new UUID

When choosing a new swap partition location:

  • SSD-based swap offers faster performance than HDD
  • Consider swap priority if using multiple swap areas
  • Monitor performance with vmstat 1 after migration