Last active
February 23, 2020 00:38
-
-
Save dcoles/8acf05761e6579aeb8fff74bcfa7a755 to your computer and use it in GitHub Desktop.
Run AppImage container
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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