What's new

Swap File Mount/Unmount

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

How to automatically mount a swap partition at startup and un-mount it at shutdown or when you unplug the USB storage device.

/jffs/configs/fstab
Code:
UUID=11111111-2222-3333-4444-555555555555  none  swap  defaults   0  0

Type blkid, to know the UUID of the partition.

Never use /dev/sda1 in /jffs/configs/fstab for a swap partition because you could mistakenly erase the wrong partition, when you have multiple USB storage devices.

If Asuswrt had enabled the Linux device mapper, you could encrypt the swap partition automatically. You never really know what's sitting in your swap space.
 
Last edited:
How to automatically mount a swap partition at startup and un-mount it at shutdown or when you unplug the USB storage device.

/jffs/configs/fstab
Code:
UUID=11111111-2222-3333-4444-555555555555      none                   swap     defaults          0  0

Type blkid, to know the UUID of the partition.

Never use /dev/sda1 in /jffs/configs/fstab for a swap partition because you could mistakenly erase the wrong partition, when you have multiple USB storage devices.

If Asuswrt had enabled the Linux device mapper, you could encrypt the swap partition automatically. You never really know what's sitting in your swap space.
Thanks for this useful info Fitz. Are all those spaces between UUID/noen/swap/etc needed exactly like that?

Kind Regards.
 
That would appear to be the case. Which would be a fairly poorly written program that doesn't check the available memory before trying to use it.:rolleyes:

Try giving it want it wants by recreating your swapfile as (say) 1GB.

Not to say that it's never been done - not throwing stones at glass houses - running off the edge of memory is like finding the edge of the flat earth - fascinating place that it is :D

Funny that the kernel OOM killer didn't run into the fire and put it out... unless it couldn't.

Going back to my post above - even with ZRam, KSM, etc, a bad process can make things interesting, normally not in a good way - esp with limited RAM devices like the asusWRT's...
 
Never use /dev/sda1 in /jffs/configs/fstab for a swap partition because you could mistakenly erase the wrong partition, when you have multiple USB storage devices.

If Asuswrt had enabled the Linux device mapper, you could encrypt the swap partition automatically. You never really know what's sitting in your swap space.

With AsusWRT, definitely agree there - and enabling mapper isn't really a do-able thing...
 
How to automatically mount a swap partition at startup and un-mount it at shutdown or when you unplug the USB storage device.

Is there a benefit of adding it to fstab compared to the (usually on this forum advised way of adding to the) post-mount script?

Code:
#!/bin/sh

swapon /tmp/mnt/usb1/swapfile.swp
 
Thanks for this useful info Fitz. Are all those spaces between UUID/noen/swap/etc needed exactly like that?

Kind Regards.

The spaces are there just to make it more clear. Are no important in this case.


Never use /dev/sda1 in /jffs/configs/fstab for a swap partition because you could mistakenly erase the wrong partition, when you have multiple USB storage devices..

Hi Fitz, I am using the fstab method as above to mount my USB swap partition for a while now. In fstab I have a second line to map the second partition of my USB as /dev/sda2. Therefore, my swap is always automount as /dev/sda1.
I do have another dock in USB3 port with two hard drives.
Just out of curiosity (so to keep in mind and not be that someone), how could someone mistakenly erase the wrong partition ?
 
Thanks M@rco for your suggestion (nice idea!), and thanks again Colin for sharing your knowledge and bumping it even higher.

I have set 1GB swap now. Will test it and if it fails I will try swap partition instead (I've already read how to accomplish it)

Will report back with results!

Thanks!!!
Write error still showing up with 1GB swap file. Guess I'll have to try to repartition my flash drive to create a swap partition and mount it using either traditional approach (post-mount) or fstab one.
 
Last edited:
Is there a benefit of adding it to fstab compared to the (usually on this forum advised way of adding to the) post-mount script?

Code:
#!/bin/sh

swapon /tmp/mnt/usb1/swapfile.swp
I think he's referring to using a swap partition instead of a swap file. I tried turning my USB drive into one swap partition but it wouldn't automount at startup (and therefore post-mount wouldn't run).

EDIT: I've been trying to use my USB drive as swap since I read this thread, but I haven't been able to get it to turn on after a reboot. Am I correct in assuming the swap partition won't mount like a NTFS, FAT, etc. partition would? There is a post at the top of this page that tells how to use fstab to activate the swap on boot, but I can't seem to find the UUID of my partition since
blkid doesn't output anything. Sorry for hijacking your thread!

EDIT2: I did a little searching and found out that even though the partition is of type Linux Swap, I still have to do
mkswap on the device... in my case mkswap /dev/sda1. After that I was given a UUID.
 
Last edited:
..., how could someone mistakenly erase the wrong partition ?
I done it before, but I don't remember how exactly. When you have multiple USB storage devices that are partitioned differently. Maybe the first partition, /dev/sda1, will not always be swap space? It could be a data partition with important files, then you plug it in by mistake and Asuswrt automatically mounts it as swap space. Oops.
 
Is there a benefit of adding it to fstab compared to the (usually on this forum advised way of adding to the) post-mount script?
Your swap file requires a clean filesystem (ext4, FAT32, etc.) whereas a swap partition does not. And when you use fstab, I think it's supposed to mount and un-mount automatically.
 
I done it before, but I don't remember how exactly. When you have multiple USB storage devices that are partitioned differently. Maybe the first partition, /dev/sda1, will not always be swap space? It could be a data partition with important files, then you plug it in by mistake and Asuswrt automatically mounts it as swap space. Oops.


Thanks for the advice.

I have it in fstab so I guess it auto-mounts as I don't specify any point.

UUID=9b0cf09c-c998-42ce-bbd0-8375ae58g56g none swap sw 0 0
 
Last edited:
... so I guess it auto-mounts ...
Type free to check if Linux is using the swap space. If the Swap numbers are non-zero, that's good.
Code:
# free
             total       used       free     shared    buffers     cached
Mem:        255684     225580      30104          0      51564      57704
-/+ buffers/cache:     116312     139372
Swap:       975516       9384     966132
 
Type free to check if Linux is using the swap space. If the Swap numbers are non-zero, that's good.
Code:
# free
             total       used       free     shared    buffers     cached
Mem:        255684     225580      30104          0      51564      57704
-/+ buffers/cache:     116312     139372
Swap:       975516       9384     966132

I know it mounts as I have checked it with a couple of methods. I have seen it mount mou sda1 once and when I rebooted as sdc1.
So I was wondering if it mounts to a random point or at the first available one for instance if the available is /dev/sdc1
 
use the 'unmount' script instead of services-stop.
I'm using a swap file /tmp/mnt/SNB/myswap.swp
I have this in /jffs/scripts/unmount
Code:
#!/bin/sh

awk '/SwapTotal/ {if($2>0) {system("swapoff /opt/swap")} else print "Swap not mounted"}' /proc/meminfo
Seeing my SNB drive "not cleanly unmounted" tells me that does not work. Not sure if I should replace that
"swapoff /opt/swap" in the awk line with
Code:
swapoff /tmp/mnt/SNB/myswap.swp
or add the above code after the awk line?
 
I'm using a swap file /tmp/mnt/SNB/myswap.swp
I have this in /jffs/scripts/unmount
Code:
#!/bin/sh

awk '/SwapTotal/ {if($2>0) {system("swapoff /opt/swap")} else print "Swap not mounted"}' /proc/meminfo
Seeing my SNB drive "not cleanly unmounted" tells me that does not work. Not sure if I should replace that
"swapoff /opt/swap" in the awk line with
Code:
swapoff /tmp/mnt/SNB/myswap.swp
or add the above code after the awk line?
What I would do is add to unmount script
Code:
logger "End of unmount script"
Then call the script
Code:
/jffs/scripts/unmount
If the logger entry ends up in the system log it is a dirty way of knowing your command in this case the awk line is run without error. You can then add the swapoff command if needed.
 
What I would do is add to unmount script
Code:
logger "End of unmount script"
Then call the script
Code:
/jffs/scripts/unmount
If the logger entry ends up in the system log it is a dirty way of knowing your command in this case the awk line is run without error. You can then add the swapoff command if needed.
Calling /jffs/scripts/unmount from /jffs/scripts/unmount script makes no sense to me!
 
Calling /jffs/scripts/unmount from /jffs/scripts/unmount script makes no sense to me!
Sorry for the confusion I was just saying to call the unmount script at commnad line to see if it runs. Don't add this line to the unmount script.
 
Did you get zram working in asus merlin? I don't find it as a valid opkg pacakge.


Fun stuff...

Enable Zram - keeps things from banging on flash - kernel support is obviously needed... goes to say that one needs some RAM to do this - I also mount /tmp over to tmpfs...

Classic android thing with low-end devices... with a fixed swapfile, you can still get some benefit - tune the limits

(FWIW - my science project aggressively uses zram and ksm, the ARM Cortex MMU is pretty good there)


Code:
# !/bin/bash
# Tuned for quad core, 1 GB RAM models
# put me in /etc/init.d/zram.sh and make me executable
# then run "sudo update-rc.d zram.sh defaults"
modprobe zram
echo 3 >/sys/devices/virtual/block/zram0/max_comp_streams
echo lz4 >/sys/devices/virtual/block/zram0/comp_algorithm
echo 268435456 >/sys/devices/virtual/block/zram0/mem_limit
echo 536870912 >/sys/devices/virtual/block/zram0/disksize
mkswap /dev/zram0
swapon -p 0 /dev/zram0
sysctl vm.swappiness=10

we put tmp into tmpfs... by adding the mount to /etc/fstab

Code:
# move /tmp stuff into RAM - ephemeral stuff - let's not worry about it
# persistent things should stay in /var/tmp
# logs are always in another place - /var/log
tmpfs   /tmp   tmpfs    defaults   0   0

SO we bash the hell out of the MMU and VM, and this is ok...

Extra credit - and this is picking up loose ends...

KSM... adding the following to /etc/rc.local -- this also requires kernel support

Code:
echo 1 > /sys/kernel/mm/ksm/run
echo 1000 > /sys/kernel/mm/ksm/sleep_millisecs

I do a fair amount of other kernel sysctl tuning as well to get good performance out of low-end ARM's...
 

Similar threads

Latest threads

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!

Staff online

Top