Adding an extra disk to a KVM VM
One of my weakest topics is linux disk partitioning, volumes, and logical volumes. To practice for the RHCE exam I want to go through several different scenarios with storage in my lab environment.
To facilitate this learning a separate disk can be created on the hyper-visor and attached to a running VM like so:
cd /var/lib/libvirt/filesystems/ qemu-img create -f raw ex-s-vdab-5G 5G virsh attach-disk ex-s /var/lib/libvirt/filesystems/ex-s-vdab-5G vdab --cache none
In the above scenario I have created a 5 gigabyte raw disk with qemu-img. I store the disk in the /var/lib/libvirt/filesystems/ path which is accessible by the qemu user.
The image is attached to the running vm with the attach-disk command:
attach-disk domain source target [--driver driver] [--subdriver subdriver] [--cache cache] [--type type] [--mode mode] [--config] [--sourcetype soucetype] [--serial serial] [--shareable] [--rawio] [--address address] [--multifunction] Attach a new disk device to the domain. source is path for the files and devices. target controls the bus or device under which the disk is exposed to the guest OS . It indicates the "logical" device name. driver can be file, tap or phy for the Xen hypervisor depending on the kind of access; or qemu for the QEMU emulator. Further details to the driver can be passed using subdriver. For Xen subdriver can be aio, while for QEMU subdriver should match the format of the disk source, such as raw or qcow2. Hypervisor default will be used if subdriver is not specified. However, the default may not be correct, esp. for QEMU as for security reasons it is configured not to detect disk formats. type can indicate lun, cdrom or floppy as alternative to the disk default, although this use only replaces the media within the existing virtual cdrom or floppy device; consider using update-device for this usage instead. mode can specify the two specific mode readonly or shareable. --config indicates the changes will affect the next boot of the domain, for compatibility purposes, --persistent is alias of --config. sourcetype can indicate the type of source (block|file) cache can be one of "default", "none", "writethrough", "writeback", "directsync" or "unsafe". serial is the serial of disk device. shareable indicates the disk device is shareable between domains. rawio indicates the disk needs rawio capability. address is the address of disk device in the form of pci:domain.bus.slot.function, scsi:controller.bus.unit or ide:controller.bus.unit. multifunction indicates specified pci address is a multifunction pci device address.
The result of the above actions will be a disk named vdab on the running VM named ex-s. From the perspective of the VM the disk looks like this:
[root@ex-s ~]# fdisk -l Disk /dev/vda: 16.1 GB, 16106127360 bytes, 31457280 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 Disk label type: dos Disk identifier: 0x0009082c Device Boot Start End Blocks Id System /dev/vda1 * 2048 1026047 512000 83 Linux /dev/vda2 1026048 31457279 15215616 8e Linux LVM Disk /dev/mapper/centos-swap: 1610 MB, 1610612736 bytes, 3145728 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 Disk /dev/mapper/centos-root: 14.0 GB, 13967032320 bytes, 27279360 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 Disk /dev/vdb: 5368 MB, 5368709120 bytes, 10485760 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