17

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.)

3
  • What do you have in /etc/fstab?
    – eftshift0
    May 28, 2018 at 2:54
  • @eftshift0 - see update at the bottom of the post; there is nothing about a swap partition in the swap file, just mention of dphys-swapfile. May 28, 2018 at 3:03
  • Also, if I run sudo update-rc.d -f dphys-swapfile remove there's still a dphys-swapfile startup script inside /etc/init.d... Do I have to manually delete that file? May 28, 2018 at 3:07

2 Answers 2

22

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
4
  • How exactly is it better than the other suggestions? Jul 18, 2019 at 9:24
  • @DmitryGrigoryev, the other solution leave errors on startup. It doesn't.
    – Feriman
    Jul 18, 2019 at 21:57
  • Explaining that your solution avoids startup errors (and perhaps saying which ones) would make your answer much better than just claiming it's "the best way". Jul 19, 2019 at 6:46
  • @DmitryGrigoryev, you're right, I edited my answer. Thanks!
    – Feriman
    Jul 22, 2019 at 16:25
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

1
  • 6
    So... as it turns out, I had the command I was running split up on three lines but in a way that bash was ignoring the 2nd and 3rd lines—so dphys-swapfile swapoff was always evaluated, but the other two commands (including the remove one) were not. Therefore it would come back on reboot. D'oh! Quoting things in bash... May 29, 2018 at 23:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.