Linux KVM CLI Basics

These are some notes I wrote sometime ago.

Installation:

The following packages may need to be installed:

 yum update -y
 yum install libvirt qemu-kvm virt-manager virt-install
 yum install libguestfs-tools
 yum install qemu-img -y

From the RHEL6: Virtualization Host Configuration and Guest Installation Guide

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

List all virtual machines:

[ ~]# virsh list --all
Id Name                        State
----------------------------------------------------
- openstack-compute           shut off
- openstack-controller        shut off
- openstack-nethost           shut off
- openstack-TEMPLATE          shut off
- rhel6x-cloud                shut off

List network configuration:

[ ~]# virsh net-list
Name              State   Autostart Persistent
----------------------------------------------------------
default           active yes        yes

Define a network:

If no "default" (NAT) network is configured:

virsh net-define /usr/share/libvirt/networks/default.xml

List defined virtual networks:

# virsh net-list
Name              State   Autostart Persistent

----------------------------------------------------------

default           active yes        yes

Show specific network information:

[ ~]# virsh net-info default
Name         default
UUID         99ff5b5f-a04e-498e-ab06-dc1cdd8d544c
Active:      yes
Persistent: yes
Autostart:   yes
Bridge:      virbr0

Edit a network:

To edit network configuration (using default as an example):

virsh net-edit default

Network “default” standard definition looks like this:

<network>
 <name>default</name>
 <bridge name="virbr0" />
 <forward/>
 <ip address="192.168.122.1" netmask="255.255.255.0">
<dhcp>
  <range start="192.168.122.2" end="192.168.122.254" />
</dhcp>
 </ip>
</network>

The Network can be modified by changing IP scheme and many other things.

One trick is to assign IP address based on MAC, and also adjust the pool size or range for dhcp leases:

<network>
 <name>default</name>
 <uuid>99ff5b5f-a04e-498e-ab06-dc1cdd8d544c</uuid>
 <forward mode='nat'/>
 <bridge name='virbr0' stp='on' delay='0' />
 <mac address='52:54:00:40:bd:27'/>
 <ip address='192.168.122.1' netmask='255.255.255.0'>
<dhcp>
  <range start='192.168.122.2' end='192.168.122.9' />
  <host mac='52:54:00:00:00:01' name='openstack-controller' ip='192.168.122.101' />
  <host mac='52:54:00:00:00:02' name='openstack-nethost' ip='192.168.122.102' />
  <host mac='52:54:00:00:00:03' name='openstack-compute' ip='192.168.122.103' />
  <host mac='52:54:00:00:00:04' name='openstack-controller2' ip='192.168.122.104' />
</dhcp>
 </ip>
</network>

Start a network if it’s not running:

virsh net-start default

Stop a network:

virsh net-destroy default

Set a network to auto start:

virsh net-autostart default

Edit a working network definition:

Edit a current running/working network without need to hard restart

[ ~]# virsh net-update default add-last ip-dhcp-host \
>    --xml "<host mac='52:54:00:00:00:12' name='server2' ip='192.168.122.102' />" \
>    --live --config
Updated network default persistent config and live state

Create a Virtual Image:

Create a virtual image that will boot using an installation image

Example A:

virt-install   --name=openstack-controller \
       --ram=2000 \
       --file=/var/lib/libvirt/images/openstack-controller.img \
       --cdrom=/var/lib/libvirt/images/rhel-server-6.5-x86_64-dvd.iso \
       --noautoconsole \
       --vnc \
       --file-size 30 \
       --os-type linux \
       --os-variant rhel6 \
       --network network:default,mac=52:54:00:00:00:01
`

Example B:
```bash
virt-install --name rhel6x-cloud \
--ram 1000 \
--cdrom /var/lib/libvirt/images/rhel-server-6.5-x86_64-dvd.iso \
--noautoconsole \
--vnc \
--os-variant rhel6 \
--network network:default,mac=52:55:00:00:00:01 \
--disk path=/var/lib/libvirt/images/rhel6x-cloud.qcow2,format=qcow2,size=10

Example C:

qemu-img create  -f qcow2 -o size=20G,preallocation=metadata rhel6-disk.qcow2

virt-install --name rhel6-guest \
       --ram=2048 \
       --vcpus=2 \
       --cpu host \
       --hvm \
       --cdrom /var/lib/libvirt/isos/rhel-server-6.6-x86_64-dvd.iso \
       --noautoconsole \
       --graphics vnc \
       --os-variant rhel6 \
       --description "RHEL6 KVM guest 1" \
       --network networkefault \
       --disk path=/var/lib/libvirt/images/rhel6-disk.qcow2,format=qcow2,bus=virtio

Example D:

virt-install --name rhnsat56 \
--vcpus 2 \
--ram 4096 \
--cdrom /var/lib/libvirt/images/rhel-server-6.5-x86_64-dvd.iso \
--noautoconsole \
--os-variant rhel6 \
--network network:default,mac=52:54:00:00:00:04 \
--disk path=/home/VirtualImages/rhnsat56-root.qcow2,format=qcow2,size=20 \
--disk path=/home/VirtualImages/rhnsat56-satellite.qcow2,format=qcow2,size=80 \
--disk path=/home/VirtualImages/rhnsat56-cache.qcow2,format=qcow2,size=10 \
--disk path=/home/VirtualImages/rhnsat56-pgsql.qcow2,format=qcow2,size=20

Example E:

virt-install --name lxc-host-1 \
--vcpus 2 \
--ram 2048 \
--cdrom /var/lib/libvirt/images/rhel-server-6.4-x86_64-dvd.iso \
--noautoconsole \
--os-variant rhel6 \
--network network:default,mac=52:54:00:00:00:04 \
--disk path=/home/VirtualMachines/lxc-host-1.qcow2,format=qcow2,size=20

Prepare VM for cloning:

After the installation is finish and the VM is powered off, if cloning will be needed then it is recommended that the virtual machine is prepared for cloning. For this use virt-sysprep, which resets or unconfigures a virtual machine so that clones can be made from it.

Virt-sysprep is also useful when images will be used on PaaS systems like openstack.

virt-sysprep -d openstack-controller

Another common thing to do for preparing a VM is to configure the ifcfg-eth{x}file to “ONBOOT=yes” so the networking auto starts upon first boot.

virt-edit -d rhel64-cloud /etc/sysconfig/network-scripts/ifcfg-eth0 -e 's/^ONBOOT=.*/ONBOOT="yes"/'
By @Jean Figarella in
Tags :