Skip to content

Instantly share code, notes, and snippets.

@dcoles
Last active February 23, 2020 00:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcoles/8acf05761e6579aeb8fff74bcfa7a755 to your computer and use it in GitHub Desktop.
Save dcoles/8acf05761e6579aeb8fff74bcfa7a755 to your computer and use it in GitHub Desktop.
Run AppImage container
#!/bin/bash
# Run AppImage container
set -e
function _mount {
local target
target="$(mktemp --tmpdir --directory appimage.XXXXXXXXXX)"
/bin/mount --types squashfs -o offset="${2:-0}" --read-only -- "${1}" "${target}"
echo "${target}"
}
function _unmount {
/bin/umount -- "${1}"
rmdir "${1}"
}
function squashfs_offset {
# Get offset from runner
PATH="${PATH}:${PWD}" /sbin/runuser -u "${USER}" -- "${APP}" --appimage-offset
# Alternatively we can sniff the AppImage
# Squashfs magic: 0x73717368 (i.e. "hsqs" little-endian)
#grep -obUa 'hsqs' "$1" | tail -n 1 | cut -d : -f 1 -
}
if [[ $# -lt 1 ]]; then
echo >&2 'Usage: <appimage> <arg>...'
exit 1
fi
if [[ "${EUID}" -ne 0 ]]; then
# Must run as root
exec /usr/bin/sudo "$0" "$@"
fi
USER="${SUDO_USER:-root}"
APP="$1"
APPNAME="$(basename "${APP}")"
APPRUN=./AppRun
OFFSET="$(squashfs_offset "${APP}")"
# Mount app
TARGET="$(_mount "${APP}" "${OFFSET}")"
trap "_unmount \"\${TARGET}\"" EXIT
# Run app
shift
(
cd "${TARGET}"
/sbin/runuser -u "${USER}" -- "${BASH}" -c "exec -a "${APPNAME}" \"\$@\"" -- "${APPRUN}" "$@"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment