Skip to content

Instantly share code, notes, and snippets.

@lewangdev
Created September 8, 2020 10:35
Show Gist options
  • Save lewangdev/9fc8461d3a038da4813a415858a918d1 to your computer and use it in GitHub Desktop.
Save lewangdev/9fc8461d3a038da4813a415858a918d1 to your computer and use it in GitHub Desktop.
Create KVM virtual machine
qemu-img create -f qcow2 /var/lib/libvirt/images/base-image-centos7.qcow2 40G
virt-install \
--virt-type kvm \
--name base-image-centos7 \
--vcpus 2 \
--ram 4096 \
--cdrom=/root/Downloads/CentOS-7-x86_64-Minimal-2003.iso \
--disk /var/lib/libvirt/images/base-image-centos7.qcow2,format=qcow2 \
--network bridge=br0 \
--autostart \
--noautoconsole
#!/bin/bash -
showhelp() {
echo "Usage: ./vmcreate.sh VM_IP"
}
[ -z "$1" ] && showhelp && exit 0
VM_IP=$1
VM_NAME=${VM_IP//\./-}
echo "Creating VM: ${VM_NAME} IP: ${VM_IP}"
virt-clone --original base-image-centos7 --name ${VM_NAME} --file /var/lib/libvirt/images/${VM_NAME}.qcow2
virt-sysprep -d ${VM_NAME} --hostname ${VM_NAME}
virt-edit -d ${VM_NAME} /etc/sysconfig/network-scripts/ifcfg-eth0 -e "s#IPADDR=".*"#IPADDR="${VM_IP}"#"
virsh start ${VM_NAME}
virsh autostart ${VM_NAME}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment