189 lines
9.9 KiB
Python
189 lines
9.9 KiB
Python
#!/usr/bin/python3
|
|
|
|
import sys
|
|
import os
|
|
import logging
|
|
import subprocess
|
|
import glob
|
|
import stat
|
|
|
|
curdir = os.path.dirname (__file__)
|
|
sys.path.insert (0, curdir)
|
|
from boottoolsfunctions import _run, _is_mounted, 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')
|
|
|
|
if not os.path.exists ('/tmp/opengnsys_installer/opengnsys/client/engine'):
|
|
branch = 'main'
|
|
tmpdir = subprocess.run (['mktemp', '--tmpdir', '--directory', 'oggit.XXXXXX']).stdout.decode ('utf-8').strip()
|
|
_run (['git', 'clone', '-c', 'http.sslVerify=false', '--branch', branch, 'https://ognproject.evlt.uma.es/gitea/opengnsys/opengnsys.git', tmpdir])
|
|
_run (['rsync', '-aH', '{}/client/engine'.format(tmpdir), '{}/client/shared'.format(tmpdir), '/tmp/opengnsys_installer/opengnsys/client/'])
|
|
_run (['rm', '-rf', tmpdir])
|
|
|
|
type_client = sys.argv[1] if len(sys.argv)>1 else 'host'
|
|
#WORKDIR = '/tmp/opengnsys_installer'
|
|
#INSTALL_TARGET = '/opt/opengnsys'
|
|
#PROGRAMDIR = $(readlink -e $(dirname "$0"))
|
|
|
|
if os.getuid():
|
|
logger.error ('ERROR: this program must run under root privileges!!')
|
|
os._exit (1)
|
|
|
|
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')
|
|
ogclientcfg, osdistrib, oscodename, osrelease, osarch, oshttp = btogGetOsInfo1(type_client)
|
|
btdir, bttargetdir, btrootfsimg, btrootfsmnt, btrootfsimglabel, log_file, versionboottools, btvirtualdisksize = btogGetVar(osarch)
|
|
gitrelease, nameisoclient, namehostclient = btogGetOsInfo2(type_client, versionboottools, ogclientcfg, osdistrib, oscodename, osrelease, osarch, oshttp)
|
|
logger.info ('OpenGnsys CLIENT installation begins')
|
|
|
|
|
|
##########################################################################
|
|
logger.info ('FASE 2 - Instalación de software adicional.')
|
|
#grep "http://free.nchc.org.tw/drbl-core" /etc/apt/sources.list || echo "deb http://free.nchc.org.tw/drbl-core drbl stable" >> /etc/apt/sources.list
|
|
_run (['apt-get', 'update']) #>>/tmp/fase2.out
|
|
acse_gpxe, _ = _run (['apt-cache', 'search', 'gpxe'])
|
|
acse_ipxe, _ = _run (['apt-cache', 'search', 'ipxe'])
|
|
if acse_ipxe:
|
|
pxepkg = 'ipxe'
|
|
elif acse_gpxe:
|
|
pxepkg = 'gpxe'
|
|
else:
|
|
logger.error ('neither gpxe nor ipxe found in apt-cache')
|
|
os._exit (1)
|
|
logger.info ('PXE package is "{}"'.format (pxepkg))
|
|
## TODO qemu no existe, hace falta?
|
|
_run (['apt-get', '-y', 'install', 'jq', 'syslinux', 'syslinux-efi', 'syslinux-utils', 'debootstrap', 'subversion', 'schroot', 'squashfs-tools', 'syslinux', 'genisoimage', 'qemu-utils', 'lsof', pxepkg]) #>>/tmp/fase2.out
|
|
|
|
|
|
###################################################################3
|
|
logger.info ('FASE 3 - Creación del Sistema raiz RootFS (Segundo Sistema archivos (img))')
|
|
logger.info ('Fase 3.1 Generar y formatear el disco virtual. Generar el dispositivo loop.')
|
|
rc = subprocess.run ('file {} |grep -q "partition 1 *: ID=0x83"'.format (btrootfsimg), shell=True).returncode
|
|
print (rc)
|
|
if (rc): ## 'file|grep' failed
|
|
try: btogSetFsVirtual (btrootfsimg, btrootfsimglabel, btrootfsmnt, btvirtualdisksize, bttargetdir, osarch)
|
|
except Exception as e:
|
|
logger.error (str (e))
|
|
os._exit (2)
|
|
#echo ================= nati after btogSetFsVirtual; ls -la /opt/opengnsys/tftpboot/ogclient/
|
|
|
|
logger.info ('Fase 3.2 - Configurar acceso schroot al Segundo Sistema de archivos (img)')
|
|
#cat /etc/schroot/schroot.conf | grep $BTROOTFSIMG || btogSetFsAccess
|
|
btogSetFsAccess (btrootfsimg)
|
|
|
|
## para hacer schroot --cosas, el mntpt tiene que estar desmontado
|
|
## si está montado da un pete tal que 'E: 10mount: mount: /run/schroot/mount/IMGogclient-7fbf51a2-e37e-48e5-8e5d-83f8901fc7ed: wrong fs type, bad option, bad superblock on /dev/loop1, missing codepage or helper program, or other error.'
|
|
|
|
logger.info ('Fase 3.3 Generar sistema de archivos con debootstrap')
|
|
logger.debug ('Try creation of a file within chroot (this operation may fail with "... etc/resolv.conf: No such file or directory"--that is ok)')
|
|
logger.debug ('Running \'schroot --chroot IMGogclient -- stat /etc\'')
|
|
cp = subprocess.run (['schroot', '--chroot', 'IMGogclient', '--', 'stat', '/etc'])
|
|
if (cp.returncode):
|
|
logger.debug ('schroot returned code "{}", calling btogSetFsBase()'.format (cp.returncode))
|
|
try: _run (['mount', btrootfsimg, btrootfsmnt, '-o', 'loop,offset=32256'])
|
|
except:
|
|
logger.error ('mount failed')
|
|
os._exit (3)
|
|
try: btogSetFsBase (btrootfsimg, btrootfsmnt, osarch, oscodename, oshttp) #>>/tmp/fase3.out
|
|
except Exception as e:
|
|
logger.error (str (e))
|
|
os._exit (3)
|
|
#echo ================= nati after fase 3; ls -la /opt/opengnsys/tftpboot/ogclient/
|
|
|
|
###########################################################################
|
|
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
|
|
#_run (['cp', '-a'] + glob.glob ('{}/includes/usr/bin/*'.format (btdir)) + ['/tmp'])
|
|
|
|
#chmod +x /tmp/boot-tools/*.sh
|
|
#for i in glob.glob ('/tmp/boot-tools/*.sh'):
|
|
# st = os.stat (i)
|
|
# os.chmod (i, st.st_mode|stat.S_IXUSR|stat.S_IXGRP|stat.S_IXOTH)
|
|
|
|
# Incluir revisión.
|
|
## FIXME esto la incluye incondicionalmente, y luego terminamos con "OpenGnsys Client 1.2.0-rc1 gitrelease (osrelease) gitrelease (osrelease) gitrelease (osrelease) gitrelease (osrelease) gitrelease (osrelease) gitrelease (osrelease) ..."
|
|
#sed -i "1 s/$/ $GITRELEASE ($OSRELEASE)/" ${BTDIR}/includes/etc/initramfs-tools/scripts/VERSION.txt
|
|
_run (['sed', '-i', '1 s/$/ {} ({})/'.format (gitrelease, osrelease), '{}/includes/etc/initramfs-tools/scripts/VERSION.txt'.format (btdir)])
|
|
|
|
# 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
|
|
|
|
if (not _is_mounted (btrootfsmnt)):
|
|
try: _run (['mount', btrootfsimg, btrootfsmnt, '-o', 'loop,offset=32256'])
|
|
except:
|
|
logger.error ('mount failed')
|
|
os._exit (3)
|
|
|
|
logger.debug ('running \'{}/02-boottoolsFsOpengnsys.py --mntpt "{}" --osdistrib "{}" --oscodename "{}" --osrelease "{}" --osarch "{}" --oshttp "{}"\''.format (curdir, btrootfsmnt, osdistrib, oscodename, osrelease, osarch, oshttp))
|
|
stdout, _ = _run (['{}/02-boottoolsFsOpengnsys.py'.format (curdir), '--mntpt', btrootfsmnt, '--osdistrib', osdistrib, '--oscodename', oscodename, '--osrelease', osrelease, '--osarch', osarch, '--oshttp', oshttp])
|
|
logger.debug ('02-boottoolsFsOpengnsys stdout follows: {}'.format (stdout))
|
|
## /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/"
|
|
|
|
_run (['umount', btrootfsmnt])
|
|
|
|
|
|
############################################################################################
|
|
logger.info ('FASE 5 - Instalar software')
|
|
logger.info ('Fase 5.1 instalar paquetes deb con apt-get')
|
|
logger.debug ('running \'schroot --chroot IMGogclient -- {}/03-boottoolsSoftwareInstall.py --osrelease "{}" --osarch "{}"\''.format (curdir, osrelease, osarch))
|
|
stdout, _ = _run (['schroot', '--chroot', 'IMGogclient', '--', '{}/03-boottoolsSoftwareInstall.py'.format (curdir), '--osrelease', osrelease, '--osarch', osarch])
|
|
logger.debug ('03-boottoolsSoftwareInstall stdout follows: {}'.format (stdout))
|
|
|
|
"""
|
|
schroot --chroot IMGogclient -- /usr/bin/boot-tools/boottoolsSoftwareInstall.sh >>/tmp/fase5.out
|
|
logger.info ('Fase 5.2 compilar software.')
|
|
cd /
|
|
schroot --chroot IMGogclient -- /usr/bin/boot-tools/boottoolsSoftwareCompile.sh >>/tmp/fase5.out
|
|
cd -
|
|
|
|
|
|
############################################################################################
|
|
logger.info ('FASE 6 - Personalizar el sistema creado')
|
|
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 --chroot IMGogclient -- /usr/bin/boot-tools/boottoolsSshServer.sh
|
|
cd -
|
|
logger.info ('Fase 6.2. Incorpoar la clave publica del propio cliente')
|
|
schroot --chroot IMGogclient -- /usr/bin/boot-tools/boottoolsSshClient.sh
|
|
|
|
logger.info ('Fase 6.3. Configurando las locales')
|
|
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
|
|
for i in `mount | grep IMGogclient | grep /var | cut -f3 -d" "`; do echo $i; umount $i; done
|
|
|
|
echo ================= nati after fase 6; ls -la /opt/opengnsys/tftpboot/ogclient/
|
|
|
|
|
|
#########################################################################
|
|
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
|
|
logger.info ('Fase 7.3 Generar la ISO')
|
|
btogIsoGenerator >>/tmp/fase7.out
|
|
echo ================= nati after fase 8.3; ls -la /opt/opengnsys/tftpboot/ogclient/
|
|
|
|
echoAndLog "OpenGnsys installation finished at $(date)"
|
|
"""
|