Jim Meyering
2008-Dec-02 17:10 UTC
[Ovirt-devel] [PATCH node-image] clean up quoting, cleanup-on-exception, use mktemp-not-/tmp, etc.
--- ovirt-flash-static | 77 ++++++++++++++++++++++++++++++++++++--------------- 1 files changed, 54 insertions(+), 23 deletions(-) diff --git a/ovirt-flash-static b/ovirt-flash-static index 4c2bb22..00b26de 100755 --- a/ovirt-flash-static +++ b/ovirt-flash-static @@ -26,39 +26,70 @@ test $# != 2 && die "Usage: $ME <usbdevice> <iso-image>" USBDEVICE=$1 ISO=$2 -test ! -r $ISO && die "$ISO is not a readable file" -test ! -b $USBDEVICE && die "$USBDEVICE is not a valid block device" +test ! -r "$ISO" && die "$ISO is not a readable file" +test ! -b "$USBDEVICE" && die "$USBDEVICE is not a valid block device" test $( id -u ) -ne 0 && die "$ME must run as root" -IMGTMP=/var/tmp/ovirt-$$ -SQUASHTMP=/var/tmp/ovirt-squash-$$ -USBTMP=/var/tmp/ovirt-usb-$$ +case $ISO in + *.iso) ;; + *) die "ME: ISO file name, '$ISO' lacks .iso suffix" +esac + +tmpdir=$(mktemp -d) || exit 1 + +IMGTMP="$tmpdir/ovirt" +SQUASHTMP="$tmpdir/ovirt-squash" +USBTMP="$tmpdir/ovirt-usb" + +cleanup() +{ + { umount "$USBTMP" + umount "$SQUASHTMP" + umount "$IMGTMP" + } 2> /dev/null || : + rm -rf "$tmpdir" +} +trap cleanup 0 +trap 'Exit $?' 1 2 13 15 + +# From here on, any failure makes the script fail. +set -e # do setup -mkdir -p $IMGTMP $SQUASHTMP $USBTMP -mount -o loop $ISO $IMGTMP -mount -o loop $IMGTMP/LiveOS/squashfs.img $SQUASHTMP +mkdir -p "$IMGTMP" "$SQUASHTMP" "$USBTMP" +mount -o loop "$ISO" "$IMGTMP" + +squashfs_img="$IMGTMP/LiveOS/squashfs.img" +find "$IMGTMP" # FIXME +test -f "$squashfs_img" \ + || die "not a LiveCD image: $ISO" + +mount -o loop "$squashfs_img" "$SQUASHTMP" # clear out the old partition table -dd if=/dev/zero of=$USBDEVICE bs=4096 count=1 -printf 'n\np\n1\n\n\nt\n83\na\n1\nw\n' | fdisk $USBDEVICE +dd if=/dev/zero of="$USBDEVICE" bs=4096 count=1 +printf 'n\np\n1\n\n\nt\n83\na\n1\nw\n' | fdisk "$USBDEVICE" -cat /usr/lib/syslinux/mbr.bin > $USBDEVICE -dd if=$SQUASHTMP/LiveOS/ext3fs.img of=${USBDEVICE}1 +cat /usr/lib/syslinux/mbr.bin > "$USBDEVICE" +dd if="$SQUASHTMP/LiveOS/ext3fs.img" of="${USBDEVICE}1" -mount ${USBDEVICE}1 $USBTMP +mount "${USBDEVICE}1" "$USBTMP" -cp $IMGTMP/isolinux/* $USBTMP +cp "$IMGTMP"/isolinux/* "$USBTMP" -rm -f $USBTMP/isolinux.bin -mv $USBTMP/isolinux.cfg $USBTMP/extlinux.conf +rm -f "$USBTMP/isolinux.bin" +mv "$USBTMP/isolinux.cfg" "$USBTMP/extlinux.conf" -LABEL=`echo $ISO | cut -d'.' -f1 | cut -c-16` -sed -i -e "s/ *append.*/ append initrd=initrd.img root=LABEL=$LABEL ro/" $USBTMP/extlinux.conf +iso_base=$(basename "$ISO" .iso) +# sanitize for sed and the label name and limit to 16 bytes +LABEL=$(echo "$iso_base" | cut -b-16 | tr -c '[[:alnum:]_.-]' _) +sed -i -e "s/ *append.*/ append initrd=initrd.img root=LABEL=$LABEL ro/" \ + "$USBTMP/extlinux.conf" -extlinux -i $USBTMP +extlinux -i "$USBTMP" -umount $USBTMP -umount $SQUASHTMP -umount $IMGTMP -rm -rf $SQUASHTMP $IMGTMP $USBTMP +# To test: +cat <<\EOF > /dev/null +mkdir -p t/LiveOS && (cd t/LiveOS && touch ext3fs.img squashfs.img) +genisoimage -U -o k2.iso t +EOF -- 1.6.1.rc1.279.g45d11
Jim Meyering
2008-Dec-05 18:49 UTC
[Ovirt-devel] [PATCH node-image] clean up quoting, cleanup-on-exception, use mktemp-not-/tmp, etc.
I've just pushed the following with the two marked corrections: Jim Meyering <jim at meyering.net> wrote:> --- > ovirt-flash-static | 77 ++++++++++++++++++++++++++++++++++++--------------- > 1 files changed, 54 insertions(+), 23 deletions(-) > > diff --git a/ovirt-flash-static b/ovirt-flash-static...> +cleanup() > +{ > + { umount "$USBTMP" > + umount "$SQUASHTMP" > + umount "$IMGTMP" > + } 2> /dev/null || : > + rm -rf "$tmpdir" > +} > +trap cleanup 0 > +trap 'Exit $?' 1 2 13 15s/Exit/exit/ ...> +find "$IMGTMP" # FIXMERemove the debug "find".