Product SiteDocumentation Site

27.3. Adding storage devices to guests

This section covers adding storage devices to a virtualized guest. Additional storage can only be added after guests are created.

27.3.1. Adding file based storage to a guest

File-based storage or file-based containers are files on the hosts file system which act as virtualized hard drives for virtualized guests. To add a file-based container perform the following steps:
  1. Create an empty container file or using an existing file container (such as an ISO file).
    1. Create a sparse file using the dd command. 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
      
    2. 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
      
    Both commands create a 400MB file which can be used as additional storage for a virtualized guest.
  2. 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
    
  3. 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:
    <disk type='file' device='disk'>
         <driver name='virtio' cache='none'/>
        <source file='/var/lib/libvirt/images/Guest1.img'/>
        <target dev='sda'/>
    </disk>
    
  4. 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 named FileName.img.
    <disk type='file' device='disk'>
        <driver name='virtio' cache='none'/>
        <source file='/var/lib/libvirt/images/Guest1.img'/>
        <target dev='sda'/>
    </disk>
    <disk type='file' device='disk'>
         <driver name='virtio' cache='none'/>
        <source file='/var/lib/libvirt/images/FileName.img'/>
        <target dev='sdb'/>
    </disk>
    
  5. Restart the guest from the updated configuration file.
    # virsh create Guest1.xml
    
  6. 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
    The guest now uses the file FileName.img as 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.
    1. Press n for a new partition.
      # fdisk /dev/sdb
      Command (m for help):
      
    2. Press p for a primary partition.
      Command action
      e   extended
      p   primary partition (1-4)
      
    3. Choose an available partition number. In this example the first partition is chosen by entering 1.
      Partition number (1-4): 1
    4. Enter the default first cylinder by pressing Enter.
      First cylinder (1-400, default 1):
      
    5. 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):
      
    6. Set the type of partition by pressing t.
      Command (m for help): t
    7. Choose the partition you created in the previous steps. In this example, the partition number is 1.
      Partition number (1-4): 1
    8. Enter 83 for a linux partition.
      Hex code (type L to list codes): 83
    9. write changes to disk and quit.
      Command (m for help): w 
      Command (m for help): q
    10. Format the new partition with the ext3 file system.
      # mke2fs -j /dev/sdb1
  7. Mount the disk on the guest.
    # mount /dev/sdb1 /myfiles
The guest now has an additional virtualized file-based storage device.