21

I'm at my wit's end; it seems there's no way to tell Raspbian Stretch Lite to permanently disable swap space.

Now, it's a bit of an edge case, wanting to keep swap turned off (in this case it's for a Kubernetes cluster, so kubectl doesn't complain about swap being enabled)... but it's something that's easy enough to do on most any other platform I've tried.

Here's what I'm currently doing (seems to be recommended all over the Internet as the normal way of disabling swap space permanently):

sudo dphys-swapfile swapoff && \
sudo dphys-swapfile uninstall && \
sudo systemctl disable dphys-swapfile

(Whether you use systemctl disable or update-rc.d remove, it does the same thing—and I've tried both.)

Every time I reboot the Pi, I get:

$ free -h
              total        used        free      shared  buff/cache   available
Mem:           976M         89M        579M        6.5M        308M        829M
Swap:           99M          0B         99M

And then I have to run the commands to disable swap again. Am I going mad or is there really no way to tell the Pi to stop creating swap space on boot!?

The Pi/Raspbian Stretch seems to not use fstab as would be standard on most of the other systems I manage; after reboot, when free -h shows 99M of swap, and kubelet complains that swap needs to be disabled, this is the exact contents of the swap file on one of the nodes:

$ cat /etc/fstab
proc            /proc           proc    defaults          0       0
PARTUUID=c220ffa8-01  /boot           vfat    defaults          0       2
PARTUUID=c220ffa8-02  /               ext4    defaults,noatime  0       1
# a swapfile is not a swap partition, no line here
#   use  dphys-swapfile swap[on|off]  for that
10.0.100.61:/srv/nfs /mnt/nfs nfs noauto,x-systemd.automount,x-systemd.device-timeout=10,timeo=14 0 0

(Note that it contains verbiage about using dphys-swapfile there.)

geerlingguy
  • 1,676
  • 2
  • 16
  • 22

2 Answers2

26

To avoid an error on startup regarding dphys service, try to disable swap on this way:

sudo dphys-swapfile swapoff
sudo dphys-swapfile uninstall
sudo update-rc.d dphys-swapfile remove
sudo apt purge dphys-swapfile -y

I'd also suggest to set swapiness to 0 to avoid high CPU usage of the kswapd0 process:

sudo sysctl -w vm.swappiness=0
Feriman
  • 386
  • 3
  • 5
12

I used to use sudo update-rc.d dphys-swapfile remove

I haven't tried recently so I'll assume that no longer works as you have tried using it.

Perhaps try CONF_SWAPSIZE=0 in /etc/dphys-swapfile

joan
  • 71,852
  • 5
  • 76
  • 108