Create self-extractable archive installer on Linux/Unix








The solution is simple to explain: The self-extractable file are joint from an extractor shell script and an tar archive, which contains the actual contents to be extracted and installed into the system. The extractor script is a text file while the archive file is binary. They are joint together using cat to make the self-extractable archive. Thus, the self-extractable archive file should have the following parts:
  • Script part: The extractor shell script, extract.sh:
    #!/bin/bash
    
    #
    # the extractor script
    # 
    
    # create a temporary directory
    TMP_DIR=$(mktemp -d InstXXXXXX)
    
    
    # extract the archive
    ARCHIVE=`awk '/^__ARCHIVE_BEGIN__/ {print NR + 1; exit 0; }' $0`
    tail -n+$ARCHIVE $0 | tar xzvf -C $TMP_DIR
    
    ## Alternatively you can use sed
    #sed -e '1,/^__ARCHIVE_BEGIN__$/d' "$0" | tar xzvf -C $TMP_DIR
    
    CUR_DIR=$(pwd)
    
    # execute install.sh
    cd $TMP_DIR; $TMP_DIR/install.sh
    
    cd $CUR_DIR
    
    rm -rf $TMP_DIR
    
    exit 0
    
    __ARCHIVE_BEGIN__
    
  • Archive Part: The tar.gz archive contains the contents to install. Inside the archive, there should be an installer script, which is executed when extractor script has extracted the archive into a temporary directory. Therefore, the archive should contains below:
    • the installer script: install.sh:
      #!/bin/bash
      
      #
      # The installer script (example only, you should modify to adapt your needs.)
      # 
      
      CUR_DIR=$(pwd)
      SRC_DIR=$(cd $(dirname $0);pwd)
      DST_DIR=/usr/local/example
      
      sudo mkdir -p $DST_DIR
      cp -pr ./* $DST_DIR/
      
    • the actual content files
Also, we use a builder script: build.sh to create the self-extractable archive file.

  1. To start, we need to prepare thhe directory structure like below:
    ./archive
    ./archive/bin
    ./archive/doc
    ./archive/install.sh
    ./archive/lib
    ./build.sh
    ./extract.sh
    
    The archive directory is the directory contains the install.sh and the actual content files to install. The extract.sh file is extractor script.
  2. The build.sh script:
    #!/bin/bash
    
    #
    # The builder script
    #
    
    cd archive
    tar pczvf ../arc.tar.gz ./*
    cd ..
    cat extract.sh arc.tar.gz > install.bin
    rm arc.tar.gz
    chmod +x install.bin
    
  3. The final self-extractable installer: install.bin

See Also







No comments:

Post a Comment