Quantcast
Channel: Hyperthese » Debian
Viewing all articles
Browse latest Browse all 3

KVM-ized Debian on LVM

$
0
0

The question of storing virtual machines datas has a lot of answers, but only a few seems to be as flexible as what you’ll find on a plain old physical machine.

First try

For instance, what I looked for was to be able to have a storage pool inside of which I can extend partitions sizes as needed, because I host several services with relatively unpredicted expansion.

Usually when you need to do that, you can use LVM, and hot resize you partitions. However, the virtual world is not so simple, and this can’t be done directly. My first try was to have my standard LVM pool in which I would create the logical volumes I need, and then add them as independent disks.

This appoach works, however the Debian installer requires that you create a partition table, which makes it pretty difficult to use from the host, and moreover is almost impossible to resize.

A few commands

Imagine that the volume storing your virtual hard disk is the LVM logical volume /dev/mapper/myvm-root. First you have to tell the kernel to map the devices from the partition table:

kpartx -a /dev/mapper/myvm-root
ls /dev/mapper/myvm-root*

This shows you that your partitions appeared in the mapper. You can now use them as usual

mount /dev/mapper/myvm-root1 /mnt

But now if you want to resize the hard disk, you have to resize your LVM volume, but also to make the change in your partition table. As nobody has ever heard of size-changing hard disks, I don’t know any tool that does this, so unless I missed an option somewhere, it’s pretty much fucked up for you.

Better solution

In fact, you must create the partitions beforehand, so the installer sees them and can use them without creating a brand new partition table. And you can see the partition directly from the host.

A few other commands

Once the logical volume is created, you can format it the standard way:

mkfs.ext4 /dev/mapper/myvm-root

Then when you launch the Debian installer you’ll see the partition as already existing, and won’t be prompted to create a partition table:

Once your Debian is installed, you can just mount the partition(s):

mount -o ro /dev/mapper/myvm-root /mnt

Or if your VM is down, you can even resize the volume

lvresize -L +1G /dev/mapper/myvm-root
e2fsck -f /dev/mapper/myvm-root
resize2fs /dev/mapper/myvm-root

Then when you reboot your VM it will have got a 1 gigabyte bigger hard disk.

Other Approaches

This is certainly not the only way to achieve the goal of be able to dynamically resize your hard drive. For instance, you could probably manage something with qcow2 disks growing as needed, or maybe create several disks that you aggregate as physical volumes, or who knows.

But the solution I found here is what I found to be the more practical to use, without loosing too much performances. Still, you can’t do hot resize, and this might be a problem for some people, but I don’t see how you could hot-resize virtual disks. I guess it’s a feature to come!

Anyway, if you know a better way to get this done, I’m quite interested in it ;)


Viewing all articles
Browse latest Browse all 3

Trending Articles