[Ovirt-devel] [PATCH node-image] Added script for opening up livecd (Node) images to edit/add files

Perry Myers pmyers at redhat.com
Sun Nov 9 02:52:38 UTC 2008


This script is useful for people who don't have the development and
yum infrastructure set up to build nodes from scratch.  Changes
to packaging and binaries should always be done by modifying kickstart
and rebuilding via livecd-creator.  But if all you want to do is add
a config file, public key for SSH or change the root password this
can be done with the edit-livecd script.

Included a simple script to be used in conjunction with edit-livecd
that allows you to set the root password and provide an authorized_keys
file to be embedded in the Node.  It can be used as follows:

sudo edit-livecd /usr/share/ovirt-node-image/ovirt-node-image.iso \
     /usr/sbin/livecd-setauth

This will output a file in the current working directory called:
ovirt-node-image-custom.iso

Which can then be added to a fake node with:

sudo create-ovirt-iso-nodes -n ovirt-node-image-custom.iso

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

diff --git a/Makefile.am b/Makefile.am
index 8f7d7b9..575e95d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -32,7 +32,9 @@ EXTRA_DIST =				\
   ovirt-flash-static		\
   ovirt-node-image.ks		\
   ovirt-pxe					\
-  create-ovirt-iso-nodes
+  create-ovirt-iso-nodes	\
+  edit-livecd				\
+  livecd-setauth
 
 DISTCLEANFILES = $(PACKAGE)-$(VERSION).tar.gz
 
diff --git a/edit-livecd b/edit-livecd
new file mode 100755
index 0000000..d9e995f
--- /dev/null
+++ b/edit-livecd
@@ -0,0 +1,128 @@
+#!/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
+
+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; }
+
+usage() {
+    case $# in 1) warn "$1"; try_h; exit 1;; esac
+    cat <<EOF
+Usage: $ME livecd.iso program
+  livecd.iso - LiveCD ISO to edit
+  program - Arbitrary program/script that is run inside of the livecd root
+            filesystem.  This script is not run in a chroot environment so
+            it can access the host filesystem.  The program should be executable
+
+  Example Script:
+    #!/bin/sh
+    touch etc/sysconfig/foo
+
+  This will create a file /etc/sysconfig/foo in the livecd filesystem.
+EOF
+}
+
+# exit after any error:
+set -e
+
+test $# -lt 2 && { usage; exit 1; }
+
+# first, check to see we are root
+if [ $( id -u ) -ne 0 ]; then
+    die "Must run as root"
+fi
+
+CD=$1
+PROG=$2
+
+which mkisofs mksquashfs sed > /dev/null 2>&1
+
+WDIR=`mktemp -d $PWD/livecd.XXXXXXXXXX`
+ISO="${CD##*/}"
+ISO="${ISO%.iso}-custom.iso"
+
+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"
+}
+
+addExit "rm -Rf $WDIR"
+
+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 $PROG $WDIR/ex
+pushd $WDIR/ex
+set +e
+./$PROG
+set -e
+popd
+rm -f $WDIR/ex/$PROG
+
+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/livecd-setauth b/livecd-setauth
new file mode 100755
index 0000000..eb8922f
--- /dev/null
+++ b/livecd-setauth
@@ -0,0 +1,50 @@
+#!/bin/bash
+#
+# Script to interactively add root password and authorized_keys file
+# to a livecd
+# 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.
+
+DEFAULT_AUTH=~/.ssh/authorized_keys
+
+printf "Do you want to set a root password? [y/N]: "
+read yesno
+if [ "$yesno" = "y" -o "$yesno" = "Y" ]; then
+    chroot . passwd root
+fi
+
+printf "Do you want to set an authorized_keys file? [y/N]: "
+read yesno
+if [ "$yesno" = "y" -o "$yesno" = "Y" ]; then
+    echo "Enter the location of the authorized_keys file [default: $DEFAULT_AUTH]: "
+    read -e authkeys
+    if [ -z "$authkeys" ]; then
+        authkeys=$DEFAULT_AUTH
+    fi
+
+    authkeys=$(eval echo $authkeys)
+    if [ -f $authkeys ]; then
+        SSH=root/.ssh
+        AUTH=$SSH/authorized_keys
+
+        mkdir -p $SSH
+        chmod 755 $SSH
+        cp -v $authkeys $AUTH
+        chmod 644 $AUTH
+    else
+        echo "$authkeys not found, skipping"
+    fi
+fi
diff --git a/ovirt-node-image.spec.in b/ovirt-node-image.spec.in
index 138f4a4..440ba2f 100644
--- a/ovirt-node-image.spec.in
+++ b/ovirt-node-image.spec.in
@@ -68,6 +68,8 @@ mkdir %{buildroot}
 %{__install} -p -m0755 ovirt-flash %{buildroot}%{_sbindir}
 %{__install} -p -m0755 ovirt-flash-static %{buildroot}%{_sbindir}
 %{__install} -p -m0755 create-ovirt-iso-nodes %{buildroot}%{_sbindir}
+%{__install} -p -m0755 edit-livecd %{buildroot}%{_sbindir}
+%{__install} -p -m0755 livecd-setauth %{buildroot}%{_sbindir}
 
 %clean
 %{__rm} -rf %{buildroot}
@@ -83,6 +85,8 @@ cobbler sync > /dev/null 2>&1 || :
 %{_sbindir}/ovirt-flash
 %{_sbindir}/ovirt-flash-static
 %{_sbindir}/create-ovirt-iso-nodes
+%{_sbindir}/edit-livecd
+%{_sbindir}/livecd-setauth
 
 %files pxe
 %defattr(-,root,root,0644)
-- 
1.6.0.3




More information about the ovirt-devel mailing list