Tuesday, 29 March 2011

How to use your Android phone as a Linux live USB stick

Two of the things I carry with me everywhere are a USB stick containing a live Linux installation (so that I can boot Windows machines into Linux and get to my own desktop and files), and my phone. This week, in a fit of ruthless efficiency, I discovered that it's possible to do without the USB stick: I can plug my phone into a computer, and boot directly from it.

My wife's Windoze laptop, booted into Fedora using my phone
When I connect my phone (an HTC Desire, but this should apply to all Android phones) to a computer and set the phone to 'Disk drive' mode, it exposes the whole SD card as a block device. That means that you can do pretty much anything to it that you could do if it were a USB stick. Format it, partition it, boot off it...

That opens up a world of possibilities. Effectively you can carry your computer round in your pocket, with all the programs you want, and all your documents safely encrypted. For bonus marks, you can set your phone up to read the encrypted image and get access to your documents directly from your phone.

Here's what to do to get your phone set up for booting.

  1. Prepare a Fedora ISO image that you'd like to boot from. I've got my own that I built, with the programs on it that I generally use, but the easiest way to get one to experiment with is to download the live CD image from the Fedora web site.
  2. Make sure there's enough room on your phone's SD card. To play this game properly, you'll need enough space for the installation image (~650MB for the live CD), some space for a persistent overlay so that you can install and remove other programs and edit system settings (~350MB should be plenty), and some space for an encrypted filesystem containing your documents; so maybe 1GB plus document storage space. You don't need to repartition: this can all go on the FAT32-formatted partition you already have. (The whole process is non-destructive.)
  3. Set the FAT32 partition on your phone's SD card to be bootable. Plug your phone into your computer, put it in 'Disk drive' mode, and then use parted on Linux, or a GParted live CD, or whatever you Windows types use for partition management.
  4. Now install the image. I'm using Fedora, so the gubbins I need to perform the installation is already there, in the livecd-tools package; the command I use to install onto a USB stick is
    livecd-iso-to-disk --reset-mbr --overlay-size-mb 350 --home-size-mb 1024 whatever.iso /dev/sdb1
    Be very careful to get this right! You need to replace '/dev/sdb1' with the device representing your card's FAT32 partition. The '--reset-mbr' isn't as scary as it looks: it doesn't destroy the partition table, but it does set the master boot record to something that you can boot from.
  5. Reboot your phone to convince yourself you didn't brick it.
  6. Now boot your computer from your phone! Set your phone to 'Disk drive' mode again, reboot your computer, and hit F12 or whatever lets you choose a boot device, and select 'USB device' or equivalent.
I've found that it is a little slow when booting up, but operates at a fair old lick when running. Read performance isn't too bad, and write performance is hugely helped by the cacheing, as long as you don't do anything to hammer the disk. For web browsing, editing OpenOffice documents, programming and pretty much anything, it works very nicely. I even compiled a kernel and it coped just fine.

Answers to questions for more excitable types:
  • Do I need to have rooted my phone? No. All you're doing is using it to store some files. On the other hand, if you have rooted your phone, you'll be able to access the encrypted files directly from your phone.
  • What happens if my phone battery runs out? It won't. On my phone, at least, the USB port supplies more than enough power to keep it operating as a disk drive, so it'll charge up rather than drain.
  • What do I do if the phone rings? Answer it. There's nothing to stop you using your phone as a phone, as long as you don't unplug it, reboot it or turn it off 'Disk drive' mode. (That does mean that your FAT32 partition won't be mounted on your phone, so any apps that you've got stored on the SD card using the native Froyo system won't be operational. If you've used an A2SD-style separate partition, your apps will all work fine.)
  • What's the best way to make use of all this? That rather depends on what you want to do. I use Unison to sync my files so that everything's up to date, but you could equally use Dropbox or similar. Really, the sky's the limit: you can use it to do anything you could do with your normal computer.
  • How do I mount the encrypted partition directly from my phone? This takes a little bit of planning, and I'll write a full article on that soon. You need four things to get it to work:
    • a rooted phone;
    • a cryptsetup binary compiled for ARM (download);
    • a recent busybox binary (if you haven't got it already, install from the Market or download);
    • a phone kernel with compiled-in support for the encryption present in the encrypted partition.
    If you don't already have the first, you probably shouldn't be messing with this low-level stuff. The fourth is the trickiest, but not impossible; and by far the easiest approach is to change the encryption to match your kernel rather than change your kernel to match the encryption. Here's the rough outline of what to do to open the image, and how to change the encryption if necessary. It assumes some familiarity with doing bad things to your phone:
    1. Use adb shell to get a terminal on your phone.
    2. Map the encrypted image to a free loop device:
      1. Use busybox losetup -f to find a free one.
      2. Create it if necessary: busybox mknod -m 0600 /dev/loopx b 7 x (replacing 'x' with the number of the first free device, if it doesn't exist).
      3. Check that the one you've created is still free! It should be, but for some reason, when I create /dev/loop0 through to /dev/loop3 on my phone, they all get eaten straight away. Anything numbered from 4 upwards works fine for me.
      4. Map the device: busybox losetup /dev/loopx /sdcard/LiveOS/home.img
    3. Try to open it: cryptsetup luksOpen /dev/loopx enchome
    4. If you're lucky, it'll open fine, and you won't need to change the encryption. If you get an error telling you to check your kernel for the right cipher support, it means you're going to need to change the encryption. If you've stored anything important in the encrypted image, stop and copy it out, because this will destroy it (but you only have to do it once):
      1. Format it with a different cipher:
        cryptsetup --cipher=aes-cbc-benbi luksFormat /dev/loopx
        You might need to try different cipher specs till you find one that your kernel supports. You could try 'aes-cbc-plain' or just 'aes' or even 'twofish'. A look at /proc/crypto will give you some clues as to what's available, but it's not easy to work out exactly what it all means. Make sure you stick to something that gives you a decent level of security.
      2. Try opening it again, using the luksOpen command above.
      3. You'll now need to format it again, with
        busybox mke2fs -m 0 /dev/mapper/enchome
        If you get an 'applet not found' error, your version of busybox isn't recent enough.
    5. Once you've successfully run the luksOpen command, you can now mount the image. Make an empty directory somewhere that you can mount it in (say, /sdcard/encimage), and then mount it with: mount /dev/mapper/enchome /sdcard/encimage
    From now on, you should be able to follow this procedure (without needing to change the encryption every time) to mount the image with full read/write access. This really needs automating, and a little GUI putting together... I'm working on it.


  1. great article, I've been wanting to do this for a long time! I've tried this with my HTC Incredible, installing the latest Ubuntu on it's external SD card. So far, no success. I don't have the Fedora livecd-tools package, so I first tried installing grub2 manually, then tried using unetbootin. Neither method worked.

    I am wondering if it might have something to do with the fact than when I choose to use "usb storage" on my phone (putting it in disk drive mode), TWO sd cards are mounted on the computer, both the external SD card and the phone's built-in internal sd card. Perhaps the computer first mounts the internal sd card, doesn't see a bootable partition, then ignores the bootable external sd card?

  2. Bizarre--that doesn't happen with an HTC Desire. Are you definitely seeing two devices, rather than two partitions on the same device?

    I'm amazed that you can see the internal memory. It's supposed to be protected from the outside world, which is why it took such a long time for Google to sort out installing apps to the external SD card. It would also let you do serious damage to the phone if you could mess arbitrarily with the internal card as a block device.

    In principle, you could install GRUB on the internal memory, and get it to look for the files on the external card. But that would mean altering the MBR on the internal memory, which might brick your phone. (That's one of the reasons I'm so surprised that it exposes internal memory as a block device.)

  3. Yeah I was surprised too. Of course, my phone is rooted and running Cyanogenmod 7, which installs some apps to the external SD card by default. It's possible that my ROM is what makes both SD cards mountable. And yeah, they definitely show up as two separate devices rather than as two partitions on the same device. I was surprised by that, I didn't know one could mount more than one device thru one USB port...

    Anyway, I certainly don't want to mess with the internal SD card. I haven't found anyone else who's tried this though. Props to you for making it work!

    One last question, what exactly are you encrypting? Is it everything on the SD card, or are you just creating an encrypted section of files on it? I'm always interested in adding encryption to my documents and files, but so far I haven't seen any options for android.

  4. I can't get this to work on the motorola milestone.

    Followed the instructions to the button 3 times. I guess it just wasn't meant to be. It's like all my computers don't "see" the sdcard at boot time.

    Perhaps this phone unmounts during power off, and takes too long to mount the sdcard to read/write mode. So by the time it's active, the computer is well past checking for boot options.

    Just my guess.

  5. That's a bit odd. Does it usually give you the option of booting off a USB stick?

    You could try plugging the phone in, turning the computer on, and then going into the BIOS for a few seconds. See whether it's ready to let you boot off the phone when you come out.

    If so, you might also try turning the computer on, selecting boot options, and then, if it doesn't list the phone ('USB Device' is how it comes up on mine), hit Ctrl-Alt-Del and try again. I'd hope that a soft reset wouldn't make the phone unmount and mount again.

  6. Hey James,

    All computers I tested on can boot from usb and is set to check usb first in the bios.

    I tried that last night. Booting into the bios to see if that would delay long enough for the sdcard to mount. It did, however as soon as you leave the bios the computer restarts, and immediately the sdcard unmounts and then tries to remount again as it boots back. It's an annoying situation because if it only did it just under a second quicker, I think it would recognize it.

    It just must be an issue with the milestone. It could be solved if I can find an app that just leaves the sdcard mounted as a usb device by default, rather than automatically unmounting and mounting as soon as it's plugged in/out.

    It's the same on my laptop and my desktop.

    However my desktop running Ubuntu Lucid Lynx when fully booted and logged in, when I connect my phone via USB it adds and mounts the image on the sdcard as a live cd that I can run inside of the desktop. So there is nothing wrong with the configuration, it is seen as a boot cd. I can still run the linux live "cd" inside the host OS.

    I just can't get it to run at boot up which is the entire point.

    If I can solve this I'll post what I done here so other users with the same model/issue have this problem can see what I have done.

    Thanks for the reply man and if you can think of anything let me know. :)

  7. The only other thing I can think of is to try it from recovery mode. I've got ClockworkMod recovery installed on mine, and one of the options in there is for mounting the sd card and exposing it over USB when you're in recovery. I think it's under 'Advanced'.

    You could try that, and see if it gets you anywhere. It's not a very good long term solution, because you don't really want to have to leave your phone in recovery mode all the time, but it might at least tell you that it's the mounting/unmounting during normal phone operation. (Maybe it'll do that in recovery mode too, but I doubt it.)

  8. hey try to see if you can change the bios setings to wait longer (ie for the boot screen with the logo) i know i had a computer i shorted it for so i think it would work both ways

    also mabey you can reserch for drives after its insail serch? (i know this more out there but im not at a windows computer right now so i can't test this idea)

  9. I just saw this thread, sorry I'm a little late to the game, but I should be able to clear up a couple of questions - first, @Danarchy - I believe the HTC Incredible has two memories, plus the internal firmware. The external SDCard should be able to be run from, like you are trying, while the internal is probably best to leave alone, but you could in reality run from it as well (like James said, this is a non-destructive process, so... However, to get it to boot, have you tried using the boot chooser (or boot menu?) Most computers have an option to pull up a boot menu outside of the bios menu to be able to select exactly which device to read from. On my Motorola Photon I have the same issue (with the two devices showing up), but as long as I use the boot menu to select the correct one (which was just guesswork at first, but I've figured out which one it is for each computer), it will boot.
    Also, @Dangerous Dave - did you leave the bios without saving? Usually that should not cause a system restart, only if you save - thus no power interruption and the device should boot. If that doesn't work, you didn't say whether you had tried the boot menu option like I described for Danarchy - have you done that, instead of just trying to use the bios? On most computers this menu is either F8 or F12, but I have found a few (most notably Vaio's) that I wasn't able to access a boot menu in, so that may be a problem. Let me know if this helped, or if you're still trying and I can suggest something else!

  10. I'm looking for a good solution to the same issue, it takes my phone way too long to enable usb storage mode before the bios goes through detecting bootable usb devices. I've tried a few different laptops I have laying around, going straight to the boot menu (esc or f12, etc) but I can't get the timing right. I even tried hitting pausebreak on one computer to try and pause it before the detection sequence, but that doesn't seem like a preferable work-around either.

    Perhaps there's an app or mod to simply toggle a listen mode for usb to be always ON or OFF, instead?

    Would love to have my android phone bootable!

  11. can anybody try it with linuxlive.com ? I install it on my microSDHC but i can't boot it.
    Boot flag OK
    from Vbox it run perfect

    maybe android (gingerbread) remove some files when mounted in phone because it's removing autorun.inf :(

  12. Hey, I successfully did this on my HTC desire, its awesome! I put linux mint on it, it's my very first smart phone and very first time rooting (or jail-breaking, etc) any sort of device. It got me interested in Linux, and I really like it so far. I was just wondering if this could damage my phone at all. I understand anything could potentially damage my phone (like rooting, s-off, over-clocking, etc), but is it possible to damage the sd card, or perhaps the cpu with over-use? Does your phone get hot when you do this? I'm a total rookie so any info would help out lots.

    1. Hi Brendan,

      No, it won't damage your phone. It's not doing a huge amount of writing to the SD card when you boot up: only the home directory is mounted as writeable. If you had a swap partition on your SD card you might be in danger of thrashing it, but you won't have one of those unless you go out of your way to create it.

      The phone isn't doing anything other than allowing SD card access over the USB port, so it also isn't doing much, and won't get hot.

      The three other things you mention (rooting, s-off, overclocking) are all potentially dangerous, and can brick your phone, but this is perfectly safe.


  13. Can the android OS on the phone be used as a boot device for the laptop? @mookerjee

  14. For me, on Gparted -- It shows up as Unallocated Space (around 11GB)

    It won't let me repartition it to FAT32 (or anything else for that matter)

    So now im stuck. Any ideas?

    1. BTW, I have a Samsung Galaxy S2, Rooted. Ubuntu Detects the phone fine.

    2. If this is built-in storage rather than an SD card, it might be tricky. Have you tried repartitioning it from recovery mode? Take a backup first, though...

      I think that the partition table for internal storage is in the HBOOT image, so you would probably have to flash a custom HBOOT with the appropriate entries in the partition table. After that, you'd be able to format the partitions.

      You could look at http://alpharev.nl/ for some sample HBOOT images for the HTC Desire, to give you an idea of how a new HBOOT can change your partition table. (Don't flash one of these to an S2, though!)

  15. I FOUND A SOLUTION to a problem a lot of you were having. That is, the problem of the computer not recognizing the phone as a USB drive. Some of you mentioned that it could be because the phone takes too long to re-initialize the usb share after the computer re-boots. I tried sharing the usb from Clockworkmod and it worked like a charm! (One of you mentioned this idea, I'm verifying that it worked for me.)

    I'm actually not booting linux, I'm booting Kon-Boot (a tool to bypass password security).
    Also, I didn't install the iso to the sd card using the software in this tutorial, instead I used UNetBootin. And I also didn't do this via usb cable through the phone, I used a usb card reader (just for the setup, not for the actual use, of course).

    Anyways, I don't think those differences would make any difference in terms of the computer recognizing the phone as a bootable usb, so if you're having issues with the computer not recognizing it try sharing the sd card from ClockworkMod > Tools > Share sd card. Not the most practical way to do it because you have to power off your phone every time you want to boot from it, but at least it works. Does anybody know of a way to achieve constant sd card sharing from within Android?

  16. Hi, thank you James, and fellows for comments.
    That would interest me but i see it practical with some phones only, unless I missed an important point.
    Basically there's no way to "Set this phone to 'Disk drive' mode" if unconnected to a running OS with e.g. a Galaxy Ace GT-S5830 that has Android 2.3.3

    On this phone the "connect usb storage" does pop-up only when plugged on a running OS (Linux), and disappears at some point while the OS is shutting down (around the time when the usb layer shuts down I believe). Btw the whole usb connect as mass storage process is not that stable: Has to either plug it in another usb port or do a full reboot after I connected/disconnected mass storage one time, to get the phone's sd-card be visible again -lol

    Please do you think it's all software related (and Ben's hack to sharing usb from Clockworkmod would work), or might be hardware limited for such phones as the Galaxy Ace GT-S5830?

    Again, thanks everyone for sharing, both how-to, questions and answers ^o^

  17. At the same time, not everyone who performs gay sizegenetics will probably be willing to come to one's location. Some people who definitely are new toward the enjoyment for a gay sizegenetics may not be comfortable going into a house or studio with the male sizegenetics. As you check whether generally are not a practitioner will come to one's location, you can inquire if you can find a difference between the price for an in call visit at their location, or an out call. You may want to never ever feel pressure to go on a location you feel uneasy about. Your erotic gay sizegenetics should, at no time ever give you uncomfortable.

  18. I used BOOTICE to install grub4dos on my sd and it worked fine. now I have hiren on my sd.

    for usb recognation on boot menu I paused my pc boot after system beep sound and after a few seconds I mound sd on phone and wait to complet correctly then return pc boot by ESC. it will show sd on boot menu.

  19. hi...Im student from Informatics engineering nice article,
    thanks for sharing :)

  20. Hey, Your post is very informative and helpful for us. In fact i am looking this type of article from some days. Thanks a lot to share this informative article.
    You can also see this MicFlip: World's First Reversible Micro USB Cable .


  21. Stupendous blog you guys have provided there, I will absolutely valuate your effort.smoking phone

  22. I just got my new android delivered using The Box Zone and it came in perfect condition. I can't wait to use some of your ideas on it. Thanks for the post!

  23. Really awesome tips.
    I did some of the above mention tips few times.
    But I didn’t use this methods simultaneous.

  24. Thanks for sharing Information About convert mobile intoDesktop

  25. Press the Back button to return to the previous screen.
    c. Speak Caller ID allows users to decide whether Talkback. apk

  26. "I very much enjoyed this article.Nice article thanks for given this information. i hope it useful to many pepole.php jobs in hyderabad.