Virtualization

0. Check if the CPU supports virtualization

$ egrep 'vmx|smu' /proc/cpuinfo

1. Installing Virtualization Packages

$ sudo yum install qemu-kvm libvirt
  • qemu-kvm : Provides the user-level KVM emulator and facilitates communication between hosts and gest virtual machines
  • libvirt : Provides the server and host side libraries for interacting with hypervisors and hosts systems, and the libvirtd daemon that handles the library calls, manages virtual machines and controls the hypervisor

2. Install utilities

$ sudo yum install virt-install libvirt-python virt-manager libvirt-client
  • virt-install : Provides the virt-install command to create new virtual machines
  • libvirt-python : Allow python applications to use the libvirt API
  • virt-manager : Provides the virt-manager tool, also known as the Virtual Machine Manager
  • libvirt-client : Provides client-side APIs and libraries for accessing libvirt servers

Optional

We can also install the virtualization package groups, some of them are :

  • Virtualization Hypervisor
  • Virtualization Client
  • Virtualization Platform
  • Virtualization Tools

To install a package group with its optional libraries, we can execute the next command

$ sudo yum groupinstall "Virtualization Tools"

4. Configure libvirtd

Execute the next command, will also enable the network bridge needed to connect the virtual machine network with the host network

$ chconfig libvirtd on; shutdown -r now

5. Install Virtual Machines

$ virt-install --name centos7 --ram 1024 --disk path=./centos7.qcow2,size=8 --vcpus 1 --os-type linux --os-variant centos7.0 --network bridge=virbr0 --graphics none --console pty,target_type=serial --location 'http://mirror.i3d.net/pub/centos/7/os/x86_64/' --extra-args 'console=ttyS0,115200n8 serial'

6. Select the installer option

We will select the text mode in our case

7. Setup the installation configurations

8. List all virtual machines

Execute this command in order to list all virtual machines

$ virsh list --all

9. Start a virtual machine

Execute the next command in order to start a virtual machine, if the name of it is centos7

$ virsh start centos7

10. Get into a virtual machine console

$ virsh console centos7

11. Start a virtual machine at boot time

$ virsh autostart centos7

12. Shutdown a virtual machine

$ virsh shutdown centos7

13. Force a shutdown on a virtual machine

$ virsh destroy centos7

14. Delete a virtual machine

$ virsh undefine centos7

15. Set maximum memory

$ virsh setmaxmem centos7 2G --config

16. Set memory amount 

$ virsh setmem centos7 2G --config

17. Edit a virtual machine

$ virsh edit centos7

18. Display virtual machine information

$ virsh dominfo centos7

19. Reboot a virtual machine

$ virsh reboot centos7

20. Reize virtual machine disk

# Install tools like virt-resize
$ sudo yum install libguestfs-tools
# Check virtual machine current information
$ qemu-img info centos7.qcow2
# Add space to the virtual machine
$ qemu-img resize centos7.qcow2 +20GB
# Now we expand the filesystem inside the virtual machine, making a copy of it
$ cp centos7.qcow2 centos7-orig.qcow2
$ virt-resize --expand /dev/sda1 centos7-orig.qcow2 centos7.qcow2
# verify that the file system has indeed grow
$ virt-filesystems --long -h --all -a centos7.qcow2

References

Resize disk for vm
Manage virtual machines
Delete virtual machines with virsh
Install virtualization into a Redhat like OS
Virt-install command to create a virtual machine