[PATCH v2] base-files: restore status of system-services after sysupgrade

Simonas Tamošaitis simsasss at gmail.com
Tue Jan 12 06:45:02 EST 2021


On 1/12/21 12:07 AM, Sven Roederer wrote:
> Restore the status of the system-services after sysupgrade.
> Create a file with the status of all known services and keep it during
> upgrade. After upgrade run a uci-default script to restore every
> single service.
> The list of the service status will be stored in etc/backup folder also
> and formated as:
> /etc/init.d/<service> {enable|disable}
>
> When upgrading with an generic image all system services are enabled by
> default which is usually not expected and can cause trouble.
> The default behavior can be flipped with the "-s" option of sysupgrade.
>
> Signed-off-by: Sven Roederer <devel-sven at geroedel.de>
> ---
>
> This v2 includes the feedback from the mailinglist:
> * entangles this feature from "storing the packagelist"
> * adds a option for disabling to sysupgrade
> * makes it enabled by default
> * includes the glue-script to restore the services after upgrade
>
> I was not able to test on a real system yet.
>
>   package/base-files/Makefile                   |  2 +-
>   .../uci-defaults/14_restore-services-state    |  3 ++
>   package/base-files/files/sbin/sysupgrade      | 38 ++++++++++++++-----
>   3 files changed, 32 insertions(+), 11 deletions(-)
>   create mode 100644 package/base-files/files/etc/uci-defaults/14_restore-services-state
>
> diff --git a/package/base-files/Makefile b/package/base-files/Makefile
> index f18f221129..5a9bacba13 100644
> --- a/package/base-files/Makefile
> +++ b/package/base-files/Makefile
> @@ -12,7 +12,7 @@ include $(INCLUDE_DIR)/version.mk
>   include $(INCLUDE_DIR)/feeds.mk
>   
>   PKG_NAME:=base-files
> -PKG_RELEASE:=246
> +PKG_RELEASE:=247
>   PKG_FLAGS:=nonshared
>   
>   PKG_FILE_DEPENDS:=$(PLATFORM_DIR)/ $(GENERIC_PLATFORM_DIR)/base-files/
> diff --git a/package/base-files/files/etc/uci-defaults/14_restore-services-state b/package/base-files/files/etc/uci-defaults/14_restore-services-state
> new file mode 100644
> index 0000000000..d0085f3f80
> --- /dev/null
> +++ b/package/base-files/files/etc/uci-defaults/14_restore-services-state
> @@ -0,0 +1,3 @@
> +SERVICE_STATUS=/etc/backup/service_status.txt
> +
> +[ -e ${SERVICE_STATUS} ] && . ${SERVICE_STATUS}
> diff --git a/package/base-files/files/sbin/sysupgrade b/package/base-files/files/sbin/sysupgrade
> index 50f4e08e44..068ec27e3d 100755
> --- a/package/base-files/files/sbin/sysupgrade
> +++ b/package/base-files/files/sbin/sysupgrade
> @@ -15,6 +15,7 @@ export SAVE_OVERLAY_PATH=
>   export SAVE_PARTITIONS=1
>   export SAVE_INSTALLED_PKGS=0
>   export SKIP_UNCHANGED=1
> +export SAVE_SERVICE_STATUS=1
>   export CONF_IMAGE=
>   export CONF_BACKUP_LIST=0
>   export CONF_BACKUP=
> @@ -35,6 +36,7 @@ while [ -n "$1" ]; do
>   		-c) export SAVE_OVERLAY=1 SAVE_OVERLAY_PATH=/etc;;
>   		-o) export SAVE_OVERLAY=1 SAVE_OVERLAY_PATH=/;;
>   		-p) export SAVE_PARTITIONS=0;;
> +		-s) export SAVE_SERVICE_STATUS=0;;
>   		-k) export SAVE_INSTALLED_PKGS=1;;
>   		-U) export SKIP_UNCHANGED=0;;
>   		-b|--create-backup) export CONF_BACKUP="$2" NEED_IMAGE=1; shift;;
> @@ -57,13 +59,14 @@ export CONFFILES=/tmp/sysupgrade.conffiles
>   export CONF_TAR=/tmp/sysupgrade.tgz
>   export ETCBACKUP_DIR=/etc/backup
>   export INSTALLED_PACKAGES=${ETCBACKUP_DIR}/installed_packages.txt
> +export SERVICE_STATUS=${ETCBACKUP_DIR}/service_status.txt
>   
>   IMAGE="$1"
>   
>   [ -z "$IMAGE" -a -z "$NEED_IMAGE" -a $CONF_BACKUP_LIST -eq 0 -o $HELP -gt 0 ] && {
>   	cat <<EOF
>   Usage: $0 [<upgrade-option>...] <image file or URL>
> -       $0 [-q] [-i] [-c] [-U] [-o] [-k] <backup-command> <file>
> +       $0 [-q] [-i] [-c] [-U] [-o] [-s] [-k] <backup-command> <file>
>   
>   upgrade-option:
>   	-f <config>  restore configuration from .tar.gz (file or url)
> @@ -75,6 +78,7 @@ upgrade-option:
>   	             (this is a modification of Freifunk Berlin to unset -u)
>   	-n           do not save configuration over reflash
>   	-p           do not attempt to restore the partition table after flash.
> +	-s           do not attempt to restore the status of the services.
>   	-k           include in backup a list of current installed packages at
>   	             $INSTALLED_PACKAGES
>   	-T | --test
> @@ -231,8 +235,7 @@ do_save_conffiles() {
>   	run_hooks "$CONFFILES" $sysupgrade_init_conffiles
>   	ask_bool 0 "Edit config file list" && vi "$CONFFILES"
>   
> -	if [ "$SAVE_INSTALLED_PKGS" -eq 1 ]; then
> -		echo "${INSTALLED_PACKAGES}" >> "$CONFFILES"
> +	if [ "$SAVE_INSTALLED_PKGS" -eq 1 ] || [ "$SAVE_SERVICE_STATUS" -eq 1 ]; then
>   		mkdir -p "$ETCBACKUP_DIR"
>   		# Avoid touching filesystem on each backup
>   		RAMFS="$(mktemp -d -t sysupgrade.XXXXXX)"
> @@ -243,13 +246,28 @@ do_save_conffiles() {
>   				ask_bool 0 "Abort" && exit
>   			}
>   
> -		# Format: pkg-name<TAB>{rom,overlay,unkown}
> -		# rom is used for pkgs in /rom, even if updated later
> -		find /usr/lib/opkg/info -name "*.control" \( \
> -			\( -exec test -f /rom/{} \; -exec echo {} rom \; \) -o \
> -			\( -exec test -f /overlay/upper/{} \; -exec echo {} overlay \; \) -o \
> -			\( -exec echo {} unknown \; \) \
> -			\) | sed -e 's,.*/,,;s/\.control /\t/' > ${INSTALLED_PACKAGES}
> +		if [ "$SAVE_INSTALLED_PKGS" -eq 1 ]; then
> +			echo "${INSTALLED_PACKAGES}" >> "$CONFFILES"
> +			# Format: pkg-name<TAB>{rom,overlay,unkown}
> +			# rom is used for pkgs in /rom, even if updated later
> +			find /usr/lib/opkg/info -name "*.control" \( \
> +				\( -exec test -f /rom/{} \; -exec echo {} rom \; \) -o \
> +				\( -exec test -f /overlay/upper/{} \; -exec echo {} overlay \; \) -o \
> +				\( -exec echo {} unknown \; \) \
> +					\) | sed -e 's,.*/,,;s/\.control /\t/' > ${INSTALLED_PACKAGES}
> +		fi
> +
> +		if [ "$SAVE_SERVICE_STATUS" -eq 1 ]; then
> +			echo "${SERVICE_STATUS}" >> "$CONFFILES"
> +			# Format: /etc/init.d/servicename {enable,disable}
> +			rm -f ${SERVICE_STATUS}
> +			for service in /etc/init.d/* ; do \
> +				${service} enabled && \
> +					echo >> ${SERVICE_STATUS} "$service" "enable" || \
> +					echo >> ${SERVICE_STATUS} "$service" "disable" \
> +				; \
> +			done
> +		fi
>   	fi
>   
>   	v "Saving config files..."
Hello,
You don't have to run such complicated script. Just restore /etc/rc.d/ 
directory after upgrade.



More information about the openwrt-devel mailing list