9.2. Adding storage devices to guests
- local hard drive partitions,
- logical volumes,
- Fibre Channel or iSCSI directly connected to the host.
- File containers residing in a file system on the host.
- NFS file systems mounted directly by the virtual machine.
- iSCSI storage directly accessed by the guest.
- Cluster File Systems (GFS).
File-based storage or file-based containers are files on the hosts file system which act as virtualized hard drives for virtual machines. To add a file-based container perform the following steps:
- Create an empty container file or using an existing file container (such as an ISO file).
- Create a sparse file using the
ddcommand. Sparse files are not recommended due to data integrity and performance issues. Sparse files are created much faster and can used for testing but should not be used in production environments.dd if=/dev/zero of=/var/lib/libvirt/images/FileName.img bs=1M seek=4096 count=0
# dd if=/dev/zero of=/var/lib/libvirt/images/FileName.img bs=1M seek=4096 count=0Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Non-sparse, pre-allocated files are recommended for file-based storage images. Create a non-sparse file, execute:
dd if=/dev/zero of=/var/lib/libvirt/images/FileName.img bs=1M count=4096
# dd if=/dev/zero of=/var/lib/libvirt/images/FileName.img bs=1M count=4096Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Both commands create a 4GB file which can be used as additional storage for a virtual machine. - Dump the configuration for the guest. In this example the guest is called Guest1 and the file is saved in the users home directory.
virsh dumpxml Guest1 > ~/Guest1.xml
# virsh dumpxml Guest1 > ~/Guest1.xmlCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Open the configuration file (Guest1.xml in this example) in a text editor. Find the
<disk>elements, these elements describe storage devices. The following is an example disk element:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Add the additional storage by duplicating or writing a new
<disk>element. Ensure you specify a device name for the virtual block device attributes. These attributes must be unique for each guest configuration file. The following example is a configuration file section which contains an additional file-based storage container namedFileName.img.Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Restart the guest from the updated configuration file.
virsh create Guest1.xml
# virsh create Guest1.xmlCopy to Clipboard Copied! Toggle word wrap Toggle overflow - The following steps are Linux guest specific. Other operating systems handle new storage devices in different ways. For other systems, see your operating system's documentation.The guest now uses the file
FileName.imgas the device called/dev/sdb. This device requires formatting from the guest. On the guest, partition the device into one primary partition for the entire device then format the device.- Press
nfor a new partition.fdisk /dev/sdb
# fdisk /dev/sdb Command (m for help):Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Press
pfor a primary partition.Command action e extended p primary partition (1-4)
Command action e extended p primary partition (1-4)Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Choose an available partition number. In this example the first partition is chosen by entering
1.Partition number (1-4): 1
Partition number (1-4): 1Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Enter the default first cylinder by pressing
Enter.First cylinder (1-400, default 1):
First cylinder (1-400, default 1):Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Select the size of the partition. In this example the entire disk is allocated by pressing
Enter.Last cylinder or +size or +sizeM or +sizeK (2-400, default 400):
Last cylinder or +size or +sizeM or +sizeK (2-400, default 400):Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Set the type of partition by pressing
t.Command (m for help): t
Command (m for help): tCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Choose the partition you created in the previous steps. In this example, the partition number is
1.Partition number (1-4): 1
Partition number (1-4): 1Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Enter
83for a linux partition.Hex code (type L to list codes): 83
Hex code (type L to list codes): 83Copy to Clipboard Copied! Toggle word wrap Toggle overflow - write changes to disk and quit.
Command (m for help): w Command (m for help): q
Command (m for help): w Command (m for help): qCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Format the new partition with the
ext3file system.mke2fs -j /dev/sdb1
# mke2fs -j /dev/sdb1Copy to Clipboard Copied! Toggle word wrap Toggle overflow
- Mount the disk on the guest.
mount /dev/sdb1 /myfiles
# mount /dev/sdb1 /myfilesCopy to Clipboard Copied! Toggle word wrap Toggle overflow
System administrators use additional hard drives for to provide more storage space or to separate system data from user data. This procedure, Procedure 9.1, “Adding physical block devices to virtual machines”, describes how to add a hard drive on the host to a guest.
Warning
fstab file, the initrd file or used by the kernel command line. If less privileged users, especially virtual machines, have write access to whole partitions or LVM volumes the host system could be compromised.
/dev/sdb). Virtual machines with access to block devices may be able to access other block devices on the system or modify volume labels which can be used to compromise the host system. Use partitions (for example, /dev/sdb1) or LVM volumes to prevent this issue.
Procedure 9.1. Adding physical block devices to virtual machines
- Physically attach the hard disk device to the host. Configure the host if the drive is not accessible by default.
- Configure the device with
multipathand persistence on the host if required. - Use the
virsh attachcommand. Replace: myguest with your guest's name,/dev/sdb1with the device to add, and sdc with the location for the device on the guest. The sdc must be an unused device name. Use the sd* notation for Windows guests as well, the guest will recognize the device correctly.Append the--type cdromparameter to the command for CD-ROM or DVD devices.Append the--type floppyparameter to the command for floppy devices.virsh attach-disk myguest
# virsh attach-disk myguest /dev/sdb1 sdc --driver tap --mode readonlyCopy to Clipboard Copied! Toggle word wrap Toggle overflow - The guest now has a new hard disk device called
/dev/sdbon Linux orD: drive, or similar, on Windows. This device may require formatting.