-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslcreatevm.sh
More file actions
165 lines (154 loc) · 5.07 KB
/
Copy pathslcreatevm.sh
File metadata and controls
165 lines (154 loc) · 5.07 KB
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
159
160
161
162
163
164
165
#!/bin/bash
##
## /usr/local/script/slcreatevm.sh
## Create a kernel-based VM
##
## Created on 14 OKT 2013
## Version 1.0 dated 20 OKT 2013
##
# Set variables and default values
set -u
F_TEST=FALSE # Script test flag
F_OVERWRITE=FALSE # Overwrite previosu VM flag
VMINSTALLSCRIPT="" # KVM installer
ARGUMENTFILE="" # File with arguments for KVM installer
ISOFILE="" # ISO-file
CMD_CREATEVM="" # Command to execute and create VM
CMD_ARGVALSEP=" " # Seprator between arguments and value on command line
EXITCODE=0 # Assume everything went ok
argument[0]="" # Array with arguments
value[0]="" # Array with argument values
# Define functions
function checktocontinue() {
# Ask to continue
read -p "Continue (y/n)? " -n 1 -r
if [[ "$REPLY" =~ ^[Yy]$ ]]; then
printf "\n"
else
printf "\nYou choose not to continue. Exiting.\n"
exit 1
fi
}
# Check if this script is run as root, otherwise exit.
if [[ $(whoami) != root ]]; then
printf "Must be root to execute this script. Exiting.\n"
exit 1
fi
# Evaluate given options using getops; set variables accordingly
while getopts "i:oth" opt; do
case "$opt" in
i)
ISOFILE=${OPTARG}
#printf -- "ISO-file specified: %s\n" "${ISOFILE}"
;;
o)
F_OVERWRITE=TRUE
#printf -- "Previous VM will be overwritten."
;;
t)
F_TEST=TRUE
printf -- "Test mode: script will only dry-run.\n"
;;
\? | h)
printf -- "Script for creating KVM guests to be run by the system libvirtd instance.\n"
printf -- "Usage: %s [-h] [-t] [-o] [-i isofile] \n" "${0##*/}"
printf -- " -h Help: show this help message and exit.\n"
printf -- " -t Test mode: script will only dry-run.\n"
printf -- " -i ISO-file: install KVM-guest from 'isofile' using virt-install.\n"
printf -- " If no ISO-file is specified, vmbuilder will be used to install a JeOS KVM-guest.\n"
printf -- " -o Overwrite VM if already existing (vmbuilder only; virt-install should prompt).\n"
printf -- "Required files in the current installation directory: \n"
printf -- " '<vmbuilder|virt-install>.arguments': file with arguments passed to the install command.\n"
# printf -- " 'firstboot.sh': optional script to be run on first boot of the virtual machine.\n"
# printf -- " 'firstlogin.sh': optional script to be used on first user login.\n"
exit 2
;;
:)
printf -- "Option -%s requires an argument. Exiting.\n" "${OPTARG}"
exit 1
;;
esac
done
# Check install method and set-up initial command
if [[ -z "${ISOFILE}" ]]
then
VMINSTALLSCRIPT="vmbuilder"
CMD_ARGVALSEP=" "
CMD_CREATEVM="${VMINSTALLSCRIPT} kvm ubuntu -v --libvirt qemu:///system "
if [[ ${F_OVERWRITE} == TRUE ]]
then
CMD_CREATEVM="${CMD_CREATEVM} -o "
fi
printf "No ISO-file specified. Using '%s' to install a JeOS KVM-guest.\n" ${VMINSTALLSCRIPT}
else
VMINSTALLSCRIPT="virt-install"
CMD_ARGVALSEP="="
CMD_CREATEVM="${VMINSTALLSCRIPT} --connect=qemu:///system --cdrom=${ISOFILE} --check-cpu "
printf "Using '%s' to install the OS from the specified ISO-file '%s'.\n" ${VMINSTALLSCRIPT} ${ISOFILE}
fi
if [[ ! -x $(which "${VMINSTALLSCRIPT}") ]]
then
printf "\nCannot execute %s. Have the required packages correctly been installed? Exiting.\n" ${VMINSTALLSCRIPT}
exit 1
fi
# Check current directory
printf "Executing %s in %s. " "${VMINSTALLSCRIPT}" "$(pwd)"
checktocontinue
# Read arguments from file in current VM directory
ARGUMENTFILE="${VMINSTALLSCRIPT}.arguments"
if [[ -r "${ARGUMENTFILE}" ]]
then
# Compile argument list from the first field of each record.
i=0
while read line; do
if [[ "${line}" =~ ^[^#]*= ]]; then
argument[i]="--"${line%% =*}
value[i]=${line#*= }
((i++))
fi
done < ${ARGUMENTFILE}
# Compile final command
printf "The following %s arguments were specified in %s:\n" "${VMINSTALLSCRIPT}" "${ARGUMENTFILE}"
j=0
while [[ ${j} -lt ${i} ]]; do
printf " %s %s \n" ${argument[$j]} ${value[$j]}
CMD_CREATEVM="${CMD_CREATEVM} ${argument[$j]}${CMD_ARGVALSEP}${value[$j]} "
((j++))
done
printf "Please check if these arguments are correct. "
checktocontinue
else
printf "Cannot read arguments from '%s'. Exiting.\n" "${ARGUMENTFILE}"
exit 1
fi
printf "The following command will be executed: \n'%s'\n" "${CMD_CREATEVM}"
# Test or real?
if [[ ${F_TEST} == TRUE ]]
then
# Test run only
case "${VMINSTALLSCRIPT}" in
vmbuilder)
printf "Test mode specified, but vmbuilder cannot dry-run.\n"
;;
virt-install)
CMD_CREATEVM="${CMD_CREATEVM} --dry-run"
printf "Test mode specified, so virt-install will dry-run only. "
checktocontinue
${CMD_CREATEVM}
EXITCODE=$?
;;
*)
printf "Error in script. Unknown install command %s.\nThis should not occur. Exiting." "${VMINSTALLSCRIPT}"
exit -1
;;
esac
else
# The real thing
printf "YOU ARE ABOUT TO CREATE A NEW KVM-GUEST. "
checktocontinue
${CMD_CREATEVM}
EXITCODE=$?
fi
# End of script; exit with exit-code of the real install script
printf "All done.\n"
exit ${EXITCODE}