Next Previous Contents

2. The basics

To begin our journey, you will have to install some essential programs. Please note that Linux-side programs might be available as native packages for your distribution, i.e. as .rpm or .deb archives.

The very first thing you should do is enable USB debugging; this feature is found under ``Settings'', ``Developer options''. If your device lacks this entry (shame on the vendor!), you can enable developer options by tapping 7 times --- I'm not kidding! --- on the ``Build version'' menu.

2.1 Linux ADB (Android Debug Bridge)

ADB is a command line tool, installable on your Linux box. It lets you communicate with an Andbox, usually connected via USB, in order to perform a wide range of operations.

If you're lucky, your distribution may include a package called android-tools-adb, which contains the adb command. If not, get the Android SDK Tools for Linux from:

http://developer.android.com/sdk/

and find out how to install it on your Linux box.

I suggest that you open a terminal and run adb with no options to get an information screen. You should become familiar at least with the following options:

Before using the adb commands, you must make your Andbox visible to the Linux box. Enable USB debugging on your device and connect it via USB cable to your Linux host; the Andbox will probably make a sound and/or flash the screen and/or ask for your permission. Issue this command:

Linux:~$ adb devices
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
List of devices attached 
245a16e41fe71a95427cc4e65d36cc9f        device

If you don't see any output after ``List of devices attached'', or if a row of `?' is displayed, some steps and some patience are required to make your device visible. First of all, find out your device's Vendor ID and Product ID:

Linux:~$ lsusb
...
Bus 001 Device 004: ID 1e68:0072 TrekStor GmbH & Co. KG 
...

In this example, 1e68 is my tablet's Vendor ID, while 0072 is the Product ID.

I assume that your Linux distribution uses udev. As root, edit the file:

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

(create a new file if it's missing) and add this line:

SUBSYSTEM=="usb", SYSFS{idVendor}=="1e68", MODE="0666"

Change your Vendor ID and username as necessary, save the file and restart udev:

Linux:~$ sudo udevadm control --reload

Some devices may also need this additional step:

Linux:~$ mkdir $HOME/.android
echo "0X1E68" >> $HOME/.android/adb_usb.ini

that is, 0X (zero-ex) followed by your Vendor ID, all uppercase. Now unplug the USB cable, plug it in again, and run the commands:

Linux:~$ adb kill-server
Linux:~$ adb devices
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
List of devices attached 
245a16e41fe71a95427cc4e65d36cc9f        device

Now your Andbox should be visibile. If it's still not recognised, try all the above as root. If it still doesn't work, there might be a problem with your Andbox; you should consult your device's vendor.

If you have more than one device, you might have to restart the adb service to make the new Andbox visible when you plug it:

Linux:~$ adb devices
List of devices attached

Linux:~$ # no luck. Unplug the device
Linux:~$ adb kill-server
Linux:~$ # plug the device
Linux:~$ adb devices
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
List of devices attached 
047011c34260a417        device

2.2 The Android file system

Now we can talk to the Andbox via adb, so let's have a look at the Android file system hierarchy. I assume that you're familiar with the standard Linux directory hieararchy; Android's is fairly similar, with a couple of important differences.

Connect your Andbox and run the adb shell command, which launches Android's internal shell. Please be aware that Android's native shell is much less sophisticated than bash; available commands are not made by GNU either, so you should expect minor differences.

Linux:~$ adb shell
shell@android:/ $ ls -l
drwxr-xr-x root     root              2015-10-19 09:48 acct
drwxrwx--- system   cache             2015-10-19 10:52 cache
dr-x------ root     root              2015-10-19 09:48 config
lrwxrwxrwx root     root              2015-10-19 09:48 d -> /sys/kernel/debug
drwxrwx--x system   system            2015-10-06 07:53 data
-rw-r--r-- root     root          116 1970-01-01 01:00 default.prop
drwxr-xr-x root     root              2015-10-19 09:48 dev
lrwxrwxrwx root     root              2015-10-19 09:48 etc -> /system/etc
-rwxr-x--- root     root        98692 1970-01-01 01:00 init
-rwxr-x--- root     root         7272 1970-01-01 01:00 init.antares.rc
-rwxr-x--- root     root         2344 1970-01-01 01:00 init.goldfish.rc
-rwxr-x--- root     root         2820 1970-01-01 01:00 init.nv_dev_board.usb.rc
-rwxr-x--- root     root        17549 1970-01-01 01:00 init.rc
drwxrwxr-x root     system            2015-10-19 09:48 mnt
drwx------ root     root              1970-01-01 01:00 modules
dr-xr-xr-x root     root              1970-01-01 01:00 proc
drwx------ root     root              2012-08-30 12:39 root
drwxr-x--- root     root              1970-01-01 01:00 sbin
lrwxrwxrwx root     root              2015-10-19 09:48 sdcard -> /mnt/sdcard
drwxr-xr-x root     root              2015-10-19 09:48 sys
drwxr-xr-x root     root              2014-01-15 13:23 system
-rw-r--r-- root     root         1308 1970-01-01 01:00 ueventd.antares.rc
-rw-r--r-- root     root          272 1970-01-01 01:00 ueventd.goldfish.rc
-rw-r--r-- root     root         3825 1970-01-01 01:00 ueventd.rc
lrwxrwxrwx root     root              2015-10-19 09:48 vendor -> /system/vendor
shell@android:/ $ _

Minor differences are possible; for example, in the above screenshot I omitted a directory that is apparently tied to a specific vendor. Note that all directories except three belong to root:root; as a consequence, access to their contents will be limited on unrooted devices.

Now run the mount command, to see what devices correspond to what directories (line-broken for readability):

shell@android:/ $ mount
rootfs / rootfs ro,relatime 0 0
tmpfs /dev tmpfs rw,nosuid,relatime,mode=755 0 0
devpts /dev/pts devpts rw,relatime,mode=600 0 0
proc /proc proc rw,relatime 0 0
sysfs /sys sysfs rw,relatime 0 0
tmpfs /mnt tmpfs rw,relatime,mode=775,gid=1000 0 0
debugfs /sys/kernel/debug debugfs rw,relatime 0 0
none /acct cgroup rw,relatime,cpuacct 0 0
tmpfs /mnt/asec tmpfs rw,relatime,mode=755,gid=1000 0 0
tmpfs /mnt/obb tmpfs rw,relatime,mode=755,gid=1000 0 0
none /dev/cpuctl cgroup rw,relatime,cpu 0 0
/dev/block/platform/sdhci-tegra.3/by-num/p3 /system ext4 \
  ro,relatime,user_xattr,acl,barrier=1,data=ordered 0 0
/dev/block/platform/sdhci-tegra.3/by-num/p7 /data ext4 \
  rw,nosuid,nodev,noatime,errors=panic,user_xattr,acl,barrier=1,\
  journal_async_commit,nodelalloc,data=ordered 0 0
/dev/block/platform/sdhci-tegra.3/by-num/p4 /cache ext4 \
  rw,nosuid,nodev,noatime,errors=panic,user_xattr,acl,barrier=1,\
  journal_async_commit,nodelalloc,data=ordered 0 0
/dev/block/vold/179:8 /mnt/sdcard vfat \
  rw,dirsync,nosuid,nodev,noexec,relatime,gid=1015,fmask=0002,\
  dmask=0002,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,\
  shortname=mixed,utf8,errors=remount-ro 0 0
/dev/block/vold/179:8 /mnt/secure/asec vfat \
  rw,dirsync,nosuid,nodev,noexec,relatime,gid=1015,fmask=0002,\
  dmask=0002,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,\
  shortname=mixed,utf8,errors=remount-ro 0 0
tmpfs /mnt/sdcard/.android_secure tmpfs ro,relatime,size=0k,mode=000 0 0
/dev/block/dm-0 /mnt/asec/com.collabora.libreoffice-2 vfat \
  ro,dirsync,nosuid,nodev,relatime,uid=1000,fmask=0222,dmask=0222,\
  codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0
shell@android:/ $ _

Note the similarities and the differences with Linux filesystems; it looks a lot like standard Unix, anyway.

What you can actually do with the file system layout is much less than you would with Linux. On standard, unrooted devices, you can only access the internal storage, usually mounted on /mnt/sdcard, and the external storage (if available), usually mounted on /mnt/external_sd. The actual names of these directories may vary; e.g. /storage/sdcard0 or something else. You'll probably have to figure it out for each device.

User data, programs included, are stored in /data. Using the Android shell you can cd to that directory, but you can't list its contents if you lack root permissions. Programs are usually stored in /data/app, but others (typically, paid programs and programs moved to the external storage) under /mnt/asec.

A very important directory under /data is accessible and writeable: /data/local/tmp. We'll make use of this directory later.

Note that internal and external storage is mounted as VFAT, the beloved file system that lacks file permissions and many more features. We will see in the following how to circumvent VFAT limitations.

2.3 Main directories in the internal storage

Assuming that /mnt/sdcard is the directory containing the internal storage, you should become familiar with the following sub-directories. Remember that /mnt/sdcard is mounted as VFAT; directory names are therefore case-insensitive.

In addition, some applications make their data directory under /mnt/sdcard directly; for example, /mnt/sdcard/Foo123.


Next Previous Contents