Search

Ubuntu - Automount Nexus 7 in MTP mode

Contents[Hide]

dropcap-ubuntu-android

Latest Android devices, like Nexus 7, are using 2 different USB connexion modes : PTP and MTP.

With PTP mode, the tablet is seen as a digital camera. This is working Out Of The Box under Ubuntu 12.04. But, you can only access DCIM directory of your device.

With MTP mode, the tablet is seen as a multimedia device where you can access the complete exported filesystem. But it doesn't work Out Of The Box under Ubuntu 12.04. It needs some setup and configuration to work.

Even if you can access easily a MTP device thru a program called gmtp, it is not as convenient as a conventional USB mass storage, accessible directly from Nautilus.

This guide explains how to configure your Ubuntu computer to directly access your Nexus 7 exported filesystem in MTP mode as soon as you plug it to a USB port.

Thanks to some udev rules, your Nexus 7 will automatically mount when device is plugged in & unmount when device is unplugged.

This guide has been done for Google Nexus 7, but it should work with any other MTP device (Android phone or tablet, MP4 player, ...) if you adapt the Udev rules according to your device.
I've got some feedback that it has already been adapted succesfully to Samsung Galaxy S2, Galaxy S3, Motorola Atrix HD & Google Nexus 4.

It has been done under Ubuntu 12.04, but has also been tested succesfully under Ubuntu 12.10.

A new guide with a One Click MTP Device Configuration is also available.
With this guide, all the configuration job is done by script. It should work with any MTP device.

1. Setup MTP Fuse

1.1. Select Fuse extension

Different fuse extension are available to mount a MTP device on your Ubuntu filesystem.

I won't explain in details what are the differences between these fuse MTP extensions, but I selected go-mtpfs because :

  • overall it is much more stable than the other ones
  • connexion and directory change is quite fast
  • file transfer speed is quite good
  • file transfer uses much less of temporary space on tmpfs
  • you can directly rename files from the directories

There is still some limitation as :

  • you can not transfer files bigger than 2 Gb
  • file timestamp is not preserved
  • you can not access the device from another account than the one used for mounting the filesystem

But it is more than acceptable. So lets start.

1.2. Configure Fuse

Ubuntu 12.04 doens't allow normal users to access fuse configuration file. This is a bug, which is quite easy to correct by giving read attribute to /etc/fuse.conf.

By default, fuse does not allow mounted filesystems to be accessed by anybody else that the user who has mounted it. As MTP filesystem will be mounted by root to be used by any user, we need to modify fuse default behaviour to allow it. This is configured by uncommenting the user_allow_other key in /etc/fuse.conf.

# sudo chmod a+r /etc/fuse.conf
# sudo gedit /etc/fuse.conf

/etc/fuse.conf

...
# Allow non-root users to specify the 'allow_other' or 'allow_root' mount options.
user_allow_other

1.3. Compile & Install go-mtpfs

go-mtpfs is using Google go compiler. It's GIT repository is available from https://github.com/hanwen/go-mtpfs/.

Now that Go compiler has reached Ubuntu official repositories, thing are quite easy.

We will have to :

  1. Install GIT & Go compiler
  2. Create temporary compilation environment
  3. Retreive go-mtpfs from its GIT repository and compile it
  4. Copy the binary under /usr/local/sbin

# sudo apt-get install libmtp-dev git golang
# mkdir /tmp/go
# export GOPATH=/tmp/go
# go get github.com/hanwen/go-mtpfs
# sudo mv /tmp/go/bin/go-mtpfs /usr/local/sbin/go-mtpfs
# sudo chmod a+x /usr/local/sbin/go-mtpfs

go-mtpfs is installed and ready to use.

1.4. Create Mount Point

Then, we have to create the Nexus 7 mount point that will be used by go-mtpfs.

This mount point should be accessible by anybody as you will later mount the device using your user account.

# sudo mkdir /media/Nexus7
# sudo chmod 777 /media/Nexus7

Everything is now ready to mount your Nexus 7 under /media/Nexus7.

2. Create UDEV Rules

2.1. Determine Vendor_ID and Product_ID

If you want to be able to access your Nexus 7 (or any other device) in any mode, you have to declare some udev rules with different configurations :

  • Recovery mode
  • Fastboot mode
  • PTP mode (with USB debug on or off)
  • MTP mode (with USB debug on or off)

For every configuration, you can determine the Vendor_ID an Product_ID by running the lsusb command with your tablet connected :

# lsusb
Bus 001 Device 024: ID 18d1:4e42 Google Inc.

Here, the Vendor_ID is in blue and the Product_ID is in velvet.

These rules will have to be declared in the /etc/udev/rules.d/99-android.rules file (see below).

2.2. Determine MODEL and MODEL_ID

Next step is to configure udev to detect Nexus 7 connection and disconnection.

This will allow to automatically :

  • mount MTP filesystem when you plug your Nexus 7 in MTP mode
  • unmount the filesystem when you unplug it

These udev rules need to be based on the device MODEL and MODEL_ID to work when plugged and unplugged.

The following procedure will allow us to check the udev MODEL and MODEL_ID of our Nexus 7 :

  1. start the udev monitoring command,
  2. plug your Nexus 7 in MTP mode
  3. wait few seconds
  4. unplug it.

This should give something like this :


# udevadm monitor --environment --udev
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing

UDEV [66298.131958] add /devices/pci0000:00/0000:00:1d.7/usb1/1-5/1-5.1 (usb)
ACTION=add
BUSNUM=001
DEVLINKS=/dev/libmtp-1-5.1
DEVNAME=/dev/bus/usb/001/053
DEVNUM=053
DEVPATH=/devices/pci0000:00/0000:00:1d.7/usb1/1-5/1-5.1
DEVTYPE=usb_device
ID_BUS=usb
ID_MEDIA_PLAYER=1
ID_MODEL=Nexus
ID_MODEL_ENC=Nexus
ID_MODEL_ID=4e41
ID_MTP_DEVICE=1
ID_REVISION=9999
...
  

UDEV [61359.304558] remove /devices/pci0000:00/0000:00:1d.7/usb1/1-5/1-5.1 (usb)
ACTION=remove
BUSNUM=001
DEVLINKS=/dev/libmtp-1-5.1
DEVNAME=/dev/bus/usb/001/047
DEVNUM=047
DEVPATH=/devices/pci0000:00/0000:00:1d.7/usb1/1-5/1-5.1
DEVTYPE=usb_device
ID_BUS=usb
ID_MEDIA_PLAYER=1
ID_MODEL=Nexus
ID_MODEL_ENC=Nexus
ID_MODEL_ID=4e41
ID_MTP_DEVICE=1
ID_REVISION=9999
... 

We now know the USB ID_MODEL and ID_MODEL_ID of Nexus 7.

You will get different ID_MODEL according to android version (thanks to Alex Flynn for the info) :

  • Nexus for version up to Jelly Bean 4.1.2
  • Nexus_7 for version Jelly Bean 4.2 +

You will get different ID_MODEL_ID values if USB Debug is on or off on the device :

  • 4e41 if USB debug is off
  • 4e42 if USB debug is on

2.3. Setup UDEV Rules

Now that we have these data, we can create the udev rules in /etc/udev/rules.d/99-android.rules.

# sudo gedit /etc/udev/rules.d/99-android.rules

/etc/udev/rules.d/99-android.rules

# Google Nexus 7 16 Gb
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e40", MODE="0666" # Bootloader
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="d001", MODE="0666" # Recovery
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e43", MODE="0666" # PTP media, USB debug off
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e44", MODE="0666" # PTP media, USB debug on
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e41", MODE="0666" # MTP media, USB debug off
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e42", MODE="0666" # MTP media, USB debug on

# Google Nexus 7 MTP mode under JB 4.1 : automatic mount & umount when plugged (USB debug on & off)
ENV{ID_MODEL}=="Nexus", ENV{ID_MODEL_ID}=="4e41", ACTION=="add", RUN+="/usr/bin/sudo -b -u yourlogin /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 yourlogin /usr/local/sbin/go-mtpfs -allow-other=true /media/Nexus7"
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"

# Google Nexus 7 MTP mode under JB 4.2+ : automatic mount & umount when plugged (USB debug on & off)
ENV{ID_MODEL}=="Nexus_7", ENV{ID_MODEL_ID}=="4e41", ACTION=="add", RUN+="/usr/bin/sudo -b -u yourlogin /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 yourlogin /usr/local/sbin/go-mtpfs -allow-other=true /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"

You have to replace yourlogin by your Ubuntu session login.

During mount time, the sudo -b option is there to avoid some udev timeout (thanks to Andrew Underhill for the feedback).

Last thing is to restart udev for the new rules to become operationnal.

# sudo service udev restart

Your Nexus 7 filesystem is now accessible straight from your Ubuntu workstation,whatever mode (MTP, PTP, USB debug on or off) you are using.

3. Declaration in /etc/fstab

nexus7-ubuntu-precise-mount-computerAt this stage, you are able to automatically mount and unmount your Nexus 7.

Your Nexus 7 device should appear after few seconds in Nautilus computer section.

You can now browse your device straight from Nautilus. You can copy files from and to your Nexus 7, you can rename files, ...

You have to know that you will face some limitations as MTP is not a real filesystem protocol :

  • you can not copy files bigger than 2 Gb,
  • you can not create empty files,
  • you can not move file within the phone with drag & drop
  • you cannot open files in write mode directly from Nexus

 Another problem is that you can not unmount your Nexus 7 straight from Nautilus.

If you click on the Eject button, you will get an error message saying :

umount: /media/Nexus7 is not in the fstab (and you are not root)

To solve that, it has to be declared in /etc/fstab with its fuse caracteristics.

To get these caracteristics just type mount command with the Nexus 7 mounted :

# mount
/dev/sda1 on / type ext4 (rw,noatime,errors=remount-ro)
proc on /proc type proc (rw,noexec,nosuid,nodev)
...
DeviceFs(Nexus 7) on /media/Nexus7 type fuse.DeviceFs(Nexus 7) (rw,nosuid,nodev,allow_other,user=yourlogin)

The mount caracteristics are highlighted in red. We can now declare the device in /etc/fstab.

As /etc/fstab is using SPACE as a separator and as our Nexus 7 fuse caracteristics include SPACE caracter, we have to convert them to its octal escape code \040.

# sudo gedit /etc/fstab

/etc/fstab

...

DeviceFs(Nexus\0407)    /media/Nexus7    fuse.DeviceFs(Nexus\0407)    allow_other,rw,user,noauto    0    0

When you will remount it in MTP mode, your Nexus 7 will be seen as a device.

nexus7-ubuntu-precise-mount-deviceIn Nautilus, it appears in the device section and you are now able to Eject it ...

 

Hope it helps.

Signature Technoblog

This article is published "as is", without any warranty that it will work for your specific need.
If you think this article needs some complement, or simply if you think it saved you lots of time & trouble,
just let me know at This email address is being protected from spambots. You need JavaScript enabled to view it.. Cheers !

icon linux icon debian icon apache icon mysql icon php icon piwik icon googleplus