diff --git a/rpm/sdcard-utils.spec b/rpm/sdcard-utils.spec index c107e84..5b2cda5 100755 --- a/rpm/sdcard-utils.spec +++ b/rpm/sdcard-utils.spec @@ -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 @@ -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} diff --git a/rules/90-mount-sd.rules b/rules/90-mount-sd.rules index b06cd27..eb63611 100644 --- a/rules/90-mount-sd.rules +++ b/rules/90-mount-sd.rules @@ -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" diff --git a/scripts/mount-sd.sh b/scripts/mount-sd.sh index 59841ce..5dea821 100755 --- a/scripts/mount-sd.sh +++ b/scripts/mount-sd.sh @@ -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 @@ -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) @@ -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