summaryrefslogtreecommitdiff
path: root/other/scripts/centoskvm.sh
blob: 965550db9a6486e0747b2b62456ae3b2ee7a7824 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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
#==============================================================================+