[Linux] Building up a virtual machine with qemu

You are here:
  • Main
  • Linux
  • [Linux] Building up a virtual machine with qemu
< All Topics


  • CentOS 7
  • Root privileges

Hi all, in this tutorial we are going to build a virtual machine using qemu and virsh.

1 – Setup environment

First of all we have to install all the necessary packages:

yum install qemu-kvm qemu-img virt-manager libvirt libvirt-python libvirt-client virt-install

Then we have to start the libvirt daemon and check if the kvm module is installed:

systemctl enable libvirtd
systemctl start libvirtd
lsmod | grep kvm

In the end, we are going to download the .iso image of the OS we are going to install in our virtual machine, in this case the minimal version of CentOS 7:


2 – Storage pool definition

Next, we are going to define out storage pool, where our machine will be created:

mkdir -p /home/mymachine
virsh pool-define-as mypool dir - - - - "/home/mymachine"
virsh pool-build mypool
virsh pool-start mypool

We can check the status of our pool with the following command:

virsh pool-list --all
Name                 State      Autostart
mypool               active     no

3 – Virtual Machine creation

In the end, we are going to create our virtual machine:

qemu-img create -f raw /home/mymachine/disk1.img 4G
virt-install --name=mymachine --disk path=/home/mymachine/disk1.img --vcpu=1 --ram=1024 --location=/tmp/CentOS-7-x86_64-Minimal-1908.iso --console pty,target_type=serial --graphics none --extra-args='console=ttyS0'

I will explain all of the arguments:

  • name: name of the machine
  • disk: physical path of the virtual machine disk
  • vcpu: number of cpu assigned to the machine
  • ram: amount of RAM assigned to the machine
  • location: physical path of the operating system to install
  • console: specifies that we are using a serial console to create the virtual machine
  • graphics: specifies that we do not want to use any GUI for the installation process
  • extra-args: in this case specifies the correct console to use (ttyS0 on CentOS)

Once started, the installation proceeds as usual, for those not used to using only command line and not GUI can be a bit tricky, but in the end the steps are the same.

Hope it was clear, see you in the next tutorial!

Table of Contents