Loop Devices¶
Loop devices (/dev/loop*) expose regular files as block devices —
commonly used in tests as backing storage for LVM, DM, and filesystem operations
without requiring real disks.
sts.loop
¶
Loop device management -- create, discover, attach/detach backing files.
LoopDevice
pydantic-model
¶
Bases: StorageDevice
Loop device backed by a regular file.
Construction performs no I/O. Call discover() to populate
attributes from losetup/blockdev, or use the create() /
using() class methods. Supports context-manager cleanup.
Example
device = LoopDevice(name='loop0').discover()
device = LoopDevice.create(size_mb=1024)
with LoopDevice.using(size_mb=512) as dev:
...
Show JSON schema:
{
"$defs": {
"LoopDeviceInfo": {
"description": "Parsed ``losetup -J`` output for a single loop device.\n\nAttributes:\n sizelimit: Size limit in bytes (0 = unlimited).\n offset: Offset in bytes (0 = start of file).\n autoclear: Whether device is automatically detached on last close.\n dio: Whether direct I/O is enabled (bypass page cache).",
"properties": {
"name": {
"title": "Name",
"type": "string"
},
"back-file": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"title": "Back-File"
},
"sizelimit": {
"anyOf": [
{
"type": "integer"
},
{
"type": "null"
}
],
"default": null,
"title": "Sizelimit"
},
"offset": {
"anyOf": [
{
"type": "integer"
},
{
"type": "null"
}
],
"default": null,
"title": "Offset"
},
"autoclear": {
"default": false,
"title": "Autoclear",
"type": "boolean"
},
"ro": {
"default": false,
"title": "Ro",
"type": "boolean"
},
"dio": {
"default": false,
"title": "Dio",
"type": "boolean"
},
"log-sec": {
"anyOf": [
{
"type": "integer"
},
{
"type": "null"
}
],
"default": null,
"title": "Log-Sec"
}
},
"required": [
"name"
],
"title": "LoopDeviceInfo",
"type": "object"
}
},
"additionalProperties": false,
"description": "Loop device backed by a regular file.\n\nConstruction performs no I/O. Call ``discover()`` to populate\nattributes from losetup/blockdev, or use the ``create()`` /\n``using()`` class methods. Supports context-manager cleanup.\n\nExample:\n ```python\n device = LoopDevice(name='loop0').discover()\n device = LoopDevice.create(size_mb=1024)\n with LoopDevice.using(size_mb=512) as dev:\n ...\n ```",
"properties": {
"path": {
"anyOf": [
{
"type": "string"
},
{
"format": "path",
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"title": "Path"
},
"name": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"title": "Name"
},
"size": {
"anyOf": [
{
"type": "integer"
},
{
"type": "null"
}
],
"default": null,
"title": "Size"
},
"model": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"title": "Model"
},
"image_path": {
"anyOf": [
{
"format": "path",
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"title": "Image Path"
},
"info": {
"anyOf": [
{
"$ref": "#/$defs/LoopDeviceInfo"
},
{
"type": "null"
}
],
"default": null
},
"backing_file_exists": {
"default": false,
"title": "Backing File Exists",
"type": "boolean"
}
},
"title": "LoopDevice",
"type": "object"
}
Fields:
-
name(str | None) -
path(PathOrStr | None) -
size(int | None) -
model(str | None) -
image_path(Path | None) -
info(LoopDeviceInfo | None) -
backing_file_exists(bool)
Validators:
-
_derive_fields
Source code in sts_libs/src/sts/loop.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 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 155 156 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 232 233 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 | |
backing_file
property
¶
Path to backing file, or None if not attached or file missing.
device_path
property
¶
Resolved path to the device node.
Raises:
| Type | Description |
|---|---|
DeviceNotFoundError
|
If device name is unset or node does not exist. |
__exit__(exc_type, exc_val, exc_tb)
¶
Remove device and backing file on context exit.
Source code in sts_libs/src/sts/loop.py
137 138 139 140 141 142 143 144 | |
create(name=None, *, size_mb=DEFAULT_SIZE_MB, image_path=DEFAULT_IMAGE_PATH, reuse_file=False)
classmethod
¶
Create a loop device with a backing file, returning None on failure.
Auto-detects the next free device if name is not provided.
Source code in sts_libs/src/sts/loop.py
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 | |
create_multiple(count, *, size_mb=DEFAULT_SIZE_MB, image_path=DEFAULT_IMAGE_PATH)
classmethod
¶
Create count loop devices, cleaning up all on failure.
Source code in sts_libs/src/sts/loop.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | |
detach()
¶
Detach device from backing file (file is not deleted).
Source code in sts_libs/src/sts/loop.py
176 177 178 179 180 181 182 183 184 185 186 | |
discover()
¶
Load loop device state (losetup, blockdev) from the system; returns self for chaining.
Source code in sts_libs/src/sts/loop.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
get_all()
classmethod
¶
Get all active loop devices.
Source code in sts_libs/src/sts/loop.py
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | |
get_by_name(name)
classmethod
¶
Find an active loop device by name (e.g. 'loop0').
Source code in sts_libs/src/sts/loop.py
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | |
remove()
¶
Detach device and delete backing file.
Source code in sts_libs/src/sts/loop.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
using(name=None, *, size_mb=DEFAULT_SIZE_MB, image_path=DEFAULT_IMAGE_PATH, reuse_file=False)
classmethod
¶
Like create(), but raises on failure (for use as a context manager).
Source code in sts_libs/src/sts/loop.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | |
LoopDeviceInfo
pydantic-model
¶
Bases: ReportModel
Parsed losetup -J output for a single loop device.
Attributes:
| Name | Type | Description |
|---|---|---|
sizelimit |
int | None
|
Size limit in bytes (0 = unlimited). |
offset |
int | None
|
Offset in bytes (0 = start of file). |
autoclear |
CoerceBool
|
Whether device is automatically detached on last close. |
dio |
CoerceBool
|
Whether direct I/O is enabled (bypass page cache). |
Show JSON schema:
{
"description": "Parsed ``losetup -J`` output for a single loop device.\n\nAttributes:\n sizelimit: Size limit in bytes (0 = unlimited).\n offset: Offset in bytes (0 = start of file).\n autoclear: Whether device is automatically detached on last close.\n dio: Whether direct I/O is enabled (bypass page cache).",
"properties": {
"name": {
"title": "Name",
"type": "string"
},
"back-file": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"title": "Back-File"
},
"sizelimit": {
"anyOf": [
{
"type": "integer"
},
{
"type": "null"
}
],
"default": null,
"title": "Sizelimit"
},
"offset": {
"anyOf": [
{
"type": "integer"
},
{
"type": "null"
}
],
"default": null,
"title": "Offset"
},
"autoclear": {
"default": false,
"title": "Autoclear",
"type": "boolean"
},
"ro": {
"default": false,
"title": "Ro",
"type": "boolean"
},
"dio": {
"default": false,
"title": "Dio",
"type": "boolean"
},
"log-sec": {
"anyOf": [
{
"type": "integer"
},
{
"type": "null"
}
],
"default": null,
"title": "Log-Sec"
}
},
"required": [
"name"
],
"title": "LoopDeviceInfo",
"type": "object"
}
Config:
populate_by_name:True
Fields:
-
name(str) -
back_file(str | None) -
sizelimit(int | None) -
offset(int | None) -
autoclear(CoerceBool) -
ro(CoerceBool) -
dio(CoerceBool) -
log_sec(int | None)
Source code in sts_libs/src/sts/loop.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |