We have moved to a new Sailfish OS Forum. Please start new discussions there.
4

Tune / bug fix /usr/sbin/mount-sd.sh

asked 2015-06-20 03:35:00 +0200

V10lator gravatar image

updated 2015-06-20 03:49:54 +0200

This thread complements https://together.jolla.com/question/96765/trim-on-all-supported-file-systems/ and https://together.jolla.com/question/73696/sd-card-mount-slow-with-11127-so-android-has-problems/

I updated the /usr/sbin/mount-sd.sh script. Changes:

  • Wait for tracker after mounting the filesystem instead of before. This works as we trigger the indexing by touching the mount dir anyway, so now we do mount -> wait -> touch instead of wait -> mount -> touch.
  • Trigger indexing after unmounting.
  • Remove temporary mount directory after unmounting.
  • Add discard mount option for ext4 and btrfs.
  • Remove some duplicated code.

To the Jolle devs: Feel free to use this script (as a template) for the next SailfishOS version. Just diff it with the current to see my changes.

#!/bin/bash

# The only case where this script would fail is:
# mkfs.vfat /dev/mmcblk1 then repartitioning to create an empty ext2 partition

DEF_UID=$(grep "^UID_MIN" /etc/login.defs |  tr -s " " | cut -d " " -f2)
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"
ACTION=$1
DEVNAME=$2

if [ -z "${ACTION}" ] || [ -z "${DEVNAME}" ]; then
    exit 1
fi

systemd-cat -t mount-sd /bin/echo "Called to ${ACTION} ${DEVNAME}"

if [ "$ACTION" = "add" ]; then

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

    if [ -z "${UUID}" ] || [ -z "${TYPE}" ]; then
        exit 1
    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

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

    case "${TYPE}" in
    vfat|exfat)
            MOUNT_OPTS="uid=$DEF_UID,gid=$DEF_GID,$MOUNT_OPTS,utf8,flush,discard"
        ;;
    # NTFS support has not been tested but it's being left to please the ego of an engineer!
    ntfs)
            MOUNT_OPTS="uid=$DEF_UID,gid=$DEF_GID,$MOUNT_OPTS,utf8"
        ;;
    # ext and btrfs are able to discard. Add more to the list if needed.
    ext4|btrfs)
            MOUNT_OPTS+=",discard"
        ;;
    esac
    mount ${DEVNAME} $MNT/${UUID} -o $MOUNT_OPTS || /bin/rmdir $MNT/${UUID}

    # This hack is here to delay triggering indexing before the tracker has started.
    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

    # Trigger indexing
    test -d $MNT/${UUID} && touch $MNT/${UUID}

    systemd-cat -t mount-sd /bin/echo "Finished ${ACTION}ing ${DEVNAME} of type ${TYPE} at $MNT/${UUID}"

else
    DIR=$(grep -w ${DEVNAME} /proc/mounts | cut -d \  -f 2)
    if [ -n "${DIR}" ] ; then
        if [ "${DIR##$MNT}" = "${DIR}" ]; then
            systemd-cat -t mount-sd /bin/echo "${DEVNAME} mountpoint ${DIR} is not under ${MNT}, ignoring"
            exit 0
        fi
        umount $DIR || umount -l $DIR

        # Deregister the indexed files from tracker
        touch ${DIR}

        # Remove directory
        rmdir ${DIR}

        systemd-cat -t mount-sd /bin/echo "Finished ${ACTION}ing ${DEVNAME} at ${DIR}"
    fi
fi

edit retag flag offensive close delete

Comments

1
# NTFS support has not been tested but it's being left to please the ego of an engineer!

Well, now the engineer can test it if they want: I've compiled a version of NTFS-3G for Jolla Phone.

DrYak ( 2015-06-20 07:36:58 +0200 )edit

Using this mount script I experience a sudden reboot (after a while) then auto-reboot again when the lock screen appears then somehow it comes back. First time reboot came when I opened photos from Gallery app. Today it rebooted when a video was played with ytplayer with wifi and 70% battery. Maybe the issues are not tied together - experiences of others might clarify.

llg179 ( 2015-06-23 17:51:27 +0200 )edit

Thanks for your report. It's just a shot in the blue but maybe reverting https://github.com/V10lator/sd-utils/commit/e332cbc8b0b681209b0fab932217dc763efe3382 might help.

To revert that simply open the mount script and re-add the line marked with the subtraction sign in front (but don't copy the subtraction mark itself).

Please report back if that helped. :)

//EDIT: But please also clean your battery contacts as they might be the real root of the reboots.

V10lator ( 2015-06-24 14:28:59 +0200 )edit

Some months ago I put paper to bottom of battery to avoid random reboots - I think the reboot was not caused by contact problem of battery. My main motivation was to move /home/nemo/.local/share to sdcard but some apps launch later than the sdcard is mounted therefore call history and sms send-receive did not work. With your updated script the call history could not load either so it did not bring the desired effect by itself. Maybe the startup script dependencies should be configured too. I will read more about dbus and systemd - but for me a workaround was to restart the voicecall-ui service after mounting the sdcard. I gave up the idea of using ~/.local/share as a link to folder of sdcard for a while because I need a reliable phone and both your elegant script and linking share to sdcard jeopardized that. Hopefully we will receive further feedbacks soon from others. :)

How could indexing or tracker cause reboots? How can a reboot be troubleshooted?

llg179 ( 2015-06-25 00:46:52 +0200 )edit

Neither indexing nor tracker should cause reboots. That's why I said it's just a shot in the blue. :)

Now as you already had problems with your battery contacts cleaning them won't harm, will it? Jolla itself has a how-to do it the best way somewhere around, am too lazy to search right now but IIRC it was basically "re-insert battery around 10 times".

You see, for me it works and nobody other than you confirmed your bug. So hunting it in some corners is the only thing we can do.

V10lator ( 2015-06-26 15:50:15 +0200 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2015-06-26 12:12:22 +0200

DrYak gravatar image

Other modification:

I've done a mod on github to add support for swap partition on SDcard.

They are mounted with

  • "discard" ("discard=once" isn't supported on Jolla, apparently)
  • "priority=10" (i.e.: lower than the zram swaps)
edit flag offensive delete publish link more

Comments

Great! my changes are also transformed into pull request (and actually under review) : https://github.com/nemomobile/sd-utils/pull/19

V10lator ( 2015-06-26 15:51:33 +0200 )edit

Do you think mount-sd@.service should be edited too? For example when /home/nemo/.local/share is moved and linked to sdcard then voicecall-ui does not work properly. Service restart of voicecall-ui helps. At least the voiceall-ui should start after proper mount of sdcard but there may be other applications too which should start after mount of sdcard.

llg179 ( 2015-06-27 20:30:38 +0200 )edit

Swap patch + my pull request now available at OpenRepos: https://openrepos.net/content/v10lator/bleeding-edge-sd-utils :)

V10lator ( 2015-06-28 08:53:34 +0200 )edit

How would I partition a card correctly for this?

Is exfat & swap on the card possible while still having it readable under windows that does not read flash memory with multiple partitions? Is SWAP regarded as a proper partition under windows?

Hope someone can chime in and help

MoritzJT ( 2015-06-29 01:10:23 +0200 )edit
1

How would I partition a card correctly for this?

When partitioning flash media, the most important is to keep the same alignment of start of partition.

e.g.: You'll notice that you current partition with which the media was sold starts at a 1MiB boundary (at sector 2048). Thus, if you want to create a swap partition, best would be to align it to 1MiB boundary too (at another multiple of 2048).

At all costs, avoid older software which try to align on cylinder boundaries (weird multiple of 63*255 sectors because that's what modern media pretend to have as sumber of heads and sectors per cylinder, even if the number don't actually make any sense in the real world).

Software like gparted has nice graphical interface and correctly supports alignments.

On the command line, you could use fdisk - that one is even available inside SailfishOS - or gdisk - most Linux distro come out of the box with it. (These last 2 are what I personally used).

I've personally used GPT disk label (exactly like Jolla phone's internal flash space), but you could probably keep the oridinal DOS disk label if you like.


Windows has its own partition manager. I don't have much experience with it. But I suppose I wouldn't be that difficult to repartition your flash media with it. Just create an unformated partition, then finish the job under Sailfish OS (Set the type with: fdisk /dev/mmcblk1, command t, select your new partition 2 and type 82Linux Swap. Then format the partition with mkswap -L 'SDSwap' /dev/mmcblk1p2)

DrYak ( 2015-06-29 11:07:06 +0200 )edit
Login/Signup to Answer

Question tools

Follow
4 followers

Stats

Asked: 2015-06-20 03:35:00 +0200

Seen: 502 times

Last updated: Jun 26 '15