diff --git a/04-boottoolsSoftwareCompile.py b/04-boottoolsSoftwareCompile.py index cf85567..696082f 100755 --- a/04-boottoolsSoftwareCompile.py +++ b/04-boottoolsSoftwareCompile.py @@ -31,10 +31,11 @@ except: _run (['mkdir', '/usr/share/doc/spartlnx']) _run (['unzip', '-j', '-o', 'savepart.zip', '-d', '/usr/share/doc/spartlnx/', 'doc/en/*']) -print ('python-libfdisk') -_run (['apt-get', '-y', 'install', 'python3-psutil', 'python3-dev', 'libfdisk-dev', 'python3-setuptools']) -_run (['git', 'clone', 'git://git.48k.eu/python-libfdisk']) -os.chdir ('python-libfdisk') -_run (['python3', 'setup.py', 'install']) -os.chdir ('..') -_run (['apt-get', '-y', 'remove', 'python3-dev', 'python3-setuptools']) +if not os.path.exists ('python-libfdisk'): + print ('python-libfdisk') + _run (['apt-get', '-y', 'install', 'python3-psutil', 'python3-dev', 'libfdisk-dev', 'python3-setuptools']) + _run (['git', 'clone', 'git://git.48k.eu/python-libfdisk']) + os.chdir ('python-libfdisk') + _run (['python3', 'setup.py', 'install']) + os.chdir ('..') + _run (['apt-get', '-y', 'remove', 'python3-dev', 'python3-setuptools']) diff --git a/05-boottoolsFsLocales.py b/05-boottoolsFsLocales.py index f61b469..a7a5387 100755 --- a/05-boottoolsFsLocales.py +++ b/05-boottoolsFsLocales.py @@ -2,6 +2,7 @@ import os import subprocess +from boottoolsfunctions import _run debconf_settings = """ console-setup console-setup/charmap47 select UTF-8 diff --git a/06-boottoolsInitrdGenerate.py b/06-boottoolsInitrdGenerate.py index 328bf18..2d50941 100755 --- a/06-boottoolsInitrdGenerate.py +++ b/06-boottoolsInitrdGenerate.py @@ -10,6 +10,7 @@ def boottoolsInitrdGenerate (osrelease): for f in glob.glob ('/usr/lib/initramfs-tools/bin/*'): os.unlink (f) shutil.copy ('/bin/busybox', '/usr/lib/initramfs-tools/bin') + os.chdir ('/tmp') _run (['mkinitramfs', '-o', '/tmp/initrd.img-{}'.format (osrelease), '-v', osrelease]) shutil.copy ('/boot/vmlinuz-{}'.format (osrelease), '/tmp/') diff --git a/boottoolsfunctions/__init__.py b/boottoolsfunctions/__init__.py index 143ee42..04498f5 100644 --- a/boottoolsfunctions/__init__.py +++ b/boottoolsfunctions/__init__.py @@ -6,6 +6,7 @@ import logging import subprocess import shutil import glob +import datetime logger = logging.getLogger ('boottools') @@ -316,7 +317,7 @@ def btogSetFsBase (btrootfsimg, btrootfsmnt, osarch, oscodename, oshttp): logger.info ('debootstrap --arch="{}" --components=main,universe "{}" "{}" "{}" : ok'.format (osarch, oscodename, btrootfsmnt, oshttp)) return 0 -def boottoolsSshServer(): +def boottoolsSshServer (btrootfsmnt): if not os.path.exists ('/root/.ssh/id_rsa'): ## crea un par de claves en la VM, no en el chroot _run (['ssh-keygen', '-q', '-f', '/root/.ssh/id_rsa', '-N', '']) #shutil.copy ('/root/.ssh/id_rsa.pub', '/tmp/') @@ -325,12 +326,12 @@ def boottoolsSshServer(): if not os.path.exists ('{}/root/.ssh'.format (btrootfsmnt)): ## crea directorio dentro del chroot logger.debug ('creando directorio .ssh 600') os.mkdir ('{}/root/.ssh'.format (btrootfsmnt)) - os.chmod (0o700, '{}/root/.ssh'.format (btrootfsmnt)) + os.chmod ('{}/root/.ssh'.format (btrootfsmnt), 0o700) logger.debug ('creando el fichero authorized_keys') ## crea archivo en el chroot if not os.path.exists ('{}/root/.ssh/authorized_keys'.format (btrootfsmnt)): open ('{}/root/.ssh/authorized_keys'.format (btrootfsmnt), 'w').close() - os.chmod (0o600, '{}/root/.ssh/authorized_keys'.format (btrootfsmnt)) + os.chmod ('{}/root/.ssh/authorized_keys'.format (btrootfsmnt), 0o600) logger.debug ('importando la clave publica del servidor OG') #cat /tmp/id_rsa.pub @@ -348,8 +349,9 @@ def boottoolsSshServer(): else: logger.error ('no key publica og') -def boottoolsSshClient(): - _run (['ssh-keygen', '-q', '-f', '{}/root/.ssh/id_rsa'.format (btrootfsmnt), '-N', '']) ## crea un par de claves en el chroot +def boottoolsSshClient (btrootfsmnt): + if not os.path.exists ('{}/root/.ssh/id_rsa'.format (btrootfsmnt)): + _run (['ssh-keygen', '-q', '-f', '{}/root/.ssh/id_rsa'.format (btrootfsmnt), '-N', '']) ## crea un par de claves en el chroot #cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys ## coge la publica y se la autoriza a sí mismo fdin = open ('{}//root/.ssh/id_rsa.pub'.format (btrootfsmnt), 'r') @@ -365,23 +367,37 @@ def boottoolsSshClient(): #cp /root/.ssh/id_rsa.pub /tmp/rsa.ogclient.pub #btogFsInitrd genera un initrd. -def btogFsInitrd(): - pass - #echo "cp /tmp/*-${OSRELEASE} ${BTTARGETDIR}" ## esto copia algo?? - #cp /tmp/*-${OSRELEASE} ${BTTARGETDIR} +def btogFsInitrd (bttargetdir, osrelease): + #echo "cp /tmp/*-${OSRELEASE} ${BTTARGETDIR}" + #cp /tmp/*-${OSRELEASE} ${BTTARGETDIR} ## esto copia algo?? ## backup de oginitrd.img, oginitrd.img.sum, ogvmlinuz y ogvmlinuz.sum - #TIMEMOD=`date +%Y%m%d-%H%M%S` - #[ -f $BTTARGETDIR/oginitrd.img ] && mv $BTTARGETDIR/oginitrd.img $BTTARGETDIR/oginitrd.img.$TIMEMOD; mv $BTTARGETDIR/oginitrd.img.sum $BTTARGETDIR/oginitrd.img.sum.$TIMEMOD - #[ -f $BTTARGETDIR/ogvmlinuz ] && mv $BTTARGETDIR/ogvmlinuz $BTTARGETDIR/ogvmlinuz.$TIMEMOD; mv $BTTARGETDIR/ogvmlinuz.sum $BTTARGETDIR/ogvmlinuz.sum.$TIMEMOD + now = datetime.datetime.now(datetime.timezone.utc).strftime ('%Y%m%d-%H%M%S%z') + if os.path.exists ('{}/oginitrd.img'.format (bttargetdir)): + os.rename ('{}/oginitrd.img' .format (bttargetdir), '{}/oginitrd.img.{}' .format (bttargetdir, now)) + os.rename ('{}/oginitrd.img.sum'.format (bttargetdir), '{}/oginitrd.img.sum.{}'.format (bttargetdir, now)) + if os.path.exists ('{}/ogvmlinuz'.format (bttargetdir)): + os.rename ('{}/ogvmlinuz' .format (bttargetdir), '{}/ogvmlinuz.{}' .format (bttargetdir, now)) + os.rename ('{}/ogvmlinuz.sum'.format (bttargetdir), '{}/ogvmlinuz.sum.{}'.format (bttargetdir, now)) - #cp /tmp/initrd.img-${OSRELEASE} ${BTTARGETDIR}/oginitrd.img - #cp /tmp/vmlinuz-${OSRELEASE} ${BTTARGETDIR}/ogvmlinuz + shutil.copy ('/tmp/initrd.img-{}'.format (osrelease), '{}/oginitrd.img'.format (bttargetdir)) + shutil.copy ('/tmp/vmlinuz-{}' .format (osrelease), '{}/ogvmlinuz' .format (bttargetdir)) #DATASUM=`md5sum "${BTTARGETDIR}/oginitrd.img" | cut -f1 -d" "` + md5, _ = _run (['md5sum', '{}/oginitrd.img'.format (bttargetdir)]) + md5, rest = md5.split (' ', 1) #echo $DATASUM > ${BTTARGETDIR}/oginitrd.img.sum + with open ('{}/oginitrd.img.sum'.format (bttargetdir), 'w') as fd: + fd.write (md5) + #DATASUM=`md5sum "${BTTARGETDIR}/ogvmlinuz" | cut -f1 -d" "` + md5, _ = _run (['md5sum', '{}/ogvmlinuz'.format (bttargetdir)]) + md5, rest = md5.split (' ', 1) #echo $DATASUM > ${BTTARGETDIR}/ogvmlinuz.sum + with open ('{}/ogvmlinuz.sum'.format (bttargetdir), 'w') as fd: + fd.write (md5) #cd - #chmod -R 755 $BTTARGETDIR + for f in glob.glob ('{}/oginitrd*'.format (bttargetdir)) + glob.glob ('{}/vmlinuz*'.format (bttargetdir)): + os.chmod (f, 0o755) diff --git a/boottoolsgenerator.py b/boottoolsgenerator.py index 579a12b..187fd5e 100755 --- a/boottoolsgenerator.py +++ b/boottoolsgenerator.py @@ -163,18 +163,22 @@ stdout, _ = _run (['schroot', '--chroot', 'IMGogclient', '--', '{}/04-boottoolsS #cd - logger.debug ('04-boottoolsSoftwareCompile stdout follows: {}'.format (stdout)) +try: _run (['mount', btrootfsimg, btrootfsmnt, '-o', 'loop,offset=32256']) +except: + logger.error ('mount failed') + sys.exit (3) ############################################################################################ logger.info ('FASE 6 - Personalizar el sistema creado') logger.info ('Fase 6.1 Incorporar la clave publica del servidor') #cd / #schroot --chroot IMGogclient -- /usr/bin/boot-tools/boottoolsSshServer.sh ## no necesita chroot -boottoolsSshServer() +boottoolsSshServer (btrootfsmnt) #cd - -logger.info ('Fase 6.2. Incorpoar la clave publica del propio cliente') +logger.info ('Fase 6.2 Incorporar la clave publica del propio cliente') #schroot --chroot IMGogclient -- /usr/bin/boot-tools/boottoolsSshClient.sh ## no necesita chroot -boottoolsSshClient() +boottoolsSshClient (btrootfsmnt) ## el resultado es: ## - hay un nuevo par de claves en la VM /root/.ssh @@ -182,21 +186,22 @@ boottoolsSshClient() ## - las dos claves públicas (una de cada par) están autorizadan en el rootfs /var/lib/tftpboot/ogclient/ogclientmount/root/.ssh/authorized_keys ############################################################################################ -logger.info ('Fase 6.3. Configurando las locales') +if _is_mounted (btrootfsmnt): + _run (['umount', btrootfsmnt]) + +logger.info ('Fase 6.3 Configurando las locales') logger.debug ('running \'schroot --chroot IMGogclient -- {}/05-boottoolsFsLocales.py\'') stdout, _ = _run (['schroot', '--chroot', 'IMGogclient', '--', '{}/05-boottoolsFsLocales.py'.format (curdir)]) logger.debug ('05-boottoolsFsLocales stdout follows: {}'.format (stdout)) -if _is_mounted (btrootfsmnt): - _run (['umount', btrootfsmnt]) - #cd / #schroot -c IMGogclient -- /usr/bin/boot-tools/boottoolsInitrdGenerate.sh logger.debug ('running \'schroot --chroot IMGogclient -- {}/06-boottoolsInitrdGenerate.py --osrelease "{}"\''.format (curdir, osrelease)) stdout, _ = _run (['schroot', '--chroot', 'IMGogclient', '--', '{}/06-boottoolsInitrdGenerate.py'.format (curdir), '--osrelease', osrelease]) 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 -btogFsInitrd() +btogFsInitrd (bttargetdir, osrelease) """