Add support to build an ext4 image from a filesystem
This is suitable for passing to Qemu with -hda which makes it appear
as a disk to a Qemu instance. Once it appears, one can chroot into it.
Note:
- Device need not be connected, this option is only used to prepare
a standalone image.
- BCC isn't built but is just cloned into the image.
- Other options such as --buildtar are ignored.
Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
diff --git a/README.md b/README.md
index 3dbc035..d7d3e85 100644
--- a/README.md
+++ b/README.md
@@ -124,3 +124,10 @@
```
androdeb prepare --archive /path/androdeb-fs.tgz
```
+
+### Build a standalone raw EXT4 image out of the FS:
+```
+androdeb prepare --buildimage /path/to/image.img
+```
+This can then be passed to Qemu as -hda. Note: This option doesn't need a
+device connected.
diff --git a/androdeb b/androdeb
index 9645f81..eca1de0 100755
--- a/androdeb
+++ b/androdeb
@@ -47,6 +47,7 @@
--tempdir) TDIR="$2"; shift || true; shift || true; ;;
--buildtar) TARDIR="$2"; shift || true; shift || true; ;;
--device|-s) ADB="$ADB -s $2"; shift || true; shift || true; ;;
+ --build-image) BUILD_IMAGE=$2; shift || true; shift || true; ;;
--debug) set -x; shift || true; ;;
*) echo "Unknown option ($1)"; usage; ;;
esac
@@ -60,17 +61,26 @@
exit 0
fi
-if [ ! -z "$PREPARE" ] && [ -z "$DOWNLOAD" ] && [ -z "$TARF" ] && [ -z "$PACKAGES" ] && [ -z "$KERNELSRC" ] && [ -z "$KERNELHDRS" ]; then
+if [ -z "$PACKAGES" ]; then
+ echo "No packages specified, so I'm going to do a --fullbuild"
+ config_full_build
+fi
+
+if [ ! -z "$PREPARE" ] && [ -z "$DOWNLOAD" ] && [ -z "$TARF" ] &&
+ [ -z "$KERNELSRC" ] && [ -z "$KERNELHDRS" ] &&
+ [ -z "$BUILD_IMAGE" ]; then
echo "Need to specifify something to prepare, or try: ./andrdeb prepare --download"; usage; fi
if [[ ! -z ${TARDIR+x} ]] && [[ ! -d $TARDIR ]]; then die 7 "Tar dir specified doesn't exist"; fi
+if [ -z "$BUILD_IMAGE" ]; then
do_adb_root "$ADB" || die 3 "adb root failed, make sure:
- If multiple devices connected, provide --device <serialno> (or -s <serialno>)
- Try to run \"adb root\" manually and see if it works. Typically this needs a userdebug build.
Note: adb can be typically obtained using the android-tools-adb or the adb
packages on your distro, or by installing the Android SDK."
+fi
if [ ! -z "$REMOVE" ]; then
die_if_no_androdeb "Nothing to remove."
@@ -154,7 +164,7 @@
echo "Building updating kernel headers from supplied tar.gz ($KERNELHDRS)"
# Is header tar gz update the only thing left to do?
- if [[ -z "$PACKAGES" ]] && [[ -z "$TARF" ]] && [[ -z "$KERNELSRC" ]] ; then
+ if [[ -z "$PACKAGES" ]] && [[ -z "$TARF" ]] && [[ -z "$BUILD_IMAGE" ]] && [[ -z "$KERNELSRC" ]] ; then
push_unpack_tarred_headers $KERNELHDRS; do_cleanup; all_done_banner; exit 0; fi
tar -xvf $KERNELHDRS -C $OUT_TMP/ > /dev/null
@@ -166,7 +176,7 @@
$spath/bcc/build-kheaders-targz.sh ${KERNELSRC} $TDIR_ABS/kh.tgz > /dev/null
# Is header update the only thing left to do?
- if [[ -z "$PACKAGES" ]] && [[ -z "$TARF" ]]; then
+ if [[ -z "$PACKAGES" ]] && [[ -z "$BUILD_IMAGE" ]] && [[ -z "$TARF" ]]; then
push_unpack_headers; do_cleanup; all_done_banner; exit 0; fi
mkdir $OUT_TMP/kernel-headers
@@ -192,7 +202,19 @@
if [[ $EUID -ne 0 ]]; then echo "The next stage runs as sudo, please enter password if asked."; fi
-sudo $spath/buildstrap $ARCH $DISTRO $TDIR $OUT_TMP "$(make_csv "$PACKAGES")" $INSTALL_BCC
+SKIP_COMPRESS=0; if [ ! -z "$BUILD_IMAGE" ]; then SKIP_COMPRESS=1; fi
+
+sudo $spath/buildstrap $ARCH $DISTRO $TDIR $OUT_TMP "$(make_csv "$PACKAGES")" $INSTALL_BCC $SKIP_COMPRESS
+
+# If we only wanted to prepare a rootfs and don't have
+# a device connected, then just echo that and skip cleanup
+if [ ! -z "$BUILD_IMAGE" ]; then
+ echo "For --build-image, only the image is built"
+ echo "Note that BCC will not be built on the image, you've to do it yourself"
+ sudo $spath/buildimage $OUT_TMP $(dirname $BUILD_IMAGE)/$(basename $BUILD_IMAGE)
+ sudo chmod a+rw $(dirname $BUILD_IMAGE)/$(basename $BUILD_IMAGE)
+ do_cleanup; echo "Your .img has been built! Enjoy!"; exit 0
+fi
# Push tar to device and start unpack
$ADB shell mkdir -p /data/androdeb/
diff --git a/buildimage b/buildimage
new file mode 100755
index 0000000..ccdc65d
--- /dev/null
+++ b/buildimage
@@ -0,0 +1,11 @@
+#!/bin/bash
+set -e
+
+# Given a path, make an ext4 image out of it, store it in $2
+dd if=/dev/zero of=$2 bs=4k count=$((256 * 1024 * 2))
+mkfs.ext4 $2
+
+OUT=`mktemp -d`
+mount -o loop $2 $OUT/
+rsync -ra $1/ $OUT/
+umount $OUT/
diff --git a/buildstrap b/buildstrap
index 881dcbc..e6b7679 100755
--- a/buildstrap
+++ b/buildstrap
@@ -10,6 +10,7 @@
OUT_TMP=$4
PACKAGES=$5
INSTALL_BCC=$6
+SKIP_COMPRESS=$7
time qemu-debootstrap --arch $ARCH --include=$(make_csv "$PACKAGES") \
$DISTRO $OUT_TMP http://deb.debian.org/debian/
@@ -41,9 +42,12 @@
git clone https://github.com/iovisor/bcc.git $TDIR/debian/bcc-master
cp $spath/bcc/build-bcc.sh $TDIR/debian/bcc-master/; fi
+# Should be really do this?
+chmod -R 0777 $TDIR/
+
+if [ $SKIP_COMPRESS -eq 1 ]; then exit 0; fi
+
echo "Compressing new filesystem to prepare to push to Android /data/androdeb/"
tar -zcf $TDIR/deb.tar.gz -C $TDIR debian
-chmod -R 0777 $TDIR/
-
chmod 0777 $TDIR/deb.tar.gz
diff --git a/utils/banners b/utils/banners
index 14ae5dc..342e48a 100755
--- a/utils/banners
+++ b/utils/banners
@@ -21,6 +21,7 @@
echo ""
echo " --download Download full FS archive from web (overrides all tools specified)"
echo " --archive Use archive for root fs (overrides all other prepare options)"
+ echo " --build-image Build an ext4 .img with the base image and BCC (useful for Qemu)"
echo ""
echo " --bcc Build and install BCC from source"
echo " --kernelsrc Extract kernel headers for BCC from here"