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!

elbubi

Regular Contributor
Hi!

So, I created a 256MB swap file on my usb2.0 flash drive (usb1 label) with the following sentences:
Code:
dd if=/dev/zero of=/tmp/mnt/usb1/swapfile.swp bs=1k count=262144
mkswap /tmp/mnt/usb1/swapfile.swp
swapon /tmp/mnt/usb1/swapfile.swp
Then, I added this command to "post-mount" script in order to enable it at every reboot:
Code:
swapon /tmp/mnt/usb1/swapfile.swp

Swap file is active and running fine.

My concern: is it neccesary to disable swap before every reboot via services-stop script?:
Code:
swapoff /tmp/mnt/usb1/swapfile.swp

As always, thanks in advance for your kind help!
 
... is it neccesary to disable swap before every reboot via services-stop script?
I don't think it's necessary. However, for security reasons you could disable it and then fill it with zeros, just before system shutdown. Sometimes the swap space contains fragments of passwords or encryption keys.
 
Thanks both for your replies.

Advice taken john, didn't notice that much more accurate script. Besides, is it necessary to run it or theres no downside on rebooting without swapoff? My flash drive is connected 24/7 and I never take it off.

Regards!
 
Advice taken john, didn't notice that much more accurate script. Besides, is it necessary to run it or theres no downside on rebooting without swapoff? My flash drive is connected 24/7 and I never take it off.
When the router is restarted it will try to unmount the USB drive. It can't do that if there are processes still running that have files open on the USB drive. So it's good practice to cleanly shutdown any user-created processes to avoid the possibility of the router hanging or the USB drive becoming corrupted.
 
When the router is restarted it will try to unmount the USB drive. It can't do that if there are processes still running that have files open on the USB drive. So it's good practice to cleanly shutdown any user-created processes to avoid the possibility of the router hanging or the USB drive becoming corrupted.
Thanks Colin, now I understand it much better.

Regards!
 
After 2 days of working just fine, today I got thousand repeats of this error in log:

Code:
kernel: Write-error on swap-device (8:17:675637960)
kernel: attempt to access beyond end of device

Swap file is large 256MB and "free" reports only 16mb are being used.

Any hints of what may be happening?

Regards!
 
After 2 days of working just fine, today I got thousand repeats of this error in log:

Code:
kernel: Write-error on swap-device (8:17:675637960)
kernel: attempt to access beyond end of device

Swap file is large 256MB and "free" reports only 16mb are being used.

Any hints of what may be happening?

Regards!

Yeah - the logs suggest as much...
 
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...
 
Last edited:
There's one more line in log I forgot to copy (sorry for the mess)
Code:
kernel: Write-error on swap-device (8:17:675637960)
kernel: attempt to access beyond end of device
kernel: sdb1: rw=1, want=675569640, limit=250675136

Regards!
 
If I understand correctly, some proccess (or kernet itselfs) "wants" to access/manage a larger memory space than available in swap file?

My flash drive is formatted in NTFS, just in case its relevant.

Regards!


Ps: Thanks @sfx2000 for your previous answer. Honestly, I didn't understand much.
 
If I understand correctly, some proccess (or kernet itselfs) "wants" to access/manage a larger memory space than available in swap file?
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.
 
I was thinking of doing exactly that.

I'm not 100% sure, but it seems to be Download Master whose triggering that issue.

Tonight when I get home I'll try it and let you know how it goes.

Regards and huge thanks for trying to help me!
 
Not sure, but the fact that it's on a NTFS drive might be causing problems as well... We just had a discussion about that in the dnscrypt thread (https://www.snbforums.com/threads/release-dnscrypt-installer-for-asuswrt.36071/page-31). What you could do is repartition your flash drive and use the space you used for the current swapfile to create an ext2 partition somewhat larger than the 256Mb you need (the filesystem uses some space too) and recreate the swap like you describe above on the newly created ext2 partition. This just might solve your issues.
 
What you could do is repartition your flash drive and use the space you used for the current swapfile to create an ext2 partition somewhat larger than the 256Mb you need (the filesystem uses some space too) and recreate the swap like you describe above on the newly created ext2 partition. This just might solve your issues.
If he's going to repartition the drive then he might as well just use the raw partition as swap. It would be more efficient in terms of I/O as well.
 
If he's going to repartition the drive then he might as well just use the raw partition as swap. It would be more efficient in terms of I/O as well.

Haha, if only I knew what you're talking about. I don't, that's beyond my knowlegde. Just passed by, read his post, and remembered the discussion we had how entware has issues with ntfs (knowing Download Manager is based on Optware), I suggested to use an ext 2 partition. If you care to explain, then I might learn something new.
 
@M@rco When you partition a disk you also set a "partition type" value. This tells the operating system what the partition will contain. Often it's something like NTFS, ext2 or FAT32 in which case the operating system expects it to be formatted in that way. But there are other types, one of the most common (at least in the Unix world) is "swap".

Swap is just a series of disk blocks that is managed directly by the operating system, it doesn't need to be formatted in any way. When you create a swap "file" what you are doing is creating a "fake" swap partition inside a regular file. This makes it much easier to add/delete/resize swap but there is a small performance penalty.
 
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!!!
 
@ColinTaylor thank you for your clear explanation and your willingness to explain. I used to have a dedicated swap partition, but most of the scripts (Skynet, AB, dnscrypt-proxy etcetera) now seem to rely on/look for a swapfile instead, so I repartitioned my flashdrive a few days ago. But if I understand you correctly, I could make swapping a little bit faster if I (re-)created a swap partition (like I did before, following these instructions - second part) and used that partition to create a swapfile on it? Or would I have to use a separate small flash drive for it to benefit? The scripts mentioned before seem to specifically look for 'myswap.swp' (at least, that's what they create).

@elbubi, sorry for sidetracking from your original question. If you mind, just say the word ;)
 

Similar 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!
Top