<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Varun Palekar</title>
    <description>The latest articles on DEV Community by Varun Palekar (@varunpalekar).</description>
    <link>https://dev.to/varunpalekar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1704336%2Fc7b32a7d-cce9-4aaf-acbd-3d52533ec363.jpeg</url>
      <title>DEV Community: Varun Palekar</title>
      <link>https://dev.to/varunpalekar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/varunpalekar"/>
    <language>en</language>
    <item>
      <title>Use Android Phone as advance router using OpenWrt</title>
      <dc:creator>Varun Palekar</dc:creator>
      <pubDate>Sat, 13 Jul 2024 10:40:32 +0000</pubDate>
      <link>https://dev.to/varunpalekar/use-android-phone-as-advance-router-using-openwrt-26hf</link>
      <guid>https://dev.to/varunpalekar/use-android-phone-as-advance-router-using-openwrt-26hf</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;This project began with the idea of running OpenWrt with a 5G modem in the most affordable way possible.&lt;/p&gt;

&lt;h1&gt;
  
  
  Background
&lt;/h1&gt;

&lt;p&gt;As a software engineer specializing in Platform Engineering, I sought an advanced router solution for home use. Previously I already had a intel NUC which I used to run OpenWRT router for home network.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7rnnp06rzvrtn6dfafwi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7rnnp06rzvrtn6dfafwi.png" alt="Image description" width="661" height="496"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Power Consumption:
&lt;/h1&gt;

&lt;p&gt;After observing a power consumption of approximately 14 watts during normal use, I decided to explore ARM alternatives.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy0yy4x6qh0q5n6vyp905.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy0yy4x6qh0q5n6vyp905.png" alt="Image description" width="781" height="581"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Research and Alternatives
&lt;/h1&gt;

&lt;p&gt;I initially researched alternatives like the Raspberry Pi 4B and 5 models. I also considered using mobile operator internet, as I have a free 5G trial from my provider.&lt;/p&gt;

&lt;h1&gt;
  
  
  Cost Considerations:
&lt;/h1&gt;

&lt;p&gt;I found 5G modems for the Raspberry Pi to be too expensive, leading me to consider using android mobile to run OpenWrt.&lt;/p&gt;

&lt;h1&gt;
  
  
  Project Steps
&lt;/h1&gt;

&lt;p&gt;This led me to the idea of running OpenWrt on an Android phone to save the cost of a Raspberry Pi. Below are some options of running OpenWrt on Android&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Directly run OpenWrt on Android
&lt;/h2&gt;

&lt;p&gt;This is exceedingly difficult, as you need to rebuild openwrt for your phone and need to figure out all drivers and other things.&lt;br&gt;
I have dropped this idea as need to spend lots of time figuring it out.&lt;/p&gt;
&lt;h2&gt;
  
  
  2. Running OpenWrt on Android OS as Docker container (or maybe lxc)
&lt;/h2&gt;

&lt;p&gt;There are two main prerequisites:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;A custom kernel build is required to enable features not available by default on Android. So, it is only possible for those phones which provide source code of their kernel or any third-party Android OS like lineageos if available for your phone. Detailed steps are already share by someone else in this post &lt;a href="https://gist.github.com/FreddieOliveira/efe850df7ff3951cb62d74bd770dce27" rel="noopener noreferrer"&gt;https://gist.github.com/FreddieOliveira/efe850df7ff3951cb62d74bd770dce27&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Need to have root access, this is very easy if your already figure out above steps&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For detailed instructions OpenWrt already maintained a guide given below.&lt;br&gt;
Note: I tried the latest available version 23.05.3 is not working according to the given setup. Procd hangs with /sbin/init script, which can be due to multiple reasons, if someone interested can get into &lt;a href="https://openwrt.org/docs/techref/init.detail.cc" rel="noopener noreferrer"&gt;https://openwrt.org/docs/techref/init.detail.cc&lt;/a&gt; at step 2, it get hanged.&lt;/p&gt;
&lt;h2&gt;
  
  
  3. Running OpenWrt on Android OS in QEMU
&lt;/h2&gt;
&lt;h3&gt;
  
  
  3.1 QEMU with KVM
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;This process is currently complicated as KVM is not available on most phones. So, there are two options which I can get
Using official kvm which is only available for Google Pixel 6+ from android 13. So we don't know when it will be available for other&lt;/li&gt;
&lt;li&gt;Build own android kernel with KVM support.
This is also prety much similar to Running OpenWrt on Android OS as Docker container, where need to build kernel with kvm module. I search the internet but didn't find single solution which works for large set of android models&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  3.2 QEMU without any hardware virtualization
&lt;/h3&gt;

&lt;p&gt;Pre-requisite:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Root phone&lt;/li&gt;
&lt;li&gt;termux installed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is the easiest and most compatible way to run openwrt or any OS. The only problem is slow performance as compared to other solutions.&lt;br&gt;
I will describe it in detail below how I tried it on Oneplus 9R Android Phone.&lt;/p&gt;
&lt;h2&gt;
  
  
  4. Running OpenWrt component directly in Android OS (in progress)
&lt;/h2&gt;

&lt;p&gt;Initially I started to try to run using proot avaialble in termux but not able to run it. When I run /sbin/init it gets hanged, after looking to code looks like it hanged at Step 2 of &lt;a href="https://openwrt.org/docs/techref/init.detail.cc" rel="noopener noreferrer"&gt;https://openwrt.org/docs/techref/init.detail.cc&lt;/a&gt; as procd want to acquire pid 1 which is not possible (still my guess needs to look to code of it). &lt;/p&gt;
&lt;h2&gt;
  
  
  5. Running OpenWrt in Limbo EMU (in progress)
&lt;/h2&gt;

&lt;p&gt;Running OpenWrt on Limbo EMU is faster in performance as compared to qemu in termux. I already tried Limbo x86 with OpenWrt x86 which is running smoothly with greater performance, but I am not able to use bridge and tap networking.&lt;br&gt;
My android phone is root but Limbo EMU is not able to get root access for tap and bridging. If someone can get it working, please let me know, I am also curious.&lt;/p&gt;
&lt;h1&gt;
  
  
  QEMU without any hardware virtualization
&lt;/h1&gt;

&lt;p&gt;In this we will explain the process of running OpenWrt on android phone. I have tested the solution on OnePlus 9R (android 13) which is rooted, but we generally using general android adb command/features which are available in all phones, please let me know if you have any problems/questions.&lt;/p&gt;
&lt;h2&gt;
  
  
  1. Pre-requisite:
&lt;/h2&gt;

&lt;p&gt;1.1 Rooted phone (su access)&lt;br&gt;
   1.2 termux installed&lt;br&gt;
   1.3 a system with adb connectivity with phone for shell access&lt;/p&gt;
&lt;h2&gt;
  
  
  2. Selection of networks
&lt;/h2&gt;
&lt;h3&gt;
  
  
  2.1. Internet Source (WAN)
&lt;/h3&gt;

&lt;p&gt;We need to find the source of WAN which will be used to get the internet. It can be your mobile operator network or can also be WIFI or can also be from ethernet connected to your android Phone. &lt;br&gt;
Phone just need to connect to internet via any means, it will automatically be used by OpenWrt, no extra configuration needed.&lt;/p&gt;
&lt;h3&gt;
  
  
  2.2. Internet sharing (LAN)
&lt;/h3&gt;

&lt;p&gt;This is the network which others used to connect to your Phone. We can use android existing personal hotspot also for this.&lt;br&gt;
To achieve this open Android hotspot setting in Android and set all setting required. Enable hotspot and disable it (why disable? we will enable it using cli later)&lt;/p&gt;
&lt;h4&gt;
  
  
  2.1.1. Now open termux
&lt;/h4&gt;
&lt;h4&gt;
  
  
  2.1.2. Check it required setting for hotspot by &lt;code&gt;cat /data/vendor/wifi/hostapd/hostapd_wlan1.conf&lt;/code&gt;
&lt;/h4&gt;
&lt;h4&gt;
  
  
  2.1.3. Turn on hotspot by CLI command &lt;code&gt;Note: If OS get restart, please do again&lt;/code&gt;
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pkg install iw -y
unset LD_PRELOAD
# Created a virtual managed interface for hotspot
sudo iw phy phy0 interface add wlan1 type managed
# Run hotspot. Don't connect your phone now as they will not able to get IP and internet
sudo /vendor/bin/hw/hostapd -dd -g /data/vendor/wifi/hostapd/global -B /data/vendor/wifi/hostapd/hostapd_wlan1.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Note: Some android phones are capable of provide hotspot and connect to other wifi network simultaneously. In this example I am using 5 ghz (Wifi 6 mode) for hotspot (For LAN) and 2.4 ghz to connect internet (For WAN). So please use your setting according to network technology needed for LAN and WAN. &lt;/p&gt;
&lt;h3&gt;
  
  
  2.3 Install Qemu
&lt;/h3&gt;
&lt;h4&gt;
  
  
  2.3.1 Use adb to access termux from adb (optional)
&lt;/h4&gt;

&lt;p&gt;As operating termux from mobile is not friendly as compared to from big screen computer, so below are some steps to run termux from computer.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open termux app on android&lt;/li&gt;
&lt;li&gt;Run following command to install ssh server
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pkg update
pkg install openssh -y
# Set password for ssh
passwd
# run ssh server in background
sshd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Now connect from laptop after having adb connect (connect android phone from system and enable usb debugging)
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Forward android 
adb forward tcp:8022 tcp:8022
#ssh connect to android
ssh user@localhost -p 8022
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now you don't need to type commands in termux&lt;/p&gt;
&lt;h4&gt;
  
  
  2.3.2 Install qemu and other required software/files
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pkg install qemu-system-aarch64-headless qemu-utils wget curl tsu vim iw -y
mkdir openwrt
cd openwrt
# Download openwrt kernel
wget https://downloads.openwrt.org/releases/23.05.3/targets/armsr/armv8/openwrt-23.05.3-armsr-armv8-generic-kernel.bin -O openwrt-23-kernel
# Download openwrt root image
wget https://downloads.openwrt.org/releases/23.05.3/targets/armsr/armv8/openwrt-23.05.3-armsr-armv8-generic-squashfs-rootfs.img.gz -O openwrt-23-root.img.gz
gunzip -d openwrt-23-root.img.gz

# Convert and Resize root img
qemu-img convert -f raw -O qcow2 openwrt-23-root.img openwrt-23-root.qcow2
rm openwrt-23-root.img
qemu-img resize openwrt-23-root.qcow2 1G # replace 1G with your value
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  2.3.3 Setup network bridge, so that OpenWrt access and manage personal android hotspot.
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Create a file &lt;code&gt;/data/data/com.termux/files/usr/etc/qemu/bridge.conf&lt;/code&gt; with following entry
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;allow br0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Create bridge network for sharing. &lt;code&gt;Note: If OS get restart, please do again&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ip link add br0 type bridge
sudo ip link set dev wlan1 master br0
sudo ip link set dev br0 up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  2.3.4 Run Openwrt
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Qemu Start
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo qemu-system-aarch64 -M virt -cpu cortex-a76 -accel tcg,thread=multi -m 256 -smp 4 -nographic -kernel openwrt-23-kernel -drive if=none,file=openwrt-23-root.qcow2,id=hd0 -device virtio-blk-device,drive=hd0 -append "root=/dev/vda" -device virtio-net,netdev=eth0,mac=92:e1:8f:69:43:b4 -netdev bridge,br=br0,id=eth0 -device virtio-net,netdev=eth1 -netdev user,id=eth1,net=192.0.2.0/24,dns=8.8.8.8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Wait for system to boot (takes 2-5 min), when you see 8021q: adding VLAN 0 to HW filter on device eth1, then press enter
Check all networking is setup by ifconfig
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ifconfig
br-lan    Link encap:Ethernet  HWaddr 92:E1:8F:69:43:B4
          inet addr:192.168.1.1  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fd52:7f52:bb2b::1/60 Scope:Global
          inet6 addr: fe80::90e1:8fff:fe69:43b4/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4 errors:0 dropped:0 overruns:0 frame:0
          TX packets:32 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:240 (240.0 B)  TX bytes:4384 (4.2 KiB)

eth0      Link encap:Ethernet  HWaddr 92:E1:8F:69:43:B4
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:11 errors:0 dropped:0 overruns:0 frame:0
          TX packets:41 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:866 (866.0 B)  TX bytes:7056 (6.8 KiB)

eth1      Link encap:Ethernet  HWaddr 52:54:00:12:34:56
          inet addr:192.0.2.15  Bcast:192.0.2.255  Mask:255.255.255.0
          inet6 addr: fec0::5054:ff:fe12:3456/64 Scope:Site
          inet6 addr: fe80::5054:ff:fe12:3456/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:176 errors:0 dropped:0 overruns:0 frame:0
          TX packets:198 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:19852 (19.3 KiB)  TX bytes:17786 (17.3 KiB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:212 errors:0 dropped:0 overruns:0 frame:0
          TX packets:212 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Configure dns forwarding
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;uci add_list dhcp.@dnsmasq[0].server="8.8.8.8"
uci add_list dhcp.@dnsmasq[0].server="8.8.4.4"
uci commit dhcp
service dnsmasq restart
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Now that is all, please connect to Wifi personal hotspot, hopefully internet will be running.
&lt;/h2&gt;
&lt;h4&gt;
  
  
  2.3.5 Configure QEMU for internet speed test (optional)
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;opkg update
opkg install iperf3
iperf3 -s -f M
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now on your connected system run iperf3 for testing&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;iperf3 -f m -c 192.168.1.1 -n 10240M

# When screen off
[  5] 459.00-460.00 sec  15.0 MBytes   126 Mbits/sec    0   2.00 MBytes   57ms
[  5] 460.00-461.00 sec  15.3 MBytes   128 Mbits/sec    0   2.00 MBytes   68ms
[  5] 461.00-462.01 sec  14.4 MBytes   119 Mbits/sec    0   2.00 MBytes   64ms
[  5] 462.01-463.00 sec  15.3 MBytes   130 Mbits/sec    0   2.00 MBytes   59ms
[  5] 463.00-464.00 sec  14.3 MBytes   120 Mbits/sec    0   2.00 MBytes   60ms
[  5] 464.00-465.00 sec  15.6 MBytes   131 Mbits/sec    0   2.00 MBytes   53ms
[  5] 465.00-466.00 sec  15.0 MBytes   126 Mbits/sec    0   2.00 MBytes   54ms
[  5] 466.00-467.00 sec  15.0 MBytes   126 Mbits/sec    0   2.00 MBytes   57m

# when screen On
[  5]   3.00-4.00   sec  44.4 MBytes   372 Mbits/sec    0   2.00 MBytes   14ms
[  5]   4.00-5.00   sec  41.4 MBytes   348 Mbits/sec    0   2.00 MBytes   31ms
[  5]   5.00-6.00   sec  39.3 MBytes   329 Mbits/sec    0   2.00 MBytes   16ms
[  5]   6.00-7.00   sec  40.9 MBytes   343 Mbits/sec    0   2.00 MBytes   22ms
[  5]   7.00-8.00   sec  40.6 MBytes   340 Mbits/sec    0   2.00 MBytes   23ms
[  5]   8.00-9.00   sec  39.7 MBytes   333 Mbits/sec    0   2.00 MBytes   17ms
[  5]   9.00-10.00  sec  39.2 MBytes   329 Mbits/sec    0   2.00 MBytes   15ms
[  5]  10.00-11.00  sec  41.5 MBytes   348 Mbits/sec    0   2.00 MBytes   44ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2.3.6 Use USB connected to android in OpenWrt/Qemu
&lt;/h4&gt;

&lt;p&gt;You can directly mount USB connected to android Phone in Qemu, just add following command according to your necessity in qemu &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Get productid and vendor id from lsusb 
If you don't get enough information from lsusb on android please search the product name in folder /sys/bus/usb/drivers/usb/*/ , in these folders you will find all required information which is generally shown in lsusb on linux. &lt;/li&gt;
&lt;li&gt;Add extra argument in QEMU command as below to share get USB in QEMU
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   -usb -device qemu-xhci,id=xhci -device usb-host,vendorid=0x0bda,productid=0x8153,bus=xhci.0,id=dev01
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;FAQs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;IP not getting when connect to Android Hotspot&lt;/p&gt;

&lt;p&gt;This generally happens when dhcp server of OpenWrt is not running. Please check 67/UDP port is listing or not. For more details please go to &lt;a href="https://openwrt.org/docs/guide-user/base-system/dhcp" rel="noopener noreferrer"&gt;https://openwrt.org/docs/guide-user/base-system/dhcp&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;#openwrt&lt;/p&gt;

</description>
      <category>openwrt</category>
      <category>android</category>
      <category>virtualmachine</category>
      <category>networking</category>
    </item>
  </channel>
</rss>
