diff --git a/environment-setup/femtofox.chroot b/environment-setup/femtofox.chroot new file mode 100644 index 0000000..b231965 --- /dev/null +++ b/environment-setup/femtofox.chroot @@ -0,0 +1,96 @@ +echo "Inside chroot environment..." +echo "tmpfs /run tmpfs rw,nodev,nosuid,size=32M 0 0" | tee -a /etc/fstab + +# Temporarily removed for SEGFAULT fix done after upgrade below. +#echo "Installing Meshtastic..." + +#wget -qO- https://meshtastic.github.io/meshtastic-deb.asc | tee /etc/apt/keyrings/meshtastic-deb.asc >/dev/null +#if [[ $? -eq 2 ]]; then echo "Error, step failed..."; fi +#echo "deb [arch=all signed-by=/etc/apt/keyrings/meshtastic-deb.asc] https://meshtastic.github.io/deb stable main" | tee /etc/apt/sources.list.d/meshtastic-deb.list >/dev/null + +echo "Removing netdevice rules..." + +ln -s /dev/null /etc/udev/rules.d/80-net-setup-link.rules + +echo "Setting up kernel modules..." + +touch /lib/modules/5.10.160/modules.order +touch /lib/modules/5.10.160/modules.builtin +depmod -a 5.10.160 +if [[ $? -eq 2 ]]; then echo "Error, step failed..."; fi + +echo "Setting localtime to UTC..." + +rm /etc/localtime +ln -sf /usr/share/zoneinfo/UTC /etc/localtime + +echo "Installing packages..." + +apt update +DEBIAN_FRONTEND=noninteractive apt install -y --option Dpkg::Options::="--force-confold" linux-firmware wireless-tools git python3.10-venv libgpiod-dev libyaml-cpp-dev libbluetooth-dev openssl libssl-dev libulfius-dev liborcania-dev evtest screen avahi-daemon protobuf-compiler telnet fonts-noto-color-emoji ninja-build chrony qrencode +if [[ $? -eq 2 ]]; then echo "Error, step failed..."; fi +DEBIAN_FRONTEND=noninteractive apt upgrade -y --option Dpkg::Options::="--force-confold" + +# Tempfix for SEGFAULT +wget https://github.com/meshtastic/firmware/releases/download/v2.5.11.8e2a3e5/meshtasticd_2.5.11.8e2a3e5_armhf.deb +DEBIAN_FRONTEND=noninteractive apt install -y --option Dpkg::Options::="--force-confold" ./meshtasticd_2.5.11.8e2a3e5_armhf.deb + + +echo "Installing dependencies for meshing-around and meshtastic cli..." + +pip3 install requests pyephem pytap2 meshtastic pypubsub geopy maidenhead beautifulsoup4 dadjokes schedule wikipedia googlesearch-python +if [[ $? -eq 2 ]]; then echo "Error, step failed..."; fi + +echo "Setting MOTD..." + +mv /etc/update-motd.d/10-help-text /etc/update-motd.d/10-help-text.bak +mv /etc/update-motd.d/60-unminimize /etc/update-motd.d/60-unminimize.bak + +echo "Setting hostname to femtofox..." + +echo "femtofox" | tee /etc/hostname > /dev/null + +echo "Configuring autostart of systemd services..." + +systemctl enable button +systemctl enable wifi-mesh-control +systemctl disable NetworkManager +systemctl disable NetworkManager-dispatcher +systemctl disable NetworkManager-wait-online +systemctl disable vsftpd.service +systemctl disable ModemManager.service +systemctl disable getty@tty1.service +systemctl disable acpid +systemctl disable acpid.socket +systemctl disable acpid.service +systemctl mask alsa-restore.service +systemctl disable alsa-restore.service +systemctl disable alsa-state.service +systemctl mask sound.target +systemctl disable sound.target +systemctl disable veritysetup.target +systemctl disable systemd-pstore.service + +echo "Adding femto and pico users/groups..." + +groupmod -n femto pico +usermod -l femto pico +usermod -aG sudo,input femto +echo "femto ALL=(ALL:ALL) ALL" | tee /etc/sudoers.d/femto > /dev/null +chmod 440 /etc/sudoers.d/femto + +# this seems messy, user:group should be set cleanly and not corrected after? OSC: somethings were owned my pico from factory + +find / -group pico -exec chgrp femto {} \; 2>/dev/null +find / -user pico -exec chown femto {} \; 2>/dev/null +usermod -d /home/femto -m femto +ls -ld /home/femto +echo 'femto:femto' | chpasswd +sudo chage -d 0 femto +usermod -a -G tty femto +usermod -a -G dialout femto + +echo "Cleaning up chroot..." + +apt clean && rm -rf /var/lib/apt/lists/* && rm -rf /tmp/* && rm -rf /var/tmp/* && find /var/log -type f -exec truncate -s 0 {} + && : > /root/.bash_history && history -c +exit \ No newline at end of file diff --git a/environment-setup/foxbuntu-builder.sh b/environment-setup/foxbuntu-builder.sh new file mode 100644 index 0000000..8fc46bf --- /dev/null +++ b/environment-setup/foxbuntu-builder.sh @@ -0,0 +1,323 @@ +#!/bin/bash + +if [[ $(id -u) != 0 ]]; then + echo "This script must be run as root; use sudo" + exit 1 +fi + +sudoer=$(echo $SUDO_USER) + +# Check if 'dialog' is installed, install it if missing +if ! command -v dialog &> /dev/null; then + echo "The 'dialog' package is required to run this script. Press any key to install it." + read -n 1 -s -r + apt update && apt install -y dialog +fi + +################ TODO ################ +# Add more error handling +# Address potential issues in comments +# Package selection with curses +# Switch chroot packages install +# Modify DTS etc to enable SPI1 +###################################### + +install_prerequisites() { + echo "Setting up Foxbuntu build environment..." + apt update + apt install -y git ssh make gcc gcc-multilib g++-multilib module-assistant expect g++ gawk texinfo libssl-dev bison flex fakeroot cmake unzip gperf autoconf device-tree-compiler libncurses5-dev pkg-config bc python-is-python3 passwd openssl openssh-server openssh-client vim file cpio rsync qemu-user-static binfmt-support dialog +} + +clone_repos() { + echo "Cloning repos..." + cd /home/${sudoer}/ + git clone https://github.com/LuckfoxTECH/luckfox-pico.git + git clone https://github.com/noon92/femtofox.git +} + +build_env() { + echo "Setting up SDK env..." + echo "When the menu appears to choose your board choose Luckfox Pico Mini A (1), SDCard (0) and Ubuntu (1)." + echo "Press any key to continue building the environment..." + read -n 1 -s -r + cd /home/${sudoer}/luckfox-pico + ./build.sh env +} + +build_sysdrv() { + echo "Building sysdrv..." + cd /home/${sudoer}/luckfox-pico + ./build.sh sysdrv +} + +build_uboot() { + echo "Building uboot..." + cd /home/${sudoer}/luckfox-pico + ./build.sh uboot +} + +build_rootfs() { + echo "Building rootfs..." + cd /home/${sudoer}/luckfox-pico + ./build.sh rootfs +} + +copy_femtofox_kernelcfg() { + echo "Merging in Foxbuntu modifications..." + cd /home/${sudoer}/femtofox/foxbuntu/ + rsync -aHAXv --progress --keep-dirlinks --itemize-changes /home/${sudoer}/femtofox/foxbuntu/sysdrv/ /home/${sudoer}/luckfox-pico/sysdrv/ + rsync -aHAXv --progress --keep-dirlinks --itemize-changes /home/${sudoer}/femtofox/foxbuntu/project/ /home/${sudoer}/luckfox-pico/project/ + rsync -aHAXv --progress --keep-dirlinks --itemize-changes /home/${sudoer}/femtofox/foxbuntu/output/image/ /home/${sudoer}/luckfox-pico/output/image/ +} + +build_kernelconfig() { + echo "Building kernelconfig... Please exit without making any changes unless you know what you are doing." + echo "Press any key to continue building the kernel..." + read -n 1 -s -r + cd /home/${sudoer}/luckfox-pico + ./build.sh kernelconfig + ./build.sh kernel +} + +modify_kernel() { + echo "Building kernel... ." + echo "After making kernel configuration changes, make sure to save as .config (default) before exiting." + echo "Press any key to continue building the kernel..." + read -n 1 -s -r + cd /home/${sudoer}/luckfox-pico + ./build.sh kernelconfig + ./build.sh kernel + build_rootfs + build_firmware + cp /home/${sudoer}/luckfox-pico/sysdrv/out/kernel_drv_ko/* /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/lib/modules/5.10.160/ + echo "Entering chroot..." + mount --bind /proc /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/proc + mount --bind /sys /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/sys + mount --bind /dev /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/dev + mount --bind /dev/pts /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/dev/pts + chroot /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106 /bin/bash < /root/.bash_history && history -c +exit +EOF + + umount /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/dev/pts + umount /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/proc + umount /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/sys + umount /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/dev + build_rootfs + build_firmware + create_image +} + +modify_chroot() { + echo "Entering chroot... make your changes and then type exit when you are done and it will build the image with your changes." + echo "Press any key to continue entering chroot..." + read -n 1 -s -r + mount --bind /proc /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/proc + mount --bind /sys /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/sys + mount --bind /dev /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/dev + mount --bind /dev/pts /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/dev/pts + chroot /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106 /bin/bash + umount /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/dev/pts + umount /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/proc + umount /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/sys + umount /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/dev + build_rootfs + build_firmware + create_image +} + +inject_chroot() { + chroot_script=${CHROOT_SCRIPT:-/home/${sudoer}/femtofox.chroot} + if [[ ! -f $chroot_script ]]; then + echo "Error: Chroot script $chroot_script not found." + exit 1 + fi + + cp "$chroot_script" /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/tmp/chroot_script.sh + chmod +x /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/tmp/chroot_script.sh + + echo "Press any key to continue entering chroot..." + read -n 1 -s -r + + echo "Entering chroot and running commands..." + + mount --bind /proc /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/proc + mount --bind /sys /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/sys + mount --bind /dev /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/dev + mount --bind /dev/pts /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/dev/pts + chroot /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106 /tmp/chroot_script.sh + umount /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/dev/pts + umount /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/proc + umount /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/sys + umount /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/dev + rm /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/tmp/chroot_script.sh + build_rootfs + build_firmware + create_image +} + + +update_image() { + echo "Updating repo..." + cd /home/${sudoer}/femtofox + git pull + cd /home/${sudoer}/ + copy_femtofox_kernelcfg + build_kernelconfig + build_rootfs + build_firmware + create_image +} + +modify_rootfs() { + echo "Modifying rootfs..." + cd /home/${sudoer}/luckfox-pico/output/image + echo "Copying kernel modules..." + mkdir -p /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/lib/modules/5.10.160 + cp /home/${sudoer}/luckfox-pico/sysdrv/out/kernel_drv_ko/* /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/lib/modules/5.10.160/ + which qemu-arm-static + + chroot_script=${CHROOT_SCRIPT:-/home/${sudoer}/femtofox.chroot} + if [[ ! -f $chroot_script ]]; then + echo "Error: Chroot script $chroot_script not found." + exit 1 + fi + + cp "$chroot_script" /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/tmp/chroot_script.sh + chmod +x /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/tmp/chroot_script.sh + + echo "Entering chroot and running commands..." + cp /usr/bin/qemu-arm-static /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/usr/bin/ + mount --bind /proc /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/proc + mount --bind /sys /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/sys + mount --bind /dev /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/dev + mount --bind /dev/pts /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/dev/pts + + chroot /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106 /tmp/chroot_script.sh + + umount /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/dev/pts + umount /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/proc + umount /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/sys + umount /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/dev + + rm /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/tmp/chroot_script.sh +} + +build_firmware() { + echo "Building firmware..." + cd /home/${sudoer}/luckfox-pico/ + ./build.sh firmware +} + +create_image() { + echo "Creating final sdcard img..." + cd /home/${sudoer}/luckfox-pico/output/image + /home/${sudoer}/luckfox-pico/output/image/blkenvflash /home/${sudoer}/luckfox-pico/foxbuntu.img + if [[ $? -eq 2 ]]; then echo "Error, sdcard img failed to build..."; exit 2; else echo "foxbuntu.img build completed."; fi + du -h /home/${sudoer}/luckfox-pico/foxbuntu.img +} + +sdk_install() { + start_time=$(date +%s) + install_prerequisites + clone_repos + build_env + build_uboot + copy_femtofox_kernelcfg + build_kernelconfig + build_rootfs + rsync -aHAXv --progress --keep-dirlinks --itemize-changes /home/${sudoer}/femtofox/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/ /home/${sudoer}/luckfox-pico/sysdrv/out/rootfs_uclibc_rv1106/ + build_firmware + modify_rootfs + build_rootfs + build_firmware + create_image + end_time=$(date +%s) + elapsed=$(( end_time - start_time )) + hours=$(( elapsed / 3600 )) + minutes=$(( (elapsed % 3600) / 60 )) + seconds=$(( elapsed % 60 )) + printf "Environment installation time: %02d:%02d:%02d\\n" $hours $minutes $seconds +} + +usage() { + echo "The following functions are available in this script:" + echo "To install the development environment use the arg 'sdk_install' and is intended to be run ONCE only." + echo "To modify the chroot and build an updated image use the arg 'modify_chroot'." + echo "To modify the kernel and build an updated image use the arg 'modify_kernel'." + echo "other args: install_prerequisites clone_repos build_env build_sysdrv copy_femtofox_kernelcfg build_kernelconfig modify_rootfs build_rootf build_uboot build_firmware get_envblkflash create_image" + echo "Example: sudo ~/foxbunto_env_setup.sh sdk_install" + echo "Example: sudo ~/foxbunto_env_setup.sh modify_chroot" + exit 0 +} +################### MENU SYSTEM ################### + +if [[ "${1}" =~ ^(-h|--help|h|help)$ ]]; then + usage +elif [[ -z ${1} ]]; then + if ! command -v dialog &> /dev/null; then + echo "The 'dialog' package is required to load the menu." + echo "Please install it using: sudo apt install dialog" + exit 1 + fi + while true; do + CHOICE=$(dialog --clear --no-cancel --backtitle "Foxbuntu SDK Builder" \ + --title "Main Menu" \ + --menu "Choose an action:" 20 60 12 \ + 1 "Update Image" \ + 2 "Modify Chroot" \ + 3 "Inject Chroot" \ + 4 "Modify Kernel" \ + 5 "Install Prerequisites" \ + 6 "Clone Repositories" \ + 7 "Build Environment" \ + 8 "Build SysDrv" \ + 9 "Build U-Boot" \ + 10 "Build RootFS" \ + 11 "Create Final Image" \ + 12 "SDK Install (Run All Steps)" \ + 13 "Exit" \ + 2>&1 >/dev/tty) + + clear + + case $CHOICE in + 1) update_image ;; + 2) modify_chroot ;; + 3) inject_chroot ;; + 4) modify_kernel ;; + 5) install_prerequisites ;; + 6) clone_repos ;; + 7) build_env ;; + 8) build_sysdrv ;; + 9) build_uboot ;; + 10) build_rootfs ;; + 11) create_image ;; + 12) sdk_install ;; + 13) echo "Exiting..."; break ;; + *) echo "Invalid option, please try again." ;; + esac + + # Pause after executing a command + echo "Menu selection completed. Press any key to return to the menu." + read -n 1 -s -r + done +else + if [[ "${1}" == "--chroot-script" ]]; then + CHROOT_SCRIPT=${2} + fi + if declare -f "${1}" > /dev/null; then + "${1}" + else + echo "Error: Function '${1}' not found." + usage + exit 1 + fi +fi + +exit 0