Browse Source

autosign [Linux]: use blkid to test for device insertion

The MMGen Project 6 months ago
parent
commit
a9e6f0a79b

+ 7 - 8
mmgen/autosign.py

@@ -12,10 +12,10 @@
 autosign: Auto-sign MMGen transactions, message files and XMR wallet output files
 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 stat import S_ISDIR,S_IWUSR,S_IRUSR
 from pathlib import Path
 from pathlib import Path
-from subprocess import run,DEVNULL
+from subprocess import run, PIPE, DEVNULL
 
 
 from .cfg import Config
 from .cfg import Config
 from .util import msg,msg_r,ymsg,rmsg,gmsg,bmsg,die,suf,fmt,fmt_list,async_run
 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'
 			self.dfl_shm_dir    = '/dev/shm'
 
 
 			# linux-only attrs:
 			# linux-only attrs:
-			self.dev_label_dir = Path('/dev/disk/by-label')
 			self.old_dfl_mountpoint = '/mnt/tx'
 			self.old_dfl_mountpoint = '/mnt/tx'
 			self.old_dfl_mountpoint_errmsg = f"""
 			self.old_dfl_mountpoint_errmsg = f"""
 				Mountpoint ‘{self.old_dfl_mountpoint}’ is no longer supported!
 				Mountpoint ‘{self.old_dfl_mountpoint}’ is no longer supported!
@@ -395,10 +394,7 @@ class Autosign:
 
 
 		self.init_fixup()
 		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
 			from .platform.darwin.util import MacOSRamDisk
 			self.ramdisk = MacOSRamDisk(cfg, self.macOS_ramdisk_name, 10, path=self.shm_dir)
 			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:
 		if self.cfg.no_insert_check:
 			return True
 			return True
 		if sys.platform == 'linux':
 		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':
 		elif sys.platform == 'darwin':
 			if self.cfg.test_suite_root_pfx:
 			if self.cfg.test_suite_root_pfx:
 				return self.mountpoint.exists()
 				return self.mountpoint.exists()

+ 1 - 1
mmgen/data/release_date

@@ -1 +1 @@
-August 2024
+September 2024

+ 1 - 1
mmgen/data/version

@@ -1 +1 @@
-15.0.dev4
+15.0.dev5

+ 24 - 9
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.network_ids = [c+'_tn' for c in self.daemon_coins] + self.daemon_coins
 
 
 		self._create_autosign_instances(create_dirs=not cfg.skipping_deps)
 		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':
 		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):
 		if not (cfg.skipping_deps or self.live):
 			self._create_removable_device()
 			self._create_removable_device()
@@ -115,7 +116,7 @@ class CmdTestAutosignBase(CmdTestBase):
 				}))
 				}))
 
 
 			if create_dirs and not self.live:
 			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'):
 					if subdir == 'online' and k in ('shm_dir', 'wallet_dir'):
 						continue
 						continue
 					if sys.platform == 'darwin' and k != 'mountpoint':
 					if sys.platform == 'darwin' and k != 'mountpoint':
@@ -127,6 +128,10 @@ class CmdTestAutosignBase(CmdTestBase):
 
 
 			setattr(self, data['name'], asi)
 			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):
 	def _create_removable_device(self):
 		if sys.platform == 'linux':
 		if sys.platform == 'linux':
 			self.txdev.create()
 			self.txdev.create()
@@ -143,14 +148,14 @@ class CmdTestAutosignBase(CmdTestBase):
 			cmd = [
 			cmd = [
 				'hdiutil', 'create', '-size', '10M', '-fs', 'exFAT',
 				'hdiutil', 'create', '-size', '10M', '-fs', 'exFAT',
 				'-volname', self.asi.dev_label,
 				'-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
 			redir = None if cfg.exact_output or cfg.verbose else DEVNULL
 			run(cmd, stdout=redir, check=True)
 			run(cmd, stdout=redir, check=True)
 
 
 	def _macOS_mount_fs_image(self, loc):
 	def _macOS_mount_fs_image(self, loc):
 		time.sleep(0.2)
 		time.sleep(0.2)
 		run(
 		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)
 			stdout=DEVNULL, check=True)
 
 
 	def _macOS_eject_disk(self, label):
 	def _macOS_eject_disk(self, label):
@@ -236,8 +241,14 @@ class CmdTestAutosignBase(CmdTestBase):
 			return
 			return
 		loc = getattr(self, asi)
 		loc = getattr(self, asi)
 		if sys.platform == 'linux':
 		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':
 		elif sys.platform == 'darwin':
 			self._macOS_mount_fs_image(loc)
 			self._macOS_mount_fs_image(loc)
 
 
@@ -246,9 +257,13 @@ class CmdTestAutosignBase(CmdTestBase):
 			return
 			return
 		loc = getattr(self, asi)
 		loc = getattr(self, asi)
 		if sys.platform == 'linux':
 		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':
 		elif sys.platform == 'darwin':
 			self._macOS_eject_disk(loc.dev_label)
 			self._macOS_eject_disk(loc.dev_label)
 
 

+ 6 - 7
test/overlay/fakemods/mmgen/autosign.py

@@ -5,18 +5,17 @@ class overlay_fake_Autosign:
 	def init_fixup(self):
 	def init_fixup(self):
 		if pfx := self.cfg.test_suite_root_pfx:
 		if pfx := self.cfg.test_suite_root_pfx:
 			subdir = pfx + '/' + ('online' if self.cfg.online else 'offline')
 			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
 			# 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
 			import sys
 			if sys.platform == 'linux':
 			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}'
 				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.linux_mount_subdir = 'mmgen_ts_autosign'
 Autosign.macOS_ramdisk_name = 'TestAutosignRamDisk'
 Autosign.macOS_ramdisk_name = 'TestAutosignRamDisk'
 Autosign.init_fixup         = overlay_fake_Autosign.init_fixup
 Autosign.init_fixup         = overlay_fake_Autosign.init_fixup