将路由器作下载器有时会遇到内存居高不下,需要添加虚拟内存做后援。一般虚拟内存设为32M 或64M即可,再大没有意义,拖慢速度。如果实在不明白此脚本的功能,何须介意,不必强开。
按照下面的教程,先开启路由器的SSH和JFFS功能。
一、软配置:
1)登录路由器,进入【系统管理】设置页面
2)进入【系统设置】选项,打开JFFS2和SSH功能
4)下载WinSCP (点我下载 密码:6q52)
5)输入路由器IP,SCP协议,用户名、密码登录
6)进入路由器主目录/
7)进入 /jffs/scripts/目录
8)新建文件
9)文件名sdaswap,注意更改编码格式为UTF-8,否则后面会提示无此文件。
10)选择UTF-8编码,然后将下面脚本代码复制到编辑器,并保存
#!/bin/sh
R_BOLD="\033[31m\033[1m"
G_BOLD="\033[32m\033[1m"
BOLD="\033[33m\033[1m"
NORM="\033[0m"
INFO="$BOLD Info: $NORM"
ERROR="$BOLD *** Error: $NORM"
INPUT="$BOLD => $NORM"
SWAP_FILE=`nvram get apps_swap_file`
SWAP_SIZE=`nvram get apps_swap_size`
i=1
cd /tmp
echo -e "$INFO This script will guide you through the swap installation."
echo -e "$INFO Script modifies \"swap\" folder only on the chosen drive,"
echo -e "$INFO Looking for available partitions..."
for mounted in `/bin/mount |awk '{if($0 ~/mnt/){ print $3}}'` ; do
echo -e "$G_BOLD [$i] --> $mounted $NORM"
eval mounts$i=$mounted
i=`expr $i + 1`
done
if [ $i == "1" ] ; then
echo -e "$ERROR $R_BOLD No partitions available. Exiting...$NORM"
exit 1
fi
echo -en "$INPUT $BOLD Please enter partition number or 0 to exit $NORM\n$BOLD[0-`expr $i - 1`]$NORM: "
read partitionNumber
if [ "$partitionNumber" == "0" ] ; then
echo -e $INFO Exiting...
exit 0
fi
if [ "$partitionNumber" = "" ] || [ "`echo $partitionNumber|sed 's/[0-9]//g'`" != "" ] ; then
echo -e "$ERROR $R_BOLD Invalid arguments! Exiting...$NORM"
exit 1
fi
if [ "$partitionNumber" -gt `expr $i - 1` ] ; then
echo -e "$ERROR $R_BOLD Invalid partition number! Exiting...$NORM"
exit 1
fi
eval entPartition=\$mounts$partitionNumber
echo -e "$INFO $G_BOLD $entPartition $NORM selected."
APPS_INSTALL_PATH=$entPartition/swap
case "$1" in
start)
mem_size=`free |awk '$0 ~/Swap/{print $4}'`
pool_size=`df |awk '{if($0 ~"'$entPartition'") {print $4}}'`
if [ $pool_size -gt $SWAP_SIZE ]; then
[ -e "$APPS_INSTALL_PATH/$SWAP_FILE" ] && swapoff $APPS_INSTALL_PATH/$SWAP_FILE
[ -d "$APPS_INSTALL_PATH" ] && rm -rf $APPS_INSTALL_PATH
echo -e "$INFO Creating $APPS_INSTALL_PATH folder..."
mkdir -p $APPS_INSTALL_PATH
echo -en "$INFO Swap size is [$BOLD$SWAP_SIZE$NORM],changed:\c $BOLD"
read answer
if [ "$answer" = "" ]
then
{
echo -e "$INFO Swap size was not changed"
}
else
{
if [ "$answer" != "" ] && [ "`echo $answer|sed 's/[0-9]//g'`" = "" ] && [ $answer -lt $pool_size ]
then
{
SWAP_SIZE=$answer
echo -en "$INFO Swap size was changed $BOLD[$SWAP_SIZE]$NORM \n"
}
else
{
echo -e "$ERROR $R_BOLD Invalid arguments $NORM"
exit 1
}
fi
}
fi
swap_count=`expr $SWAP_SIZE / 1000 - 1`
echo -e "$INFO dd if=/dev/zero of=$APPS_INSTALL_PATH/$SWAP_FILE bs=1M count=$swap_count"
dd if=/dev/zero of=$APPS_INSTALL_PATH/$SWAP_FILE bs=1M count=$swap_count
echo -e "$INFO mkswap $APPS_INSTALL_PATH/$SWAP_FILE"
mkswap $APPS_INSTALL_PATH/$SWAP_FILE
echo -e "$INFO $G_BOLD swapon $APPS_INSTALL_PATH/$SWAP_FILE $NORM"
swapon $APPS_INSTALL_PATH/$SWAP_FILE
echo -e "**********************************************************"
echo -e " ${G_BOLD}Swap:$NORM Total($G_BOLD$(free |grep -A1 "Swap" |awk '{print $2}')$NORM) Used($G_BOLD$(free |grep -A1 "Swap" |awk '{print $3}')$NORM) Free($G_BOLD$(free |grep -A1 "Swap" |awk '{print $4}')$NORM)"
echo -e "**********************************************************"
fi
echo -e "$INFO Create boot entry, y? :\c "
read yor
if [ "$yor" = "y" ]
then
{
[ -e "/jffs/scripts/services-start" ] && [ `cat /jffs/scripts/services-start |awk '{if($0 ~/swapon/) {print $0}}'|wc -l` -ge 1 ] &&\
sed -i 'N;/\n.*swapon/!P;D' /jffs/scripts/services-start && sed -i '/swapon/d' /jffs/scripts/services-start
[ ! -e "/jffs/scripts/services-start" ] && echo "#!/bin/sh" > /jffs/scripts/services-start
[ `grep "#!/bin/sh" /jffs/scripts/services-start |wc -l` -lt 1 ] && sed -i '1i#!\/bin\/sh' /jffs/scripts/services-start
sed -i '1asleep 30' /jffs/scripts/services-start
sed -i '2aswapon '$APPS_INSTALL_PATH'/'$SWAP_FILE'' /jffs/scripts/services-start
chmod 755 /jffs/scripts/services-start
echo -e "$INFO $G_BOLD Boot entry is created $NORM"
}
else
{
echo -e "$INFO $G_BOLD Boot entry was not created,Exiting $NORM"
exit 1
}
fi
;;
stop)
[ -e "/jffs/scripts/services-start" ] && [ `cat /jffs/scripts/services-start |awk '{if($0 ~/swapon/) {print $0}}'|wc -l` -ge 1 ] &&\
sed -i 'N;/\n.*swapon/!P;D' /jffs/scripts/services-start && sed -i '/swapon/d' /jffs/scripts/services-start
[ -e "$APPS_INSTALL_PATH/$SWAP_FILE" ] && swapoff $APPS_INSTALL_PATH/$SWAP_FILE
[ -d "$APPS_INSTALL_PATH" ] && rm -rf $APPS_INSTALL_PATH
echo -e "**********************************************************"
echo -e " ${G_BOLD}Swap:$NORM Total($G_BOLD$(free |grep -A1 "Swap" |awk '{print $2}')$NORM) Used($G_BOLD$(free |grep -A1 "Swap" |awk '{print $3}')$NORM) Free($G_BOLD$(free |grep -A1 "Swap" |awk '{print $4}')$NORM)"
echo -e "**********************************************************"
;;
*)
exit 1
;;
esac
11)在sdaswap文件右击鼠标,属性。设置为0755权限 (该权限是可读写、执行)
到此可以关闭WinSCP软件。
二、执行命令
1)进入路由器的软件中心 Software Center 安装shellinabox命令工具
输入用户名和密码登录
2)运行命令行:/jffs/scripts/sdaswap start
注意“start”的参数不可少.
3)脚本自动扫描路由器外接的USB设备,列出所有存储类设备供选择使用哪个USB硬盘作为虚拟内存的载体。
支持NTFS\EXT2,3,4
注意:建议使用高速U盘或者SSD硬盘,虚拟内存类似于普通内存,需要多次高速读写,整体速度取决于USB设备的速度。这里可以选择虚拟内存的大小,默认为33000=32M,可以设置为64M=>65500。如果没有特殊需求,建议使用默认大小。并不是越大越好,更大的内存容量 可能会拖慢系统速度。
4)加载成功后,自动加载Swap的信息。Total是总缓存,Used 已使用,Free未使用
如果Total显示为0证明没有加载成功或者已关闭虚拟内存功能。
最后一行是提示是否加载到开机启动项。y回车是添加到启动项。
5)停用虚拟内存功能命令:/jffs/scripts/sdaswap stop
扫描USB设备,选择要停用哪个设备的虚拟内存。Swap: Total显示0,证明已经停用虚拟内存
下面是增加虚拟内存前后的结果图
-The End-