LFCS - Creating Virtual Machines in KVM

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
368
Reaction score
423
Credits
13,526
Now that we have installed the Kernel-based Virtual Machine (KVM), if you have not done this then see LFCS -KVM Installation, we are ready to create Virtual Machines (VM).

We have three basic options to install a KVM:
  1. ISO file
  2. FTP/HTTP/PXE
  3. Command-Line Interface (CLI)
We will cover these each separately, but the PXE installation requires a lot more setup than the other two options.

ISO File

This is the easiest method to perform. If you have used another Virtual method, such as VirtualBox, then you should be familiar with performing this installation.

Because we are using an ISO file, we must download the ISO from the Internet. Place the ISO file in a place where you can access it easily. By default, on CentOS, use the folder '/var/lib/libvirt/images'. Do not extract the ISO file. For Ubuntu, use the path '/var/lib/libvirt/boot'.

Open 'Virt-Manager' and you should see a window like Figure 1.

Figure 01.jpg

FIGURE 1

The top-left icon allows you to create a new virtual machine, it is in Figure 1 with the cursor on it.

After you click on the icon, a new window should appear to allow you to select what type of installation you want to perform. You can see the window in Figure 2.

Figure 02.jpg

FIGURE 2

Here, you can see four options. The first choice is to use an ISO or a CD-ROM. The second option is to use an FTP/HTTP/NFS Server. In the LFCS articles, we’ve gone over these types of servers before, except NFS. The third choice is to boot the Virtual Machine with PXE. PXE will require FTP, TFTP and DHCP to work. Information on these are in the PXE article. Your last choice is to load an existing disk image and import it. This can happen if you move a Virtual Machine from another system, or delete the VM from your 'Virt-Manager' and not delete the VM from the disk.

Make your choice. Here, select the first option for the ISO file. Click 'Forward' to continue to the next step.

Now, in Figure 3, you can select to use a CD or DVD and then select the drive. There is also an option to choose an ISO file. After making this selection, you click on 'Browse' and find the ISO. Again, the default folder to open for the ISO file is '/var/lib/libvirt/images'. Once you have made your choice and set the location of a disc or file, click on 'Forward' to continue creating the VM.

Figure 03.jpg

FIGURE 3

The next window is 'Step 3 of 5' for setting up the VM, as shown in Figure 4. Here, we can specify the amount of memory to set aside for use by the VM, as well as the number of CPU cores. Make your choices and click on 'Forward'.

Figure 04.jpg

FIGURE 4

Next, we set the amount of disk space we will use for the VM. Figure 5 shows the window. Click 'Forward' when done setting the amount of storage space to use.

Figure 05.jpg

FIGURE 5

The last step, Figure 6, is to give a name to the VM and check all the choices made for the VM. Click 'Finish' to finish the setup.

Figure 06.jpg

FIGURE 6

The system creates the VM and starts it. In an example, I used the 'CentOS 7.2' ISO and created a VM. After creating the VM, the machine starts and you can see it in Figure 7.

Figure 07.jpg

FIGURE 7

You can see that the installation ISO is starting and you can now install the ISO you downloaded.

FTP/HTTP/PXE Installation

For this type of installation, you need to set up and configure the FTP/HTTP/NFS Server on your network (it can also be the local system for testing). In this example, I set up my system as an FTP Server.

Within the folder '/var/ftp/pub', you need to create a folder for your various Operating Systems (OS). In my case, I made a folder named 'CentOS72'.

I mounted the ISO file for CentOS 7.2 to the folder '/mnt/extract', then copied the files and folders from the mounted folder to '/var/ftp/pub/CentOS72/' with the command 'sudo cp -r /mnt/extract/* /var/ftp/pub/CentOS72'. Once extracted, I can start the FTP install by selecting the second option in Figure 2.

After clicking 'Forward', I then see Figure 8. Here, I can put in the URL of the FTP Server and folders to the installation files. On my system, the IP Address is '192.168.1.217' and the URL is 'ftp://192.168.1.217/pub/CentOS72/'.

Figure 8.jpg

FIGURE 8

The remaining three steps are the same in Figures 4 to 6.

After you create the VM, it should start up the installation as it would use an ISO file. Just make sure you set up the FTP/HTTP/PXE server properly.

NOTE: Unlike using the ISO file, you need to extract the ISO file for use by the FTP or HTTP method.

Command-Line Interface Installation

The GUI seems easier to use, but some people want to work from the command-line. The command-line uses the command 'virsh'.

To install and view virtual machines from the command-line, you need to install:

for CentOS:
Code:
sudo yum install virt-install virt-viewer
for Ubuntu:
Code:
sudo apt install virt-install virt-viewer

We can get a list of the virtual machines with the command 'sudo virsh list', but to see those that are turned off, use the command 'sudo virsh list -all'.

Now, every choice made in the GUI method needs to be specified in the command-line method as well.

The minimum parameters are:

  • --name – name of the virtual machine
  • --memory – amount of memory allocated for the virtual machine
  • --disk – name and location of an image file for storage for the virtual machine
  • --cdrom or --location – local path or the URL to the ISO image of the OS
An example would be:

virt-install --name CentOS_72 --memory 2048 --disk path=~/VM/CentOS72.img,size=10 --network network=default --location ~/Downloads/CentOS72.iso

Here, the name of the new VM will be 'CentOS_72'. The amount of RAM set aside for the VM is '2,048 MB' or 2 GB. The path to the saved VM is at '~/VM' and the file is called 'CentOS72.img', while the file or storage size is '10 GB'. We are using the 'default' virtual network adapter. The location of the installation media, an ISO file, is at '~/Downloads/CentOS72.iso'.

The command should create the VM and start the VM in the 'virt-viewer'.'

Once it creates the VM, we can start it from the CLI with the command 'virsh start CentOS_72'. You can change the VM name to whatever you want, but do not use spaces in the name.

We can also shut down a running VM with the command 'virsh shutdown CentOS_72'. At an extreme, if the VM does not respond, you can force it to shut down with the command 'virsh destroy CentOS_72'.

Other parameters that you can use when creating a VM are:

  • --vcpus=# - set the number of CPUs to allocate to the VM. The default is 1
  • --os-variant=rhel7 - sets the general distro type. This example is specifying Red Hat Linux 7, or CentOS7.
Conclusion

This should give you a basic understanding of setting up and using KVM to create a Virtual Machine.

Practice these techniques to become familiar with them. Also, try to install various distros to practice the methods. Be sure to try both the GUI and CLI to be aware of how they both work.
 
Last edited:

Members online


Top