-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-vm
executable file
·34 lines (30 loc) · 1.25 KB
/
create-vm
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
#!/usr/bin/env bash
# Location: /usr/local/bin/create-vm
# Scope: each libvirt host
# Purpose: create vm guest, return ip address
# Notes: latest vanilla cloud image can be found here,
# https://cloud-images.ubuntu.com/releases/16.04/release/ubuntu-16.04-server-cloudimg-amd64-disk1.img
# you can also prepare a custom qcow image by mounting with qemu-nbd and using chroot
VM_TYPE=${1:-node}
VM_NAME="${VM_TYPE}-$(head -c64 /dev/urandom | tr -dc a-z0-9 | head -c 5)"
CPU_SIZE="4"
RAM_SIZE="8192"
BASE_IMAGE=/data/images/ubuntu-16.04-server-cloudimg-amd64-BASE-9aa192c.qcow2
qemu-img create -f qcow2 -b "${BASE_IMAGE}" /data/images/${VM_NAME}.qcow2 > /dev/null 2>&1
virt-install \
--name "${VM_NAME}" \
--vcpus "${CPU_SIZE}" --ram "${RAM_SIZE}" \
--os-type=linux --os-variant ubuntu17.10 \
--network network=default,model=virtio \
--disk path=/data/images/${VM_NAME}.qcow2,format=qcow2,bus=virtio \
--channel unix,target_type=virtio,name=org.qemu.guest_agent.0 \
--nographics --noautoconsole --import > /dev/null 2>&1
# find ip address of guest
while true ; do
IP=`virsh domifaddr ${VM_NAME} | grep ipv4 | awk '{print $4}' | awk -F'/' '{print $1}'`
if [[ "${IP}" == *"168"* ]] ; then
echo "${VM_NAME} ${IP}"
break
fi
sleep 1
done