leon / controller.sh
Created at Wed Jun 17 10:16:17 WIB 2020
Create Controller
controller.sh
Raw
#eth0
VM_NAME=alora-controller
ADDRESS1=10.30.30.60
GATEWAY1=10.30.30.1
NETWORK_NAME1=net-10.30.30

#eth1
ADDRESS2=10.40.40.60
GATEWAY2=10.40.40.1
NETWORK_NAME2=net-10.40.40

create_vm() {
        virt-clone -o template-centos7.7 -n $VM_NAME -f /data/vms/$VM_NAME.qcow2
        virsh start $VM_NAME

        sleep 60

        ssh root@192.168.100.253 /bin/bash << EOF
        
        #For eth0
        sed -i "s/^IPADDR.*/IPADDR=$ADDRESS1/g" /etc/sysconfig/network-scripts/ifcfg-eth0
        sed -i "s/^GATEWAY.*/GATEWAY=$GATEWAY1/g" /etc/sysconfig/network-scripts/ifcfg-eth0
       
        #for eth1
        cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth1
        sed -i "s/^IPADDR.*/IPADDR=$ADDRESS2/g" /etc/sysconfig/network-scripts/ifcfg-eth1
        sed -i "s/^GATEWAY.*/GATEWAY=$GATEWAY2/g" /etc/sysconfig/network-scripts/ifcfg-eth1
        echo "$VM_NAME" > /etc/hostname
        echo "$ADDRESS1 $VM_NAME" >> /etc/hosts
        echo "$ADDRESS2 $VM_NAME" >> /etc/hosts
EOF

        virsh shutdown $VM_NAME
        sleep 30

        virsh detach-interface $VM_NAME --type network --config
        virsh attach-interface --domain $VM_NAME --type network --source $NETWORK_NAME1 --model virtio --config
        virsh attach-interface --domain $VM_NAME --type network --source $NETWORK_NAME2 --model virtio --config

        virsh start $VM_NAME
}

create_vm