Swap file not mounting on boot

Hi all. My swap file I created isn't mounting on boot!

I used this guide.
https://www.thegeekstuff.com/2010/08/how-to-add-swap-space/?utm_source=feedburner

My /etc/fstab:

# <file system> <mount point> <type> <options> <dump> <pass>
/mnt/sda1/Utilities/swap none swap sw 0 0

Swap file mounts just fine when I

swapon /mnt/sda1/Utilities/swap

Any thoughts?

How are you mounting sda1? Are you sure it gets mounted before the swap file?

I mounted sda1 through the web interface and I imagine that updated /etc/config/fstab

config mount
option enabled '1'
option uuid 'd7e5eabc-872f-463d-aeb7-5aea406b320f'
option target '/mnt/sda1'

I'm not sure that's the problem because when I try swapoff and on with the -a attribute (I believe that re-reads the /etc/fstab config) it doesn't work.

root@LEDE:~# swapoff -a
root@LEDE:~# free -k
total used free shared buffers cached
Mem: 513600 488220 25380 18100 2664 416484
-/+ buffers/cache: 69072 444528
Swap: 0 0 0

but when I try

root@LEDE:~# swapon -a
root@LEDE:~# free 0k
total used free shared buffers cached
Mem: 513600 488452 25148 18328 4216 412944
-/+ buffers/cache: 71292 442308
Swap: 0 0 0

Swap is still not being mounted. Only

swap /mnt/sda1/Utilities/swap

does the trick.

You have configured the device at "/etc/config/fstab", and the swap at "/etc/fstab"... I would try to configure both on the same file, preferably at "/etc/config/fstab".

They seem to be two different files though...

/etc/fstab
# <file system> <mount point> <type> <options> <dump> <pass>
/mnt/sda1/Utilities/swap none swap sw 0 0

etc/config/fstab

config global
    option anon_swap '0'
    option anon_mount '0'
    option auto_swap '1'
    option auto_mount '1'
    option delay_root '15'
    option check_fs '0'

config mount
        option enabled '1'
        option uuid 'd7e5eabc-872f-463d-aeb7-5aea406b320f'
        option target '/mnt/sda1'

I'm not sure why it's not mounting at boot, but fwiw i added my swap through Luci and it only shows up in /etc/config/fstab, my /etc/fstab is empty. Here's what my swap looks like in /etc/config/fstab -

    config global
    	option anon_swap '0'
    	option anon_mount '0'
    	option auto_swap '1'
    	option auto_mount '1'
    	option delay_root '5'
    	option check_fs '0'    


config swap
    	option enabled '1'
    	option device '/dev/sda1'

The file "/etc/fstab" belongs to the standard Linux utils, and the "/etc/config/fstab" belongs to LEDE/OpenWrt's "uci" system; to avoid problems, I would use just one of them; if you want to use the LuCi interface, you have to use the "/etc/config/fstab" file.

@mike pointed out a method to config the swap, but notice that you are using a file inside "/dev/sda1", and not the whole partition.

That makes sense. How do I config Luci's swap file at /etc/config/fstab to recognize a file as a swap?

In my system log it has this:
[ 7.415461] block: attempting to load /tmp/ubifs_cfg/upper/etc/config/fstab
[ 7.424005] block: extroot: not configured
[ 7.461952] mount_root: switching to ubifs overlay

Any ideas on this one?

You seem to have 512MB RAM. Why do you want to have swap?

  1. Because you think you need swap
  2. Because you know you need swap (please explain further)

Good point. I run transmission-daemon as well as an Apple File Server (for Time Machine backup). I routinely have less than 10% total available memory on my status page when all things are running.

Also, in reading through here: https://wiki.openwrt.org/doc/uci/transmission?s[]=transmission

It says transmission runs much better when a swap is mounted.

I've always used partitions rather than files for swap so i'm not sure, my guess would be that you would just make the swap file and point to it with option device in /etc/config/fstab, but i've never tried with a file so i just don't know for sure.

Fwiw i added swap for that reason too (transmission), but on my wrt1200 with 512mb ram i can run transmission and minidlna at the smae time and never use more than 1-2% swap, so you may not even need it as @tmomas pointed out.

The advice is written for the usual routers, which used to have 32 or 64 MB of RAM.

Your 512 MB practically guarantees that you do not need swap. Most likely transmission and your Apple thing just try to use all free memory that they can get, as a buffer, but you can likely limit that amount in its settings.

And most likely some of the "used" memory is soft-grabbed cache (or buffers) e.g. for disk handling, not really used. Transmission can be disk intensive, so it is likely that Linux allocated large disk buffers when there is lots of disk activity and you are not needing the RAM for any other actual usage.

Example from my router, notice the difference of LuCI's hard/pure free 387M and the actual "soft" free with temporary buffers and cache included is 410M as displayed on the second line of "free" output. The difference is not that big, as there is nothing disk intensive going on:

I think that you are solidly on the path of "Because you think you need swap".

Ps. You might also read a rather good explanation from

1 Like

Gotcha. The only other rub here is that I installed rclone (https://rclone.org/) on the router and sometimes use that to sync my Download drive from transmission on the router with the cloud (OpenWrt is awesome!). I'll monitor the usage when that's going on but I think you're right for now I might not need the swap.

If you later decide you need swap, I would follow mike's advice and use a partition instead of a file.

I would do that, I just couldn't find a good guide on how to partition out a drive without any data loss if that's even possible.

@ryrhrbhdd Have a look at GParted (but I would always have a back-up copy of all important information on the drive, so you need another drive anyways).

I had trouble mounting multiple swap FILES residing on different partitions but solved like this on the command line:

CREATE SWAP AS A FILE ON FIRST DISK DRIVE:

dd if=/dev/zero of=/mnt/sda1/swapfile bs=1M count=256
chmod 600 /mnt/sda1/swapfile
mkswap /mnt/sda1/swapfile

CREATE ANOTHER ON SECOND DISK DRIVE:

dd if=/dev/zero of=/mnt/sdb1/swapfile bs=1M count=256
chmod 600 /mnt/sdb1/swapfile
mkswap /mnt/sdb1/swapfile

FOOLISH THINGS:

uci set fstab.@global[0].anon_swap='1'
uci set fstab.@global[0].auto_swap='1'

CLEAN HOUSE:

while uci -q delete fstab.@swap[-1]; do :; done

AUTOSTART ENTRY 1 WITH A FAKE LABEL:

uci add fstab swap
uci set fstab.@swap[-1].enabled='1'
uci set fstab.@swap[-1].device='/mnt/sda1/swapfile'
uci set fstab.@swap[-1].label='foo'

AUTOSTART ENTRY 2 WITH A FAKE LABEL:

uci add fstab swap
uci set fstab.@swap[-1].enabled='1'
uci set fstab.@swap[-1].device='/mnt/sdb1/swapfile'
uci set fstab.@swap[-1].label='bar'

COMMIT AND REBOOT FOR PROOF:

uci commit fstab
reboot

AND WE HAVE SUCCESS:

free
Swap: 524280

w00h00!
Now you can plug and unplug hard drives as either sda1 or sdb1 while the power is off, and as long as one of them contains a swapfile file then it'll mount at bootup.

2 Likes

As I stated here What caused deleting contents of /overlay/upper?, I'm in a big problem and reset doesn't survive me from this. Could you please help?