Swap File Plugin


Recommended Posts

Here is a quick and dirty plugin that I wrap to enable/disable the swap file I have on my cache drive.  Perhaps some of you guys will find it usually, but most importantly, perhaps we can improve upon it.

 

It's pretty static right now... It creates (if the file doesn't already exists) a 16GB swap file on /mnt/cache, sets permissions, then mounts it.  On shutdown, it automatically disables it.

 

Comments and criticisms certainly appreciated.

swapfile.plg.zip

Link to comment

 

If you know what device his the swap partition, you would just swap the commands out to point to the correct device.  If you'd like to make it dynamic, you would just grep your device out of the 'cat /proc/partitions' command you listed. 

 

You could use something like this in the swap_start piece (note that this hasn't been tested)

 

if [ -d /dev/sdg ]; then

  # USB Swap drive is ready

  mkswap -f /dev/sdg

        swapon /dev/sdg

   

fi

 

in swap_stop, try something like this

 

if [ -d /dev/sdg ]; then

  # USB drive exists

  swapoff /dev/sdg

   

fi

 

This assumes that your sdg drive is always that USB stick.  It doesn't check to make sure that it is, so it could be dangerous.

 

I am about to go on the road for a few hours, but when i get some back, I'll take a few minutes to see if we can make the script a bit more dynamic.

 

 

Link to comment

I made the changes, but without the code: 

 

mkswap -f /dev/sdg

 

Note: My USB drive has changed since my last post to /dev/sde.

 

You are right about this command being a dangerous/unforgiving command if you are not correct about the drive, you will have destroyed the drive on witch it is assigned. 

 

The swapon and swapoff will just error out if the drive is not swap drive.

 

now the dynamic part:  if the script could check for where a swap drive/partition or file is and then assign the swapon/swapoff, that would be great.

Link to comment
  • 2 weeks later...

Here is a quick and dirty plugin that I wrap to enable/disable the swap file I have on my cache drive.  Perhaps some of you guys will find it usually, but most importantly, perhaps we can improve upon it.

 

It's pretty static right now... It creates (if the file doesn't already exists) a 16GB swap file on /mnt/cache, sets permissions, then mounts it.  On shutdown, it automatically disables it.

 

Comments and criticisms certainly appreciated.

Pardon my somewhat linux noobness. If I understand the code correctly swap_stop() removes the swap file /mnt/cache/.swap

 

Since swap_start() creates the swap file only if it doesn't already exist, would it be OK to not remove it and so save the time it takes to create it during boot?

 

Link to comment

 

Pardon my somewhat linux noobness. If I understand the code correctly swap_stop() removes the swap file /mnt/cache/.swap

 

Since swap_start() creates the swap file only if it doesn't already exist, would it be OK to not remove it and so save the time it takes to create it during boot?

Following up my own question with more questions:

 

Does the swap file need to be zero-filled just prior to swapon and so it does need to be recreated during boot?

 

If using a swap partition instead of a file does the OS zero the partition on boot? Am I needlessly trying to save a few seconds here? I seem to remember the swap I used under 4.7 from unMenu as takiing a lot of time to create during boot but maybe not.

Link to comment

I did some modifications to the original plugin by "cofin" because it didn't work "out of the box".

Used Joe L's unmenu swafile plugin as reference - Thank you Joe.

 

Main changes - added:

  • mkswap command
  • chmod command
  • logger commands to log in syslog

 

Note that I changed the size to 2G instead of 16G - that is what I need and don't want to waste time each reboot to create the larger file.

 

I would also like to echo trurl's question: Does the swap file need to be recreated (zeros) each time it is turned on "swapon"?

swapfile.zip

Link to comment
  • 2 weeks later...

according to the how-to i just found, they created the file, assign ownership/permissions and then added a line to the fstab file for automount the swap file upon bootup - and this was not creating the file at every bootup

 

I edited the rc.swap to not delete the swap file

 

as is it written, if the swap file exists it will use it, otherwise it will created it and then use it

 

Myk

Link to comment

according to the how-to i just found, they created the file, assign ownership/permissions and then added a line to the fstab file for automount the swap file upon bootup - and this was not creating the file at every bootup

 

I edited the rc.swap to not delete the swap file

 

as is it written, if the swap file exists it will use it, otherwise it will created it and then use it

 

Myk

 

Can you post this link to the how-to?

 

That is one of the question posted above by trurl and myself... Do we need to recreate the swap file each boot?

Link to comment
  • 1 month later...
  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.