Rework cfg01 setup script for libvirt

Rework define-vm.sh script for booting cfg01 VM with libvirt.

Also updated README.

Related-Prod: PROD-27573 (PROD:27573)
Related-Prod: PROD-27656 (PROD:27656)
Related-Prod: PROD-27990 (PROD:27990)
Change-Id: Iacbfea2ec4f3a55e83bfe9c290bf3ff60b18b02f
diff --git a/predefine-vm/README.rst b/predefine-vm/README.rst
index a894f16..fae8f34 100644
--- a/predefine-vm/README.rst
+++ b/predefine-vm/README.rst
@@ -1,3 +1,48 @@
+====================
+Deploy cfg01 locally
+====================
+
+Deploy cfg01 on Ubuntu with QEMU/KVM (libvirt)
+==============================================
+
+**Prerequisites**
+
+Script will check and install next required packages: qemu-utils libvirt-bin virtinst qemu-kvm.
+
+**Common info**
+
+Script gives you an ability to deploy cfg01 VM with provided cfg01 Qcwo2 disk
+image and config-drive iso file on your local laptop.
+
+Script is operating by next ENV variables:
+
+    * VM_NAME - the name of VM to be created in VirtualBox. Default: 'cfg01-mcp.local'.
+    * VM_SOURCE_DISK - the name of virtual disk to be used for virtual machine. Can be relative or absolute path.
+    * VM_CONFIG_DISK - same as VM_SOURCE_DISK, but for config-drive ISO file.
+    * VM_MGM_BRIDGE_NAME - Bridge name to use for deploy management network. Should have Internet access if not
+      offline case. Optional, default: 'br-mgm'
+    * VM_CTL_BRIDGE_NAME - Bridge name to use for control network. Optional, default: 'br-ctl'
+    * VM_MGM_BRIDGE_DISABLE - Do not use host bridge for deploy management network and create new nat-network.
+      Optional, default: false
+    * VM_CTL_BRIDGE_DISABLE - Do not use host bridge for control network and create host-only based new network.
+      Optional, default: false
+    * VM_MGM_NETWORK_NAME - Name for deploy management network. Optional, default: 'mgm_network'
+    * VM_CTL_NETWORK_NAME - Name for control network. Optional, default: 'ctl_network'
+    * VM_MGM_NETWORK_GATEWAY - NAT-Service network gateway for deploy management network.
+      Optional, default: '192.168.15.1'
+    * VM_MGM_NETWORK_MASK - Network mask for deploy management network. Optional, default: '255.255.255.0'
+    * VM_CTL_NETWORK_GATEWAY - Host-only based network gateway for control network.
+      Optional, default: '192.168.56.1'
+    * VM_CTL_NETWORK_MASK - Network mask for control network. Optional, default: '255.255.255.0'
+
+Script will check that disk and config-drive are present and then define needed networks and spawn virtual machine.
+Then check that VM is up and running.
+
+Once VM is up and running you can use ``virsh console`` to check what is going on during deploy.
+It is recommended to specify username and password during model generation for login via VM console if
+something goes wrong. Once you are logged in you can follow usual debug procedure for cfg01 node.
+
+
 Deploy cfg01 on Mac OS with VirtualBox
 ======================================
 
@@ -63,6 +108,6 @@
     * Run virtual machine.
 
 Once VM is up and running you can use VirtualBox VM console to check what is going on during deploy.
-It will drop all logs into console and it doesn't matter loged in user or not. It is good to specify during
-model generation username and password to be able to login via VM console if something goes wrong.
+It will drop all logs into console and it doesn't matter loged in user or not. It is recommended to specify
+username and password during model generation for login via VM console if something goes wrong.
 Once you are logged in you can follow usual debug procedure for cfg01 node.
\ No newline at end of file
diff --git a/predefine-vm/define-vm.sh b/predefine-vm/define-vm.sh
index 5f68169..86bd566 100755
--- a/predefine-vm/define-vm.sh
+++ b/predefine-vm/define-vm.sh
@@ -1,9 +1,18 @@
 #!/bin/bash -xe
 
+VM_MGM_BRIDGE_DISABLE=${VM_MGM_BRIDGE_DISABLE:-false}
+VM_CTL_BRIDGE_DISABLE=${VM_CTL_BRIDGE_DISABLE:-false}
 VM_MGM_BRIDGE_NAME=${VM_MGM_BRIDGE_NAME:-"br-mgm"}
 VM_CTL_BRIDGE_NAME=${VM_CTL_BRIDGE_NAME:-"br-ctl"}
+VM_MGM_NETWORK_NAME=${VM_MGM_NETWORK_NAME:-"mgm_network"}
+VM_CTL_NETWORK_NAME=${VM_CTL_NETWORK_NAME:-"ctl_network"}
 VM_MEM_KB=${VM_MEM_KB:-"12589056"}
 VM_CPUS=${VM_CPUS:-"4"}
+# optional params if you won't use bridge on host
+VM_MGM_NETWORK_GATEWAY=${VM_MGM_NETWORK_GATEWAY:-"192.168.56.1"}
+VM_MGM_NETWORK_MASK=${VM_MGM_NETWORK_MASK:-"255.255.255.0"}
+VM_CTL_NETWORK_GATEWAY=${VM_CTL_NETWORK_GATEWAY:-"192.168.57.1"}
+VM_CTL_NETWORK_MASK=${VM_CTL_NETWORK_MASK:-"255.255.255.0"}
 
 if [[ -z ${VM_NAME} ]]; then
   echo "ERROR: \$VM_NAME not set!"
@@ -18,6 +27,59 @@
   exit 1
 fi
 
+function check_packages {
+    PACKAGES="qemu-utils libvirt-bin qemu-kvm"
+    for i in $PACKAGES; do
+       dpkg -s $i &> /dev/null || { echo "Package $i is not installed!"; exit 1; }
+    done
+}
+
+function create_network {
+    local network=${1}
+    virsh net-destroy ${network} 2> /dev/null || true
+    virsh net-undefine ${network} 2> /dev/null || true
+    virsh net-define ${network}.xml
+    virsh net-autostart ${network}
+    virsh net-start ${network}
+}
+
+function create_bridge_network {
+    local network=$1
+    local bridge_name=$2
+    cat <<EOF > $(pwd)/${network}.xml
+<network>
+  <name>${network}</name>
+  <forward mode="bridge"/>
+  <bridge name="${bridge_name}" />
+</network>
+EOF
+    create_network ${network}
+}
+
+function create_host_network {
+    local network=$1
+    local gateway=$2
+    local netmask=$3
+    local nat=${4:-false}
+    cat <<EOF > $(pwd)/${network}.xml
+<network>
+  <name>${network}</name>
+  <bridge name="${network}" />
+  <ip address="${gateway}" netmask="${netmask}"/>
+EOF
+    if [[ ${nat} ]]; then
+        cat <<EOF>> $(pwd)/${network}.xml
+  <forward mode="nat"/>
+EOF
+    fi
+    cat <<EOF>> $(pwd)/${network}.xml
+</network>
+EOF
+    create_network ${network}
+}
+
+check_packages
+
 # Template definition
 cat <<EOF > $(pwd)/${VM_NAME}-vm.xml
 <domain type='kvm'>
@@ -60,21 +122,47 @@
       <readonly/>
       <address type='drive' controller='0' bus='0' target='0' unit='0'/>
     </disk>
+EOF
+if [[ ! ${VM_MGM_BRIDGE_DISABLE} ]]; then
+    create_bridge_network "${VM_MGM_NETWORK_NAME}" "${VM_MGM_BRIDGE_NAME}"
+    cat <<EOF >> $(pwd)/${VM_NAME}-vm.xml
     <interface type='bridge'>
       <source bridge='$VM_MGM_BRIDGE_NAME'/>
       <model type='virtio'/>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
     </interface>
 EOF
+else
+    create_host_network "${VM_MGM_NETWORK_NAME}" "${VM_MGM_NETWORK_GATEWAY}" "${VM_MGM_NETWORK_MASK}" true
+    cat <<EOF >> $(pwd)/${VM_NAME}-vm.xml
+    <interface type='network'>
+      <source network='$VM_MGM_NETWORK_NAME'/>
+      <model type='virtio'/>
+      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
+    </interface>
+EOF
+fi
+
 if [[ ! ${VM_CTL_BRIDGE_DISABLE} ]]; then
-cat <<EOF >> $(pwd)/${VM_NAME}-vm.xml
+    create_bridge_network "${VM_CTL_NETWORK_NAME}" "${VM_CTL_BRIDGE_NAME}"
+    cat <<EOF >> $(pwd)/${VM_NAME}-vm.xml
     <interface type='bridge'>
       <source bridge='$VM_CTL_BRIDGE_NAME'/>
       <model type='virtio'/>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
     </interface>
 EOF
+else
+    create_host_network "${VM_CTL_NETWORK_NAME}" "${VM_CTL_NETWORK_GATEWAY}" "${VM_CTL_NETWORK_MASK}"
+    cat <<EOF >> $(pwd)/${VM_NAME}-vm.xml
+    <interface type='network'>
+      <source network='$VM_CTL_NETWORK_NAME'/>
+      <model type='virtio'/>
+      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
+    </interface>
+EOF
 fi
+
 cat <<EOF >> $(pwd)/${VM_NAME}-vm.xml
     <serial type='pty'>
       <source path='/dev/pts/1'/>