Wednesday, August 22, 2012

Improving the Performance of a VM by Using a Real Disk Partition

I have used zentyal as a gateway server, and it is actually running on a virtual machine. An important component of it is squid, which is supposed to improve the efficiency of the network connection by saving in cache the web pages that have been already visited, and fetching them from the cache when they are re-visited.
This is an operation that makes intesive usage of the disk, and virtual disks (used on virtual machines) are not particularly efficient for it. However, a virtual machine can also use a real partition, so the efficience of squid can be improved by attaching a real partition to the virtual machine and using it for the cache.

Attach a Real Partition to the Virtual Machine

The host where zentyal is installed is a CentOS system, and the disk partitions are managed by LVM. So, first of all we should create a new LVM partition to be attached to the virtual machine:
vgdisplay
lvdisplay
lvcreate vg_data -L 50G -n /dev/vg_data/lv_cache
In LVM, Volume Groups (VG) are the equivalents of disks, and Logical Volumes (LV) are the equivalents of partitions. In this case, we are creating alogical volume with name /dev/vg_data/lv_cache, on the volume group with name vg_data. The size of this LV partition is 50G, and this is enough for the squid cache.
Then we add this partition to the configuration file of the virtual machine /etc/libvirt/qemu/zentyal.xml:
<devices>
  . . . . .
  <disk type='block' device='disk'>
    <driver name='qemu' type='raw' cache='none' io='native'/>
    <source dev='/dev/vg_data/lv_cache'/>
    <target dev='sdb' bus='ide'/>
    <address type='drive' controller='0' bus='0' unit='1'/>
  </disk>
  . . . . .
</devices>
Inside the virtual machine this disk will appear and will be accessed as /dev/sdb.
Now we have to enable it, by reloading (re-defining) the configuration file:
virsh define /etc/libvirt/qemu/zentyal.xml
virsh shutdown zentyal
virsh start zentyal

Using the New Disk Inside the Virtual Machine

Inside the virtual machine (zentyal), we can access the new disk as /dev/sdb. First we have to format and mount it, and then we can change the squidconfiguration to use it.
  • Format the new partition with the reiserfs format (which is a recommended format for the squid cache):
    mkfs.reiserfs -f /dev/sdb
    
  • Append this line to /etc/fstab:
    /dev/sdb  /var/spool/squid  reiserfs  defaults,notail,noatime  1  2
    
  • Then stop squid and mount the new partition:
    service squid stop
    rm -rf /var/spool/squid/*
    mount -a
    chown proxy:proxy /var/spool/squid/
    chmod 750 /var/spool/squid/
    squid -z
    service squid start
    
  • Edit /usr/share/zentyal/stubs/squid/squid.conf.mas and change this line:
    cache_dir ufs /var/spool/squid <% $cacheDirSize %> 16 256
    
    to this one (by adding an a option):
    cache_dir aufs /var/spool/squid <% $cacheDirSize %> 16 256
    
    Then restart:
    service squid restart
    

No comments:

Post a Comment