libvirt volume management.

On libvirt default storage pool is created called default, default pool will defined to use use location /var/lib/libvirt/images for file based volumes.

when ever i create a kvm vm it will create a image in .qcow2 default disk storage for the newly created vm, in other words it is called as volume.

Each vm created will have a voulme, volumes can be created before hand and volume destination can be provided for the virt-install command line.

Lets check the storage pools on the system.

virsh pool-list
 Name                 State      Autostart 
-------------------------------------------
 cipher               active     yes       
 default              active     yes       
 store                active     yes 

from the above we have three storage pools under which volumes or .qcow2 disk allocations can be done to facilitate for vm installation.

Getting more information on pools.

virsh pool-info default
Name:           default
UUID:           5a67026a-5879-4294-9595-297d79ae8d23
State:          running
Persistent:     yes
Autostart:      yes
Capacity:       78.24 GiB
Allocation:     42.74 GiB
Available:      35.51 GiB

virsh pool-info store
Name:           store
UUID:           f51da59f-59e5-45d7-9f13-138a067ddc49
State:          running
Persistent:     yes
Autostart:      yes
Capacity:       596.17 GiB
Allocation:     50.00 GiB
Available:      546.17 GiB


Checking the volumes under the storage pool default.

virsh vol-list default 
 Name                 Path                                    
------------------------------------------------------------------------------
 centos-VAGRANTSLASH-7_vagrant_box_image_1802.01.img /var/lib/libvirt/images/centos-VAGRANTSLASH-7_vagrant_box_image_1802.01.img
 centosext.qcow2      /var/lib/libvirt/images/centosext.qcow2 
 centosext1.qcow2     /var/lib/libvirt/images/centosext1.qcow2
 centosext2.qcow2     /var/lib/libvirt/images/centosext2.qcow2
 centosext3.qcow2     /var/lib/libvirt/images/centosext3.qcow2
 centosext4.qcow2     /var/lib/libvirt/images/centosext4.qcow2
 centosext5.qcow2     /var/lib/libvirt/images/centosext5.qcow2
 chef-repo_node1.img  /var/lib/libvirt/images/chef-repo_node1.img
 chef-repo_node2.img  /var/lib/libvirt/images/chef-repo_node2.img
 chef-repo_node3.img  /var/lib/libvirt/images/chef-repo_node3.img
 chefnode1.qcow2      /var/lib/libvirt/images/chefnode1.qcow2 
 chefnode2.qcow2      /var/lib/libvirt/images/chefnode2.qcow2 
 chefnode3.qcow2      /var/lib/libvirt/images/chefnode3.qcow2 
 chefnode4.qcow2      /var/lib/libvirt/images/chefnode4.qcow2 
 cipher_default.img   /var/lib/libvirt/images/cipher_default.img
 haproxy_ha.img       /var/lib/libvirt/images/haproxy_ha.img  
 haproxy_one.img      /var/lib/libvirt/images/haproxy_one.img 
 haproxy_two.img      /var/lib/libvirt/images/haproxy_two.img 

volumes can be in .qcow2 or .img format, img was older default for volumes creation.
.qcow2 has added benefits over .img format and preferred for openstack deployments.

Voulmes can be in .qcow2 , .img or it can be lvm or physical partiton itlself.

Checking the volume information for storage pool “store”

virsh vol-list store
 Name                 Path                                    
------------------------------------------------------------------------------
 linux1               /dev/store/linux1                       
 linux2               /dev/store/linux2                       

Creating new volumes, which can be used for new vm’s as storge disk destinations.

for this we need create a xml file vol-create a volume.
Here is a volume xml file for /var/lib/libvirt/images/centosext.qcow2

virsh vol-dumpxml /var/lib/libvirt/images/centosext.qcow2  
<volume type='file'>
  <name>centosext.qcow2</name>
  <key>/var/lib/libvirt/images/centosext.qcow2</key>
  <source>
  </source>
  <capacity unit='bytes'>107374182400</capacity>
  <allocation unit='bytes'>3099361280</allocation>
  <physical unit='bytes'>107390828544</physical>
  <target>
    <path>/var/lib/libvirt/images/centosext.qcow2</path>
    <format type='qcow2'/>
    <permissions>
      <mode>0600</mode>
      <owner>0</owner>
      <group>0</group>
      <label>unconfined_u:object_r:virt_image_t:s0</label>
    </permissions>
    <timestamps>
      <atime>1522580831.457883233</atime>
      <mtime>1520994404.759347857</mtime>
      <ctime>1521800537.813106612</ctime>
    </timestamps>
    <compat>1.1</compat>
    <features>
      <lazy_refcounts/>
    </features>
  </target>
</volume>

Lets dump the output to alpha.xml

virsh vol-dumpxml /var/lib/libvirt/images/centosext.qcow2  > alpha.xml


cat alpha.xml 
<volume type='file'>
  <name>centosext.qcow2</name>
  <key>/var/lib/libvirt/images/centosext.qcow2</key>
  <source>
  </source>
  <capacity unit='bytes'>107374182400</capacity>
  <allocation unit='bytes'>3099361280</allocation>
  <physical unit='bytes'>107390828544</physical>
  <target>
    <path>/var/lib/libvirt/images/centosext.qcow2</path>
    <format type='qcow2'/>
    <permissions>
      <mode>0600</mode>
      <owner>0</owner>
      <group>0</group>
      <label>unconfined_u:object_r:virt_image_t:s0</label>
    </permissions>
    <timestamps>
      <atime>1522580831.457883233</atime>
      <mtime>1520994404.759347857</mtime>
      <ctime>1521800537.813106612</ctime>
    </timestamps>
    <compat>1.1</compat>
    <features>
      <lazy_refcounts/>
    </features>
  </target>
</volume>

Edit the new alpha.xml file and plug in the desirable voulme name and size.

cat alpha.xml 
<volume type='file'>
  <name>alpha.qcow2</name>
  <key>/var/lib/libvirt/images/alpha.qcow2</key>
  <source>
  </source>
  <capacity unit='bytes'>107374182400</capacity>
  <allocation unit='bytes'>3099361280</allocation>
  <physical unit='bytes'>107390828544</physical>
  <target>
    <path>/var/lib/libvirt/images/alpha.qcow2</path>
    <format type='qcow2'/>
    <permissions>
      <mode>0600</mode>
      <owner>0</owner>
      <group>0</group>
      <label>unconfined_u:object_r:virt_image_t:s0</label>
    </permissions>
    <timestamps>
      <atime>1522580831.457883233</atime>
      <mtime>1520994404.759347857</mtime>
      <ctime>1521800537.813106612</ctime>
    </timestamps>
    <compat>1.1</compat>
    <features>
      <lazy_refcounts/>
    </features>
  </target>
</volume>

Creating a voulme

$
$virsh vol-create  default alpha.xml 
Vol alpha.qcow2 created from alpha.xml


$virsh vol-list default 
 Name                 Path                                    
------------------------------------------------------------------------------
 alpha.qcow2          /var/lib/libvirt/imaes/alpha.qcow2     

Checking the size of newly created volume /var/lib/libvirt/images/alpha.qcow2

$
virsh  vol-info /var/lib/libvirt/images/alpha.qcow2 
Name:           alpha.qcow2
Type:           file
Capacity:       100.00 GiB
Allocation:     196.00 KiB

.qcow2, .img are file based volumes for kvm.

It may be difficult to get a xml file if the server is setup a new , does not have any vms setup on it yet ,in that case we can use vol-create-as to create a volume.

$
virsh vol-create-as default beta.qcow2 2G  
Vol beta.qcow2 created

Getting more info on the volumes.

$
virsh vol-list default 
 Name                 Path                                    
------------------------------------------------------------------------------
 alpha.qcow2          /var/lib/libvirt/images/alpha.qcow2     
 beta.qcow2           /var/lib/libvirt/images/beta.qcow2      

Identifying the the type of storege device and disk allocation size.

$
virsh vol-info /var/lib/libvirt/images/beta.qcow2 
Name:           beta.qcow2
Type:           file
Capacity:       2.00 GiB
Allocation:     2.00 GiB

Data on the volumes can be wiped off , be reused to setup new vms on kvm node.

$

$virsh vol-wipe /var/lib/libvirt/images/alpha.qcow2 
Vol /var/lib/libvirt/images/alpha.qcow2 wiped

Volumes can be deleted with vol-delete command.

$virsh vol-delete /var/lib/libvirt/images/alpha.qcow2 
Vol /var/lib/libvirt/images/alpha.qcow2 deleted

$virsh vol-delete /var/lib/libvirt/images/beta.qcow2 
Vol /var/lib/libvirt/images/beta.qcow2 deleted

Creating volumes on lvm storage pool will be similar.

$virsh vol-create-as  store linux4 25G ; virsh  store vol-create-as linux5 25G 
Vol linux4 created

Deleting the creadted volume

$virsh vol-delete /dev/store/linux4 
Vol /dev/store/linux4 deleted