|
|
|
@ -142,8 +142,6 @@ green() { echo -e "\e[32m$@\e[0m" ; } |
|
|
|
|
|
|
|
|
|
|
|
die() { red "ERR: $@" >&2 ; exit 2 ; } |
|
|
|
die() { red "ERR: $@" >&2 ; exit 2 ; } |
|
|
|
silent() { "$@" > /dev/null 2>&1 ; } |
|
|
|
silent() { "$@" > /dev/null 2>&1 ; } |
|
|
|
has_bin() { silent which $1 ; } |
|
|
|
|
|
|
|
title() { bold "$@" ; } |
|
|
|
|
|
|
|
output() { echo -e "- $@" ; } |
|
|
|
output() { echo -e "- $@" ; } |
|
|
|
outputn() { echo -en "- $@ ... " ; } |
|
|
|
outputn() { echo -en "- $@ ... " ; } |
|
|
|
ok() { green "${@:-OK}" ; } |
|
|
|
ok() { green "${@:-OK}" ; } |
|
|
|
@ -162,35 +160,37 @@ function set_wget () |
|
|
|
fi |
|
|
|
fi |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function delete_vm () |
|
|
|
function check_vmname_set () |
|
|
|
{ |
|
|
|
{ |
|
|
|
VM=$1 |
|
|
|
test -n ${VMNAME} || die "VMNAME not set." |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
# Set defaults to get IMAGEDIR |
|
|
|
function delete_vm () |
|
|
|
set_defaults |
|
|
|
{ |
|
|
|
|
|
|
|
check_vmname_set |
|
|
|
|
|
|
|
|
|
|
|
if [ "${DOMAIN_EXISTS}" -eq 1 ] |
|
|
|
if [ "${DOMAIN_EXISTS}" -eq 1 ] |
|
|
|
then |
|
|
|
then |
|
|
|
outputn "Destroying ${VM} domain" |
|
|
|
outputn "Destroying ${VMNAME} domain" |
|
|
|
virsh destroy ${VM} > /dev/null 2>&1; ok |
|
|
|
virsh destroy ${VMNAME} > /dev/null 2>&1; ok |
|
|
|
|
|
|
|
|
|
|
|
outputn "Undefining ${VM} domain" |
|
|
|
outputn "Undefining ${VMNAME} domain" |
|
|
|
virsh undefine ${VM} > /dev/null 2>&1 && ok |
|
|
|
virsh undefine ${VMNAME} > /dev/null 2>&1 && ok |
|
|
|
|
|
|
|
|
|
|
|
[ -d "$IMAGEDIR/$VM" ] \ |
|
|
|
[ -d "${IMAGEDIR}/${VMNAME}" ] \ |
|
|
|
&& outputn "Deleting ${VM} files" \ |
|
|
|
&& outputn "Deleting ${VMNAME} files" \ |
|
|
|
&& rm -rf $IMAGEDIR/$VM \ |
|
|
|
&& rm -rf ${IMAGEDIR}/${VMNAME} \ |
|
|
|
&& ok |
|
|
|
&& ok |
|
|
|
else |
|
|
|
else |
|
|
|
output "Domain ${VM} does not exist" |
|
|
|
output "Domain ${VMNAME} does not exist" |
|
|
|
fi |
|
|
|
fi |
|
|
|
|
|
|
|
|
|
|
|
if [ "${STORPOOL_EXISTS}" -eq 1 ] |
|
|
|
if [ "${STORPOOL_EXISTS}" -eq 1 ] |
|
|
|
then |
|
|
|
then |
|
|
|
outputn "Destroying ${VM} storage pool" |
|
|
|
outputn "Destroying ${VMNAME} storage pool" |
|
|
|
virsh pool-destroy ${VM} > /dev/null 2>&1 && ok |
|
|
|
virsh pool-destroy ${VMNAME} > /dev/null 2>&1 && ok |
|
|
|
else |
|
|
|
else |
|
|
|
output "Storage pool ${VM} does not exist" |
|
|
|
output "Storage pool ${VMNAME} does not exist" |
|
|
|
fi |
|
|
|
fi |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -311,8 +311,7 @@ function get_pkg_mgr () |
|
|
|
SUDOGROUP="sudo" |
|
|
|
SUDOGROUP="sudo" |
|
|
|
;; |
|
|
|
;; |
|
|
|
*) |
|
|
|
*) |
|
|
|
echo "OS not supported." |
|
|
|
die "OS not supported." |
|
|
|
exit 2 |
|
|
|
|
|
|
|
;; |
|
|
|
;; |
|
|
|
esac |
|
|
|
esac |
|
|
|
} |
|
|
|
} |
|
|
|
@ -337,6 +336,8 @@ function check_delete_known_host () |
|
|
|
|
|
|
|
|
|
|
|
function create_vm () |
|
|
|
function create_vm () |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
check_vmname_set |
|
|
|
|
|
|
|
|
|
|
|
# Start clean |
|
|
|
# Start clean |
|
|
|
[ -d "${IMAGEDIR}/${VMNAME}" ] && rm -rf ${IMAGEDIR}/${VMNAME} |
|
|
|
[ -d "${IMAGEDIR}/${VMNAME}" ] && rm -rf ${IMAGEDIR}/${VMNAME} |
|
|
|
mkdir -p ${IMAGEDIR}/${VMNAME} |
|
|
|
mkdir -p ${IMAGEDIR}/${VMNAME} |
|
|
|
@ -405,14 +406,17 @@ _EOF_ |
|
|
|
genisoimage -output $CI_ISO -volid cidata -joliet -r $USER_DATA $META_DATA &>> ${VMNAME}.log \ |
|
|
|
genisoimage -output $CI_ISO -volid cidata -joliet -r $USER_DATA $META_DATA &>> ${VMNAME}.log \ |
|
|
|
&& ok |
|
|
|
&& ok |
|
|
|
|
|
|
|
|
|
|
|
# #TODO: only output if verbose |
|
|
|
if [ "${VERBOSE}" -eq 1 ] |
|
|
|
# echo "Creating Storage Pool with the following command:" |
|
|
|
then |
|
|
|
# echo " virsh pool-create-as \\" |
|
|
|
output "Creating storage pool with the following command" |
|
|
|
# echo " --name ${VMNAME} \\" |
|
|
|
printf " virsh pool-create-as \\ \n" |
|
|
|
# echo " --type dir \\" |
|
|
|
printf " --name ${VMNAME} \\ \n" |
|
|
|
# echo " --target ${IMAGEDIR}/${VMNAME}" |
|
|
|
printf " --type dir \\ \n" |
|
|
|
|
|
|
|
printf " --target ${IMAGEDIR}/${VMNAME} \n" |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
outputn "Creating storage pool" |
|
|
|
|
|
|
|
fi |
|
|
|
|
|
|
|
|
|
|
|
outputn "Creating storage pool" |
|
|
|
|
|
|
|
# Create new storage pool for new VM |
|
|
|
# Create new storage pool for new VM |
|
|
|
(virsh pool-create-as \ |
|
|
|
(virsh pool-create-as \ |
|
|
|
--name ${VMNAME} \ |
|
|
|
--name ${VMNAME} \ |
|
|
|
@ -428,22 +432,25 @@ _EOF_ |
|
|
|
NETWORK_PARAMS="bridge=${BRIDGE},model=virtio,mac=${MACADDRESS}" |
|
|
|
NETWORK_PARAMS="bridge=${BRIDGE},model=virtio,mac=${MACADDRESS}" |
|
|
|
fi |
|
|
|
fi |
|
|
|
|
|
|
|
|
|
|
|
outputn "Installing the domain and adjusting the configuration" |
|
|
|
if [ "${VERBOSE}" -eq 1 ] |
|
|
|
# #TODO: only output if verbose |
|
|
|
then |
|
|
|
# echo "[$(date +%r)]----> Installing with the following command:" |
|
|
|
output "Installing the domain with the following command" |
|
|
|
# echo " virt-install \\" |
|
|
|
printf " virt-install \\ \n" |
|
|
|
# echo " --import \\" |
|
|
|
printf " --import \\ \n" |
|
|
|
# echo " --name ${VMNAME} \\" |
|
|
|
printf " --name ${VMNAME} \\ \n" |
|
|
|
# echo " --memory ${MEMORY} \\" |
|
|
|
printf " --memory ${MEMORY} \\ \n" |
|
|
|
# echo " --vcpus ${CPUS} \\" |
|
|
|
printf " --vcpus ${CPUS} \\ \n" |
|
|
|
# echo " --cpu ${FEATURE} \\" |
|
|
|
printf " --cpu ${FEATURE} \\ \n" |
|
|
|
# echo " --disk ${DISK},format=qcow2,bus=virtio \\" |
|
|
|
printf " --disk ${DISK},format=qcow2,bus=virtio \\ \n" |
|
|
|
# echo " --disk ${CI_ISO},device=cdrom \\" |
|
|
|
printf " --disk ${CI_ISO},device=cdrom \\ \n" |
|
|
|
# echo " --network ${NETWORK_PARAMS} \\" |
|
|
|
printf " --network ${NETWORK_PARAMS} \\ \n" |
|
|
|
# echo " --os-type=linux \\" |
|
|
|
printf " --os-type=linux \\ \n" |
|
|
|
# echo " --os-variant=${OS_VARIANT} \\" |
|
|
|
printf " --os-variant=${OS_VARIANT} \\ \n" |
|
|
|
# echo " --graphics spice \\" |
|
|
|
printf " --graphics spice \\ \n" |
|
|
|
# echo " --noautoconsole " |
|
|
|
printf " --noautoconsole \n" |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
outputn "Installing the domain" |
|
|
|
|
|
|
|
fi |
|
|
|
|
|
|
|
|
|
|
|
# Call virt-install to import the cloud image and create a new VM |
|
|
|
# Call virt-install to import the cloud image and create a new VM |
|
|
|
(virt-install --import \ |
|
|
|
(virt-install --import \ |
|
|
|
@ -460,18 +467,13 @@ _EOF_ |
|
|
|
--noautoconsole &>> ${VMNAME}.log && ok ) \ |
|
|
|
--noautoconsole &>> ${VMNAME}.log && ok ) \ |
|
|
|
|| die "Could not create domain with virt-install." |
|
|
|
|| die "Could not create domain with virt-install." |
|
|
|
|
|
|
|
|
|
|
|
# RET=$? |
|
|
|
|
|
|
|
# if [ "$RET" -ne 0 ]; then |
|
|
|
|
|
|
|
# echo "[ERROR] virt-install failed." |
|
|
|
|
|
|
|
# exit 3 |
|
|
|
|
|
|
|
# fi |
|
|
|
|
|
|
|
virsh dominfo ${VMNAME} &>> ${VMNAME}.log |
|
|
|
virsh dominfo ${VMNAME} &>> ${VMNAME}.log |
|
|
|
|
|
|
|
|
|
|
|
# Eject cdrom |
|
|
|
# Eject cdrom |
|
|
|
virsh change-media ${VMNAME} hda --eject --config &>> ${VMNAME}.log |
|
|
|
virsh change-media ${VMNAME} hda --eject --config &>> ${VMNAME}.log |
|
|
|
|
|
|
|
|
|
|
|
# Remove the unnecessary cloud init files |
|
|
|
# Remove the unnecessary cloud init files |
|
|
|
outputn "Cleaning up cloud-init" |
|
|
|
outputn "Cleaning up cloud-init files" |
|
|
|
rm $USER_DATA $META_DATA $CI_ISO && ok |
|
|
|
rm $USER_DATA $META_DATA $CI_ISO && ok |
|
|
|
|
|
|
|
|
|
|
|
outputn "Waiting for domain to get an IP address" |
|
|
|
outputn "Waiting for domain to get an IP address" |
|
|
|
@ -491,6 +493,8 @@ _EOF_ |
|
|
|
|
|
|
|
|
|
|
|
printf "\n" |
|
|
|
printf "\n" |
|
|
|
check_delete_known_host |
|
|
|
check_delete_known_host |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
printf "\n" |
|
|
|
output "SSH to ${VMNAME}: 'ssh ${LOGIN_USER}@${IP}' or 'ssh ${LOGIN_USER}@${VMNAME}'" |
|
|
|
output "SSH to ${VMNAME}: 'ssh ${LOGIN_USER}@${IP}' or 'ssh ${LOGIN_USER}@${VMNAME}'" |
|
|
|
output "DONE" |
|
|
|
output "DONE" |
|
|
|
|
|
|
|
|
|
|
|
@ -500,20 +504,33 @@ _EOF_ |
|
|
|
# Delete VM |
|
|
|
# Delete VM |
|
|
|
function remove () |
|
|
|
function remove () |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
# Parse command line arguments |
|
|
|
|
|
|
|
while getopts ":hv" opt |
|
|
|
|
|
|
|
do |
|
|
|
|
|
|
|
case "$opt" in |
|
|
|
|
|
|
|
v ) VERBOSE=1 ;; |
|
|
|
|
|
|
|
h|* ) usage; exit 1 ;; |
|
|
|
|
|
|
|
esac |
|
|
|
|
|
|
|
done |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
shift $((OPTIND - 1)) |
|
|
|
|
|
|
|
|
|
|
|
if [ "$#" != 1 ] |
|
|
|
if [ "$#" != 1 ] |
|
|
|
then |
|
|
|
then |
|
|
|
printf "Too many arguments.\n" |
|
|
|
printf "Please specify a single host to remove.\n" |
|
|
|
printf "Run '$prog help remove' for usage.\n" |
|
|
|
printf "Run 'kvm-install-vm help remove' for usage.\n" |
|
|
|
exit 1 |
|
|
|
exit 1 |
|
|
|
else |
|
|
|
else |
|
|
|
# Check if domain exists and set DOMAIN_EXISTS variable. |
|
|
|
VMNAME=$1 |
|
|
|
domain_exists "${1}" |
|
|
|
fi |
|
|
|
|
|
|
|
|
|
|
|
# Check if storage pool exists and set STORPOOL_EXISTS variable. |
|
|
|
# Check if domain exists and set DOMAIN_EXISTS variable. |
|
|
|
storpool_exists "${1}" |
|
|
|
domain_exists "${VMNAME}" |
|
|
|
|
|
|
|
|
|
|
|
delete_vm "${1}" |
|
|
|
# Check if storage pool exists and set STORPOOL_EXISTS variable. |
|
|
|
fi |
|
|
|
storpool_exists "${VMNAME}" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
delete_vm "${VMNAME}" |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function set_defaults () |
|
|
|
function set_defaults () |
|
|
|
@ -531,6 +548,7 @@ function set_defaults () |
|
|
|
DISTRO=centos7 # Distribution |
|
|
|
DISTRO=centos7 # Distribution |
|
|
|
MACADDRESS= # MAC Address |
|
|
|
MACADDRESS= # MAC Address |
|
|
|
TIMEZONE=US/Eastern # Timezone |
|
|
|
TIMEZONE=US/Eastern # Timezone |
|
|
|
|
|
|
|
VERBOSE=0 # Verbosity |
|
|
|
|
|
|
|
|
|
|
|
# Reset OPTIND |
|
|
|
# Reset OPTIND |
|
|
|
OPTIND=1 |
|
|
|
OPTIND=1 |
|
|
|
@ -538,11 +556,8 @@ function set_defaults () |
|
|
|
|
|
|
|
|
|
|
|
function create () |
|
|
|
function create () |
|
|
|
{ |
|
|
|
{ |
|
|
|
# Set default variables |
|
|
|
|
|
|
|
set_defaults |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Parse command line arguments |
|
|
|
# Parse command line arguments |
|
|
|
while getopts ":b:c:d:D:f:i:k:l:m:M:t:T:h" opt |
|
|
|
while getopts ":b:c:d:D:f:i:k:l:m:M:t:T:hv" opt |
|
|
|
do |
|
|
|
do |
|
|
|
case "$opt" in |
|
|
|
case "$opt" in |
|
|
|
b ) BRIDGE="${OPTARG}" ;; |
|
|
|
b ) BRIDGE="${OPTARG}" ;; |
|
|
|
@ -564,6 +579,7 @@ function create () |
|
|
|
M ) MACADDRESS="${OPTARG}" ;; |
|
|
|
M ) MACADDRESS="${OPTARG}" ;; |
|
|
|
t ) DISTRO="${OPTARG}" ;; |
|
|
|
t ) DISTRO="${OPTARG}" ;; |
|
|
|
T ) TIMEZONE="${OPTARG}" ;; |
|
|
|
T ) TIMEZONE="${OPTARG}" ;; |
|
|
|
|
|
|
|
v ) VERBOSE=1 ;; |
|
|
|
h|* ) usage; exit 1 ;; |
|
|
|
h|* ) usage; exit 1 ;; |
|
|
|
esac |
|
|
|
esac |
|
|
|
done |
|
|
|
done |
|
|
|
@ -578,7 +594,6 @@ function create () |
|
|
|
else |
|
|
|
else |
|
|
|
VMNAME=$1 |
|
|
|
VMNAME=$1 |
|
|
|
fi |
|
|
|
fi |
|
|
|
|
|
|
|
|
|
|
|
# Set cloud-init variables after VMNAME is assigned |
|
|
|
# Set cloud-init variables after VMNAME is assigned |
|
|
|
USER_DATA=user-data |
|
|
|
USER_DATA=user-data |
|
|
|
META_DATA=meta-data |
|
|
|
META_DATA=meta-data |
|
|
|
@ -589,7 +604,7 @@ function create () |
|
|
|
|
|
|
|
|
|
|
|
if [ ! -z "${IMAGE+x}" ] |
|
|
|
if [ ! -z "${IMAGE+x}" ] |
|
|
|
then |
|
|
|
then |
|
|
|
echo "[$(date +%r)]----> Using custom QCOW2 image: ${IMAGE}." |
|
|
|
output "Using custom QCOW2 image: ${IMAGE}." |
|
|
|
OS_VARIANT="auto" |
|
|
|
OS_VARIANT="auto" |
|
|
|
LOGIN_USER="<use the default account in your custom image>" |
|
|
|
LOGIN_USER="<use the default account in your custom image>" |
|
|
|
else |
|
|
|
else |
|
|
|
@ -656,22 +671,20 @@ function attach-disk () |
|
|
|
|
|
|
|
|
|
|
|
if [ ! -f "${DISKDIR}/${DISKNAME}" ] |
|
|
|
if [ ! -f "${DISKDIR}/${DISKNAME}" ] |
|
|
|
then |
|
|
|
then |
|
|
|
echo "[$(date +%r)]----> Creating new '${TARGET}' disk image for domain ${VMNAME}." |
|
|
|
outputn "Creating new '${TARGET}' disk image for domain ${VMNAME}" |
|
|
|
qemu-img create -f ${FORMAT} -o size=$DISKSIZE,preallocation=metadata \ |
|
|
|
(qemu-img create -f ${FORMAT} -o size=$DISKSIZE,preallocation=metadata \ |
|
|
|
${DISKDIR}/${DISKNAME} >> ${DISKDIR}/${VMNAME}.log && \ |
|
|
|
${DISKDIR}/${DISKNAME} &>> ${DISKDIR}/${VMNAME}.log && ok ) && \ |
|
|
|
|
|
|
|
|
|
|
|
echo "[$(date +%r)]----> Attaching ${DISKNAME} to domain ${VMNAME}." && \ |
|
|
|
outputn "Attaching ${DISKNAME} to domain ${VMNAME}" |
|
|
|
virsh attach-disk ${VMNAME} \ |
|
|
|
(virsh attach-disk ${VMNAME} \ |
|
|
|
--source $DISKDIR/${DISKNAME} \ |
|
|
|
--source $DISKDIR/${DISKNAME} \ |
|
|
|
--target ${TARGET} \ |
|
|
|
--target ${TARGET} \ |
|
|
|
--subdriver ${FORMAT} \ |
|
|
|
--subdriver ${FORMAT} \ |
|
|
|
--cache none \ |
|
|
|
--cache none \ |
|
|
|
--persistent >> ${DISKDIR}/${VMNAME}.log && \ |
|
|
|
--persistent &>> ${DISKDIR}/${VMNAME}.log && ok ) \ |
|
|
|
|
|
|
|
|| die "Could not attach disk." |
|
|
|
exit "$?" |
|
|
|
|
|
|
|
else |
|
|
|
else |
|
|
|
printf "Target ${TARGET} is already created or in use.\n" |
|
|
|
die "Target ${TARGET} is already created or in use." |
|
|
|
exit 1 |
|
|
|
|
|
|
|
fi |
|
|
|
fi |
|
|
|
|
|
|
|
|
|
|
|
fi |
|
|
|
fi |
|
|
|
@ -714,6 +727,7 @@ case "${subcommand}" in |
|
|
|
usage_subcommand "${subcommand}" |
|
|
|
usage_subcommand "${subcommand}" |
|
|
|
exit 1 |
|
|
|
exit 1 |
|
|
|
else |
|
|
|
else |
|
|
|
|
|
|
|
set_defaults |
|
|
|
"${subcommand}" "$@" |
|
|
|
"${subcommand}" "$@" |
|
|
|
exit $? |
|
|
|
exit $? |
|
|
|
fi |
|
|
|
fi |
|
|
|
|