chg: Disk, Pool: check if already in use

This commit is contained in:
timeshifter
2021-11-26 13:57:23 +01:00
parent 3f26d4776e
commit b13ad9ab8b
+14 -4
View File
@@ -57,7 +57,7 @@ class Manager:
def _backup_dataset(self, local_dataset: Dataset): def _backup_dataset(self, local_dataset: Dataset):
remote_dataset = self._external_pool.search_dataset_like(local_dataset) remote_dataset = self._external_pool.search_dataset_like(local_dataset)
last_common_snapshot = get_last_common_snapshot(remote_dataset, 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) sender = ZFS.send(last_common_snapshot, last_local_snapshot)
ZFS.receive(sender, self._external_pool.name) ZFS.receive(sender, self._external_pool.name)
@@ -140,17 +140,17 @@ class ZFS:
return [Dataset(ZFSPath.from_string(d)) for d in datasets_list] return [Dataset(ZFSPath.from_string(d)) for d in datasets_list]
@staticmethod @staticmethod
def import_pool_from_directory(name, directory) -> None: def import_pool_from_directory(name: str, directory: Path) -> None:
# -N: no mount # -N: no mount
# -d: directory to search the pool in # -d: directory to search the pool in
run_command([config.ZPOOL, "import", "-N", "-d", directory, name]) run_command([config.ZPOOL, "import", "-N", "-d", directory, name])
@staticmethod @staticmethod
def export_pool(name) -> None: def export_pool(name: str) -> None:
run_command([config.ZPOOL, "export", name]) run_command([config.ZPOOL, "export", name])
@staticmethod @staticmethod
def get_snapshots(name) -> list[Snapshot]: def get_snapshots(name: str) -> list[Snapshot]:
return [ return [
Snapshot(ZFSPath.from_string(name)) Snapshot(ZFSPath.from_string(name))
for name in get_output_of_command( for name in get_output_of_command(
@@ -212,6 +212,8 @@ class Disk:
self._name = name self._name = name
def decrypt(self) -> None: def decrypt(self) -> None:
if self._decrypted:
raise ValueError(f"Already decrypted {self._name}")
self._verify_mapper_entry_not_in_use() self._verify_mapper_entry_not_in_use()
self._decrypt_with_cryptsetup() self._decrypt_with_cryptsetup()
self._decrypted = True self._decrypted = True
@@ -234,6 +236,8 @@ class Disk:
return f"crypt-{self._name}" return f"crypt-{self._name}"
def encrypt(self): def encrypt(self):
if not self._decrypted:
raise ValueError(f"Not decrypted {self._name}")
get_output_of_command([config.CRYPTSETUP, "close", self._name]) get_output_of_command([config.CRYPTSETUP, "close", self._name])
self._decrypted = False self._decrypted = False
@@ -278,12 +282,18 @@ class ExternalPool(Pool):
self._disk = disk self._disk = disk
def import_(self) -> None: def import_(self) -> None:
if self._imported:
raise ValueError(f"Already imported {self.name}")
self._disk.decrypt() self._disk.decrypt()
self._import_from_directory(MAPPER_PATH) self._import_from_directory(MAPPER_PATH)
self._imported = True
def export(self) -> None: def export(self) -> None:
if not self._imported:
raise ValueError(f"Not imported {self.name}")
self._export_pool() self._export_pool()
self._disk.encrypt() self._disk.encrypt()
self._imported = False
def _import_from_directory(self, directory: Path) -> None: def _import_from_directory(self, directory: Path) -> None:
ZFS.import_pool_from_directory(self._name, directory) ZFS.import_pool_from_directory(self._name, directory)