Introduction
Virtualisation, Kernel Virtual Machine (KVM) using libvirt in CentOS 7. Create and edit a virtual drive image. Create a Windows server guest and add a data drive when offline or running guest.
Hardware
CPU: Intel(R) Xeon(R) 2 GHz and above
RAM: 32 GB and above
Host
OS: CentOS Linux release 7.4.1708 (Core)
Check KVM capability
egrep '^flags.*(vmx|svm)' /proc/cpuinfo
CPU info
cat /proc/cpuinfo virsh sys-info
Check for AMD-V extension
modprobe kvm-amd
Check free memory
virsh freecell
Check NUMA and CPU mappings
virsh capabilities
Check cpu mapping for running vm
virsh vcpuinfo <domain name>
Disk/Drive image
Disk image info
qemu-img info <image path>
Only a selected group of formats support consistency checks. These include qcow2, vdi, vhdx, vmdk, and qed.
qemu-img check [-f format] <image path>
Create drive image
Default drive format is Raw
System drives should be non-sparse, data drives can be sparse.
qemu-img create -f raw <image path> 5G
Create new drive (default sparse is ON, drive size will be minimum and will increase when used)
qemu-img create win2k12r2_disk1.raw 32G or qemu-img create win2k12r2_disk1.raw 32G -f raw or qemu-img create win2k12r2_disk1.raw -f raw -o size=32G
For non-sparse image of 10 GB
dd if=/dev/zero of=non-sparse_raw.img bs=1M count=10240
Get disk info
qemu-img info /path/to/disk/image
Convert image format
From raw (sparse) -> raw (non-spase)
cp --sparse=never source.img destination.img
From qcow2 or raw (non-sparse) -> raw (sparse)
qemu-img convert -O raw source.img destination.img
From raw (sparse, non-sparse) -> qcow2
qemu-img convert -O qcow2 source.img destination.img
Resize image
qemu-img resize raw.img +10GB
Add drive to vm (running)
for ide
virsh attach-disk win2012r2 /data/vm/win2012r2_disk1.raw --targetbus virtio --cache none
for virtio
virsh attach-disk win2012r2 /data/vm/win2012r2_disk1.raw --targetbus virtio --cache none
Add drive to vm (shutdown/offline)
Edit guest xml and add
For ide bus
<disk type='file' device='disk'> <driver name='qemu' type='raw' cache='none'/> <source file='/data/vm/win2012r2_disk1.raw'/> <target dev='hdb' bus='ide'/> </disk>
For virtio bus
<disk type='file' device='disk'> <driver name='qemu' type='raw' cache='none'/> <source file='/data/vm/win2012r2_disk1.raw'/> <target dev='vdb' bus='virtio'/> </disk>
Virtio-Win
Use the Fedora Virtio-Win repo to install vid, iso and drives to /usr/share/virtio-win/
wget https://fedorapeople.org/groups/virt/virtio-win/virtio-win.repo -O /etc/yum.repos.d/virtio-win.repo yum install virtio-win
Create Guest: Windows Server 2012 R2
Command
virt-install --help virt install --option=?
Create guest virtual machine
virt-install \ --name winserver-2012r2 \ --memory 32768 \ --disk path=/data/vm/winserver-2012r2/winserver-2012r2_disk0.img,format=raw,size=100,sparse=no,cache=none,bus=virtio \ --controller scsi,model=virtio-scsi \ --graphics vnc,password="ansura.net" \ --vcpus 8,cpuset=auto \ --cpu host \ --cdrom /data/iso/win2012r2_oem.iso \ --network bridge=br0,model=virtio \ --disk path=/usr/share/virtio-win/virtio-win_servers_amd64.vfd,device=floppy \ --os-variant win2k12r2 \ --noautoconsole \ --dry-run
Options
- For a NUMA system, use the cpuset=auto
- Use option –dry-run to check parameters
- Post install mount the virtio-win.iso file and install the balloon driver (its not there in vfd file).