13.3. Directory-based Storage Pools
This section covers storing guest virtual machines in a directory on the host physical machine.
Directory-based storage pools can be created with
virt-manager or the virsh command-line tools.
13.3.1. Creating a Directory-based Storage Pool with virt-manager
Create the local directory
Optional: Create a new directory for the storage pool
Create the directory on the host physical machine for the storage pool. This example uses a directory named /guest_images.#
mkdir /guest_imagesSet directory ownership
Change the user and group ownership of the directory. The directory must be owned by the root user.#
chown root:root /guest_imagesSet directory permissions
Change the file permissions of the directory.#
chmod 700 /guest_imagesVerify the changes
Verify the permissions were modified. The output shows a correctly configured empty directory.#
ls -la /guest_imagestotal 8 drwx------. 2 root root 4096 May 28 13:57 . dr-xr-xr-x. 26 root root 4096 May 28 13:57 ..
Configure SELinux file contexts
Configure the correct SELinux context for the new directory. Note that the name of the pool and the directory do not have to match. However, when you shut down the guest virtual machine, libvirt has to set the context back to a default value. The context of the directory determines what this default value is. It is worth explicitly labeling the directory virt_image_t, so that when the guest virtual machine is shutdown, the images get labeled 'virt_image_t' and are thus isolated from other processes running on the host physical machine.#
semanage fcontext -a -t virt_image_t '/guest_images(/.*)?'#restorecon -R /guest_imagesOpen the storage pool settings
- In the
virt-managergraphical interface, select the host physical machine from the main window.Open the Edit menu and select Connection Details - click the Storage tab of the Connection Details window.
Create the new storage pool
Add a new pool (part 1)
Press the + button (the add pool button). The Add a New Storage Pool wizard appears.Choose a for the storage pool. This example uses the name guest_images. Change the todir: Filesystem Directory.Press the button to continue.Add a new pool (part 2)
Change the field. For example, /guest_images.Verify the details and press the button to create the storage pool.
Verify the new storage pool
The new storage pool appears in the storage list on the left after a few seconds. Verify the size is reported as expected, 36.41 GB Free in this example. Verify the field reports the new storage pool as Active.Select the storage pool. In the field, confirm that the check box is checked. This will make sure the storage pool starts whenever thelibvirtdservice starts.The storage pool is now created, close the Connection Details window.
13.3.2. Deleting a Storage Pool Using virt-manager
This procedure demonstrates how to delete a storage pool.
- To avoid any issues with other guest virtual machines using the same pool, it is best to stop the storage pool and release any resources in use by it. To do this, select the storage pool you want to stop and click
at the bottom of the Storage window.
- Delete the storage pool by clicking
. This icon is only enabled if you stop the storage pool first.
13.3.3. Creating a Directory-based Storage Pool with virsh
Create the storage pool definition
Use thevirsh pool-define-ascommand to define a new storage pool. There are two options required for creating directory-based storage pools:- The
nameof the storage pool.This example uses the name guest_images. All furthervirshcommands used in this example use this name. - The
pathto a file system directory for storing guest image files. If this directory does not exist,virshwill create it.This example uses the /guest_images directory.
#
virsh pool-define-as guest_images dir - - - - "/guest_images" Pool guest_images definedVerify the storage pool is listed
Verify the storage pool object is created correctly and the state reports it asinactive.#
virsh pool-list --allName State Autostart ----------------------------------------- default active yes guest_images inactive noCreate the local directory
Use thevirsh pool-buildcommand to build the directory-based storage pool for the directory guest_images (for example), as shown:#
virsh pool-build guest_imagesPool guest_images built #ls -la /guest_imagestotal 8 drwx------. 2 root root 4096 May 30 02:44 . dr-xr-xr-x. 26 root root 4096 May 30 02:44 .. #virsh pool-list --allName State Autostart ----------------------------------------- default active yes guest_images inactive noStart the storage pool
Use the virsh commandpool-startto enable a directory storage pool, thereby allowing volumes of the pool to be used as guest disk images.#
virsh pool-start guest_imagesPool guest_images started #virsh pool-list --allName State Autostart ----------------------------------------- default active yes guest_images active noTurn on autostart
Turn onautostartfor the storage pool. Autostart configures thelibvirtdservice to start the storage pool when the service starts.#
virsh pool-autostart guest_imagesPool guest_images marked as autostarted #virsh pool-list --allName State Autostart ----------------------------------------- default active yes guest_images active yesVerify the storage pool configuration
Verify the storage pool was created correctly, the size is reported correctly, and the state is reported asrunning. If you want the pool to be accessible even if the guest virtual machine is not running, make sure thatPersistentis reported asyes. If you want the pool to start automatically when the service starts, make sure thatAutostartis reported asyes.#
virsh pool-info guest_imagesName: guest_images UUID: 779081bf-7a82-107b-2874-a19a9c51d24c State: running Persistent: yes Autostart: yes Capacity: 49.22 GB Allocation: 12.80 GB Available: 36.41 GB #ls -la /guest_imagestotal 8 drwx------. 2 root root 4096 May 30 02:44 . dr-xr-xr-x. 26 root root 4096 May 30 02:44 .. #
A directory-based storage pool is now available.
13.3.4. Deleting a Storage Pool Using virsh
The following demonstrates how to delete a storage pool using virsh:
- To avoid any issues with other guest virtual machines using the same pool, it is best to stop the storage pool and release any resources in use by it.
#
virsh pool-destroy guest_images_disk - Optionally, if you want to remove the directory where the storage pool resides use the following command:
#
virsh pool-delete guest_images_disk - Remove the storage pool's definition
#
virsh pool-undefine guest_images_disk



