#3180 Issue closed: Scaring Error() exit in sbin/rear when unsuccessful mktmp

Labels: bug, fixed / solved / done, blocker

jsmeix opened issue at 2024-03-18 14:03:

  • ReaR version ("/usr/sbin/rear -V"):
    current GitHub master code

  • Description of the issue (ideally so that others can reproduce it):

One method to reporoduce it is to e.g.

# export TMPDIR=QQQ

where the directory QQQ does not exists.

On a SLES15-SP5 test VM with default btrfs structure
I get the following excerpts on the terminal
(i.e. also the 'set-x' output is on the terminal
because it Could not create build area ''):

# usr/sbin/rear -D mkrescue
tac: failed to create temporary file in 'QQQ': No such file or directory
tac: failed to create temporary file in 'QQQ': No such file or directory
mktemp: failed to create directory via template 'QQQ/rear.XXXXXXXXXXXXXXX': No such file or directory
ERROR: Could not create build area ''
===== Stack trace =====
Trace 0: usr/sbin/rear:426 main
=== End stack trace ===
++ LogPrint 'Error exit of rear mkrescue (PID 1802) and its descendant processes'
...
Running exit tasks
...
++ Log 'Exit task '\''cleanup_build_area_and_end_program'\'''
...
+++ Log 'Removing build area '
...
+++ rm -Rf --one-file-system
...
rmdir: removing directory, ''
rmdir: failed to remove '': No such file or directory
...
Could not remove build area  (something still exists therein)
...
+++ mounted_in_BUILD_DIR='  proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
  sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
  devtmpfs on /dev type devtmpfs (rw,nosuid,size=4096k,nr_inodes=1048576,mode=755,inode64)
   ...
  /dev/sda2 on / type btrfs (rw,relatime,space_cache,subvolid=268,subvol=/@/.snapshots/1/snapshot)
  ...
  /dev/sda2 on /.snapshots type btrfs (rw,relatime,space_cache,subvolid=267,subvol=/@/.snapshots)
  /dev/sda2 on /boot/grub2/x86_64-efi type btrfs (rw,relatime,space_cache,subvolid=265,subvol=/@/boot/grub2/x86_64-efi)
  /dev/sda2 on /boot/grub2/i386-pc type btrfs (rw,relatime,space_cache,subvolid=266,subvol=/@/boot/grub2/i386-pc)
  /dev/sda2 on /home type btrfs (rw,relatime,space_cache,subvolid=264,subvol=/@/home)
  /dev/sda2 on /opt type btrfs (rw,relatime,space_cache,subvolid=263,subvol=/@/opt)
  /dev/sda2 on /root type btrfs (rw,relatime,space_cache,subvolid=262,subvol=/@/root)
  /dev/sda2 on /srv type btrfs (rw,relatime,space_cache,subvolid=261,subvol=/@/srv)
  /dev/sda2 on /tmp type btrfs (rw,relatime,space_cache,subvolid=260,subvol=/@/tmp)
  /dev/sda2 on /usr/local type btrfs (rw,relatime,space_cache,subvolid=259,subvol=/@/usr/local)
  /dev/sda2 on /var type btrfs (rw,relatime,space_cache,subvolid=258,subvol=/@/var)
  tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,size=201216k,nr_inodes=50304,mode=700,inode64)'
...
Something is still mounted within the build area
...
You must manually umount it, then you could manually remove the build area
...
To manually remove the build area use (with caution): rm -Rf --one-file-system
...
Terminated

The "tac: failed to create temporary file" comes from

function get_bash_flags_and_options_commands () {
    ...
    set +o | tac | tr '\n' ';'

because 'tac' needs TMPDIR when called in a pipe

# cat /etc/os-release | tac
tac: failed to create temporary file in 'QQQ': No such file or directory

in contrast to when 'tac' is called with a regular file
(at least for me with /usr/bin/tac from coreutils-8.32).

I also tried what happens on that test VM
when I do what ReaR tells to "manually remove the build area":

# rm -Rfv --one-file-system && echo y || echo n
y

# rm -Rfvi --one-file-system && echo y || echo n
rm: missing operand
Try 'rm --help' for more information.
n

Puh!
Basically by pure luck 'rm' does not remove anything
when there is nothing explicitly specified to be removed:

# rm
rm: missing operand
Try 'rm --help' for more information.

As far as I see the problem is in sbin/rear
only between successful

source $SHARE_DIR/lib/_input-output-functions.sh

and unsuccessful

BUILD_DIR="$( mktemp -d -t rear.XXXXXXXXXXXXXXX || Error "Could not create build area '$BUILD_DIR'" )"

and the basically only thing that is in between is

QuietAddExitTask cleanup_build_area_and_end_program

so the whole problematic code is

if ! source $SHARE_DIR/lib/_input-output-functions.sh ; then
    echo -e "ERROR: BUG in $PRODUCT\nFailed to source $SHARE_DIR/lib/_input-output-functions.sh\nPlease report it at $BUG_REPORT_SITE" >&2
    exit 1
fi
...
if test "$WORKFLOW" != "help" ; then
    # Prepend temporary working area (BUILD_DIR) removal exit task
    # so it gets executed directly before the above listed exit tasks.
    # This must be done before the first possible call of the 'Error' function
    # otherwise we may error out but leave the build area behind:
    QuietAddExitTask cleanup_build_area_and_end_program
    # Create temporary working area:
    BUILD_DIR="$( mktemp -d -t rear.XXXXXXXXXXXXXXX || Error "Could not create build area '$BUILD_DIR'" )"

which I did via
https://github.com/rear/rear/commit/3f4bc829ec106ecf3456e305d48a4f2423458303
that points to
https://github.com/rear/rear/pull/2633
therein 3f4bc82 appears only once just before
https://github.com/rear/rear/pull/2633#issuecomment-866666112

jsmeix commented at 2024-03-18 14:11:

@pcahyna
because you did much improve the
cleanup_build_area_and_end_program() related code
I dared to also assing you here because
I would much appreciate it
if you could also have a look here.

I think I will at least for now move the

QuietAddExitTask cleanup_build_area_and_end_program

directly after the

BUILD_DIR="$( mktemp -d -t rear.XXXXXXXXXXXXXXX ...

because if 'mktemp' fails then there is no
$TMPDIR/rear.XXXXXXXXXXXXXXX
so no build area is left behind
(and if at all only an empty $TMPDIR/rear.XXXXXXXXXXXXXXX
would be left behind).

I wonder if it is needed to be on the safe side
to manually call

rm -Rf --one-file-system $BUILD_DIR || true

when 'mktemp' fails or if this is superfluous because
when 'mktemp' fails, there cannot be a $BUILD_DIR?

jsmeix commented at 2024-03-18 15:53:

This issue will be fixed via
https://github.com/rear/rear/pull/3168
see
https://github.com/rear/rear/pull/3168#issuecomment-2004294531

jsmeix commented at 2024-03-26 12:23:

With https://github.com/rear/rear/pull/3168 merged
this issue is fixed.


[Export of Github issue for rear/rear.]