oglive-builder/chroot-tasks.py

142 lines
5.6 KiB
Python

#!/usr/bin/python3
import argparse
import shutil
import os
import glob
import sys
import re
import subprocess
from boottools import utils, apt
config = utils.read_config ('mkoglive.cfg')
if config is None:
sys.exit (1)
debconf_settings = config['General'].get ('debconf_settings')
debconf_settings2 = config['General'].get ('debconf_settings2')
def _oghook_deactivate():
#Desactivamos el hook del oginitrd.img para evitar problemas, al final de este escripts se activará
os.rename ('/etc/initramfs-tools/hooks/oghooks', '/etc/initramfs-tools/oghooks')
def _oghook_activate():
#Activamos el hook del oginitrd.img
os.rename ('/etc/initramfs-tools/oghooks', '/etc/initramfs-tools/hooks/oghooks')
#def _mock_mtab():
# # Preparamos el mtab necesario para la instalacion correcta de paquetes.
# #echo "/dev/sda1 / ext4 rw,errors=remount-ro 0 0" > /etc/mtab ## nati: falla porque es un symlink a ../proc/self/mounts
# pass
#def _restore_mtab():
# # Dejamos el mtab como al principio
# #echo " " > /etc/mtab
# pass
def boottoolsSoftwareInstall (osarch, osrelease):
os.environ['LANGUAGE'] = 'C'
os.environ['LC_ALL'] = 'C'
os.environ['LANG'] = 'C'
os.environ['DEBIAN_FRONTEND'] = 'noninteractive'
stdout, _ = utils.run (['dpkg-divert', '--list'])
if not re.findall (r'local diversion of /sbin/initctl to /sbin/initctl.distrib', stdout):
utils.run (['dpkg-divert', '--local', '--rename', '--add', '/sbin/initctl'])
os.symlink ('/bin/true', '/sbin/initctl')
pkgs32 = []
if 'i386' != osarch:
utils.run (['dpkg', '--add-architecture', 'i386'])
pkgs32 = 'lib32gcc-s1 lib32stdc++6 lib32z1 libc6-i386'.split (' ') ## he cambiado lib32gcc1 por lib32gcc-s1 pero como queramos crear un oglive viejo, esto va a petar
_oghook_deactivate()
#_mock_mtab()
apt.install ([f'linux-image-{osrelease}', f'linux-headers-{osrelease}', f'linux-modules-{osrelease}', f'linux-modules-extra-{osrelease}', 'dkms', 'shim-signed', 'openssl'])
subprocess.run (['debconf-set-selections'], input=debconf_settings, text=True)
apt.install (['sshfs', 'kexec-tools'] + pkgs32, opts={'DPkg::Options::': '--force-confdef'}) ## hace falta --force-confdef para evitar un tema interactivo del /etc/ssh/ssh_config
pkgs = []
for section in config.options('Packages'):
pkgs += re.split (r'[ \n]', config['Packages'].get(section).strip())
apt.install (pkgs)
# Instalar módulos que algunos paquetes puedan tener pendientes de compilar.
stdout, _ = utils.run (['dkms', 'status'])
for l in stdout.strip().split ('\n'):
if not l: continue
print (f'l "{l}"')
mod, vers, status = l.split (',')
if 'added' in status:
print (f'dkms installing {mod} {vers}')
utils.run (['dkms', 'install', '-m', mod.strip(), '-v', vers.strip()])
_oghook_activate()
#_restore_mtab()
apt.clean()
apt.autoremove()
def boottoolsSoftwareCompile():
os.environ['LANGUAGE'] = os.environ['LC_ALL'] = os.environ['LANG'] = 'C'
os.chdir ('/tmp')
print ('ms-sys')
try: utils.run (['which', 'ms-sys'])
except:
utils.run (['wget', 'https://sourceforge.net/projects/ms-sys/files/latest/download', '-O', 'ms-sys.tar.gz'])
utils.run (['tar', '-xpzf', 'ms-sys.tar.gz'])
mssys_dir = subprocess.run (['tar tzf ms-sys.tar.gz |head -n 1'], shell=True, capture_output=True, text=True).stdout.strip()
print (f'mssys_dir "{mssys_dir}"')
os.chdir (mssys_dir)
utils.run (['make', 'install'])
os.chdir ('..')
print ('spartlnx')
try: utils.run (['which', 'spartl64.run'])
except:
utils.run (['wget', 'http://damien.guibouret.free.fr/savepart.zip'])
utils.run (['unzip', '-o', 'savepart.zip', '-d', '/sbin/', 'spartl64.run'])
utils.run (['mkdir', '/usr/share/doc/spartlnx'])
utils.run (['unzip', '-j', '-o', 'savepart.zip', '-d', '/usr/share/doc/spartlnx/', 'doc/en/*'])
if not os.path.exists ('python-libfdisk'):
print ('python-libfdisk')
apt.install (['python3-psutil', 'python3-dev', 'libfdisk-dev', 'python3-setuptools'])
utils.run (['git', 'clone', 'git://git.48k.eu/python-libfdisk'])
os.chdir ('python-libfdisk')
utils.run (['python3', 'setup.py', 'install'])
os.chdir ('..')
apt.remove (['python3-dev', 'python3-setuptools'])
## TODO restore os.environ and pwd
os.environ['LANGUAGE'] = os.environ['LC_ALL'] = os.environ['LANG'] = 'C'
os.chdir ('/tmp')
def boottoolsFsLocales():
subprocess.run (['debconf-set-selections'], input=debconf_settings2, text=True)
## despues de esto, debconf-get-selections devuelve los valores antiguos, no se por que...
utils.run (['dpkg-reconfigure', '--frontend', 'noninteractive', 'console-setup', 'locales'])
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')
utils.run (['mkinitramfs', '-o', f'/tmp/initrd.img-{osrelease}', '-v', osrelease])
shutil.copy (f'/boot/vmlinuz-{osrelease}', '/tmp/')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument ('--osarch', help='OS architecture', action='store', required=True)
parser.add_argument ('--osrelease', help='OS release', action='store', required=True)
args = parser.parse_args()
boottoolsSoftwareInstall (args.osarch, args.osrelease)
boottoolsSoftwareCompile()
boottoolsFsLocales()
boottoolsInitrdGenerate (args.osrelease)