[Libguestfs] [p2v PATCH 3/4] make-disk: replace here-docs with printfs for better indentation

Laszlo Ersek lersek at redhat.com
Fri Jan 20 15:34:02 UTC 2023


On 1/19/23 19:56, Richard W.M. Jones wrote:
> On Thu, Jan 19, 2023 at 01:14:47PM +0100, Laszlo Ersek wrote:
>> Here-documents break the indentation of the script; replace them with the
>> "printf" shell utility. (We could use the <<- redirection operator for
>> stripping leading TABs from the here-docs, but the script itself does not
>> use TABs for indentation, so that would introduce a different kind of
>> inconsistency.)
>>
>> Because we use a quoted word ('EOF') for our delimiter in the
>> here-documents, variables in the here-docs are not expanded; stick with
>> that.
>>
>> Use a sole '%s\n' format operand with each printf invocation: "[t]he
>> /format/ operand shall be reused as often as necessary to satisfy the
>> /argument/ operands"
>> <https://pubs.opengroup.org/onlinepubs/9699919799/utilities/printf.html>.
>>
>> (I've tried to regression-test this with the two latest Ubuntu images:
>> ubuntu-18.04, ubuntu-20.04. The former wouldn't build, and the latter only
>> booted to a GDM login screen. My build host is RHEL-9.1, so the kiosk is
>> not actually expected to work. Any further investigation here is left to
>> Ubuntu users.
>>
>> The Fedora 37 image works fine, even with the virt-p2v binary coming from
>> RHEL-9.1.)
>>
>> Signed-off-by: Laszlo Ersek <lersek at redhat.com>
>> ---
>>  virt-p2v-make-disk.in | 28 ++++++++++++++--------------
>>  1 file changed, 14 insertions(+), 14 deletions(-)
>>
>> diff --git a/virt-p2v-make-disk.in b/virt-p2v-make-disk.in
>> index 44ff0b45ac7a..0bf7f0e0414f 100644
>> --- a/virt-p2v-make-disk.in
>> +++ b/virt-p2v-make-disk.in
>> @@ -170,15 +170,15 @@ xzcat "$virt_p2v_xz_binary" > "$virt_p2v_binary"
>>  case "$osversion" in
>>      centos-*|fedora-*|rhel-*|scientificlinux-*|oraclelinux-*)
>>          depsfile="$datadir/dependencies.redhat"
>> -        cat > $tmpdir/p2v.conf <<'EOF'
>> -add_drivers+=" usb-storage "
>> -EOF
>> -        cat > $tmpdir/post-install <<'EOF'
>> -#!/bin/bash
>> -# Rebuild the initramfs.
>> -latest_version="$(cd /lib/modules; ls -1vr | head -1)"
>> -dracut -v -f --kver $latest_version
>> -EOF
>> +        printf '%s\n'                      \
>> +            'add_drivers+=" usb-storage "' \
>> +            > $tmpdir/p2v.conf
>> +        printf '%s\n'                                                \
>> +            '#!/bin/bash'                                            \
>> +            '# Rebuild the initramfs.'                               \
>> +            'latest_version="$(cd /lib/modules; ls -1vr | head -1)"' \
>> +            'dracut -v -f --kver $latest_version'                    \
>> +            > $tmpdir/post-install
>>          # Double quotes because we want $tmpdir to be expanded:
>>          extra_args="
>>            --selinux-relabel
>> @@ -201,11 +201,11 @@ EOF
>>          ;;
>>      debian-*|ubuntu-*)
>>          depsfile="$datadir/dependencies.debian"
>> -        cat > $tmpdir/policy-rc.d <<'EOF'
>> -#!/bin/sh
>> -# Avoid running daemons during the upgrade/installation
>> -exit 101
>> -EOF
>> +        printf '%s\n' \                                               \
>> +            '#!/bin/sh'                                               \
>> +            '# Avoid running daemons during the upgrade/installation' \
>> +            'exit 101'                                                \
>> +            > $tmpdir/policy-rc.d
>>          chmod +x $tmpdir/policy-rc.d
>>          # Double quotes because we want $tmpdir to be expanded:
>>          preinstall_args="
> 
> Interesting - didn't know this would work ...
> 
> $ printf '%s\n' 'abc' 'def' 123
> abc
> def
> 123
> 
> It seems like it applies the format pattern to every parameter?!
> 
> I verified this works both with bash printf and coreutils.
> 
> bash info says:
> 
>      The FORMAT is reused as necessary to consume all of the ARGUMENTS.
>      If the FORMAT requires more ARGUMENTS than are supplied, the extra
>      format specifications behave as if a zero value or null string, as
>      appropriate, had been supplied.  The return value is zero on
>      success, non-zero on failure.
> 
> and almost the same text appears in coreutils info.

POSIX even provides an informative EXAMPLES section that demonstrates
the behavior when you have N > 1 conversion specifiers in the /format/
operand, but M > 0 /argument/ operands where M is not a whole multiple of N:

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/printf.html#tag_20_94_17

printf "%5d%4d\n" 1 21 321 4321 54321

    1  21
  3214321
54321   0

There is no /argument/ operand for %4d in the third iteration, so 0 is
substituted for it.

That too happens by rule#9
<https://pubs.opengroup.org/onlinepubs/9699919799/utilities/printf.html#tag_20_94_13>.

> So there we are.  ACK!

Series merged as commit range 2082cf98add8..f4c7ae6ba98f.

Thanks!
Laszlo



More information about the Libguestfs mailing list