Conty/squashfs-start.sh

193 lines
5.4 KiB
Bash
Raw Normal View History

2021-03-26 18:03:50 +01:00
#!/usr/bin/env bash
## Dependencies: fuse2 tar
# Prevent launching as root
if [ -z $ALLOW_ROOT ]; then
if [ $EUID = 0 ]; then
echo "Do not run this app as root!"
echo
echo "If you really need to run it as root, set ALLOW_ROOT env variable."
2021-03-26 18:03:50 +01:00
exit 1
fi
fi
# Full path to the script
script_literal="${BASH_SOURCE[0]}"
script_name="$(basename "${script_literal}")"
script="$(readlink -f "${script_literal}")"
2021-03-26 18:03:50 +01:00
# Working directory where squashfs image will be mounted
# Default path: /tmp/scriptname_username_randomnumber
working_dir=/tmp/"$(basename "${script}")"_"$(id -un)"_$RANDOM
2021-03-26 18:03:50 +01:00
# It's important to set correct sizes below, otherwise there will be
# a problem with mounting the squashfs image due to an incorrectly calculated offset.
# The size of this script
scriptsize=5483
2021-03-26 18:03:50 +01:00
# The size of the utils.tar archive
# utils.tar contains bwrap and squashfuse binaries
utilssize=1259520
# Offset where the squashfs image is stored
offset=$((scriptsize+utilssize))
if [ "$1" = "--help" ] || [ "$1" = "-h" ] || ([ -z "$1" ] && [ -z "${AUTOSTART}" ] && [ ! -L "${script_literal}" ]); then
echo "Usage: ./conty.sh command command_arguments"
echo
2021-03-26 18:03:50 +01:00
echo "Arguments:"
echo
echo -e "-e \tExtract squashfs image"
echo -e "-o \tShow squashfs image offset"
2021-03-26 18:03:50 +01:00
echo
echo "Environment variables:"
echo
echo -e "AUTOSTART \tAutostarts an application specified in this variable"
echo -e "\t\tFor example, AUTOSTART=\"steam\" or AUTOSTART=\"/home/username/"
echo -e "\t\tprogram\""
echo -e "AUTOARGS \tAutomatically appends arguments from this variable to a"
echo -e "\t\tlaunched application. For example, AUTOARGS=\"--version\""
echo -e "\t\tCan be used together with AUTOSTART, but also without it."
2021-03-26 18:03:50 +01:00
echo -e "DISABLE_NET \tDisables network access"
echo -e "SANDBOX \tEnables filesystem sandbox"
echo -e "BIND \t\tBinds directories and files (separated by space) from host"
echo -e "\t\tsystem to the container. All specified items must exist."
echo -e "\t\tFor example, BIND=\"/home/username/.config /etc/pacman.conf\""
2021-03-26 18:03:50 +01:00
echo
echo "If you enable SANDBOX but don't set BIND, then"
2021-03-26 18:03:50 +01:00
echo "no directories will be available at all. And a fake temporary HOME"
echo "directory will be created inside the container."
echo
echo "Also, if the script is a symlink to itself but with different name,"
echo "then the symlinked script will automatically run a program according it's name."
echo "For instance, if the script is a symlink with the name \"wine\", then it will"
echo "automatically run wine during launch. This is an alternative"
echo "to AUTOSTART variable, but the variable has higher priority."
2021-03-26 18:03:50 +01:00
exit
elif [ "$1" = "-e" ]; then
if command -v unsquashfs 1>/dev/null; then
unsquashfs -o $offset -d "$(basename "${script}")"_files "${script}"
2021-03-26 18:03:50 +01:00
else
echo "To extract the image install squashfs-tools."
fi
2021-03-26 18:03:50 +01:00
exit
elif [ "$1" = "-o" ]; then
echo $offset
2021-03-26 18:03:50 +01:00
exit
fi
# Check if FUSE2 is installed
if command -v fusermount 1>/dev/null; then
fmount=fusermount
else
echo "Please install fuse2 and run the app again"
2021-03-26 18:03:50 +01:00
exit 1
fi
# Extract utils.tar
mkdir -p "${working_dir}"
tail -c +$((scriptsize+1)) "${script}" | head -c $utilssize > "${working_dir}"/utils.tar
tar -C "${working_dir}" -xf "${working_dir}"/utils.tar
rm "${working_dir}"/utils.tar
export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${working_dir}/utils"
sfuse="${working_dir}"/utils/squashfuse
bwrap="${working_dir}"/utils/bwrap
chmod +x "${sfuse}"
chmod +x "${bwrap}"
run_bwrap () {
if [ -n "$DISABLE_NET" ]; then
echo "Network is disabled"
2021-03-26 18:03:50 +01:00
net="--unshare-net"
fi
2021-03-26 18:03:50 +01:00
if [ -n "$SANDBOX" ]; then
echo "Filesystem sandbox is enabled"
2021-03-26 18:03:50 +01:00
dirs="--tmpfs /home --tmpfs /opt --tmpfs /mnt --dir ${HOME}"
unshare="--unshare-user-try --unshare-pid --unshare-uts --unshare-cgroup-try \
--hostname Conty"
2021-03-26 18:03:50 +01:00
else
2021-03-27 12:56:22 +01:00
dirs="--bind /home /home --bind-try /mnt /mnt --bind-try /opt /opt --bind-try /media /media"
2021-03-26 18:03:50 +01:00
fi
if [ -n "$BIND" ]; then
echo "Binded items: ${BIND}"
for i in ${BIND}; do
bind="${bind} --bind ${i} ${i}"
done
dirs="${dirs} ${bind}"
fi
2021-03-26 18:03:50 +01:00
echo
"${bwrap}" --ro-bind "${working_dir}"/mnt / \
--dev-bind /dev /dev \
--ro-bind /sys /sys \
--bind /run /run \
--bind /var /var \
--bind /tmp /tmp \
--ro-bind-try /etc/resolv.conf /etc/resolv.conf \
--ro-bind-try /etc/hosts /etc/hosts \
--ro-bind-try /etc/nsswitch.conf /etc/nsswitch.conf \
--proc /proc \
--ro-bind-try /usr/local /usr/local \
${dirs} ${net} \
2021-03-27 13:30:56 +01:00
--setenv PATH "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/lib/jvm/default/bin:${PATH}" \
2021-03-26 18:03:50 +01:00
"$@"
}
# Mount boostrap image
mkdir -p "${working_dir}"/mnt
"${fmount}" -u "${working_dir}"/mnt 2>/dev/null || umount "${working_dir}"/mnt 2>/dev/null
"${sfuse}" -o offset="${offset}" "${script}" "${working_dir}"/mnt
if [ $? = 0 ]; then
echo "Running Conty"
if [ -n "${AUTOSTART}" ]; then
autostart="${AUTOSTART}"
elif [ -L "${script_literal}" ]; then
if [ -f "${working_dir}"/mnt/usr/bin/"${script_name}" ]; then
autostart="${script_name}"
fi
fi
if [ -n "${AUTOARGS}" ]; then
echo "Automatically append arguments: ${AUTOARGS}"
fi
if [ -n "${autostart}" ]; then
echo "Autostarting ${autostart}"
run_bwrap "${autostart}" "$@" ${AUTOARGS}
else
run_bwrap "$@" ${AUTOARGS}
fi
2021-03-26 18:03:50 +01:00
"${fmount}" -uz "${working_dir}"/mnt 2>/dev/null || umount --lazy "${working_dir}"/mnt 2>/dev/null
else
echo "Mounting the squashfs image failed!"
2021-03-26 18:03:50 +01:00
exit 1
fi
2021-03-28 11:21:05 +02:00
sleep 2
2021-03-26 18:03:50 +01:00
rm -rf "${working_dir}"
exit