Laszlo Ersek
2022-Aug-26 11:39 UTC
[Libguestfs] [p2v PATCH 1/4] Makefile.am: factor out "make-physical-machine.sh"
Extract and somewhat generalize the recipe for the $(PHYSICAL_MACHINE) target to a separate shell script. In preparation for the multiple steps we're going to introduce later, redirect virt-builder to a temp file at first (placed in the same directory as the finally expected disk image), and rename that file upon success. Signed-off-by: Laszlo Ersek <lersek at redhat.com> --- Makefile.am | 5 ++- .gitignore | 1 + make-physical-machine.sh | 37 ++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/Makefile.am b/Makefile.am index d94ac6fdaf94..f25de74b8690 100644 --- a/Makefile.am +++ b/Makefile.am @@ -41,6 +41,7 @@ EXTRA_DIST = \ kiwi-config.xml.in \ launch-virt-p2v \ libguestfs/README \ + make-physical-machine.sh \ miniexpect/README \ p2v.ks.in \ p2v.service \ @@ -66,6 +67,7 @@ CLEANFILES += \ $(generated_sources) \ $(PHYSICAL_MACHINE) $(BLANK_DISK) \ about-authors.c \ + physical-machine.tmp.* \ stamp-test-virt-p2v-pxe-data-files \ stamp-test-virt-p2v-pxe-kernel \ test-virt-p2v-pxe.authorized_keys \ @@ -255,6 +257,7 @@ if HAVE_LIBGUESTFS check_DATA += \ $(PHYSICAL_MACHINE) \ $(BLANK_DISK) +check_SCRIPTS = make-physical-machine.sh endif HAVE_LIBGUESTFS run-virt-p2v-directly: $(PHYSICAL_MACHINE) @@ -317,7 +320,7 @@ run-virt-p2v-non-gui-conversion: $(PHYSICAL_MACHINE) stamp-test-virt-p2v-pxe-dat SLOW=1 $(top_builddir)/run ./test-virt-p2v-pxe.sh $(PHYSICAL_MACHINE): - $(top_builddir)/run virt-builder --format raw -o $@ fedora-35 + $(top_builddir)/run ./make-physical-machine.sh $@ $(BLANK_DISK): $(top_builddir)/run guestfish -N $@=part exit diff --git a/.gitignore b/.gitignore index a870e2b6186b..eea5128411c2 100644 --- a/.gitignore +++ b/.gitignore @@ -41,6 +41,7 @@ Makefile.in /m4/ltsugar.m4 /m4/ltversion.m4 /p2v-config.h +/physical-machine.tmp.* /podwrapper.pl /run /stamp-h1 diff --git a/make-physical-machine.sh b/make-physical-machine.sh new file mode 100755 index 000000000000..fb42cda34fca --- /dev/null +++ b/make-physical-machine.sh @@ -0,0 +1,37 @@ +#!/bin/bash - +# Copyright (C) 2022 Red Hat Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +set -e -u -C + +disk+ +cleanup() +{ + set +e + if test -n "$disk"; then + rm -f -- "$disk" + disk+ fi +} + +trap cleanup EXIT + +output=$1 +outdir=$(dirname -- "$output") +disk=$(mktemp -p "$outdir" physical-machine.tmp.XXXXXXXXXX) +virt-builder --format raw -o "$disk" fedora-35 +mv -- "$disk" "$output" +disk=
Eric Blake
2022-Aug-26 12:39 UTC
[Libguestfs] [p2v PATCH 1/4] Makefile.am: factor out "make-physical-machine.sh"
On Fri, Aug 26, 2022 at 01:39:05PM +0200, Laszlo Ersek wrote:> Extract and somewhat generalize the recipe for the $(PHYSICAL_MACHINE) > target to a separate shell script. In preparation for the multiple steps > we're going to introduce later, redirect virt-builder to a temp file at > first (placed in the same directory as the finally expected disk image), > and rename that file upon success. > > Signed-off-by: Laszlo Ersek <lersek at redhat.com> > ---> +++ b/make-physical-machine.sh > @@ -0,0 +1,37 @@ > +#!/bin/bash -See the response in the other thread about not needing the - here. Are we sure that /bin/bash is on all systems where this script will be run, or is it better as '#!/bin/env bash'?> + > +set -e -u -CMy personal opinion is that 'set -e' is a crutch that should be avoided because of its unintended interaction with functions; but I'm not adamant enough about it to tell people to rip it out of scripts. For short scripts, like this one, it's easy enough to check that we aren't tripping over any of -e's surprises.> + > +disk> + > +cleanup() > +{ > + set +e > + if test -n "$disk"; then > + rm -f -- "$disk" > + disk> + fi > +} > + > +trap cleanup EXITIs it intentional that you are not also cleaning up on signals like INT and HUP?> + > +output=$1 > +outdir=$(dirname -- "$output") > +disk=$(mktemp -p "$outdir" physical-machine.tmp.XXXXXXXXXX) > +virt-builder --format raw -o "$disk" fedora-35 > +mv -- "$disk" "$output" > +disk-- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org