4

The swap file created during install, when deleted, is recovered after reboot.

I did a clean install of Ubuntu 22.04 64-bit Desktop on a Raspberry Pi 4 8 GB using a Kingston 960 GB SSD. Added a swap partition and deleted the swap file. When I reboot, the swap file is back. Something is recovering the deleted swap file.

root@RPi:~# free
               total        used        free      shared  buff/cache   available
Mem:         7994256      832340     4953420      425556     2208496     6480364
Swap:       13631824           0    13631824
root@RPi:~# swapon
NAME      TYPE       SIZE USED PRIO
/dev/sda4 partition   12G   0B   -2
/swapfile file      1024M   0B   -3
root@RPi:~# swapoff -av
swapoff /swapfile
swapoff /dev/sda4
root@RPi:~# free
               total        used        free      shared  buff/cache   available
Mem:         7994256      848096     4925720      433404     2220440     6455352
Swap:              0           0           0
root@RPi:~# swapon -av
swapon: /dev/sda4: found signature [pagesize=4096, signature=swap]
swapon: /dev/sda4: pagesize=4096, swapsize=12885254144, devsize=12885254144
swapon /dev/sda4
root@RPi:~# free
               total        used        free      shared  buff/cache   available
Mem:         7994256      850320     4923016      433812     2220920     6452704
Swap:       12583252           0    12583252
root@RPi:~# ll /swapfile
-rw------- 1 root root 1073741824 Jul  1 22:37 /swapfile
root@RPi:~# rm -v /swapfile
removed '/swapfile'
root@RPi:~# ll /swapfile
ls: cannot access '/swapfile': No such file or directory
root@RPi:~# 

reboot

root@RPi:~# free
               total        used        free      shared  buff/cache   available
Mem:         7994256      889208     4879004      434496     2226044     6414208
Swap:       13631824           0    13631824
root@RPi:~# swapon
NAME      TYPE       SIZE USED PRIO
/dev/sda4 partition   12G   0B   -2
/swapfile file      1024M   0B   -3
root@RPi:~# ll /swapfile
-rw------- 1 root root 1073741824 Jul  1 22:37 /swapfile
root@RPi:~# 
root@RPi:~# cat /etc/fstab
#LABEL=writable /   ext4    discard,x-systemd.growfs    0   1
#LABEL=system-boot       /boot/firmware  vfat    defaults        0       1
UUID=D7A9-3EE6       /boot/firmware  vfat    defaults        0       1
UUID=09799e9f-8009-4b0c-84bc-9761d73d9670   /   ext4    discard,x-systemd.growfs    0   1
UUID=6e6305ff-5e0c-4918-9de7-0f102acabe93    /home     ext4    defaults    0 0
UUID=3af55a00-f9ba-4121-b77a-08f4e911459d none swap sw 0 0
root@RPi:~# 

Note the timestamp on /swapfile.

I did stumble over the behavior of swapon -av not finding /swapfile, even though the swap file has not been deleted. That may be related to my earlier fumbling around trying to delete the swap file. I'm pretty sure that I did a swapoff /swapfile earlier, probably a couple of times.

I don't know if this problem is unique to Ubuntu 22.04, or unique to RPi, or both.

I'm not finding anything on the Internet about this particular problem, mostly the steps I've used to try to disable and delete the swap file. Not critical, but annoying.

update 1

The swap in fstab is the partition, not the file.

root@RPi:~# swapon --output-all
NAME      TYPE       SIZE USED PRIO UUID                                 LABEL
/dev/sda4 partition   12G   0B   -2 3af55a00-f9ba-4121-b77a-08f4e911459d swap
/swapfile file      1024M   0B   -3 51ac3104-f6c0-4688-9b82-a4ad60bc9661 
root@RPi:~# 

update 2 with answer

Raffa's answer fixed the problem! Thank you Raffa, tomorrow I will go out and have an espresso in your honor. :-)

root@RPi:~# 
root@RPi:~# systemctl status "*.swap"
● dev-disk-by\x2duuid-3af55a00\x2df9ba\x2d4121\x2db77a\x2d08f4e911459d.swap - /dev/disk/by-uuid/3af55a00-f9ba-4121-b77a-08f4e911459d
     Loaded: loaded (/etc/fstab; generated)
     Active: active since Fri 2022-07-01 22:38:27 PDT; 2 days ago
       What: /dev/disk/by-uuid/3af55a00-f9ba-4121-b77a-08f4e911459d
       Docs: man:fstab(5)
             man:systemd-fstab-generator(8)
      Tasks: 0 (limit: 8949)
     Memory: 148.0K
        CPU: 25ms
     CGroup: /system.slice/dev-disk-by\x2duuid-3af55a00\x2df9ba\x2d4121\x2db77a\x2d08f4e911459d.swap

Jul 01 22:38:27 RPi systemd[1]: Activating swap /dev/disk/by-uuid/3af55a00-f9ba-4121-b77a-08f4e911459d... Jul 01 22:38:27 RPi systemd[1]: Activated swap /dev/disk/by-uuid/3af55a00-f9ba-4121-b77a-08f4e911459d.

● swapfile.swap - The default swapfile Loaded: loaded (/lib/systemd/system/swapfile.swap; static) Active: active since Fri 2022-07-01 22:38:28 PDT; 2 days ago What: /swapfile Tasks: 0 (limit: 8949) Memory: 1.2M CPU: 11ms CGroup: /system.slice/swapfile.swap

Jul 01 22:38:28 RPi systemd[1]: Activating swap The default swapfile... Jul 01 22:38:28 RPi systemd[1]: Activated swap The default swapfile. root@RPi:~# root@RPi:~# root@RPi:~# systemctl mask swapfile.swap Created symlink /etc/systemd/system/swapfile.swap → /dev/null. root@RPi:~# root@RPi:~# root@RPi:~# swapoff -av swapoff /swapfile swapoff /dev/sda4 root@RPi:~# root@RPi:~# root@RPi:~# swapon -av swapon: /dev/sda4: found signature [pagesize=4096, signature=swap] swapon: /dev/sda4: pagesize=4096, swapsize=12885254144, devsize=12885254144 swapon /dev/sda4 root@RPi:~# root@RPi:~# root@RPi:~# swapon --output-all NAME TYPE SIZE USED PRIO UUID LABEL /dev/sda4 partition 12G 0B -2 3af55a00-f9ba-4121-b77a-08f4e911459d swap root@RPi:~#

reboot

root@RPi:~# 
root@RPi:~# swapon --output-all
NAME      TYPE      SIZE USED PRIO UUID                                 LABEL
/dev/sda4 partition  12G   0B   -2 3af55a00-f9ba-4121-b77a-08f4e911459d swap
root@RPi:~# 

update 3

Bad words and foul language!! I should rename this post to "the swapfile that would not die".

The swapfile keeps getting recovered ... at least I think it is being recovered. The timestamp and size appear to be the same, so at least the directory entry is being un-deleted and I suspect the same file blocks are being used. It appears that swap is not using the swapfile.

I'm sorry Raffa, I have to un-select your answer, but you still have my up-vote.

root@RPi:~# 
root@RPi:~# swapon --output-all
NAME      TYPE      SIZE USED PRIO UUID                                 LABEL
/dev/sda4 partition  12G   0B   -2 3af55a00-f9ba-4121-b77a-08f4e911459d swap
root@RPi:~# 
root@RPi:~# 
root@RPi:~# ll /swapfile 
-rw------- 1 root root 1073741824 Jul  1 22:38 /swapfile
root@RPi:~# 
root@RPi:~# 
root@RPi:~# rm -v /swapfile 
removed '/swapfile'
root@RPi:~# 
root@RPi:~# 
root@RPi:~# ll /swapfile 
ls: cannot access '/swapfile': No such file or directory
root@RPi:~# 

reboot

root@RPi:~# 
root@RPi:~# ll /swapfile 
-rw------- 1 root root 1073741824 Jul  1 22:38 /swapfile
root@RPi:~# 
root@RPi:~# swapon --output-all
NAME      TYPE      SIZE USED PRIO UUID                                 LABEL
/dev/sda4 partition  12G   0B   -2 3af55a00-f9ba-4121-b77a-08f4e911459d swap
root@RPi:~# 
  • 1
    Remove the swap file entry from /etc/fstab. – waltinator Jul 02 '22 at 22:26
  • @waltinator, the swap entry in fstab is the partition, not the file. – WaffleHouse2t2 Jul 03 '22 at 00:58
  • Hmm, I've been having serious problems with this brand new SSD, thinking it might be bad blocks. Now I'm wondering if it might be cross-linked blocks when I deleted the swapfile, some other file may have used some (at the time) free blocks, reboot, swapfile recovered, and blocks used in multiple files. Bugger! – WaffleHouse2t2 Jul 04 '22 at 18:47
  • The swap file will eventually die :-) take a look at https://manpages.ubuntu.com/manpages/trusty/man8/dphys-swapfile.8.html and run sudo apt remove dphys-swapfile … ***carefully check no other packages are to be removed before you answer yes** I will update the answer tomorrow as now I’m commenting on my phone. – Raffa Jul 04 '22 at 19:40
  • @Raffa, tried the sudo apt remove dphys-swapfile, got Package 'dphys-swapfile' is not installed, so not removed. – WaffleHouse2t2 Jul 04 '22 at 20:19
  • Then, AFAIK, that behavior of the swap file is not justified ... will try to investigate tomorrow on a VM and let you know if I discover something ... That file, however, cannot be used now for swap. – Raffa Jul 04 '22 at 20:27
  • 1
    I checked on a new install of Ubuntu 22.04 and successfully disabled and removed /swapfile and rebooted multiple times and confirmed it was gone forever(normal expected behavior) ... So, I can now confirm that this behavior(/swapfile coming back from the dead) is neither related to swap(anymore) nor related to a fresh install of Ubuntu 22.04 ... However, inspecting the output of journalctl -o short-precise -k -b all | grep -i swap might help understand what is happening – Raffa Jul 05 '22 at 08:03
  • 1
    @Raffa, ran journalctl -o short-precise -k -b all | grep -i swap, lot of swapper, zswap, swap partition. Reran replacing swap with swapfile, just adding swapfile to swap before masking swapfile from your answer, nothing about recovering the swapfile. – WaffleHouse2t2 Jul 06 '22 at 01:14
  • 1
    Since this is "unexpected behavior" and new install, I'm calling this done. I'm going to format the drive and do another install (see if I can make the swap partition during the install this time). Also marking your answer as accepted. And I did have an espresso this morning and said good things about you in your (hopefully) general direction. Thank you Raffa and take care. – WaffleHouse2t2 Jul 06 '22 at 01:14

1 Answers1

5

A systemd .swap unit configuration file is automatically generated for each swap entry in /etc/fstab and named after the swap file or device ... If removing/commenting the entry in /etc/fstab doesn't reflect in systemd(check with systemctl status "*.swap"), then you might, as well, need to mask that unit configuration file(e.g. swapfile.swap) like so:

sudo systemctl mask swapfile.swap

You can unmask it later when needed like so:

sudo systemctl unmask swapfile.swap
Raffa
  • 35,113
  • For those watching from the stands, this answer turned off '/swapfile' from being used as swap. When '/swapfile' is deleted, the file is recovered during boot, but '/swapfile' is not used as swap. Something is un-deleting '/swapfile'. Clear as mud? Super! – WaffleHouse2t2 Jul 05 '22 at 02:21