2026-03-05 17:56:15 -07:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
# Fix ownership and permissions on the WROLPi media directory so WROLPi can read and write all files.
|
|
|
|
|
|
|
|
|
|
# Re-execute this script if it wasn't called with sudo.
|
|
|
|
|
if [ $EUID != 0 ]; then
|
|
|
|
|
sudo "$0" "$@"
|
|
|
|
|
exit $?
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
MEDIA_DIRECTORY="/media/wrolpi"
|
|
|
|
|
|
2026-11-30 19:32:39 -07:00
|
|
|
# Directories: WROLPi can list, enter, and write them; every service can list and enter them. Files: WROLPi can read and
|
|
|
|
|
# write them; every service can read them (Caddy serves media as its own user, so a 700 directory is a 403). Bits are
|
|
|
|
|
# only ever added. config/ holds private files (the VPN's key and login, and more): there, only WROLPi's own bits are
|
|
|
|
|
# fixed.
|
|
|
|
|
fix_modes() {
|
|
|
|
|
local media=$1
|
|
|
|
|
# find prints paths as given, so "/media/wrolpi/" would never match the "/media/wrolpi//config" prune.
|
|
|
|
|
while [ "${media}" != "/" ] && [ "${media%/}" != "${media}" ]; do
|
|
|
|
|
media=${media%/}
|
|
|
|
|
done
|
|
|
|
|
local skip=( \( -path "${media}/config" -o -path "${media}/lost+found" \) -prune -o )
|
|
|
|
|
dirs_fixed=$(find "${media}" "${skip[@]}" -type d ! -perm -755 -print 2>/dev/null | wc -l | tr -d ' ')
|
|
|
|
|
if [ "${dirs_fixed}" -gt 0 ]; then
|
|
|
|
|
find "${media}" "${skip[@]}" -type d ! -perm -755 -exec chmod u+rwx,go+rx {} + 2>/dev/null || errors=$((errors + 1))
|
|
|
|
|
fi
|
|
|
|
|
files_fixed=$(find "${media}" "${skip[@]}" -type f ! -perm -644 -print 2>/dev/null | wc -l | tr -d ' ')
|
|
|
|
|
if [ "${files_fixed}" -gt 0 ]; then
|
|
|
|
|
find "${media}" "${skip[@]}" -type f ! -perm -644 -exec chmod u+rw,go+r {} + 2>/dev/null || errors=$((errors + 1))
|
|
|
|
|
fi
|
|
|
|
|
local config="${media}/config" count
|
|
|
|
|
if [ -d "${config}" ]; then
|
|
|
|
|
count=$(find "${config}" -type d ! -perm -700 -print 2>/dev/null | wc -l | tr -d ' ')
|
|
|
|
|
if [ "${count}" -gt 0 ]; then
|
|
|
|
|
find "${config}" -type d ! -perm -700 -exec chmod u+rwx {} + 2>/dev/null || errors=$((errors + 1))
|
|
|
|
|
dirs_fixed=$((dirs_fixed + count))
|
|
|
|
|
fi
|
|
|
|
|
count=$(find "${config}" -type f ! -perm -600 -print 2>/dev/null | wc -l | tr -d ' ')
|
|
|
|
|
if [ "${count}" -gt 0 ]; then
|
|
|
|
|
find "${config}" -type f ! -perm -600 -exec chmod u+rw {} + 2>/dev/null || errors=$((errors + 1))
|
|
|
|
|
files_fixed=$((files_fixed + count))
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-05 17:56:15 -07:00
|
|
|
# Source .env override if it exists.
|
|
|
|
|
if [ -f /opt/wrolpi/.env ]; then
|
|
|
|
|
# shellcheck disable=SC1091
|
|
|
|
|
source /opt/wrolpi/.env
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ ! -d "${MEDIA_DIRECTORY}" ]; then
|
|
|
|
|
echo "ERROR: Media directory does not exist: ${MEDIA_DIRECTORY}"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
2026-03-05 18:18:53 -07:00
|
|
|
# Detect the filesystem type of the media directory.
|
|
|
|
|
fstype=$(findmnt -n -o FSTYPE --target "${MEDIA_DIRECTORY}" 2>/dev/null)
|
|
|
|
|
|
|
|
|
|
if [[ "${fstype}" == "vfat" || "${fstype}" == "exfat" || "${fstype}" == "ntfs" || "${fstype}" == "fuseblk" ]]; then
|
|
|
|
|
echo "ERROR: ${MEDIA_DIRECTORY} is on a ${fstype} filesystem."
|
|
|
|
|
echo ""
|
|
|
|
|
echo "FAT, exFAT, and NTFS filesystems do not support Unix file permissions."
|
|
|
|
|
echo "Ownership and permissions are controlled by mount options instead."
|
|
|
|
|
echo ""
|
|
|
|
|
echo "To fix permissions, remount the drive with the correct options:"
|
|
|
|
|
echo " sudo mount -o remount,uid=wrolpi,gid=wrolpi ${MEDIA_DIRECTORY}"
|
|
|
|
|
echo ""
|
|
|
|
|
echo "Or add to /etc/fstab for a permanent fix:"
|
|
|
|
|
echo " UUID=<your-drive-uuid> ${MEDIA_DIRECTORY} ${fstype} uid=wrolpi,gid=wrolpi,nofail 0 0"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
2026-03-05 17:56:15 -07:00
|
|
|
echo "Fixing permissions on ${MEDIA_DIRECTORY}..."
|
|
|
|
|
|
|
|
|
|
errors=0
|
|
|
|
|
dirs_fixed=0
|
|
|
|
|
files_fixed=0
|
|
|
|
|
|
2026-03-05 18:18:53 -07:00
|
|
|
# Fix ownership.
|
2026-11-30 19:32:39 -07:00
|
|
|
ownership_fixed=$(find "${MEDIA_DIRECTORY}" \( ! -user wrolpi -o ! -group wrolpi \) -print 2>/dev/null | wc -l | tr -d ' ')
|
2026-03-05 17:56:15 -07:00
|
|
|
if ! chown -R wrolpi:wrolpi "${MEDIA_DIRECTORY}" 2>/dev/null; then
|
2026-03-05 18:18:53 -07:00
|
|
|
echo "WARNING: Could not change ownership."
|
2026-03-05 17:56:15 -07:00
|
|
|
errors=$((errors + 1))
|
|
|
|
|
fi
|
|
|
|
|
|
2026-11-30 19:32:39 -07:00
|
|
|
fix_modes "${MEDIA_DIRECTORY}"
|
2026-03-05 17:56:15 -07:00
|
|
|
|
|
|
|
|
echo ""
|
|
|
|
|
echo "=== Summary ==="
|
2026-11-30 19:32:39 -07:00
|
|
|
echo "Ownership fixed: ${ownership_fixed}"
|
2026-03-05 17:56:15 -07:00
|
|
|
echo "Directories fixed: ${dirs_fixed}"
|
|
|
|
|
echo "Files fixed: ${files_fixed}"
|
|
|
|
|
echo "Errors: ${errors}"
|
|
|
|
|
|
|
|
|
|
if [ "$errors" -gt 0 ]; then
|
|
|
|
|
echo ""
|
2026-03-05 18:18:53 -07:00
|
|
|
echo "Some operations failed."
|
2026-03-05 17:56:15 -07:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
echo ""
|
|
|
|
|
echo "Done."
|