39bc38f243693222da13fee5061e978ff85354ec
[kuberef.git] / create_vm.sh
1 #!/bin/bash
2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c)
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 # TODO This will be merged in main functions.sh
12
13 sudo mkdir -p /var/lib/libvirt/images/$1
14 sudo qemu-img create -f qcow2 \
15     -o backing_file=/var/lib/libvirt/images/ubuntu-18.04.qcow2 \
16     /var/lib/libvirt/images/$1/"$1".qcow2 10G
17
18 # Create cloud-init configuration files
19 cat <<EOL > user-data
20 #cloud-config
21 users:
22   - name: ubuntu
23     ssh-authorized-keys:
24       - $(cat $HOME/.ssh/id_rsa.pub)
25     sudo: ['ALL=(ALL) NOPASSWD:ALL']
26     groups: sudo
27     shell: /bin/bash
28 EOL
29 cat <<EOL > meta-data
30 local-hostname: $VM_NAME
31 EOL
32
33 sudo genisoimage  -output /var/lib/libvirt/images/$1/"$1"-cidata.iso \
34     -volid cidata -joliet -rock user-data meta-data
35
36 sudo virt-install --connect qemu:///system --name $VM_NAME \
37     --ram 4096 --vcpus=4 --os-type linux --os-variant ubuntu16.04 \
38     --disk path=/var/lib/libvirt/images/$1/"$1".qcow2,format=qcow2 \
39     --disk /var/lib/libvirt/images/$1/"$1"-cidata.iso,device=cdrom \
40     --import --network network=default --network bridge=$BRIDGE,model=rtl8139 --noautoconsole