Skip to content

Instantly share code, notes, and snippets.

@craigtracey
Last active December 31, 2015 07:09
Show Gist options
  • Save craigtracey/7951775 to your computer and use it in GitHub Desktop.
Save craigtracey/7951775 to your computer and use it in GitHub Desktop.
Janky image builder
#!/bin/bash
set -e
NAME=$1
KICKSTART=$2
RPM_REPO=$3
BRIDGE="br100"
BRIDGE="virbr0"
function usage {
print "imagebuilder.sh <name of image> <kickstart file> [<rpm repo>]"
}
if [ -z $NAME ]; then
echo "You must provide a name for this image"
exit -1
fi
if [ -z $KICKSTART ] || [ ! -f $KICKSTART ]; then
echo "You must provide a valid kickstart for this image"
exit -1
fi
RPM_REPO_DEFAULT='http://centos.netnitco.net/6/os/x86_64/'
if [ -z $RPM_REPO ]; then
echo "WARNING: Using $RPM_REPO_DEFAULT as you did not specify an RPM repo"
RPM_REPO=$RPM_REPO_DEFAULT
fi
if [ $UID != 0 ]; then
echo "You must run this as root"
exit -1
fi
VMSTATUS=`virsh list | tail -n+3 | grep $NAME | awk '{print $3}'`
if [ ! -e $VMSTATUS ]; then
if [ "$VMSTATUS" == "running" ]; then
echo "There is already a virtual machine running with name: $NAME"
read -p "Would you like to destroy and undefine this virtual machine (y/n)? "
REPLY=`echo $REPLY | awk '{print tolower($0)}'`
if [ "$REPLY" == "n" ]; then
echo "Exiting"
exit -1
fi
virsh destroy $NAME
virsh undefine $NAME
fi
fi
TEMPDIR=`mktemp -d`
ln -s `realpath $KICKSTART` $TEMPDIR/ks.cfg
virt-install --accelerate --name=$NAME \
--noreboot \
--ram=1024 \
--file=$NAME.img \
--file-size=10 \
--machine=pc \
--location $RPM_REPO \
--graphics vnc,listen=0.0.0.0 \
--noautoconsole \
--initrd-inject=$TEMPDIR/ks.cfg --extra-args "ks=file:/ks.cfg stage2=$RPM_REPO/images/install.img text" \
-w bridge:$BRIDGE -w bridge:$BRIDGE
rm -rf $TEMPDIR
#virsh destroy $NAME
#virsh undefine $NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment