Last active
May 1, 2022 23:54
-
-
Save DavesCodeMusings/95d27c689f981714fcb60ffa885a7143 to your computer and use it in GitHub Desktop.
Create a new storage pool with libvirt-python api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # Define a storage pool using the libvirt API. | |
| # virsh pool-destroy ; virsh pool-undefine can be used to remove. | |
| import libvirt | |
| import sys | |
| import uuid | |
| import os | |
| pool_name = "test" | |
| pool_path = os.path.join("/var/lib/libvirt", pool_name) | |
| pool_uuid = uuid.uuid4() | |
| # The directory path must exist prior to creating the pool. | |
| if not os.path.isdir(pool_path): | |
| print("Creating directory", pool_path) | |
| os.mkdir(pool_path, mode=0o755) | |
| print("Connecting to API") | |
| conn = None | |
| try: | |
| conn = libvirt.open("qemu:///system") | |
| except libvirt.libvirtError as e: | |
| print(repr(e), file=sys.stderr) | |
| exit(1) | |
| print("Creating storage pool", pool_name) | |
| poolXML = """<pool type='dir'> | |
| <name>{0}</name> | |
| <uuid>{1}</uuid> | |
| <target> | |
| <path>{2}</path> | |
| </target> | |
| </pool>""".format(pool_name, pool_uuid, pool_path) | |
| pool = conn.storagePoolDefineXML(poolXML, 0) | |
| pool.setAutostart(1) | |
| pool.create() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment