diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2016-09-16 20:59:06 +0200 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2016-09-16 20:59:06 +0200 |
commit | cde82ec07f72c988accdd18619f94c9db5fb177a (patch) | |
tree | 5f5905bb79444fe0905193e47c32236a488d2a7e /other/scripts/centoskvm.sh | |
parent | 3df750c466fceca80ac7f16694d59c6a8a13ba47 (diff) | |
download | centosauto-cde82ec07f72c988accdd18619f94c9db5fb177a.tar.gz centosauto-cde82ec07f72c988accdd18619f94c9db5fb177a.tar.bz2 |
Diffstat (limited to 'other/scripts/centoskvm.sh')
-rwxr-xr-x | other/scripts/centoskvm.sh | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/other/scripts/centoskvm.sh b/other/scripts/centoskvm.sh new file mode 100755 index 0000000..965550d --- /dev/null +++ b/other/scripts/centoskvm.sh @@ -0,0 +1,158 @@ +#!/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 <http://www.gnu.org/licenses/>. +# +# 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 " <image name> [ x86_64 | i386 ] [ 5 | 6 | 7 ] [<kickstart>] [<options>]" + echo " being architecture and 5 6 7 being the major Centos version" + echo " <kickstart> provide your own optional kickstart file" + echo " <options> 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 <<EOF + #sh load_policy + #sh 'restorecon -R -v /' + #EOF + # fails with: + #*stdin*:1: libguestfs: error: sh: SELinux: Could not downgrade policy file /etc/selinux/targeted/policy/policy.24, searching for an older version. + #SELinux: Could not open policy file <= /etc/selinux/targeted/policy/policy.24: No such file or directory + # really needed? + + # make a virtual machine disk sparse + TMPDIR=/data/tmp virt-sparsify --compress --convert qcow2 --format qcow2 $IMGNAME.$EXT $IMGNAME-sparsified.$EXT + + # remove original image + rm -rf $IMGNAME.$EXT + + # rename sparsified + mv $IMGNAME-sparsified.$EXT $IMGNAME.$EXT + +fi + +# set correct ownership for the VM image file +#chown qemu:qemu $IMGNAME.$EXT + +echo "Process Completed. Use the 'virt start $IMGNAME' command to start the newly created VM." + +#==============================================================================+ +# END OF FILE +#==============================================================================+ |