Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for OpenWRT #169

Closed
safwanrahman opened this issue May 17, 2020 · 26 comments · Fixed by #401
Closed

Add support for OpenWRT #169

safwanrahman opened this issue May 17, 2020 · 26 comments · Fixed by #401

Comments

@safwanrahman
Copy link

Thanks a lot for the nice and useful package. As OpenWRT is also a linux distribution, is it possible to build a binary for OpenWRT?

@imsnif
Copy link
Owner

imsnif commented May 17, 2020

Sure, glad you like it :)

Does the generic binary in the releases work on OpenWRT?

@safwanrahman
Copy link
Author

@imsnif The generic binary raises following error when I try to run it.

root@OpenWrt:/# ./bandwhich 
./bandwhich: line 1: syntax error: unexpected ")"

@imsnif
Copy link
Owner

imsnif commented May 20, 2020

Wow, that's really odd!

I don't know a lot about OpenWRT, but are you able to run other musl binaries normally? Could you walk me through the process you did from downloading the binary until running? Maybe there's a clue there?

@safwanrahman
Copy link
Author

@imsnif I actually never run musl binary directly in OpenWRT. I will check more about running musl binary in OpenWRT.
I have downloaded the binary to my machine, copied it to OpenWRT then run chmod +X bandwhich and try to execute the binary with ./bandwhich.

Can you let me know where the building code reside?
Maybe this will be helpful: https://openwrt.org/docs/guide-developer/crosscompile

@Alcaro
Copy link

Alcaro commented May 20, 2020

Sounds to me like your OpenWRT is installed on a non-x86 architecture and can't run x86_64 binaries. If execve() returns ENOEXEC (An executable is not in a recognized format, is for the wrong architecture, or has some other format error that means it cannot be executed.), the shell falls back to /bin/sh, which prints a ridiculous error.

To fix, compile for the correct architecture, not x86_64. To find which architecture you're on, use uname -m, -p, or -i (all three print x86_64 for me, I don't know what the difference is).

@safwanrahman
Copy link
Author

@Alcaro I am using Linksys WRT3200ACM which seems like using arm processor.

uname -m print like following

root@OpenWrt:/# uname -m
armv7l

@imsnif
Copy link
Owner

imsnif commented May 20, 2020

@safwanrahman - I think your best bet in this case, as @Alcaro mentions, would be to compile the source with cargo (directly on the openwrt machine), as described here: https://github.com/imsnif/bandwhich#other-linux-flavours

@safwanrahman
Copy link
Author

@imsnif Thanks. Can you guide me how you are building currently? As I need to build it in my host machine for ARM then transfer it to the router.

@Alcaro
Copy link

Alcaro commented May 20, 2020

Compiling anything needs a lot of RAM, CPU and storage - resources which are typically not abundant on routers.

I think your best bet would be installing Cargo on a PC and telling it to cross compile. Googling 'rust openwrt' returns a few relevant-looking things, or you can try 'rust cross compile armv7l'.

Alternatively, you may be able to install an emulated armv7l environment on your PC, using (for example) QEMU. Emulating an ARM is slower than a native cross compiler, but may be easier to set up. Or maybe it's harder - I don't know how easy Rust is to cross compile.

@imsnif
Copy link
Owner

imsnif commented May 20, 2020

Thanks very much for your input @Alcaro! I think the idea of running bandwhich on OpenWRT is quite interesting. So @safwanrahman - if you get it to work, I would very much like to hear more. Maybe we could make it easier for others wanting to do this.

@safwanrahman
Copy link
Author

@Alcaro Thanks for the suggestion. I have very less idea about rust toolchain as well as OpenWRT toolchain. I will try to find out how to fix it. Thanks for your suggestion.

@imsnif Can you let me know how do you build using cargo?

@imsnif
Copy link
Owner

imsnif commented May 20, 2020

I do a cargo build --release --target x86_64-unknown-linux-musl --locked on the source in order to build the musl binary, and a plain cargo build in order to build for the current machine.

Doing some searching, this seems like a good place to start: https://github.com/japaric/rust-cross

Please feel free to reach out if you're having trouble.

@TheLostLambda
Copy link
Collaborator

TheLostLambda commented May 20, 2020

@imsnif The generic binary raises following error when I try to run it.

root@OpenWrt:/# ./bandwhich 
./bandwhich: line 1: syntax error: unexpected ")"

Hi! Sorry to jump in out of nowhere, but could you run file bandwhich or cat bandwhich on that executable? I suspect that if you downloaded things with curl, it might have ignored a redirect or something. That's certainly a shot in the dark, but should be easier to check and might save you from cross-compiling.

EDIT: Whoops, I totally missed the different CPU architecture, you'll likely still need to cross compile. Sorry about that!

@safwanrahman
Copy link
Author

@TheLostLambda Thanks for the concern! I have run cat bandwhich which seems like a binary.

@safwanrahman
Copy link
Author

@Alcaro @imsnif Do you have any idea which linker should I use?

@safwanrahman
Copy link
Author

Seems like not possible at this moment because of rust bug rust-lang/rust#37507

@safwanrahman
Copy link
Author

So after working a lot the night and day, Finally I have got a suggestion of my fellow friend @mominul to use rust-embedded/cross to cross compile for OpenWRT machine and it worked! 🥳 🚀

At first I compiled with mips-unknown-linux-musl target, but it did not work. But later I have read about the armv7 processor and compiled with armv7-unknown-linux-musleabihf target and It works like charm! 🎉🚀

@imsnif
Copy link
Owner

imsnif commented May 21, 2020

I'm really happy to hear that, @safwanrahman - great work!

Would you be willing to create a pull request to add these instructions to the README? I think it will be really helpful to others wanting to do the same. Either that or if you prefer, you can write down the commands you used here and I'll add it.

@safwanrahman
Copy link
Author

safwanrahman commented May 21, 2020

@imsnif Sure, I am writing the process below. I am currently trying to figure out to cross compile for the targets so that it can be installed by the Opkg Package Manager.
I have done following:

  • Check the processor of my router as suggested by @Alcaro by using uname -m
  • Clone the repository
  • Install cross using cargo install cross
  • build the bandwhich package using cross build --target armv7-unknown-linux-musleabihf
  • Copy the binary files from target/armv7-unknown-linux-musleabihf/debug/bandwhich to the router using scp by running scp bandwhich root@192.168.1.1:~/
  • And finally enter the router using ssh and run the binary directly with ./bandwhich

@imsnif Can you arrange the instruction and write it down in the README? I can not figure out how to put it in there.

BTW, thanks a lot @Alcaro for pointing the architecture issue. I would have never found it if you did not mention in the earlier comment.

@imsnif
Copy link
Owner

imsnif commented May 21, 2020

Thank you very much @safwanrahman! I'll write it up in the README in the next few days.

@imsnif
Copy link
Owner

imsnif commented May 23, 2020

I added these instructions to the readme. Thanks again @safwanrahman for sharing these!

@imsnif imsnif closed this as completed May 23, 2020
@smorimoto
Copy link
Contributor

Oh, I just noticed this. I don't know why you were mentioning me, but I'm glad that this seems to have been closed.

@smorimoto
Copy link
Contributor

By the way, since I don't use macOS anymore and use Windows (WSL2) and Void Linux, I will mainly update the Void Linux's package repository. Of course, it's not impossible for me to maintain Homebrew one, so please let me know if you need it.

@safwanrahman
Copy link
Author

@imbsky Sorry for mentioning you! Github suggestion suggested your username when I tried to mention @imsnif and I did not notice it!

@smorimoto
Copy link
Contributor

Oh, I see. No worries 🙂

@cyqsimon
Copy link
Collaborator

Since we're doing cross-compile stuff in #401, we might as well revisit this and add prebuilt binaries for armv7 targets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants