In this article we will be seeing how to run multiple virtual machines in our host machine through cli mode.

Creating Logical volume for the Virtual Machine:

    Since the VM needs physical requirements such as Hard-disk, Memory, CPU. We will allocate those from the host machine. For HD we will create a logical volume using the below command,

$ lvcreate -L 50G -n ljunix.vmserver-1 kvm

lvcreate command use to create the logical volume.
-L stands for the size of the HD interms of GB
-n is for the Virtual Machine name.
kvm is the default path for the volumes being stored. The file path is /dev/kvm

Installing the Virtual Machine:

    Now our VM has some physical volume to run the installation, Since our Hypervisor is KVM we will be installing Kernel based virtual machines through virt command.

    virt-install is a command line tool which provides an easy way to provision operating systems into virtual machines.

    The following command will install a Centos 7 machine from the official Centos site,

$ virt-install --connect qemu:///system --name ljunix.vmserver-1 --ram 2048 --vcpus=2 --disk path=/dev/kvm/ljunix.vmserver-1 --location http://vault.centos.org/7.0.1406/os/x86_64/ --vnc --noautoconsole --os-type linux --accelerate --network=bridge:br0 --hvm

FlagDescription
--nameName of your VM.
--ramRAM for your VM(in Megabytes).
--vcpusNumber of virtual CPU's.
--disk pathLogical Volume path.
--locationISO Image location.
--vncEnables Virtual Network Computing
--noautoconsoleAuto console will be disabled.
--os-typeProbably Linux.
--networkCreates a bridge between the Guest machine and Host machine.
--hvmHardware-assisted Virtual Machine.

         With the above command we have launched a Centos 7 machine with 2 GB RAM and 2 VCPU's. You can also define different configuration and IOS images.

    The installation runs on the background and to complete the installation you have to use a GUI interface. That can be done using a Virtual Manager for linux users.

    For Centos images click here.

    For Ubuntu images click here.

To list the VM's and its states in your hypervisor use the below command.

$ virsh list --all

[jhony@ljunix~]$ virsh list --all
Id    Name                    State
----------------------------------------------------
1    ljunix.vmserver-1    running
-    ljunix.vmserver-2    shut off
3    ljunix.vmserver-3    running
-    ljunix.vmserver-4    shut off

To start a VM use the below command,

$ virsh start VM_Name

[jhony@ljunix~]$ virsh start ljunix.vmserver-2
Domain ljunix.vmserver-2 started

To stop a VM use the below command,

$ virsh shutdown VM_Name

[jhony@ljunix~]$ virsh shutdown ljunix.vmserver-3
Domain ljunix.vmserver-3 is being shutdown

[jhony@ljunix~]$ virsh list --all
Id    Name                    State
----------------------------------------------------
1    ljunix.vmserver-1    running
5    ljunix.vmserver-2    running
-    ljunix.vmserver-3    shut off
-    ljunix.vmserver-4    shut off

Deleting a Virtual Machine:

    Before deleting a VM you have to undefine it. The undefine allows you to edit your VM configuration such as Name, RAM, VCPU,etc.,. We will see this is detail on upcoming articles. Use the following command to undefine a VM.

$ virsh undefine VM_Name

[jhony@ljunix~]$ virsh undefine ljunix.vmserver-3
Domain ljunix.vmserver-3 has been undefined

[jhony@ljunix~]$ virsh list --all
Id    Name                    State
----------------------------------------------------
1    ljunix.vmserver-1    running
5    ljunix.vmserver-2    running
-    ljunix.vmserver-4    shut off

    Now you can delete your VM using destroy command,

$ virsh destroy VM_Name

[jhony@ljunix~]$ virsh destroy ljunix.vmserver-3
Domain ljunix.vmserver-3 destroyed

    We can release the logical volume used by the destroyed VM using lvremove command.

$ lvremove path_to_the_logical_volume

[jhony@ljunix~]$ lvremove /dev/kvm/ljunix.vmserver-3
Do you really want to remove active logical volume kubemaster3? [y/n]: y
Logical volume "kubemaster3" successfully removed

    Feel free to ask if you have any questions.

Comments

  1. Be the first to add a comment.