[Nexus 4 Android Development] MisakaImouto:
OP's use of udev inspired me to come up with this solution using ADB.

I'll assume you have ADB installed. There's plenty of indepth guides for that elsewhere.

This tutorial will have you playing around in system directories on your Linux install. I tried to make this guide user friendly but you should have at least SOME knowledge of Unix like OSes before you even try this.

Not for the faint of heart.

Get http://https://play.google.com/store/apps/details?id=berserker.android.apps.sshdroid&hl=en from the play store. Enable USB debugging in the developer options on your phone.

Open SSHDroid on your phone and set your port to something other than 22. I use 2222.

Open a terminal window on your computer and elevate yourself to root (Preferably using su -. Otherwise use sudo su -.).

Now that we got that done, try logging into your phone to make sure everything works. It'll be something like

ssh -p2222 root@[yourPhone'sIP]

. If you get asked a password it probably works. Just hit ^C (Ctrl+C) to exit that password prompt since we don't need to go further yet.

Next you'll need to create a passwordless SSH login. Run

ssh-keygen -t rsa

. Just hit enter to use the defaults on all the questions, we don't need anything more.

Now run

scp -P2222 ~/.ssh/id_rsa.pub root@[yourPhone'sIP]:/emulated/storage/legacy/

. When it asks for the password just type "admin", though it should tell you that anyway.

If the file copied over successfully you should no be able to type

ssh -p2222 root@[yourPhone'sIP]

again and not be prompted with a password at all. Cool!

At this point it's recommended you go into the SSHDroid settings on your phone and untick the "Enable Password" box, though it's not required, especially if you want to ssh to your phone from other devices/computers.

Run lsusb and look for the line that says Google in it. You want to look out for something like

Bus 001 Device 070: ID 18d1:4ee2 Google Inc.

Copy the two alphanumeric codes before "Google Inc.". The first is your vendor ID and the second is your device ID. You might want to label them so you remember which is which.

Here's the scary part of the tutorial. Use your favorite text editor (preferably command line based) and create a new file at "/etc/udev/rules.d/85-android_mount.rules". Copy and paste this and edit to fit the IDs you got in the previous command.

SUBSYSTEMS=="usb", ACTION=="add", ATTRS{idVendor}=="18d1", ATTRS{idProduct}=="4ee2", RUN+="/usr/local/sbin/phone-mount -m" SYMLINK+="Nexus_4"
SUBSYSTEMS=="usb", ACTION=="remove", ATTRS{idVendor}=="18d1", ATTRS{idProduct}=="4ee2", RUN+="/usr/local/sbin/phone-mount -u" SYMLINK+="Nexus_4"

It should be obvious where you need to put your Vendor and Device IDs now.

Save it and run these commands.

chmod +x /etc/udev/rules.d/85-android_mount.rules
chmod 775 /etc/udev/rules.d/85-android_mount.rules
chown root:root /etc/udev/rules.d/85-android_mount.rules

Now we need to create another file at "/usr/local/sbin/phone-mount" and paste this inside.

#!/bin/bash
export PATH=/opt/android-sdk-update-manager/platform-tools/:$PATH

case "$1" in
-m )
adb wait-for-device
adb forward tcp:2222 tcp:2222
# If you have SSHDroid Pro you just need to add "pro" to the "sshdroid".
adb shell am broadcast -a berserker.android.apps.sshdroid.command.START
sshfs -p2222 -o allow_other,idmap=user root@localhost:/storage/emulated/legacy/ /media/nexus4
exit 0
;;

-u )
umount /media/nexus4
exit 0
;;
esac

We need to modify the permissions for this file too.

chmod +x /usr/local/sbin/phone-mount
chmod 775 /usr/local/sbin/phone-mount
chown root:root /usr/local/sbin/phone-mount

Almost done. If you don't already have sshfs installed on your Linux install then do so now. You'll also need to enable the "allow_other" option. For my distro it's as simple as editing "/etc/fuse.conf" and uncommenting the "user_allow_other" line. Your results may vary.

Now finally we need to create the actual folder you'll be mounting your Nexus 4 at. If you blindly followed my guide that'll be /media/nexus4.

mkdir /media/nexus4
chown root:users /media/nexus4
chmod 775 /media/nexus4

Now all we need to do is restart udev. Right now you'll want to make sure the phone is unplugged, then restart your udev daemon. For Debian based distros like Mint, Ubuntu, Debian, Crunchbang, etc. you'd run.

service udev restart

After it's done restarting you can plug your phone in, wait a few seconds, and hopefully have your phone automount to /media/nexus4 .

You may have to improvise in a few spots because all distros are not the same, especially when you compare something like Gentoo to Debian. If you're having problems with sshfs chances are your answer is on Google. You'll honestly get better help there than having me trying to guess your system.

Also note this is a pretty hacky and dirty way to do things. Don't go yanking your phone's USB cord out without unmounting "/media/nexus4" first. You shouldn't have any problems if you're not reading or writing anything, but there is the potential for data loss and crashing udev.