Skip to content

Instantly share code, notes, and snippets.

@hkfuertes
Last active August 6, 2024 20:28
Show Gist options
  • Save hkfuertes/a37776f4e94de34cc3a4658b15ec1545 to your computer and use it in GitHub Desktop.
Save hkfuertes/a37776f4e94de34cc3a4658b15ec1545 to your computer and use it in GitHub Desktop.

Simple steps to use with https://firmware-selector.openwrt.org/ to generate an OpenWrt image for Raspberrypi with Wifi enabled and network over usb (zeros, 4 and 3a).

Packages:

... kmod-usb-gadget kmod-usb-gadget-eth kmod-usb-dwc2 parted losetup resize2fs

uci-defaults:

# Change to not default network
uci set network.lan.ipaddr="192.168.2.1"
uci commit network

# Enable Wifi
uci set wireless.@wifi-device[0].disabled='0'
uci set wireless.@wifi-iface[0].disabled='0'
uci set wireless.@wifi-iface[0].encryption='open'
uci set wireless.@wifi-iface[0].ssid="OpenWrt"
uci commit wireless

# Enable usb0
grep -qxF 'dtoverlay=dwc2' /boot/config.txt || echo 'dtoverlay=dwc2' >> /boot/config.txt
echo "modprobe g_ether" > /etc/rc.local
uci add_list network.@device[0].ports='usb0' 
uci add_list network.@device[0].ports='eth0'
uci commit network

# Expand ROOTFS
cat << "EOF" > /etc/uci-defaults/70-rootpt-resize
if [ ! -e /etc/rootpt-resize ] \
&& type parted > /dev/null \
&& lock -n /var/lock/root-resize
then
ROOT_BLK="$(readlink -f /sys/dev/block/"$(awk -e \
'$9=="/dev/root"{print $3}' /proc/self/mountinfo)")"
ROOT_DISK="/dev/$(basename "${ROOT_BLK%/*}")"
ROOT_PART="${ROOT_BLK##*[^0-9]}"
parted -f -s "${ROOT_DISK}" \
resizepart "${ROOT_PART}" 100%
mount_root done
touch /etc/rootpt-resize
reboot
fi
exit 1
EOF
cat << "EOF" > /etc/uci-defaults/80-rootfs-resize
if [ ! -e /etc/rootfs-resize ] \
&& [ -e /etc/rootpt-resize ] \
&& type losetup > /dev/null \
&& type resize2fs > /dev/null \
&& lock -n /var/lock/root-resize
then
ROOT_BLK="$(readlink -f /sys/dev/block/"$(awk -e \
'$9=="/dev/root"{print $3}' /proc/self/mountinfo)")"
ROOT_DEV="/dev/${ROOT_BLK##*/}"
LOOP_DEV="$(awk -e '$5=="/overlay"{print $9}' \
/proc/self/mountinfo)"
if [ -z "${LOOP_DEV}" ]
then
LOOP_DEV="$(losetup -f)"
losetup "${LOOP_DEV}" "${ROOT_DEV}"
fi
resize2fs -f "${LOOP_DEV}"
mount_root done
touch /etc/rootfs-resize
reboot
fi
exit 1
EOF
cat << "EOF" >> /etc/sysupgrade.conf
/etc/uci-defaults/70-rootpt-resize
/etc/uci-defaults/80-rootfs-resize
EOF

# To make the config.txt changes effective...
reboot

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