From a9e6f0a79ba36b620382aa083f374e90b182a656 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Mon, 2 Sep 2024 09:43:48 +0000 Subject: [PATCH] autosign [Linux]: use blkid to test for device insertion --- mmgen/autosign.py | 15 ++++++----- mmgen/data/release_date | 2 +- mmgen/data/version | 2 +- test/cmdtest_py_d/ct_autosign.py | 33 ++++++++++++++++++------- test/overlay/fakemods/mmgen/autosign.py | 13 +++++----- 5 files changed, 39 insertions(+), 26 deletions(-) diff --git a/mmgen/autosign.py b/mmgen/autosign.py index 3cbeb40c..9c501934 100755 --- a/mmgen/autosign.py +++ b/mmgen/autosign.py @@ -12,10 +12,10 @@ autosign: Auto-sign MMGen transactions, message files and XMR wallet output files """ -import sys,os,asyncio +import sys, os, asyncio from stat import S_ISDIR,S_IWUSR,S_IRUSR from pathlib import Path -from subprocess import run,DEVNULL +from subprocess import run, PIPE, DEVNULL from .cfg import Config from .util import msg,msg_r,ymsg,rmsg,gmsg,bmsg,die,suf,fmt,fmt_list,async_run @@ -363,7 +363,6 @@ class Autosign: self.dfl_shm_dir = '/dev/shm' # linux-only attrs: - self.dev_label_dir = Path('/dev/disk/by-label') self.old_dfl_mountpoint = '/mnt/tx' self.old_dfl_mountpoint_errmsg = f""" Mountpoint ‘{self.old_dfl_mountpoint}’ is no longer supported! @@ -395,10 +394,7 @@ class Autosign: self.init_fixup() - # these use the ‘fixed-up’ values: - if sys.platform == 'linux': - self.dev_label_path = self.dev_label_dir / self.dev_label - elif sys.platform == 'darwin': + if sys.platform == 'darwin': # test suite uses ‘fixed-up’ dir for ramdisk from .platform.darwin.util import MacOSRamDisk self.ramdisk = MacOSRamDisk(cfg, self.macOS_ramdisk_name, 10, path=self.shm_dir) @@ -729,7 +725,10 @@ class Autosign: if self.cfg.no_insert_check: return True if sys.platform == 'linux': - return self.dev_label_path.exists() + cp = run('sudo blkid -s LABEL -o value'.split(), stdout=PIPE, text=True) + if cp.returncode not in (0, 2): + die(2, f'blkid exited with error code {cp.returncode}') + return self.dev_label in cp.stdout.splitlines() elif sys.platform == 'darwin': if self.cfg.test_suite_root_pfx: return self.mountpoint.exists() diff --git a/mmgen/data/release_date b/mmgen/data/release_date index bbf9c7cc..69bfda8c 100644 --- a/mmgen/data/release_date +++ b/mmgen/data/release_date @@ -1 +1 @@ -August 2024 +September 2024 diff --git a/mmgen/data/version b/mmgen/data/version index f483d2dc..921bb519 100644 --- a/mmgen/data/version +++ b/mmgen/data/version @@ -1 +1 @@ -15.0.dev4 +15.0.dev5 diff --git a/test/cmdtest_py_d/ct_autosign.py b/test/cmdtest_py_d/ct_autosign.py index edf135b0..c60daf57 100755 --- a/test/cmdtest_py_d/ct_autosign.py +++ b/test/cmdtest_py_d/ct_autosign.py @@ -69,9 +69,10 @@ class CmdTestAutosignBase(CmdTestBase): self.network_ids = [c+'_tn' for c in self.daemon_coins] + self.daemon_coins self._create_autosign_instances(create_dirs=not cfg.skipping_deps) + self.fs_image_path = Path(self.tmpdir).absolute() / 'removable_device_image' if sys.platform == 'linux': - self.txdev = VirtBlockDevice(self.asi.fs_image_path, '10M') + self.txdev = VirtBlockDevice(self.fs_image_path, '10M') if not (cfg.skipping_deps or self.live): self._create_removable_device() @@ -115,7 +116,7 @@ class CmdTestAutosignBase(CmdTestBase): })) if create_dirs and not self.live: - for k in ('mountpoint', 'shm_dir', 'wallet_dir', 'dev_label_dir'): + for k in ('mountpoint', 'shm_dir', 'wallet_dir'): if subdir == 'online' and k in ('shm_dir', 'wallet_dir'): continue if sys.platform == 'darwin' and k != 'mountpoint': @@ -127,6 +128,10 @@ class CmdTestAutosignBase(CmdTestBase): setattr(self, data['name'], asi) + def _set_e2label(self, label): + imsg(f'Setting label to {label}') + run(['/sbin/e2label', str(self.txdev.img_path), label], check=True) + def _create_removable_device(self): if sys.platform == 'linux': self.txdev.create() @@ -143,14 +148,14 @@ class CmdTestAutosignBase(CmdTestBase): cmd = [ 'hdiutil', 'create', '-size', '10M', '-fs', 'exFAT', '-volname', self.asi.dev_label, - str(self.asi.fs_image_path)] + str(self.fs_image_path)] redir = None if cfg.exact_output or cfg.verbose else DEVNULL run(cmd, stdout=redir, check=True) def _macOS_mount_fs_image(self, loc): time.sleep(0.2) run( - ['hdiutil', 'attach', '-mountpoint', str(loc.mountpoint), f'{loc.fs_image_path}.dmg'], + ['hdiutil', 'attach', '-mountpoint', str(loc.mountpoint), f'{self.fs_image_path}.dmg'], stdout=DEVNULL, check=True) def _macOS_eject_disk(self, label): @@ -236,8 +241,14 @@ class CmdTestAutosignBase(CmdTestBase): return loc = getattr(self, asi) if sys.platform == 'linux': - loc.dev_label_path.touch() - # self.txdev.attach() # WIP + self._set_e2label(loc.dev_label) + self.txdev.attach() + for _ in range(20): + if loc.device_inserted: + break + time.sleep(0.1) + else: + die(2, f'device insert timeout exceeded {loc.dev_label}') elif sys.platform == 'darwin': self._macOS_mount_fs_image(loc) @@ -246,9 +257,13 @@ class CmdTestAutosignBase(CmdTestBase): return loc = getattr(self, asi) if sys.platform == 'linux': - if loc.dev_label_path.exists(): - loc.dev_label_path.unlink() - # self.txdev.detach() # WIP + self.txdev.detach() + for _ in range(20): + if not loc.device_inserted: + break + time.sleep(0.1) + else: + die(2, f'device remove timeout exceeded {loc.dev_label}') elif sys.platform == 'darwin': self._macOS_eject_disk(loc.dev_label) diff --git a/test/overlay/fakemods/mmgen/autosign.py b/test/overlay/fakemods/mmgen/autosign.py index 6b806f3b..8d839fc3 100644 --- a/test/overlay/fakemods/mmgen/autosign.py +++ b/test/overlay/fakemods/mmgen/autosign.py @@ -5,18 +5,17 @@ class overlay_fake_Autosign: def init_fixup(self): if pfx := self.cfg.test_suite_root_pfx: subdir = pfx + '/' + ('online' if self.cfg.online else 'offline') - for k in ('mountpoint', 'shm_dir', 'wallet_dir', 'dev_label_dir'): - if hasattr(self, k): - orig_path = str(getattr(self, k)) - setattr(self, k, Path(subdir + orig_path.removeprefix(subdir))) + for k in ('mountpoint', 'shm_dir', 'wallet_dir'): + orig_path = str(getattr(self, k)) + setattr(self, k, Path(subdir + orig_path.removeprefix(subdir))) # mount --type=fuse-ext2 --options=rw+ ### current fuse-ext2 (0.4 29) is buggy - can’t use - self.fs_image_path = Path(f'{pfx}/removable_device_image').absolute() import sys if sys.platform == 'linux': - self.mount_cmd = f'sudo mount {self.fs_image_path} {self.mountpoint}' + self.dev_label = 'MMGEN_TS_ONLINE' if self.cfg.online else 'MMGEN_TS_OFFLINE' + self.mount_cmd = f'sudo mount LABEL={self.dev_label} {self.mountpoint}' self.umount_cmd = f'sudo umount {self.mountpoint}' -Autosign.dev_label = 'MMGEN_TS_TX' +Autosign.dev_label = 'MMGEN_TS_TX' # autosign_live only (Linux) Autosign.linux_mount_subdir = 'mmgen_ts_autosign' Autosign.macOS_ramdisk_name = 'TestAutosignRamDisk' Autosign.init_fixup = overlay_fake_Autosign.init_fixup