diff mbox series

[V3,2/3] base-files: sysupgrade: use tar helper to include installed_packages.txt

Message ID 20240228105102.3399-2-zajec5@gmail.com
State Accepted
Delegated to: Rafał Miłecki
Headers show
Series [V3,1/3] base-files: sysupgrade: add tar.sh with helpers for building archives | expand

Commit Message

Rafał Miłecki Feb. 28, 2024, 10:51 a.m. UTC
From: Rafał Miłecki <rafal@milecki.pl>

Replace mount + overlay with manually built tar archive that gets
prepended to the actual config files backup. This allows more
flexibility with including extra backup files. They can be included at
any paths and don't require writing to flash or mounting an overlay
which has its own limitations (mount points).

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
---
 package/base-files/files/sbin/sysupgrade | 44 +++++++++---------------
 1 file changed, 16 insertions(+), 28 deletions(-)

Comments

Jo-Philipp Wich Feb. 29, 2024, 1:41 p.m. UTC | #1
Hi,

> [...]
> Replace mount + overlay with manually built tar archive that gets
> prepended to the actual config files backup. This allows more
> flexibility with including extra backup files. They can be included at
> any paths and don't require writing to flash or mounting an overlay
> which has its own limitations (mount points).
> 
> Signed-off-by: Rafał Miłecki <rafal@milecki.pl>

Acked-by: Jo-Philipp Wich <jo@mein.io>

~ Jo
Luiz Angelo Daros de Luca March 1, 2024, 5:01 a.m. UTC | #2
>
> Replace mount + overlay with manually built tar archive that gets
> prepended to the actual config files backup. This allows more
> flexibility with including extra backup files. They can be included at
> any paths and don't require writing to flash or mounting an overlay
> which has its own limitations (mount points).
>
> Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
> ---
>  package/base-files/files/sbin/sysupgrade | 44 +++++++++---------------
>  1 file changed, 16 insertions(+), 28 deletions(-)
>
> diff --git a/package/base-files/files/sbin/sysupgrade b/package/base-files/files/sbin/sysupgrade
> index 6b3fb0666f..a11e17615c 100755
> --- a/package/base-files/files/sbin/sysupgrade
> +++ b/package/base-files/files/sbin/sysupgrade
> @@ -237,8 +237,6 @@ include /lib/upgrade
>  create_backup_archive() {
>         local conf_tar="$1"
>
> -       local umount_etcbackup_dir=0
> -
>         [ "$(rootfs_type)" = "tmpfs" ] && {
>                 echo "Cannot save config while running from ramdisk." >&2
>                 ask_bool 0 "Abort" && exit
> @@ -248,41 +246,31 @@ create_backup_archive() {
>         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"
> -               mkdir -p "$ETCBACKUP_DIR"
> -               # Avoid touching filesystem on each backup
> -               RAMFS="$(mktemp -d -t sysupgrade.XXXXXX)"
> -               mkdir -p "$RAMFS/upper" "$RAMFS/work"
> -               mount -t overlay overlay -o lowerdir=$ETCBACKUP_DIR,upperdir=$RAMFS/upper,workdir=$RAMFS/work $ETCBACKUP_DIR &&
> -                       umount_etcbackup_dir=1 || {
> -                               echo "Cannot mount '$ETCBACKUP_DIR' as tmpfs to avoid touching disk while saving the list of installed packages." >&2
> -                               ask_bool 0 "Abort" && exit
> -                       }
> -
> -               # Format: pkg-name<TAB>{rom,overlay,unknown}
> -               # 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
> -
>         v "Saving config files..."
>         [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
>         sed -i -e 's,^/,,' "$CONFFILES"
> -       tar c${TAR_V}zf "$conf_tar" -C / -T "$CONFFILES"
> +       {
> +               # Part of archive with installed packages info
> +               if [ "$SAVE_INSTALLED_PKGS" -eq 1 ]; then
> +                       # Format: pkg-name<TAB>{rom,overlay,unknown}
> +                       # rom is used for pkgs in /rom, even if updated later
> +                       tar_print_member "$INSTALLED_PACKAGES" "$(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/')"
> +               fi

I don't like the idea of running tar_print_member without checking for
errors. This is even more important for the following tar that
includes files specified by the user. In my opinion, it should abort
the process if tar fails.
As ash cannot read the pipe exit status, you could print the error and
touch a /tmp/${mypid}_abort, checking for it after the gzip to abort
the process (and erase the error flag file).

> +
> +               # Rest of archive with config files and ending padding
> +               tar c${TAR_V} -C / -T "$CONFFILES"
> +       } | gzip > "$conf_tar"
> +
>         local err=$?
>         if [ "$err" -ne 0 ]; then
>                 echo "Failed to create the configuration backup."
>                 rm -f "$conf_tar"
>         fi
>
> -       [ "$umount_etcbackup_dir" -eq 1 ] && {
> -               umount "$ETCBACKUP_DIR"
> -               rm -rf "$RAMFS"
> -       }
>         rm -f "$CONFFILES"
>
>         return "$err"
> --
> 2.35.3
>
diff mbox series

Patch

diff --git a/package/base-files/files/sbin/sysupgrade b/package/base-files/files/sbin/sysupgrade
index 6b3fb0666f..a11e17615c 100755
--- a/package/base-files/files/sbin/sysupgrade
+++ b/package/base-files/files/sbin/sysupgrade
@@ -237,8 +237,6 @@  include /lib/upgrade
 create_backup_archive() {
 	local conf_tar="$1"
 
-	local umount_etcbackup_dir=0
-
 	[ "$(rootfs_type)" = "tmpfs" ] && {
 		echo "Cannot save config while running from ramdisk." >&2
 		ask_bool 0 "Abort" && exit
@@ -248,41 +246,31 @@  create_backup_archive() {
 	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"
-		mkdir -p "$ETCBACKUP_DIR"
-		# Avoid touching filesystem on each backup
-		RAMFS="$(mktemp -d -t sysupgrade.XXXXXX)"
-		mkdir -p "$RAMFS/upper" "$RAMFS/work"
-		mount -t overlay overlay -o lowerdir=$ETCBACKUP_DIR,upperdir=$RAMFS/upper,workdir=$RAMFS/work $ETCBACKUP_DIR &&
-			umount_etcbackup_dir=1 || {
-				echo "Cannot mount '$ETCBACKUP_DIR' as tmpfs to avoid touching disk while saving the list of installed packages." >&2
-				ask_bool 0 "Abort" && exit
-			}
-
-		# Format: pkg-name<TAB>{rom,overlay,unknown}
-		# 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
-
 	v "Saving config files..."
 	[ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
 	sed -i -e 's,^/,,' "$CONFFILES"
-	tar c${TAR_V}zf "$conf_tar" -C / -T "$CONFFILES"
+	{
+		# Part of archive with installed packages info
+		if [ "$SAVE_INSTALLED_PKGS" -eq 1 ]; then
+			# Format: pkg-name<TAB>{rom,overlay,unknown}
+			# rom is used for pkgs in /rom, even if updated later
+			tar_print_member "$INSTALLED_PACKAGES" "$(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/')"
+		fi
+
+		# Rest of archive with config files and ending padding
+		tar c${TAR_V} -C / -T "$CONFFILES"
+	} | gzip > "$conf_tar"
+
 	local err=$?
 	if [ "$err" -ne 0 ]; then
 		echo "Failed to create the configuration backup."
 		rm -f "$conf_tar"
 	fi
 
-	[ "$umount_etcbackup_dir" -eq 1 ] && {
-		umount "$ETCBACKUP_DIR"
-		rm -rf "$RAMFS"
-	}
 	rm -f "$CONFFILES"
 
 	return "$err"