-
-# Create jumphost VM
-create_jump() {
-# Create VM image
- sudo mkdir -p "/var/lib/libvirt/images/$VM_NAME"
- sudo qemu-img create -f qcow2 \
- -o backing_file=/var/lib/libvirt/images/ubuntu-18.04.qcow2 \
- "/var/lib/libvirt/images/$VM_NAME/$VM_NAME.qcow2" 10G
-
-# Create VM cloud-init configuration files
- cat <<EOL > user-data
- #cloud-config
- users:
- - name: $USERNAME
- ssh-authorized-keys:
- - $(cat "$HOME/.ssh/id_rsa.pub")
- sudo: ['ALL=(ALL) NOPASSWD:ALL']
- groups: sudo
- shell: /bin/bash
-EOL
- cat <<EOL > meta-data
- local-hostname: $VM_NAME
-EOL
-
-# Create VM
- sudo genisoimage -output "/var/lib/libvirt/images/$VM_NAME/$VM_NAME-cidata.iso" \
- -volid cidata -joliet -rock user-data meta-data
- sudo virt-customize -a "/var/lib/libvirt/images/$VM_NAME/$VM_NAME.qcow2" \
- --root-password password:"$ROOT_PASSWORD"
- sudo virt-install --connect qemu:///system --name "$VM_NAME" \
- --ram 4096 --vcpus=4 --os-type linux --os-variant ubuntu16.04 \
- --disk path="/var/lib/libvirt/images/$VM_NAME/$VM_NAME.qcow2",format=qcow2 \
- --disk "/var/lib/libvirt/images/$VM_NAME/$VM_NAME-cidata.iso",device=cdrom \
- --import --network network=default --network bridge="$BRIDGE",model=rtl8139 --noautoconsole
- jumpbox_ip=$(get_vm_ip)
- i=0
- while [ -z "$jumpbox_ip" ]; do
- sleep $((++i))
- jumpbox_ip=$(get_vm_ip)
- done
- i=0
- until nc -w5 -z "$jumpbox_ip" 22; do
- sleep $((++i))
- done
+ #-------------------------------------------------------------------------------
+ # Check if user belongs to libvirt's group
+ #-------------------------------------------------------------------------------
+ libvirt_group="libvirt"
+ # shellcheck disable=SC1091
+ source /etc/os-release || source /usr/lib/os-release
+ if [ "${ID,,}" == "ubuntu" ] && [ "$VERSION_ID" == "16.04" ]; then
+ libvirt_group+="d"
+ fi
+ if ! groups | grep " $libvirt_group "; then
+ echo "ERROR : $(id -nu) user doesn't belong to $libvirt_group group."
+ exit 1
+ fi