Linux系统下安装包制作
转载自:http://blog.sina.com.cn/s/blog_6f83fdb40101ic17.html
本文以OpenDDS开源代码为例,编写Linux的bin安装包制作流程及详细代码,将已经搭建好的DDS环境打包制作成bin安装包,在目标Linux系统上,直接安装(免予多次编译),即可使用。
前提是有一个已经搭建好的OpenDDS环境,关于Linux系统下DDS编译方法请参考前一博文:Linux下OpenDDS环境搭建
bin安装包的制作思想:
将安装脚本install.sh和源码包打成一个bin文件,当在命令行运行bin安装文件时,其实就是运行bin里面的install.sh来解压bin中的tar.gz的过程。
安装脚本install.sh和打包脚本auto_package.sh的具体代码如下:
1、install.sh,内容如下:
#!/bin/bash
PATH=/usr/bin:/bin
ProductName="OpenDDS"
ProductVer="3.5"
AppFolder="/usr"
InstallPath=
TAR_NAME="$ProductName$ProductVer.tar.gz"
#打印许可
function printLicense() {
more <<"EOF"
License Agreement
.............
EOF
read -p "Do you agree to the above license terms? [y/n]:" reply leftpver
case $reply in
y* | Y*)
echo "You have agreed to the license, install start...";;
n* | N*)
echo "You don't agreed to the license, you can't install $ProductName$ProductVer."
exit 1;;
*)
echo "Invalid select, exit install ."
exit 1;;
esac
}
#版本号比较
function VersionComp() {
if [ "$1" = "$2" ]; then
echo "eq"
else
lrel=`printf "%s\n%s\n" $1 $2 | \
sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n -k 5,5n | \
head -1`
if [ "$1" = "${lrel}" ]; then
echo "lt"
else
echo "gt"
fi
fi
}
#软件版本号检测
function VersionCheck() {
DDS_ROOT=${DDS_ROOT}
VersionFile=$DDS_ROOT/VERSION
if [ -e $VersionFile ]; then
Version=`head -1 $VersionFile | awk '{print $5}' | cut -d"," -f1`
if [ "gt" = $(VersionComp "$Version" "$ProductVer") ]; then
echo "Newer version ($Version) is already installed, please uninstall first!"
exit 1
else
read -p "Older version ($Version) is already installed, upgrade to $ProductVer? [y/n]:" reply leftover
case $reply in
y* | Y*)
;;
n* | N*)
echo "You don't agreed to the license, you can't install $ProductName$ProductVer."
exit 1;;
*)
echo "Invalid select, exit install ."
exit 1;;
esac
fi
fi
}
#设置安装路径
function SetInstallPath() {
read -p "Install $ProductName$ProductVer to $AppFolder, sure? [y/n]:" reply leftover
case $reply in
y* | Y*)
;;
n* | N*)
read -p "Please input install path: " AppFolder leftover;;
*)
echo "Invalid select, exit install ."
exit 1;;
esac
AppFolder=`readlink -f $AppFolder`
InstallPath=$AppFolder/
mkdir -p $InstallPath
}
#文件校验
function CheckSum() {
if [ ! -x /usr/bin/sum ]; then
echo "Can't find /usr/bin/sum to do checksum, continuing anyway."
return 0
fi
echo "Checksumming..."
sumret=(`/usr/bin/sum $TAR_NAME`)
if [ ${sumret[0]} != sum_value -o ${sumret[1]} != sum_size ]; then
echo "The install file appears to be corrupted."
echo "Please do not attempt to install this file."
exit 1;
fi
}
#解包
function Unpack() {
echo "Unpacking..."
lines=`awk '/^exit 0/print NR+1;exit 1;}' "$0"`
tail -n +$lines "$0" > $TAR_NAME 2>/dev/null
CheckSum
tar -zvxf $TAR_NAME -C $InstallPath/ 2>/dev/null
rm $TAR_NAME
}
#写环境变量
function WriteEnv() {
echo "
export DDS_ROOT=$InstallPath/$ProductName$ProductVer/DDS
export PATH=\$DDS_ROOT/bin:\$PATH
export LD_LIBRARY_PATH=\$DDS_ROOT/lib:\$LD_LIBRARY_PATH
" >> ~/.bashrc
echo "Please restart, to make environment variable effect."
}
VersionCheck
printLicense
SetInstallPath
Unpack
WriteEnv
echo "Install complete!"
exit 0
2、auto_package.sh,内容如下:
#!/bin/bash
PATH=/usr/bin:/bin
ProductName="OpenDDS"
ProductVer="3.5"
DDS_ROOT=${DDS_ROOT}
DDS_SRC="$ProductName$ProductVer/"
#拷贝源码目录构建安装目录
rm -rf $DDS_SRC
mkdir -p $DDS_SRC
cp -r $DDS_ROOT $DDS_SRC
#删除多余的文件和目录
find ./ -name *.sln -exec rm -r {} \;
find ./ -name *vcproj* -exec rm -r {} \;
find ./ -name *vcxproj* -exec rm -r {} \;
find ./ -name .obj -type d -exec rm -r {} \;
find ./ -name .shobj -type d -exec rm -r {} \;
TAR_NAME="$ProductName$ProductVer.tar.gz"
Release="$ProductName$ProductVer.bin"
#将安装文件打成tar包
rm $TAR_NAME $Release
tar -zcvf $TAR_NAME $DDS_SRC
#计算文件校验值并写到安装脚本中
dds_sum=(`/usr/bin/sum $TAR_NAME`)
sum_value=${dds_sum[0]}
sum_size=${dds_sum[1]}
cat install.sh | sed -e s/sum_value/$sum_value/ -e s/sum_size/$sum_size/ > temp.bin
#将安装脚本和目标文件一起打成bin文件
cat temp.bin $TAR_NAME > $Release
chmod a+x $Release
rm -rf $DDS_SRC $TAR_NAME temp.bin
3、执行打包脚本:
将第一步和第二步创建的两个脚本文件放在同一目录下,执行auto_packet.sh脚本,就可以自动创建一个OpenDDS的bin安装包了,将打好的安装包OpenDDS3.5.bin拷贝到目标机上,使用./OpenDDS3.5.bin命令即可启动安装。
关于Linux系统下bin安装包制作的原理性解释请参考:http://www.cnblogs.com/itech/archive/2010/02/01/1661248.html