distribution/packages/jelos/sources/scripts/automount

175 lines
4.8 KiB
Bash
Executable file

#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2023 JELOS (https://github.com/JustEnoughLinuxOS)
. /etc/profile
. /etc/os-release
UPDATE_ROOT="/storage/.update"
MOUNT_GAMES=$(get_setting system.automount)
GAMES_DEVICE=$(get_setting system.gamesdevice)
MS_ENABLED=$(get_setting system.merged.storage)
MS_DEVICE=$(get_setting system.merged.device)
MS_PATH="/storage/roms"
if [ -z "${MS_DEVICE}" ]
then
MS_DEVICE="external"
fi
MOUNT_PATH="/storage/games-${MS_DEVICE}"
function start_ms() {
if [ -e "/storage/.ms_unsupported" ] || \
[ "${MS_ENABLED}" = "false" ]
then
# If we're not using merged storage, bind mount the external storage path
# so we don't need to change any configs.
grep ${MOUNT_PATH} /proc/mounts >/dev/null 2>&1
if [ ! $? = 0 ]
then
MOUNT_PATH="/storage/games-internal"
fi
log $0 "Executing bind mount of ${MOUNT_PATH} to ${MS_PATH}"
mount --bind ${MOUNT_PATH}/roms ${MS_PATH}
exit 0
else
log $0 "Enabling merged storage."
case ${MS_DEVICE} in
internal)
LOWER="internal"
UPPER="external"
;;
*)
LOWER="external"
UPPER="internal"
;;
esac
for DIR in /storage/games-${UPPER}/.tmp/games-workdir /storage/games-${LOWER}/roms /storage/games-${UPPER}/roms
do
if [ ! -d "${DIR}" ]
then
mkdir -p "${DIR}"
fi
done
mount overlay -t overlay -o lowerdir=/storage/games-${LOWER}/roms,upperdir=/storage/games-${UPPER}/roms,workdir=/storage/games-${UPPER}/.tmp/games-workdir /storage/roms
fi
}
if [[ ! "${MOUNT_GAMES}" =~ [0-9] ]]
then
set_setting system.automount 1
elif [[ "${MOUNT_GAMES}" == "0" ]]
then
start_ms
exit 0
fi
function unmount_roms() {
NULL=$(cat /proc/mounts | grep -v -e "${MS_PATH}" 2>/dev/null | grep ${1})
if [ ! "$?" = "0" ]
then
umount -f "${MS_PATH}"
fi
}
function load_modules() {
for MODULE in exfat vfat
do
lsmod | grep ${MODULE} >/dev/null 2>&1
if [ ! $? = 0 ]
then
log $0 "Loading ${MODULE}."
modprobe ${MODULE} >/dev/null 2>&1
fi
done
}
function mount_games() {
unmount_roms
FSTYPE=$(blkid -o export ${1} | awk 'BEGIN {FS="="} /TYPE/ {print $2}')
case ${FSTYPE} in
ext4)
log $0 "Found supported partition for overlayfs."
if [ -e "/storage/.ms_unsupported" ]
then
rm -f /storage/.ms_unsupported
fi
touch /storage/.ms_supported
set_setting system.merged.storage 1
;;
*)
log $0 "Partition does not support overlayfs, disabling."
if [ -e "/storage/.ms_supported" ]
then
rm -f /storage/.ms_supported
fi
touch /storage/.ms_unsupported
;;
esac
if [ ! -d "${MOUNT_PATH}" ]
then
log $0 "Create directory ${MOUNT_PATH}"
/usr/bin/busybox mkdir -p ${MOUNT_PATH} >/dev/null 2>&1
fi
NULL=$(cat /proc/mounts | grep -v -e "/var/media" 2>/dev/null | grep ${1})
if [ ! "$?" = "0" ] && \
[ -e "${1}" ] && \
[ ! -e "/storage/.please_resize_me" ]
then
### Udevil shouldn't mount it this early, but just in-case.
umount /var/media/* 2>/dev/null
log $0 "Checking filesystem ${1}."
fsck -Mly ${1} >/dev/null 2>&1
log $0 "Mounting ${1} on ${MOUNT_PATH}"
/usr/bin/busybox mount ${1} ${MOUNT_PATH} >/dev/null 2>&1
fi
start_ms
exit 0
}
function find_games() {
if /usr/bin/busybox mountpoint -q /storage ; then
for DEV in $(for dev in mmcblk[0-9] sd[a-z] nvme[0-9]; do blkid | grep ${dev} | awk 'BEGIN {FS=":"}; /ext4/ || /fat/ {print $1}' | sort -r; done)
do
ROOTDEV=$(echo ${DEV} | sed -e "s#^/.*/##g" -e "s#p[0-9].*\$##g")
SIZE=$(awk '/'${ROOTDEV}'$/ {print $3}' /proc/partitions)
if (( ${SIZE} <= 8388608 ))
then
log $0 "Device ${ROOTDEV} is too small, ignoring."
# We don't want to mount partitions smaller than ~8GB.
continue
fi
if [ -L "/sys/class/block/${ROOTDEV}boot0" ]
then
log $0 "Device ${ROOTDEV} might be Android, ignoring."
# Assume this is an android boot device and ignore it.
continue
fi
# Udevil might mount it, and we shouldn't care if it does.
NULL=$(cat /proc/mounts | grep -v -e "/var/media" 2>/dev/null | grep ${DEV})
if [ ! "$?" = "0" ] && \
[ -e "${DEV}" ] && \
[ ! -e "/storage/.please_resize_me" ]
then
GAMES_DEVICE=${DEV}
log $0 "Found ${DEV} available to mount."
mount_games "${DEV}"
break
fi
done
# If we're here there is no external storage to use, but we want to start merged storage or the bind mount anyway.
start_ms
fi
}
load_modules
if [ -e "${GAMES_DEVICE}" ]
then
mount_games ${GAMES_DEVICE}
else
find_games
fi