Useful scripts: Compiling big projects remotely

At the very start of the New Year I decided it’s big time to clean up all the trash from my ‘useful scripts’ folder. Wipe most, improve and document the most useful ones. (And check how the new syntax highlighting plugin works in wordpress and how the content shows up in Medium). I’ll start with the script I use to compile ‘heavy’ software on the ‘big’ server. (I’ll tell more about how I picked the hardware for the task later).

OpenWRT. Takes a while to build it with a bunch of packages enabled.

So, I have the following environment:

  • My laptop. I work on it,
  • Home Server Intel Xeon, fast SSD, huge amounts of RAM and a container with a correct environment for the build.

We want to compile stuff remotely, while working locally (No, no VNC! VNC sucks!). The build directory with all the sources is mounted via NFS. We only need to edit a few files, while the server needs to access them as fast as possible. Perhaps, SSHFS is another option, but while at home NFS is just plain faster. I also don’t want the build directory mounted all the time. So I’ve come up with a simple solution:

#!/bin/bash

NFSPATH=192.168.143.2:/srv/sanctuary/build
LOCALPATH=/srv/build
REMOTEPATH=/srv
SSHCMD="ssh buildblade.home -t"

checkmount() {
    MCL=`mount | grep "$LOCALPATH" | wc -l`
    if [ "$MCL" -lt "1" ]; then
        echo "[W] Staging area not mounted, mounting"
        sudo mount -t nfs -o vers=3 $NFSPATH $LOCALPATH
    else
        echo "[I] Staging area already mounted, skipping"
    fi
}

unmount() {
    sudo umount -l $LOCALPATH
}

cmd() {
    checkmount
    CMD="$*"
    CMD=${CMD/${LOCALPATH}/${REMOTEPATH}}
    PWD="`pwd`"
    PWD=${PWD/${LOCALPATH}/${REMOTEPATH}}
    echo "[I] Executing transformed cmd: ${CMD} in ${PWD}"
    $SSHCMD "cd ${PWD} && ${CMD}"
}

case "$1" in
    "--mount")
        checkmount
        ;;
    "--unmount")
        unmount
        ;;
    "--help")
        echo "Usage: bb [--mount | --unmount]"
        echo "Usage: bb cmd"
        ;;
    *)
        cmd $*
        ;;
esac

This script is put somewhere in your PATH. (I call this script bb for short). The usage is plain and simple:

  • bb –mount – mount the build directory locally
  • bb –unmount – unmount the build directory
  • bb command – Run the command in the same directory, but on the remote server

The script takes care to adjust the directory path, since the location may be different on the build machine and the laptop.

It has it’s limitation, like handling of quotes, but for anything that complex we can just dump it into a simple shell script and call it via bb. This is the way I occasionally compile LibreELEC, OpenIPC, OpenWRT, crosstool-ng and other huge projects that take a lot of CPU time.

One thought on “Useful scripts: Compiling big projects remotely

  1. There’s UNIX method for mounting without passing every time mount parameters. One should symply add an entry into /etc/fstab with noauto option to prevent automatical mounting at boot time:

    192.168.143.2:/srv/sanctuary/build /srv/build nfs defaults,rw,vers=3,noauto,_netdev 0 0

    Then all you need is just:
    1) mount /srv/build
    2) umount /srv/build
    3) check by
    mount -t nfs | grep /srv/build
    or
    mount | grep /srv/build

    By the way, as I understand, /srv directory is suited for server needs and not for client. I.e., /mnt was initially suited for mount points. That is mainly declarative, although, but can be affected by SELinux, AppArmor or another security mechanisms rules.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.