jacklul

entware-pi-hole asuswrt-merlin

Feb 3rd, 2026 (edited)
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 8.62 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # THIS IS A DRAFT
  4. # A lot of stuff missing, some code is unfinished and won't work
  5. #
  6. # Notes:
  7. # We are utilizing the following scripts:
  8. # /jffs/scripts/dnsmasq.postconf
  9. # /jffs/scripts/firewall-start
  10. # /jffs/configs/passwd.add
  11. # /jffs/configs/group.add
  12. #
  13. # IDEA: perhaps instead of writing a lot of code to these
  14. # merlin scripts, a simple helper script could be introduced
  15. # and each of mentioned scripts would just call it like:
  16. # /jffs/scripts/pihole_helper.sh dnsmasq_postconf
  17. # /jffs/scripts/pihole_helper.sh firewall_start
  18. # etc...
  19.  
  20. set -e
  21.  
  22. if ! grep -q "^pihole:" /etc/passwd; then
  23.     echo "Adding 'pihole' user and preparting .add configs in /jffs/configs..."
  24.  
  25.     used_uids=$(cut -d ':' -f 3 /etc/passwd)
  26.     next_uid=1501
  27.  
  28.     while true; do
  29.         for test_uid in $used_uids; do
  30.             if [ "$test_uid" -eq "$next_uid" ]; then
  31.                 next_uid=$((next_uid + 1))
  32.                 continue 2
  33.             fi
  34.         done
  35.  
  36.         break;
  37.     done
  38.  
  39.     mkdir -p /jffs/configs
  40.  
  41.     contents="pihole:x:$next_uid:$next_uid::/dev/null:/dev/null"
  42.     echo "$contents" >> /etc/passwd
  43.  
  44.     if ! grep -q "^pihole:" /jffs/configs/passwd.add; then
  45.         echo "$contents" >> /jffs/configs/passwd.add
  46.     fi
  47.  
  48.     contents="pihole:x:$next_uid:"
  49.     echo "$contents" >> /etc/group
  50.  
  51.     if ! grep -q "^pihole:" /jffs/configs/group.add; then
  52.         echo "$contents" >> /jffs/configs/group.add
  53.     fi
  54. fi
  55.  
  56. platform="$(uname -m)"
  57. case "$platform" in
  58.     armv7l)
  59.         repo_url='https://jacklul.github.io/entware-pi-hole/armv7sf-k3.2/'
  60.     ;;
  61.     aarch64)
  62.         repo_url='https://jacklul.github.io/entware-pi-hole/aarch64-k3.10/'
  63.     ;;
  64.     x86_64)
  65.         repo_url='https://jacklul.github.io/entware-pi-hole/x64-k3.2/'
  66.     ;;
  67.     *)
  68.         echo "Unsupported platform: $platform"
  69.         exit 1
  70.     ;;
  71. esac
  72.  
  73. installed="$(/opt/bin/opkg list-installed)"
  74. if ! echo "$installed" | grep -Fq "wget-ssl " || ! echo "$installed" | grep -Fq "ca-certificates "; then
  75.     echo "Installing needed packages to support HTTPS repositories..."
  76.     /opt/bin/opkg update
  77.     /opt/bin/opkg install wget-ssl ca-certificates
  78. fi
  79.  
  80. if ! grep -Fq "/entware-pi-hole/" /opt/etc/opkg.conf; then
  81.     echo "Adding repository '$repo_url' to opkg.conf..."
  82.     echo "src/gz pi-hole $repo_url" >> /opt/etc/opkg.conf
  83. fi
  84.  
  85. echo "Installing Pi-hole's OPKG package..."
  86. /opt/bin/opkg update
  87. /opt/bin/opkg install pi-hole
  88.  
  89. # Installer already installs minimal /opt/etc/pihole/pihole.toml with recommended starting values
  90. # First call to pihole-FTL --config should populate the config with missing variables
  91.  
  92. if [ ! -f /jffs/scripts/dnsmasq.postconf ]; then
  93.     mkdir -p /jffs/scripts
  94.  
  95.     cat <<EOF > /jffs/scripts/dnsmasq.postconf
  96. #!/bin/sh
  97.  
  98. [ -z "\$1" ] && exit 1
  99.  
  100. EOF
  101.     chmod +x /jffs/scripts/dnsmasq.postconf
  102. fi
  103.  
  104. if ! grep -Fq "# entware-pi-hole" /jffs/scripts/dnsmasq.postconf; then
  105.     echo "Installing /jffs/scripts/dnsmasq.postconf script..."
  106.  
  107.     # Perhaps this could be simplified by using helper functions from /usr/sbin/helper.sh?
  108.     cat <<EOF > /jffs/scripts/dnsmasq.postconf
  109. # entware-pi-hole START
  110.  
  111. # Make firmware Dnsmasq listen for DNS on non-default port (we need it for the reverse lookups)
  112. # If this device is not a DHCP server then you may replace 8053 with 0 to disable Dnsmasq's DNS server
  113. if ! grep -q "^port=8053" "\$1"; then
  114.     sed '/^port=/ s/^/#/' -i "\$1"
  115.     sed "/^user=/a port=8053" -i "\$1"
  116. fi
  117.  
  118. # Force firmware dnsmasq to advertise itself as DNS server via DHCP
  119. if ! grep -q "^dhcp-option=lan,6,0.0.0.0" "\$1"; then
  120.     sed '/^dhcp-option=lan,6,/ s/^/#/' -i "\$1"
  121.     sed "/^user=/a dhcp-option=lan,6,0.0.0.0" -i "\$1"
  122. fi
  123.  
  124. # Make sure system uses Pi-hole for DNS requests
  125. # This is optional and disabled (commented) by default.
  126. #resolvconf="\$(readlink -f /etc/resolv.conf)"
  127. #if ! head -n 1 "\$resolvconf" | grep -q "^nameserver 127.0.0.1"; then
  128. #    sed '/127.0.0.1/d' -i "\$resolvconf"
  129. #    sed '1i nameserver 127.0.0.1' -i "\$resolvconf"
  130. #fi
  131.  
  132. # entware-pi-hole END
  133. EOF
  134. fi
  135.  
  136. # Update Pi-hole config
  137. echo "Updating Pi-hole configuration..."
  138.  
  139. # Upstream DNS servers to use
  140. # It would be great to import DNS servers from the router's WAN page here instead
  141. # I believe we could also use "127.0.0.1:8053" here to forward to dnsmasq (and respect router's DNS settings)
  142. # but generally I would recommend to just set target DNS servers here
  143. /opt/bin/pihole-FTL --config dns.upstreams '["1.1.1.1","1.0.0.1"]'
  144.  
  145. # Correct interface to use should be fetched from nvram
  146. /opt/bin/pihole-FTL --config dns.interface br0
  147.  
  148. # Only queries at most one hop away will be accepted
  149. /opt/bin/pihole-FTL --config dns.listeningMode LOCAL
  150.  
  151. # Reverse queries server, could be made optional but without it
  152. # you will proably not be able to resolve LAN names
  153. # '192.168.1.0/24' and 'lan' must be somehow grabbed from the router (nvram?)
  154. # If the device we are installing on has DHCP disabled then we must also
  155. # replace '127.0.0.1#8053' with correct DHCP server IP (and use #53 port)
  156. /opt/bin/pihole-FTL --config dns.revServers "true,192.168.1.0/24,127.0.0.1#8053,lan"
  157.  
  158. # Ports to listen on for the webserver
  159. # '192.168.1.1' - router IP should be fetched from nvram
  160. # '127.0.0.1:5080' is required for Pi-hole scripts to access the API without issues
  161. /opt/bin/pihole-FTL --config webserver.port "127.0.0.1:5080,192.168.1.1:5080,192.168.1.1:5443s"
  162.  
  163. # Disable NTP features
  164. /opt/bin/pihole-FTL --config ntp.ipv4.active false
  165. /opt/bin/pihole-FTL --config ntp.ipv6.active false
  166. /opt/bin/pihole-FTL --config ntp.sync.active false
  167.  
  168. # Situation specific configuration changes:
  169.  
  170. # When user wants to integrate with local Unbound (unbound_manager) - ask user for this?
  171. # https://github.com/jacklul/entware-pi-hole/wiki/Using-Unbound-as-upstream-DNS-server
  172. if [ "$unbound_integration" = true ]; then
  173.     echo "Integrating with local Unbound instance..."
  174.  
  175.     /opt/bin/pihole-FTL --config dns.upstreams "127.0.0.1#53535"
  176. fi
  177.  
  178. # Dedicated interface to run Pi-hole on so that the pi.hole domain can work - ask user for this?
  179. # https://github.com/jacklul/entware-pi-hole/wiki/Using-dedicated-IP-for-the-web-interface
  180. if [ "$extra_ip_setup" = true ]; then
  181.     echo "Adding dedicated interface and IP for Pi-hole (pi.hole domain support)..."
  182.  
  183.     if [ ! -f /jffs/scripts/firewall-start ]; then
  184.         cat <<EOF > /jffs/scripts/firewall-start
  185. #!/bin/sh
  186.  
  187. [ -z "\$1" ] && exit 1
  188.  
  189. EOF
  190.         chmod +x /jffs/scripts/firewall-start
  191.     fi
  192.  
  193.     if ! grep -Fq "# entware-pi-hole" /jffs/scripts/firewall-start; then
  194.         echo "Installing /jffs/scripts/firewall-start script..."
  195.  
  196.         # I assume this is the correct place to make sure the interface is added
  197.         cat <<EOF > /jffs/scripts/firewall-start
  198. # entware-pi-hole START
  199.  
  200. # These should be read dynamically from nvram...
  201. ADDRESS=192.168.1.254/24
  202. INTERFACE=br0
  203.  
  204. if ! ip addr show dev "\$INTERFACE" | grep -q "inet \$ADDRESS "; then
  205.     ip -4 addr add "\$ADDRESS" brd + dev "\$INTERFACE" label "\$INTERFACE:pihole"
  206. fi
  207.  
  208. # entware-pi-hole END
  209. EOF
  210.     fi
  211.  
  212.     # Interface to listen for DNS on, 'br0' should be fetched from nvram depending on situation
  213.     /opt/bin/pihole-FTL --config dns.interface br0:pihole
  214.  
  215.     # Bind to given interface
  216.     /opt/bin/pihole-FTL --config dns.listeningMode BIND
  217.  
  218.     # Force pi.hole domain to have IP we specify
  219.     /opt/bin/pihole-FTL --config dns.reply.host.force4 true
  220.  
  221.     # The IP address to use for pi.hole domain, must equal the one from firewall-start script
  222.     /opt/bin/pihole-FTL --config dns.reply.host.IPv4 "192.168.1.254"
  223.  
  224.     # Webserver listening ports
  225.     # The IP address ('192.168.1.254') must equal the one from firewall-start script
  226.     # 127.0.0.1:5080 is required for Pi-hole scripts to access the API without issues
  227.     /opt/bin/pihole-FTL --config webserver.port "127.0.0.1:5080,192.168.1.254:80,192.168.1.254:443s"
  228.  
  229.     # Alternative - append to the earlier config
  230.     # In case we lose access to pi.hole domain we can access the UI through 192.168.1.1:5080 (or asusrouter.net:5080)
  231.     #current="$(/opt/bin/pihole-FTL --config webserver.port)"
  232.     #/opt/bin/pihole-FTL --config webserver.port "$current,192.168.1.254:80,192.168.1.254:443s"
  233. fi
  234.  
  235. # LAN -> DHCP Server - DNS server fields should be empty
  236. # 'Forward local domain queries to upstream DNS' should be disabled
  237. # or at least prompt the user to make those changed
  238.  
  239. # Apply changes and start the service
  240. echo "Launching Pi-hole..."
  241. service restart_dnsmasq
  242. /opt/etc/init.d/S65pihole-FTL start
  243.  
  244. # Perhaps check with /opt/etc/init.d/S65pihole-FTL check
  245. # and parse 'pihole status' looking for 'FTL is listening on port' and 'blocking is enabled'
  246.  
Advertisement
Add Comment
Please, Sign In to add comment