Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions rpm/sdcard-utils.spec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Name: sd-utils
Summary: SailfishOS scripts to mount/umount external sdcard.
Version: 0.3
Version: 0.4
Release: 1
Group: System/Base
License: MIT
Expand All @@ -9,7 +9,8 @@ URL: https://github.com/nemomobile/sd-utils/
Source0: %{name}-%{version}.tar.gz
BuildRequires: systemd
Requires: systemd
Requires: tracker
# Required for lsblk
Requires: util-linux

%description
%{summary}
Expand Down
2 changes: 1 addition & 1 deletion rules/90-mount-sd.rules
Original file line number Diff line number Diff line change
@@ -1 +1 @@
SUBSYSTEM=="block", KERNEL=="mmcblk1*", ACTION=="add", MODE="0660", TAG+="systemd", ENV{SYSTEMD_WANTS}="mount-sd@%k.service", ENV{SYSTEMD_USER_WANTS}="tracker-miner-fs.service tracker-store.service"
SUBSYSTEM=="block", KERNEL=="mmcblk1*", ACTION=="add", MODE="0660", TAG+="systemd", ENV{SYSTEMD_WANTS}="mount-sd@%k.service"
71 changes: 55 additions & 16 deletions scripts/mount-sd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ DEF_GID=$(grep "^GID_MIN" /etc/login.defs | tr -s " " | cut -d " " -f2)
DEVICEUSER=$(getent passwd $DEF_UID | sed 's/:.*//')
MNT=/media/sdcard
MOUNT_OPTS="dirsync,noatime,users"
# options: --discard=once --priority 10
SWAP_OPTS="--discard --priority 10"
ACTION=$1
DEVNAME=$2

if [ -z "${ACTION}" ] || [ -z "${DEVNAME}" ]; then
if [ -z "${ACTION}" ]; then
systemd-cat -t mount-sd /bin/echo "ERROR: Action needs to be defined."
exit 1
fi

if [ -z "${DEVNAME}" ]; then
systemd-cat -t mount-sd /bin/echo "ERROR: Device name needs to be defined."
exit 1
fi

Expand All @@ -21,36 +29,59 @@ if [ "$ACTION" = "add" ]; then

eval "$(/sbin/blkid -c /dev/null -o export /dev/$2)"

if [ -z "${UUID}" ] || [ -z "${TYPE}" ]; then
if [ -z "${TYPE}" ]; then
systemd-cat -t mount-sd /bin/echo "ERROR: Filesystem type missing for ${DEVNAME}."
exit 1
fi

if [ "${TYPE}" = "swap" ]; then
SWAP=$(grep -w ${DEVNAME} /proc/swaps | cut -d \ -f 1)
if [ -n "$SWAP" ]; then
systemd-cat -t mount-sd /bin/echo "${DEVNAME} already used as swap space, ignoring"
exit 0
fi
systemd-cat -t mount-sd /bin/echo "${DEVNAME} seems to be swap space"
swapon $SWAP_OPTS ${DEVNAME}
systemd-cat -t mount-sd /bin/echo "Finished ${ACTION}ing ${DEVNAME} of type ${TYPE} as swap space"
exit 0
fi

if [ -z "${UUID}" ]; then
# In case device does not have UUID lets create one for it based on
# the card identification.
PKNAME=$(lsblk -n -o PKNAME ${DEVNAME})
if [ -e "/sys/block/${PKNAME}/device/cid" ]; then
CID=$(cat /sys/block/${PKNAME}/device/cid)
if [ -n "${CID}" ]; then
IDNAME=$(lsblk -n -o NAME ${DEVNAME})
UUID="${CID}-${IDNAME}"
fi
fi

if [ -z "${UUID}" ]; then
# Exit here as in the future there might be things like USB OTG disks or
# sdcards attached via adapter that might behave differently and needs special case
# in case such happens fail so we don't break anything.
systemd-cat -t mount-sd /bin/echo "ERROR: Could not find or generate UUID for device ${DEVNAME}."
exit 1
fi
fi

DIR=$(grep -w ${DEVNAME} /proc/mounts | cut -d \ -f 2)
if [ -n "$DIR" ]; then
systemd-cat -t mount-sd /bin/echo "${DEVNAME} already mounted on ${DIR}, ignoring"
exit 0
fi

# This hack is here to delay mounting the sdcard until tracker is ready
export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$DEF_UID/dbus/user_bus_socket
count=1
while true; do
test $count -ge 64 && break
MINER_STATUS="$(dbus-send --type=method_call --print-reply --session --dest=org.freedesktop.Tracker1.Miner.Files /org/freedesktop/Tracker1/Miner/Files org.freedesktop.Tracker1.Miner.GetStatus | grep -o 'Idle')"
STORE_STATUS="$(dbus-send --type=method_call --print-reply --session --dest=org.freedesktop.Tracker1 /org/freedesktop/Tracker1/Status org.freedesktop.Tracker1.Status.GetStatus | grep -o 'Idle')"
test "$MINER_STATUS" = "Idle" -a "$STORE_STATUS" = "Idle" && break
systemd-cat -t mount-sd /bin/echo "Waiting $count seconds for tracker"
sleep $count ;
count=$(( count + count ))
done

test -d $MNT/${UUID} || mkdir -p $MNT/${UUID}
chown $DEF_UID:$DEF_GID $MNT $MNT/${UUID}
touch $MNT/${UUID}

case "${TYPE}" in
vfat|exfat)
mount ${DEVNAME} $MNT/${UUID} -o uid=$DEF_UID,gid=$DEF_GID,$MOUNT_OPTS,utf8,flush,discard || /bin/rmdir $MNT/${UUID}
mkdir -p /opt/alien/storage/sdcard1/
#mount ${DEVNAME} $MNT/${UUID} -o uid=$DEF_UID,gid=$DEF_GID,$MOUNT_OPTS,utf8,flush,discard || /bin/rmdir $MNT/${UUID}
mount ${DEVNAME} /opt/alien/storage/sdcard1/ -o uid=1015,gid=1015,fmask=002,dmask=002,$MOUNT_OPTS,utf8,flush,discard || /bin/rmdir $MNT/${UUID}
;;
# NTFS support has not been tested but it's being left to please the ego of an engineer!
ntfs)
Expand All @@ -72,6 +103,14 @@ else
fi
umount $DIR || umount -l $DIR
systemd-cat -t mount-sd /bin/echo "Finished ${ACTION}ing ${DEVNAME} at ${DIR}"
else
SWAP=$(grep -w ${DEVNAME} /proc/swaps | cut -d \ -f 1)
if [ -z "$SWAP" ]; then
systemd-cat -t mount-sd /bin/echo "${DEVNAME} in not currently used as swap space, ignoring"
exit 0
fi
swapoff "${SWAP}"
systemd-cat -t mount-sd /bin/echo "Finished ${ACTION}ing ${DEVNAME} as swap space"
fi
fi