[PATCH 1/4] Add an example of DeviceFactory usage.
David Lehman
dlehman at redhat.com
Tue Jul 30 16:47:25 UTC 2013
Also add examples/common.py which contains convenience functions for
use in examples.
---
examples/common.py | 43 +++++++++++++++++++++++++++++++++++++++
examples/factory.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++++
examples/list_devices.py | 24 +++++-----------------
3 files changed, 101 insertions(+), 19 deletions(-)
create mode 100644 examples/common.py
create mode 100644 examples/factory.py
diff --git a/examples/common.py b/examples/common.py
new file mode 100644
index 0000000..467df98
--- /dev/null
+++ b/examples/common.py
@@ -0,0 +1,43 @@
+import logging
+
+def set_up_logging():
+ """ Configure the blivet logger to use /tmp/blivet.log as its log file. """
+ blivet_log = logging.getLogger("blivet")
+ blivet_log.setLevel(logging.DEBUG)
+ program_log = logging.getLogger("program")
+ program_log.setLevel(logging.DEBUG)
+ handler = logging.FileHandler("/tmp/blivet.log")
+ handler.setLevel(logging.DEBUG)
+ formatter = logging.Formatter("%(asctime)s,%(msecs)03d %(levelname)s %(name)s: %(message)s")
+ handler.setFormatter(formatter)
+ blivet_log.addHandler(handler)
+ program_log.addHandler(handler)
+
+def print_devices(b):
+ for device in sorted(b.devices, key=lambda d: len(d.ancestors)):
+ print device # this is a blivet.devices.StorageDevice instance
+
+ print
+
+def create_sparse_file(b, name, size):
+ """ Create a sparse file for use as a disk image. """
+ import blivet
+ import tempfile
+
+ (fd, path) = tempfile.mkstemp(prefix="blivet.", suffix="-image-%s" % name)
+
+ file_device = blivet.devices.SparseFileDevice(path, size=size)
+ file_device.create()
+ return path
+
+def tear_down_disk_images(b):
+ """ Tear down any disk image stacks. """
+ b.devicetree.teardownAll()
+ for (name, path) in b.config.diskImages.items():
+ dm_device = b.devicetree.getDeviceByName(name)
+ if not dm_device:
+ continue
+
+ dm_device.deactivate()
+ loop_device = dm_device.parents[0]
+ loop_device.teardown()
diff --git a/examples/factory.py b/examples/factory.py
new file mode 100644
index 0000000..7e6391c
--- /dev/null
+++ b/examples/factory.py
@@ -0,0 +1,53 @@
+import logging
+import sys
+import os
+
+from common import set_up_logging
+from common import create_sparse_file
+from common import tear_down_disk_images
+from common import print_devices
+
+# doing this before importing blivet gets the logging from format class
+# registrations and other stuff triggered by the import
+set_up_logging()
+blivet_log = logging.getLogger("blivet")
+blivet_log.info(sys.argv[0])
+
+import blivet
+
+b = blivet.Blivet() # create an instance of Blivet (don't add system devices)
+
+# create two disk image files on which to create new devices
+disk1_file = create_sparse_file(b, "disk1", 100000)
+b.config.diskImages["disk1"] = disk1_file
+disk2_file = create_sparse_file(b, "disk2", 100000)
+b.config.diskImages["disk2"] = disk2_file
+
+b.reset()
+
+try:
+ disk1 = b.devicetree.getDeviceByName("disk1")
+ disk2 = b.devicetree.getDeviceByName("disk2")
+ disk1.format = blivet.formats.getFormat("disklabel", device=disk1.path)
+ disk2.format = blivet.formats.getFormat("disklabel", device=disk2.path)
+
+ # create an lv named data in a vg named testvg
+ device = b.factoryDevice(blivet.devicefactory.DEVICE_TYPE_LVM, 50000,
+ disks=[disk1, disk2],
+ fstype="xfs", mountpoint="/data")
+ print_devices(b)
+
+ # change testvg to have an md RAID1 pv instead of partition pvs
+ device = b.factoryDevice(blivet.devicefactory.DEVICE_TYPE_LVM, 50000,
+ disks=[disk1, disk2],
+ fstype="xfs", mountpoint="/data",
+ container_raid_level="raid1",
+ device=device)
+ print_devices(b)
+
+ b.devicetree.processActions()
+ print_devices(b)
+finally:
+ tear_down_disk_images(b)
+ os.unlink(disk1_file)
+ os.unlink(disk2_file)
diff --git a/examples/list_devices.py b/examples/list_devices.py
index 8539ea1..7310e7e 100644
--- a/examples/list_devices.py
+++ b/examples/list_devices.py
@@ -1,32 +1,18 @@
import logging
import sys
-blivet_log = logging.getLogger("blivet")
-
-def set_up_logging():
- """ Configure the blivet logger to use /tmp/blivet.log as its log file. """
- blivet_log.setLevel(logging.DEBUG)
- program_log = logging.getLogger("program")
- program_log.setLevel(logging.DEBUG)
- handler = logging.FileHandler("/tmp/blivet.log")
- handler.setLevel(logging.DEBUG)
- formatter = logging.Formatter("%(asctime)s,%(msecs)03d %(levelname)s %(name)s: %(message)s")
- handler.setFormatter(formatter)
- blivet_log.addHandler(handler)
- program_log.addHandler(handler)
-
+from common import set_up_logging
+from common import print_devices
# doing this before importing blivet gets the logging from format class
# registrations and other stuff triggered by the import
set_up_logging()
+blivet_log = logging.getLogger("blivet")
+blivet_log.info(sys.argv[0])
import blivet
-blivet_log.info(sys.argv[0])
-
storage = blivet.Blivet() # create an instance of Blivet
storage.reset() # detect system storage configuration
-for device in sorted(storage.devices, key=lambda d: len(d.ancestors)):
- print device # this is a blivet.devices.StorageDevice instance
-
+print_devices(b)
--
1.8.1.4
More information about the anaconda-patches
mailing list