HomeGeneral › Using compression to supercharge your Linux virtual image with btrfs and zram

Using compression to supercharge your Linux virtual image with btrfs and zram

As the size of Linux distributions grow along with the resource demands of modern software, virtual images have grown in size both with respect to memory and disk space. Here are couple of ways to save memory and disk space when running a modern Linux Distribution such as Oracle Linux 6.3 (also applies to other similar Linux distros such as RHEL 6.3 or CentOS 6.3). I am using VirtualBox for this image.

1. First rule that i use is to always do a “minimal” install and allocate only 4GB to the first disk.
2. I enabled the public yum repository under /etc/yum.repo.d and chose the UEK repository.
3. Ran “yum update” to ensure UEK kernel is updated.

Now the good stuff…

Transparent filesystem compression with btrfs

  • Create/add a second disk of dynamic size of 20GB
  • Install the btrfs-progs
    $ yum install btrfs-progs
  • fdisk and then format the 2nd disk as btrfs. Yes Oracle btrfs is supported in Oracle Linux 6.3.
    $ mkfs.btrfs /dev/sdb1
  • And this is the most important step. Mount the btrfs filesystem with compress=zlib. Note you can also use other compression algorithms.
    $ mount -t btrfs /dev/sdb1 /app -o compress=zlib
  • Add a permanent entry in /etc/fstab so that this disk is mounted on reboot
    $ echo "/dev/sdb1 /app btrfs compress=zlib" >> /etc/fstab

Supercharge the swap space with zram

  • The UEK kernel already has zram compiled in so you just need to enable it
  • Note the amount of memory/space that you allocate to the /dev/zramX will depend on your own workload and memory allocated to the VM. In this case i have 4GB allocated to the VM and i giving half of it to zram. Modify this simple sample script to your liking. It is a good idea to add it to /etc/rc.local. I would remove the “echo” etc. If you google, you’ll find much more intelligent versions of this script that automatically allocate the sizes.
    #!/bin/sh
    SIZE=2147483648
    DEVICE=zram0
    echo "============== BEFORE ==============="
    free
    echo "============== AFTER ==============="
    echo $SIZE > /sys/block/zram0/disksize
    mkswap /dev/$DEVICE
    swapon -p 100 /dev/$DEVICE
    free

And there you have it. Two simple ways to minimize disk space and maximize your RAM. To give you an idea, the virtual image that i use to run with 8GB of RAM allocated to the VM, now flies with 6GB and occupies 60% less disk space. Obviously your mileage will wary but these two features have done wonders for my image 😉

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>