Android - How can I use my Galaxy Nexus as an external USB storage drive?

Short Answer:

Currently, you can't make use of your device (any device that relies on MTP) to act as an MSC, due to the protocol specifications!

Elaborating

As to clarify, Galaxy Nexus uses one massive partition for the entire device, it's like saying that: system, data and sdcard are all together.

MTP was selected for Galaxy Nexus because it allows the use of this large storage area, that from a system point of view, insures a better communication with files, provides a more secure file system and gives less trouble to maintain.

Why you can't do it the way you want:

There are no separated partitions in Galaxy Nexus, only one large storage area. Even emulating a fat32 file system, one would still need to be able to translate between fat32 and ext4 at a low level. This is practically impossible.

If the Galaxy Nexus had a partition, you could have block-level access and thus you could have mass storage access.


Long boring version:


Short background

The Mass Storage Class (MSC), commonly know as USB Mass Storage (UMS), that we are all used to use on our devices, is a protocol that allows a Universal Serial Bus (USB) device to become accessible to a host computing device, to enable file transfers between the two. On the golden days, one just needed to plug in the phone, hit "USB mode" and start moving files, editing files, etc...


Media Transfer Protocol (MTP)

Things have changed a lot, when we talk of the new transfer method that's being used on your device, the Media Transfer Protocol (MTP):

1 Introduction see page 15 zip below

Media Transfer Protocol, or MTP, is a protocol designed for content exchange with and command and control of transient storage devices. It was been developed as an extension to PTP, or Picture Transfer Protocol, and is targeted primarily at Digital Still Cameras, Portable Media Players and Cellular phones.

While the introduction still leaves margin for doubt, this protocol was developed and implemented with a very specific purpose: to facilitate the transfer of media files and associated metadata between devices:

1.1 Purpose see page 15 zip below

The primary purpose of this protocol is to facilitate communication between media devices that have transient connectivity and significant storage capacity. This includes the exchange of binary objects and the enumeration of the contents of that connected device.


File Types supported?

At this point, we already know that this is for media files and media based devices, but one question that I've seen on the comments remains: Can we copy files other than media files (audio/video)? Yes we can:

1.3 MTP Object Model see page 15 zip below

The term "media" in "Media Transfer Protocol" is used to identify any binary data, and is not restricted to audio/video formats to which it is commonly applied. Some examples of non-audio/video objects include contacts, programs, scheduled events and text files.


What about "live edit" ?

Your question specially concerns the usage of your device (Galaxy Nexus) as an external USB storage drive. Why it can't be done? The MTP protocol limits data to a unidirectional operation, also latter explained that the device storage is used by the computer as a local file:

4.2 Unidirectional Data Flow see page 29 zip below

The data flow in MTP is always unidirectional. When initiating an operation, data flows only from the Initiator to the Responder. When responding to the requested operation, the data flows only from the Responder to the Initiator. During the binary data-exchange phase, data may flow from the Responder to the Initiator or from the Initiator to the Responder, but never both. Bi-directional, binary data exchange must be performed by multiple operations.


Advantages vs Drawbacks

Advantages

The MTP does carry some advantages with it, mainly concerning the security and integrity of the user data:

Microsoft MTP Implementation Overview

When a USB host computer has mounted an MSC partition, it assumes absolute control of the storage, allowing for example, the file system to get corrupted or reformat to an unsupported type.

Meanwhile, the MTP overcomes this by making the unit of managed storage a local file that can either be written or read.

Drawbacks

The MTP isn't treated as a traditional removable drive, so, no recovery tools can be used to save the day.

The support is still underway, some operating systems still require third-party software.

MTP standards (as mentioned earlier) don't allow for direct modification of objects. Instead, modified objects must be reuploaded in their entirety.


References:

I hope this clarifies as to why MTP is good (improvement), but OTOH bad (user will have to change habits).

Here's the reading material used to synthesize the answer:

  • usb.org :: Media Transfer Protocol v.1.1 Spec and MTP v.1.1 Adopters Agreement .zip with pdf files
  • Wikipedia :: Media Transfer Protocol
  • Android Central :: MTP - what is it, why use it, and how to set it up
  • Microsoft :: Media Transfer Protocol Enhanced Specification
  • Microsoft :: Media Transfer Protocol Implementation Details

The above answer is credited to the contents and authors of these reference links.


It may be possible to export a loopbacked image file via USB as UMS/MSC.

You will definitely need:

  • root
  • kernel support: loopback (/dev/block/loop0) and UMS (/sys/devices/platform/usb_mass_storage/)
  • custom app to enable/disable this or a custom init script to always export the image file
    Here's an example for an easy hackable app, a little development needed though... One needs to change the default block device (/dev/block/mmcblk0pX) to the loop0 and also losetup the loop0 beforehand, then build. Additionally it could be possible to have it mounted on the Android device if it's not exported, e.g. at /sdcard/fat

Here's a short test on my Galaxy Nexus (in recovery mode):

#create a 10mb image file and format it with FAT, name it testFat:
me@workstation:~$ dd if=/dev/zero of=/tmp/fat.img count=10 bs=1M
10+0 records in
10+0 records out
10485760 Bytes (10 MB) copied, 0,00699535 s, 1,5 GB/s
me@workstation:~$ mkfs.vfat -n fatTest /tmp/fat.img 
mkfs.vfat 3.0.9 (31 Jan 2010)
me@workstation:~$ file /tmp/fat.img
/tmp/fat.img: x86 boot sector, [...], label: "fatTest    ", FAT (16 bit)

#Push it to my phone, create loop0 from it and export it via UMS
me@workstation:~$ adb push /tmp/fat.img /sdcard/fat.img
5215 KB/s (10485760 bytes in 1.963s)
me@workstation:~$ adb shell
root@android # ls /sys/devices/platform/usb_mass_storage/lun0
file    power   ro      uevent
root@android # losetup /dev/block/loop0 /sdcard/fat.img
root@android # losetup
/dev/block/loop0: 0 /sdcard/fat.img
root@android # echo /dev/block/loop0 > /sys/devices/platform/usb_mass_storage/lun0/file 
root@android # exit

#See that it works. Yay!
me@workstation:~$ 
me@workstation:~$ mount
[...]
/dev/sdg on /media/fatTest type vfat (rw,nosuid,nodev,uid=1000,gid=1000,shortname=mixed,dmask=0077,utf8=1,showexec,flush,uhelper=udisks)
me@workstation:~$ dmesg
[137805.009285] sd 27:0:0:0: [sdg] 20480 512-byte logical blocks: (10.4 MB/10.0 MiB)
[137805.010024] sd 27:0:0:0: [sdg] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
[137805.013446]  sdg:
me@workstation:~$

This is the TL;DR version.

Sorry, USB Mass Storage is simply impossible with the Galaxy Nexus.

The reason for this is that USB Mass Storage gives the PC block-level access to the filesystem, and to maintain the integrity of the filesystem, it must be exclusive access. This is why Android phones unmount the SD card and apps can't use data on the SD card while the phone is in USB Mass Storage mode.

The Galaxy Nexus, however, doesn't have an SD card. It's the phone's voluminous internal storage that is being accessed.

MTP (see @Zuul's excellent explanation) gets around this and lets both the phone and PC access the files without the phone having to be locked out. The down sides are that you don't get exclusive block-level access, and operating system support for MTP just isn't quite there yet.

If this is a feature you simply can't live without, then your best bet is to use a different phone. Not coincidentally, this is one of the reasons I got rid of the Galaxy Nexus and got a DROID RAZR MAXX.