#!/bin/sh
#==============================================================================+
# File name : centoskvm.sh
# Begin : 2013-04-18
# Last Update : 2013-04-25
# Version : 1.0.0
#
# Description : Shell script used to generate a CentOS Virtual Machine image.
#
# Website : https://github.com/fubralimited/CentOS-KVM-Image-Tools
#
# Author: Nicola Asuni
#
# (c) Copyright:
# Fubra Limited
# Manor Coach House
# Church Hill
# Aldershot
# Hampshire
# GU12 4RQ
# UK
# http://www.fubra.com
# support@fubra.com
#
# License:
# Copyright (C) 2012-2013 Fubra Limited
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see .
#
# See LICENSE.TXT file for more information.
#==============================================================================+
# CONFIGURATION
LIBVIRT_IMAGE_BASE=/data/libvirt
# USAGE EXAMPLE:
# sh centoskvm.sh centos-gold-master
# ensure script is being run as root
if [ `whoami` != root ]; then
echo "ERROR: This script must be run as root" 1>&2
exit 1
fi
# check for image name and kickstart file
if [ -z "$1" -o -z "$2" -o -z "$3" ]; then
echo "ERROR: No argument supplied. Please provide the following parameters:"
echo " [ x86_64 | i386 ] [ 5 | 6 | 7 ] [] []"
echo " being architecture and 5 6 7 being the major Centos version"
echo " provide your own optional kickstart file"
echo " currently the only option is 'noshrink':"
echo " noshrink: don't skrink the image to minimal size after building"
exit 1
fi
# name of the image
IMGNAME=$1
# architecture
ARCH=$2
# version of Centos
VER=$3
if test "X$4" != "X"; then
KICKSTART=$4
else
# compose the name of the kickstart file
KICKSTART=centos$3-$ARCH-master.cfg
fi
# some sanity checks per architecture and major release
# and set some limits
case ${ARCH}_${VER} in
i386_5|x86_64_5|i386_6|x86_64_6)
VM_MEMORY=512
;;
x86_64_7)
VM_MEMORY=1024
;;
i386_7|*)
echo "Unsupported architecture '$ARCH' and major release '$VER'" 1>&2
exit 1
esac
# VM image file extension
EXT="qcow2"
echo "Generating VM ..."
# create image file
virt-install \
--name $IMGNAME \
--ram $VM_MEMORY \
--cpu host \
--vcpus 1 \
--nographics \
--os-type=linux \
--os-variant=rhel$VER \
--location=http://mirror.switch.ch/ftp/mirror/centos/$VER/os/$ARCH \
--initrd-inject=kickstarts/$KICKSTART \
--extra-args="ks=file:/$KICKSTART text console=tty0 utf8 console=ttyS0,115200" \
--disk path=$LIBVIRT_IMAGE_BASE/$IMGNAME.$EXT,size=10,bus=virtio,format=qcow2 \
--force \
--noreboot
# change directory
cd $LIBVIRT_IMAGE_BASE
if test "X$5" != "Xnoshrink"; then
# reset, unconfigure a virtual machine so clones can be made
virt-sysprep --no-selinux-relabel -a $IMGNAME.$EXT
# SELinux: relabelling all filesystem
#guestfish --selinux -i $IMGNAME.$EXT <