Data Storage virtualization in Linux (Part 2)

In the previous part we talked about how we can create sparse file storage and create file system to mount or share across network. Now we will look into some common disk operations that we can do with this storage like scaling, encryption etc.

6 min read
Data Storage virtualization in Linux (Part 2)

In the previous part we talked about how we can create sparse file storage and create file system to mount or share across network. Now we will look into some common disk operations that we can do with this storage like scaling, encryption etc.

General disk operating

1a) Scaling the size(Increasing)

If a user demands more storage then previously allocated then it can be a headache for you since these type of storage doesn't support disk operation like resizing(specially on-line resizing) and you can't put the storage unmount or offline for little time otherwise you will not consider to be good service provider. But with little trick and manipulation you can even archive that also. For that you have to follow the exact procedure that I am using.
Suppose a user want his space to be increase by 1 Gb (i.e 2Gb total) then first you need to increase the size of the file by 1 Gb. For that we will use the same truncate tool since it work on already created file also and make the file of desired size.

$truncate --size=2GB test.img
# or by using qemu-img
$qemu-img resize test.img +2GB

You will find out that there will be no change in mounted space since the loop device has not detected the increased size. To make it detected to the loop device it have to be first check by e2fsck by

$e2fsck -f test.img

after that since the file system is only made on 1Gb partition it have to be resized but the condition is we don't want our data to to be lost or deleted and also we can't handle the break in service by unmounting it for some seconds even. so we need to do on-line resizing by

$resize2fs test.img

we have specially used ext2 file system since online resizing and file checking only works perfectly for ext2 file system with resize2fs utility.
This will make our file partition ready to use but all this changes are not yet detected by loop device so same operations have to be done on loop device.

$losetup -a |grep mnt
$losetup -c /dev/loop0

with -c argument in losetup will detect the increment in the size but we still need to make the change in file system for loop device

$resize2fs /dev/loop0

Now our new storage size is ready to use.

1b) Decreasing the storage

To decrease the partition size it is not required to decrease the size of file by truncate utility since it may make some half data chunks or bad blocks left and due to those bad blocks you will not able to use the remaining space until you format it again.
So, the best solution can be to just decrease the file system layer on the file by resize2fs command.

$resize2fs test.img 1G

where 1G is the new decreased size.
You not need to be worry about the size shown in metadata since the user will only able to use the formated space. And since the file is a sparse file the remaining space will not take any space in your system.
In looped device we just need to detect the changed size

$losetup -c /dev/loop0

Now your storage of decreased size is ready.

To detach the storage from loop device use ``` $ losetup -d /dev/loop0 ```

2) Backup the data

Features that every storage service provider must have are backup,snapshot and clustering. Data snapshot are seem to be less sensible so we will try not to concentrate on it much for now but will talk about that in later part. But if we talk about backup, what this do is to create a backup file which will save all useful data for future disaster recovery.
So lets make a backup of our storage by using rsync utility.

$rsync -avz test.img test_backup.img

this will create a testbackup.img file with same data blocks. So every time you run this command 'new changes made since last backup' will be get saved in test_backup.img

If you create the backup image of you file system through rsync the backup file will not be a sparse file so it will allocate all of the space at once.

although it looks simple but it is not much efficient solution since once data starts overwriting in some blocks in original storage image it will be exactly get copied in backup image.
So a better solution can be to rsync the mounted path.

$rsync -avz /mnt/ /mnt_backup

To make the backup process automatic you can use lsyncd for live synchronization.
Install lsyncd in RHEL system

$yum install -y lsyncd

edit the configure file lsyncd.conf

$cat /etc/lsyncd.conf 
----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync.
--
settings = {
logfile = "/var/log/lsyncd.log",
statusFile = "/var/log/lsyncd.stat",
statusInterval = 2,
}
sync{
default.rsync,
source="/mnt/",
target="192.168.1.15:/backup/",
rsync={rsh ="/usr/bin/ssh -l root -i /root/.ssh/id_rsa",}
}

First you need to connect to the backup machine by ssh using ssh-keygen.

If you want to automate the backup process you can use fsmonitor npm

$fsmonitor rsync -azP /mnt/ /mnt_backup

Also you can even use rsnapshot for incremental backup.

3) Snapshots of storage data

Snapshot are facility available in different linux utility to save your storage at particuar state.It can be a useful feature for Staas provider because it will help your storage to revert back to any previous state.

Snapshot is different then backup because it doesn't take your storage space until changes are made on the storage.To save space it just copy the file that have been deleted.
For our file storage we will be using qemu-img utility for snapshot. So first create a new snapshot of your storage

$qemu-img snapshot -c backup_snapshot test.img

-c is use for creating new snapshot.To revert back to a particular state

$qemu-img snapshot -a 5 test.img

where 5 is the snapshot id.To see all the available snapshots

$qemu-img -l test.img

To delete a snapshot

$qemu-img snapshot -d 2 /images/sles11sp1.qcow2

4) Securing your virtual storage

The other profit of using file storage is its easy shipping like a container. But with shipping comes the responsibility to secure your storage. So a good solution for secure your virtual data storage is by protection using encryption so that you need password every time to mount that. For encrypting your storage we will use dm-crypt.
Lets try the encryption on fresh file storage.

$truncate encrypted.raw --size=2GB

next setup a LUKS header.

$cryptsetup luksFormat encrypted.raw

Don't try this with already formated partition because it will delete all the previous data inside partition.

this will prompt to enter a fresh password.
To gain access to the device

$cryptsetup open encrypted.raw my_encp.raw

myencp.raw is the name of the file whereas our partition get mapped in /dev/mapper/. Now you can create a file system above it

$mkfs.fstype /dev/mapper/my_encp.raw

Mount the newly created partition anywhere with mount

$mount -t ext2 /dev/mapper/my_encp.raw /mnt/

Once use of the storage is finish you can unmount it.

$ umount /mnt/
$ cryptsetup close my_encp.raw

You can do the same disk scaling and other operations on disk but now you need to make changes to /dev/mapper/my_encp.raw rather then /dev/loop0.

5) Using File storage virtual machine

A greate use of file storage is its use as base storage for virtual machines.Once you decide to use a file storage for a os run on virtual machine other operations like scaling and encryption can also be applied on that.To create a virtual machine instace from our already created storage we will use qemu-kvm utility.

$qemu-kvm -name "my_os" -m 1024 -smp 2 -drive file=test.img,if=virtio,\
index=0,media=disk,format=raw -drive file=ubuntu-14.04.iso,index=1,media=cdrom

This will start a new virtual machine with minimal option selected.here -m define the amount of ram allocate and -smp refer to the no of cores.You can read about more available option in qemu-kvm man page or by using qemu-kvm –help.
To start already created virtual machine.

$qemu-kvm -name "my_os" -m 1024 -smp 2 -drive\ file=/images/sles11/hda,if=virtio,index=0,media=disk,format=raw

Final thoughts

File storage can be a good solution for your environment or personal use but what matter is your idea to make it useful at different level of your cloud solution.
Storage management in case of file storage doesn't end here since there is lot more you can do with it.It can be a powerful as well as flexible solution for storage management but require a tough knowledge to make your job done.

Useful resources

For lsyncd:link
For qemu utility:link
For Dm-crypt encryption:link