From 764efb3177885b935896269409ec1149a2c7d482 Mon Sep 17 00:00:00 2001 From: Albrecht Lohofener Date: Mon, 19 Feb 2024 20:31:37 +0100 Subject: [PATCH] spit run_openwrt.sh in multiple shell scripts. --- docker-compose.yml | 2 +- src/helpers.sh | 17 +++++ src/init_container.sh | 52 +++++----------- src/install_openwrt_rootfs.sh | 30 +++++++++ src/migrate_openwrt_rootfs.sh | 80 ++++++++++++++++++++++++ src/run_openwrt.sh | 114 +++------------------------------- supervisord.conf | 17 ++++- 7 files changed, 167 insertions(+), 145 deletions(-) create mode 100644 src/helpers.sh create mode 100644 src/install_openwrt_rootfs.sh create mode 100644 src/migrate_openwrt_rootfs.sh diff --git a/docker-compose.yml b/docker-compose.yml index 007d609..2f66c43 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,7 +14,7 @@ services: # If WAN interface is not specified the Docker network # In this case a port forwarding from port OpenWrt 80 to port 8000 is available #WAN_IF: "enx00e04c0407a6" - #LAN_IF: "enx00e04d68033f" + LAN_IF: "enx00e04d68033f" # USB passthrough # Format : like 'lsusb' is given diff --git a/src/helpers.sh b/src/helpers.sh new file mode 100644 index 0000000..41ce606 --- /dev/null +++ b/src/helpers.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +set -Eeuo pipefail + +info () { printf "%b%s%b" "\E[1;34m❯ \E[1;36m" "$1" "\E[0m\n"; } +error () { printf "%b%s%b" "\E[1;31m❯ " "ERROR: $1" "\E[0m\n" >&2; } +warn () { printf "%b%s%b" "\E[1;31m❯ " "Warning: $1" "\E[0m\n" >&2; } + +trap 'error "Status $? while: $BASH_COMMAND (line $LINENO/$BASH_LINENO)"' ERR + +# Docker environment variables +: "${CPU_COUNT:=""}" # Physical LAN interface name +: "${WAN_IF:=""}" # Physical WAN interface name +: "${LAN_IF:=""}" # Physical LAN interface name +: "${USB_VID_1:=""}" # USB vendor ID +: "${USB_PID_1:=""}" # USB product ID +: "${IS_U_OS_APP:=""}" # By default this container is not a u-OS app +: "${DEBUG:="N"}" # Disable debugging diff --git a/src/init_container.sh b/src/init_container.sh index e7ddf03..d151e9e 100644 --- a/src/init_container.sh +++ b/src/init_container.sh @@ -1,42 +1,18 @@ #!/usr/bin/env bash set -Eeuo pipefail -info () { printf "%b%s%b" "\E[1;34m❯ \E[1;36m" "$1" "\E[0m\n"; } -error () { printf "%b%s%b" "\E[1;31m❯ " "ERROR: $1" "\E[0m\n" >&2; } -warn () { printf "%b%s%b" "\E[1;31m❯ " "Warning: $1" "\E[0m\n" >&2; } +. /run/helpers.sh -trap 'error "Status $? while: $BASH_COMMAND (line $LINENO/$BASH_LINENO)"' ERR - -[ ! -f "/run/entry.sh" ] && error "Script must run inside Docker container!" && exit 11 +[ ! -f "/run/init_container.sh" ] && error "Script must run inside Docker container!" && exit 11 [ "$(id -u)" -ne "0" ] && error "Script must be executed with root privileges." && exit 12 +APP="OpenWrt" +SUPPORT="https://github.com/AlbrechtL/openwrt-docker-arm64-build" + echo "❯ Starting $APP for Docker v$( /var/log/script-server.log & +supervisorctl start script-server -return 0 + +# ******* nginx handling ******* +cp -r /var/www/* /run/shm +supervisorctl start nginx # Start webserver + + +# ******* OpenWrt handling ******* +supervisorctl start openwrt # Start webserver \ No newline at end of file diff --git a/src/install_openwrt_rootfs.sh b/src/install_openwrt_rootfs.sh new file mode 100644 index 0000000..9d8a785 --- /dev/null +++ b/src/install_openwrt_rootfs.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +set -Eeuo pipefail +trap - ERR + +. /run/helpers.sh + +# Check if rootfs is in volume +FILE=/storage/rootfs-${OPENWRT_VERSION}.img +if [ -f "$FILE" ]; then + info "$FILE exists. Nothing to do." +else + info "$FILE does not exist. Copying rootfs-${OPENWRT_VERSION}.img to storage ..." + cp /var/vm/rootfs-${OPENWRT_VERSION}.img.gz /storage/rootfs-${OPENWRT_VERSION}.img.gz + gzip -d /storage/rootfs-${OPENWRT_VERSION}.img.gz + + info "Inject some additional files into the image" + mount /storage/rootfs-${OPENWRT_VERSION}.img /mnt + # mount -o offset=$((512*262656)) /storage/disk.img /mnt # combined image ext4 partition starts at offset 262656 + chmod +x /var/vm/openwrt_additional/bin/* + cp /var/vm/openwrt_additional/bin/* /mnt/usr/bin/ + umount /mnt + + if [ -f "/storage/current_version" ]; then + mv /storage/current_version /storage/old_version + fi + + touch /storage/current_version + echo "rootfs-${OPENWRT_VERSION}.img" > /storage/current_version +fi diff --git a/src/migrate_openwrt_rootfs.sh b/src/migrate_openwrt_rootfs.sh new file mode 100644 index 0000000..2a1b8d4 --- /dev/null +++ b/src/migrate_openwrt_rootfs.sh @@ -0,0 +1,80 @@ +#!/usr/bin/env bash + +set -Eeuo pipefail +trap - ERR + +. /run/helpers.sh + +# Migrate settings from old OpenWrt version to new one +if [ -f /storage/old_version ]; then + OLD_VERSION_ROOTFS=`cat /storage/old_version` + + info "Migrate settings from $OLD_VERSION_ROOTFS to rootfs-${OPENWRT_VERSION}.img. This can take some time because we need to boot openwrt multiple times." + + mount /storage/$OLD_VERSION_ROOTFS /mnt + mv /mnt/etc/rc.local /mnt/etc/rc.local.disabled + cp /var/vm/openwrt_additional/do_backup_rc.local /mnt/etc/rc.local + chmod +x /mnt/etc/rc.local + umount /mnt + + info "Boot previous $OLD_VERSION_ROOTFS to get config" + qemu-system-aarch64 -M virt \ + -m 128 \ + -nodefaults \ + $CPU_ARGS -smp $CPU_COUNT \ + -bios /usr/share/qemu/edk2-aarch64-code.fd \ + -display vnc=:0,websocket=5700 \ + -vga none -device ramfb \ + -kernel /var/vm/kernel.bin -append "root=fe00 console=tty0" \ + -blockdev driver=raw,node-name=hd0,cache.direct=on,file.driver=file,file.filename=/storage/${OLD_VERSION_ROOTFS} \ + -device virtio-blk-pci,drive=hd0 \ + -device qemu-xhci -device usb-kbd + + # Get config backup from old rootfs + mount /storage/$OLD_VERSION_ROOTFS /mnt + rm /mnt/etc/rc.local + mv /mnt/etc/rc.local.disabled /mnt/etc/rc.local + + if [ -f /mnt/root/backup/config.tar.gz ]; then + cp /mnt/root/backup/config.tar.gz /storage/config-`basename $OLD_VERSION_ROOTFS .img`.tar.gz + else + error "No config.tar.gz in $OLD_VERSION_ROOTFS found" + fi + umount /mnt + + # Put config backup to new rootfs + mount $FILE /mnt + mkdir -p /mnt/root/backup + cp /storage/config-`basename $OLD_VERSION_ROOTFS .img`.tar.gz /mnt/root/backup/config.tar.gz + + mv /mnt/etc/rc.local /mnt/etc/rc.local.disabled + cp /var/vm/openwrt_additional/restore_backup_rc.local /mnt/etc/rc.local + chmod +x /mnt/etc/rc.local + umount /mnt + + info "Boot current rootfs-${OPENWRT_VERSION}.img to install config" + qemu-system-aarch64 -M virt \ + -m 128 \ + -nodefaults \ + $CPU_ARGS -smp $CPU_COUNT \ + -bios /usr/share/qemu/edk2-aarch64-code.fd \ + -display vnc=:0,websocket=5700 \ + -vga none -device ramfb \ + -kernel /var/vm/kernel.bin -append "root=fe00 console=tty0" \ + -blockdev driver=raw,node-name=hd0,cache.direct=on,file.driver=file,file.filename=${FILE} \ + -device virtio-blk-pci,drive=hd0 \ + -device qemu-xhci -device usb-kbd + + mount $FILE /mnt + rm /mnt/etc/rc.local + mv /mnt/etc/rc.local.disabled /mnt/etc/rc.local + mv /mnt/root/backup/config.tar.gz /mnt/root/backup/config-`basename $OLD_VERSION_ROOTFS .img`.tar.gz + umount /mnt + + # Finally remove old rootfs and old files + rm /storage/$OLD_VERSION_ROOTFS + rm /storage/old_version + rm /storage/config-`basename $OLD_VERSION_ROOTFS .img`.tar.gz +else + info "rootfs-${OPENWRT_VERSION}.img is up to date. Nothing to do." +fi diff --git a/src/run_openwrt.sh b/src/run_openwrt.sh index 1c114b7..426fadd 100755 --- a/src/run_openwrt.sh +++ b/src/run_openwrt.sh @@ -2,10 +2,13 @@ # Based on https://github.com/qemus/qemu-docker set -Eeuo pipefail +trap - ERR +. /run/helpers.sh +. /run/install_openwrt_rootfs.sh +. /run/migrate_openwrt_rootfs.sh -APP="OpenWrt" -SUPPORT="https://github.com/AlbrechtL/openwrt-docker-arm64-build" +VERS=$(qemu-system-aarch64 --version | head -n 1 | cut -d '(' -f 1) attach_eth_if () { HOST_IF=$1 @@ -39,36 +42,6 @@ attach_eth_if () { #ip link set $QEMU_IF up } -cd /run - -# Initialize system -. reset.sh - -trap - ERR - -# Check if rootfs is in volume -FILE=/storage/rootfs-${OPENWRT_VERSION}.img -if [ -f "$FILE" ]; then - info "$FILE exists. Nothing to do." -else - info "$FILE does not exist. Copying rootfs-${OPENWRT_VERSION}.img to storage ..." - cp /var/vm/rootfs-${OPENWRT_VERSION}.img.gz /storage/rootfs-${OPENWRT_VERSION}.img.gz - gzip -d /storage/rootfs-${OPENWRT_VERSION}.img.gz - - info "Inject some additional files into the image" - mount /storage/rootfs-${OPENWRT_VERSION}.img /mnt - # mount -o offset=$((512*262656)) /storage/disk.img /mnt # combined image ext4 partition starts at offset 262656 - chmod +x /var/vm/openwrt_additional/bin/* - cp /var/vm/openwrt_additional/bin/* /mnt/usr/bin/ - umount /mnt - - if [ -f "/storage/current_version" ]; then - mv /storage/current_version /storage/old_version - fi - - touch /storage/current_version - echo "rootfs-${OPENWRT_VERSION}.img" > /storage/current_version -fi # Check KVM info "Checking for KVM ..." @@ -119,87 +92,14 @@ else USB_ARGS="-device usb-host,vendorid=0x$USB_VID_1,productid=0x$USB_PID_1" fi -# Migrate settings from old OpenWrt version to new one -if [ -f /storage/old_version ]; then - OLD_VERSION_ROOTFS=`cat /storage/old_version` - - info "Migrate settings from $OLD_VERSION_ROOTFS to rootfs-${OPENWRT_VERSION}.img. This can take some time because we need to boot openwrt multiple times." - - mount /storage/$OLD_VERSION_ROOTFS /mnt - mv /mnt/etc/rc.local /mnt/etc/rc.local.disabled - cp /var/vm/openwrt_additional/do_backup_rc.local /mnt/etc/rc.local - chmod +x /mnt/etc/rc.local - umount /mnt - - info "Boot previous $OLD_VERSION_ROOTFS to get config" - qemu-system-aarch64 -M virt \ - -m 128 \ - -nodefaults \ - $CPU_ARGS -smp $CPU_COUNT \ - -bios /usr/share/qemu/edk2-aarch64-code.fd \ - -display vnc=:0,websocket=5700 \ - -vga none -device ramfb \ - -kernel /var/vm/kernel.bin -append "root=fe00 console=tty0" \ - -blockdev driver=raw,node-name=hd0,cache.direct=on,file.driver=file,file.filename=/storage/${OLD_VERSION_ROOTFS} \ - -device virtio-blk-pci,drive=hd0 \ - -device qemu-xhci -device usb-kbd - - # Get config backup from old rootfs - mount /storage/$OLD_VERSION_ROOTFS /mnt - rm /mnt/etc/rc.local - mv /mnt/etc/rc.local.disabled /mnt/etc/rc.local - - if [ -f /mnt/root/backup/config.tar.gz ]; then - cp /mnt/root/backup/config.tar.gz /storage/config-`basename $OLD_VERSION_ROOTFS .img`.tar.gz - else - error "No config.tar.gz in $OLD_VERSION_ROOTFS found" - fi - umount /mnt - - # Put config backup to new rootfs - mount $FILE /mnt - mkdir -p /mnt/root/backup - cp /storage/config-`basename $OLD_VERSION_ROOTFS .img`.tar.gz /mnt/root/backup/config.tar.gz - - mv /mnt/etc/rc.local /mnt/etc/rc.local.disabled - cp /var/vm/openwrt_additional/restore_backup_rc.local /mnt/etc/rc.local - chmod +x /mnt/etc/rc.local - umount /mnt - - info "Boot current rootfs-${OPENWRT_VERSION}.img to install config" - qemu-system-aarch64 -M virt \ - -m 128 \ - -nodefaults \ - $CPU_ARGS -smp $CPU_COUNT \ - -bios /usr/share/qemu/edk2-aarch64-code.fd \ - -display vnc=:0,websocket=5700 \ - -vga none -device ramfb \ - -kernel /var/vm/kernel.bin -append "root=fe00 console=tty0" \ - -blockdev driver=raw,node-name=hd0,cache.direct=on,file.driver=file,file.filename=${FILE} \ - -device virtio-blk-pci,drive=hd0 \ - -device qemu-xhci -device usb-kbd - - mount $FILE /mnt - rm /mnt/etc/rc.local - mv /mnt/etc/rc.local.disabled /mnt/etc/rc.local - mv /mnt/root/backup/config.tar.gz /mnt/root/backup/config-`basename $OLD_VERSION_ROOTFS .img`.tar.gz - umount /mnt - - # Finally remove old rootfs and old files - rm /storage/$OLD_VERSION_ROOTFS - rm /storage/old_version - rm /storage/config-`basename $OLD_VERSION_ROOTFS .img`.tar.gz -fi - +info "Booting image using $VERS..." # See qemu command if debug is enabled [[ "$DEBUG" == [Yy1]* ]] && set -x #************************ FINAL BOOTING ************************ - -info "Booting image using $VERS..." -exec qemu-system-aarch64 -M virt \ +qemu-system-aarch64 -M virt \ -m 128 \ -nodefaults \ $CPU_ARGS -smp $CPU_COUNT \ diff --git a/supervisord.conf b/supervisord.conf index 4411922..9b22d27 100644 --- a/supervisord.conf +++ b/supervisord.conf @@ -13,7 +13,7 @@ file = /var/run/supervisord.sock supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface [program:init] -command=/run/entry.sh +command=/run/init_container.sh process_name=%(program_name)s autostart=true autorestart=false @@ -26,3 +26,18 @@ stderr_logfile_maxbytes=0 command=nginx process_name=%(program_name)s autostart=false + +[program:script-server] +command=/var/lib/script-server-env/bin/python /usr/share/script-server/launcher.py -d /var/script-server +process_name=%(program_name)s +autostart=false + +[program:openwrt] +command=/run/run_openwrt.sh +process_name=%(program_name)s +autostart=false +autorestart=false +stdout_logfile=/dev/stdout +stdout_logfile_maxbytes=0 +stderr_logfile=/dev/stderr +stderr_logfile_maxbytes=0