Introduction

This first article in the LFCS series is showing you how to prepare a potential learning environment. I use CentOS 7 as my main Linux Distribution, and I will guide you through the process of installing a CentOS virtual machine using KVM. The instructions should be similar if you are planning on using Debian too. What could be different are the names of the packages to install. As I would like to be able to help you as much as I can, I will have a new article on how to use KVM in Debian later.

IMPORTANT NOTICE!

Now, an important thing to keep in mind is that depending on the Linux distribution you are using, the names of the packages to install could be different. This is really important, as you could find yourself in strange situations trying to install packages with the name from another distribution, wondering why that particular package is not in the repositories.


With this exercises, we will cover the Virtualization competences, which is a great thing. I will show you everything using only the command line because while you are in the exam, the only available environment for you is the CLI. As I know that all those tasks are easier to accomplish using the GUI, you will need to know how to do them using only the command line! Below are the Virtualization competences listed:


  • Virtualization - 5%

    • Configure a hypervisor to host virtual guests
    • Access a VM console
    • Configure systems to launch virtual machines at boot
    • Evaluate memory usage of virtual machines
    • Resize RAM or storage of VMs


Preliminary requirements

Before stepping into installing KVM, you should be certain that your system has virtualization available and enabled. For this, you should look for vmx (for Intel procs) or svm (for AMD procs) inside the cpuinfo file:

cat /proc/cpuinfo | grep vmx

Now, check if the proper kernel modules for virtualization have been loaded.

lsmod | grep kvm


Installing KVM

To install the entire Virtualization stack you will need to install the following groups from the repositories:

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

Thus, in CentOS, you will use the command:

sudo yum groupinstall "Virtualization Hypervisor" "Virtualization Client" "Virtualization Platform" "Virtualization Tools"

Optional, you could install the following packages:

sudo yum install qemu-kvm-tools


Enabling KVM

Now you must enable the libvirt daemon to start at system start. You will do that with the command:

sudo systemctl enable libvirtd
sudo systemctl start libvirtd

You can check the staus with the command:

sudo systemctl status libvirtd


Download the CentOS 7 iso file

Before creating a virtual machine, you will need to download the iso file of CentOS 7. I will show you how to download the iso file using the command line, but in order to do this, you will need to know the address/location of the file. I will use a mirror that is available in my country (Romania), but you should use one that is available to yours. You will find a list of available mirrors for your country at the following address: http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1708.iso

From all the mirrors listed, I will choose this one: http://ftp.ines.lug.ro/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1708.iso. Now, in order to download the file, I will use the wget command, as follows:

wget -c 'http://ftp.ines.lug.ro/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1708.iso'

Now, to check that everything is in order, I will use the command:

cd ~/Downloads
ls -la
total 4418804
drwxr-xr-x.  4 alexandru alexandru       4096 Mar 17 15:33 .
drwx--x---+ 23 alexandru alexandru       4096 Mar 17 11:23 ..
drwxr-xr-x.  2 alexandru alexandru       4096 Mar 17 11:24 centos
-rw-rw-r--.  1 alexandru alexandru  851459712 Sep  6  2017 CentOS-7-x86_64-Minimal-1708.iso
drwxr-xr-x.  2 alexandru alexandru       4096 Mar 17 11:25 debian
-rw-rw-r--.  1 alexandru alexandru     100023 Aug 24  2017 dropbox.rpm

And you will see that the CentOS iso file is there.

Now, in order to avoid any SELinux related problems, you will have to move your iso file from the Downloads directory to the /var/lib/libvirt/images/ directory. For this, you should run the command:

sudo cp -p /home/alexandru/Downloads/CentOS-7-x86_64-Minimal-1708.iso /var/lib/libvirt/images/


Download the Debian 9 iso file

Before creating a virtual machine, you will need to download the iso file of Debian 9. I will show you how to download the iso file using the command line, but in order to do this, you will need to know the address/location of the file. I will download it from the following address: https://cdimage.debian.org/cdimage/unofficial/non-free/cd-including-firmware/9.4.0+nonfree/amd64/iso-cd/firmware-9.4.0-amd64-netinst.iso

Now, in order to download the file, I will use the wget command, as follows:

wget -c 'https://cdimage.debian.org/cdimage/unofficial/non-free/cd-including-firmware/9.4.0+nonfree/amd64/iso-cd/firmware-9.4.0-amd64-netinst.iso'

Now, to check that everything is in order, I will use the command:

cd ~/Downloads
ls -la

And you will see that the Debian iso file is there.

Now, in order to avoid any SELinux related problems, you will have to move your iso file from the Downloads directory to the /var/lib/libvirt/images/ directory. For this, you should run the command:

sudo cp -p /home/alexandru/Downloads/firmware-9.4.0-amd64-DVD-1.iso /var/lib/libvirt/images/

Following, we will focus on the centos7 guest, but you could change the commands accordingly to use the debian9 virtual guest. I just wanted to show you how to download the Debian 9 iso in case you wanted to use it instead of CentOS.


Prepare to create a new Virtual Guest

Configure the NAT and default Network

NAT is the most common method to share network connections. In order to do this, you must configure the host with the command:

sudo virsh net-list --all

If your output is empty, check to see if you have the default.xml file in /etc/libvirt/qemu/networks/. If you do, then run the following command:

sudo virsh net-autostart default

and then

sudo virsh net-list --all

and the output should be as follows:

 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes

Now, if you run the command

brctl show

you will see that there is an isolated bridge device available, but it uses NAT and has no physical interface added.

bridge name	bridge id		STP enabled	interfaces
virbr0		8000.52540049910c	yes		virbr0-nic
							vnet0

To be sure that everything will work as planning, you should add the line net.ipv4.ip_forward = 1 to the /etc/sysctl.conf file.

Now, you should continue to create a Virtual Guest.


Create a new CentOS 7 Virtual Guest

To create a new virtual machine with KVM, you will need to use the virt-install utility. This utility has several parameters that you will need to fill in order for the VM to be created correctly. Let’s see them. You can also read about them using the man virt-install command.

NOTE: the – sign is actually two hyphens like - - but without the space between them!

  • –name = the name of the virtual machine you are about to create
  • –os-type = the type of the os, in our case Linux
  • –os-variant = what variant of the os type is it, in our case rhel7
  • –disk path = location of the disk image
  • –disk size = the size of the disk image to be created - in GiB
  • –disk bus = the
  • –graphics = is the way to connect to the virtual machine - usually it is spice
  • –vcpu = the number of virtual cpu’s
  • –ram = the amount of RAM in megabytes
  • –cdrom = the source path of the installation iso file
  • –network = the bridge adapter to be used by the virtual machine

The command to setup a CentOS Virtual Guest is as follows:

sudo virt-install --name=centos7 --os-type=Linux --os-variant=rhel7 --memory=1024 --vcpus 1 --disk path=/var/lib/libvirt/images/centos.qcow2,bus=virtio,size=9 --graphics=spice --cdrom /var/lib/libvirt/images/CentOS-7-x86_64-Minimal-1708.iso --network=default

This command will automatically switch to the gui and virt-manager interface, showing you the installation options.

Below you have the GUI process of installing CentOS7 Minimal in the new Virtual Guest.

one

two

three

four

five

six

seven

eight

nine


Create a new Debian 9 Virtual Guest

The command to setup a Debian 9 Virtual Guest is as follows:

sudo virt-install --name=debian9 --os-type=Linux --os-variant=debian --memory=1024 --vcpus 1 --disk path=/var/lib/libvirt/images/debian.qcow2,bus=virtio,size=9 --graphics=spice --cdrom /var/lib/libvirt/images/firmware-9.4.0-amd64-netinst.iso --network=default

This command will automatically switch to the gui and virt-manager interface, showing you the installation options.

Below is only one screenshot of the Debian 9 installer inside a Virtual Guest:

deb-one


Working with the Virtual Guest from the Command Line

Now, what we would like to do is to connect to the new virtual guest through the command line and do all the tasks needed from our distribution’s terminal. For this, we will use the virsh command.
For this exercise, I have closed our newly created virtual guest and now we will have to connect to it using our terminal. This is how to do it.
First, we will use virsh to see which guests are running. Remember to use sudo or sudo su to log in as root!!! The command to see this is:

sudo virsh list

Now, if we would like to see all the available guests, including the ones that are not running, we will use the same command with the --all option at the end:

sudo virsh list --all

virsh-list

Now, we can start our virtual guest from the command line, using virsh and the name of the guest, in our case centos7.

Start the Virtual Guest

sudo virsh start centos7

Notice in the image below that in the left window, the centos7 guest (or domain how virsh is calling it) shows to be running.

virsh-start

Obtain the Virtual Guests’ IP address

Now, in order to connect to the virtual guest using the command line, we will need to know it’s IP, and it would be great to know that IP without connecting to it. How could we do this? Well, just issueing a ip addr command won’t show me the virtual machines IP. There is another command that does this, and it is the arp command used with the -an options.

arp -an

In my case, the output is as follows:

? (192.168.0.19) at xx:xx:xx:xx:xx:xx [ether] on enp14s0
? (192.168.0.1) at xx:xx:xx:xx:xx:xx [ether] on enp14s0
? (192.168.122.93) at xx:xx:xx:xx:xx:xx [ether] on virbr0

and it shows the gateway (.1) another machine on the network (.19) and the virtual guest (.93). Now, that we know its IP, we can ssh into it and run the commands directly on the virtual guest.

Connect through SSH to the Virtual Guest

To simply connect to the VG, use the following command: ssh user@virt-guest-IP. In my case, this translates into:

ssh alexandru@192.168.122.93

The output is:

The authenticity of host '192.168.122.93 (192.168.122.93)' can't be established.
....
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.122.93' (ECDSA) to the list of known hosts.
alexandru@192.168.122.93's password: 
[alexandru@localhost ~]$ 

The last line in the terminal shows that we are connected to the host with the user alexandru. If I type exit then the connection with the virtual host will be terminated:

[alexandru@localhost ~]$ exit
logout
Connection to 192.168.122.93 closed.
[alexandru@localhost lark]$ 

Close the running Virtual Guest

To close the running virtual guest, we will use virsh again with the shutdown option and the guest/domain name:

sudo virsh shutdown centos7

virsh-shutdown


Cloning a Virtual Guest

You can clone a newly installed and unaltered virtual guest, in case you want to have multiple instances of the same OS. To clone from the command line, you will use the virt-clone command.
For example, to clone the centos7 guest, to a new, automatically generated guest, we will use the command:

sudo virt-clone --original centos7 --auto-clone

virt-clone

Now you have two virtual guests, that are identical. We can start them both using the virsh command. Also, we could run an arp -an to check for their IPs too. In my case, the output would be:

? (192.168.122.244) at xx:xx:xx:xx:xx:xx [ether] on virbr0
? (192.168.0.19) at xx:xx:xx:xx:xx:xx [ether] on enp14s0
? (192.168.0.1) at xx:xx:xx:xx:xx:xx [ether] on enp14s0
? (192.168.122.93) at xx:xx:xx:xx:xx:xx [ether] on virbr0

virsh-start-2


Change the amount of RAM and CPU of a Virtual Guest

Now, in order to change the amount of RAM and CPU, you should have the guests shut down at first. Let us take for example the centos7 guest. With it shut down, we will run the following command:

sudo virsh edit centos7

This will open a VIM-like editor where you can edit the XML configuration file of the guest. To change the amount of RAM, look for the lines that say:

<memory unit='KiB'>1048576</memory>
<currentMemory unit='KiB'>1048576</currentMemory>

The amount is written in KiB and in binary mode. Thus, 1048576 = 1 GB of RAM. To change this in 2G of RAM, you should change the amount to 2097152. Thus, 2097152 = 2 GB of RAM. After you change the lines, press esc and then :wg! to save the file and exit the editor. Now that the file is rewritten, you can run the command virsh dominfo centos7 to display the domain info. You will see that the Max memory and Used memory are changed from 1G to 2G.

Here is a short list of tranformations:

  • 512 MB = 524288 KB (in binary)
  • 1 GB = 1,048,576 KB (in binary)
  • 2 GB = 2097152 KB (in binary)
  • 4 GB = 4194304 KB (in binary)
  • 6 GB = 6291456 KB (in binary)
  • 8 GB = 8388608 KB (in binary)
  • 10 GB = 10485760 KB (in binary)
  • 12 GB = 12582912 KB (in binary)
  • 14 GB = 14680064 KB (in binary)
  • 16 GB = 16777216 KB (in binary)

To change the CPU number, look for the line that says:

 <vcpu placement='static'>1</vcpu>

and change the number from 1 to 2. This will give the Virtual Guest a number of 2 virtual CPUs. To see the changes, run the virsh dominfo centos7 command and the amount of CPUs has changed to 2.

virsh dominfo centos7
Id:             10
Name:           centos7
UUID:           0b86ac1f-5a1c-4daf-a3c5-98fa44356abb
OS Type:        hvm
State:          running
CPU(s):         2
CPU time:       28.1s
Max memory:     2097152 KiB
Used memory:    2097152 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: selinux
Security DOI:   0
Security label: system_u:system_r:svirt_t:s0:c477,c482 (enforcing)


Resize the amount of storage for the Virtual Guest

One other task in the LFCS list of competences is to change the size of the Virtual Guest storage. Now, as you remember, when we created the virtual guest we also created an image for the virtual guest that was 9G in size with the options --disk path=/var/lib/libvirt/images/centos.qcow2,bus=virtio,size=9 inside the virt-install command. Let’s say that we need the image to be 15G in size. In order to do this, we will use the command:

sudo qemu-img resize /var/lib/libvirt/images/centos7.0.qcow2 +6G

This will increase the size to 15G.

NOTE: We will run this command as the virtual guest is turned off!


Now, you can check to see if the image really has 15G. For this, you will use the command:

sudo qemu-img info /var/lib/libvirt/images/centos7.0.qcow2

In my case, the output is:

image: /var/lib/libvirt/images/centos7.0.qcow2
file format: qcow2
virtual size: 15G (16106127360 bytes)
disk size: 1.3G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true
    refcount bits: 16
    corrupt: false

You can also SSH into the virtual guest and check it’s file size using the sudo fdisk -l command. This will show the exact same size, even though a little bit different. Here is my output:

$ sudo fdisk -l

Disk /dev/vda: 16.1 GB, 16106127360 bytes, 31457280 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000c1b12

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200    18874367     8387584   8e  Linux LVM

Notice that, the amount of bytes is actually the same, even if one output says 15 and the other 16 G. They both are essentialy 16106127360 bytes in size.

There you have it, you changed the size of the virtual guests’ image from 9G to 15G.

Nevertheless, if we try and check the size of the qcow2 file on the host, we will see this:

sudo ls -lah /var/lib/libvirt/images/centos7.0.qcow2
-rw-------. 1 root root 9.1G Mar 18 12:12 /var/lib/libvirt/images/centos7.0.qcow2

As you can see, the size is still 9.1G on disk, which is fine, as we modified the virtual size only.

Now we will resize the filesystem also, using the commands virt-resize and virt-filesystems. For this, we will need to install the libguestfs-tools-c package first:

sudo yum install libguestfs-tools-c

and we will make a copy of the qcow2 file as follows:

cp centos7.0.qcow2 centos7.1.qcow2

Then, we will use the qemu info from the 7.1 file to modify the 7.0 file.

But until then, we will check the underlying filesystem of the centos7.1.qcow2 file with the command:

sudo virt-filesystems --partitions --long -a centos7.1.qcow2

Name       Type       MBR  Size        Parent
/dev/sda1  partition  83   1073741824  /dev/sda
/dev/sda2  partition  8e   8588886016  /dev/sda

This will show you that the underlying filesystem is still 9G, which we will need to change to 15G. Out of the two partitions /dev/sda1 and /dev/sda2 we will change only the second one, as the first one is for boot. Thus, we will use the command:

sudo virt-resize --expand /dev/sda2 centos7.1.qcow2 centos7.0.qcow2 

The output will be:

[   0.0] Examining centos7.1.qcow2
**********

Summary of changes:

/dev/sda1: This partition will be left alone.

/dev/sda2: This partition will be resized from 8.0G to 14.0G.  The LVM PV 
on /dev/sda2 will be expanded using the ‘pvresize’ method.

**********
[   3.0] Setting up initial partition table on centos7.0.qcow2
[   3.5] Copying /dev/sda1
 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ --:--
[   6.2] Copying /dev/sda2
 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
[  30.6] Expanding /dev/sda2 using the ‘pvresize’ method

Resize operation completed with no errors.  Before deleting the old disk, 
carefully check that the resized disk boots and works correctly.

As you can see, the partition was modified from 8 to 14G, which is correct, because the missing 1G is actually the size of the boot partition /dev/sda1.

If we will do another fdisk -l on the guest, we will have another output:

Disk /dev/vda: 16.1 GB, 16106127360 bytes, 31457280 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000c1b12

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200    31454847    14677824   8e  Linux LVM

You will see that the number of blocks have changed, meaning that the partition has been modified.

Now, if you want to shrink the image with a similar command but with a -6G at the end (to make it back to 9G) you will get an error, just like this:

sudo qemu-img resize /var/lib/libvirt/images/centos7.0.qcow2 -6G
qemu-img: qcow2 doesn't support shrinking images yet


Configure a Virtual Guest to automatically start at boot

Now, if you would like to make everything more easy and the virtual guest to start automatially when the system starts, you can use the virsh autostart command. Like this:

sudo virsh autostart centos7

This will assure that the centos7 guest will automatically start at boot. To check this, you can also run the command virsh dominfo centos7 like this:

sudo virsh dominfo centos7

If you want to disable autostart, you can use the --disable option like this:

sudo virsh autostart centos7 --disable

virsh-autostart


Access a Virtual Guest console

Now, lets say that you don’t want to acces a Virtual Guest using SSH, but you prefer to connect to it using the console provided by virsh. This could be useful when troubleshooting or when you don’t have internet connection or the possibility to access the GUI. Nevertheless, I think that you would most likely connect through SSH. But, as this is one of the competences from LFCS, why should just solve this elegantly. Here is a solution. This solution has to distinct stages:

  • first you must set the serial console on the virtual guest
  • secondly you need to configure the KVM host to enable the serial console

Set the serial console on the virtual guest

First, you should connect to the virtual guest using SSH and edit the GRUB file. This how to do this:

ssh alexandru@192.168.122.93
sudo yum install vim
sudo vim /etc/default/grub

I had to install vim, as it was not available by default in the Minimal install of CentOS 7. After I installed it, I was able to open the grub file. Now, inside that file, look for the line that says:

GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet"

and replace it with the line:

GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet console=ttyS0"

Now press esc and :wq! to save and quit the editor.

edit-grub

Next step is to rebuild the grub config file using the command:

grub2-mkconfig -o /boot/grub2/grub.cfg

rebuild-grub

Configure the KVM host to enable the serial console

Now, lets say that we want to configure only one of the virtual guests, for example centos7. If we were to configure both, it is really simple, just redo the steps for the second clone.

First we will run the virsh edit command to edit the configuration file of the host:

sudo virsh edit centos7

Inside the editor, search for the line that says:

<console type='pty'>
 <target type='serial' port='0'/>
 </console>

config-old

and change it to:

<console type='pty' tty='/dev/pts/6'>
 <source path='/dev/pts/6'/>
 <target type='serial' port='0'/>
 <alias name='serial0'/>
 </console>

config-new

Now, like in VIM, press esc and then wg! to save and exit the editor. The next step is to close the guest with the command:

sudo virsh shutdown centos7

shutdown

and then start it up again with the command:

sudo virsh start centos7

and then connect to the serial console of the guest with the command:

sudo virsh console centos7

This will bring up the serial console inside our terminal and thus it is much easier to work with the guest. If we want to close the serial console, we would just have to press Ctrl+].

console-login


SUMMARY

As you can see, going through the needed Virtualization competences of LFCS created a quite long article, which means that, if you didn’t figure that yet, the subject of Virtualization is a really broad and complicated one. Even though it has only 5% out of the total number of competences, it is a very important subject. A subject that you will have to know very well in order to work with Virtual Machines and Guests on a daily basis.


Now, as a Conclusion, I would like to make a “short” list with all the commands and parameters/options that we used in this article. Those commands you will have to understand and memorize in order to be able to pass the Virtualization tasks inside an LFCS exam. If not all the parameters, at least the base commands, as you will always have the man pages at your disposal.


Just to imagine how demanding this is, I will tell you that writing this article took me two full days, as there was a constant need to test and experiment with all the commands and see that everything is working fine. Plus, the actual writing of the article. Thus, if you plan on taking this certification, I think that at least one month of constant preparation and exercising with the command line interface will be needed. And if you think that they teach you all this in the LF201 Essentials of Linux Administration course, you are very wrong, my friend. That course is only the tip of the iceberg, is more like an introduction compared to the competences they present. The labs from that course are quite good though!


List of Commands


Checking to see if virtualization is supported

cat /proc/cpuinfo | grep vmx
lsmod | grep kvm


Installing KVM

sudo yum groupinstall "Virtualization Hypervisor" "Virtualization Client" "Virtualization Platform" "Virtualization Tools"
sudo yum install qemu-kvm-tools


Enabling KVM

sudo systemctl enable libvirtd
sudo systemctl start libvirtd
sudo systemctl status libvirtd


Download and copy ISO file

wget -c 'http://ftp.ines.lug.ro/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1708.iso'
cd ~/Downloads
ls -la
sudo cp -p /home/alexandru/Downloads/CentOS-7-x86_64-Minimal-1708.iso /var/lib/libvirt/images/


Prepare to create Virtual Guest

sudo virsh net-list --all
ls -la /etc/libvirt/qemu/networks/
sudo virsh net-autostart default
sudo virsh net-list --all
brctl show
vim /etc/sysctl.conf #add line: net.ipv4.ip_forward = 1


Create a new Virtual Guest

sudo virt-install --name=centos7 --os-type=Linux --os-variant=rhel7 --memory=1024 --vcpus 1 --disk path=/var/lib/libvirt/images/centos.qcow2,bus=virtio,size=9 --graphics=spice --cdrom /var/lib/libvirt/images/CentOS-7-x86_64-Minimal-1708.iso --network=default


Working with the Virtual Guest from the command line

sudo virsh list
sudo virsh list --all
sudo virsh start centos7 #start the virtual guest
arp -an #obtain vg ip address
ssh alexandru@192.168.122.93 #ssh into the vg
sudo virsh shutdown centos7 #close the running guest


Cloning a Virtual Guest

sudo virt-clone --original centos7 --auto-clone


Change amount of RAM and CPU of Virtual Guest

sudo virsh shutdown centos7
sudo virsh edit centos7
#edit the config file at line
<memory unit='KiB'>1048576</memory>
<currentMemory unit='KiB'>1048576</currentMemory>
#and at line
<vcpu placement='static'>1</vcpu>
#check info about the domain/guest
sudo virsh dominfo centos7


Resize storage of the Virtual Guest

sudo qemu-img resize /var/lib/libvirt/images/centos7.0.qcow2 +6G
sudo qemu-img info /var/lib/libvirt/images/centos7.0.qcow2
ssh alexandru@192.168.122.93
sudo fdisk -l
#back on the host we do
sudo ls -lah /var/lib/libvirt/images/centos7.0.qcow2
#install an extra package
sudo yum install libguestfs-tools-c
#create a copy of the file
cp centos7.0.qcow2 centos7.1.qcow2
#check underlying filesystem
sudo virt-filesystems --partitions --long -a centos7.1.qcow2
#resize the underlying filesystem
sudo virt-resize --expand /dev/sda2 centos7.1.qcow2 centos7.0.qcow2 
#check to see if shrinking is working
sudo qemu-img resize /var/lib/libvirt/images/centos7.0.qcow2 -6G


Configure a Virtual Guest to automatically start at boot

sudo virsh autostart centos7
sudo virsh dominfo centos7
#to disable autorestart do
sudo virsh autostart centos7 --disable


Access a Virtual Guest console

#set the serial console on the virtual guest
ssh alexandru@192.168.122.93
sudo yum install vim
sudo vim /etc/default/grub
#add the last string on this line:
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet console=ttyS0"
#rebuild the grub
grub2-mkconfig -o /boot/grub2/grub.cfg
#now configure the KVM host to enable serial console
sudo virsh edit centos7
#edit the config file like this:
<console type='pty' tty='/dev/pts/6'>
 <source path='/dev/pts/6'/>
 <target type='serial' port='0'/>
 <alias name='serial0'/>
 </console>
#close the guest
sudo virsh shutdown centos7
sudo virsh start centos7
sudo virsh console centos7