Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distinguish between fs types when mounting sdcards #8

Merged
merged 3 commits into from
Feb 6, 2014
Merged

Distinguish between fs types when mounting sdcards #8

merged 3 commits into from
Feb 6, 2014

Conversation

misja
Copy link
Contributor

@misja misja commented Dec 14, 2013

Currently only vfat formatted sdcards will get mounted, when using other formats like ext4 or btrfs the mount will fail since unsupported mount options are being passed (uid,gid).

Currently only vfat formatted sdcards will get mounted, when using other formats like ext4 or btrfs the mount will fail since  unsupported mount options are being passed (uid,gid).
Using user id instead of user name and making group explicit.
@misja
Copy link
Contributor Author

misja commented Dec 21, 2013

I've tried this with vfat and with btrfs plus received reports from users this also works fine with ext4.

@misja
Copy link
Contributor Author

misja commented Dec 21, 2013

As a sidenote, on ext4/btrfs etc, if the user already has files on the card under a different owner/group (eg created on a PC) then obviously the files will not be writable by user nemo. A fix for that will be to do a recursive chown but don't think that will be wise on 32/64/128Gb partitions ... Plus I assume that users choosing to use these filesystems will understand what they are doing.

I did some extra testing to make sure it all works well and noticed that this script gets called twice (haven't traced it), the first time getting passed an empty ID_FS_TYPE. This change checks if this value is set.
@viq
Copy link

viq commented Dec 31, 2013

@misja how are you getting/setting ID_FS_TYPE?

@viq
Copy link

viq commented Dec 31, 2013

FWIW I found this to work:
ID_FS_TYPE=$(/bin/lsblk -no FSTYPE /dev/mmcblk1p1)
or full version at https://gist.github.com/viq/8201021

@manu007
Copy link

manu007 commented Jan 1, 2014

I try to help, this is my first post.
This scriP only look for one partition, but imagine an SD card with more than one partition with different types of filesystems. I quickly write a different approach ;

#!/bin/bash

mount_vfat() {
echo Mount vfat partition user $MUID group $MGID target $MOUNTP type $PARTYPE
}

mount_all() {
echo Mount user $MUID group $MGID target $MOUNTP type $PARTYPE
}

USERMNT=manu
MUID=NONE
DEVICE=/dev/sdc
MOUNT=/mnt/blk
MMC=""

while [ -n "$1" ] ; do
case "$1" in
-us) shift; USERMNT=$1;;
-dv) shift; DEVICE=$1;;
-mn) shift; MOUNT=$1;;
esac
shift
done

MUID=$(getent passwd $USERMNT | cut -d ":" -f 3)

if [ "$MUID" = "" ]; then # no user found
exit 1
fi

MGID=$(getent passwd $USERMNT | cut -d ":" -f 4)

if [[ "$DEVICE" =~ "/dev/mm*" ]]
then
MMC="p"
fi

PARTNO=0
while [ "$PARTNO" -le "16" ]; do
if [ "$PARTNO" -eq "0" ]; then
PART=$DEVICE
MOUNTP=$MOUNT
else
PART=$DEVICE$MMC$PARTNO
MOUNTP=$MOUNT$PARTNO
fi
echo looking in $PART
PARTYPE=$(blkid -s TYPE $PART | cut -d """ -f 2)
if [ "$PARTYPE" != "" ]; then # something to mount
case "$PARTYPE" in
swap) echo swap nothing to do;;
vfat) mount_vfat;;
*) mount_all;;
esac
else
echo Nothing to mount $PARTYPE
fi
PARTNO=$[ $PARTNO+1 ]
done

exit 0

@misja
Copy link
Contributor Author

misja commented Jan 1, 2014

@viq and @manu007 This isn't a standalone script but gets triggered by a udev event. ID_FS_TYPE gets set by udev, and passing custom script arguments is nonsensical in this context. Best to check how udev works first before suggesting changes.

@viq
Copy link

viq commented Jan 2, 2014

I admit ignorance on the subject, all I know is that without this change the card did not mount for me, at the very least on boot.

@manu007
Copy link

manu007 commented Jan 2, 2014

IMHO with the actual script, only one partition will be mounted, if we insert an MSD with more than one partition and different FS types, we will have a lot of problems, because udev will call /usr/sbin/mount-sd.sh for the whole device and for each partition.
We can pass arguments like the partition number, device itself ;
SUBSYSTEM=="block", KERNEL=="mmcblk1*", ACTION=="add", MODE="0660", RUN+="/usr/sbin/mount-sd.sh %k %n %E"
And, with ID_FS_TYPE, check if there something to mount.

@misja
Copy link
Contributor Author

misja commented Jan 3, 2014

@viq Sure you didn't call the script directly?
@manu007 As for the possibility of mounting different partitions, I'll leave that to the nemo maintainers :) Personally I've never seen much use in having multiple partitions on an sdcard ...

@manu007
Copy link

manu007 commented Jan 5, 2014

/etc/udev/rules.d/90

SUBSYSTEM=="block", KERNEL=="mmcblk_", ACTION=="add", MODE="0660", RUN+="/usr/sbin/mount-sd.sh %k %n"
SUBSYSTEM=="block", KERNEL=="mmcblk_", ACTION=="remove", RUN+="/usr/sbin/mount-sd.sh %k %n"

/usr/sbin/mount_sd.h

#!/bin/bash

if [ $# -lt 2 ]; then # No arguments
exit -1
fi

if [ -z "$ID_FS_TYPE" ]; then # No FS to mount
exit 0
fi

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=/run/user/$DEF_UID/media/sdcard$2
SDCARD=/dev/sdcard$2
DEVICE=/dev/$1

if [ "$ACTION" = "add" ]; then # Mount action

if [ ! -d "$MNT" ]; then
    mkdir -p $MNT 
fi

ln -s $DEVICE $SDCARD 

if [ "$ID_FS_TYPE" = "vfat" ]; then                 # crazy FS 
    mount $DEVICE $MNT -o uid=$DEF_UID,gid=$DEF_GID 
else
    mount $DEVICE $MNT  
fi

fi

After inserting a SD card with two partitions (vfat & btrfs) ;

/dev/mmcblk0p4 13459968 56 11334144 1% /run/user/503/media/sdcard4
/dev/mmcblk0p1 2093048 115028 1978020 6% /run/user/503/media/sdcard1

Warning, removing the SD card without umounting previously the fs (btrfs), will hung your device. I think we need a GUI to umount safely all the FS presents in the SD before removing the card.

@foolab
Copy link
Contributor

foolab commented Feb 5, 2014

LGTM

foolab added a commit that referenced this pull request Feb 6, 2014
[sd-utils] Distinguish between fs types when mounting sdcards
@foolab foolab merged commit aac9482 into nemomobile:master Feb 6, 2014
@misja misja deleted the patch-1 branch February 6, 2014 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants