From 45f533aca9384d9ea2c1788288c5c4f71864e3f7 Mon Sep 17 00:00:00 2001 From: Natalia Serrano Date: Wed, 14 Aug 2024 11:01:35 +0200 Subject: [PATCH] refs #596 refactor (u)mounts --- boottoolsgenerator.py | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/boottoolsgenerator.py b/boottoolsgenerator.py index 285f1cf..6891796 100755 --- a/boottoolsgenerator.py +++ b/boottoolsgenerator.py @@ -11,7 +11,7 @@ import shutil curdir = os.path.dirname (__file__) sys.path.insert (0, curdir) -from boottoolsfunctions import _run, _is_mounted, btogGetOsInfo1, btogGetOsInfo2, btogGetVar, btogSetFsVirtual, btogSetFsAccess, btogSetFsBase, boottoolsSshServer, boottoolsSshClient, btogFsInitrd, btogFsSqfs, btogIsoGenerator +from boottoolsfunctions import _run, _mount, _umount, btogGetOsInfo1, btogGetOsInfo2, btogGetVar, btogSetFsVirtual, btogSetFsAccess, btogSetFsBase, boottoolsSshServer, boottoolsSshClient, btogFsInitrd, btogFsSqfs, btogIsoGenerator def _logging(): #logging.root.handlers = [] @@ -63,9 +63,13 @@ ogclientcfg, osdistrib, oscodename, osrelease, osarch, oshttp = btogGetOsInfo1(t btdir, bttargetdir, btrootfsimg, btrootfsmnt, btrootfsimglabel, log_file, versionboottools, btvirtualdisksize = btogGetVar(osarch) gitrelease, nameisoclient, namehostclient = btogGetOsInfo2(type_client, versionboottools, ogclientcfg, osdistrib, oscodename, osrelease, osarch, oshttp) +def _mount_rootfs(): + global btrootfsimg, btrootfsmnt + _mount (btrootfsimg, btrootfsmnt, opts=['-o', 'loop,offset=32256']) + ## this is convenient in case the previous run failed and we want to run this program again -if _is_mounted (btrootfsmnt): - _run (['umount', btrootfsmnt]) +try: _umount (btrootfsmnt) +except: pass ########################################################################## @@ -116,7 +120,7 @@ def _debootstrap(): cp = subprocess.run (['schroot', '--chroot', 'IMGogclient', '--', 'stat', '/etc']) if (cp.returncode): logger.debug (f'schroot returned code "{cp.returncode}", calling btogSetFsBase()') - try: _run (['mount', btrootfsimg, btrootfsmnt, '-o', 'loop,offset=32256']) + try: _mount_rootfs() except: logger.error ('mount failed') sys.exit (3) @@ -149,17 +153,16 @@ _run (['sed', '-i', f'1 s/$/ {gitrelease} ({osrelease})/', f'{btdir}/includes/et #umount $BTROOTFSMNT 2>/dev/null || (kill -9 $(lsof -t $BTROOTFSMNT); umount $BTROOTFSMNT) 2>/dev/null def _cerodos(): - if (not _is_mounted (btrootfsmnt)): - try: _run (['mount', btrootfsimg, btrootfsmnt, '-o', 'loop,offset=32256']) - except: - logger.error ('mount failed') - sys.exit (3) + try: _mount_rootfs() + except: + logger.error ('mount failed') + sys.exit (3) logger.debug (f'running \'{curdir}/02-boottoolsFsOpengnsys.py --mntpt "{btrootfsmnt}" --osdistrib "{osdistrib}" --oscodename "{oscodename}" --osrelease "{osrelease}" --osarch "{osarch}" --oshttp "{oshttp))}"\'') stdout, _ = _run ([f'{curdir}/02-boottoolsFsOpengnsys.py', '--mntpt', btrootfsmnt, '--osdistrib', osdistrib, '--oscodename', oscodename, '--osrelease', osrelease, '--osarch', osarch, '--oshttp', oshttp]) logger.debug (f'02-boottoolsFsOpengnsys stdout follows: {stdout}') - _run (['umount', btrootfsmnt]) + _umount (btrootfsmnt) _cerodos() @@ -191,7 +194,7 @@ _cerocuatro(): def _ssh_stuff(): - try: _run (['mount', btrootfsimg, btrootfsmnt, '-o', 'loop,offset=32256']) + try: _mount_rootfs() except: logger.error ('mount failed') sys.exit (3) @@ -204,8 +207,7 @@ def _ssh_stuff(): #schroot --chroot IMGogclient -- /usr/bin/boot-tools/boottoolsSshClient.sh ## no necesita chroot boottoolsSshClient (btrootfsmnt) - if _is_mounted (btrootfsmnt): - _run (['umount', btrootfsmnt]) + _umount (btrootfsmnt) ## el resultado es: ## - hay un nuevo par de claves en la VM /root/.ssh @@ -250,12 +252,12 @@ btogFsInitrd (bttargetdir, osrelease) logger.info ('Fase 7.2 Generar fichero sqfs a partir del fichero img') -try: _run (['mount', btrootfsimg, btrootfsmnt, '-o', 'loop,offset=32256']) +try: _mount_rootfs() except: logger.error ('mount failed') sys.exit (3) btogFsSqfs (bttargetdir, btrootfsmnt) -_run (['umount', btrootfsmnt]) +_umount (btrootfsmnt) logger.info ('Fase 7.3 Generar la ISO') btogIsoGenerator (pxepkg, isolinux_tpl, bttargetdir, nameisoclient)