Quantcast
Channel: xda-developers - Android Software and Hacking General [Developers Only]
Viewing all articles
Browse latest Browse all 3614

[SCRIPT] Enable Ram Swap & Create a Swap File with |=~>SwapItOn

$
0
0
Quote:

Hi, thanks for checking out my thread and your interest in…
Defiant07’s Super Stupendous & Splendid SwapItOn Script.
Now that I have blatantly lied and oversold things, let me explicitly state that I am new to android, in general, and to bash, especially, so I know the code is far from elegant and efficient. This is my first public script so devs don’t laugh too hard and please offer any suggestions. Now on to the good stuff…
Quote:

What will this script do?
 
It will do two things: (1) it will create a swap file in /sdcard that is a size specified by the user; and (2) it will create an init.d script that enables ram swap and sets the swappiness level to a user specified value during each boot process.

Quote:

What are the requirements?
 
  • root access
  • init.d support (if you don’t have it search XDA there are a of couple of apps and numerous scripts that add this feature)
  • a kernel capable of swap (to check if swap is supported in terminal enter
    Code:

    cat /proc/cpuinfo
    and under ‘features’ you should see ‘swp’ or ‘swap’)
  • storage named “/mnt/sdcard” as opposed to /sdcard0 or something else
  • note: one of the benefits of the method deployed by this script is that it does NOT require a special partition

Quote:

Disclaimer & Warning:
 
Although this script works as it should on my device I am not responsible for bootloops, bricks, bad big booty b*tches, or the imminently impending zombie apocalypse. Furthermore, please perform due diligence and do some research about the costs and benefits of ram swap before using this script. In addition, please use good practice and make a nandroid backup before making any changes to your system.

Quote:

Usage:
 
Option 1 - Make a nandroid backup. Download the attached “swapit_[NOT_FLASHABLE]_v2” zip, extract it wherever you want it, and run the script with root/superuser permission in ScriptManager or a similar app, although I have found other apps lacking/deficient. Read the script’s output/follow the script’s instructions, reboot, and you’ll be good to go.

Option 2 - Make a nandroid backup. Download the attached “Defiant07s_SwapItOn_v2” zip and install it via cwm, reboot, and in a terminal emulator input
Code:

su
swapit

or
Code:

su -c swapit
. Read the script’s output/follow the script’s instructions, reboot, and you’ll be good to go.

Uninstall - First in terminal enter
Code:

swapoff /mnt/sdcard/swapfile
. Now you can delete the “swapfile” and “swappinessvalue” files located in /sdcard as well as the “Z99SwapOn” script located in /etc/init.d and the “swapit” script located wherever you put it or in /system/xbin if you flashed it.

Quote:

Possible FAQ:
There are other apps and scripts that perform the same/similar function, why use this script?
 
Unlike apps there is no ram and cpu usage - it applies your settings during the boot process…basically all you need to do set it up once and then you can forget about it. Unlike all the similar scripts I’ve looked at this script takes user input for the swapfile size and swappiness value - the others use predefined sizes and values, which imo is silly since preferences and needs vary from user to user and from device to device. In addition, unlike many similar apps and scripts there is NO need for a special partition.

The init.d script seems to be running on boot being that swap is enabled, but the swappiness value I chose is not sticking?
 
The most likely cause of this is a conflicting init.d script. The solution is to search your other init.d scripts for “swappiness” and edit value accordingly.

Why do you give the init.d script such high priority?
 
I personally am a user of V6 SuperCharger (<<best script ever imo) and I found that when running this script at lower priority two things would happen: (1) minfree and adj values did not stick; and (2) logging was totally jacked up. This may not happen on your device so feel free experiment with renaming the init.d script so as to adjust its priority. Note: should you rename it and then later rerun the main “swapit” script to change your settings, upon completion you should delete the new “Z99SwapOn” script located in /etc/init.d [do not worry your new settings will still be applied by the old script] otherwise you will have duplicate scripts.

Quote:

For those interested in the technical details, here’s the -lt optimal code:
 
Code:

#!/system/bin/sh

# This script was authored by Defiant07 @ XDA.  If you use/redistribute it, even in an altered form, please give proper credit.

clear

echo Hi. Thanks for using my SwapItOn script. Hope you like it!
sleep 1
echo
echo First I need some input from you so as to customize the script...
echo according to your needs and preferences.
sleep 1
echo

echo Please input the desired size in mb as an integer - not a...
echo fraction or decimal - of your swap file and then press 'enter.'
echo
sleep 1
echo I recommend using a value approximately half the size of your...
echo accessible ram. By accessible ram I mean if you navigate to...
echo settings--apps--running you will see XXXmb used and XXXmb free...
echo values - add these two values and divide the sum by two...
echo and this will give you the recommended value.
echo
read desiredsize
echo
sleep 1
echo Please input your desired swappiness value - any integer from...
echo 0 to 100, although 0 would be counterproductive - and then...
echo press 'enter.'
echo
sleep 1
echo Note: The higher the value the higher the probability things...
echo will get transferred to the swap file.
echo
read desiredswappiness
echo
sleep 1

echo Please wait while I create the swap file and boot script...
echo according to your specifications. It could take 1-5 minutes.
echo

if [ -f "/mnt/sdcard/swapfile" ]; then
swapoff /mnt/sdcard/swapfile
sleep 10
rm /mnt/sdcard/swapfile
fi
if [ -f "/data/Ran_SwapOn.log" ]; then
rm /data/Ran_SwapOn.log
fi
echo "$desiredsize" > /data/Ran_SwapOn.log
sleep 10
dd if=/dev/zero of=/mnt/sdcard/swapfile bs=1048576 count=$(cat /data/Ran_SwapOn.log)
sleep 20
mkswap /mnt/sdcard/swapfile
sleep 10
swapon /mnt/sdcard/swapfile

if [ -f "/mnt/sdcard/swappinessvalue.txt" ]; then
rm /mnt/sdcard/swappinessvalue.txt
fi
echo "$desiredswappiness" > /mnt/sdcard/swappinessvalue.txt
mount -o rw,remount /system
if [ -f "/system/etc/init.d/Z99SwapOn" ]; then
rm /system/etc/init.d/Z99SwapOn
fi
if [ -d "/sqlite_stmt_journals" ]; then
rmdir /sqlite_stmt_journals
fi
sleep 10
mkdir /sqlite_stmt_journals

cat > /system/etc/init.d/Z99SwapOn << EOF
#!/system/bin/sh

# This script was authored by Defiant07 @ XDA.  If you use/redistribute it, even in an altered form, please give proper credit.

if [ -e /data/Ran_SwapOn.log ]; then
rm /data/Ran_SwapOn.log
fi

while [ 1 ]
do
if [ -f "/mnt/sdcard/swapfile" ]; then
echo " Mounted...Time To Get Our SwapOn!!!" > /data/Ran_SwapOn.log
echo "" >> /data/Ran_SwapOn.log
swapon /mnt/sdcard/swapfile
echo "\$(cat /sdcard/swappinessvalue.txt)" > /proc/sys/vm/swappiness
echo " ##########################################################################" >> /data/Ran_SwapOn.log
echo " #>>>>>>>°•°•°•[{[{Defiant07's SwapOn Verification!?!?!?!}]}]•°•°•°<<<<<<<#" >> /data/Ran_SwapOn.log
echo " ##########################################################################" >> /data/Ran_SwapOn.log
echo "" >> /data/Ran_SwapOn.log
echo " Swappy Swap Swappin' Swappiness: \$(cat /proc/sys/vm/swappiness)" >> /data/Ran_SwapOn.log
echo " SwapOn@: \$(date +"%r %Y.%m.%d")" >> /data/Ran_SwapOn.log
echo "" >> /data/Ran_SwapOn.log
free >> /data/Ran_SwapOn.log
echo "" >> /data/Ran_SwapOn.log
echo " ##########################################################################" >> /data/Ran_SwapOn.log
exit 0; else
sleep 1
fi
done
EOF

sleep 10
chmod 755 /system/etc/init.d/Z99SwapOn
mount -o ro,remount /system

echo
echo Okay all done...that was easy...
echo
sleep 1
echo A few closing notes:
echo
sleep 1
echo 1. do not delete or move the swapfile and swappinessvalue...
echo files that are located in /sdcard.
echo
sleep 1
echo 2. to verify that all is working reboot and then check /data...
echo for Ran_SwapOn.log or in terminal use -free- and...
echo -cat /proc/sys/vm/swappiness- commands.
echo
sleep 1
echo 3. if you ever want to change the swapfile size or swappiness...
echo value just rerun this script.
echo
sleep 1
echo 4. it is probably not a bad idea to wipe cache and dalvik...
echo before rebooting.
echo
sleep 1
echo Thanks for using my script. Peace and Respect.
echo You can close your app now.

exit


Quote:

Change Log:
 
v2 - forgot a few "\", which caused minor bugs in the Ran_SwapOn.log file created by the init.d script and would cause the user's new swappiness level setting to not be applied if they renamed the old init.d script and deleted the newly created "Z99SwapOn" script following rerunning the "swapit" script

Please hit THANKS and give a 5 STAR rating if you found this product of my labour useful…don’t be a total free rider. Also please post any comments and suggestions…if there is sufficient interest from those not comfortable with making the edits themselves I could make versions of the script that make the swap file in different locations (e.g. /extsd) and that are compatible with different storage names (e.g. /sdcard0). Should you want to use or redistribute this script, even in an altered form, please feel free to do so; all I ask for in return is credit...gimme my props in the form of a shout out and/or a link to this thread.


Viewing all articles
Browse latest Browse all 3614

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>