What's new
  • 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!

Enabling swap at startup

dciarnie

Occasional Visitor
Hi,

I have set up a swap partition on a drive attached to the USB3 port of my AC68U which I would like to be enabled when the router is booted. I have added the following lines to the end of the services-start script:

swapon UUID="<swap partition UUID>"
echo 20 > /proc/sys/vm/swappiness

but neither of the above commands seem to be executed as swapping is not enabled and swappiness is not updated. Executing the commands manually works.

Any ideas as to what the problem is?

Thanks!
 
For one thing, services-start is too early (your drive may not be mounted yet). Use a post-mount script. The name of the mnt point is passed as a parameter to the script and the script is called for each drive mounted. Check in your script for the mnt point that you want to use for your swap file then run the commands.

Here's an example from my post-mount script that uses a swapfile ($scr_name is defined earlier, it's not predefined)....
Code:
if [ $1 == "/tmp/mnt/extsys" ] && [ -f "/tmp/mnt/extsys/.swap" ]; then
        logger -t $scr_name "starting swapfile on $1"
        swapon /mnt/extsys/.swap
fi
 
It's not immediately clear from the wiki page what order events happen unless it's implied by the order in which they're listed. You may be right that services-start is too early but it doesn't really make sense to me that that should be the case. I would think that there are going to be services, such as minidlna, that require disks to be available by the time they start and it looks to me that services-start should be called after that. Also, I would think that the /proc file system would be available by the time services-start is called and yet /proc/sys/vm/swappiness does not reflect new value.

Also, I'm not using a swap file but a swap partition so post-mount isn't really applicable. What does need to be available is an active USB connection to the drive but again, I would think that would be available by the time services-start is called.

I could use post-mount as a proxy to check which drive is being mounted and make an assumption that the swap partition can then be activated but that's rather kludgy, in my opinion.
 
AFAIK, there is no guaranteed order in which the scripts will execute. Starting of things that require the USB disks, like minidlna, are actually triggered by hotplug2 when a disk is mounted, which is the same thing that triggers the post-mount script.
 
And I can tell you from experience the order depends on which firmware - I have recently seen latest John's fork mounts usb drives much earlier than Merlin 380.57 on an N66 for example.
 
Well, I now know what the problem is and it seems a pretty serious one to me.

I have Entware installed on my system. Entware is installed to a USB drive and it obviously assumes that the drive will be available when services-start is called:

Code:
#!/bin/sh

RC='/opt/etc/init.d/rc.unslung'

i=30
until [ -x "$RC" ] ; do
  i=$(($i-1))
  if [ "$i" -lt 1 ] ; then
    logger "Could not start Entware"
    exit
  fi
  sleep 1
done
$RC start

(My changes are at the end of this script.)

The disk is obviously not mounted when services-start is called because this script exits (the error message is in syslog). This is obviously problematic because it means that Entware and any services that it manages are not started at system start.
 
That Entware script waits at least 30 seconds for the usb opt folder contents to exist. The post-mount makes the dynamic link for /opt.
 
Last edited:
It's actually waiting 30 sec for the drive to come ready. It's possible there is something unique to your configuration that is delaying or causing restarts on some services. Would probably be worthwhile to look again at the syslog to see if there is anything there (if there is a disk error detected and a disk check is being started for example).
 
I missed the 30s loop where the script is repeatedly checking to see if entware is available.

I suspect that a disk check is indeed what caused the problem. My router became unstable and the only way that I could get it to recover was to toggle the power. That would mean that the drive was not properly unmounted so when the router came back up an fsck was necessary. On a 3TB disk, that's going to take a bit of time...

Thanks for your input, guys. I'll try to keep these things in mind the next time I have to restart the router.
 
Feel free to increase the number of retries if for some reason your disk is taking too long to mount, or the time the script waits with each iteration (sleep 1 = 1 second).
 

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