Skip to content

Instantly share code, notes, and snippets.

@shirish87
Last active October 14, 2016 04:37
Show Gist options
  • Save shirish87/97b7e2790252bd0c0255749328eb5e0b to your computer and use it in GitHub Desktop.
Save shirish87/97b7e2790252bd0c0255749328eb5e0b to your computer and use it in GitHub Desktop.
Install Debian (chroot) on Android
# #######################################
# Adapted from DebKit
# https://github.com/cybertim/DebKit/blob/db894dea44705c4a67e0bb8faa67fc3010755483/src/main/res/raw/debkit
# #######################################
set -e
# install location
ROOT=/data/sdext2/home/setup
# path to folder on sdcard for storing disk image
KIT=/storage/external_SD/debkit
# Debian suite
DST=jessie
SRC=debianmirror.nkn.in/debian # httpredir.debian.org/debian
DEBOOSTRAP_SRC="http://$SRC/pool/main/d/debootstrap/debootstrap_1.0.84_all.deb"
# size of the disk image. installed packages take up ~280MB of space
SIZ=500
TOOLS_SRC=https://raw.githubusercontent.com/shirish87/DebKit/master/src/main/res/raw
TOOLS="busybox pkgdetails mke2fs"
# packages preinstalled in the disk image
INCLUDE_PACKAGES="--include=locales,sudo,man-db,perl,psmisc,ca-certificates,vim,screen,python-pip,openssh-server,telnet"
# ####################################
# folder to store setup files
BIN=$ROOT/setup
# mounting path
MNT=$ROOT/mnt
# busybox path
BB=$BIN/bin
# debootstrap extract path
BS=$BIN/bootstrap
# disk image
DISK_IMG=$KIT/disk.img
# block device
BLK_DEVICE=/dev/block/loop99
# mount check file
MNT_CHECK=$MNT/.mount_check
# filesystem type
FS_TYPE=ext2
load_fstype()
{
FS_TYPE=""
TEST_FS="ext2 ext4"
for fs in ${TEST_FS[@]}; do
if [ $(grep -c $fs /proc/filesystems) -eq 1 ]; then
FS_TYPE=$fs
break
fi
done
if [ -z $FS_TYPE ]; then
echo "Unsupported filesystems: $TEST_FS"
exit 1
fi
}
setup_sys()
{
case "$1" in
dependencies)
echo "Fetching dependencies..."
mkdir -p $BIN
cp "$0" "$BIN/debkit"
for t in ${TOOLS[@]}; do
if [ ! -f "$BIN/$t" ]; then
curl -k $TOOLS_SRC/$t > "$BIN/$t"
fi
chmod +x "$BIN/$t"
done
;;
system)
echo "Creating directory $KIT..."
$BIN/busybox mkdir -p $KIT
echo "Creating directory $MNT..."
$BIN/busybox mkdir -p $MNT
echo "Creating directory $BS..."
$BIN/busybox mkdir -p $BS
echo "Creating directory $BB..."
$BIN/busybox mkdir -p $BB
;;
busybox)
echo "Installing Busybox in $BB..."
$BIN/busybox cp $BIN/busybox $BB/busybox
$BB/busybox --install $BB
echo "Done."
;;
debootstrap)
echo "Installing debootstrap in $BS..."
if [ ! -f "data.tar.gz" ]; then
DEST_DEB="$BIN/debootstrap.deb"
if [ ! -f "$DEST_DEB" ]; then
curl -k "$DEBOOSTRAP_SRC" > "$DEST_DEB"
fi
$BIN/busybox ar vx "$DEST_DEB" > /dev/null
fi
$BIN/busybox tar -C $BS -xzvf data.tar.gz > /dev/null
echo "Patching debootstrap to function properly on android..."
$BIN/busybox sed -i "496,498d" $BS/usr/sbin/debootstrap
$BIN/busybox sed -i "12i [ ! -x /debootstrap/debootstrap ] && export DEBOOTSTRAP_DIR=$BS/usr/share/debootstrap" $BS/usr/sbin/debootstrap
$BIN/busybox sed -i "1iPATH=/bin:/usr/bin:/sbin:/usr/sbin:/debootstrap:$BB" $BS/usr/sbin/debootstrap
$BIN/busybox sed -i "2d" $BS/usr/sbin/debootstrap
$BIN/busybox cp $BIN/pkgdetails $BS/usr/share/debootstrap/pkgdetails
$BIN/busybox chmod 777 $BS/usr/share/debootstrap/pkgdetails
echo "Done."
;;
image)
echo "Creating empty $FS_TYPE diskimage..."
$BIN/busybox dd if=/dev/zero of=$DISK_IMG bs=1M count=0 seek=$SIZ
$BIN/mke2fs -t $FS_TYPE -F $DISK_IMG
echo "Done."
;;
setup)
echo "Writing basic config files to debian system..."
echo "deb http://$SRC $DST main" > $MNT/etc/apt/sources.list
echo -e "domain local\nsearch local\n" > $MNT/etc/resolv.conf
echo -e "# DNS Google\nnameserver 8.8.8.8\nnameserver 8.8.4.4\n" >> $MNT/etc/resolv.conf
echo debkit > $MNT/etc/debian_chroot
echo -e "LS_COLORS='rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:';\nexport LS_COLORS\nalias ls='ls --color=auto'" > $MNT/root/.bashrc
echo "Done."
;;
chroot)
clear
echo "Setting up the environment..."
export TMPDIR=/tmp
export USER=root
export HOME=/root
export SHELL=/bin/bash
export TERM=xterm
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin:/usr/local/bin
export LC_ALL=C
export LANGUAGE=C
export LANG=C
echo "chrooting into the debkit..."
$BIN/busybox chroot $MNT /bin/bash
;;
link)
echo "Making /system RW..."
mount -o rw,remount /system
echo "Linking debkit into /system/xbin..."
$BIN/busybox ln -snf $BIN/debkit /system/xbin/debkit
echo "Making /system RO..."
mount -o ro,remount /system
;;
esac
}
mount_sys()
{
case "$1" in
root)
echo "Mounting system onto $MNT..."
if [ $(mount | grep -c " $MNT " || :) -eq 0 ]; then
if [ -b "$BLK_DEVICE" ]; then
remove_block_dev
fi
echo "Creating block device $BLK_DEVICE"
$BIN/busybox mknod $BLK_DEVICE b 7 ${BLK_DEVICE##*loop}
$BIN/busybox losetup $BLK_DEVICE $DISK_IMG
$BIN/busybox mount -t $FS_TYPE -o exec,dev,suid,relatime $BLK_DEVICE $MNT
assert_mount $MNT
touch "$MNT_CHECK"
else
echo "Already mounted: $MNT"
fi
;;
proc)
echo "Mounting proc..."
$BIN/busybox mkdir -p $MNT/proc
$BIN/busybox mount -t proc none $MNT/proc
;;
sys)
echo "Mounting sys..."
$BIN/busybox mkdir -p $MNT/sys
$BIN/busybox mount -t sysfs none $MNT/sys
;;
dev)
echo "Mounting dev..."
$BIN/busybox mkdir -p $MNT/dev $MNT/dev/pts
$BIN/busybox mount -o bind /dev $MNT/dev
$BIN/busybox mount -t devpts none $MNT/dev/pts
;;
esac
}
safe_umount()
{
out=$($BIN/busybox umount "$1" 2>&1)
if [ $(mount | grep -c " $1 " || :) -eq 1 ]; then
echo "Failed to umount $1:\n> $out"
exit 1
fi
}
umount_sys()
{
case "$1" in
dev)
echo "Unmounting dev..."
safe_umount $MNT/dev/pts
safe_umount $MNT/dev
;;
sys)
echo "Unmounting sys..."
safe_umount $MNT/sys
;;
proc)
echo "Unmounting proc..."
safe_umount $MNT/proc
;;
root)
echo "Unmounting $MNT..."
safe_umount $MNT
$BIN/busybox rm -f "$MNT_CHECK"
remove_block_dev
;;
esac
}
remove_block_dev()
{
if [ -b "$BLK_DEVICE" ]; then
$($BIN/busybox losetup -d $BLK_DEVICE > /dev/null 2>&1 || :)
$BIN/busybox rm -f $BLK_DEVICE
fi
}
assert_mount()
{
if [ $(mount | grep -c " $1 " || :) -eq 0 ]; then
echo "Failed to mount $1"
exit 1
fi
}
assert_root_mount()
{
if [ ! -f $MNT_CHECK ]; then
echo "Mount command failed"
exit 1
fi
}
debootstrap_sys()
{
case "$1" in
one)
echo "Going into stage one of debootstrapping the new system..."
FLAGS="--verbose --no-check-gpg --foreign --arch=armhf"
$BS/usr/sbin/debootstrap $FLAGS --extractor=ar $INCLUDE_PACKAGES $DST $MNT http://$SRC
echo "Done."
;;
two)
echo "Going into stage two of debootstrapping the new system..."
$BIN/busybox chroot $MNT /bin/sh -c '/debootstrap/debootstrap --verbose --no-check-gpg --second-stage'
echo "Done."
;;
esac
}
# determine availability of either ext2 or ext4 support
# although ext4 support may be present, chroot doesn't quite work with a mounted ext4 disk
# (fails with "Segmentation fault")
load_fstype
case "$1" in
install)
setup_steps="dependencies system busybox debootstrap image1"
for step in ${setup_steps[@]}; do setup_sys $step; done
## Installing directly to the disk image fails on step two of debootstrap
# mount_sys root
# assert_root_mount
##
debootstrap_sys one
# You may see the following error in the logs. Still works fine. After chroot'ing, update packages with aptitude.
# > W: Failure while configuring base packages. This will be re-attempted up to five times
debootstrap_sys two
setup_sys setup
## We install to $MNT on the native filesystem
## and copy over all files to the mounted disk image
mv $MNT "${MNT}_bk"
mkdir -p $MNT
mount_sys root
assert_root_mount
echo "Copying files..."
$BIN/busybox cp -va ${MNT}_bk/* $MNT/
##
echo "Cleaning up..."
$BIN/busybox rm -rf "${MNT}_bk"
umount_sys root
setup_sys link
echo "Done."
;;
chroot)
assert_root_mount
setup_sys chroot
;;
mount)
mount_sys root
assert_root_mount
for t in dev sys proc; do mount_sys $t; done
;;
umount)
if [ -z "$2" ]; then
for t in dev sys proc root; do umount_sys $t; done
else
umount_sys "$2"
fi
;;
*)
echo "Debkit Debian on Android Installation Kit"
echo "Usage: debkit mount|umount|chroot|install"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment