[Ovirt-devel] [PATCH node-image] Add convenience script for editing a Node livecd

Perry Myers pmyers at redhat.com
Sat Nov 8 08:48:19 UTC 2008


Also add script for creating fake nodes using a livecd for boot

Signed-off-by: Perry Myers <pmyers at redhat.com>
---
 Makefile.am              |    4 +-
 create-ovirt-iso-nodes   |   96 +++++++++++++++++++++++++++++++++++++++++
 edit-livecd              |  107 ++++++++++++++++++++++++++++++++++++++++++++++
 ovirt-node-image.spec.in |    4 ++
 4 files changed, 210 insertions(+), 1 deletions(-)
 create mode 100755 create-ovirt-iso-nodes
 create mode 100755 edit-livecd

diff --git a/Makefile.am b/Makefile.am
index a3d4828..ab0623f 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -31,7 +31,9 @@ EXTRA_DIST =				\
   ovirt-flash				\
   ovirt-flash-static		\
   ovirt-node-image.ks		\
-  ovirt-pxe
+  ovirt-pxe					\
+  edit-livecd				\
+  create-ovirt-iso-nodes
 
 DISTCLEANFILES = $(PACKAGE)-$(VERSION).tar.gz
 
diff --git a/create-ovirt-iso-nodes b/create-ovirt-iso-nodes
new file mode 100755
index 0000000..5864439
--- /dev/null
+++ b/create-ovirt-iso-nodes
@@ -0,0 +1,96 @@
+#!/bin/bash
+#
+# Create fake oVirt Nodes for testing CDROM boot
+# Copyright 2008 Red Hat, Inc.
+# Written by Perry Myers <pmyers at redhat.com>
+#
+# 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; version 2 of the License.
+#
+# 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 Library General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+PATH=$PATH:/sbin:/usr/sbin
+
+ME=$(basename "$0")
+warn() { printf '%s: %s\n' "$ME" "$*" >&2; }
+try_h() { printf "Try \`$ME -h' for more information.\n" >&2; }
+die() { warn "$@"; try_h; exit 1; }
+
+BRIDGENAME=ovirtbr0
+IMGDIR_DEFAULT=/var/lib/libvirt/images
+NODEIMG_DEFAULT=$(rpm --eval='%_datadir')/$(rpm -q --qf '%{name}' ovirt-node-image)/ovirt-node-image.iso
+imgdir=$IMGDIR_DEFAULT
+nodeimg=$NODEIMG_DEFAULT
+
+# first, check to see we are root
+if [ $( id -u ) -ne 0 ]; then
+    die "Must run as root"
+fi
+
+gen_fake_managed_node() {
+    local num=$1
+    local nodeimg=$2
+    local last_mac=$(( 54 + $num ))
+
+    echo "Creating fake node$num using $nodeimg..."
+    virsh destroy node$num > /dev/null 2>&1
+    virsh undefine node$num > /dev/null 2>&1
+    virt-install --name=node$num --ram=512 --vcpus=1 \
+        --file $imgdir/node${i}-sda.raw --file-size=.15 \
+        --network=bridge:$BRIDGENAME --mac=00:16:3e:12:34:$last_mac \
+        --cdrom=$nodeimg \
+        --vnc --accelerate --hvm --noacpi --noautoconsole \
+        --os-type=linux --os-variant=fedora8 \
+        --force --noreboot
+    virsh destroy node$num > /dev/null 2>&1
+    TMPXML=$(mktemp) || exit 1
+    virsh dumpxml node$num | sed \
+        -e "s/boot dev='.*'/boot dev='cdrom'/" \
+        -e "s^\(device='cdrom'>\)^\1\n<source file='$nodeimg'/>^" > $TMPXML
+    virsh define $TMPXML
+    rm -f $TMPXML
+    echo "node$num created"
+}
+
+usage() {
+    case $# in 1) warn "$1"; try_h; exit 1;; esac
+    cat <<EOF
+Usage: $ME [-d image_dir] [-n node.iso]
+  -n: node.iso to boot (default: $NODEIMG_DEFAULT)
+  -d: directory to place virtual disk (default: $IMGDIR_DEFAULT)
+  -h: display this help and exit
+EOF
+}
+
+err=0 help=0
+while getopts :d:n:h c; do
+    case $c in
+        n) nodeimg=$OPTARG;;
+        d) imgdir=$OPTARG;;
+        h) help=1;;
+        '?') err=1; warn "invalid option: \`-$OPTARG'";;
+        :) err=1; warn "missing argument to \`-$OPTARG' option";;
+        *) err=1; warn "internal error: \`-$OPTARG' not handled";;
+    esac
+done
+test $err = 1 && { try_h; exit 1; }
+test $help = 1 && { usage; exit 0; }
+
+mkdir -p $imgdir
+
+test -f $nodeimg || die "could not find $nodeimg"
+cp $nodeimg $imgdir
+
+# define the fake managed nodes we will use.
+for i in `seq 3 5` ; do
+    gen_fake_managed_node $i $imgdir/$(basename $nodeimg)
+done
+
diff --git a/edit-livecd b/edit-livecd
new file mode 100755
index 0000000..18e9e7a
--- /dev/null
+++ b/edit-livecd
@@ -0,0 +1,107 @@
+#!/bin/bash
+#
+# Edit a livecd to insert files
+# Copyright 2008 Red Hat, Inc.
+# Written by Perry Myers <pmyers at redhat.com>
+#
+# 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; version 2 of the License.
+#
+# 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 Library General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#!/bin/bash
+
+# exit after any error:
+set -e
+
+export PATH=$PATH:/sbin:/usr/sbin
+
+# first, check to see we are root
+if [ $( id -u ) -ne 0 ]; then
+    echo "Must run as root"
+    exit 1
+fi
+
+if [[ $# < 2 ]]; then
+   echo "usage: $0 livecd.iso script"
+   exit 1
+fi
+
+CD=$1
+SCRIPT=$2
+
+which mkisofs mksquashfs sed
+
+WDIR=`mktemp -d $PWD/livecd.XXXXXXXXXX`
+ISO="${CD##*/}"
+ISO="${ISO%.iso}-custom.iso"
+EXIT=""
+
+function addExit() {
+    EXIT="$@ ; $EXIT"
+    trap "$EXIT" EXIT HUP TERM INT QUIT
+}
+
+function mnt() {
+    local margs="$1" ; shift
+    local mp="$WDIR/$1"
+    for D in "$@" ; do
+        mkdir -v -p "$WDIR/$D"
+    done
+    mount -v $margs "$mp"
+    addExit "df | grep $mp > /dev/null 2>&1 && umount -v $mp"
+}
+
+LABEL=$(isoinfo -d -i $CD | awk -F ": " '/Volume id:/ {print $2}')
+
+# mount the CD image
+mnt "-t auto $CD -o loop,ro" cd
+
+# mount compressed filesystem
+mnt "-t squashfs $WDIR/cd/LiveOS/squashfs.img -o ro,loop" sq
+
+# create writable copy of the new filesystem for the CD
+cp -a $WDIR/cd $WDIR/cd-w
+
+# create writable copy of the filesystem for the new compressed squashfs filesystem
+cp -a $WDIR/sq $WDIR/sq-w
+
+# mount ext3 filesystem
+mnt "-t auto $WDIR/sq-w/LiveOS/ext3fs.img -o rw,loop" ex
+
+echo ">>> Updating CD content"
+
+cp -av $SCRIPT $WDIR/ex
+pushd $WDIR/ex
+./$SCRIPT
+popd
+rm -f $WDIR/ex/$SCRIPT
+
+echo ">>> Unmounting ext3fs"
+umount $WDIR/ex
+
+echo ">>> Compressing filesystem"
+mksquashfs $WDIR/sq-w/ $WDIR/cd-w/LiveOS/squashfs.img -noappend
+
+echo ">>> Recomputing MD5 sums"
+( cd $WDIR/cd-w && find . -type f -not -name md5sum.txt -not -path '*/isolinux/*' -print0 | xargs -0 -- md5sum > md5sum.txt )
+
+echo ">>> Creating ISO image $ISO"
+mkisofs \
+    -V "$LABEL" \
+    -r -cache-inodes -J -l \
+    -b isolinux/isolinux.bin \
+    -c isolinux/boot.cat \
+    -no-emul-boot -boot-load-size 4 -boot-info-table \
+    -o "$ISO" \
+    $WDIR/cd-w
+
+# The trap ... callbacks will unmount everything.
+set +e
diff --git a/ovirt-node-image.spec.in b/ovirt-node-image.spec.in
index 086dff6..fff4210 100644
--- a/ovirt-node-image.spec.in
+++ b/ovirt-node-image.spec.in
@@ -67,6 +67,8 @@ mkdir %{buildroot}
 %{__install} -p -m0755 ovirt-pxe %{buildroot}%{_sbindir}
 %{__install} -p -m0755 ovirt-flash %{buildroot}%{_sbindir}
 %{__install} -p -m0755 ovirt-flash-static %{buildroot}%{_sbindir}
+%{__install} -p -m0755 edit-livecd %{buildroot}%{_sbindir}
+%{__install} -p -m0755 create-ovirt-iso-nodes %{buildroot}%{_sbindir}
 
 %clean
 %{__rm} -rf %{buildroot}
@@ -81,6 +83,8 @@ cobbler sync > /dev/null 2>&1 || :
 %{_sbindir}/ovirt-pxe
 %{_sbindir}/ovirt-flash
 %{_sbindir}/ovirt-flash-static
+%{_sbindir}/edit-livecd
+%{_sbindir}/create-ovirt-iso-nodes
 
 %files pxe
 %defattr(-,root,root,0644)
-- 
1.6.0.3




More information about the ovirt-devel mailing list