Browse Source

Initial commit

cryobry 5 years ago
parent
commit
fbe8e21972
4 changed files with 394 additions and 2 deletions
  1. 4 2
      .atom-build.yml
  2. 55 0
      functions
  3. 209 0
      openwrtBuild
  4. 126 0
      openwrtContainerBuild

+ 4 - 2
.atom-build.yml

@@ -1,5 +1,7 @@
 cmd: 'echo "Pick a command (see .atom-build.yml)"'
 name: ''
 targets:
-  Run in toolbox:
-    cmd: 'buildWrapper toolboxRun -c openwrt {FILE_ACTIVE}'
+  Build RPi4 snapshot in toolbox:
+    cmd: 'buildWrapper toolboxRun -c openwrt {FILE_ACTIVE} --version snapshot --target brcm2708/bcm2711 --profile rpi-4 --ssh-backup root@192.168.2.1'
+  Build Archer snapshot in toolbox:
+    cmd: 'buildWrapper toolboxRun -c openwrt {FILE_ACTIVE} --version "snapshot" --target "brcm2708/bcm2711" --profile "rpi-4" --ssh-backup "root@192.168.2.1"'

+ 55 - 0
functions

@@ -0,0 +1,55 @@
+#!/usr/bin/env bash
+
+#####################
+##### DEFAULTS ######
+#####################
+
+declare -x _packages=(
+    "luci" "luci-theme-material" "luci-app-ddns" \
+    "wireguard" "luci-app-wireguard" "luci-app-vpn-policy-routing" \
+    "-dnsmasq" "dnsmasq-full" \
+    "luci-app-upnp" \
+    "nano" "htop")
+declare -x _builddir
+_builddir="$(pwd)"
+declare -x _filesroot="$_builddir/files/"
+declare -x _ssh_backup="root@192.168.2.1"
+declare -x _backedup=(
+    '/etc/config/*' \
+    '/etc/dropbear/*')
+declare -x _debug="false"
+
+#####################
+##### FUNCTIONS #####
+#####################
+
+runDebug () { [[ "$_debug" == "true" ]] && echo "Running: " "$@" ; }
+
+
+sshBackup () {
+
+    runDebug "${FUNCNAME[0]}"
+
+    for fd in "${_backedup[@]}"; do
+        _dir="${fd%/*}/"
+        [[ ! -d "$_filesroot$_dir" ]] && mkdir -p "$_filesroot/$_dir"
+        if ! scp -rp "$_ssh_backup:$fd" "$_filesroot/$_dir"; then
+            echo "Did not successfully backup files from --ssh-backup"
+            echo "Exiting now to prevent data loss!"
+            exit 1
+        fi
+    done
+}
+
+
+getOS () {
+
+    runDebug "${FUNCNAME[0]}"
+
+    if [[ -f /etc/os-release ]]; then
+        source /etc/os-release
+    else
+        echo "Cannot detect OS!"
+        exit 1
+    fi
+}

+ 209 - 0
openwrtBuild

@@ -0,0 +1,209 @@
+#!/usr/bin/env bash
+source ./functions
+
+#####################
+##### DEFAULTS ######
+#####################
+
+# TP-Link Archer C7 v2
+_version="19.07.2"
+_target="ath79/generic"
+_profile="tplink_archer-c7-v2"
+
+# Raspberry Pi 4
+#_version="snapshot"
+#_target="brcm2708/bcm2711"
+#_profile="rpi-4"
+
+_debug="false" # Turn debugging on by default (useful for testing)
+
+#####################
+##### FUNCTIONS #####
+#####################
+printHelpAndExit () {
+
+    runDebug "${FUNCNAME[0]}"
+
+    cat <<-'EOF'
+USAGE:
+buildOpenWRT [[OPTION] [VALUE]]...
+
+OPTIONS
+    --version, -v OPENWRT_VERSION
+        Default: 19.07.02
+    --target, -t TARGET
+        Default: ath79/generic
+    --profile, -p PROFILE
+        Default: tplink_archer-c7-v2
+    --builddir, -b PATH
+        Default: Current working directory
+    --ssh-backup SSH path
+        Example: root@192.168.1.1
+    --debug, -d
+    --help, -h
+EOF
+        # Exit using passed exit code
+        [[ -z $1 ]] && exit 0 || exit "$1"
+}
+
+
+parseInput () {
+
+    runDebug "${FUNCNAME[0]}"
+
+    if _input=$(getopt -o +v:t:p:b:dh -l version:,target:,profile:,builddir:,ssh-backup:debug,help -- "$@"); then
+        eval set -- "$_input"
+        while true; do
+            case "$1" in
+                --version|-v)
+                    shift && _version="$1"
+                    ;;
+                --target|-t)
+                    shift && _target="$1"
+                    ;;
+                --profile|-p)
+                    shift && _profile="$1"
+                    ;;
+                --builddir|-b)
+                    shift && _builddir="$1"
+                    ;;
+                --ssh-backup)
+                    shift && _ssh_backup="$1"
+                    ;;
+                --debug|-d)
+                    echo "Debugging on"
+                    _debug="true"
+                    ;;
+                --help|-h)
+                    _printHelpAndExit 0
+                    ;;
+                --)
+                    shift
+                    break
+                    ;;
+            esac
+            shift
+        done
+    else
+        echo "Incorrect options provided"
+        printHelpAndExit 1
+    fi
+}
+
+
+mkDirs () {
+
+    runDebug "${FUNCNAME[0]}"
+
+    [[ ! -d "$_builddir/output/sources" ]] && mkdir -p "$_builddir/output/sources"
+    [[ ! -d "$_filesroot" ]] && mkdir -p "$_filesroot"
+    [[ ! -d "$_builddir/bin" ]] && mkdir -p "$_builddir/bin"
+}
+
+
+installPrerequisites () {
+
+    runDebug "${FUNCNAME[0]}"
+
+    if [[ "$ID" == "fedora" ]]; then
+        if ! sudo dnf -y install @c-development @development-tools @development-libs zlib-static elfutils-libelf-devel gawk unzip file wget python3 python2 > /dev/null 2>&1; then
+            echo "Warning: Problem installing prerequisites"
+        fi
+    elif [[ "$ID" =~ ^(debian|ubuntu)$ ]]; then
+        if ! sudo apt-get -y install build-essential libncurses5-dev libncursesw5-dev zlib1g-dev gawk git gettext libssl-dev xsltproc wget unzip python > /dev/null 2>&1; then
+            echo "Warning: Problem installing prerequisites"
+        fi
+    fi
+}
+
+
+acquireImageBuilder () {
+
+    runDebug "${FUNCNAME[0]}"
+
+    local _url
+
+    if [[ "$_version" == "snapshot" ]]; then
+        _filename="openwrt-imagebuilder-${_target//\//-}.Linux-x86_64.tar.xz"
+        _url="https://downloads.openwrt.org/snapshots/targets/$_target/$_filename"
+    else
+        _filename="openwrt-imagebuilder-$_version-${_target//\//-}.Linux-x86_64.tar.xz"
+        _url="https://downloads.openwrt.org/releases/$_version/targets/$_target/$_filename"
+    fi
+
+    if [[ ! -f "$_builddir/output/sources/$_filename" ]]; then
+            echo "Downloading $_url to $_builddir/output/sources"
+        if ! wget -q -P "$_builddir/output/sources" "$_url"; then
+            echo "Could not download Image Builder"
+            exit 1
+        fi
+    else
+        echo "Image builder already exists, skipping download..."
+    fi
+}
+
+
+extractImageBuilder () {
+
+    runDebug "${FUNCNAME[0]}"
+
+    if [[ -f "$_builddir/output/sources/$_filename" ]]; then
+        if ! tar -xf "$_builddir/output/sources/$_filename" -C "$_builddir/output/sources/"; then
+            echo "Extraction failed"
+            exit 1
+        fi
+    fi
+}
+
+
+makeImage () {
+
+    runDebug "${FUNCNAME[0]}"
+
+    # move to extracted source directory
+    if ! pushd "$_builddir/output/sources/${_filename%.tar.xz}" > /dev/null 2>&1; then
+        exit 1
+    fi
+
+    # build image
+    if ! make -j4 image BIN_DIR="$_builddir/bin" PROFILE="$_profile" PACKAGES="${_packages[*]}" FILES="$_filesroot"; then
+        echo "Make image failed!"
+        exit 1
+    fi
+
+    if ! popd > /dev/null 2>&1; then
+        exit 1
+    fi
+}
+
+# TODO
+#flashImage () {
+#
+#    if ! scp -rp  "$_bin_path" "$_ssh_backup:/tmp/$_bin_name"; then
+#        echo "Could not copy update file to device!"
+#        exit 1
+#    fi
+#
+#3    # shellcheck disable=SC2029
+#    if ! ssh "$_ssh_backup" "sysupgrade -v /tmp/$_bin_name"; then
+#3        echo "sysupgrade failed!"
+#        exit 1
+#    fi
+#}
+
+
+__main () {
+
+    parseInput "$@"
+    mkDirs
+    getOS
+    installPrerequisites
+    acquireImageBuilder
+    extractImageBuilder
+    [[ -n $_ssh_backup ]] && sshBackup
+    makeImage
+    #flashImage # TODO
+    exit 0
+}
+
+__main "$@"

+ 126 - 0
openwrtContainerBuild

@@ -0,0 +1,126 @@
+#!/usr/bin/env bash
+source ./functions
+
+#####################
+##### DEFAULTS ######
+#####################
+
+_image="ath79-generic-19.07.2"
+_profile="tplink_archer-c7-v2"
+_debug="true" # Turn debugging on by default (useful for testing)
+
+#####################
+##### FUNCTIONS #####
+#####################
+printHelpAndExit () {
+
+    runDebug "${FUNCNAME[0]}"
+
+    cat <<-'EOF'
+USAGE:
+buildOpenWRT [[OPTION] [VALUE]]...
+
+OPTIONS
+    --image, -i OPENWRT_IMAGE
+        Default: "ath79-generic-19.07.2"
+    --profile, -p PROFILE
+        Default: tplink_archer-c7-v2
+    --builddir, -b PATH
+        Default: Current working directory
+    --package
+        Add this package to the packages array declared in DEFAULTS
+        Supports multiple --package directives
+    --ssh-backup SSH_PATH
+        Example: root@192.168.1.1
+    --backup PATH
+        Add this file or directory to the packages array declared in DEFAULTS
+    --debug, -d
+    --help, -h
+EOF
+        # Exit using passed exit code
+        [[ -z $1 ]] && exit 0 || exit "$1"
+}
+
+
+parseInput () {
+
+    runDebug "${FUNCNAME[0]}"
+
+    if _input=$(getopt -o +i:p:b:dh -l image:,profile:,builddir:,ssh-backup:,backup:,package:,debug,help -- "$@"); then
+        eval set -- "$_input"
+        while true; do
+            case "$1" in
+                --image|-i)
+                    shift && _image="$1"
+                    ;;
+                --profile|-p)
+                    shift && _profile="$1"
+                    ;;
+                --package)
+                    shift && _packages+=("$1")
+                    ;;
+                --builddir|-b)
+                    shift && _builddir="$1"
+                    ;;
+                --ssh-backup)
+                    shift && _ssh_backup="$1"
+                    ;;
+                --backup)
+                    shift && _backedup+=("$1")
+                    ;;
+                --debug|-d)
+                    echo "Debugging on"
+                    _debug="true"
+                    ;;
+                --help|-h)
+                    _printHelpAndExit 0
+                    ;;
+                --)
+                    shift
+                    break
+                    ;;
+            esac
+            shift
+        done
+    else
+        echo "Incorrect options provided"
+        printHelpAndExit 1
+    fi
+}
+
+
+mkDirs () {
+
+    runDebug "${FUNCNAME[0]}"
+
+    [[ ! -d "$_filesroot" ]] && \
+        mkdir -p "$_filesroot"
+
+    [[ ! -d "$_builddir/bin" ]] && \
+        mkdir -p "$_builddir/bin"
+
+
+    # fix SELinux contexts
+    chcon -t container_file_t -R "$_builddir/bin"
+    chcon -t container_file_t -R "$_filesroot"
+}
+
+
+__main() {
+
+    parseInput "$@"
+
+    mkDirs
+
+    [[ -n $_ssh_backup ]] && sshBackup
+
+    podman run \
+            --rm \
+            --userns="keep-id" \
+            -v "$_builddir/bin/:/home/build/openwrt/bin" \
+            -v "$_filesroot:$_filesroot" \
+            openwrtorg/imagebuilder:"$_image" \
+            make image PROFILE="$_profile" PACKAGES="${_packages[*]}" FILES="$_filesroot"
+}
+
+__main "$@"