#1376 Issue closed: grep with Perl regular expression causes failures (native grep extended regular expression works)

Labels: bug, fixed / solved / done

bingham-ctx opened issue at 2017-06-06 18:57:

So I'm running into the "Filesystem for device '/dev/sdXN' could not be found' issue that I'm seeing reported by a few other users but thus-far I can't seem to figure out why or how to fix it.

Steps that I'm using:

  1. Attach USB drive and run rear format /dev/sdb (log not attached as this is completing without issue).
  2. Build the rescue image and send it to the USB drive using rear -v -d -D mkrescue since I use Bareos for backup.
[root@devlamp2 ~]# rear -v -d -D mkrescue
Relax-and-Recover 2.00 / Git
Using log file: /var/log/rear/rear-devlamp2.log
Creating disk layout
Creating root filesystem layout
TIP: To login as root via ssh you need to set up /root/.ssh/authorized_keys or SSH_ROOT_PASSWORD in your configuration file
Copying logfile /var/log/rear/rear-devlamp2.log into initramfs as '/tmp/rear-devlamp2-partial-2017-06-06T11:25:10-0700.log'
Copying files and directories
Copying binaries and libraries
Copying kernel modules
Creating initramfs
Saved /var/log/rear/rear-devlamp2.log as rear/devlamp2/20170606.1124/rear-devlamp2.log
ERROR:
====================
BUG in /usr/share/rear/output/USB/Linux-i386/850_make_USB_bootable.sh:
'Filesystem for device '/dev/sdb1' could not be found'
--------------------
Please report this issue at https://github.com/rear/rear/issues
and include the relevant parts from /var/log/rear/rear-devlamp2.log
preferably with full debug information via 'rear -d -D mkrescue'
====================
Aborting due to an error, check /var/log/rear/rear-devlamp2.log for details
You should also rm -Rf /tmp/rear.YTLUFaIiVrU3342
Terminated

I've tried adding sleep 10 to the 850_make_USB_bootable.sh script both before and after it tries to parse the filesystem type from /proc/mounts and I see the drive mounted during those sleep periods and can parse the type using the same grep command from another ssh session.

[root@devlamp2 ~]# USB_DEVICE=/dev/disk/by-label/REAR-000; REAL_USB_DEVICE=/dev/sdb1; usb_filesystem=$(grep -E "^($USB_DEVICE|$REAL_USB_DEVICE)\\s" /proc/mounts | cut -d' ' -f3 | tail -1); echo $usb_filesystem
ext3

However, for some reason the script is unable to.

Environment Information below and log from rear -v -d -D mkrescue attached:

  • rear version (/usr/sbin/rear -V): Relax-and-Recover 2.00 / Git Installed via yum/rpm
  • OS version (cat /etc/rear/os.conf or lsb_release -a):
OS_VENDOR=RedHatEnterpriseServer
OS_VERSION=5

LSB Version:    :core-3.1-ia32:core-3.1-noarch:graphics-3.1-ia32:graphics-3.1-noarch
Distributor ID: CentOS
Description:    CentOS release 5.2 (Final)
Release:        5.2
Codename:       Final
  • rear configuration files (cat /etc/rear/site.conf or cat /etc/rear/local.conf):
OUTPUT=USB
USB_DEVICE=/dev/disk/by-label/REAR-000
BACKUP=BAREOS
BAREOS_CLIENT=devlamp2-fd
BAREOS_FILESET="Linux - All Filesystems"
GRUB_RESCUE=1
  • Are you using legacy BIOS or UEFI boot? BIOS
  • Contents of /proc/mounts with USB drive manually mounted:
[root@devlamp2 ~]# mkdir /tmp/rear-test; mount -v -o rw,noatime /dev/disk/by-label/REAR-000 /tmp/rear-test; echo; cat /proc/mounts; umount /tmp/rear-test; rmdir /tmp/rear-test/
mount: you didn't specify a filesystem type for /dev/disk/by-label/REAR-000
       I will try type ext3
/dev/sdb1 on /tmp/rear-test type ext3 (rw,noatime)

rootfs / rootfs rw 0 0
/dev/root / ext3 rw,data=ordered 0 0
/dev /dev tmpfs rw 0 0
/proc /proc proc rw 0 0
/sys /sys sysfs rw 0 0
/proc/bus/usb /proc/bus/usb usbfs rw 0 0
devpts /dev/pts devpts rw 0 0
/dev/sda1 /boot ext3 rw,data=ordered 0 0
tmpfs /dev/shm tmpfs rw 0 0
none /proc/sys/fs/binfmt_misc binfmt_misc rw 0 0
sunrpc /var/lib/nfs/rpc_pipefs rpc_pipefs rw 0 0
/etc/auto.misc /misc autofs rw,fd=6,pgrp=7607,timeout=300,minproto=5,maxproto=5,indirect 0 0
-hosts /net autofs rw,fd=12,pgrp=7607,timeout=300,minproto=5,maxproto=5,indirect 0 0
nfsd /proc/fs/nfsd nfsd rw 0 0
/dev/disk/by-label/REAR-000 /tmp/rear-test ext3 rw,noatime,data=ordered 0 0

rear-devlamp2.log.txt

jsmeix commented at 2017-06-07 12:09:

@bingham-ctx
only an offhanded (and untested) idea:

Can you replace the sophisticated Perl regular expression

grep -E "^($USB_DEVICE|$REAL_USB_DEVICE)\\s"

in the 850_make_USB_bootable.sh script by something simpler
e.g. a simple native grep extended regular expression like

grep -E "^$USB_DEVICE |^$REAL_USB_DEVICE "

and try out if that makes it work?

FYI:
one may like to use more sophisticated syntax for the space
character in the grep extended regular expression but
I think that won't help in the end because the subsequent

cut -d' ' -f3

requires single space as delimter - otherwise it results:

# echo 'foo  bar  baz' | cut -d' ' -f3
bar

jsmeix commented at 2017-06-07 12:16:

@phracek

git log -p --follow usr/share/rear/output/USB/Linux-i386/850_make_USB_bootable.sh

tells me that you replaced 'grep -P' with 'grep -E' via
https://github.com/rear/rear/commit/c0eaef1a3afb4ec9d5981b9501ff7c1c9edc8fce

Can you tell more about that change and the
reasoning behind?

In particular why 'grep -P' was replaced with 'grep -E'
but the sophisticated Perl regular expression is still used?

bingham-ctx commented at 2017-06-07 21:28:

@jsmeix
Your suggestion to change 850_make_USB_bootable.sh to use the grep extended regex yielded a successful execution of the mkrescue command. The debug log for the mkresuce is attached. rear-devlamp2.log.txt

Still trying to confirm that the resulting USB drive is usable. Most of my systems are VMware VMs and getting them to boot to a USB is proving more difficult than I expected.

jsmeix commented at 2017-06-08 10:49:

By a very quick initial search only via

find usr/sbin/rear usr/share/rear/ | xargs grep 'grep.*\\s'

I found the following cases of grep with Perl regular expressions

usr/share/rear/layout/save/default/310_autoexclude_usb.sh:
        local usb_mntpt=$(grep -E "^$REAL_USB_DEVICE\s" /proc/mounts | cut -d" " -f2 | tail -1)

usr/share/rear/output/USB/Linux-i386/850_make_USB_bootable.sh:
usb_filesystem=$(grep -E "^($USB_DEVICE|$REAL_USB_DEVICE)\\s" /proc/mounts | cut -d' ' -f3 | tail -1)

usr/share/rear/prep/USB/Linux-i386/350_check_usb_disk.sh:
StopIfError "USB device '$USB_DEVICE' is already mounted on $(grep -E "^$REAL_USB_DEVICE\\s" /proc/mounts | cut -d' '
 -f2 |tail -1)"

but there are probably much more...

jsmeix commented at 2017-06-08 10:51:

@bingham-ctx
regarding VMware VMs and getting them to boot to a USB:
This is a separated issue and I would like to not mix up
that separated issue with this issue here.
For each separated issue open a separated GitHub issue.
But I think making VMware VMs to boot to a USB
is not a ReaR issue.

jsmeix commented at 2017-06-08 14:31:

I also found this grep with Perl regular expression in
usr/share/rear/output/USB/Linux-i386/300_create_extlinux.sh

function get_usb_syslinux_version {
    for file in $BUILD_DIR/outputfs/{boot/syslinux,}/{ld,ext}linux.sys; do
        if [[ -s "$file" ]];  then
            strings $file | grep -E -m1 "^(EXT|SYS)LINUX \\d+.\\d+" | cut -d' ' -f2
            return 0
        fi
    done
    return 1
}

but currently I don't know how to fix it properly
because I don't know enough about the context.

@gdha

git log -p --follow usr/share/rear/output/USB/Linux-i386/300_create_extlinux.sh

tells that you replaced here the grep -P with grep -E via
https://github.com/rear/rear/commit/a7e001ef10f3a3dd95247ac8954097c270aeda26
which also lists several other places where
grep with Perl regular expression is used.
The reason was https://github.com/rear/rear/issues/565
Could you have a look at those code places and
try to replace Perl regular expressions with
native grep (extended) regular expressions?

jsmeix commented at 2017-06-08 14:45:

According to how I understand the Red Hat issue

SELinux is preventing /usr/bin/grep from using
the 'execmem' accesses on a process.

https://bugzilla.redhat.com/show_bug.cgi?id=1079534#c8
and
https://bugzilla.redhat.com/show_bug.cgi?id=1079534#c15

This comes from pcre
...
Just observing that grep doesn't use PROT_EXEC in mmap(),
but libpcre does.

plain replacing 'grep -P' with 'grep -E' does not fix anything
unless also the regular expression is converted from
a Perl regular expression into a native grep (extended)
regular expression.

bingham-ctx commented at 2017-06-09 17:59:

@jsmeix, regarding your comment about USB & VM boot... yeah. I agree that the USB/VM issue is a separate issue and likely not an issue with ReaR. I only brought it up because I'm still trying to test the built USB since the suggested change. Apologies for any confusion or implication that I was clouding this issue. I'll continue testing that issue and will open another issue if I feel I've sufficiently eliminated other potential causes.

Still trying to get a bare metal test box that I can use to test the whole shebang (rear format, rear mkrescue, and boot to USB followed by Bareos recovery) after the suggested modification.

jsmeix commented at 2017-06-22 10:38:

With https://github.com/rear/rear/pull/1378 merged
some perl regexp should now be fixed
but others still need to be checked and also fixed,
cf. https://github.com/rear/rear/issues/1376#issuecomment-307121735
so that currently this issue is not yet fixed.

jsmeix commented at 2017-07-14 10:44:

@gdha
cloud you please have a look here - if time permits.
Perhaps something more could be fixed for ReaR v 2.2.
But if not could you postpone it to ReaR v 2.3?

gdha commented at 2017-07-19 15:10:

post-pone to release 2.3

gdha commented at 2017-11-27 17:35:

@jsmeix To fix https://github.com/rear/rear/issues/1376#issuecomment-307121735 we need modify the line to:

strings /usr/sbin/extlinux | grep -E -m1 "^(EXT|SYS)LINUX\\s" | cut -d' ' -f2

I will do this tomorrow

gdha commented at 2017-12-08 11:16:

Can we say it is sufficient fixed?

jsmeix commented at 2017-12-08 12:18:

I think so.


[Export of Github issue for rear/rear.]