| 1 | #!/usr/bin/python3 | 
|---|
| 2 |  | 
|---|
| 3 | import argparse | 
|---|
| 4 | import shutil | 
|---|
| 5 | import os | 
|---|
| 6 | import glob | 
|---|
| 7 | import sys | 
|---|
| 8 | import re | 
|---|
| 9 | import subprocess | 
|---|
| 10 |  | 
|---|
| 11 | from boottools import utils, apt | 
|---|
| 12 |  | 
|---|
| 13 | def _oghook_deactivate(): | 
|---|
| 14 | #Desactivamos el hook del oginitrd.img para evitar problemas, al final de este escripts se activará | 
|---|
| 15 | os.rename ('/etc/initramfs-tools/hooks/oghooks', '/etc/initramfs-tools/oghooks') | 
|---|
| 16 |  | 
|---|
| 17 | def _oghook_activate(): | 
|---|
| 18 | #Activamos el hook del oginitrd.img | 
|---|
| 19 | os.rename ('/etc/initramfs-tools/oghooks', '/etc/initramfs-tools/hooks/oghooks') | 
|---|
| 20 |  | 
|---|
| 21 | def boottoolsSoftwareInstall (osarch, osrelease): | 
|---|
| 22 | os.environ['LANGUAGE'] = 'C' | 
|---|
| 23 | os.environ['LC_ALL'] = 'C' | 
|---|
| 24 | os.environ['LANG'] = 'C' | 
|---|
| 25 | os.environ['DEBIAN_FRONTEND'] = 'noninteractive' | 
|---|
| 26 |  | 
|---|
| 27 | stdout, _ = utils.run (['dpkg-divert', '--list']) | 
|---|
| 28 | if not re.findall (r'local diversion of /sbin/initctl to /sbin/initctl.distrib', stdout): | 
|---|
| 29 | utils.run (['dpkg-divert', '--local', '--rename', '--add', '/sbin/initctl']) | 
|---|
| 30 | os.symlink ('/bin/true', '/sbin/initctl') | 
|---|
| 31 |  | 
|---|
| 32 | pkgs32 = [] | 
|---|
| 33 | if 'i386' != osarch: | 
|---|
| 34 | utils.run (['dpkg', '--add-architecture', 'i386']) | 
|---|
| 35 | pkgs32 = 'lib32gcc-s1 lib32stdc++6 lib32z1 libc6-i386'.split (' ') | 
|---|
| 36 |  | 
|---|
| 37 | _oghook_deactivate() | 
|---|
| 38 |  | 
|---|
| 39 | print ('boottoolsSoftwareInstall: debconf-set-selections', file=sys.stderr) | 
|---|
| 40 | subprocess.run (['debconf-set-selections'], input=debconf_settings, text=True) | 
|---|
| 41 | utils.run (['dpkg-reconfigure', '--frontend', 'noninteractive', 'console-setup', 'locales'])   ## XXX: despues de esto, debconf-get-selections devuelve los valores antiguos, no se por que... | 
|---|
| 42 |  | 
|---|
| 43 | pkgs = [] | 
|---|
| 44 | for section in config.options('Packages'): | 
|---|
| 45 | pkgs += re.split (r'[ \n]', config['Packages'].get(section).strip()) | 
|---|
| 46 |  | 
|---|
| 47 | apt.update() | 
|---|
| 48 | apt.upgrade() | 
|---|
| 49 |  | 
|---|
| 50 | pkgs = [f'linux-image-{osrelease}', f'linux-headers-{osrelease}', f'linux-modules-{osrelease}', f'linux-modules-extra-{osrelease}', 'dkms', 'shim-signed', 'openssl', 'sshfs', 'kexec-tools'] + pkgs32 + pkgs | 
|---|
| 51 | print (f'boottoolsSoftwareInstall: installing packages: {str(pkgs)}', file=sys.stderr) | 
|---|
| 52 | apt.install (pkgs, opts={'DPkg::Options::': '--force-confdef'})         ## --force-confdef is required to avoid an interactive question regarding /etc/ssh/ssh_config | 
|---|
| 53 |  | 
|---|
| 54 | # Instalar módulos que algunos paquetes puedan tener pendientes de compilar. | 
|---|
| 55 | print ('boottoolsSoftwareInstall: dkms', file=sys.stderr) | 
|---|
| 56 | stdout, _ = utils.run (['dkms', 'status']) | 
|---|
| 57 | for l in stdout.strip().split ('\n'): | 
|---|
| 58 | if not l: continue | 
|---|
| 59 | #print (f'l "{l}"') | 
|---|
| 60 | mod, vers, status = l.split (',') | 
|---|
| 61 | if 'added' in status: | 
|---|
| 62 | print (f'dkms installing {mod} {vers}') | 
|---|
| 63 | utils.run (['dkms', 'install', '-m', mod.strip(), '-v', vers.strip()]) | 
|---|
| 64 |  | 
|---|
| 65 | _oghook_activate() | 
|---|
| 66 | apt.clean() | 
|---|
| 67 | apt.autoremove() | 
|---|
| 68 |  | 
|---|
| 69 | def boottoolsSoftwareCompile(): | 
|---|
| 70 | env_language = os.environ['LANGUAGE'] | 
|---|
| 71 | env_lc_all   = os.environ['LC_ALL'] | 
|---|
| 72 | env_lang     = os.environ['LANG'] | 
|---|
| 73 | os.environ['LANGUAGE'] = os.environ['LC_ALL'] = os.environ['LANG'] = 'C' | 
|---|
| 74 | os.chdir ('/tmp') | 
|---|
| 75 |  | 
|---|
| 76 | print ('boottoolsSoftwareCompile: ms-sys', file=sys.stderr) | 
|---|
| 77 | try: utils.run (['which', 'ms-sys']) | 
|---|
| 78 | except: | 
|---|
| 79 | utils.run (['wget', '--quiet', 'https://sourceforge.net/projects/ms-sys/files/latest/download', '-O', 'ms-sys.tar.gz']) | 
|---|
| 80 | utils.run (['tar', '-xpzf', 'ms-sys.tar.gz']) | 
|---|
| 81 | mssys_dir = subprocess.run (['tar tzf ms-sys.tar.gz |head -n 1'], shell=True, capture_output=True, text=True).stdout.strip() | 
|---|
| 82 | print (f'mssys_dir "{mssys_dir}"') | 
|---|
| 83 | os.chdir (mssys_dir) | 
|---|
| 84 | utils.run (['make', 'install']) | 
|---|
| 85 | os.chdir ('..') | 
|---|
| 86 |  | 
|---|
| 87 | print ('boottoolsSoftwareCompile: spartlnx', file=sys.stderr) | 
|---|
| 88 | try: utils.run (['which', 'spartl64.run']) | 
|---|
| 89 | except: | 
|---|
| 90 | utils.run (['wget', '--tries=5', 'http://damien.guibouret.free.fr/savepart.zip']) | 
|---|
| 91 | utils.run (['unzip', '-o', 'savepart.zip', '-d', '/sbin/', 'spartl64.run', 'spartlnx.run']) | 
|---|
| 92 | utils.run (['mkdir', '/usr/share/doc/spartlnx']) | 
|---|
| 93 | utils.run (['unzip', '-j', '-o', 'savepart.zip', '-d', '/usr/share/doc/spartlnx/', 'doc/en/*']) | 
|---|
| 94 |  | 
|---|
| 95 | if not os.path.exists ('python-libfdisk'): | 
|---|
| 96 | print ('boottoolsSoftwareCompile: python-libfdisk', file=sys.stderr) | 
|---|
| 97 | apt.install (['python3-psutil', 'python3-dev', 'libfdisk-dev', 'python3-setuptools']) | 
|---|
| 98 | utils.run (['git', 'clone', 'https://ognproject.evlt.uma.es/gitea/48k.eu-mirror/python-libfdisk.git']) | 
|---|
| 99 | os.chdir ('python-libfdisk') | 
|---|
| 100 | utils.run (['python3', 'setup.py', 'install']) | 
|---|
| 101 | os.chdir ('..') | 
|---|
| 102 |  | 
|---|
| 103 | os.environ['LANGUAGE'] = env_language | 
|---|
| 104 | os.environ['LC_ALL']   = env_lc_all | 
|---|
| 105 | os.environ['LANG']     = env_lang | 
|---|
| 106 |  | 
|---|
| 107 | def updateCaCertificates(): | 
|---|
| 108 | print ('Updating CA trust Store', file=sys.stderr) | 
|---|
| 109 | utils.run (['update-ca-certificates']) | 
|---|
| 110 |  | 
|---|
| 111 | def boottoolsPythonModules(): | 
|---|
| 112 | utils.run (['pip3', 'install', 'pyblkid', '--break-system-packages']) | 
|---|
| 113 |  | 
|---|
| 114 | def boottoolsRemovePackages(): | 
|---|
| 115 | print ('boottoolsRemovePackages', file=sys.stderr) | 
|---|
| 116 | apt.remove (['python3-dev', 'python3-setuptools', 'python3-pip']) | 
|---|
| 117 |  | 
|---|
| 118 | ## have working DNS during the build | 
|---|
| 119 | def setup_resolvconf1(): | 
|---|
| 120 | print ('setup_resolvconf1', file=sys.stderr) | 
|---|
| 121 | os.unlink ('/etc/resolv.conf') | 
|---|
| 122 | f = open ('/etc/resolv.conf', 'w') | 
|---|
| 123 | f.write ('nameserver 8.8.8.8') | 
|---|
| 124 | f.close() | 
|---|
| 125 |  | 
|---|
| 126 | ## leave the final configuration for oglive | 
|---|
| 127 | def setup_resolvconf2(): | 
|---|
| 128 | print ('setup_resolvconf2', file=sys.stderr) | 
|---|
| 129 | os.unlink ('/etc/resolv.conf') | 
|---|
| 130 | os.symlink ('/run/resolvconf/resolv.conf', '/etc/resolv.conf') | 
|---|
| 131 |  | 
|---|
| 132 | def boottoolsInitrdGenerate (osrelease): | 
|---|
| 133 | print ('boottoolsInitrdGenerate', file=sys.stderr) | 
|---|
| 134 | for f in glob.glob ('/usr/lib/initramfs-tools/bin/*'): | 
|---|
| 135 | os.unlink (f) | 
|---|
| 136 | shutil.copy ('/bin/busybox', '/usr/lib/initramfs-tools/bin') | 
|---|
| 137 |  | 
|---|
| 138 | initrd_img = f'/tmp/initrd.img-{osrelease}' | 
|---|
| 139 |  | 
|---|
| 140 | os.chdir ('/tmp') | 
|---|
| 141 | utils.run (['mkinitramfs', '-o', initrd_img, osrelease]) | 
|---|
| 142 | shutil.copy (f'/boot/vmlinuz-{osrelease}', '/tmp/') | 
|---|
| 143 |  | 
|---|
| 144 | ## turn cpio-with-prepended-stuff into a regular cpio, see #975 | 
|---|
| 145 | utils.run (['unmkinitramfs', initrd_img, 'undone']) | 
|---|
| 146 | os.mkdir ('undone/merged') | 
|---|
| 147 | subprocess.run (['rsync -aH undone/early/* undone/main/* undone/merged/'], shell=True) | 
|---|
| 148 | shutil.rmtree ('undone/early') | 
|---|
| 149 | shutil.rmtree ('undone/main') | 
|---|
| 150 | os.chdir ('undone/merged/') | 
|---|
| 151 | subprocess.run ([f'find . |cpio -H newc -oa >{initrd_img}'], shell=True) | 
|---|
| 152 | os.chdir ('/tmp') | 
|---|
| 153 | shutil.rmtree ('undone') | 
|---|
| 154 |  | 
|---|
| 155 |  | 
|---|
| 156 | if __name__ == '__main__': | 
|---|
| 157 | parser = argparse.ArgumentParser() | 
|---|
| 158 | parser.add_argument ('--osarch',    help='OS architecture',            action='store', required=True) | 
|---|
| 159 | parser.add_argument ('--osrelease', help='OS release',                 action='store', required=True) | 
|---|
| 160 | parser.add_argument ('--config',    help='Path to configuration file', action='store') | 
|---|
| 161 | args = parser.parse_args() | 
|---|
| 162 |  | 
|---|
| 163 | config = utils.read_config (args.config or 'mkoglive.cfg') | 
|---|
| 164 | if config is None: | 
|---|
| 165 | sys.exit (1) | 
|---|
| 166 | debconf_settings = config['General'].get ('debconf_settings') | 
|---|
| 167 |  | 
|---|
| 168 | updateCaCertificates() | 
|---|
| 169 | setup_resolvconf1() | 
|---|
| 170 | boottoolsSoftwareInstall (args.osarch, args.osrelease) | 
|---|
| 171 | boottoolsSoftwareCompile() | 
|---|
| 172 | boottoolsPythonModules() | 
|---|
| 173 | boottoolsRemovePackages() | 
|---|
| 174 | setup_resolvconf2() | 
|---|
| 175 | boottoolsInitrdGenerate (args.osrelease) | 
|---|