13.3. Adding Storage Devices to Guests This section covers adding storage devices to a guest. Additional storage can only be added as needed. 13.3.1. Adding File-based Storage to a Guest File-based storage is a collection of files that are stored on the host physical machines file system that act as virtualized hard drives for guests. To add file-based storage, perform the following steps: Procedure 13.1. Adding file-based storage Create a storage file or use an existing file (such as an IMG file). Note that both of the following commands create a 4GB file which can be used as additional storage for a guest: Pre-allocated files are recommended for file-based storage images. Create a pre-allocated file using the following dd command as shown: Alternatively, create a sparse file instead of a pre-allocated file. Sparse files are created much faster and can be used for testing, but are not recommended for production environments due to data integrity and performance issues. Create the additional storage by writing a <disk> element in a new file. In this example, this file will be known as NewStorage.xml . A <disk> element describes the source of the disk, and a device name for the virtual block device. The device name should be unique across all devices in the guest, and identifies the bus on which the guest will find the virtual block device. The following example defines a virtio block device whose source is a file-based storage container named FileName.img : Device names can also start with "hd" or "sd", identifying respectively an IDE and a SCSI disk. The configuration file can also contain an <address> sub-element that specifies the position on the bus for the new device. In the case of virtio block devices, this should be a PCI address. Omitting the <address> sub-element lets libvirt locate and assign the available PCI slot. Attach the CD-ROM as follows: Add the device defined in NewStorage.xml with your guest ( Guest1 ): Note This change will only apply after the guest has been destroyed and restarted. In addition, persistent devices can only be added to a persistent domain, that is a domain whose configuration has been saved with virsh define command. If the guest is running, and you want the new device to be added temporarily until the guest is destroyed, omit the --config option: Note The virsh command allows for an attach-disk command that can set a limited number of parameters with a simpler syntax and without the need to create an XML file. The attach-disk command is used in a similar manner to the attach-device command mentioned previously, as shown: Note that the virsh attach-disk command also accepts the --config option. Start the guest machine (if it is currently not running): Note The following steps are Linux guest specific. Other operating systems handle new storage devices in different ways. For other systems, refer to that operating system's documentation. Partitioning the disk drive The guest now has a hard disk device called /dev/vdb . If required, partition this disk drive and format the partitions. If you do not see the device that you added, then it indicates that there is an issue with the disk hotplug in your guest's operating system. Start fdisk for the new device: Type n for a new partition. The following appears: Type p for a primary partition. Choose an available partition number. In this example, the first partition is chosen by entering 1 . Enter the default first cylinder by pressing Enter . Select the size of the partition. In this example the entire disk is allocated by pressing Enter . Enter t to configure the partition type. Select the partition you created in the steps. In this example, the partition number is 1 as there was only one partition created and fdisk automatically selected partition 1. Enter 83 for a Linux partition. Enter w to write changes and quit. Format the new partition with the ext3 file system. Create a mount directory, and mount the disk on the guest. In this example, the directory is located in myfiles . The guest now has an additional virtualized file-based storage device. Note however, that this storage will not mount persistently across reboot unless defined in the guest's /etc/fstab file:

Commands:
# 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 seek=4096 count=0
<disk type='file' device='disk'> <driver name='qemu' type='raw' cache='none'/> <source file='/var/lib/libvirt/images/FileName.img'/> <target dev='vdb'/> </disk>
<disk type='file' device='cdrom'> <driver name='qemu' type='raw' cache='none'/> <source file='/var/lib/libvirt/images/FileName.img'/> <readonly/> <target dev='hdc'/> </disk >
# virsh attach-device --config Guest1 ~/NewStorage.xml
# virsh attach-device Guest1 ~/NewStorage.xml
# virsh attach-disk Guest1 /var/lib/libvirt/images/FileName.img vdb --cache none --driver qemu --subdriver raw
# virsh start Guest1
# fdisk /dev/vdb Command (m for help):
Command action e extended p primary partition (1-4)
Partition number (1-4): 1
First cylinder (1-400, default 1):
Last cylinder or +size or +sizeM or +sizeK (2-400, default 400):
Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): 83
Command (m for help): w
# mke2fs -j /dev/vdb1
# mkdir /myfiles # mount /dev/vdb1 /myfiles
/dev/vdb1 /myfiles ext3 defaults 0 0