libvirt
This chapter introduces advanced topics about manipulating storage from the perspective of the VM Host Server.
virtlockd
#Edit sourceLocking block devices and disk files prevents concurrent writes to these resources from different VM Guests. It provides protection against starting the same VM Guest twice, or adding the same disk to two different virtual machines. This reduces the risk of a virtual machine's disk image becoming corrupted because of a wrong configuration.
The locking is controlled by a daemon called
virtlockd
. Since it operates
independently from the libvirtd
daemon, locks endure a crash or a
restart of libvirtd
. Locks even persist during an update of the
virtlockd
itself, since it can
re-execute itself. This ensures that VM Guests do
not need to be restarted upon a
virtlockd
update.
virtlockd
is supported for
KVM, QEMU, and Xen.
Locking virtual disks is not enabled by default on openSUSE Leap. To enable and automatically start it upon rebooting, perform the following steps:
Edit /etc/libvirt/qemu.conf
and set
lock_manager = "lockd"
Start the virtlockd
daemon
with the following command:
>
sudo
systemctl start virtlockd
Restart the libvirtd
daemon with:
>
sudo
systemctl restart libvirtd
Make sure virtlockd
is
automatically started when booting the system:
>
sudo
systemctl enable virtlockd
By default virtlockd
is
configured to automatically lock all disks configured for your
VM Guests. The default setting uses a “direct” lockspace,
where the locks are acquired against the actual file paths associated
with the VM Guest <disk> devices. For example,
flock(2)
is called directly on
/var/lib/libvirt/images/my-server/disk0.raw
when
the VM Guest contains the following <disk> device:
<disk type='file' device='disk'> <driver name='qemu' type='raw'/> <source file='/var/lib/libvirt/images/my-server/disk0.raw'/> <target dev='vda' bus='virtio'/> </disk>
The virtlockd
configuration can
be changed by editing the file
/etc/libvirt/qemu-lockd.conf
. It also contains
detailed comments with further information. Make sure to activate
configuration changes by reloading
virtlockd
:
>
sudo
systemctl reload virtlockd
When wanting to lock virtual disks placed on LVM or iSCSI volumes shared by several hosts, locking needs to be done by UUID rather than by path (which is used by default). Furthermore, the lockspace directory needs to be placed on a shared file system accessible by all hosts sharing the volume. Set the following options for LVM and/or iSCSI:
lvm_lockspace_dir = "/MY_LOCKSPACE_DIRECTORY" iscsi_lockspace_dir = "/MY_LOCKSPACE_DIRECTORY"
Sometimes you need to change—extend or shrink—the size of the
block device used by your guest system. For example, when the disk space
originally allocated is no longer enough, it is time to increase its
size. If the guest disk resides on a logical volume,
you can resize it while the guest system is running. This is a big
advantage over an offline disk resizing (see the
virt-resize
command from the
Section 18.3, “Guestfs tools” package) as the service provided by
the guest is not interrupted by the resizing process. To resize a
VM Guest disk, follow these steps:
Inside the guest system, check the current size of the disk (for
example /dev/vda
).
#
fdisk -l /dev/vda
Disk /dev/sda: 160.0 GB, 160041885696 bytes, 312581808 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
On the host, resize the logical volume holding the
/dev/vda
disk of the guest to the required size,
for example 200 GB.
#
lvresize -L 200G /dev/mapper/vg00-home
Extending logical volume home to 200 GiB
Logical volume home successfully resized
On the host, resize the block device related to the disk
/dev/mapper/vg00-home
of the guest. You can find
the DOMAIN_ID with virsh
list
.
#
virsh blockresize --path /dev/vg00/home --size 200G DOMAIN_ID
Block device '/dev/vg00/home' is resized
Check that the new disk size is accepted by the guest.
#
fdisk -l /dev/vda
Disk /dev/sda: 200.0 GB, 200052357120 bytes, 390727260 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
libvirt
#Edit source
RADOS Block Devices (RBD) store data in a Ceph cluster. They allow
snapshotting, replication and data consistency. You can use an RBD from
your libvirt
-managed VM Guests similarly to how you use other block
devices.