wiki:KVM

Version 12 (modified by sky, 2 years ago) (diff)

--

KVM (Kernel-based Virtual Machine)

## VM Remove
virsh undefine test101
virsh undefine test102 --managed-save
virsh undefine test103 --remove-all-storage
rm -f /opt/kvm/test10[1-3].qcow2

## VM Clone
virt-clone --original template --name test101 --file /opt/kvm/test101.qcow2
virt-clone --original template --name test102 --file /opt/kvm/test102.qcow2
virt-clone --original template --name test103 --file /opt/kvm/test103.qcow2

################################################################################

## VM Console & Initialize (CentOS 6)
virsh start test101 && virsh console test101


NNAME=test101
NIP=10.59.72.101

sed -i "/^HOSTNAME/ s/template/$NNAME/" /etc/sysconfig/network
sed -i "/^IPADDR/ s/10.59.72.100/$NIP/" /etc/sysconfig/network-scripts/ifcfg-eth0
cat /dev/null >/etc/udev/rules.d/70-persistent-net.rules
init 6

################################################################################

## VM Console & Initialize (CentOS 7)
virsh start test104 && virsh console test104


NNAME=test104
NIP=10.59.72.104

hostnamectl set-hostname "$NNAME"
sed -i "/^IPADDR/ s/10.59.72.110/$NIP/" /etc/sysconfig/network-scripts/ifcfg-eth0
init 6

################################################################################

Debian 11 Bullseye

## default xml file /etc/libvirt/qemu
## default disk image /var/lib/libvirt/images
## Create a VM
virt-install \
--virt-type kvm \
--name debian \
--vcpus 4 \
--memory 8192 \
--disk size=64 \
--os-variant debiantesting \
--cdrom debian-11.5.0-amd64-DVD-1.iso \
--network bridge=vbr0 \
--graphics vnc,listen=0.0.0.0

## Clone a VM
virt-clone --original debian --name newdebian --auto-clone

Launch Windows (with virtio)

## For RHEL6
virt-install \
--name windows-7 \
--vcpus 2 --ram 4096 \
--os-type windows --os-variant win7 \
--cdrom /opt/kvm/windows_7_x64.iso \
--disk path=/opt/kvm/virtio-win-0.1.118.iso,device=cdrom \
--disk path=/opt/kvm/windows-7.qcow2,format=qcow2,size=20,device=disk,bus=virtio \
--network bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0

## For RHEL7
virt-install \
--name win2012r2 \
--vcpus 2 --ram 4096 \
--os-type windows --os-variant win2k8 \
--disk path=/opt/kvm/Win2012_R2_x64_CN.iso,device=cdrom \
--disk path=/opt/kvm/virtio-win-0.1.118.iso,device=cdrom \
--disk path=/opt/kvm/win2012r2.qcow2,format=qcow2,size=20,device=disk,bus=virtio \
--network bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0

Attach Disk

## RAW (vdb)
qemu-img create -f raw /opt/kvm/test101-vdb.raw 200G
virsh attach-disk --domain test101 --source /opt/kvm/test101-vdb.raw --target vdb --persistent

## QCOW2 (vdc)
qemu-img create -f qcow2 /opt/kvm/test101-vdc.qcow2 200G
virsh attach-disk test101 /opt/kvm/test101-vdc.qcow2 vdc --persistent --driver qemu --subdriver qcow2

Attach Interface

## Bridge (br1)
## (optional) --model e.g.: 'rtl8139', 'e1000', 'virtio'
virsh attach-interface --domain test101 --type bridge --source br1 --model virtio --config