傻瓜笔记,包你看懂~
1. 创建一个debian目录
linux #打包目录
DEBIAN #debian目录
control #deb包必须具备的描述性文件
#以下脚本文件不是必备的,如果需要执行某些特殊操作,可以手动维护
preinst #安装前
postinst #安装后
prerm #卸载前
postrm #卸载后
opt #安装目录结构,该目录即为程序最终安装目录/opt/apps/install-dir/
apps
install-dir
#package.sh(可自编写脚本,管理打包流程)
2. control文件内容
#包含软件包描述信息,如包名、版本等。
control #文件名
Package:test-program #软件包名称
OsType:uos #适配系统类型
Version:1.0.0 #软件版本号
Architecture:amd64 #适配架构
Maintainer:<email@example.com> #软件包开发维护有效邮箱地址
Description:description #软件包简短的描述
Depends: dependency1, dependency2 #依赖
3. 安装过程脚本内容
tips:deb在升级安装时,所有的脚本全部执行新包中的,升级变化可以在新脚本中编写
(1) preinst
tips:执行停服务,检查依赖关系,清理旧的遗留数据
(2)postinst
tips:通常是进行一些状态判断以进行对应的操作
#!/bin/sh
#进行程序启动前的配置、赋权限、库拷贝等
chmod -R 777 /opt/apps/install-dir/
echo y | cp /opt/apps/install-dir/lib.so /usr/lib/lib.so
#启动服务、启动进程脚本
systemctl enable test-program.service
/opt/apps/install-dir/programstart.sh start
#以当前登录用户权限启动(安装脚本通常以root权限执行,特殊情况下可能需要降权启动进程)
user_name=$(who | awk '{print $1}' | head -n 1) #获取当前登录用户名(不适用多用户登录情况)
su $user_name -c /opt/apps/install-dir/programstart.sh\ start
#runuser -l $user_name -c '/opt/apps/install-dir/programstart.sh start' #降权执行的另一个命令
(3)prerm
tips:停止相关进程服务,进行防卸载判断
#!/bin/sh
#卸载校验(可以在执行卸载前进行指定操作或者输出指定变量进行变量值判断)
if [ $1 = "remove" ]; then
if [ "$UNINSTALL_VAR" != "allow_uninstall" ]; then
exit 1 #退出卸载脚本
fi
fi
#停相关进程
if [ -f /opt/apps/install-dir/programstart.sh ]; then
/opt/apps/install-dir/programstart.sh stop
fi
(4)postrm
tips:进行卸载后的残留文件清理
#!/bin/sh
#卸载删除残留文件
if [ $1 = "remove" ]; then
if [ -d /opt/apps/test-pro ]; then
rm -rf /opt/apps/test-pro
fi
fi
4. 脚本获取参数时机
tips:在脚本中使用$1获取参数
操作 | 脚本 | 参数 |
安装 | preinst | install |
postinst | configure | |
升级 | prerm | upgrade |
preinst | upgrade | |
postrm | upgrade | |
postinst | configure | |
卸载 | prerm | remove |
postrm | remove | |
卸载出错 | postinst | abort-remove |
5. 打包脚本的编写
#!/bin/sh
#获取架构信息
Arch=$(uname -m)
if echo $Arch | grep -q "x86_64";then
Arch="amd64"
fi
#更新control
cd $(dirname $0) #进入脚本所在的目录,dirname获取该路径的目录部分
cat > $(pwd)/DEBIAN/control << EOF #使用cat命令将输入的内容写入到指定文件中,直到遇到EOF输出结束
Package:test-program
OsType:$(cat /etc/os-release |grep -w NAME |awk -F "=" '{print $2}' |awk -F "\"" '{print $2}')
Version:1.0.0
Architecture:$Arch
Maintainer:testing.net
Description:testing
EOF
#打包
rm -rf $(pwd)/opt/apps/test-program #清理
mkdir -p $(pwd)/opt/apps/test-program #创建安装目录结构
echo y | cp -r $(pwd)/编译生成库路径 $(pwd)/opt/apps/test-program #将程序及相关库等拷贝到该目录下
dpkg -b $(pwd) $(pwd)/test-program.deb #打包
rm -rf $(pwd)/opt/apps/test-program #清理
6. dpkg常用命令
dpkg-deb --help //查看相关命令参数
常用命令如下
dpkg -b ./linux ./test-program.deb #指定DEBIAN的上级目录 指定生成的包名
dpkg -i test-program.deb #安装
dpkg -x test-program.deb ./unpack #解压安装包内容到./unpack下
dpkg -r test-program #卸载 参数control中的包名
dpkg -P test-program #卸载 参数control中的包名
dpkg -l #查看已安装软件
dpkg -l | grep test-program #筛选自己的安装包信息
dpkg -L test-program #查看安装包安装路径
dpkg -I test-program.deb #输出安装包信息及程序描述信息
dpkg -s test-program #输出程序描述信息