diff --git a/02-boottoolsFsOpengnsys.py b/02-boottoolsFsOpengnsys.py new file mode 100755 index 0000000..98c6b01 --- /dev/null +++ b/02-boottoolsFsOpengnsys.py @@ -0,0 +1,80 @@ +#!/usr/bin/python3 + +import os +import logging +import subprocess +import shutil +import argparse + +#VERSIONBOOTTOOLS="ogLive" +nameisoclientfile = '/tmp/opengnsys_info_rootfs' +namehostclientfile = '/tmp/opengnsys_chroot' + +svnclientdir = '/tmp/opengnsys_installer/opengnsys/client/boot-tools' +svnclientstructure = '/tmp/opengnsys_installer/opengnsys/client/shared' +svnclientengine = '/tmp/opengnsys_installer/opengnsys/client/engine' + +ogclientmount = '' + +def boottoolsFsOpengnsys (osdistrib, oscodename, osrelease, osarch, oshttp): + print (':'.join ([osdistrib, oscodename, osrelease, osarch, oshttp])) + + print ('Iniciando la personalización con datos del repositorio') + + ## TODO mount fs, para lo cual necesito helpers + + #sed -e "s/OSCODENAME/$OSCODENAME/g" ${SVNCLIENTDIR}/includes/etc/apt/sources.list.${OSDISTRIB,,} > ${SVNCLIENTDIR}/includes/etc/apt/sources.list + sources_list_in = '{}/includes/etc/apt/sources.list.{}'.format (svnclientdir, osdistrib.lower()) + sources_list_out = '{}/includes/etc/apt/sources.list'.format (svnclientdir) + fdin = open (sources_list_in, 'r') + fdout = open (sources_list_out, 'w') + while True: + l = fdin.readline() + if not l: break + fdout.write (l.replace ('OSCODENAME', oscodename)) + fdin.close() + fdout.close() + + subprocess.run ('chmod -R 775 {}/includes/usr/bin/*'.format (svnclientdir), shell=True) + + os.makedirs ('{}/opt/opengnsys/lib/engine/bin/'.format (ogclientmount), exist_ok=True) + os.makedirs ('{}/usr/local/etc' .format (ogclientmount), exist_ok=True) + os.makedirs ('{}/usr/local/lib' .format (ogclientmount), exist_ok=True) + os.makedirs ('{}/usr/local/plugins' .format (ogclientmount), exist_ok=True) + + subprocess.run ('rsync -aH {}/includes/* {}/' .format (svnclientdir, ogclientmount), shell=True) + subprocess.run ('rsync -aH {}/* {}/opt/opengnsys/' .format (svnclientstructure, ogclientmount), shell=True) + subprocess.run ('rsync -aH {}/* {}/opt/opengnsys/lib/engine/bin/'.format (svnclientengine, ogclientmount), shell=True) + + # Si no existe, copiar pci.ids. + if not os.path.exists ('{}/etc/pci.ids'.format (ogclientmount)): + shutil.copy ('{}/lib/pci.ids'.format (svnclientstructure), '{}/etc/'.format (ogclientmount)) + + # Dependencias Qt para el Browser. + subprocess.run ('rsync -aH {}/etc/*.qmap {}/usr/local/etc' .format (svnclientstructure, ogclientmount), shell=True) + subprocess.run ('rsync -aH {}/lib/qtlib/* {}/usr/local/lib' .format (svnclientstructure, ogclientmount), shell=True) + subprocess.run ('rsync -aH {}/lib/fonts {}/usr/local/lib' .format (svnclientstructure, ogclientmount), shell=True) + subprocess.run ('rsync -aH {}/lib/qtplugins/* {}/usr/local/plugins'.format (svnclientstructure, ogclientmount), shell=True) + + # Browser y ogAdmClient. + if os.path.exists ('{}/bin/browser'.format (svnclientstructure)): + shutil.copy ('{}/bin/browser'.format (svnclientstructure), '{}/bin/'.format (ogclientmount)) + if os.path.exists ('{}/bin/ogAdmClient'.format (svnclientstructure)): + shutil.copy ('{}/bin/ogAdmClient'.format (svnclientstructure), '{}/bin/'.format (ogclientmount)) + + # El fichero de configuración debe sustituir a los 2 ficheros (borrar las 2 líneas). + #echo "${VERSIONBOOTTOOLS}-${OSCODENAME}-${OSRELEASE}-${GITRELEASE}" > $nameisoclientfile + #echo "${VERSIONBOOTTOOLS}-${OSCODENAME}-${GITRELEASE}" > $namehostclientfile + + ## desmontar de nuevo, ejem... + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument ('--mntpt', help='rootfs mount point', action='store', required=True) + parser.add_argument ('--osdistrib', help='OS distribution', action='store', required=True) + parser.add_argument ('--oscodename', help='OS codename', action='store', required=True) + parser.add_argument ('--osrelease', help='OS release', action='store', required=True) + parser.add_argument ('--osarch', help='OS architecture', action='store', required=True) + parser.add_argument ('--oshttp', help='OS HTTP source', action='store', required=True) + args = parser.parse_args() + boottoolsFsOpengnsys (args.mntpt, args.osdistrib, args.oscodename, args.osrelease, args.osarch, args.oshttp) diff --git a/boottoolsfunctions/__init__.py b/boottoolsfunctions/__init__.py index 2368017..7eb4cd4 100644 --- a/boottoolsfunctions/__init__.py +++ b/boottoolsfunctions/__init__.py @@ -1,4 +1,3 @@ - import platform import os import re @@ -10,6 +9,28 @@ import glob logger = logging.getLogger ('boottools') +def _run (args): + cp = subprocess.run (args, capture_output=True) + if cp.returncode: + logger.error ('command "{}" failed with rc "{}"'.format (' '.join(args), cp.returncode)) + raise + stdout = cp.stdout.decode ('utf-8').strip() + stderr = cp.stderr.decode ('utf-8').strip() + return stdout, stderr + +def _grep (regex, file): + with open (file, 'r') as f: + for l in f: + if (re.findall (regex, l)): return 1 + return 0 + +def _is_mounted (mntpt): + return _grep (mntpt, '/proc/mounts') + +def _umount (mntpt): + if (_is_mounted (mntpt)): + subprocess.run (['umount', mntpt]) + def btogGetVar (osarch): btdir = '/tmp/opengnsys_installer/opengnsys/client/boot-tools' bttargetdir = '/var/lib/tftpboot/ogclient/' @@ -146,28 +167,6 @@ def btogGetOsInfo2 (type_client, versionboottools, ogclientcfg, osdistrib, oscod return gitrelease, nameisoclient, namehostclient -def _run (args): - cp = subprocess.run (args, capture_output=True) - if cp.returncode: - logger.error ('command "{}" failed with rc "{}"'.format (' '.join(args), cp.returncode)) - raise - stdout = cp.stdout.decode ('utf-8').strip() - stderr = cp.stderr.decode ('utf-8').strip() - return stdout, stderr - -def _grep (regex, file): - with open (file, 'r') as f: - for l in f: - if (re.findall (regex, l)): return 1 - return 0 - -def _is_mounted (mntpt): - return _grep (mntpt, '/proc/mounts') - -def _umount (mntpt): - if (_is_mounted (mntpt)): - subprocess.run (['umount', mntpt]) - def btogSetFsVirtual (btrootfsimg, btrootfsimglabel, btrootfsmnt, btvirtualdisksize, bttargetdir, osarch): #if not btrootfsimg: btogGetVar() @@ -285,20 +284,13 @@ def btogSetFsAccess (btrootfsimg): return 0 # btogSetfsBase: Genera el sistema root base con debootstrap +# trabaja sobre un rootfs ya montado def btogSetFsBase (btrootfsimg, btrootfsmnt, osarch, oscodename, oshttp): logger.info ('Iniciando la generación del sistema de archivos') #mount | grep $BTROOTFSMNT || mount $BTROOTFSIMG $BTROOTFSMNT -o loop,offset=32256 if (not _is_mounted (btrootfsmnt)): - try: _run (['mount', btrootfsimg, btrootfsmnt, '-o', 'loop,offset=32256']) - except: - raise Exception ('mount failed') - - #mount | grep $BTROOTFSMNT && echoAndLog "$FUNCNAME: mount $BTROOTFSIMG $BTROOTFSMNT -o loop,offset=32256 OK " || errorAndLog "$FUNCNAME: mount $BTROOTFSIMG $BTROOTFSMNT -o loop,offset=32256 : FAILURE " - if (_is_mounted (btrootfsmnt)): - logger.info ('mount "{}" "{}" -o loop,offset=32256 OK'.format (btrootfsimg, btrootfsmnt)) - else: - raise Exception ('mount "{}" "{}" -o loop,offset=32256 : FAILURE'.format (btrootfsimg, btrootfsmnt)) + raise Exception ('rootfs should be mounted') logger.info ('debootstrapping system') try: _run (['debootstrap', '--arch="{}"'.format(osarch), '--components=main,universe', oscodename, btrootfsmnt, oshttp]) @@ -307,7 +299,5 @@ def btogSetFsBase (btrootfsimg, btrootfsmnt, osarch, oscodename, oshttp): subprocess.run (['umount', btrootfsmnt]) raise Exception ('debootstrap --arch={} --components=main,universe "{}" "{}" "{}" : ha fallado!'.format (osarch, oscodename, btrootfsmnt, oshttp)) - if (_is_mounted (btrootfsmnt)): - subprocess.run (['umount', btrootfsmnt]) logger.info ('debootstrap --arch="{}" --components=main,universe "{}" "{}" "{}" : ok'.format (osarch, oscodename, btrootfsmnt, oshttp)) return 0 diff --git a/boottoolsgenerator.py b/boottoolsgenerator.py index d238da5..6ba3706 100755 --- a/boottoolsgenerator.py +++ b/boottoolsgenerator.py @@ -7,8 +7,9 @@ import subprocess import glob import stat -sys.path.insert (0, __file__) -from boottoolsfunctions import btogGetOsInfo1, btogGetOsInfo2, btogGetVar, btogSetFsVirtual, btogSetFsAccess, btogSetFsBase +curdir = os.path.dirname (__file__) +sys.path.insert (0, curdir) +from boottoolsfunctions import _run, btogGetOsInfo1, btogGetOsInfo2, btogGetVar, btogSetFsVirtual, btogSetFsAccess, btogSetFsBase logging.basicConfig (filename='/tmp/boot-tools_installation.log', filemode='a', format='%(levelname)s %(asctime)s (%(funcName)s) %(message)s', level=logging.INFO) logger = logging.getLogger ('boottools') @@ -79,10 +80,17 @@ logger.info ('Fase 3.2 - Configurar acceso schroot al Segundo Sistema de archivo #cat /etc/schroot/schroot.conf | grep $BTROOTFSIMG || btogSetFsAccess btogSetFsAccess (btrootfsimg) +try: _run (['mount', btrootfsimg, btrootfsmnt, '-o', 'loop,offset=32256']) +except: + logger.error ('mount failed') + os._exit (3) + logger.info ('Fase 3.3 Generar sistema de archivos con debootstrap') logger.debug ('Try creation of a file within chroot (this operation may fail--that is ok)') -cp = subprocess.run (['schroot', '--preserve-environment', '--chroot', 'IMGogclient', '--', 'touch', '/tmp/ogclientOK']) +logger.debug ('Running \'schroot --chroot IMGogclient -- touch /tmp/ogclientOK\'') +cp = subprocess.run (['schroot', '--chroot', 'IMGogclient', '--', 'touch', '/tmp/ogclientOK']) if (cp.returncode): + logger.debug ('schroot returned code "{}", calling btogSetFsBase()'.format (cp.returncode)) try: btogSetFsBase (btrootfsimg, btrootfsmnt, osarch, oscodename, oshttp) #>>/tmp/fase3.out except Exception as e: logger.error (str (e)) @@ -95,6 +103,8 @@ else: ########################################################################### logger.info ('FASE 4 - Incorporando ficheros OpenGnsys al sistema raíz rootfs') +## por qué esta copia??? + #cp -a ${BTDIR}/includes/usr/bin/* /tmp >>/tmp/fase5.out subprocess.run (['cp', '-a'] + glob.glob ('{}/includes/usr/bin/*'.format (btdir)) + ['/tmp']) @@ -109,17 +119,22 @@ subprocess.run (['sed', '-i', '1 s/$/ {} ({})/'.format (gitrelease, osrelease), # En Ubuntu 13.04+ es necesario matar proceso de "udev" antes de desmontar. #umount $BTROOTFSMNT 2>/dev/null || (kill -9 $(lsof -t $BTROOTFSMNT); umount $BTROOTFSMNT) 2>/dev/null -""" -schroot --preserve-environment --chroot IMGogclient -- /tmp/boot-tools/boottoolsFsOpengnsys.sh >>/tmp/fase4.out + +logger.info ('running \'{}/02-boottoolsFsOpengnsys.py --mntpt "{}" --osdistrib "{}" --oscodename "{}" --osrelease "{}" --osarch "{}" --oshttp "{}"\''.format (curdir, btrootfsmnt, osdistrib, oscodename, osrelease, osarch, oshttp)) +cp = subprocess.run (['{}/02-boottoolsFsOpengnsys.py'.format (curdir), '--mntpt', btrootfsmnt, '--osdistrib', osdistrib, '--oscodename', oscodename, '--osrelease', osrelease, '--osarch', osarch, '--oshttp', oshttp], capture_output=True) +logger.info ('02-boottoolsFsOpengnsys stdout follows: {}'.format (cp.stdout.decode ('utf-8'))) +## /tmp/02.py --osdistrib ubuntu --oscodename focal --osrelease 5.4.0-42-generic --osarch amd64 --oshttp http://es.archive.ubuntu.com/ubuntu/ +## /tmp/opengnsys_installer/opengnsys/client/./boot-tools/02-boottoolsFsOpengnsys.py --mntpt "/var/lib/tftpboot/ogclient/ogclientmount" --osdistrib "Ubuntu" --oscodename "noble" --osrelease "6.8.0-39-generic" --osarch "amd64" --oshttp "http://es.archive.ubuntu.com/ubuntu/" ############################################################################################ logger.info ('FASE 5 - Instalar software') logger.info ('Fase 5.1 instalar paquetes deb con apt-get') -schroot --preserve-environment --chroot IMGogclient -- /usr/bin/boot-tools/boottoolsSoftwareInstall.sh >>/tmp/fase5.out +""" +schroot --chroot IMGogclient -- /usr/bin/boot-tools/boottoolsSoftwareInstall.sh >>/tmp/fase5.out logger.info ('Fase 5.2 compilar software.') cd / -schroot --preserve-environment --chroot IMGogclient -- /usr/bin/boot-tools/boottoolsSoftwareCompile.sh >>/tmp/fase5.out +schroot --chroot IMGogclient -- /usr/bin/boot-tools/boottoolsSoftwareCompile.sh >>/tmp/fase5.out cd - @@ -129,13 +144,13 @@ logger.info ('Fase 6.1 Incorporar la clave publica del servidor') cd / ssh-keygen -q -f /root/.ssh/id_rsa -N "" cp /root/.ssh/id_rsa.pub /tmp -schroot --preserve-environment --chroot IMGogclient -- /usr/bin/boot-tools/boottoolsSshServer.sh +schroot --chroot IMGogclient -- /usr/bin/boot-tools/boottoolsSshServer.sh cd - logger.info ('Fase 6.2. Incorpoar la clave publica del propio cliente') -schroot --preserve-environment --chroot IMGogclient -- /usr/bin/boot-tools/boottoolsSshClient.sh +schroot --chroot IMGogclient -- /usr/bin/boot-tools/boottoolsSshClient.sh logger.info ('Fase 6.3. Configurando las locales') -schroot --preserve-environment --chroot IMGogclient -- /usr/bin/boot-tools/boottoolsFsLocales.sh +schroot --chroot IMGogclient -- /usr/bin/boot-tools/boottoolsFsLocales.sh for i in `mount | grep IMGogclient | grep /var | cut -f3 -d" "`; do echo $i; umount $i; done for i in `mount | grep IMGogclient | grep /var | cut -f3 -d" "`; do echo $i; umount $i; done