Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

[GUIDE] [BIN] Busybox 1.23.0 Snapshot and Building On Android (from Ubuntu chroot..)

OP 7175

25th August 2014, 04:35 AM   |  #1  
7175's Avatar
OP Senior Member
Thanks Meter: 393
 
292 posts
Join Date:Joined: Feb 2013
More
About Busybox: busybox.net/about.html
More on Busybox: busybox.net

This is just for anyone who wants to try, and especially those without access to a PC.


:
: --------------- BUILD STEPS --------------
:


Things we'll need besides your phone:
- "Android Terminal Emulator" app
- "Complete Linux Installer" app , I also recommend trying "linux deploy" for more advanced usage
- internet/wifi
- latest "busybox" source


1) We need to get Ubuntu or Debian booted for a sufficient build environment (kali linux works well too). I've used them all on Android but I like the better stocked terminal in the Ubuntu images. I used the app "Complete Linux Installer" which is free and works beautifully, very simple and easy. In the app you want to follow the short instructions to download an Ubuntu image, rename it to ubuntu.img, and place it in a folder named ubuntu in /sdcard. Then hit menu in the app and click the terminal screen icon that says "Launch". An Ubuntu terminal will now open in Android Terminal Emulator. Super quick and easy.

2) Let's download GCC and ncurses-devel (libncurses5-dev) as some crucial build environment tools.
Code:
apt-get install -y gcc build-essential libncurses5-dev libpam0g-dev libsepol1-dev libselinux1-dev
--EDIT-(30AUG2014)--
For Selinux compatibility and loginutils, we need to also download a few extra packages. Already included in the code above.


3) Now the cool thing about this chroot Ubuntu environment is that we still have access to the sdcard to transfer files between Android environment and the chroot jail. Extract your downloaded busybox source to your Ubuntu home with something like:
Code:
cd
tar -xf /sdcard/Download/busybox*bz2
cd busybox*

4) Now we can build busybox statically. The first thing we do is generate a Makefile by running "make" with a "defconfig" (default configuration file) Usually you will run "./configure" with other programs, but busybox compiles more like a kernel, so it uses a defconfig which has a huge checklist of options.

(After successfully compiling busybox, we can go back and customize the .config; this entails that for each "CONFIG ..." line we see, we can uncomment it and mark it "y" or "n" to configure some option... This can be more easily done from a terminal busybox menu interface with "make menuconfig". You just need to crank font down to 7 or use telnet/ssh)
Skip "make defconfig" if you use a customized ".config".
Code:
make defconfig
If all goes well, we now have a Makefile and are ready to compile:
Code:
make clean && make LDFLAGS=-static
Let "make" crank out the binary for a couple minutes. The extra variable we set before make is an environment variable to compile statically. When compiling is complete we'll have a few different busybox binaries at the root of the source directory. We use the one named "busybox".


5) Now let's copy it to /system/usr/bin to install for test usage.
Code:
cp ./busybox /sdcard
(Open a new terminal tab to get into Android Environment)
sysrw
mkdir -p /system/usr/bin
cp -f /sdcard/busybox /system/usr/bin
chmod 0555 /system/usr/bin/busybox
/system/usr/bin/busybox --install -s /system/usr/bin
sysro
PATH+=:/system/usr/bin
.. and done. Run some scripts and enjoy your static busybox!

edit:
**Commands to run if you have compile errors:
Code:
apt-get build-dep busybox
apt-get install -y build-essential
apt-get -f update
dpkg --configure -a

:
: --------------- DOWNLOADS --------------
:


***** Attached is a busybox static binary (v1.23.0 August 2014 snapshot, 374 applets included!, 1.37MB size) *****
Since it's up-to-date it has some nice extras most people haven't seen like a "-I" option for xargs! Yes, that is correct, busybox xargs has its testicles back.
Code:
e.g.
> echo Hello | xargs -I{} echo {} world!

: ---------- UPDATES ----------

-------------------EDIT-2-(30AUG2014)----------------------
Got a Selinux-enabled busybox attached now. This means Selinux flags are integrated into applets like ls, id, ps, tar, etc, and there are now 12 extra Selinux applets to give a total of 386 applets, 1740KB in size. The previous one is more portable, but this one can completely replace toolbox and gives you Selinux control for Android 4.4+. Plus it's pure maxed-out awesomeness.

***I've also attached the .config files for each busybox I've compiled, so anybody can remake them (from their phone!) as I've posted. You just need to download and extract the .config file to the root of your busybox source directory.***


-------------------EDIT-3----------------------
YashdSaraf has made some very useful flash zips to install either the non-selinux- or selinux-enabled busybox 1.23.0 via recovery. Installation replaces the stock busybox in /system/xbin. I've attached the zips he made to the end of this OP.


(**Note: Thought I'd mention that there will be a handful of applets that don't work in "Android" environment such as su. Part of this is because of the way Android's default file structure is an amputated modified version of linux. With almost all of them, slight modifications to environment and file structure can be made to get them to work. This is just normal behaviour of busybox in android. The su and init applets shouldn't be used in Android though. I keep them anyway for completeness of the snapshot build and because they work and are needed for a root.gz initrd or some chroot environments.)


-------------------EDIT-4-(06SEPT2014)----------------------

:
: How to compile against(using) uclibc for a smaller binary!!
:


Download the attached arm-linux-uclibcgnueabi toolchain package that I pre-compiled. Extract to /data/media:
Code:
cd /data/media
zip='/sdcard/Download/2014-09-06__arm-buildroot-linux-uclibcgnueabi.tar.lz.zip'
unzip -op "$zip" | lzip -d | tar -xf -
Then let's open up the "Complete Linux Installer" terminal.
To use the toolchain with a busybox build, we just need to specify the parameter CROSS_COMPILE which is a prefix to the compile tools. So if you extracted the toolchain to /data/media, you will use:
Code:
make clean && make LDFLAGS=-static CROSS_COMPILE=/android/data/media/arm-buildroot-linux-uclibcgnueabi/bin/arm-buildroot-linux-uclibcgnueabi-
When you're done you should have a busybox binary with 374 functions with size around 1.1MB. That's a 20% decrease in size from using the standard glibc toolchain!

**IMPORTANT Notes: Selinux-enabled busybox .config errors out when building using the uclibc toolchain; I think this is a lack of support issue. In the "Complete Linux Installer" app you'll need to add the mount "/data/media" in options. This gives you access to it as "/android/data/media", very very useful for extra space needs.
Last edited by 7175; 12th October 2014 at 09:28 PM. Reason: syntax, update
The Following 8 Users Say Thank You to 7175 For This Useful Post: [ View ]
29th August 2014, 01:27 AM   |  #2  
E:V:A's Avatar
Recognized Developer
Flag -∇ϕ
Thanks Meter: 1,898
 
1,380 posts
Join Date:Joined: Dec 2011
Great Info here!

But I would be interested to know how well this method works on Samsung Stock devices running AOS 4.2 and above? Any experience?
The Following User Says Thank You to E:V:A For This Useful Post: [ View ]
30th August 2014, 03:00 PM   |  #3  
Member
Flag Mumbai
Thanks Meter: 23
 
73 posts
Join Date:Joined: Sep 2013
More
Awesome info, this thread came up #1 while googling busybox 1.23
I made a flashable zip of the attached binary in the op to clean the old one(if any) and install the new busybox in xbin, just in case if anyone needs it.
Attached Files
File Type: zip BusyBox-v1.23.0.zip - [Click for QR Code] (1.12 MB, 91 views)
The Following 5 Users Say Thank You to YashdSaraf For This Useful Post: [ View ]
30th August 2014, 03:59 PM   |  #4  
Hamidreza2010's Avatar
Senior Member
Thanks Meter: 171
 
602 posts
Join Date:Joined: Jan 2013
More
Is it work on xperia sp on 4.3 fw yes ?
30th August 2014, 06:55 PM   |  #5  
-CALIBAN666-'s Avatar
Recognized Contributor
Flag Leverkusen
Thanks Meter: 3,660
 
5,850 posts
Join Date:Joined: Feb 2012
Donate to Me
More
Quote:
Originally Posted by YashdSaraf

Awesome info, this thread came up #1 while googling busybox 1.23
I made a flashable zip of the attached binary in the op to clean the old one(if any) and install the new busybox in xbin, just in case if anyone needs it.

Thanx,worx fine with Carbon 4.4.4 on my LG.

GREETZ FROM TEAM-OPTIMA!!!
31st August 2014, 12:00 AM   |  #6  
7175's Avatar
OP Senior Member
Thanks Meter: 393
 
292 posts
Join Date:Joined: Feb 2013
More
Quote:
Originally Posted by E:V:A

Great Info here!

But I would be interested to know how well this method works on Samsung Stock devices running AOS 4.2 and above? Any experience?

Thanks man. I've been compiling tons of stuff with Debian and Ubuntu chroot no problem on top of 4.3.1 Vanir and also 4.4.4 Carbon, both are my daily drivers. "Complete Linux Installer" is pretty fast compared to some other chroot apps like GNUroot (no offense to GNUroot, it works but is way too slow). It runs real-time compared to non-chroot. When compared to my dual-core 2007 Pentium M laptop, it's about 2-3 times as slow which isn't too bad for compiling something like mksh or even busybox which takes up to 5 mins I'd say.

In terms of binary size, compiling natively is better than cross-compiling it seems. I used gcc with no size optimizations here, so 1.37MB is pretty nice compared to some others around 2MB with full configs. With this method and klcc (gcc wrapper) I got mksh compiled to 192KB. I'm currently trying to build a uclibc toolchain on my laptop that will give me a mksh binary under 300KB..



Quote:
Originally Posted by YashdSaraf

Awesome info, this thread came up #1 while googling busybox 1.23
I made a flashable zip of the attached binary in the op to clean the old one(if any) and install the new busybox in xbin, just in case if anyone needs it.

Cool thanks man! That is really useful, glad to hear from CALIBAN that it works. Could I add this to the OP with credit to you?


Quote:
Originally Posted by Hamidreza2010

Is it work on xperia sp on 4.3 fw yes ?

Yes, xperia sp uses armv7 so you should be good to go.
The Following User Says Thank You to 7175 For This Useful Post: [ View ]
31st August 2014, 01:59 PM   |  #7  
Member
Flag Mumbai
Thanks Meter: 23
 
73 posts
Join Date:Joined: Sep 2013
More
Thumbs up
Quote:
Originally Posted by 7175

Cool thanks man! That is really useful, glad to hear from CALIBAN that it works. Could I add this to the OP with credit to you?

Sure bro go ahead
Edit: Went ahead and made one for selinux enabled busybox , you can add this one in the op too.
Attached Files
File Type: zip BusyBox-v1.23.0(Selinux_Enabled).zip - [Click for QR Code] (1.29 MB, 36 views)
Last edited by YashdSaraf; 31st August 2014 at 04:27 PM.
The Following User Says Thank You to YashdSaraf For This Useful Post: [ View ]
6th September 2014, 09:36 PM   |  #8  
7175's Avatar
OP Senior Member
Thanks Meter: 393
 
292 posts
Join Date:Joined: Feb 2013
More
Hey guys I was able to get an entire uClibc toolchain built the other day (using buildroot). I tested it and it makes some nice small binaries with about 20%+ smaller size than the standard glibc. Man that took hours to compile but was well worth it. It really put the stability of Android OS to the test as well. Kitkat absolutely couldn't finish compiling with multiple oom's and oops's, but Vanir 4.3.1 stuck it out real nice. Once I had the huge amount of required buildroot packages downloaded, I was able to compile in TWRP as well with good stability. (I have the "Complete Linux Installer" startup chroot script aliased in my mkshrc so I can pull up an ubuntu terminal without starting the app. )

So I got 3 new attachments to OP:
- arm-linux-uclibc toolchain for anyone who wants to compile stuff with it (host=arm AND target=arm)
- busybox (374 fcns, non-selinux) 1116KB
- lzip binary (in case you need it to unzip the toolchain, which is highly compressed from 64MB to 14MB with lzip's lzma)



**As I mentioned in the updated OP, I wasn't able to get selinux-enabled busybox compiled with uclibc. This may be something unsupported, or there may be a patch fix. I'll check it out. I'll try with musl libc and musl-gcc as well.
Last edited by 7175; 7th September 2014 at 04:45 AM.
The Following 3 Users Say Thank You to 7175 For This Useful Post: [ View ]
7th September 2014, 03:57 AM   |  #9  
Member
Thanks Meter: 27
 
90 posts
Join Date:Joined: Jul 2013
More
I have another approach, I try aboriginal cross compiler toolchain in archLinux it produced small binary, but I can't compile busybox for android. For Linux it work. Maybe need bionic lib?
The Following User Says Thank You to ndrancs For This Useful Post: [ View ]
7th September 2014, 04:38 AM   |  #10  
7175's Avatar
OP Senior Member
Thanks Meter: 393
 
292 posts
Join Date:Joined: Feb 2013
More
Quote:
Originally Posted by ndrancs

I have another approach, I try aboriginal cross compiler toolchain in archLinux it produced small binary, but I can't compile busybox for android. For Linux it work. Maybe need bionic lib?

Sounds interesting. I honestly haven't given this a try yet, but I'm very interested in taking a look at it. At this point I'm pretty much addicted to making the smallest binaries I can and testing out different toolchains. I'll give it a good search on duckduckgo, and if you have any insightful links that would be great.

Edit: Alright cool I found the source for Aboriginal Linux at landley.net/aboriginal and am building on Android now. I'm also trying this on my x86_64 laptop so that I can compare the differences like I have with glibc, uclibc, musl, klibc binary builds in a native environment and a cross-compile environment.

I see from my laptop's build that a busybox was generated, but it was dynamic and has a libc.so.6 dependency. @ndrancs : this might be what you were talking about. Did you try compiling static? Also see if "make allnoconfig && make clean && make LDFLAGS=-static" works for compiling busybox with Aboriginal Linux.
Last edited by 7175; 7th September 2014 at 06:10 AM.

Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes