Bumps OVS version to 2.8 for OVN
[apex.git] / lib / virtual-setup-functions.sh
1 #!/usr/bin/env bash
2 ##############################################################################
3 # Copyright (c) 2015 Tim Rozet (Red Hat), Dan Radez (Red Hat) and others.
4 #
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 ##Create virtual nodes in virsh
12 ##params: vcpus, ramsize
13 function setup_virtual_baremetal {
14   local vcpus ramsize held_ramsize
15   if [ -z "$1" ]; then
16     vcpus=4
17     ramsize=8192
18   elif [ -z "$2" ]; then
19     vcpus=$1
20     ramsize=8192
21   else
22     vcpus=$1
23     ramsize=$(($2*1024))
24   fi
25   #start by generating the opening yaml for the inventory-virt.yaml file
26   cat > $APEX_TMP_DIR/inventory-virt.yaml << EOF
27 nodes:
28 EOF
29
30   # next create the virtual machines and add their definitions to the file
31   if [ "$ha_enabled" == "False" ]; then
32       controller_index=0
33   else
34       controller_index=2
35       # 3 controller + computes
36       # zero based so add 2 to compute count
37       if [ $VM_COMPUTES -lt 2 ]; then
38           VM_COMPUTES=2
39       fi
40   fi
41
42   # tmp var to hold ramsize in case modified during detection
43   held_ramsize=${ramsize}
44   for i in $(seq 0 $(($controller_index+$VM_COMPUTES))); do
45     ramsize=${held_ramsize}
46     if [ $i -gt $controller_index ]; then
47       capability="profile:compute"
48       if [ -n "$VM_COMPUTE_RAM" ]; then
49         ramsize=$((${VM_COMPUTE_RAM}*1024))
50       fi
51     else
52       capability="profile:control"
53       if [[ "${deploy_options_array['sdn_controller']}" == 'opendaylight' && "$ramsize" -lt 12288 ]]; then
54          echo "WARN: RAM per controller too low.  OpenDaylight specified in deployment requires at least 12GB"
55          echo "INFO: Increasing RAM per controller to 12GB"
56          ramsize=12288
57       elif [[ "$ramsize" -lt 10240 ]]; then
58          echo "WARN: RAM per controller too low.  Deployment requires at least 10GB"
59          echo "INFO: Increasing RAM per controller to 10GB"
60          ramsize=10240
61       fi
62     fi
63     if ! virsh list --all | grep baremetal${i} > /dev/null; then
64       define_vm baremetal${i} network 41 'admin' $vcpus $ramsize
65       for n in tenant external storage api; do
66         if [[ $enabled_network_list =~ $n ]]; then
67           echo -n "$n "
68           virsh attach-interface --domain baremetal${i} --type network --source $n --model virtio --config
69         fi
70       done
71     else
72       echo "Found baremetal${i} VM, using existing VM"
73     fi
74     #virsh vol-list default | grep baremetal${i} 2>&1> /dev/null || virsh vol-create-as default baremetal${i}.qcow2 41G --format qcow2
75     mac=$(virsh domiflist baremetal${i} | grep admin | awk '{ print $5 }')
76
77     cat >> $APEX_TMP_DIR/inventory-virt.yaml << EOF
78   node${i}:
79     mac_address: "$mac"
80     ipmi_ip: 192.168.122.1
81     ipmi_user: admin
82     ipmi_pass: "password"
83     pm_type: "pxe_ipmitool"
84     pm_port: "623$i"
85     cpu: $vcpus
86     memory: $ramsize
87     disk: 41
88     arch: "$(uname -i)"
89     capabilities: "$capability"
90 EOF
91     vbmc add baremetal$i --port 623$i
92     if service firewalld status > /dev/null; then
93         firewall-cmd --permanent --zone=public --add-port=623$i/udp
94     fi
95     # TODO: add iptables check and commands too
96     vbmc start baremetal$i
97   done
98   if service firewalld status > /dev/null; then
99     firewall-cmd --reload
100   fi
101 }
102
103 ##Create virtual nodes in virsh
104 ##params: name - String: libvirt name for VM
105 ##        bootdev - String: boot device for the VM
106 ##        disksize - Number: size of the disk in GB
107 ##        ovs_bridges: - List: list of ovs bridges
108 ##        vcpus - Number of VCPUs to use (defaults to 4)
109 ##        ramsize - Size of RAM for VM in MB (defaults to 8192)
110 function define_vm () {
111   local vcpus ramsize volume_path direct_boot kernel_args
112
113   if [ -z "$5" ]; then
114     vcpus=4
115     ramsize=8388608
116   elif [ -z "$6" ]; then
117     vcpus=$5
118     ramsize=8388608
119   else
120     vcpus=$5
121     ramsize=$(($6*1024))
122   fi
123
124   # Create the libvirt storage volume
125   if virsh vol-list default | grep ${1}.qcow2 2>&1> /dev/null; then
126     volume_path=$(virsh vol-path --pool default ${1}.qcow2 || echo "/var/lib/libvirt/images/${1}.qcow2")
127     echo "Volume ${1} exists. Deleting Existing Volume $volume_path"
128     virsh vol-dumpxml ${1}.qcow2 --pool default > /dev/null || echo '' #ok for this to fail
129     touch $volume_path
130     virsh vol-delete ${1}.qcow2 --pool default
131   fi
132   virsh vol-create-as default ${1}.qcow2 ${3}G --format qcow2
133   volume_path=$(virsh vol-path --pool default ${1}.qcow2)
134   if [ ! -f $volume_path ]; then
135       echo "$volume_path Not created successfully... Aborting"
136       exit 1
137   fi
138
139   # undercloud need to be direct booted.
140   # the upstream image no longer includes the kernel and initrd
141   if [ "$1" == 'undercloud' ]; then
142       direct_boot='--direct-boot overcloud-full'
143       kernel_args='--kernel-arg console=ttyS0 --kernel-arg root=/dev/sda'
144   fi
145
146   if [ "$(uname -i)" == 'aarch64' ]; then
147       diskbus='scsi'
148   else
149       diskbus='sata'
150   fi
151
152   # create the VM
153   $LIB/configure-vm --name $1 \
154                     --bootdev $2 \
155                     --image "$volume_path" \
156                     --diskbus $diskbus \
157                     --arch $(uname -i) \
158                     --cpus $vcpus \
159                     --memory $ramsize \
160                     --libvirt-nic-driver virtio \
161                     $direct_boot \
162                     $kernel_args \
163                     --baremetal-interface $4
164 }