13.3. Adding Storage Devices to Guests
13.3.1. Adding File-based Storage to a Guest
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
ddcommand as shown:
# dd if=/dev/zero of=/var/lib/libvirt/images/FileName.img bs=1M count=4096
- 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.
# dd if=/dev/zero of=/var/lib/libvirt/images/FileName.img bs=1M seek=4096 count=0
- Create the additional storage by writing a <disk> element in a new file. In this example, this file will be known as
<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
<disk type='file' device='disk'> <driver name='qemu' type='raw' cache='none'/> <source file='/var/lib/libvirt/images/FileName.img'/> <target dev='vdb'/> </disk>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 next available PCI slot.
- Attach the CD-ROM as follows:
<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 >
- Add the device defined in
NewStorage.xmlwith your guest (
# virsh attach-device --config Guest1 ~/NewStorage.xml
NoteThis 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 definecommand.If the guest is running, and you want the new device to be added temporarily until the guest is destroyed, omit the
# virsh attach-device Guest1 ~/NewStorage.xml
virshcommand allows for an
attach-diskcommand that can set a limited number of parameters with a simpler syntax and without the need to create an XML file. The
attach-diskcommand is used in a similar manner to the
attach-devicecommand mentioned previously, as shown:
# virsh attach-disk Guest1 /var/lib/libvirt/images/FileName.img vdb --cache noneNote that the
virsh attach-diskcommand also accepts the
- Start the guest machine (if it is currently not running):
# virsh start Guest1
NoteThe 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 driveThe 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.
fdiskfor the new device:
# fdisk /dev/vdb Command (m for help):
nfor a new partition.
- The following appears:
Command action e extended p primary partition (1-4)Type
pfor a primary partition.
- Choose an available partition number. In this example, the first partition is chosen by entering
Partition number (1-4): 1
- Enter the default first cylinder by pressing
First cylinder (1-400, default 1):
- Select the size of the partition. In this example the entire disk is allocated by pressing
Last cylinder or +size or +sizeM or +sizeK (2-400, default 400):
tto configure the partition type.
Command (m for help): t
- Select the partition you created in the previous steps. In this example, the partition number is
1as there was only one partition created and fdisk automatically selected partition 1.
Partition number (1-4): 1
83for a Linux partition.
Hex code (type L to list codes): 83
wto write changes and quit.
Command (m for help): w
- Format the new partition with the
# mke2fs -j /dev/vdb1
- Create a mount directory, and mount the disk on the guest. In this example, the directory is located in myfiles.
# mkdir /myfiles # mount /dev/vdb1 /myfilesThe 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
/dev/vdb1 /myfiles ext3 defaults 0 0