Thinstation / thinstation

A framework for making thin and light Linux based images for x86 based machines and thinclients.
https://www.thinstation.net/
777 stars 188 forks source link

Mounting (automount) MTP devices #760

Closed AlexanderZhirov closed 1 year ago

AlexanderZhirov commented 1 year ago

I'm trying to build a package for mounting MTP devices on version 5.6.

I have compiled the libusb and libmtp libraries of the latest versions. I connect my phone, it is detected and the process freezes on this:

~ # mtp-connect 
libmtp version: 1.1.19

Device 0 (VID=18d1 and PID=4ee1) is a Google Inc Nexus/Pixel (MTP).
Android device detected, assigning default bug flags

Built the mtpfs utility. Also freezes at startup:

~ # mtpfs -o allow_other /root/phone/
Listing raw device(s)
Device 0 (VID=18d1 and PID=4ee1) is a Google Inc Nexus/Pixel (MTP).
   Found 1 device(s):
   Google Inc: Nexus/Pixel (MTP) (18d1:4ee1) @ bus 1, dev 13
Attempting to connect device
Android device detected, assigning default bug flags

The only thing that helped was the aft-mtp-mount utility. But it is problematic to make automatic mounting.

Is it possible to implement a package for such things?

AlexanderZhirov commented 1 year ago

When connecting, the log writes this

Jan 11 14:45:07 (none) user.info kernel: usb 1-3: new high-speed USB device number 18 using ehci-pci
Jan 11 14:45:07 (none) user.info kernel: usb 1-3: New USB device found, idVendor=18d1, idProduct=4ee1
Jan 11 14:45:07 (none) user.info kernel: usb 1-3: New USB device strings: Mfr=1, Product=2, SerialNumber=3
Jan 11 14:45:07 (none) user.info kernel: usb 1-3: Product: Pixel 4a
Jan 11 14:45:07 (none) user.info kernel: usb 1-3: Manufacturer: Google
Jan 11 14:45:07 (none) user.info kernel: usb 1-3: SerialNumber: *********************
Jan 11 14:45:07 (none) user.info mtp-probe: checking bus 1, device 18: "/sys/devices/pci0000:00/0000:00:0f.5/usb1/1-3"
Jan 11 14:45:07 (none) user.info mtp-probe: bus: 1, device: 18 was an MTP device
AlexanderZhirov commented 1 year ago

I collected everything into one archive, libraries and utilities designed to connect mtp devices. Compiled everything with regular gcc in version ts 5.6.

mtp.zip

Doncuppjr commented 1 year ago

Why 5.6 and not 6.2?

Sent from Yahoo Mail for iPhone

On Wednesday, January 11, 2023, 5:58 AM, Alexander Zhirov @.***> wrote:

I collected everything into one archive, libraries and utilities designed to connect mtp devices.

mtp.zip

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>

AlexanderZhirov commented 1 year ago

@Doncuppjr just computers of 32 architectures, but they are suitable for working through the RDP. It is necessary to mount android devices so that files can be copied via RDP. If it works for version 5.6, then it will be possible to do the same for version 6.2.

Thinstation commented 1 year ago

Ports would have been nice.

Thinstation commented 1 year ago

I created the ports and added an android-udev package. This has rules that you could copy and modify to mount stuff.

AlexanderZhirov commented 1 year ago

@Doncuppjr Thank you for such a quick response! I will test everything and write to you.

AlexanderZhirov commented 1 year ago

@Doncuppjr The result is the same as I had.

https://user-images.githubusercontent.com/37277056/211992194-f2d00638-80aa-4ba0-8697-af56d8b087a6.mp4

Works only via aft-mtp-mount. Auto-mounting does not work.

https://user-images.githubusercontent.com/37277056/211991426-4bf8e8c9-da3a-4001-827f-dc9694bcfb1c.mp4

aft-mtp-mount itself somehow detects the connected device and mounts it

Thinstation commented 1 year ago

I added the gvfs-mtp bits

AlexanderZhirov commented 1 year ago

I added the gvfs-mtp bits

If I'm not mistaken, then this is necessary when using the Gnome environment. Most likely, this will not work with the terminal. More precisely, it does not work.

AlexanderZhirov commented 1 year ago

@Doncuppjr I mounted the device via mtpfs, but it took a huge amount of time. Captured the mounting process. It takes more than 10 minutes to mount and read the internal memory of the phone. I have indicated each step in the description of the video. What can cause such problems?

AlexanderZhirov commented 1 year ago

P.S. To mount MTP, i need to load the fuse module, which you always have to load with your hands. Is it possible to install it in the startup somehow?

UPD. I created the /etc/modules file and added fuse there. How correct is this?

Doncuppjr commented 1 year ago

Put it in a text file at /etc/modules

Sent from Yahoo Mail for iPhone

On Friday, January 13, 2023, 3:39 AM, Alexander Zhirov @.***> wrote:

P.S. To mount MTP, i need to load the fuse module, which you always have to load with your hands. Is it possible to install it in the startup somehow?

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

AlexanderZhirov commented 1 year ago

Put it in a text file at /etc/modules

Yes, it turned out to load the module when it was turned on.

I also managed to mount the device manually using simple-mtpfs. Now I want to understand how this can be done automatically? As far as I understand, this can be done via udev. Do I need to write a mount script? And how can this be implemented correctly?

For example, when connected, like a USB flash drive, auto mount the device in /mnt/usbdevice/Phone.

Doncuppjr commented 1 year ago

Look at automount package for example of how to use a udev hook and break out to a shell script. If your mount command is simple and not dynamic, you could use just the udev hook and skip the script.

Sent from Yahoo Mail for iPhone

On Friday, January 13, 2023, 6:33 AM, Alexander Zhirov @.***> wrote:

Put it in a text file at /etc/modules

Yes, it turned out to load the module when it was turned on.

I also managed to mount the device manually using simple-mtpfs. Now I want to understand how this can be done automatically? As far as I understand, this can be done via udev. Do I need to write a mount script? And how can this be implemented correctly?

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

AlexanderZhirov commented 1 year ago

Look at automount package for example of how to use a udev hook and break out to a shell script. If your mount command is simple and not dynamic, you could use just the udev hook and skip the script.

Something like that?

SUBSYSTEM=="usb", SYSFS{idVendor}=="05c6", ATTR{idProduct}=="0001"

ENV{ID_MODEL}=="Swift 2 Plus", ENV{ID_MODEL_ID}=="05c6", ACTION=="add", RUN+="/usr/bin/simple-mtpfs /root/mnt"
ENV{ID_MODEL}=="Swift 2 Plus", ENV{ID_MODEL_ID}=="05c6", ACTION=="remove", RUN+="/bin/umount /root/mnt"
Thinstation commented 1 year ago

Yes

AlexanderZhirov commented 1 year ago

For some reason, it does not work out the connection rule. Maybe I'm not doing something completely?

When the device is connected, everything is identified as needed:

Jan 14 17:04:13 (none) user.info kernel: usb 1-3: new high-speed USB device number 5 using ehci-pci
Jan 14 17:04:13 (none) user.info kernel: usb 1-3: New USB device found, idVendor=05c6, idProduct=0001
Jan 14 17:04:13 (none) user.info kernel: usb 1-3: New USB device strings: Mfr=1, Product=2, SerialNumber=3
Jan 14 17:04:13 (none) user.info kernel: usb 1-3: Product: Swift 2 Plus
Jan 14 17:04:13 (none) user.info kernel: usb 1-3: Manufacturer: Wileyfox
Jan 14 17:04:13 (none) user.info kernel: usb 1-3: SerialNumber: 1c42defa
Jan 14 17:04:13 (none) user.info mtp-probe: checking bus 1, device 5: "/sys/devices/pci0000:00/0000:00:0f.5/usb1/1-3"
Jan 14 17:04:13 (none) user.info mtp-probe: bus: 1, device: 5 was an MTP device

I get this information about the device:

~ # udevadm info -ap "/sys/devices/pci0000:00/0000:00:0f.5/usb1/1-3"

Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.

  looking at device '/devices/pci0000:00/0000:00:0f.5/usb1/1-3':
    KERNEL=="1-3"
    SUBSYSTEM=="usb"
    DRIVER=="usb"
    ATTR{authorized}=="1"
    ATTR{avoid_reset_quirk}=="0"
    ATTR{bConfigurationValue}=="1"
    ATTR{bDeviceClass}=="00"
    ATTR{bDeviceProtocol}=="00"
    ATTR{bDeviceSubClass}=="00"
    ATTR{bMaxPacketSize0}=="64"
    ATTR{bMaxPower}=="500mA"
    ATTR{bNumConfigurations}=="1"
    ATTR{bNumInterfaces}==" 1"
    ATTR{bcdDevice}=="0318"
    ATTR{bmAttributes}=="80"
    ATTR{busnum}=="1"
    ATTR{configuration}==""
    ATTR{devnum}=="5"
    ATTR{devpath}=="3"
    ATTR{devspec}=="  (null)"
    ATTR{idProduct}=="0001"
    ATTR{idVendor}=="05c6"
    ATTR{ltm_capable}=="no"
    ATTR{manufacturer}=="Wileyfox"
    ATTR{maxchild}=="0"
    ATTR{product}=="Swift 2 Plus"
    ATTR{quirks}=="0x0"
    ATTR{removable}=="unknown"
    ATTR{serial}=="1c42defa"
    ATTR{speed}=="480"
    ATTR{urbnum}=="13"
    ATTR{version}==" 2.00"

  looking at parent device '/devices/pci0000:00/0000:00:0f.5/usb1':
    KERNELS=="usb1"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{authorized}=="1"
    ATTRS{authorized_default}=="1"
    ATTRS{avoid_reset_quirk}=="0"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{bDeviceClass}=="09"
    ATTRS{bDeviceProtocol}=="00"
    ATTRS{bDeviceSubClass}=="00"
    ATTRS{bMaxPacketSize0}=="64"
    ATTRS{bMaxPower}=="0mA"
    ATTRS{bNumConfigurations}=="1"
    ATTRS{bNumInterfaces}==" 1"
    ATTRS{bcdDevice}=="0414"
    ATTRS{bmAttributes}=="e0"
    ATTRS{busnum}=="1"
    ATTRS{configuration}==""
    ATTRS{devnum}=="1"
    ATTRS{devpath}=="0"
    ATTRS{devspec}=="  (null)"
    ATTRS{idProduct}=="0002"
    ATTRS{idVendor}=="1d6b"
    ATTRS{interface_authorized_default}=="1"
    ATTRS{ltm_capable}=="no"
    ATTRS{manufacturer}=="Linux 4.14.70TS_SMP ehci_hcd"
    ATTRS{maxchild}=="4"
    ATTRS{product}=="EHCI Host Controller"
    ATTRS{quirks}=="0x0"
    ATTRS{removable}=="unknown"
    ATTRS{serial}=="0000:00:0f.5"
    ATTRS{speed}=="480"
    ATTRS{urbnum}=="101"
    ATTRS{version}==" 2.00"

  looking at parent device '/devices/pci0000:00/0000:00:0f.5':
    KERNELS=="0000:00:0f.5"
    SUBSYSTEMS=="pci"
    DRIVERS=="ehci-pci"
    ATTRS{broken_parity_status}=="0"
    ATTRS{class}=="0x0c0320"
    ATTRS{companion}==""
    ATTRS{consistent_dma_mask_bits}=="32"
    ATTRS{d3cold_allowed}=="1"
    ATTRS{device}=="0x2095"
    ATTRS{devspec}==""
    ATTRS{dma_mask_bits}=="32"
    ATTRS{driver_override}=="(null)"
    ATTRS{enable}=="1"
    ATTRS{irq}=="11"
    ATTRS{local_cpulist}=="0"
    ATTRS{local_cpus}=="1"
    ATTRS{msi_bus}=="1"
    ATTRS{revision}=="0x02"
    ATTRS{subsystem_device}=="0x0000"
    ATTRS{subsystem_vendor}=="0x0000"
    ATTRS{uframe_periodic_max}=="100"
    ATTRS{vendor}=="0x1022"

  looking at parent device '/devices/pci0000:00':
    KERNELS=="pci0000:00"
    SUBSYSTEMS==""
    DRIVERS==""
~ # udevadm info -p "/sys/devices/pci0000:00/0000:00:0f.5/usb1/1-3"
P: /devices/pci0000:00/0000:00:0f.5/usb1/1-3
N: bus/usb/001/005
S: android
S: android3
S: libmtp-1-3
E: BUSNUM=001
E: DEVLINKS=/dev/android3 /dev/android /dev/libmtp-1-3
E: DEVNAME=/dev/bus/usb/001/005
E: DEVNUM=005
E: DEVPATH=/devices/pci0000:00/0000:00:0f.5/usb1/1-3
E: DEVTYPE=usb_device
E: DRIVER=usb
E: ID_BUS=usb
E: ID_MEDIA_PLAYER=1
E: ID_MODEL=Swift_2_Plus
E: ID_MODEL_ENC=Swift\x202\x20Plus
E: ID_MODEL_ID=0001
E: ID_MTP_DEVICE=1
E: ID_REVISION=0318
E: ID_SERIAL=Wileyfox_Swift_2_Plus_1c42defa
E: ID_SERIAL_SHORT=1c42defa
E: ID_USB_INTERFACES=:ffff00:
E: ID_VENDOR=Wileyfox
E: ID_VENDOR_ENC=Wileyfox
E: ID_VENDOR_ID=05c6
E: MAJOR=189
E: MINOR=4
E: PRODUCT=5c6/1/318
E: SUBSYSTEM=usb
E: TAGS=:uaccess:
E: TYPE=0/0/0
E: USEC_INITIALIZED=472334474
E: adb_user=yes

Created this file /etc/udev/rules.d/50-phone.rules with the following data:

SUBSYSTEM=="usb", ATTR{idVendor}=="05c6", ATTR{idProduct}=="0001", ACTION=="add", RUN+="/usr/bin/simple-mtpfs /root/mnt"
SUBSYSTEM=="usb", ATTR{idVendor}=="05c6", ATTR{idProduct}=="0001", ACTION=="remove", RUN+="/bin/umount /root/mnt
AlexanderZhirov commented 1 year ago

@Doncuppjr With the help of udevadm monitor --property, I managed to correctly build commands that respond to connecting and disconnecting the device. Now the mounting works.

SUBSYSTEM=="usb", ATTRS{idVendor}=="05c6", ATTRS{idProduct}=="0001", ACTION=="add", RUN+="/usr/bin/simple-mtpfs /root/mnt"
SUBSYSTEM=="usb", ENV{ID_MODEL}=="Swift_2_Plus", ACTION=="remove", RUN+="/bin/umount /root/mnt"

It remains to put all this into the script and implement the folder structure for the devices that will be mounted when connected. Soon I will share my achievements, maybe they can be applied to the project. Maybe it will also be useful to someone.

AlexanderZhirov commented 1 year ago

I managed to implement the connection of MTP devices using a script with minor modifications:

diff --git a/etc/udev/rules.d/51-android.rules b/etc/udev/rules.d/51-android.rules
index 36f8644..d34caf5 100644
--- a/etc/udev/rules.d/51-android.rules
+++ b/etc/udev/rules.d/51-android.rules
@@ -9,11 +9,14 @@
 # https://github.com/M0Rf30/android-udev-rules

 # Skip testing for android devices if device is not add, or usb
-ACTION!="add", ACTION!="bind", GOTO="android_usb_rules_end"
+# ACTION!="add", ACTION!="bind", GOTO="android_usb_rules_end"
 SUBSYSTEM!="usb", GOTO="android_usb_rules_end"

 # Skip testing for unexpected devices like hubs, controllers or printers
 ATTR{bDeviceClass}=="09", GOTO="android_usb_rules_end"
+ENV{ID_MODEL}=="", GOTO="android_usb_rules_end"
+ENV{ID_MODEL}=="Mass_Storage_Device", GOTO="android_usb_rules_end"
+ENV{ID_MODEL}=="USB_DISK", GOTO="android_usb_rules_end"
 ENV{ID_MODEL}=="Controller", GOTO="android_usb_rules_end"
 ENV{ID_USB_INTERFACES}=="*:0701??:*", ENV{adb_adb}="yes", GOTO="android_usb_rules_end"

@@ -552,6 +555,8 @@ ATTR{idProduct}=="9011", SYMLINK+="android_adb"
 ATTR{idProduct}=="f003", SYMLINK+="android_adb"
 #   Yongnuo YN450m (identified in lsusb as Intex Aqua Fish & Jolla C Diagnostic Mode)
 ATTR{idProduct}=="9091", SYMLINK+="android_adb"
+#   Wileyfox Swift 2 Plus
+ATTR{idProduct}=="0001"
 GOTO="android_usb_rule_match"
 LABEL="not_Qualcomm"

@@ -825,6 +830,7 @@ ATTR{idVendor}=="11ca", ENV{adb_user}="yes"

 # Skip other vendor tests
 LABEL="android_usb_rule_match"
+RUN+="/etc/udev/scripts/mtp.sh"

 # Symlink shortcuts to reduce code in tests above
 ENV{adb_adbfast}=="yes", ENV{adb_adb}="yes", ENV{adb_fast}="yes"

And the mounting script turned out to be such /etc/udev/scripts/mtp.sh:

#! /bin/sh

. /etc/thinstation.env
. $TS_GLOBAL

currentDevice=$BASE_MOUNT_PATH/$USB_MOUNT_DIR/$ID_VENDOR\_$ID_MODEL

_mounted() {
    if [ -n "$(grep -oe "$1" /proc/mounts)" ]; then
        return 0
    else
        return 1
    fi
}

_mount() {
    logger "mount $ID_VENDOR"
    if [ -d $currentDevice ] && _mounted $currentDevice; then
        exit 1
    fi
    if [ ! -d $currentDevice ]; then
        mkdir $currentDevice
        if is_enabled "$USB_STORAGE_SYNC" && [ ! -n "$(echo $USB_MOUNT_OPTIONS | grep -e sync)" ]; then
            USB_MOUNT_OPTIONS=$USB_MOUNT_OPTIONS,sync
        fi
        /bin/simple-mtpfs -o $USB_MOUNT_OPTIONS $currentDevice
        if ! _mounted $currentDevice; then
            rm -r $currentDevice
        else
            logger "mounted $ID_VENDOR in $currentDevice"
        fi
    fi
}

_umount() {
    logger "unmount $ID_VENDOR"
    if [ -d $currentDevice ]; then
        while _mounted $currentDevice; do
            umount $currentDevice
        done
        logger "unmounted $ID_VENDOR in $currentDevice"
        rm -r $currentDevice
    fi
}

if [ -v $ACTION ]; then
    exit 0
fi

if [ $ACTION == "add" ]; then
    _mount
elif [ $ACTION == "remove" ]; then
    _umount
fi

exit 0
Thinstation commented 1 year ago

Why modify 51-android.rules? Can't you just make a 99-mtp.rules that has the needed bits?

AlexanderZhirov commented 1 year ago

Why modify 51-android.rules? Can't you just make a 99-mtp.rules that has the needed bits?

I just had to add my device for identification and + I removed the possibility of re-working the script, since udev runs roles for some reason twice. In fact, I only made these edits.

AlexanderZhirov commented 1 year ago

In TS 6.2, when the "remove" event occurs, udev does not recognize some device properties, although they are present when mounting.

KERNEL[5440.719559] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.2/1-3.2:1.0 (usb)
ACTION=remove
DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.2/1-3.2:1.0
SUBSYSTEM=usb
DEVTYPE=usb_interface
PRODUCT=2717/ff40/223
TYPE=0/0/0
INTERFACE=255/255/0
MODALIAS=usb:v2717pFF40d0223dc00dsc00dp00icFFiscFFip00in00
SEQNUM=2473

KERNEL[5440.720452] unbind   /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.2 (usb)
ACTION=unbind
DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.2
SUBSYSTEM=usb
DEVNAME=/dev/bus/usb/001/098
DEVTYPE=usb_device
PRODUCT=2717/ff40/223
TYPE=0/0/0
BUSNUM=001
DEVNUM=098
SEQNUM=2474
MAJOR=189
MINOR=97

KERNEL[5440.720665] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.2 (usb)
ACTION=remove
DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.2
SUBSYSTEM=usb
DEVNAME=/dev/bus/usb/001/098
DEVTYPE=usb_device
PRODUCT=2717/ff40/223
TYPE=0/0/0
BUSNUM=001
DEVNUM=098
SEQNUM=2475
MAJOR=189
MINOR=97

UDEV  [5440.914560] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.2/1-3.2:1.0 (usb)
ACTION=remove
DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.2/1-3.2:1.0
SUBSYSTEM=usb
DEVTYPE=usb_interface
PRODUCT=2717/ff40/223
TYPE=0/0/0
INTERFACE=255/255/0
MODALIAS=usb:v2717pFF40d0223dc00dsc00dp00icFFiscFFip00in00
SEQNUM=2473
USEC_INITIALIZED=5433050990
ID_VENDOR_FROM_DATABASE=Xiaomi Inc.
ID_MODEL_FROM_DATABASE=Mi/Redmi series (MTP)
ID_PATH=pci-0000:00:14.0-usb-0:3.2:1.0
ID_PATH_TAG=pci-0000_00_14_0-usb-0_3_2_1_0

KERNEL[5440.996828] add      /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.2 (usb)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.2
SUBSYSTEM=usb
DEVNAME=/dev/bus/usb/001/099
DEVTYPE=usb_device
PRODUCT=2717/ff40/223
TYPE=0/0/0
BUSNUM=001
DEVNUM=099
SEQNUM=2476
MAJOR=189
MINOR=98

KERNEL[5441.000344] add      /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.2/1-3.2:1.0 (usb)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.2/1-3.2:1.0
SUBSYSTEM=usb
DEVTYPE=usb_interface
PRODUCT=2717/ff40/223
TYPE=0/0/0
INTERFACE=255/255/0
MODALIAS=usb:v2717pFF40d0223dc00dsc00dp00icFFiscFFip00in00
SEQNUM=2477

KERNEL[5441.000456] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.2 (usb)
ACTION=bind
DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.2
SUBSYSTEM=usb
DEVNAME=/dev/bus/usb/001/099
DEVTYPE=usb_device
DRIVER=usb
PRODUCT=2717/ff40/223
TYPE=0/0/0
BUSNUM=001
DEVNUM=099
SEQNUM=2478
MAJOR=189
MINOR=98

UDEV  [5441.086179] unbind   /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.2 (usb)
ACTION=unbind
DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.2
SUBSYSTEM=usb
DEVNAME=/dev/bus/usb/001/098
DEVTYPE=usb_device
PRODUCT=2717/ff40/223
TYPE=0/0/0
BUSNUM=001
DEVNUM=098
SEQNUM=2474
USEC_INITIALIZED=5432832949
ID_PATH=pci-0000:00:14.0-usb-0:3.2
ID_PATH_TAG=pci-0000_00_14_0-usb-0_3_2
MAJOR=189
MINOR=97

UDEV  [5441.279093] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.2 (usb)
ACTION=remove
DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.2
SUBSYSTEM=usb
DEVNAME=/dev/bus/usb/001/098
DEVTYPE=usb_device
PRODUCT=2717/ff40/223
TYPE=0/0/0
BUSNUM=001
DEVNUM=098
SEQNUM=2475
USEC_INITIALIZED=5432832949
ID_PATH=pci-0000:00:14.0-usb-0:3.2
ID_PATH_TAG=pci-0000_00_14_0-usb-0_3_2
adb_user=yes
MAJOR=189
MINOR=97
DEVLINKS=/dev/android /dev/android2
TAGS=:uaccess:

UDEV  [5441.499605] add      /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.2 (usb)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.2
SUBSYSTEM=usb
DEVNAME=/dev/bus/usb/001/099
DEVTYPE=usb_device
PRODUCT=2717/ff40/223
TYPE=0/0/0
BUSNUM=001
DEVNUM=099
SEQNUM=2476
USEC_INITIALIZED=5441281617
ID_VENDOR=Xiaomi
ID_VENDOR_ENC=Xiaomi
ID_VENDOR_ID=2717
ID_MODEL=Redmi_9A
ID_MODEL_ENC=Redmi\x209A
ID_MODEL_ID=ff40
ID_REVISION=0223
ID_SERIAL=Xiaomi_Redmi_9A_XWIJ4HOBPJTSEQNB
ID_SERIAL_SHORT=XWIJ4HOBPJTSEQNB
ID_BUS=usb
ID_USB_INTERFACES=:ffff00:
ID_VENDOR_FROM_DATABASE=Xiaomi Inc.
ID_MODEL_FROM_DATABASE=Mi/Redmi series (MTP)
adb_user=yes
ID_PATH=pci-0000:00:14.0-usb-0:3.2
ID_PATH_TAG=pci-0000_00_14_0-usb-0_3_2
ID_FOR_SEAT=usb-pci-0000_00_14_0-usb-0_3_2
MAJOR=189
MINOR=98
DEVLINKS=/dev/android_adb /dev/android /dev/android2
TAGS=:seat:uaccess:

UDEV  [5441.716011] add      /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.2/1-3.2:1.0 (usb)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.2/1-3.2:1.0
SUBSYSTEM=usb
DEVTYPE=usb_interface
PRODUCT=2717/ff40/223
TYPE=0/0/0
INTERFACE=255/255/0
MODALIAS=usb:v2717pFF40d0223dc00dsc00dp00icFFiscFFip00in00
SEQNUM=2477
USEC_INITIALIZED=5441501441
ID_VENDOR_FROM_DATABASE=Xiaomi Inc.
ID_MODEL_FROM_DATABASE=Mi/Redmi series (MTP)
ID_PATH=pci-0000:00:14.0-usb-0:3.2:1.0
ID_PATH_TAG=pci-0000_00_14_0-usb-0_3_2_1_0

UDEV  [5441.891958] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.2 (usb)
ACTION=bind
DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.2
SUBSYSTEM=usb
DEVNAME=/dev/bus/usb/001/099
DEVTYPE=usb_device
DRIVER=usb
PRODUCT=2717/ff40/223
TYPE=0/0/0
BUSNUM=001
DEVNUM=099
SEQNUM=2478
USEC_INITIALIZED=5441281617
ID_VENDOR=Xiaomi
ID_VENDOR_ENC=Xiaomi
ID_VENDOR_ID=2717
ID_MODEL=Redmi_9A
ID_MODEL_ENC=Redmi\x209A
ID_MODEL_ID=ff40
ID_REVISION=0223
ID_SERIAL=Xiaomi_Redmi_9A_XWIJ4HOBPJTSEQNB
ID_SERIAL_SHORT=XWIJ4HOBPJTSEQNB
ID_BUS=usb
ID_USB_INTERFACES=:ffff00:
ID_VENDOR_FROM_DATABASE=Xiaomi Inc.
ID_MODEL_FROM_DATABASE=Mi/Redmi series (MTP)
adb_user=yes
ID_PATH=pci-0000:00:14.0-usb-0:3.2
ID_PATH_TAG=pci-0000_00_14_0-usb-0_3_2
ID_FOR_SEAT=usb-pci-0000_00_14_0-usb-0_3_2
MAJOR=189
MINOR=98
DEVLINKS=/dev/android_adb /dev/android /dev/android2
TAGS=:seat:uaccess:

The same device is recognized on 5.6. Although the udev version is 231 and the device data is recognized at the "remove" event.

Thus, it is not possible to determine the ID_MODEL of the device. Maybe it's worth rebuilding systemd?

AlexanderZhirov commented 1 year ago

Thus, it is not possible to determine the ID_MODEL of the device. Maybe it's worth rebuilding systemd?

No, it's not related to systemd. I'll try to test it with Ubuntu Kernel now.

AlexanderZhirov commented 1 year ago

With the Ubuntu kernel, there is no USB support at all on the computer under test.

Could there be a problem that the kernel does not have full support for device identification? Because when disconnecting on core 4, there is information when disconnecting the device, and the current core does not have complete information. By the value of the ID_MODEL variable, I received information about the device.

Thinstation commented 1 year ago

Try allmodules on the ubuntu kernel. We don't make the same choices.

AlexanderZhirov commented 1 year ago

Strange. I collected both with the attached kernel and with Ubuntu with all modules, anyway, there is no data about the device for the "remove" event. I tried different phones. But in TS 5.6 for some reason udev sees them.

I don't even have an idea what this is related to.

AlexanderZhirov commented 1 year ago

I tried to do the same on OS Solus 6.1.5-229 with the udev 251 version - the same result as on TS 6.2. Either it was in the linux kernel > 5 v. that some edits were made to respond to device disconnections, or the systemd system changed the code in new versions. I don't understand anything.

AlexanderZhirov commented 1 year ago

Here is the result for events on TS 5.6 kernel 4.14.70 udev 231

KERNEL[166.735616] add      /devices/pci0000:00/0000:00:0f.5/usb1/1-3 (usb)
ACTION=add
BUSNUM=001
DEVNAME=/dev/bus/usb/001/003
DEVNUM=003
DEVPATH=/devices/pci0000:00/0000:00:0f.5/usb1/1-3
DEVTYPE=usb_device
MAJOR=189
MINOR=2
PRODUCT=5c6/1/318
SEQNUM=1436
SUBSYSTEM=usb
TYPE=0/0/0

KERNEL[166.743985] add      /devices/pci0000:00/0000:00:0f.5/usb1/1-3/1-3:1.0 (usb)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:0f.5/usb1/1-3/1-3:1.0
DEVTYPE=usb_interface
INTERFACE=255/255/0
MODALIAS=usb:v05C6p0001d0318dc00dsc00dp00icFFiscFFip00in00
PRODUCT=5c6/1/318
SEQNUM=1437
SUBSYSTEM=usb
TYPE=0/0/0

KERNEL[166.981458] add      /devices/virtual/bdi/0:20 (bdi)
ACTION=add
DEVPATH=/devices/virtual/bdi/0:20
SEQNUM=1440
SUBSYSTEM=bdi

UDEV  [166.993620] add      /devices/virtual/bdi/0:20 (bdi)
ACTION=add
DEVPATH=/devices/virtual/bdi/0:20
SEQNUM=1440
SUBSYSTEM=bdi
USEC_INITIALIZED=166993200

UDEV  [167.015251] add      /devices/pci0000:00/0000:00:0f.5/usb1/1-3 (usb)
ACTION=add
BUSNUM=001
DEVLINKS=/dev/android /dev/android3
DEVNAME=/dev/bus/usb/001/003
DEVNUM=003
DEVPATH=/devices/pci0000:00/0000:00:0f.5/usb1/1-3
DEVTYPE=usb_device
ID_BUS=usb
ID_MODEL=Swift_2_Plus
ID_MODEL_ENC=Swift\x202\x20Plus
ID_MODEL_ID=0001
ID_REVISION=0318
ID_SERIAL=Wileyfox_Swift_2_Plus_1c42defa
ID_SERIAL_SHORT=1c42defa
ID_USB_INTERFACES=:ffff00:
ID_VENDOR=Wileyfox
ID_VENDOR_ENC=Wileyfox
ID_VENDOR_ID=05c6
MAJOR=189
MINOR=2
PRODUCT=5c6/1/318
SEQNUM=1436
SUBSYSTEM=usb
TAGS=:uaccess:
TYPE=0/0/0
USEC_INITIALIZED=166749862
adb_user=yes

UDEV  [167.020189] add      /devices/pci0000:00/0000:00:0f.5/usb1/1-3/1-3:1.0 (usb)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:0f.5/usb1/1-3/1-3:1.0
DEVTYPE=usb_interface
INTERFACE=255/255/0
MODALIAS=usb:v05C6p0001d0318dc00dsc00dp00icFFiscFFip00in00
PRODUCT=5c6/1/318
SEQNUM=1437
SUBSYSTEM=usb
TYPE=0/0/0
USEC_INITIALIZED=167018789

KERNEL[171.884452] remove   /devices/pci0000:00/0000:00:0f.5/usb1/1-3/1-3:1.0 (usb)
ACTION=remove
DEVPATH=/devices/pci0000:00/0000:00:0f.5/usb1/1-3/1-3:1.0
DEVTYPE=usb_interface
INTERFACE=255/255/0
MODALIAS=usb:v05C6p0001d0318dc00dsc00dp00icFFiscFFip00in00
PRODUCT=5c6/1/318
SEQNUM=1442
SUBSYSTEM=usb
TYPE=0/0/0

KERNEL[171.888589] remove   /devices/pci0000:00/0000:00:0f.5/usb1/1-3 (usb)
ACTION=remove
BUSNUM=001
DEVNAME=/dev/bus/usb/001/003
DEVNUM=003
DEVPATH=/devices/pci0000:00/0000:00:0f.5/usb1/1-3
DEVTYPE=usb_device
MAJOR=189
MINOR=2
PRODUCT=5c6/1/318
SEQNUM=1444
SUBSYSTEM=usb
TYPE=0/0/0

UDEV  [171.896356] remove   /devices/pci0000:00/0000:00:0f.5/usb1/1-3/1-3:1.0 (usb)
ACTION=remove
DEVPATH=/devices/pci0000:00/0000:00:0f.5/usb1/1-3/1-3:1.0
DEVTYPE=usb_interface
INTERFACE=255/255/0
MODALIAS=usb:v05C6p0001d0318dc00dsc00dp00icFFiscFFip00in00
PRODUCT=5c6/1/318
SEQNUM=1442
SUBSYSTEM=usb
TYPE=0/0/0
USEC_INITIALIZED=171885181

KERNEL[172.067605] remove   /devices/virtual/bdi/0:20 (bdi)
ACTION=remove
DEVPATH=/devices/virtual/bdi/0:20
SEQNUM=1445
SUBSYSTEM=bdi

UDEV  [172.076637] remove   /devices/virtual/bdi/0:20 (bdi)
ACTION=remove
DEVPATH=/devices/virtual/bdi/0:20
SEQNUM=1445
SUBSYSTEM=bdi
USEC_INITIALIZED=172068525

UDEV  [172.104393] remove   /devices/pci0000:00/0000:00:0f.5/usb1/1-3 (usb)
ACTION=remove
BUSNUM=001
DEVLINKS=/dev/android /dev/android3
DEVNAME=/dev/bus/usb/001/003
DEVNUM=003
DEVPATH=/devices/pci0000:00/0000:00:0f.5/usb1/1-3
DEVTYPE=usb_device
ID_BUS=usb
ID_MODEL=Swift_2_Plus
ID_MODEL_ENC=Swift\x202\x20Plus
ID_MODEL_ID=0001
ID_REVISION=0318
ID_SERIAL=Wileyfox_Swift_2_Plus_1c42defa
ID_SERIAL_SHORT=1c42defa
ID_USB_INTERFACES=:ffff00:
ID_VENDOR=Wileyfox
ID_VENDOR_ENC=Wileyfox
ID_VENDOR_ID=05c6
MAJOR=189
MINOR=2
PRODUCT=5c6/1/318
SEQNUM=1444
SUBSYSTEM=usb
TAGS=:uaccess:
TYPE=0/0/0
USEC_INITIALIZED=166749862
adb_user=yes
AlexanderZhirov commented 1 year ago

I come to the conclusion that, most likely, there is some kind of event post-processing process that collects information about the device. Probably I will have to cling to other identification data for mounting/unmounting.

AlexanderZhirov commented 1 year ago

It was possible to identify devices by a unique parameter PRODUCT, both at the "add" event and at the "remove" event:

PRODUCT=18d1/4ee1/440

as

ID_VENDOR_ID=18d1
ID_MODEL_ID=4ee1
ID_REVISION=0440

or

PRODUCT=5c6/1/318

as

ID_VENDOR_ID=05c6
ID_MODEL_ID=0001
ID_REVISION=0318
AlexanderZhirov commented 1 year ago

I managed to solve the unmounting problem using IMPORT{db}="ID_MODEL", IMPORT{db}="ID_VENDOR". Now, with the "remove" event, I can access data from the variables ID_MODEL and ID_VENDOR.

But now a different problem has arisen. I can't explain it even to myself.

For testing, I've simplified the mount script a bit. Now it looks like this

#!/bin/sh

. /etc/thinstation.env
. $TS_GLOBAL

if [[ $ACTION == "add" ]]; then
    echo "********************* START MOUNT *******************" | systemd-cat -p err -t "mtp"
    aft-mtp-mount /phone
    grep -oe "/phone" /proc/mounts | systemd-cat -p err -t "mtp"
    ls "/phone/Внутренний общий накопитель" | systemd-cat -p err -t "mtp"
    echo "********************** END MOUNT ********************" | systemd-cat -p err -t "mtp"
elif [[ $ACTION == "remove" ]]; then
    echo "******************** START UNMOUNT ******************" | systemd-cat -p err -t "mtp"
    umount /phone
    echo "********************* END UNMOUNT *******************" | systemd-cat -p err -t "mtp"
fi

When the "add" or "bind" event occurs, my device is mounted. At the time of execution of the script, this mounting is carried out, as can be evidenced by the command ls. But when the script works out, the mounting disappears somewhere. I captured it all on video.

At the same time, if I mount the device manually, then it is mounted and I can view it.

The other triggers are missing for this processing. I don't understand why this is happening. Can you explain it to me?

AlexanderZhirov commented 1 year ago

The other triggers are missing for this processing. I don't understand why this is happening. Can you explain it to me?

@Doncuppjr This is either a bug or an error. When I run systemd-udevd as a daemon, the mounting works. But if the service works as part of the systemd service, then mounting does not work.

https://user-images.githubusercontent.com/37277056/214816015-569a6106-de3d-4e47-b838-f73753b2810b.mp4

Doncuppjr commented 1 year ago

Systemd blocks some commands from running during init. Very annoying. Can not call mount, have to call systemd-mount.

On Thursday, January 26, 2023 at 02:44:57 AM PST, Alexander Zhirov @.***> wrote:

   The other triggers are missing for this processing. I don't understand why this is happening. Can you explain it to me?

@Doncuppjr This is either a bug or an error. When I run systemd-udevd as a daemon, the mounting works. But if the service works as part of the systemd service, then mounting does not work.

https://user-images.githubusercontent.com/37277056/214816015-569a6106-de3d-4e47-b838-f73753b2810b.mp4

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

AlexanderZhirov commented 1 year ago

@Doncuppjr I managed to solve the mounting problem.

I created a system service /etc/systemd/system/mtp@.service:

[Unit]
Description=Mounting MTP devices

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/etc/udev/scripts/mtp.sh add %I
ExecStop=/etc/udev/scripts/mtp.sh remove %I

The script that is called when the service starts/stops /etc/udev/sripts/mtp.sh:

#! /bin/sh

. /etc/thinstation.env
. $TS_GLOBAL

ACTION=$1
DEVICE_NAME=$2
mountUtility=/bin/aft-mtp-mount

currentDevice=$BASE_MOUNT_PATH/$USB_MOUNT_DIR/$DEVICE_NAME

_mounted() {
    if [ -n "$(grep -oe "$1" /proc/mounts)" ]; then
        return 0
    else
        return 1
    fi
}

_mount() {
    echo "mount $DEVICE_NAME" | systemd-cat -p info -t "mtp"
    if [ -d $currentDevice ] && _mounted $currentDevice; then
        echo "$DEVICE_NAME already mounted" | systemd-cat -p warning -t "mtp"
        exit 1
    fi
    if [ ! -d $currentDevice ]; then
        mkdir $currentDevice
        if is_enabled "$USB_STORAGE_SYNC" && [ ! -n "$(echo $USB_MOUNT_OPTIONS | grep -e sync)" ]; then
            USB_MOUNT_OPTIONS=$USB_MOUNT_OPTIONS,sync
        fi
        $mountUtility -o $USB_MOUNT_OPTIONS $currentDevice
        if _mounted $currentDevice && [ "$(ls -A $currentDevice)" ]; then
            echo "mounted $DEVICE_NAME in $currentDevice" | systemd-cat -p info -t "mtp"
        else
            echo "$DEVICE_NAME failed to mount" | systemd-cat -p warning -t "mtp"
            _umount
            exit 2
        fi
    else
        echo "$currentDevice already exists" | systemd-cat -p warning -t "mtp"
        exit 3
    fi
}

_umount() {
    echo "unmount $DEVICE_NAME" | systemd-cat -p info -t "mtp"
    if [[ -d $currentDevice ]]; then
        while _mounted $currentDevice; do
            umount $currentDevice
        done
        echo "unmounted $DEVICE_NAME in $currentDevice" | systemd-cat -p info -t "mtp"
        rm -r $currentDevice
    else
        echo "$DEVICE_NAME was not mounted" | systemd-cat -p warning -t "mtp"
    fi
}

if [ $ACTION == "add" ]; then
    _mount
elif [ $ACTION == "remove" ]; then
    _umount
fi

exit 0

And this file with a description of the rules /etc/udev/rules.d/51-android.rules with diff:

diff --git a/51-android.rules b/51-android.rules
index d75ddb3..3296025 100644
--- a/51-android.rules
+++ b/51-android.rules
@@ -8,9 +8,11 @@
 # Latest version can be found at: 
 # https://github.com/M0Rf30/android-udev-rules

-# Skip testing for android devices if device is not add, or usb
-ACTION!="add", ACTION!="bind", GOTO="android_usb_rules_end"
+IMPORT{db}="ID_VENDOR", IMPORT{db}="ID_MODEL", IMPORT{db}="ID_REVISION", IMPORT{db}="{idVendor}", IMPORT{db}="{idProduct}"
+
+# Skip testing for android devices if device is not usb, or usb_device
 SUBSYSTEM!="usb", GOTO="android_usb_rules_end"
+ENV{DEVTYPE}!="usb_device", GOTO="android_usb_rules_end"

 # Skip testing for unexpected devices like hubs, controllers or printers
 ATTR{bDeviceClass}=="09", GOTO="android_usb_rules_end"
@@ -827,6 +829,8 @@ ATTR{idVendor}=="11ca", ENV{adb_user}="yes"

 # Skip other vendor tests
 LABEL="android_usb_rule_match"
+ACTION=="add", RUN+="/bin/systemctl start mtp@$env{ID_VENDOR}_$env{ID_MODEL}_$env{ID_REVISION}.service"
+ACTION=="remove", RUN+="/bin/systemctl stop mtp@$env{ID_VENDOR}_$env{ID_MODEL}_$env{ID_REVISION}.service"

 # Symlink shortcuts to reduce code in tests above
 ENV{adb_adbfast}=="yes", ENV{adb_adb}="yes", ENV{adb_fast}="yes"

And now, after connecting my device, the mount takes place.

But there is one problem. It feels like the mount service starts when the computer boots and blocks something. Now, when i turn on the PC and load the TS, rules 51-android.rules are triggered and the distribution refuses to load. Nothing happens on the screen. But if I comment on the service call mtp@.service in the rules, everything is loaded as usual.

I am at an impasse, I ask for your help in resolving this issue.

UPD.

For more information. While I was solving the problem with mounting, I came across such posts:

  1. Auto mount usb drive from udev rules and shell script
  2. Udev rule to mount disk does not work
  3. PrivateMounts=

I tried to do it through PrivateMounts=false - it still didn't help. And in the end, I came to the solution that I described above.

kernel.log

AlexanderZhirov commented 1 year ago

@Doncuppjr I managed to get the download process

boot

AlexanderZhirov commented 1 year ago

Here are the full load details

boot kernel.log

AlexanderZhirov commented 1 year ago

I managed to solve the problem in this way. I uploaded my work here.

[Unit]
Description=Mounting MTP devices
BindsTo=dev-%i.device
After=dev-%i.device

[Service]
Type=oneshot
RemainAfterExit=true
TimeoutStartSec=30
ExecStart=/etc/udev/scripts/mtp.sh add %I
ExecStop=/etc/udev/scripts/mtp.sh remove %I

[Install]
WantedBy=dev-%i.device