fix Build-requires when image ISO is in SRPM
[ovirt-node-image.git] / ovirt-flash-static
blob79bcfbf74a1904ec219daf4448435edf5d543af3
1 #!/bin/bash
3 # Create an Ovirt Host USB device (stateful)
4 # Copyright 2008 Red Hat, Inc.
5 # Written by Chris Lalancette <clalance@redhat.com>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; version 2 of the License.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Library General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 ME=$(basename "$0")
21 warn() { printf '%s: %s\n' "$ME" "$*" >&2; }
22 die() { warn "$@"; exit 1; }
24 test $# != 2 && die "Usage: $ME <usbdevice> <iso-image>"
26 USBDEVICE=$1
27 ISO=$2
29 test ! -r "$ISO" && die "$ISO is not a readable file"
30 test ! -b "$USBDEVICE" && die "$USBDEVICE is not a valid block device"
31 test $( id -u ) -ne 0 && die "$ME must run as root"
33 case $ISO in
34 *.iso) ;;
35 *) die "ME: ISO file name, '$ISO' lacks .iso suffix"
36 esac
38 tmpdir=$(mktemp -d) || exit 1
40 IMGTMP="$tmpdir/ovirt"
41 SQUASHTMP="$tmpdir/ovirt-squash"
42 USBTMP="$tmpdir/ovirt-usb"
44 cleanup()
46 { umount "$USBTMP"
47 umount "$SQUASHTMP"
48 umount "$IMGTMP"
49 } 2> /dev/null || :
50 rm -rf "$tmpdir"
52 trap cleanup 0
53 trap 'exit $?' 1 2 13 15
55 # From here on, any failure makes the script fail.
56 set -e
58 # do setup
59 mkdir -p "$IMGTMP" "$SQUASHTMP" "$USBTMP"
60 mount -o loop "$ISO" "$IMGTMP"
62 squashfs_img="$IMGTMP/LiveOS/squashfs.img"
63 test -f "$squashfs_img" \
64 || die "not a LiveCD image: $ISO"
66 mount -o loop "$squashfs_img" "$SQUASHTMP"
68 # clear out the old partition table
69 dd if=/dev/zero of="$USBDEVICE" bs=4096 count=1
70 printf 'n\np\n1\n\n\nt\n83\na\n1\nw\n' | fdisk "$USBDEVICE"
72 cat /usr/lib/syslinux/mbr.bin > "$USBDEVICE"
73 dd if="$SQUASHTMP/LiveOS/ext3fs.img" of="${USBDEVICE}1"
75 mount "${USBDEVICE}1" "$USBTMP"
77 cp "$IMGTMP"/isolinux/* "$USBTMP"
79 rm -f "$USBTMP/isolinux.bin"
80 mv "$USBTMP/isolinux.cfg" "$USBTMP/extlinux.conf"
82 iso_base=$(basename "$ISO" .iso)
83 # sanitize for sed and the label name and limit to 16 bytes
84 LABEL=$(echo "$iso_base" | cut -b-16 | tr -c '[[:alnum:]_.-]' _)
85 sed -i -e "s/ *append.*/ append initrd=initrd.img root=LABEL=$LABEL ro/" \
86 "$USBTMP/extlinux.conf"
88 extlinux -i "$USBTMP"
90 # To test:
91 cat <<\EOF > /dev/null
92 mkdir -p t/LiveOS && (cd t/LiveOS && touch ext3fs.img squashfs.img)
93 genisoimage -U -o k2.iso t
94 EOF