【Centos】制作一键安装包.bin 文件

  • 1. 安装 Makeself
  • 2. 制作第一个Bin文件
  • 3. 执行 .bin 文件



在某些场景下,我们希望将一系列安装的动作打包在一个

.bin文件里面,在新的环境安装部署时,直接使用 .bin 文件来无脑部署。这时候就可以将安装所需要的步骤和文件打包成一个

.bin 文件,在新服务器上执行

./xxx.bin 来进行无脑安装。

1. 安装 Makeself

我这里使用的是 Centos 7.9 ,使用 yum 安装时发现有问题,这里直接下载源码压缩包来安装。

cd /usr/local/bin
curl -L -o master.zip https://github.com/megastep/makeself/archive/refs/heads/master.zip
unzip master.zip
cd makeself-master
cp makeself.sh /usr/local/bin/makeself
chmod +x makeself
cp makeself-header.sh /usr/local/bin/

如果是 Ubuntu 系统,则下载可以使用:wget https://github.com/megastep/makeself/archive/refs/heads/master.zip 在使用过程中,如果没有执行这个cp makeself-header.sh /usr/local/bin/,则执行 Makeself 有可能报错:Unable to open header file: /usr/local/bin/makeself-header.sh

2. 制作第一个Bin文件

在当前路径下新建一个文件夹,比如 echo_hello 里面存放了需要打包的所有文件

[root@localhost test_build_bin_do]# tree echo_hello
echo_hello
├── echo_hello.sh
└── hello.txt

0 directories, 2 files
[root@localhost test_build_bin_do]#

我这里简化安装包内容,只有一个安装脚本 echo_hell.shhello.txt 文件。

其中文件内容如下:

[root@localhost echo_hello]# cat echo_hello.sh 
#!/bin/bash

author=linmengmeng

mp_model=$1


# Define color codes
yellow='\033[1;33m'
red='\033[1;31m'
green='\033[1;32m'
none='\033[0m'






print_hello_info() {

  msg ok "i am echo_hello.sh content......."

  cat hello.txt


  msg ok "success....."

}

# print a mesage
msg() {
    case $1 in
    warn)
        local color=$yellow
        ;;
    err)
        local color=$red
        ;;
    ok)
        local color=$green
        ;;
    esac

    echo -e "${color}$(date +'%T')${none} ***************** ${2}${none}"
}

check_script_param() {

  # 检查是否传递了参数
  if [ -z "$1" ]; then
    echo "错误: 参数为空,请提供一个参数(1 或 2)。" >&2
  exit 1
  fi
  
  # 校验参数值
  case "$1" in
    1|2)
      echo "参数有效: $1"
      ;;
    *)
      echo "错误: 参数错误,必须为 1 或 2。" >&2
      exit 1
      ;;
  esac
  
}
  

# main
main() {

  check_script_param "$1"  # 传递参数给 check_script_param

  if [[ "$mp_model" == 1 ]];then
    msg warn "-----------------------------------------------"
    msg warn "|                 安装 模式1                   |"
    msg warn "-----------------------------------------------"
  elif [[ "$mp_model" == 2 ]];then
    msg warn "-----------------------------------------------"
    msg warn "|                 安装 模式2                   |"
    msg warn "-----------------------------------------------"
  fi

  print_hello_info

}


# start.
main $@
[root@localhost echo_hello]# cat hello.txt 
Hello World.......
[root@localhost echo_hello]#

执行命令来制作 bin 安装包:makeself echo_hello/ echo_hello.bin "Echo Hello Installation" ./echo_hello.sh

  • echo_hello 为安装文件所在的目录;
  • echo_hello.bin 为将要生成的bin安装包的名称;
  • "Echo Hello Installation" 为安装包的说明;
  • ./echo_hello.sh 指定安装包中安装脚本的入口;
[root@localhost test_build_bin_do]# ll echo_hello
总用量 8
-rwxr--r--. 1 root root 1474 8月  31 16:31 echo_hello.sh
-rw-r--r--. 1 root root   19 8月  31 16:21 hello.txt
[root@localhost test_build_bin_do]# 
[root@localhost test_build_bin_do]# 
[root@localhost test_build_bin_do]# makeself echo_hello/ echo_hello.bin "Echo Hello Installation" ./echo_hello.sh      
Header is 758 lines long

About to compress 8 KB of data...
Adding files to archive named "echo_hello.bin"...
./echo_hello.sh
./hello.txt
CRC: 3901664695
MD5: c42268189c6f278fe6a4db90398a1730

Self-extractable archive "echo_hello.bin" successfully created.
[root@localhost test_build_bin_do]# 
[root@localhost test_build_bin_do]# ll
总用量 28
drwxr-xr-x. 2 root root    44 8月  31 16:45 echo_hello
-rwxr-xr-x. 1 root root 20548 8月  31 16:49 echo_hello.bin
[root@localhost test_build_bin_do]#

可以看到在当前文件夹下生成了名称为 echo_hello.bin 的安装包了。

3. 执行 .bin 文件

[root@localhost test_build_bin_do]# sh echo_hello.bin 1
Verifying archive integrity...  100%   MD5 checksums are OK. All good.
Uncompressing Echo Hello Installation  100%  
参数有效: 1
16:53:59 ***************** -----------------------------------------------
16:53:59 ***************** |                 安装 模式1                   |
16:53:59 ***************** -----------------------------------------------
16:53:59 ***************** i am echo_hello.sh content.......
Hello World.......
16:53:59 ***************** success.....
[root@localhost test_build_bin_do]# 
[root@localhost test_build_bin_do]# 
[root@localhost test_build_bin_do]# sh echo_hello.bin 2
Verifying archive integrity...  100%   MD5 checksums are OK. All good.
Uncompressing Echo Hello Installation  100%  
参数有效: 2
16:54:21 ***************** -----------------------------------------------
16:54:21 ***************** |                 安装 模式2                   |
16:54:21 ***************** -----------------------------------------------
16:54:21 ***************** i am echo_hello.sh content.......
Hello World.......
16:54:21 ***************** success.....
[root@localhost test_build_bin_do]#

OK,完事!

现在你应该会了 1 + 1 = 2 了,那么来计算下

6702044178269172854   * 2369375447796003236

的结果吧。