diff --git a/script-server/runners/backup_openwrt_config.json b/script-server/runners/backup_openwrt_config.json new file mode 100644 index 0000000..a517288 --- /dev/null +++ b/script-server/runners/backup_openwrt_config.json @@ -0,0 +1,8 @@ +{ + "name": "1 Backup OpenWrt config", + "description": "Backup OpenWrt config. WARNING: OpenWrt will be stopped during this operation.", + "script_path": "/run/backup_openwrt_config.sh", + "output_files": [ + "/tmp/openwrt_config.tar.gz" + ] +} diff --git a/script-server/runners/factory_reset.json b/script-server/runners/factory_reset.json index 8c8c614..dd67b13 100644 --- a/script-server/runners/factory_reset.json +++ b/script-server/runners/factory_reset.json @@ -1,6 +1,6 @@ { - "name": "Apply factory reset", - "description": "Hard factory reset of Openwrt", - "script_path": "/run/qemu_qmp.sh -f" + "name": "99 Factory reset", + "description": "Hard factory reset of Openwrt. WARNING: OpenWrt will be stopped during this operation.", + "script_path": "/run/factory_reset.sh" } diff --git a/script-server/runners/reset.json b/script-server/runners/reset.json index 426639d..09b578e 100644 --- a/script-server/runners/reset.json +++ b/script-server/runners/reset.json @@ -1,6 +1,6 @@ { - "name": "Reset OpenWRT", - "description": "Hard reset of Openwrt VM", + "name": "98 Reset OpenWrt", + "description": "Hard reset of OpenWrt VM", "script_path": "/run/qemu_qmp.sh -r" } diff --git a/script-server/runners/restore_openwrt_config.json b/script-server/runners/restore_openwrt_config.json new file mode 100644 index 0000000..f8a7faa --- /dev/null +++ b/script-server/runners/restore_openwrt_config.json @@ -0,0 +1,15 @@ +{ + "name": "2 Restore OpenWrt config", + "description": "Restore OpenWrt config. WARNING: OpenWrt will be stopped during this operation.", + "script_path": "/run/restore_openwrt_config.sh", + "parameters": [ + { + "name": "File upload", + "param": "--file_upload", + "type": "file_upload", + "description": "File upload testing", + "ui": { + "width_weight": 2 + } + }] +} diff --git a/script-server/runners/start_openwrt.json b/script-server/runners/start_openwrt.json new file mode 100644 index 0000000..0be98ee --- /dev/null +++ b/script-server/runners/start_openwrt.json @@ -0,0 +1,6 @@ +{ + "name": "4 Start OpenWrt", + "description": "Start OpenWrt VM", + "script_path": "supervisorctl start openwrt" +} + diff --git a/script-server/runners/stop_openwrt.json b/script-server/runners/stop_openwrt.json new file mode 100644 index 0000000..c9250eb --- /dev/null +++ b/script-server/runners/stop_openwrt.json @@ -0,0 +1,6 @@ +{ + "name": "5 Stop OpenWrt", + "description": "Stop OpenWrt VM", + "script_path": "supervisorctl stop openwrt" +} + diff --git a/src/backup_openwrt_config.sh b/src/backup_openwrt_config.sh new file mode 100644 index 0000000..4814d75 --- /dev/null +++ b/src/backup_openwrt_config.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +set -Eeuo pipefail +trap - ERR + +. /run/helpers.sh + +CURRENT_VERSION_ROOTFS=`cat /storage/current_version` + +# Stop openwrt +echo "****** Stop openwrt ******" +supervisorctl stop openwrt + +# Get config +echo "****** Mount $CURRENT_VERSION_ROOTFS ******" +mount /storage/$CURRENT_VERSION_ROOTFS /mnt + +echo "****** Create openwrt_config.tar.gz ******" +chroot /mnt /sbin/sysupgrade -k -b /tmp/openwrt_config.tar.gz +mv /mnt/tmp/openwrt_config.tar.gz /tmp/openwrt_config.tar.gz + +echo "****** umount $CURRENT_VERSION_ROOTFS ******" +umount /mnt + +# Start openwrt again +echo "****** Start openwrt again ******" +supervisorctl start openwrt diff --git a/src/factory_reset.sh b/src/factory_reset.sh new file mode 100644 index 0000000..93083e5 --- /dev/null +++ b/src/factory_reset.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash + +set -Eeuo pipefail +trap - ERR + +. /run/helpers.sh + +CURRENT_VERSION_ROOTFS=`cat /storage/current_version` + +# Stop openwrt +echo "****** Stop openwrt ******" +supervisorctl stop openwrt + +# Do factory reset +echo "****** Do factory reset ******" + +echo "Deleting all files in /storage ..." +ls /storage/ +rm -f /storage/* + +# Start openwrt again +echo "****** Start openwrt again ******" +supervisorctl start openwrt \ No newline at end of file diff --git a/src/init_container.sh b/src/init_container.sh index d151e9e..0608f72 100644 --- a/src/init_container.sh +++ b/src/init_container.sh @@ -56,4 +56,4 @@ supervisorctl start nginx # Start webserver # ******* OpenWrt handling ******* -supervisorctl start openwrt # Start webserver \ No newline at end of file +supervisorctl start openwrt # Start openwrt \ No newline at end of file diff --git a/src/qemu_qmp.sh b/src/qemu_qmp.sh index 95c5e87..475c79d 100644 --- a/src/qemu_qmp.sh +++ b/src/qemu_qmp.sh @@ -28,7 +28,7 @@ Help() # Process the input options. Add options as needed. # ############################################################ # Get the options -while getopts ":hvrqf" option; do +while getopts ":hvrqs" option; do case $option in h) # display Help Help @@ -42,10 +42,8 @@ while getopts ":hvrqf" option; do q) # Quit Qemu echo -ne '{ "execute": "qmp_capabilities" } { "execute": "quit" }' | nc -w 1 -U /run/qmp-sock exit;; - f) # Apply factory reset - rm /storage/current_version - rm /storage/rootfs-${OPENWRT_VERSION}.img - echo -ne '{ "execute": "qmp_capabilities" } { "execute": "quit" }' | nc -w 1 -U /run/qmp-sock + s) # Shutdown VM + echo -ne '{ "execute": "qmp_capabilities" } { "execute": "system_powerdown" }' | nc -w 1 -U /run/qmp-sock exit;; \?) # Invalid option echo "Error: Invalid option" diff --git a/src/restore_openwrt_config.sh b/src/restore_openwrt_config.sh new file mode 100644 index 0000000..628ee3f --- /dev/null +++ b/src/restore_openwrt_config.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash + +set -Eeuo pipefail +trap - ERR + +. /run/helpers.sh + +if [ ! "$1" = "--file_upload" ] ; then + echo "Error: expected argument --file_upload" + exit 1 +fi + +CURRENT_VERSION_ROOTFS=`cat /storage/current_version` + +# Stop openwrt +echo "****** Stop openwrt ******" +supervisorctl stop openwrt + +# Get config +echo "****** Mount $CURRENT_VERSION_ROOTFS ******" +mount /storage/$CURRENT_VERSION_ROOTFS /mnt + +echo "****** Restore $2 ******" +cp $2 /mnt/tmp/openwrt_config.tar.gz +chroot /mnt /sbin/sysupgrade -r /tmp/openwrt_config.tar.gz + +echo "****** umount $CURRENT_VERSION_ROOTFS ******" +umount /mnt + +# Start openwrt again +echo "****** Start openwrt again ******" +supervisorctl start openwrt diff --git a/src/run_openwrt.sh b/src/run_openwrt.sh index 426fadd..10cd9c4 100755 --- a/src/run_openwrt.sh +++ b/src/run_openwrt.sh @@ -19,8 +19,15 @@ attach_eth_if () { # * https://serverfault.com/questions/688483/assign-physical-interface-to-docker-exclusively # * https://medium.com/lucjuggery/a-container-to-access-the-shell-of-the-host-2c7c227c64e9 # * https://developers.redhat.com/blog/2018/10/22/introduction-to-linux-interfaces-for-virtual-networking# - + info "Attaching physical Ethernet interface $HOST_IF into container with the name $CONTAINER_IF ..." + + # Check if interface exists + if ! nsenter --target 1 --uts --net --ipc --mount ip link show "$HOST_IF" &> /dev/null; then + info "Host Ethernet interface $HOST_IF does not exists. It can be a wrong interface name or the Ethernet interface is already assigend to this container." + return + fi + PID_CONTAINTER=$(nsenter --target 1 --uts --net --ipc --mount docker inspect -f '{{.State.Pid}}' $(cat /etc/hostname)) nsenter --target 1 --uts --net --ipc --mount mkdir -p /var/run/netns nsenter --target 1 --uts --net --ipc --mount ln -s /proc/$PID_CONTAINTER/ns/net /var/run/netns/$PID_CONTAINTER @@ -114,6 +121,6 @@ qemu-system-aarch64 -M virt \ $WAN_ARGS \ $USB_ARGS \ -qmp unix:/run/qmp-sock,server=on,wait=off - + # -device virtio-net,netdev=qlan1 -netdev user,id=qlan1,net=192.168.1.0/24,hostfwd=tcp::8000-192.168.1.1:80 \ # -blockdev driver=raw,node-name=hd0,cache.direct=on,file.driver=file,file.filename=/var/vm/openwrt-armsr-armv8-generic-ext4-combined.img \ diff --git a/supervisord.conf b/supervisord.conf index 9b22d27..186a71b 100644 --- a/supervisord.conf +++ b/supervisord.conf @@ -41,3 +41,4 @@ stdout_logfile=/dev/stdout stdout_logfile_maxbytes=0 stderr_logfile=/dev/stderr stderr_logfile_maxbytes=0 +stopasgroup=true \ No newline at end of file