Questo contenuto non è disponibile nella lingua selezionata.
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 Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
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
ddcommand as shown: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 - 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
# 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
- 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 namedFileName.img:Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Add the device defined in
NewStorage.xmlwith your guest (Guest1):virsh attach-device --config Guest1 ~/NewStorage.xml
# virsh attach-device --config Guest1 ~/NewStorage.xmlCopy to Clipboard Copied! Toggle word wrap Toggle overflow 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 withvirsh definecommand.If the guest is running, and you want the new device to be added temporarily until the guest is destroyed, omit the--configoption:virsh attach-device Guest1 ~/NewStorage.xml
# virsh attach-device Guest1 ~/NewStorage.xmlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Note
Thevirshcommand allows for anattach-diskcommand that can set a limited number of parameters with a simpler syntax and without the need to create an XML file. Theattach-diskcommand is used in a similar manner to theattach-devicecommand mentioned previously, as shown:virsh attach-disk Guest1 /var/lib/libvirt/images/FileName.img vdb --cache none --driver qemu --subdriver raw
# virsh attach-disk Guest1 /var/lib/libvirt/images/FileName.img vdb --cache none --driver qemu --subdriver rawCopy to Clipboard Copied! Toggle word wrap Toggle overflow Note that thevirsh attach-diskcommand also accepts the--configoption. - Start the guest machine (if it is currently not running):
virsh start Guest1
# virsh start Guest1Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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
fdiskfor the new device:fdisk /dev/vdb
# fdisk /dev/vdb Command (m for help):Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Type
nfor a new partition. - The following appears:
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 Typepfor a primary partition. - 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 - Enter
tto configure the partition type.Command (m for help): t
Command (m for help): tCopy to Clipboard Copied! Toggle word wrap Toggle overflow - 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
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 - Enter
wto write changes and quit.Command (m for help): w
Command (m for help): wCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Format the new partition with the
ext3file system.mke2fs -j /dev/vdb1
# mke2fs -j /dev/vdb1Copy to Clipboard Copied! Toggle word wrap Toggle overflow
- 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 /myfiles
# mkdir /myfiles # mount /dev/vdb1 /myfilesCopy to Clipboard Copied! Toggle word wrap Toggle overflow 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/fstabfile:/dev/vdb1 /myfiles ext3 defaults 0 0
/dev/vdb1 /myfiles ext3 defaults 0 0Copy to Clipboard Copied! Toggle word wrap Toggle overflow