20.04.1, problem switching from swap partition to /swapfile

I decided to switch from using a swap partition, to using a /swapfile.

But there's a problem. The system is still using the swap partition /dev/sda8, and not the /swapfile.

  • I created the /swapfile using the appropriate dd command, etc.
  • I changed /etc/fstab to use /swapfile instead of the old swap partition
  • I changed /etc/initramfs-tools/conf.d/resume to reflect resume=/swapfile
  • I checked that /etc/default/grub has no reference to resume or swap

Yet, when I do swapon -s or free -h, it's still showing /dev/sda8, instead of /swapfile.

Do I need to delete the /dev/sda8 swap partition for this to work?

Does 20.04.1 use any swap partition that it finds, even if it's not in /etc/fstab?

Update #1:

Just to document my /swapfile creation process...

Note: Incorrect use of the dd command can cause data loss. Suggest copy/paste.

In the terminal...

sudo swapoff -a           # turn off swap
sudo rm -i /swapfile      # remove old /swapfile

sudo dd if=/dev/zero of=/swapfile bs=1M count=4096

sudo chmod 600 /swapfile  # set proper file protections
sudo mkswap /swapfile     # init /swapfile
sudo swapon /swapfile     # turn on swap
free -h                   # confirm 16G RAM and 4G swap

Confirm this /swapfile line at the end of /etc/fstab... and confirm no other “swap” lines...

To edit, use sudo -H gedit /etc/fstab or sudo pico /etc/fstab

/swapfile  none  swap  sw  0  0

reboot                    # reboot and verify operation

Here's my /proc/swaps (and it's fresh from this morning)...

Filename                Type        Size    Used    Priority
/dev/sda8                               partition   9081852 0   -2

Here's the appropriate portion of my /etc/fstab:

# swap was on /dev/sda8 during installation
# UUID=071f8b0e-8e16-4f4d-90ff-a4ae9cc56e2b  none  swap       sw       0       0
/swapfile    none    swap    sw      0   0

Here's my /etc/initramfs-tools/conf.d/resume:

# RESUME=UUID=071f8b0e-8e16-4f4d-90ff-a4ae9cc56e2b
RESUME=/swapfile

sudo update-initramfs -c -k $(uname -r) didn't help.


Solution 1:

Well, it's fixed.

When I tried to do a sudo swapoff -a, it gave me a parse error on the following line in my /etc/fstab...

/swapfile    none    swap    sw      0   0

I retyped the line, and made sure to use spaces...

/swapfile  none  swap  sw  0  0

Do you see any difference?

And now it all works!

Thanks for all of the good feedback here!

Solution 2:

Did you remove /dev/sda8 from the fstab? You can use sudo swapoff -a and then turn on the swap for the file by typing sudo swapon /swapfile. After that, you can delete/format the partition if all works well.