|
|
|
|
@ -8,6 +8,7 @@ function usage {
|
|
|
|
|
echo "" |
|
|
|
|
echo "OPTIONS" |
|
|
|
|
echo " -c Number of vCPUs (default: 1)" |
|
|
|
|
echo " -f CPU Model / Feature (default: host)" |
|
|
|
|
echo " -m Memory Size (MB) (default: 1024)" |
|
|
|
|
echo " -d Disk Size (GB) (default: 10)" |
|
|
|
|
echo " -t Linux Distribution (default: centos7)" |
|
|
|
|
@ -18,10 +19,12 @@ function usage {
|
|
|
|
|
echo " -i Custom QCOW2 Image" |
|
|
|
|
echo " -n vmname Name of VM to create" |
|
|
|
|
echo " -r vmname Name of VM to delete" |
|
|
|
|
echo " -M mac Mac address (default: None)" |
|
|
|
|
echo "" |
|
|
|
|
echo "DISTRIBUTIONS" |
|
|
|
|
echo " - centos7" |
|
|
|
|
echo " - centos6" |
|
|
|
|
echo " - ubuntu1604" |
|
|
|
|
echo "" |
|
|
|
|
echo "EXAMPLES" |
|
|
|
|
echo "" |
|
|
|
|
@ -44,6 +47,7 @@ OPTIND=1
|
|
|
|
|
#-------------------------------------------------- |
|
|
|
|
|
|
|
|
|
CPUS=1 # Number of virtual CPUs |
|
|
|
|
FEATURE=host # Use host cpu features to the guest |
|
|
|
|
MEMORY=1024 # Amount of RAM in MB |
|
|
|
|
DISK_SIZE=10 # Disk Size in GB |
|
|
|
|
RESIZE_DISK=false # Resize disk (boolean) |
|
|
|
|
@ -51,14 +55,16 @@ IMAGEDIR=${HOME}/virt/images # Directory to store images
|
|
|
|
|
BRIDGE=virbr0 # Hypervisor bridge |
|
|
|
|
PUBKEY=${HOME}/.ssh/id_rsa.pub # SSH public key |
|
|
|
|
DISTRO=centos7 # Distribution |
|
|
|
|
MACADDRESS= # MAC Address |
|
|
|
|
|
|
|
|
|
#-------------------------------------------------- |
|
|
|
|
# Parse command line arguments |
|
|
|
|
#-------------------------------------------------- |
|
|
|
|
|
|
|
|
|
while getopts ":c:m:d:t:l:k:b:n:r:i:h" opt; do |
|
|
|
|
while getopts ":c:f:m:d:t:l:k:b:n:r:i:M:h" opt; do |
|
|
|
|
case "$opt" in |
|
|
|
|
c ) CPUS="${OPTARG}" ;; |
|
|
|
|
f ) FEATURE="${OPTARG}" ;; |
|
|
|
|
m ) MEMORY="${OPTARG}" ;; |
|
|
|
|
t ) DISTRO="${OPTARG}" ;; |
|
|
|
|
l ) IMAGEDIR="${OPTARG}" ;; |
|
|
|
|
@ -67,6 +73,7 @@ while getopts ":c:m:d:t:l:k:b:n:r:i:h" opt; do
|
|
|
|
|
b ) BRIDGE="${OPTARG}" ;; |
|
|
|
|
i ) IMAGE="${OPTARG}" ;; |
|
|
|
|
r ) VMDEL="${OPTARG}" ;; |
|
|
|
|
M ) MACADDRESS="${OPTARG}" ;; |
|
|
|
|
d ) |
|
|
|
|
if [ "${OPTARG}" -gt ${DISK_SIZE} ]; then |
|
|
|
|
DISK_SIZE="${OPTARG}G" |
|
|
|
|
@ -119,10 +126,11 @@ fi
|
|
|
|
|
|
|
|
|
|
function fetch_images { |
|
|
|
|
case "$DISTRO" in |
|
|
|
|
centos7 ) QCOW=CentOS-7-x86_64-GenericCloud.qcow2 ;; |
|
|
|
|
centos6 ) QCOW=CentOS-6-x86_64-GenericCloud.qcow2 ;; |
|
|
|
|
debian8 ) QCOW=debian-8-20170521-openstack-amd64.qcow2 ;; |
|
|
|
|
* ) usage; exit 2 ;; |
|
|
|
|
centos7 ) QCOW=CentOS-7-x86_64-GenericCloud.qcow2 ;; |
|
|
|
|
centos6 ) QCOW=CentOS-6-x86_64-GenericCloud.qcow2 ;; |
|
|
|
|
debian8 ) QCOW=debian-8-openstack-amd64.qcow2 ;; |
|
|
|
|
ubuntu1604 ) QCOW=ubuntu-16.04-server-cloudimg-amd64-disk1.img ;; |
|
|
|
|
* ) usage; exit 2 ;; |
|
|
|
|
esac |
|
|
|
|
|
|
|
|
|
mkdir -p ${IMAGEDIR} |
|
|
|
|
@ -157,6 +165,16 @@ function fetch_images {
|
|
|
|
|
echo "$(date -R) Cloud image not found. Downloading..." |
|
|
|
|
wget -P ${IMAGEDIR} ${DEBIAN8_URL}/${QCOW} |
|
|
|
|
fi |
|
|
|
|
elif [ "${DISTRO}" == "ubuntu1604" ] |
|
|
|
|
then |
|
|
|
|
OS_VARIANT="ubuntu16.04" |
|
|
|
|
UBUNTU16_URL=https://cloud-images.ubuntu.com/releases/16.04/release/ |
|
|
|
|
LOGIN_USER=ubuntu |
|
|
|
|
if [ ! -f ${IMAGEDIR}/${QCOW} ] |
|
|
|
|
then |
|
|
|
|
echo "$(date -R) Cloud image not found. Downloading..." |
|
|
|
|
wget -P ${IMAGEDIR} ${UBUNTU16_URL}/${QCOW} |
|
|
|
|
fi |
|
|
|
|
fi |
|
|
|
|
|
|
|
|
|
IMAGE=${IMAGEDIR}/${QCOW} |
|
|
|
|
@ -213,7 +231,7 @@ pushd ${IMAGEDIR}/${VMNAME} > /dev/null
|
|
|
|
|
touch ${VMNAME}.log |
|
|
|
|
|
|
|
|
|
# cloud-init config: set hostname, remove cloud-init package, |
|
|
|
|
# and add ssh-key |
|
|
|
|
# and add ssh-key |
|
|
|
|
cat > $USER_DATA << _EOF_ |
|
|
|
|
#cloud-config |
|
|
|
|
|
|
|
|
|
@ -223,21 +241,31 @@ hostname: ${VMNAME}
|
|
|
|
|
fqdn: ${VMNAME}.example.local |
|
|
|
|
|
|
|
|
|
# Configure where output will go |
|
|
|
|
output: |
|
|
|
|
output: |
|
|
|
|
all: ">> /var/log/cloud-init.log" |
|
|
|
|
|
|
|
|
|
# configure interaction with ssh server |
|
|
|
|
ssh_genkeytypes: ['ed25519', 'rsa'] |
|
|
|
|
|
|
|
|
|
# Install my public ssh key to the first user-defined user configured |
|
|
|
|
# Install my public ssh key to the first user-defined user configured |
|
|
|
|
# in cloud.cfg in the template (which is centos for CentOS cloud images) |
|
|
|
|
ssh_authorized_keys: |
|
|
|
|
- ${KEY} |
|
|
|
|
|
|
|
|
|
_EOF_ |
|
|
|
|
if [ "${DISTRO}" == "centos6" -o "${DISTRO}" == "centos7" ] |
|
|
|
|
then |
|
|
|
|
cat >> $USER_DATA << _EOF_ |
|
|
|
|
# Remove cloud-init when finished with it |
|
|
|
|
runcmd: |
|
|
|
|
- [ yum, -y, remove, cloud-init ] |
|
|
|
|
_EOF_ |
|
|
|
|
else |
|
|
|
|
cat >> $USER_DATA << _EOF_ |
|
|
|
|
# Remove cloud-init when finished with it |
|
|
|
|
runcmd: |
|
|
|
|
- [ apt-get, -y, remove, cloud-init ] |
|
|
|
|
_EOF_ |
|
|
|
|
fi |
|
|
|
|
|
|
|
|
|
echo "instance-id: ${VMNAME}; local-hostname: ${VMNAME}" > $META_DATA |
|
|
|
|
|
|
|
|
|
@ -263,25 +291,48 @@ _EOF_
|
|
|
|
|
echo " --name ${VMNAME} \\" |
|
|
|
|
echo " --ram ${MEMORY} \\" |
|
|
|
|
echo " --vcpus ${CPUS} \\" |
|
|
|
|
echo " --cpu ${FEATURE} \\" |
|
|
|
|
echo " --disk $DISK,format=qcow2,bus=virtio \\" |
|
|
|
|
echo " --disk $CI_ISO,device=cdrom \\" |
|
|
|
|
echo " --network bridge=${BRIDGE},model=virtio \\" |
|
|
|
|
if [ -z "${MACADDRESS}" ] |
|
|
|
|
then |
|
|
|
|
echo " --network bridge=${BRIDGE},model=virtio \\" |
|
|
|
|
else |
|
|
|
|
echo " --network bridge=${BRIDGE},model=virtio,mac=${MACADDRESS} \\" |
|
|
|
|
fi |
|
|
|
|
echo " --os-type=linux \\" |
|
|
|
|
echo " --os-variant=${OS_VARIANT} \\" |
|
|
|
|
echo " --graphics none \\" |
|
|
|
|
echo " --noautoconsole " |
|
|
|
|
|
|
|
|
|
virt-install --import \ |
|
|
|
|
--name ${VMNAME} \ |
|
|
|
|
--ram ${MEMORY} \ |
|
|
|
|
--vcpus ${CPUS} \ |
|
|
|
|
--disk $DISK,format=qcow2,bus=virtio \ |
|
|
|
|
--disk $CI_ISO,device=cdrom \ |
|
|
|
|
--network bridge=${BRIDGE},model=virtio \ |
|
|
|
|
--os-type=linux \ |
|
|
|
|
--os-variant=${OS_VARIANT} \ |
|
|
|
|
--graphics none \ |
|
|
|
|
--noautoconsole |
|
|
|
|
if [ -z "${MACADDRESS}" ] |
|
|
|
|
then |
|
|
|
|
virt-install --import \ |
|
|
|
|
--name ${VMNAME} \ |
|
|
|
|
--ram ${MEMORY} \ |
|
|
|
|
--vcpus ${CPUS} \ |
|
|
|
|
--cpu ${FEATURE} \ |
|
|
|
|
--disk $DISK,format=qcow2,bus=virtio \ |
|
|
|
|
--disk $CI_ISO,device=cdrom \ |
|
|
|
|
--network bridge=${BRIDGE},model=virtio \ |
|
|
|
|
--os-type=linux \ |
|
|
|
|
--os-variant=${OS_VARIANT} \ |
|
|
|
|
--graphics none \ |
|
|
|
|
--noautoconsole |
|
|
|
|
else |
|
|
|
|
virt-install --import \ |
|
|
|
|
--name ${VMNAME} \ |
|
|
|
|
--ram ${MEMORY} \ |
|
|
|
|
--vcpus ${CPUS} \ |
|
|
|
|
--cpu ${FEATURE} \ |
|
|
|
|
--disk $DISK,format=qcow2,bus=virtio \ |
|
|
|
|
--disk $CI_ISO,device=cdrom \ |
|
|
|
|
--network bridge=${BRIDGE},model=virtio,mac=${MACADDRESS} \ |
|
|
|
|
--os-type=linux \ |
|
|
|
|
--os-variant=${OS_VARIANT} \ |
|
|
|
|
--graphics none \ |
|
|
|
|
--noautoconsole |
|
|
|
|
fi |
|
|
|
|
|
|
|
|
|
RET=$? |
|
|
|
|
if [ "$RET" -ne 0 ]; then |
|
|
|
|
|