├── .gitignore ├── helpers ├── .gitignore ├── setenv.sh.template ├── debian_post_install.sh.template ├── userinit.sh.template ├── debian_umount.sh.template └── debian_mount.sh.template ├── TODO.md ├── env.sh.template ├── LICENSE ├── README.md └── android_chroot.sh /.gitignore: -------------------------------------------------------------------------------- 1 | env.sh 2 | -------------------------------------------------------------------------------- /helpers/.gitignore: -------------------------------------------------------------------------------- 1 | *.sh 2 | -------------------------------------------------------------------------------- /TODO.md: -------------------------------------------------------------------------------- 1 | # TODO 2 | - start w/ android 3 | - graphical output / VNC ? framebuffer ? 4 | -------------------------------------------------------------------------------- /helpers/setenv.sh.template: -------------------------------------------------------------------------------- 1 | export PATH=/usr/bin:/usr/sbin:/bin:/sbin 2 | export HOME=/root 3 | unset LD_PRELOAD 4 | -------------------------------------------------------------------------------- /helpers/debian_post_install.sh.template: -------------------------------------------------------------------------------- 1 | cd __ANDROIDSTORAGE__ 2 | unzip __IMAGENAME__.zip 3 | /system/bin/sh -x __ANDROIDSTORAGE__/debian_mount.sh 4 | chroot __ANDROIDSTORAGE__/__CHROOTDIRNAME__ /debootstrap/debootstrap --second-stage 5 | -------------------------------------------------------------------------------- /helpers/userinit.sh.template: -------------------------------------------------------------------------------- 1 | # Should be in /data/local 2 | # see https://android.stackexchange.com/questions/6558/how-can-i-run-a-script-on-boot 3 | # Does not work :-/ 4 | logwrapper /system/bin/sh -x __ANDROIDSTORAGE__/debian_mount.sh 5 | -------------------------------------------------------------------------------- /helpers/debian_umount.sh.template: -------------------------------------------------------------------------------- 1 | # Should go in __ANDROIDSTORAGE__/debian_umount.sh 2 | for i in proc dev/pts dev sys; 3 | do 4 | umount __ANDROIDSTORAGE__/__CHROOTDIRNAME__/$i 5 | done 6 | umount __ANDROIDSTORAGE__/__CHROOTDIRNAME__ 7 | ANDROIDLOOP=`losetup -a | grep __ANDROIDSTORAGE__/__IMAGENAME__ | awk -F: '{print $1}'` 8 | losetup -d $ANDROIDLOOP 9 | -------------------------------------------------------------------------------- /helpers/debian_mount.sh.template: -------------------------------------------------------------------------------- 1 | # Should go in __ANDROIDSTORAGE__/debian_mount.sh 2 | ANDROIDLOOP=`losetup -f` 3 | losetup $ANDROIDLOOP __ANDROIDSTORAGE__/__IMAGENAME__ 4 | [ ! -d __ANDROIDSTORAGE__/__CHROOTDIRNAME__ ] && mkdir -p __ANDROIDSTORAGE__/__CHROOTDIRNAME__ 5 | mount -t ext2 $ANDROIDLOOP __ANDROIDSTORAGE__/__CHROOTDIRNAME__ 6 | for i in proc dev dev/pts sys; 7 | do 8 | mount -o bind /$i __ANDROIDSTORAGE__/__CHROOTDIRNAME__/$i 9 | done 10 | -------------------------------------------------------------------------------- /env.sh.template: -------------------------------------------------------------------------------- 1 | # Chroot directory name and size of the image, eg. 500M, 3G. 2 | # Make sure your android filesystem allows for files >2G 3 | CHROOTDIRNAME=debian 4 | IMAGESIZE=1G 5 | 6 | # Debian variant and architecture You may want to [avoid 7 | # testing](https://www.reddit.com/r/debian/comments/503ja3/issues_with_debian_on_android_phone/) 8 | # for now, see [here](https://bugs.kali.org/view.php?id=3012) for a fix ? 9 | RELEASE=stable 10 | VARIANT=minbase 11 | ARCH=armel 12 | 13 | # On Linux: buid path and free loop 14 | BUILDPATH=. 15 | 16 | # On Android: target storage and free loop 17 | ANDROIDSTORAGE=/storage/sdcard0 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 aurelg (aurelien.grosdidier@gmail.com) 2 | 3 | License: MIT Permission is hereby granted, free of charge, to any person 4 | obtaining a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including without 6 | limitation the rights to use, copy, modify, merge, publish, distribute, 7 | sublicense, and/or sell copies of the Software, and to permit persons to whom 8 | the Software is furnished to do so, subject to the following conditions: . The 9 | above copyright notice and this permission notice shall be included in all 10 | copies or substantial portions of the Software. . THE SOFTWARE IS PROVIDED 11 | "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 12 | LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 13 | AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 14 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 15 | CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 16 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 17 | 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Purpose 2 | 3 | Create a loopback-based debian chroot on your Android device, in which you can 4 | do whatever you want (security stuff, development, hosting, etc.). Creating a 5 | chroot manually is very easy, but many errors can occur. This script 6 | consequently provides as much automation as possible, but stops upon errors to 7 | let you investigate what's wrong. If you can fix it, then create a PR. If you 8 | can't, create a new issue. 9 | 10 | # Requirements 11 | 12 | You need: 13 | 14 | - a rooted phone or tabled 15 | - ADB enabled, with root access 16 | 17 | Tested with: 18 | 19 | - Samsung Galaxy S3 Neo (GT-I9301I) 20 | - CM 12.1-20161031-NIGHTLY-s3ve3g (Android 5.1.1) 21 | - Debian stable/jessie chroot 22 | - Installation from Archlinux 23 | 24 | # Usage 25 | 26 | ## Create chroot (on your GNU/Linux box) 27 | 28 | - edit `env.sh` according to your needs 29 | - run `./android_chroot.sh` 30 | - enter the chroot (see below), and follow the [official 31 | guide](https://wiki.debian.org/chroot#Configuration) to setup the system 32 | according to your needs 33 | 34 | ## Use chroot (on your device) 35 | 36 | First go in the chroot directory you defined in `env.sh`, then: 37 | 38 | - mount chroot filesystem: 39 | 40 | `/system/bin/sh -x ./debian_mount.sh` 41 | 42 | - enter chroot: 43 | 44 | `chroot /bin/bash` 45 | 46 | - umount chroot filesystem: 47 | 48 | `/system/bin/sh -x ./debian_umount.sh` 49 | 50 | -------------------------------------------------------------------------------- /android_chroot.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -x 2 | 3 | # Bash unofficial 'strict' mode 4 | # http://redsymbol.net/articles/unofficial-bash-strict-mode/ 5 | 6 | set -euo pipefail 7 | IFS=$'\n\t' 8 | 9 | # 10 | # Setup variables: 11 | # - check that env.sh has been created 12 | # - load user defined environment variable 13 | # - setup other - internal - variables 14 | ANDROIDCHROOTPATH=`dirname $0` 15 | if [ ! -f $ANDROIDCHROOTPATH/env.sh ] 16 | then 17 | echo Please edit $ANDROIDCHROOTPATH/env.sh first 18 | echo cp $ANDROIDCHROOTPATH/env.sh.template $ANDROIDCHROOTPATH/env.sh 19 | fi 20 | source $ANDROIDCHROOTPATH/env.sh 21 | HELPERSPATH=$ANDROIDCHROOTPATH/helpers 22 | IMAGENAME=$CHROOTDIRNAME.img 23 | 24 | # 25 | # Create disk image and mount it 26 | # 27 | create_disk_image() 28 | { 29 | [ ! -d $BUILDPATH ] && mkdir -p $BUILDPATH 30 | cd $BUILDPATH 31 | dd if=/dev/zero of=$BUILDPATH/$IMAGENAME bs=$IMAGESIZE count=1 32 | mkfs.ext2 $BUILDPATH/$IMAGENAME 33 | mkdir $BUILDPATH/$CHROOTDIRNAME 34 | sudo mount -o loop -t ext2 $BUILDPATH/$IMAGENAME $BUILDPATH/$CHROOTDIRNAME 35 | } 36 | 37 | # 38 | # Run debootstrap - stage 1, on GNU/Linux 39 | # 40 | # As the host and the android system may have different architecture, the host 41 | # takes care of debootstrap's stage 1 (--foreign flag), and debootstrap's stage 2 is performed 42 | # directly on the target device (--second-stage flag). 43 | # 44 | run_debootstrap_stage1() 45 | { 46 | sudo debootstrap \ 47 | --arch=$ARCH \ 48 | --variant=$VARIANT \ 49 | --foreign \ 50 | $RELEASE $BUILDPATH/$CHROOTDIRNAME \ 51 | http://httpredir.debian.org/debian 52 | } 53 | 54 | # 55 | # Add helpers to image, to set proper environment variables, mount and umount 56 | # /proc, /dev, /dev/pts and /sys 57 | # 58 | add_helpers_to_image() 59 | { 60 | cat $HELPERSPATH/setenv.sh | sudo tee -a $BUILDPATH/$CHROOTDIRNAME/etc/bash.bashrc 61 | } 62 | 63 | # 64 | # Umount disk image 65 | # 66 | umount_image() 67 | { 68 | sudo umount $BUILDPATH/$CHROOTDIRNAME 69 | zip $BUILDPATH/$IMAGENAME.zip $BUILDPATH/$IMAGENAME 70 | } 71 | 72 | # Push disk image to your phone 73 | push_to_phone() 74 | { 75 | sudo adb push $BUILDPATH/$IMAGENAME.zip $ANDROIDSTORAGE/ 76 | } 77 | 78 | # 79 | # Generate helpers 80 | # 81 | generate_helpers() 82 | { 83 | SEDSTMT="" 84 | for j in ANDROIDSTORAGE CHROOTDIRNAME IMAGENAME 85 | do 86 | SEDSTMT="$SEDSTMT;s;__${j}__;${!j};g" 87 | done 88 | for i in `ls $HELPERSPATH/*.template` 89 | do 90 | sed $SEDSTMT $i > ${i%.template} 91 | done 92 | } 93 | 94 | # 95 | # Push helper and set access rights 96 | # 97 | push_helpers() 98 | { 99 | sudo adb push $HELPERSPATH/debian_mount.sh $ANDROIDSTORAGE/debian_mount.sh 100 | sudo adb push $HELPERSPATH/debian_umount.sh $ANDROIDSTORAGE/debian_umount.sh 101 | sudo adb push $HELPERSPATH/debian_post_install.sh $ANDROIDSTORAGE/debian_post_install.sh 102 | # TODO Doesn't work for some reasons 103 | # sudo adb push $HELPERSPATH/userinit.sh /data/local/userinit.sh 104 | # sudo adb shell busybox chmod +x /data/local/userinit.sh 105 | } 106 | 107 | # 108 | # Run debootstrap - stage 2, on Android 109 | # 110 | run_debootstrap_stage2() 111 | { 112 | sudo adb shell /system/bin/sh -x $ANDROIDSTORAGE/debian_post_install.sh 113 | } 114 | 115 | # 116 | # Well done :-) 117 | # 118 | finish() 119 | { 120 | echo "Well done. To enter your chroot:" 121 | echo "sudo adb shell" 122 | echo "cd $ANDROIDSTORAGE && chroot $CHROOTDIRNAME /bin/bash" 123 | echo "Enjoy!" 124 | } 125 | 126 | sudo adb root # First restart adbd as root 127 | create_disk_image 128 | generate_helpers 129 | run_debootstrap_stage1 130 | add_helpers_to_image 131 | umount_image 132 | push_to_phone 133 | push_helpers 134 | run_debootstrap_stage2 135 | finish 136 | 137 | --------------------------------------------------------------------------------