Skip to content

Block Device

This section documents the base block device functionality, which provides common operations and properties for working with block devices.

sts.blockdevice

Block device management module.

This module provides functionality for managing block devices: - Device discovery - Device information - Device operations

BlockDevice dataclass

Bases: StorageDevice

Block device representation.

This class extends StorageDevice with additional functionality: - Device information (size, model, type) - Device operations (read/write status) - Device state (mounted, removable)

Parameters:

Name Type Description Default
path Path | str | None

Device path (e.g. '/dev/sda')

None
_data_cache dict[str, Any] | None

Optional cached device data from lsblk

None

Raises:

Type Description
DeviceError

If device is not a block device or cannot be queried

Source code in sts_libs/src/sts/blockdevice.py
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
@dataclass
class BlockDevice(StorageDevice):
    """Block device representation.

    This class extends StorageDevice with additional functionality:
    - Device information (size, model, type)
    - Device operations (read/write status)
    - Device state (mounted, removable)

    Args:
        path: Device path (e.g. '/dev/sda')
        _data_cache: Optional cached device data from lsblk

    Raises:
        DeviceError: If device is not a block device or cannot be queried
    """

    # Internal data caches to avoid repeated system calls
    _data_cache: dict[str, Any] | None = field(default=None, init=True, repr=False)
    _blockdev_data: dict[str, Any] = field(init=False, repr=False)
    _lsblk_data: dict[str, Any] = field(init=False, repr=False)

    # Sysfs path for block devices
    SYS_BLOCK_PATH: ClassVar[Path] = Path('/sys/dev/block')

    def __post_init__(self) -> None:
        """Initialize device data."""
        # Initialize parent class first
        super().__post_init__()

        # Load device data from cache or system
        if self._data_cache:
            self._blockdev_data = self._data_cache
            self._lsblk_data = self._data_cache
        else:
            # Query device data if not cached
            self._blockdev_data = self._load_blockdev_data()
            self._lsblk_data = self._load_lsblk_data()

        # Set common attributes from data
        self.size = self._blockdev_data['size']
        self.model = self._lsblk_data.get('model')

    def _load_blockdev_data(self) -> dict[str, Any]:
        """Load blockdev data for device.

        Uses blockdev --report to get low-level block device information
        like sector size, read-only status, etc.

        Returns:
            Dictionary of blockdev data

        Raises:
            DeviceError: If blockdev data cannot be loaded
        """
        result = run(f'blockdev --report {self.path}')
        if result.failed:
            raise DeviceError(f'Failed to get blockdev data: {result.stderr}')

        try:
            lines = result.stdout.splitlines()
            if len(lines) < MIN_BLOCKDEV_LINES:
                raise DeviceError(f'No data from {self.path}')

            # Parse blockdev output format:
            # RO RA SSZ BSZ StartSec Size Device
            header = lines[0].split()
            fields = lines[1].split()

            if header != ['RO', 'RA', 'SSZ', 'BSZ', 'StartSec', 'Size', 'Device']:
                raise DeviceError(f'Unknown output of blockdev: {header}')

            return {
                'ro': fields[0] != 'rw',
                'ra': int(fields[1]),
                'log-sec': int(fields[2]),
                'phy-sec': int(fields[3]),
                'start': int(fields[4]),
                'size': int(fields[5]),
            }
        except (IndexError, ValueError) as e:
            raise DeviceError(f'Invalid blockdev data: {e}') from e

    def _load_lsblk_data(self) -> dict[str, Any]:
        """Load lsblk data for device.

        Uses lsblk to get detailed device information like filesystem type,
        mount status, etc.

        Returns:
            Dictionary of lsblk data

        Raises:
            DeviceError: If lsblk data cannot be loaded
        """
        result = run(f'lsblk -JOb {self.path}')
        if result.failed:
            raise DeviceError(f'Failed to get lsblk data: {result.stderr}')

        try:
            blockdevs = json.loads(result.stdout)['blockdevices']
            if not blockdevs:
                raise DeviceError(f'No data from {self.path}')

            data = blockdevs[0]
            # Add start sector if missing (needed for partition detection)
            if 'start' not in data:
                data['start'] = self._get_start_sector(data)

        except (json.JSONDecodeError, KeyError, IndexError) as e:
            raise DeviceError(f'Invalid lsblk data: {e}') from e

        return data

    def _get_start_sector(self, data: dict[str, Any]) -> int:
        """Get start sector for device.

        For partitions, reads the start sector from sysfs.
        For whole disks, returns 0.

        Args:
            data: Device data from lsblk

        Returns:
            Start sector number (0 if not found)
        """
        if not data.get('pkname'):  # No parent device = whole disk
            return 0

        # Try reading start sector from sysfs
        with suppress(ValueError, DeviceError):
            result = run(f"cat {self.SYS_BLOCK_PATH}/{data['maj:min']}/start")
            if result.succeeded and result.stdout:
                return int(result.stdout)

        return 0

    # Properties

    @property
    def is_partition(self) -> bool:
        """Return True if the device is a partition.

        Determined by start sector - partitions start after sector 0.

        Example:
            ```python
            BlockDevice('/dev/sda1').is_partition
            True
            BlockDevice('/dev/sda').is_partition
            False
            ```
        """
        return self._blockdev_data['start'] > 0

    @property
    def sector_size(self) -> int:
        """Return sector size for the device in bytes.

        Example:
            ```python
            BlockDevice('/dev/sda1').sector_size
            512
            ```
        """
        try:
            return self._blockdev_data['log-sec']
        except KeyError:
            return 0

    @property
    def block_size(self) -> int:
        """Return block size for the device in bytes.

        Example:
            ```python
            BlockDevice('/dev/sda').block_size
            4096
            ```
        """
        try:
            return self._blockdev_data['phy-sec']
        except KeyError:
            return 0

    @property
    def start_sector(self) -> int:
        """Return start sector of the device on the underlying device.

        Usually zero for full devices and non-zero for partitions.

        Example:
            ```python
            BlockDevice('/dev/sda1').start_sector
            2048
            BlockDevice('/dev/md0').start_sector
            0
            ```
        """
        return self._blockdev_data['start']

    @property
    def is_writable(self) -> bool:
        """Return True if device is writable (no RO status).

        Example:
            ```python
            BlockDevice('/dev/sda').is_writable
            True
            BlockDevice('/dev/loop1').is_writable
            False
            ```
        """
        return not self._blockdev_data['ro']

    @property
    def ra(self) -> int:
        """Return Read Ahead for the device in 512-bytes sectors.

        Read-ahead improves sequential read performance by pre-fetching data.

        Example:
            ```python
            BlockDevice('/dev/sda').ra
            256
            ```
        """
        return self._blockdev_data['ra']

    @property
    def is_removable(self) -> bool:
        """Return True if device is removable.

        Example:
            ```python
            BlockDevice('/dev/sda').is_removable
            False
            ```
        """
        return bool(self._lsblk_data['rm'])

    @property
    def hctl(self) -> str | None:
        """Return Host:Channel:Target:Lun for SCSI devices.

        SCSI addressing format used by the kernel.
        Not available for non-SCSI devices like NVMe.

        Example:
            ```python
            BlockDevice('/dev/sda').hctl
            '1:0:0:0'
            BlockDevice('/dev/nvme1n1').hctl
            None
            ```
        """
        return self._lsblk_data.get('hctl')

    @property
    def state(self) -> str | None:
        """Return state of the device.

        Example:
            ```python
            BlockDevice('/dev/nvme1n1').state
            'live'
            BlockDevice('/dev/nvme1n1p1').state
            None
            ```
        """
        return self._lsblk_data.get('state')

    @property
    def partition_type(self) -> str | None:
        """Return partition table type.

        Example:
            ```python
            BlockDevice('/dev/nvme1n1p1').partition_type
            'gpt'
            BlockDevice('/dev/nvme1n1').partition_type
            None
            ```
        """
        return self._lsblk_data.get('pttype')

    @property
    def wwn(self) -> str | None:
        """Return unique storage identifier.

        World Wide Name (WWN) uniquely identifies storage devices.
        Useful for tracking devices across reboots/reconnects.

        Example:
            ```python
            BlockDevice('/dev/nvme1n1').wwn
            'eui.00253856a5ebaa6f'
            BlockDevice('/dev/nvme1n1p1').wwn
            'eui.00253856a5ebaa6f'
            ```
        """
        return self._lsblk_data.get('wwn')

    @property
    def filesystem_type(self) -> str | None:
        """Return filesystem type.

        Example:
            ```python
            BlockDevice('/dev/nvme1n1p1').filesystem_type
            'vfat'
            BlockDevice('/dev/nvme1n1').filesystem_type
            None
            ```
        """
        return self._lsblk_data.get('fstype')

    @property
    def is_mounted(self) -> bool:
        """Return True if the device is mounted.

        Example:
            ```python
            BlockDevice('/dev/nvme1n1p1').is_mounted
            True
            ```
        """
        return bool(self._lsblk_data.get('mountpoint'))

    @property
    def type(self) -> str:
        """Return device type.

        Common types:
        - disk: Whole disk device
        - part: Partition
        - lvm: Logical volume
        - crypt: Encrypted device
        - mpath: Multipath device

        Example:
            ```python
            BlockDevice('/dev/nvme1n1').type
            'disk'
            BlockDevice('/dev/nvme1n1p1').type
            'part'
            BlockDevice('/dev/mapper/vg-lvol0').type
            'lvm'
            ```
        """
        return self._lsblk_data['type']

    @property
    def transport_type(self) -> str | None:
        """Return device transport type.

        Common types:
        - nvme: NVMe device
        - iscsi: iSCSI device
        - fc: Fibre Channel device

        Example:
            ```python
            BlockDevice('/dev/nvme1n1p1').transport_type
            'nvme'
            BlockDevice('/dev/sdc').transport_type
            'iscsi'
            ```
        """
        return self._lsblk_data.get('tran')

    def __repr__(self) -> str:
        """Return string representation of device."""
        return f'BlockDevice(path={self.path!r})'

block_size: int property

Return block size for the device in bytes.

Example
BlockDevice('/dev/sda').block_size
4096

filesystem_type: str | None property

Return filesystem type.

Example
BlockDevice('/dev/nvme1n1p1').filesystem_type
'vfat'
BlockDevice('/dev/nvme1n1').filesystem_type
None

hctl: str | None property

Return Host:Channel:Target:Lun for SCSI devices.

SCSI addressing format used by the kernel. Not available for non-SCSI devices like NVMe.

Example
BlockDevice('/dev/sda').hctl
'1:0:0:0'
BlockDevice('/dev/nvme1n1').hctl
None

is_mounted: bool property

Return True if the device is mounted.

Example
BlockDevice('/dev/nvme1n1p1').is_mounted
True

is_partition: bool property

Return True if the device is a partition.

Determined by start sector - partitions start after sector 0.

Example
BlockDevice('/dev/sda1').is_partition
True
BlockDevice('/dev/sda').is_partition
False

is_removable: bool property

Return True if device is removable.

Example
BlockDevice('/dev/sda').is_removable
False

is_writable: bool property

Return True if device is writable (no RO status).

Example
BlockDevice('/dev/sda').is_writable
True
BlockDevice('/dev/loop1').is_writable
False

partition_type: str | None property

Return partition table type.

Example
BlockDevice('/dev/nvme1n1p1').partition_type
'gpt'
BlockDevice('/dev/nvme1n1').partition_type
None

ra: int property

Return Read Ahead for the device in 512-bytes sectors.

Read-ahead improves sequential read performance by pre-fetching data.

Example
BlockDevice('/dev/sda').ra
256

sector_size: int property

Return sector size for the device in bytes.

Example
BlockDevice('/dev/sda1').sector_size
512

start_sector: int property

Return start sector of the device on the underlying device.

Usually zero for full devices and non-zero for partitions.

Example
BlockDevice('/dev/sda1').start_sector
2048
BlockDevice('/dev/md0').start_sector
0

state: str | None property

Return state of the device.

Example
BlockDevice('/dev/nvme1n1').state
'live'
BlockDevice('/dev/nvme1n1p1').state
None

transport_type: str | None property

Return device transport type.

Common types: - nvme: NVMe device - iscsi: iSCSI device - fc: Fibre Channel device

Example
BlockDevice('/dev/nvme1n1p1').transport_type
'nvme'
BlockDevice('/dev/sdc').transport_type
'iscsi'

type: str property

Return device type.

Common types: - disk: Whole disk device - part: Partition - lvm: Logical volume - crypt: Encrypted device - mpath: Multipath device

Example
BlockDevice('/dev/nvme1n1').type
'disk'
BlockDevice('/dev/nvme1n1p1').type
'part'
BlockDevice('/dev/mapper/vg-lvol0').type
'lvm'

wwn: str | None property

Return unique storage identifier.

World Wide Name (WWN) uniquely identifies storage devices. Useful for tracking devices across reboots/reconnects.

Example
BlockDevice('/dev/nvme1n1').wwn
'eui.00253856a5ebaa6f'
BlockDevice('/dev/nvme1n1p1').wwn
'eui.00253856a5ebaa6f'

__post_init__()

Initialize device data.

Source code in sts_libs/src/sts/blockdevice.py
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
def __post_init__(self) -> None:
    """Initialize device data."""
    # Initialize parent class first
    super().__post_init__()

    # Load device data from cache or system
    if self._data_cache:
        self._blockdev_data = self._data_cache
        self._lsblk_data = self._data_cache
    else:
        # Query device data if not cached
        self._blockdev_data = self._load_blockdev_data()
        self._lsblk_data = self._load_lsblk_data()

    # Set common attributes from data
    self.size = self._blockdev_data['size']
    self.model = self._lsblk_data.get('model')

__repr__()

Return string representation of device.

Source code in sts_libs/src/sts/blockdevice.py
605
606
607
def __repr__(self) -> str:
    """Return string representation of device."""
    return f'BlockDevice(path={self.path!r})'

filter_devices_by_block_sizes(block_devices, *, prefer_matching_block_sizes, required_devices=0)

Filter block devices based on their block sizes, preferring devices with matching sector and block sizes.

Parameters:

Name Type Description Default
block_devices list[BlockDevice]

List of BlockDevice objects to filter

required
required_devices int

Minimum number of devices required (defaults to env var MIN_DEVICES or 0)

0
prefer_matching_block_sizes bool

If True, prefer devices where sector_size matches block_size

required

Returns:

Type Description
tuple[int, int]

Tuple containing:

list[BlockDevice]
  • Tuple of (sector_size, block_size) for the chosen devices
tuple[tuple[int, int], list[BlockDevice]]
  • List of device paths that match the criteria
Example

devices = [ ... BlockDevice('/dev/sda', sector_size=512, block_size=512), ... BlockDevice('/dev/sdb', sector_size=512, block_size=4096), ... BlockDevice('/dev/sdc', sector_size=512, block_size=512), ... ] sizes, matching_devices = filter_devices_by_block_sizes( ... devices, prefer_matching_block_sizes=True, required_devices=2 ... ) sizes (512, 512) [device.path for device in matching_devices]['/dev/sda', '/dev/sdc']

Source code in sts_libs/src/sts/blockdevice.py
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
def filter_devices_by_block_sizes(
    block_devices: list[BlockDevice], *, prefer_matching_block_sizes: bool, required_devices: int = 0
) -> tuple[tuple[int, int], list[BlockDevice]]:
    """Filter block devices based on their block sizes, preferring devices with matching sector and block sizes.

    Args:
        block_devices: List of BlockDevice objects to filter
        required_devices: Minimum number of devices required (defaults to env var MIN_DEVICES or 0)
        prefer_matching_block_sizes: If True, prefer devices where sector_size matches block_size

    Returns:
        Tuple containing:
        - Tuple of (sector_size, block_size) for the chosen devices
        - List of device paths that match the criteria

    Example:
        >>> devices = [
        ...     BlockDevice('/dev/sda', sector_size=512, block_size=512),
        ...     BlockDevice('/dev/sdb', sector_size=512, block_size=4096),
        ...     BlockDevice('/dev/sdc', sector_size=512, block_size=512),
        ... ]
        >>> sizes, matching_devices = filter_devices_by_block_sizes(
        ...     devices, prefer_matching_block_sizes=True, required_devices=2
        ... )
        >>> sizes
        (512, 512)
        >>> [device.path for device in matching_devices]
        ['/dev/sda', '/dev/sdc']
    """
    if required_devices == 0:
        required_devices = int(getenv('MIN_DEVICES', '0'))

    if not block_devices:
        logging.warning('No block devices provided')
        return (0, 0), []

    # Group devices by their block sizes
    devices_by_block_sizes: dict[tuple[int, int], list[BlockDevice]] = {}
    for disk in block_devices:
        block_sizes = (disk.sector_size, disk.block_size)
        if block_sizes in devices_by_block_sizes:
            devices_by_block_sizes[block_sizes].append(disk)
        else:
            devices_by_block_sizes[block_sizes] = [disk]

    # Find the best group of devices based on preferences
    best_group_size = (0, 0)
    best_group_devices = []
    max_devices = 0

    for block_size, devices in devices_by_block_sizes.items():
        num_devices = len(devices)

        # Skip if we don't have enough devices and block sizes don't match
        if num_devices < required_devices and prefer_matching_block_sizes and block_size[0] != block_size[1]:
            continue

        # If we prefer matching block sizes and found a matching group with enough devices
        if prefer_matching_block_sizes and block_size[0] == block_size[1] and num_devices >= required_devices:
            best_group_size = block_size
            best_group_devices = devices
            break

        # Update best group if we found more devices
        if num_devices > max_devices:
            max_devices = num_devices
            best_group_size = block_size
            best_group_devices = devices

    logging.info(
        f"Using following disks: {', '.join([str(dev.path) for dev in best_group_devices])} "
        f"with block sizes: {best_group_size}"
    )

    return best_group_size, best_group_devices

get_all()

Get list of all block devices.

Returns:

Type Description
list[BlockDevice]

List of BlockDevice instances

Example
get_all()
[BlockDevice(path='/dev/sda'), BlockDevice(path='/dev/sda1')]
Source code in sts_libs/src/sts/blockdevice.py
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
def get_all() -> list[BlockDevice]:
    """Get list of all block devices.

    Returns:
        List of BlockDevice instances

    Example:
        ```python
        get_all()
        [BlockDevice(path='/dev/sda'), BlockDevice(path='/dev/sda1')]
        ```
    """
    devices = []
    for data in _load_all_devices():
        devices.extend(_process_device_data(data))
    return devices

get_free_disks()

Get list of unused block devices.

A device is considered free if it: - Has no parent device (not a partition) - Has no child devices (no partitions) - Is not a LVM physical volume - Is not mounted - Is not READ-ONLY

This is useful for finding disks that can be safely used for testing.

Returns:

Type Description
list[BlockDevice]

List of BlockDevice instances

Example
get_free_disks()
[BlockDevice(path='/dev/sdc')]
Source code in sts_libs/src/sts/blockdevice.py
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
def get_free_disks() -> list[BlockDevice]:
    """Get list of unused block devices.

    A device is considered free if it:
    - Has no parent device (not a partition)
    - Has no child devices (no partitions)
    - Is not a LVM physical volume
    - Is not mounted
    - Is not READ-ONLY

    This is useful for finding disks that can be safely used for testing.

    Returns:
        List of BlockDevice instances

    Example:
        ```python
        get_free_disks()
        [BlockDevice(path='/dev/sdc')]
        ```
    """
    free_devices = []
    # Get list of LVM PVs to exclude
    pvs = PhysicalVolume.get_all().keys()

    for data in _load_all_devices():
        # Skip if device has parent (is partition) or children (has partitions)
        if data.get('pkname') or data.get('children'):
            continue

        # Skip if device is LVM PV
        if f"/dev/{data['name']}" in pvs:
            continue

        # Skip if device is mounted
        if data.get('is_mounted'):
            continue

        # Skip if device is READ-ONLY
        if data.get('ro'):
            logging.debug(f"Device {data['name']} is READ-ONLY.")
            continue

        # Add start sector if missing (needed for partition detection)
        if 'start' not in data:
            data['start'] = 0

        _block_device = BlockDevice(Path('/dev') / data['name'], _data_cache=data)

        # Double check mount status (belt and suspenders)
        if not _block_device.is_mounted:
            free_devices.append(_block_device)

    return free_devices