Search This Blog

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,
    # 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
    # execute
    cd $TMP_DIR; $TMP_DIR/
    cd $CUR_DIR
    rm -rf $TMP_DIR
    exit 0
  • 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:
      # The installer script (example only, you should modify to adapt your needs.)
      SRC_DIR=$(cd $(dirname $0);pwd)
      sudo mkdir -p $DST_DIR
      cp -pr ./* $DST_DIR/
    • the actual content files
Also, we use a builder script: to create the self-extractable archive file.

  1. To start, we need to prepare thhe directory structure like below:
    The archive directory is the directory contains the and the actual content files to install. The file is extractor script.
  2. The script:
    # The builder script
    cd archive
    tar pczvf ../arc.tar.gz ./*
    cd ..
    cat 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