instant-workstation/backend/disks/disk.py
Henrik Brüsecke 7f5b5209a1 Disks Service
2026-10-22 19:38:18 +03:00

31 lines
No EOL
848 B
Python

import pathlib
import threading
import weakref
import disk_paths
import disk_utils
DICTIONARY_LOCK = threading.Lock()
DISK_LOCKS = weakref.WeakValueDictionary()
def create_disk(request):
paths = disk_paths.DiskPaths(request)
workstation = request['workstation']
with disk_lock(paths):
if not paths.target_path.exists():
disk_utils.create_os_disk(paths)
if workstation['cpu'] == 'ARM' and not paths.nvram_target_path.exists():
disk_utils.create_nvram_disk(paths)
def delete_disk(request):
paths = disk_paths.DiskPaths(request)
with disk_lock(paths):
paths.target_path.unlink(missing_ok=True)
paths.nvram_target_path.unlink(missing_ok=True)
def disk_lock(paths):
with DICTIONARY_LOCK:
return DISK_LOCKS.setdefault(paths.target_filename, threading.Lock())