Automatically mount Nexus 7 in MTP mode in Ubuntu

Source: Internet
Author: User
Tags install go

Android devices use USB cables to connect to PCs in two modes: PTP and MTP.

In PTP mode, the Android device is regarded as a digital camera, which can already work properly on Ubuntu 12.04. But only the dcim and picture folders on the device can be accessed.


In MTP mode, Android devices are accessed as multimedia devices, so they can access all file systems in the user space. However, this mode is not supported in Ubuntu 12.04, some steps are required for the configuration to work properly.

The following tutorial uses Nexus 7 as an example to enable your computer (ubuntu12.04) to use the USB port to access Nexus 7 as a large-capacity storage device, the device is automatically mounted and removed.

1. Install MTP fuse (filesystem
In userspace (fuse ))

1.1 select fuse Extension

Many fuse extensions can be used to mount MTP devices on your Ubuntu system:

Original mtpfs
The project is created on the libmtp library.

Jmtpfs, which is the rewriting of mtpfs

Go-mtpfs, a new type of MTP with some restrictions


Why I chose go-mtpfs:

1. more stable than others

2. Connect and Change sub-Directories

3. Fast File Transfer

4. The temporary space used for file transfer is small.

5. The file name can be changed directly.

But there are also some restrictions:

1. Files larger than 2 GB cannot be transmitted

2. The file timestamp cannot be saved.

3. The file system attached to another account cannot be used.

1.2 configure Fuse

1. Change the attributes of the/etc/fuse. conf file: # sudo chmod A + R/etc/fuse. conf

2. Modify the file content: uncomment The Line "user_allow_other"

The purpose is to allow other users on this computer to mount the file system.

1.3 configure and install go-mtofs

Go-mtpfs uses Google's go language compiler, git repository in: https://github.com/hanwen/go-mtpfs.

Get the code locally, and then install the go compiler first:

sudo apt-get install golang-go

Install the libmtp header file

sudo apt-get install libmtp-dev

Run sudo go install in the go-mtpfs source code directory to generate the go-mtpfs binary file, and copy go-mtpfs to/usr/loacal/sbin/go-mtpfs to complete installation.

You can also directly download the binary file go-mtpfs, http://hanwen.home.xs4all.nl/public/software/go-mtpfs/

1.4 create a mount point

# Sudo mkdir/Media/nexus7
# Sudo chmod 777/Media/nexus7

2. Create udev rules

2.1 check vendor_id and product_id

To obtain permissions to access Nexus 7 in any mode, you must declare all udev rules in these modes.

There are several modes:

  • Recovery mode
  • Fastboot Mode
  • PTP mode (with USB debug ON or OFF)
  • MTP mode (with USB debug ON or OFF)

In each mode, run the lsusb command to view the vendor_id and product_id of nexus7:

# Lsusb

Recovery mode (Power + volume): Bus 002 device 022: Id 18d1: d001 Google Inc. (select ADB Mode)

Fastboot mode: Bus 002 device 021: Id 18d1: 4e40 Google Inc.

PTP mode (with usb deug on): Bus 002 device 024: Id 18d1: 4e44 Google Inc.

PTP mode (with usb deug off): Bus 002 device 025: Id 18d1: 4e43 Google Inc.

MTP mode (with usb deug on): Bus 002 device 027: Id 18d1: 4e42 Google Inc.

MTP mode (with usb deug off): Bus 002 device 028: Id 18d1: 4e41 Google Inc.

Above, vendor_id is marked in blue, and product_id is marked in red. You need to write these rules into the file/etc/udev/rules. d/51-android.rules (see the final section ). 51-android.rules: This file was created when we installed the ADB driver. Why is this file named?

Udev is the Device Manager in Linux and supports plug-and-play. It allows you to specify a series of response rules and execute them when detecting new devices joining.

Udev requires that these rules be stored in/etc/udev/rules. d/50-udev.rules. Udev sorts the rules in these files sequentially. If you want to add rules after the default rules, the file name must be greater than 50, for example, 51

2.2 check model and model_id

This step is used to detect the insertion and removal of Nexus 7, that is, when your nexus7 is connected to a computer in MTP mode, the MTP file system is automatically mounted. When you pull out, automatically uninstall the file system.

These udev rules must be created on the device's model and model_id to work properly.

Follow these steps to check the model and model_id:

1. Run the udev monitoring command: udevadm monitor -- Environment -- udev

2. Use your nexus7 MTP mode to connect to your computer

3. wait several seconds

4. Pull out

Gexueyuan @ gexueyuan :~ $ Udevadm monitor -- Environment -- udev
Monitor will print the specified ed events:
Udev-the event which udev sends out after Rule Processing

Udev [23855.884828] Add/devices/pci0000: 00/0000: 00: 1d. 7/usb2/2-4 (USB)
Action = add
Busnum = 002
Devlinks =/dev/libmtp-2-4
Devname =/dev/bus/USB/002/030
Devnum = 030
Devpath =/devices/pci0000: 00/0000: 00: 1d. 7/usb2/2-4
Devtype = usb_device
Id_bus = USB
Id_media_player = 1
Id_model = nexus_7
Id_model_enc = nexus \ x207
Id_model_id = 4e41
Id_mtp_device = 1
Id_revision = 9999.
Id_serial = asus_nexus_7_015d15b4fe2c0200
Id_serial_short = 015d15b4fe2c0200
Id_usb_interfaces =: FFFF00:
Id_vendor = ASUS
Id_vendor_enc = ASUS
Id_vendor_id = 18d1
Major = 189
Minor= 157
Product = 18 D1/4e41/9999

..................

Udev [23993.092275] Remove/devices/pci0000: 00/0000: 00: 1d. 7/usb2/2-4 (USB)
Action = remove
Busnum = 002
Devlinks =/dev/libmtp-2-4
Devname =/dev/bus/USB/002/030
Devnum = 030
Devpath =/devices/pci0000: 00/0000: 00: 1d. 7/usb2/2-4
Devtype = usb_device
Id_bus = USB
Id_media_player = 1
Id_model = nexus_7
Id_model_enc = nexus \ x207
Id_model_id = 4e41
Id_mtp_device = 1
Id_revision = 9999.
Id_serial = asus_nexus_7_015d15b4fe2c0200
..................

Now we get the data we want. In fact, id_model is different in different Android versions.

In jelly bean4.1.2, It is nexus.

In jelly bean4.2 +, the version is nexus_7.

Run in different modes and get different id_model_id, which corresponds to the previous one. In MTP mode, if USB debug is switched off, It is 4e41, and the opposite is 4e42.

Now let's write the file 51-android.rules:

# Bootloader/fastboot Nexus 7
Subsystem = "USB", ATTR {idvendor} = "18d1", ATTR {idproduct} = "4e40", mode = "0666", owner = "<gexueyuan>"
# Recovery Nexus 7
Subsystem = "USB", ATTR {idvendor} = "18d1", ATTR {idproduct} = "d001", mode = "0666", owner = "<gexueyuan>"
# MTP mode with USB debug off Nexus 7
Subsystem = "USB", ATTR {idvendor} = "18d1", ATTR {idproduct} = "4e41", mode = "0666", owner = "<gexueyuan>"
# MTP mode with USB Debug on Nexus 7
Subsystem = "USB", ATTR {idvendor} = "18d1", ATTR {idproduct} = "4e42", mode = "0666", owner = "<gexueyuan>"
# PTP media with USB debug off Nexus 7
Subsystem = "USB", ATTR {idvendor} = "18d1", ATTR {idproduct} = "4e43", mode = "0666", owner = "<gexueyuan>"
PTP media with USB Debug on Nexus 7
Subsystem = "USB", ATTR {idvendor} = "18d1", ATTR {idproduct} = "4e44", mode = "0666", owner = "<gexueyuan>"
 
# Google Nexus 7 MTP mode: Automatic mount when plugged (all android versions)
Env {id_model }== "nexus", ENV {id_model_id }== "4e41", Action = "add ", run + = "/usr/bin/Sudo-B-u gexueyuan/usr/local/sbin/go-mtpfs-allow-Other = true/Media/nexus7"
Env {id_model }== "nexus", ENV {id_model_id }== "4e42", Action = "add ", run + = "/usr/bin/Sudo-B-u gexueyuan/usr/local/sbin/go-mtpfs-allow-Other = true/Media/nexus7"
Env {id_model }== "nexus_7", ENV {id_model_id }== "4e41", Action = "add ", run + = "/usr/bin/Sudo-B-u gexueyuan/usr/local/sbin/go-mtpfs-allow-Other = true/Media/nexus7"
Env {id_model }== "nexus_7", ENV {id_model_id }== "4e42", Action = "add ", run + = "/usr/bin/Sudo-B-u gexueyuan/usr/local/sbin/go-mtpfs-allow-Other = true/Media/nexus7"

# Google Nexus 7 MTP mode: Automatic unmount when unplugged (all android versions)
Env {id_model }== "nexus", ENV {id_model_id }== "4e41", Action = "Remove", run + = "/bin/umount/Media/nexus7"
Env {id_model }== "nexus", ENV {id_model_id }== "4e42", Action = "Remove", run + = "/bin/umount/Media/nexus7"
Env {id_model }== "nexus_7", ENV {id_model_id }== "4e41", Action = "Remove", run + = "/bin/umount/Media/nexus7"
Env {id_model }== "nexus_7", ENV {id_model_id }== "4e42", Action = "Remove", run + = "/bin/umount/Media/nexus7"

Use your logon description to replace the preceding rules.Gexueyuan,Then restart the udev service.

# Sudo service udev restart

Now your nexus7 can be automatically mounted to a file system.

3. Declare in/etc/fstab

At this point, we have achieved the purpose of Automatic File System mounting. You can copy files and rename files. However, because it is not a real file system, you still face the following problems:

1. Files larger than 2 GB cannot be copied.

2. Empty files cannot be created.

3. You cannot drag a file in this file system.

4. the file cannot be opened directly in this file system.

In addition, you cannot directly click the arrow in the file system list to unmount the file system. To solve this problem, you need to write some declarations in/etc/fstab.


First run the command: # Mount

...
Devicefs (Nexus 7) on/Media/nexus7 type fuse. devicefs (Nexus 7) (RW, nosuid, nodev, allow_other, user = yourlogin)

Then, we add a rule at the end of/etc/fstab.

......
Devicefs (nexus \ 0407)/Media/nexus7 fuse. devicefs (nexus \ 0407) allow_other, RW, user, noauto 0 0

In this way, I can unmount it in MTP mode, and your Nexus 7 will also appear in the devices column.


Reference Source: http://bernaerts.dyndns.org/linux/247-ubuntu-automount-nexus7-mtp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.