chg: Disk, Pool: check if already in use
This commit is contained in:
+14
-4
@@ -57,7 +57,7 @@ class Manager:
|
||||
def _backup_dataset(self, local_dataset: Dataset):
|
||||
remote_dataset = self._external_pool.search_dataset_like(local_dataset)
|
||||
last_common_snapshot = get_last_common_snapshot(remote_dataset, local_dataset)
|
||||
last_local_snapshot: Snapshot = local_dataset.snapshots_matching_backup_tags[-1]
|
||||
last_local_snapshot: Snapshot = local_dataset.last_snapshot
|
||||
sender = ZFS.send(last_common_snapshot, last_local_snapshot)
|
||||
ZFS.receive(sender, self._external_pool.name)
|
||||
|
||||
@@ -140,17 +140,17 @@ class ZFS:
|
||||
return [Dataset(ZFSPath.from_string(d)) for d in datasets_list]
|
||||
|
||||
@staticmethod
|
||||
def import_pool_from_directory(name, directory) -> None:
|
||||
def import_pool_from_directory(name: str, directory: Path) -> None:
|
||||
# -N: no mount
|
||||
# -d: directory to search the pool in
|
||||
run_command([config.ZPOOL, "import", "-N", "-d", directory, name])
|
||||
|
||||
@staticmethod
|
||||
def export_pool(name) -> None:
|
||||
def export_pool(name: str) -> None:
|
||||
run_command([config.ZPOOL, "export", name])
|
||||
|
||||
@staticmethod
|
||||
def get_snapshots(name) -> list[Snapshot]:
|
||||
def get_snapshots(name: str) -> list[Snapshot]:
|
||||
return [
|
||||
Snapshot(ZFSPath.from_string(name))
|
||||
for name in get_output_of_command(
|
||||
@@ -212,6 +212,8 @@ class Disk:
|
||||
self._name = name
|
||||
|
||||
def decrypt(self) -> None:
|
||||
if self._decrypted:
|
||||
raise ValueError(f"Already decrypted {self._name}")
|
||||
self._verify_mapper_entry_not_in_use()
|
||||
self._decrypt_with_cryptsetup()
|
||||
self._decrypted = True
|
||||
@@ -234,6 +236,8 @@ class Disk:
|
||||
return f"crypt-{self._name}"
|
||||
|
||||
def encrypt(self):
|
||||
if not self._decrypted:
|
||||
raise ValueError(f"Not decrypted {self._name}")
|
||||
get_output_of_command([config.CRYPTSETUP, "close", self._name])
|
||||
self._decrypted = False
|
||||
|
||||
@@ -278,12 +282,18 @@ class ExternalPool(Pool):
|
||||
self._disk = disk
|
||||
|
||||
def import_(self) -> None:
|
||||
if self._imported:
|
||||
raise ValueError(f"Already imported {self.name}")
|
||||
self._disk.decrypt()
|
||||
self._import_from_directory(MAPPER_PATH)
|
||||
self._imported = True
|
||||
|
||||
def export(self) -> None:
|
||||
if not self._imported:
|
||||
raise ValueError(f"Not imported {self.name}")
|
||||
self._export_pool()
|
||||
self._disk.encrypt()
|
||||
self._imported = False
|
||||
|
||||
def _import_from_directory(self, directory: Path) -> None:
|
||||
ZFS.import_pool_from_directory(self._name, directory)
|
||||
|
||||
Reference in New Issue
Block a user