diff --git a/boottoolsfunctions/__init__.py b/boottoolsfunctions/__init__.py index 04498f5..1f2668b 100644 --- a/boottoolsfunctions/__init__.py +++ b/boottoolsfunctions/__init__.py @@ -10,6 +10,61 @@ import datetime logger = logging.getLogger ('boottools') +isolinux_tpl = """ +DEFAULT menu.c32 +PROMPT 0 +ALLOWOPTIONS 1 + +MENU TITLE OpenGnsys __NAMEISOCLIENT__ + + +LABEL 0 +MENU LABEL ogLive vga irqpool acpi ogdebug ip:none +KERNEL /ogclient/linuxISO +APPEND initrd=/ogclient/oginitrd.img ro vga=788 irqpoll acpi=on boot=oginit og2nd=sqfs ogprotocol=local ogactiveadmin=true ogdebug=true ip=none + +LABEL 1 +MENU LABEL ogLive irqpoll acpi ip:none +KERNEL /ogclient/linuxISO +APPEND initrd=/ogclient/oginitrd.img ro irqpoll acpi=on boot=oginit og2nd=sqfs ogprotocol=local ogactiveadmin=true ogdebug=false ip=none + +LABEL 2 +MENU LABEL ogLive acpi debug ip=dhcp +KERNEL /ogclient/linuxISO +APPEND initrd=/ogclient/oginitrd.img ro acpi=on boot=oginit og2nd=sqfs ogprotocol=local ogactiveadmin=true ogdebug=true ip=dhcp + +LABEL 3 +MENU LABEL ogLive ip=dhcp +KERNEL /ogclient/linuxISO +APPEND initrd=/ogclient/oginitrd.img ro acpi=off boot=oginit og2nd=sqfs ogprotocol=local ogactiveadmin=true ogdebug=false ip=dhcp + +LABEL 4 +MENU LABEL local +localboot 0x80 +append - + +label 5 +MENU LABEL Network boot via gPXE lkrn +KERNEL __PXEPKG__.lkrn + +label 6 +MENU LABEL Network boot via gPXE usb +KERNEL __PXEPKG__.usb + +label 7 +MENU LABEL Network boot via gPXE pxe +KERNEL __PXEPKG__.pxe + +label 8 +MENU LABEL Network boot via gPXE iso +KERNEL __PXEPKG__.iso + +label 9 +MENU LABEL pxe +KERNEL /clonezilla/live/vmlinuz1 +APPEND initrd=/clonezilla/live/initrd1.img boot=live union=aufs noswap vga=788 ip=frommedia +""" + def _run (args): cp = subprocess.run (args, text=True, capture_output=True) if cp.returncode: @@ -175,7 +230,7 @@ def btogGetOsInfo2 (type_client, versionboottools, ogclientcfg, osdistrib, oscod print ('OSRELEASE="{}"'.format (osrelease), file=f) print ('OSARCH="{}"'.format (osarch), file=f) print ('OSHTTP="{}"'.format (oshttp), file=f) - print ('GITRELEASE="{}"'.format (gitrelease, file=f)) + print ('GITRELEASE="{}"'.format (gitrelease), file=f) print ('NAMEISOCLIENT="{}"'.format (nameisoclient), file=f) print ('NAMEHOSTCLIENT="{}"'.format (namehostclient), file=f) @@ -401,3 +456,81 @@ def btogFsInitrd (bttargetdir, osrelease): #chmod -R 755 $BTTARGETDIR for f in glob.glob ('{}/oginitrd*'.format (bttargetdir)) + glob.glob ('{}/vmlinuz*'.format (bttargetdir)): os.chmod (f, 0o755) + +#btogFsSqfs convierte el sistema root en sqfs +def btogFsSqfs (bttargetdir, btrootfsmnt): + logger.info ('Iniciando la creación del sistema de archivos en sqfs') + + # si ya existe un sqfs lo renombramos + if os.path.exists ('{}/ogclient.sqfs'.format (bttargetdir)): + now = datetime.datetime.now(datetime.timezone.utc).strftime ('%Y%m%d-%H%M%S%z') + os.rename ('{}/ogclient.sqfs'.format (bttargetdir), '{}/ogclient.sqfs.{}'.format (bttargetdir, now)) + + _run (['mksquashfs', btrootfsmnt, '{}/ogclient.sqfs'.format (bttargetdir)]) + os.chmod ('{}/ogclient.sqfs'.format (bttargetdir), 0o744) + + #DATASUM=`md5sum "${BTTARGETDIR}/ogclient.sqfs" | cut -f1 -d" "` + md5, _ = _run (['md5sum', '{}/ogclient.sqfs'.format (bttargetdir)]) + md5, rest = md5.split (' ', 1) + #echo $DATASUM > ${BTTARGETDIR}/ogclient.sqfs.sum + with open ('{}/ogclient.sqfs.sum'.format (bttargetdir), 'w') as fd: + fd.write (md5) + +# btogIsoGenerator genera la iso del cliente +def btogIsoGenerator (pxepkg, bttargetdir, nameisoclient): + #Preparamos los gestores de arranque + try: os.makedirs ('/tmp/iso/isolinux', exist_ok=True) + except: raise + + subprocess.run (['cp -a /usr/lib/syslinux/* /tmp/iso/isolinux/'], shell=True) + + if 'gpxe' == pxepkg: + subprocess.run (['cp -a /usr/share/gpxe/* /tmp/iso/isolinux'], shell=True) + elif 'ipxe' == pxepkg: + subprocess.run (['cp -a /usr/lib/ipxe/* /tmp/iso/isolinux'], shell=True) + else: + raise Exception ('unknown pxepkg value "{}"'.format (pxepkg)) + + # Si existe el fichero ISO, montarlo para extraer isolinux.bin. + if os.path.exists ('/tmp/iso/isolinux/{}.iso'.format (pxepkg)): + os.mkdir ('/tmp/iso/isolinux/mount') + _run (['mount', '-o', 'loop', '/tmp/iso/isolinux/{}.iso'.format (pxepkg), '/tmp/iso/isolinux/mount']) + subprocess.run (['cp -a /tmp/iso/isolinux/mount/* /tmp/iso/isolinux'], shell=True) + _run (['umount', '/tmp/iso/isolinux/mount']) + os.rmdir ('/tmp/iso/isolinux/mount') + os.unlink ('/tmp/iso/isolinux/{}.iso'.format (pxepkg)) + + ## ojo que aquí hay que interpolar $NAMEISOCLIENT y $PXEPKG + with open ('/tmp/iso/isolinux/isolinux.cfg', 'w') as fd: + fd.write (isolinux_tpl.strip().replace('__NAMEISOCLIENT__', nameisoclient).replace('__PXEPKG__', pxepkg)) + + # preparamos el directorio boot-tools. + if not os.path.exists ('/tmp/iso/ogclient'): + os.mkdir ('/tmp/iso/ogclient') + shutil.copy ('{}/ogclient.sqfs' .format (bttargetdir), '/tmp/iso/ogclient/') + shutil.copy ('{}/ogclient.sqfs.sum'.format (bttargetdir), '/tmp/iso/ogclient/') + shutil.copy ('{}/ogvmlinuz' .format (bttargetdir), '/tmp/iso/ogclient/') + shutil.copy ('{}/ogvmlinuz.sum' .format (bttargetdir), '/tmp/iso/ogclient/') + shutil.copy ('{}/ogvmlinuz' .format (bttargetdir), '/tmp/iso/ogclient/linuxISO') + shutil.copy ('{}/ogvmlinuz.sum' .format (bttargetdir), '/tmp/iso/ogclient/linuxISO.sum') + shutil.copy ('{}/oginitrd.img' .format (bttargetdir), '/tmp/iso/ogclient/') + shutil.copy ('{}/oginitrd.img.sum' .format (bttargetdir), '/tmp/iso/ogclient/') + #el ogclienteToISO debe tener una copia del ogvmlinuz como linuxISO + #cp -prv /var/lib/tftpboot/ogclientToIso/* /tmp/iso/ogclient + + oldpwd = os.getcwd() + os.chdir ('/tmp') + logger.debug ('mkisofs -V ogClient -o {}.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -J -no-emul-boot -boot-load-size 4 -boot-info-table /tmp/iso'.format (nameisoclient)) + _run (['mkisofs', '-V', 'ogClient', '-o', '{}.iso'.format (nameisoclient), '-b', 'isolinux/isolinux.bin', '-c', 'isolinux/boot.cat', '-J', '-no-emul-boot', '-boot-load-size', '4', '-boot-info-table', '/tmp/iso']) + + ##Generamos el md5 + #DATASUM=`md5sum ${NAMEISOCLIENT}.iso | cut -f1 -d" "` + md5, _ = _run (['md5sum', '{}.iso'.format (nameisoclient)]) + md5, rest = md5.split (' ', 1) + #echo $DATASUM > ${NAMEISOCLIENT}.iso.sum + with open ('{}.iso.sum'.format (nameisoclient), 'w') as fd: + fd.write (md5) + os.chdir (oldpwd) + + os.rename ('/tmp/{}.iso' .format (nameisoclient), '/var/lib/tftpboot/ogclient/{}.iso' .format (nameisoclient)) + os.rename ('/tmp/{}.iso.sum'.format (nameisoclient), '/var/lib/tftpboot/ogclient/{}.iso.sum'.format (nameisoclient)) diff --git a/boottoolsgenerator.py b/boottoolsgenerator.py index 187fd5e..e166db8 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 +from boottoolsfunctions import _run, _is_mounted, btogGetOsInfo1, btogGetOsInfo2, btogGetVar, btogSetFsVirtual, btogSetFsAccess, btogSetFsBase, boottoolsSshServer, boottoolsSshClient, btogFsInitrd, btogFsSqfs, btogIsoGenerator #logging.root.handlers = [] logging.basicConfig ( @@ -46,11 +46,6 @@ if os.getuid(): #os.chdir ('/tmp') -#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 -#for i in `mount | grep IMGogclient | grep /var | cut -f3 -d" "`; do echo $i; umount $i; done - - ####################################################################3 logger.info ('FASE 1 - Asignación de variables') @@ -59,6 +54,9 @@ btdir, bttargetdir, btrootfsimg, btrootfsmnt, btrootfsimglabel, log_file, versio gitrelease, nameisoclient, namehostclient = btogGetOsInfo2(type_client, versionboottools, ogclientcfg, osdistrib, oscodename, osrelease, osarch, oshttp) logger.info ('OpenGnsys CLIENT installation begins') +if _is_mounted (btrootfsmnt): + _run (['umount', btrootfsmnt]) + ########################################################################## logger.info ('FASE 2 - Instalación de software adicional.') @@ -201,21 +199,20 @@ stdout, _ = _run (['schroot', '--chroot', 'IMGogclient', '--', '{}/06-boottoolsI logger.debug ('06-boottoolsInitrdGenerate stdout follows: {}'.format (stdout)) ## esto deja initrd.img-6.8.0-31-generic y vmlinuz-6.8.0-31-generic en /tmp +logger.info ('FASE 7 - Generar distribucion') +logger.info ('Fase 7.1 Generar el initrd') btogFsInitrd (bttargetdir, osrelease) -""" -logger.info ('FASE 7 - Generar distribucion') -logger.info ('Fase 7.1 Generar el initrd') -btogFsInitrd >>/tmp/fase7.out -echo ================= nati after fase 8.1; ls -la /opt/opengnsys/tftpboot/ogclient/ logger.info ('Fase 7.2 Generar fichero sqfs a partir del fichero img') -btogFsSqfs -echo ================= nati after fase 8.2; ls -la /opt/opengnsys/tftpboot/ogclient/ -umount $BTROOTFSMNT 2>/dev/null +try: _run (['mount', btrootfsimg, btrootfsmnt, '-o', 'loop,offset=32256']) +except: + logger.error ('mount failed') + sys.exit (3) +btogFsSqfs (bttargetdir, btrootfsmnt) +_run (['umount', btrootfsmnt]) + logger.info ('Fase 7.3 Generar la ISO') -btogIsoGenerator >>/tmp/fase7.out -echo ================= nati after fase 8.3; ls -la /opt/opengnsys/tftpboot/ogclient/ -""" +btogIsoGenerator (pxepkg, bttargetdir, nameisoclient) logger.info ('OpenGnsys installation finished')