tessl install tessl/pypi-python-libmaas@0.6.0Python client library for MAAS 2.0+ with sync/async support, providing machine provisioning, network management, and storage configuration.
Configure Bcache to use fast SSD storage as a cache layer in front of slower HDD backing devices for improved I/O performance.
Bcache allows you to use a fast SSD as a cache in front of slower spinning disks, combining the performance of SSDs with the capacity of HDDs. It consists of:
Retrieve all Bcache cache sets configured on a machine.
# Via Machine object
machine.bcache_cache_setsfrom maas.client import connect
client = connect('http://maas.example.com:5240/MAAS/', apikey='key')
# Get a machine
machine = client.machines.get('abc123')
# List all cache sets
cache_sets = machine.bcache_cache_sets
for cs in cache_sets:
print(f"Cache set {cs.name}: {cs.cache_device}")Create a new Bcache cache set from a block device or partition.
BcacheCacheSets.create(node, cache_device)# Create cache set from SSD block device
nvme0 = machine.block_devices.get_by_name('nvme0n1')
cache_set = await machine.bcache_cache_sets.create(machine, nvme0)
# Create cache set from partition
ssd = machine.block_devices.get_by_name('sdb')
ssd_partition = await ssd.partitions.create(ssd, size=ssd.available_size)
cache_set = await machine.bcache_cache_sets.create(machine, ssd_partition)Remove a cache set from a machine.
cache_set.delete()# Delete cache set
cache_set = machine.bcache_cache_sets[0]
await cache_set.delete()Retrieve all Bcache devices configured on a machine.
# Via Machine object
machine.bcaches# List all Bcache devices
bcaches = machine.bcaches
for bcache in bcaches:
print(f"{bcache.name}: {bcache.cache_mode} - {bcache.size} bytes")
# Access by name
bcache0 = machine.bcaches.by_name['bcache0']Create a new Bcache device combining a cache set with a backing device.
Bcaches.create(
node,
name,
backing_device,
cache_set,
cache_mode,
uuid=None
)from maas.client.enum import CacheMode
# Get cache set and backing device
cache_set = machine.bcache_cache_sets[0]
hdd = machine.block_devices.get_by_name('sdc')
# Create Bcache device with writeback caching
bcache = await machine.bcaches.create(
machine,
name='bcache0',
backing_device=hdd,
cache_set=cache_set,
cache_mode=CacheMode.WRITEBACK
)
# Create Bcache with writethrough caching
bcache_safe = await machine.bcaches.create(
machine,
name='bcache1',
backing_device=hdd,
cache_set=cache_set,
cache_mode=CacheMode.WRITETHROUGH
)
# Create Bcache with partition as backing device
partition = await hdd.partitions.create(hdd, size=hdd.available_size)
bcache_part = await machine.bcaches.create(
machine,
name='bcache2',
backing_device=partition,
cache_set=cache_set,
cache_mode=CacheMode.WRITEBACK
)Modify Bcache device attributes and save changes.
bcache.save()# Update Bcache properties
bcache = machine.bcaches.get_by_name('bcache0')
bcache.cache_mode = CacheMode.WRITETHROUGH
bcache.uuid = 'new-uuid-value'
# Save changes
await bcache.save()Remove a Bcache device from a machine.
bcache.delete()# Delete Bcache device
bcache = machine.bcaches.get_by_name('bcache0')
await bcache.delete()Bcache devices can be formatted and mounted like regular block devices.
# Create Bcache device
bcache = await machine.bcaches.create(
machine,
name='bcache0',
backing_device=hdd,
cache_set=cache_set,
cache_mode=CacheMode.WRITEBACK
)
# Format and mount the virtual device
virtual_device = bcache.virtual_device
await virtual_device.format('xfs')
await virtual_device.mount('/data')Bcache supports three cache modes:
Write operations go to cache first, then to backing device asynchronously.
from maas.client.enum import CacheMode
bcache = await machine.bcaches.create(
machine,
name='bcache0',
backing_device=hdd,
cache_set=cache_set,
cache_mode=CacheMode.WRITEBACK
)Write operations go to both cache and backing device simultaneously.
bcache = await machine.bcaches.create(
machine,
name='bcache0',
backing_device=hdd,
cache_set=cache_set,
cache_mode=CacheMode.WRITETHROUGH
)Write operations bypass cache and go directly to backing device.
bcache = await machine.bcaches.create(
machine,
name='bcache0',
backing_device=hdd,
cache_set=cache_set,
cache_mode=CacheMode.WRITEAROUND
)from maas.client import connect
from maas.client.enum import CacheMode
client = connect('http://maas.example.com:5240/MAAS/', apikey='key')
machine = client.machines.get('abc123')
# Step 1: Create cache set from SSD
nvme = machine.block_devices.get_by_name('nvme0n1')
cache_set = await machine.bcache_cache_sets.create(machine, nvme)
print(f"Created cache set: {cache_set.name}")
# Step 2: Create Bcache devices for HDDs
hdd1 = machine.block_devices.get_by_name('sdb')
hdd2 = machine.block_devices.get_by_name('sdc')
bcache0 = await machine.bcaches.create(
machine,
name='bcache0',
backing_device=hdd1,
cache_set=cache_set,
cache_mode=CacheMode.WRITEBACK
)
bcache1 = await machine.bcaches.create(
machine,
name='bcache1',
backing_device=hdd2,
cache_set=cache_set,
cache_mode=CacheMode.WRITEBACK
)
# Step 3: Format and mount Bcache devices
await bcache0.virtual_device.format('xfs')
await bcache0.virtual_device.mount('/data1')
await bcache1.virtual_device.format('xfs')
await bcache1.virtual_device.mount('/data2')
print(f"\nBcache devices created:")
print(f" {bcache0.name}: {bcache0.size} bytes at /data1")
print(f" {bcache1.name}: {bcache1.size} bytes at /data2")Cache sets have the following properties:
id (int, readonly): Unique cache set identifiernode (Node, readonly): Parent machinename (str): Cache set namecache_device (BlockDevice or Partition): Cache storage deviceBcache devices have the following properties:
id (int, readonly): Unique Bcache identifiernode (Node, readonly): Parent machinename (str): Bcache device namesize (int, readonly): Total capacity in bytes (from backing device)cache_mode (CacheMode): Current cache modeuuid (str): Bcache UUIDbacking_device (BlockDevice or Partition): Slow storage devicecache_set (BcacheCacheSet): Associated cache setvirtual_device (BlockDevice, readonly): Virtual block device representing the cached storagefrom maas.client.enum import CacheMode
class CacheMode:
"""Bcache cache mode enumeration."""
WRITEBACK = "writeback" # Write to cache first
WRITETHROUGH = "writethrough" # Write to both cache and backing
WRITEAROUND = "writearound" # Write directly to backing