wifidog自动升级固件
一些组织机构运行wifidog时,会在不同的位置有许多接入点。当需要升级固件时,最痛苦的就是在所有的热点逐个升级。最好是热点能够在获取新版本固件时自动升级。
以下内容描述了如何配置固件才能让它自动升级。
前提条件
- 以下内容假定了如何编辑固件的自定义镜像
- 接入一个网站当做是自定义存储库。这个库将包含自定义固件图像和自定义包版本的目录。
编辑
假定BUILD_ROOT是openwrt镜像生成器的根目录
将自定义文件添加到镜像,在BUILE_ROOT创建一个新的名为files的目录。
创建一个包含镜像编码的文件
cd files
mkdir etc
cd etc
touch custom_compil
现在编辑custom_compil文件包含自定义镜像版本
1
添加配置文件来保存自定义升级脚本的配置选项。从BUILD_ROOT/files目录
mkdir etc/config
cd etc/config
touch customupgrade.conf
用以下信息编辑customupgrade.conf
# Config file for custom configuration options for the auto-upgrade script
# All options are defined with default values in the script
# custom_file : default /etc/custom_compil
# Specifies the file on the router that contains the number of the custom compilation of this image
#
custom_file /etc/custom_compil
# temp_file : default /tmp/latest
# The temporary file to which to download the 'latest' file from the server
# Default value should do usually, unless the tmp directory is somewhere else
# temp_file /tmp/latest
# server
# MANDATORY
# The web server root where the 'latest' file, and 'package-list' files are kept
server http://wifidog.testserver/files/
# filename : default latest
# The name of the file on the server containing the information on the latest image
#
filename latest
# packagelist : default packages-list
# The name of the file on the server containing the information on the package
# versions that should be installed on the server
packagelist packages-list
# temp_package: default /tmp/packages-list
# The temporary file to which will be downloaded the packages list file from the server
# Default value should do usually
# temp_package /tmp/packages-list
添加脚本来检测镜像和程序包的更新。从BUILD_ROOT/files目录
mkdir usr
mkdir usr/bin
cd usr/bin
touch customupgrade
a+x customupgrade
用以下脚本编辑customupgrade
#!/bin/sh
#
# This script verifies if a new firmware is available on the server
# It supposes that a file on the machine exists in /etc/custom_compil.txt containing
# the custom firmware's version. On the server, a file named "latest" contains the compil
# number of the latest firmware
CUSTOM_FILE=/etc/custom_compil.txt
TEMP_FILE=/tmp/latest
SERVER=http://wifidog.testserver/files/
FILENAME=latest
PACKAGELIST=packages-list
TEMP_PACKAGE=/tmp/packages-list
CONF_FILE=/etc/config/customupgrade.conf
# Overwrite the default values with the config if necessary
if egrep -v '^#.*' "$CONF_FILE" | grep 'custom_file'; then
CUSTOM_FILE=$(egrep -v '^#.*' "$CONF_FILE" | grep 'custom_file' | awk '{print $2}')
fi
if egrep -v '^#.*' "$CONF_FILE" | grep 'temp_file'; then
TEMP_FILE=$(egrep -v '^#.*' "$CONF_FILE" | grep 'temp_file' | awk '{print $2}')
fi
if egrep -v '^#.*' "$CONF_FILE" | grep 'server'; then
SERVER=$(egrep -v '^#.*' "$CONF_FILE" | grep 'server' | awk '{print $2}')
fi
if egrep -v '^#.*' "$CONF_FILE" | grep 'filename'; then
FILENAME=$(egrep -v '^#.*' "$CONF_FILE" | grep 'filename' | awk '{print $2}')
fi
if egrep -v '^#.*' "$CONF_FILE" | grep 'packagelist'; then
PACKAGELIST=$(egrep -v '^#.*' "$CONF_FILE" | grep 'packagelist' | awk '{print $2}')
fi
if egrep -v '^#.*' "$CONF_FILE" | grep 'temp_package'; then
TEMP_PACKAGE=$(egrep -v '^#.*' "$CONF_FILE" | grep 'temp_package' | awk '{print $2}')
fi
# Verify is a new firmware upgrade is available on the server
if [ -f $CUSTOM_FILE ]; then
ACTUAL=$(cat $CUSTOM_FILE)
if [ -f $TEMP_FILE ]; then
rm $TEMP_FILE
fi
wget -O $TEMP_FILE $SERVER$FILENAME
if [ -f $TEMP_FILE ]; then
cat $TEMP_FILE | grep 'version' | cut -d: -f2 | awk '{print $1}'
LATEST=$(cat $TEMP_FILE | grep 'version' | cut -d: -f2 | awk '{print $1}')
LATESTIMG=$(cat $TEMP_FILE | grep 'file' | cut -d: -f2 | awk '{print $1}')
MD5SUM=$(cat $TEMP_FILE | grep 'md5sum' | cut -d: -f2 | awk '{print $1}')
fi
if [ $LATEST -gt $ACTUAL ]; then
wget -O "/tmp/"$LATESTIMG $SERVER$LATESTIMG
ACTUALMD5=$(md5sum "/tmp/"$LATESTIMG | awk '{print $1}')
if [ $MD5SUM = $ACTUALMD5 ]; then
sysupgrade "/tmp/"$LATESTIMG
fi
fi
fi
# If we get here in the script, then no new firmware was installed, we check for new packages
# First, verify if the custom package repository is in the opkg.conf file
#
# If you do not plan to have one such directory on your server, you may comment out
# the following lines
#
REPO=$(grep "$SERVER" /etc/opkg.conf)
if [ -z "$REPO" ]; then
echo "src custom_pack "$SERVER"packages" >> /etc/opkg.conf
fi
# update the package repository to get all the latest versions
opkg update
# Download the packages-list file from the custom server.
# This file is such that for each line contains the package name and version to be installed
# example:
#
# wifidog 1.1.5-1
# ntp 2.1-1
#
# Only those packages will be installed if the version is different from the one currently installed
if [ -f $TEMP_PACKAGE ]; then
echo "removing file "$TEMP_PACKAGE
rm $TEMP_PACKAGE
fi
wget -O $TEMP_PACKAGE $SERVER$PACKAGELIST
if [ -f $TEMP_PACKAGE ]; then
cat $TEMP_PACKAGE | while read line; do
PACKAGE=$(echo $line | cut -f1,2 | awk '{print $1}')
VERSION=$(echo $line | cut -f1,2 | awk '{print $2}')
if opkg status $PACKAGE | grep 'Version' | grep $VERSION > /dev/null; then
echo "package "$PACKAGES" "$VERSION" is already installed"
else
echo "installing package "$PACKAGE
opkg install $PACKAGE
fi
done
fi
现在我们需要添加一个定时任务来运行这个脚本。每天或根据所需的作何时候运行。从BUILD_ROOT/files目录
cd etc
mkdir crontabs
cd crontabs
touch root
编辑root文件包含所需的定时任务
# /etc/crontab/root: Cron job to be run on custom openwrt firmware
# m h dom mon dow command
* 2 * * * /usr/bin/customupgrade > /tmp/custom.log
本文章由 http://www.wifidog.pro/2015/03/13/wifidog%E8%87%AA%E5%8A%A8%E5%8D%87%E7%BA%A7%E5%9B%BA%E4%BB%B6.html 整理编辑,转载请注明出处