From dfc004dc6e22d20e2771760c7c235dd2aaf42df6 Mon Sep 17 00:00:00 2001 From: suvendhu Date: Thu, 8 Sep 2022 19:18:25 +0530 Subject: [PATCH] swmodd: Remove dependency of lxc autoboot --- swmodd/Makefile | 4 +- swmodd/files/etc/init.d/crun | 2 +- swmodd/files/etc/init.d/swmodd | 63 +++++++++++++++++-- .../files/etc/uci-defaults/01-fix-bundle-path | 36 ++++++++--- 4 files changed, 90 insertions(+), 15 deletions(-) diff --git a/swmodd/Makefile b/swmodd/Makefile index 99d4fe7a5..7b0b3498d 100755 --- a/swmodd/Makefile +++ b/swmodd/Makefile @@ -5,12 +5,12 @@ include $(TOPDIR)/rules.mk PKG_NAME:=swmodd -PKG_VERSION:=2.1.11 +PKG_VERSION:=2.1.12 LOCAL_DEV:=0 ifneq ($(LOCAL_DEV),1) PKG_SOURCE_PROTO:=git -PKG_SOURCE_VERSION:=d3657ecee7a3f6725ff91b0803e1247c0538bd44 +PKG_SOURCE_VERSION:=9fd50dd2c526c8b58c2d2d62042c261e6a7b65e0 PKG_SOURCE_URL:=https://dev.iopsys.eu/iopsys/swmodd.git PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.gz PKG_MIRROR_HASH:=skip diff --git a/swmodd/files/etc/init.d/crun b/swmodd/files/etc/init.d/crun index 4f06ce52a..8d0019dd4 100644 --- a/swmodd/files/etc/init.d/crun +++ b/swmodd/files/etc/init.d/crun @@ -148,7 +148,7 @@ start_service() { fi config_load swmodd - config_get bundle globals bundle_root "" + config_get bundle globals oci_bundle_root "" config_get bridge globals lan_bridge "br-lan" if [ -z "${bundle}" ] || [ -z "${bridge}" ]; then diff --git a/swmodd/files/etc/init.d/swmodd b/swmodd/files/etc/init.d/swmodd index b2ccd7c22..fe8c32e7f 100644 --- a/swmodd/files/etc/init.d/swmodd +++ b/swmodd/files/etc/init.d/swmodd @@ -12,25 +12,76 @@ validate_globals_section() 'enabled:bool:1' \ 'debug:bool:false' \ 'log_level:uinteger:1' \ - 'bundle_root:string' \ + 'lxc_bundle_root:string' \ + 'oci_bundle_root:string' \ 'sock:string' } +validate_container_section() { + uci_validate_section swmodd container "${1}" \ + 'name:string' \ + 'type:string' \ + 'autostart:bool:0' \ + 'timeout:uinteger:300' +} + +start_lxc_container() { + local name type autostart timeout + + validate_container_section "${1}" || { + return 0; + } + + if [ -z "${name}" ] || [ -z "${type}" ]; then + return 0; + fi + + if [ "${type}" != "lxc" ]; then + return 0; + fi + + state=$(lxc-ls -f | tail -n +2 | grep -w "${name}" | cut -d " " -f 2) + if [ -z "${state}" ]; then + return 0; + fi + + if [ "${autostart}" -eq 0 ]; then + if [ "${state}" == "RUNNING" ]; then + # stop the container if running + lxc-stop -n "${name}" -t "${timeout}" & + return 0; + elif [ "${state}" == "FROZEN" ]; then + # first unfreeze then stop + lxc-unfreeze -n "${name}" + lxc-stop -n "${name}" -t "${timeout}" & + return 0; + fi + else + if [ "${state}" == "FROZEN" ]; then + # unfreeze the container + lxc-unfreeze -n "${name}" + elif [ "${state}" == "STOPPED" ]; then + # start the container + lxc-start -n "${name}" + return 0; + fi + fi +} + start_service() { - local enabled debug log_level sock bundle_root + local enabled debug log_level sock lxc_bundle_root oci_bundle_root config_load swmodd validate_globals_section || { - log "Validation of uci globals failed" return 1; } [ "${enabled}" -eq 0 ] && return 0 - [ -z "${bundle_root}" ] && return 0 + [ -z "${lxc_bundle_root}" ] && mkdir -p "${lxc_bundle_root}" + [ -z "${oci_bundle_root}" ] && mkdir -p "${oci_bundle_root}" - mkdir -p "${bundle_root}" procd_open_instance swmodd procd_set_param command ${PROG} @@ -46,6 +97,8 @@ start_service() { procd_set_param respawn procd_close_instance + + config_foreach start_lxc_container container } reload_service() { diff --git a/swmodd/files/etc/uci-defaults/01-fix-bundle-path b/swmodd/files/etc/uci-defaults/01-fix-bundle-path index 3ed8cf1c4..d479e4733 100644 --- a/swmodd/files/etc/uci-defaults/01-fix-bundle-path +++ b/swmodd/files/etc/uci-defaults/01-fix-bundle-path @@ -2,20 +2,42 @@ . /lib/functions.sh -# if bundle_root not define in swmodd, update it with lxc path lxc="" -bundle="" +lxc_bundle="" +oci_bundle="" if [ -f "/etc/lxc/lxc.conf" ]; then lxc=$(cat /etc/lxc/lxc.conf |grep "lxc.lxcpath"| cut -d "=" -f 2) fi config_load swmodd -config_get bundle globals bundle_root "" -if [ -z "${bundle}" ] && [ -n "${lxc}" ]; then - uci_set swmodd globals bundle_root ${lxc} +config_get lxc_bundle globals lxc_bundle_root "" +config_get oci_bundle globals oci_bundle_root "" + +if [ -n "${lxc_bundle}" ]; then + # if lxc_bundle_root define in swmodd, update it in lxc path + mkdir -p /etc/lxc + echo "lxc.lxcpath = ${lxc_bundle}" > /etc/lxc/lxc.conf +elif [ -n "${lxc}" ]; then + # if lxc_bundle_root not define in swmodd, update it with lxc path + uci_set swmodd globals lxc_bundle_root ${lxc} uci_commit -elif [ -z "${bundle}" ]; then - uci_set swmodd globals bundle_root "/tmp/" +else + mkdir -p /etc/lxc + echo "lxc.lxcpath = /srv/" > /etc/lxc/lxc.conf + uci_set swmodd globals lxc_bundle_root "/srv/" + uci_commit +fi + +if [ -z "${oci_bundle}" ] && [ -n "${lxc_bundle}" ]; then + # if oci_bundle_root not defined in swmodd, update it with lxc_bundle_root if defined + uci_set swmodd globals oci_bundle_root ${lxc_bundle} + uci_commit +elif [ -z "${oci_bundle}" ] && [ -n "${lxc}" ]; then + # if oci_bundle_root not defined in swmodd, update it with lxc path + uci_set swmodd globals oci_bundle_root ${lxc} + uci_commit +elif [ -z "${oci_bundle}" ]; then + uci_set swmodd globals oci_bundle_root "/srv/" uci_commit fi