[c6cf378] | 1 | import platform |
---|
| 2 | import os |
---|
| 3 | import time |
---|
| 4 | import logging |
---|
| 5 | import subprocess |
---|
[acc32a2] | 6 | import shutil |
---|
| 7 | import glob |
---|
[be59919] | 8 | import datetime |
---|
[c6cf378] | 9 | |
---|
[95a24ac] | 10 | from . import utils |
---|
[c6cf378] | 11 | |
---|
[1bd24c9] | 12 | logger = logging.getLogger ('boottools') |
---|
| 13 | |
---|
| 14 | def GetVar (osarch): |
---|
[ef212bc] | 15 | btdir = '/tmp/opengnsys/oglive_builder' |
---|
[c6cf378] | 16 | bttargetdir = '/var/lib/tftpboot/ogclient/' |
---|
| 17 | btrootfsimg = os.path.join (bttargetdir, 'ogclient.img') |
---|
| 18 | btrootfsmnt = os.path.join (bttargetdir, 'ogclientmount') |
---|
| 19 | btrootfsimglabel = 'ogClient' |
---|
| 20 | log_file = '/tmp/boot-tools_installation.log' |
---|
| 21 | versionboottools = 'ogLive' |
---|
| 22 | if 'i386' == osarch: |
---|
| 23 | btvirtualdisksize = '2000' # tamaño maximo limitado por schroot 2GB para 32 bits |
---|
| 24 | else: |
---|
| 25 | btvirtualdisksize = '3G' |
---|
| 26 | return btdir, bttargetdir, btrootfsimg, btrootfsmnt, btrootfsimglabel, log_file, versionboottools, btvirtualdisksize |
---|
| 27 | |
---|
[1bd24c9] | 28 | def GetOsInfo1 (type_client): |
---|
[c6cf378] | 29 | if 'precise' == type_client: # ogLive 1.0.4-rc2 basado en Ubuntu 12.04 LTS. |
---|
| 30 | osdistrib='ubuntu' |
---|
| 31 | oscodename='precise' |
---|
| 32 | osrelease='3.2.0-23-generic' |
---|
| 33 | osarch='i386' |
---|
| 34 | oshttp='http://es.archive.ubuntu.com/ubuntu/' |
---|
| 35 | elif 'wheezy' == type_client: # ogLive basado en Debian 7.3. |
---|
| 36 | osdistrib='debian' |
---|
| 37 | oscodename='wheezy' |
---|
| 38 | osrelease='3.2.0-4-i386' |
---|
| 39 | osarch='i386' |
---|
| 40 | oshttp='http://ftp.es.debian.org/debian/' |
---|
| 41 | elif 'quantal' == type_client: # ogLive 1.0.5-rc2 basado en Ubuntu 12.10 con Kernel descargado. |
---|
| 42 | osdistrib='ubuntu' |
---|
| 43 | oscodename='quantal' |
---|
| 44 | osrelease='3.7.6-030706-generic' |
---|
| 45 | osarch='i386' |
---|
| 46 | oshttp='http://es.archive.ubuntu.com/ubuntu/' |
---|
| 47 | elif 'raring' == type_client: # ogLive 1.0.5-rc3 basado en Ubuntu 13.04. |
---|
| 48 | osdistrib='ubuntu' |
---|
| 49 | oscodename='raring' |
---|
| 50 | osrelease='3.8.0-22-generic' |
---|
| 51 | osarch='i386' |
---|
| 52 | oshttp='http://es.archive.ubuntu.com/ubuntu/' |
---|
| 53 | elif 'trusty' == type_client: # ogLive 1.0.5-rc4 basado en Ubuntu 14.04. |
---|
| 54 | osdistrib='ubuntu' |
---|
| 55 | oscodename='trusty' |
---|
| 56 | osrelease='3.13.0-24-generic' |
---|
| 57 | osarch='i386' |
---|
| 58 | oshttp='http://es.archive.ubuntu.com/ubuntu/' |
---|
| 59 | elif 'vivid' == type_client: # ogLive 1.1.0-rc2 basado en Ubuntu 15.04. |
---|
| 60 | osdistrib='ubuntu' |
---|
| 61 | oscodename='vivid' |
---|
| 62 | osrelease='3.19.0-49-generic' |
---|
| 63 | osarch='i386' |
---|
| 64 | oshttp='http://es.archive.ubuntu.com/ubuntu/' |
---|
| 65 | elif 'wily' == type_client: # ogLive 1.1.0-rc1 basado en Ubuntu 15.10. |
---|
| 66 | osdistrib='ubuntu' |
---|
| 67 | oscodename='wily' |
---|
| 68 | osrelease='4.2.0-35-generic' |
---|
| 69 | osarch='i386' |
---|
| 70 | oshttp='http://es.archive.ubuntu.com/ubuntu/' |
---|
| 71 | elif 'xenial' == type_client or 'xenial-4.4' == type_client: # ogLive 1.1.0-rc4 basado en Ubuntu 16.04 y Kernel 4.4. |
---|
| 72 | osdistrib='ubuntu' |
---|
| 73 | oscodename='xenial' |
---|
| 74 | osrelease='4.4.0-34-generic' |
---|
| 75 | osarch='i386' |
---|
| 76 | oshttp='http://es.archive.ubuntu.com/ubuntu/' |
---|
| 77 | elif 'xenial-4.8' == type_client: # ogLive 1.1.0-rc5 basado en Ubuntu 16.04 y Kernel 4.8. |
---|
| 78 | osdistrib='ubuntu' |
---|
| 79 | oscodename='xenial' |
---|
| 80 | osrelease='4.8.0-39-generic' |
---|
| 81 | osarch='amd64' |
---|
| 82 | oshttp='http://es.archive.ubuntu.com/ubuntu/' |
---|
| 83 | elif 'xenial-4.13' == type_client: # ogLive 1.1.0-rc6 basado en Ubuntu 16.04 y Kernel 4.13. |
---|
| 84 | osdistrib='ubuntu' |
---|
| 85 | oscodename='xenial' |
---|
| 86 | osrelease='4.13.0-17-generic' |
---|
| 87 | osarch='amd64' |
---|
| 88 | oshttp='http://es.archive.ubuntu.com/ubuntu/' |
---|
| 89 | elif 'bionic' == type_client or 'bionic-4.15' == type_client: # ogLive 1.1.1-rc1 basado en Ubuntu 18.04 y Kernel 4.15. |
---|
| 90 | osdistrib='ubuntu' |
---|
| 91 | oscodename='bionic' |
---|
| 92 | osrelease='4.15.0-32-generic' |
---|
| 93 | osarch='amd64' |
---|
| 94 | oshttp='http://es.archive.ubuntu.com/ubuntu/' |
---|
| 95 | elif 'bionic-4.18' == type_client: # ogLive 1.1.1-rc3 basado en Ubuntu 18.04 y Kernel 4.18. |
---|
| 96 | osdistrib='ubuntu' |
---|
| 97 | oscodename='bionic' |
---|
| 98 | osrelease='4.18.0-20-generic' |
---|
| 99 | osarch='amd64' |
---|
| 100 | oshttp='http://es.archive.ubuntu.com/ubuntu/' |
---|
| 101 | elif 'bionic-5.0' == type_client: # ogLive 1.1.1-rc5 basado en Ubuntu 18.04 y Kernel 5.0. |
---|
| 102 | osdistrib='ubuntu' |
---|
| 103 | oscodename='bionic' |
---|
| 104 | osrelease='5.0.0-27-generic' |
---|
| 105 | osarch='amd64' |
---|
| 106 | oshttp='http://es.archive.ubuntu.com/ubuntu/' |
---|
| 107 | elif 'focal' == type_client: # ogLive 1.2.0-rc1 basado en Ubuntu 20.04 y Kernel 5.4. |
---|
| 108 | osdistrib='ubuntu' |
---|
| 109 | oscodename='focal' |
---|
| 110 | osrelease='5.4.0-42-generic' |
---|
| 111 | osarch='amd64' |
---|
| 112 | oshttp='http://es.archive.ubuntu.com/ubuntu/' |
---|
[ec2ab82] | 113 | elif 'noble' == type_client: # ogLive 1.2.0-rc1 basado en Ubuntu 24.04 y Kernel 6.8. |
---|
| 114 | osdistrib='ubuntu' |
---|
| 115 | oscodename='noble' |
---|
| 116 | osrelease='6.8.0-31-generic' |
---|
| 117 | osarch='amd64' |
---|
| 118 | oshttp='http://es.archive.ubuntu.com/ubuntu/' |
---|
[c6cf378] | 119 | elif 'host' == type_client: # ogLive basado en la distribución del servidor. |
---|
| 120 | osdistrib=platform.freedesktop_os_release()['NAME'] |
---|
| 121 | oscodename=platform.freedesktop_os_release()['VERSION_CODENAME'] |
---|
| 122 | osrelease=platform.uname()[2] |
---|
[95a24ac] | 123 | osarch, _=utils.run (['dpkg', '--print-architecture']) |
---|
[c6cf378] | 124 | oshttp='http://es.archive.ubuntu.com/ubuntu/' |
---|
| 125 | else: # Parámetro desconocido |
---|
[bb2d57b] | 126 | logger.error ('Parámetro no válido.') |
---|
| 127 | osdistrib=None |
---|
| 128 | oscodename=None |
---|
| 129 | osrelease=None |
---|
| 130 | osarch=None |
---|
| 131 | oshttp=None |
---|
[0371870] | 132 | return osdistrib, oscodename, osrelease, osarch, oshttp |
---|
[c6cf378] | 133 | |
---|
[1bd24c9] | 134 | def GetOsInfo2 (ogrepo_dir, versionboottools, oscodename, osrelease, osarch): |
---|
[c2fe8f7] | 135 | gitrelease, _ = utils.run (['git', '--git-dir', f'{ogrepo_dir}/.git', 'log', '--date', 'format:r%Y%m%d', '--format=%ad', '-1']) |
---|
[c6cf378] | 136 | nameisoclient ='-'.join ([versionboottools, oscodename, osrelease, osarch, gitrelease]) |
---|
| 137 | namehostclient = '-'.join ([versionboottools, oscodename, gitrelease]) |
---|
| 138 | return gitrelease, nameisoclient, namehostclient |
---|
| 139 | |
---|
[d1822ec] | 140 | def mkrootfs (btrootfsimg, btrootfsimglabel, btrootfsmnt, btvirtualdisksize, bttargetdir, osarch): |
---|
[c5e9f68] | 141 | logger.info (f'Creación y formateo del disco virtual "{btrootfsimg}" "{btvirtualdisksize}" MB') |
---|
[c6cf378] | 142 | |
---|
[95a24ac] | 143 | try: utils.umount (btrootfsmnt) |
---|
[acc32a2] | 144 | except: pass |
---|
[c6cf378] | 145 | |
---|
[95a24ac] | 146 | if (utils.is_mounted (btrootfsmnt)): |
---|
[c5e9f68] | 147 | raise Exception (f'failed to umount "{btrootfsmnt}"') |
---|
[c6cf378] | 148 | |
---|
[acc32a2] | 149 | try: os.makedirs (btrootfsmnt, exist_ok=True) |
---|
| 150 | except: |
---|
[c5e9f68] | 151 | raise Exception (f'Creando directorio "{btrootfsmnt}" : ERROR') |
---|
[c6cf378] | 152 | |
---|
[95a24ac] | 153 | try: utils.run (['chown', '-R', 'root:opengnsys', bttargetdir]) |
---|
[c2fe8f7] | 154 | except Exception as e: |
---|
| 155 | raise Exception (f'Failed to chown root:opengnsys "{btrootfsmnt}": {str(e)}') |
---|
[c6cf378] | 156 | |
---|
[c5e9f68] | 157 | logger.info (f'Creating disk image "{btrootfsimg}"') |
---|
[c6cf378] | 158 | if 'i386' == osarch: |
---|
[95a24ac] | 159 | try: utils.run (['dd', 'if=/dev/zero', f'of={btrootfsimg}', 'bs=1048576', f'count={btvirtualdisksize}']) |
---|
[c2fe8f7] | 160 | except Exception as e: |
---|
| 161 | raise Exception (f'Creando el disco virtual "{btrootfsimg}" con tamaño maxima "{btvirtualdisksize}" MB : ERROR: {str(e)}') |
---|
[c6cf378] | 162 | else: |
---|
[95a24ac] | 163 | try: utils.run (['qemu-img', 'create', btrootfsimg, btvirtualdisksize]) |
---|
[c2fe8f7] | 164 | except Exception as e: |
---|
| 165 | raise Exception (f'Creando el disco virtual "{btrootfsimg}" con tamaño maxima "{btvirtualdisksize}" MB : ERROR: {str(e)}') |
---|
[c6cf378] | 166 | |
---|
[acc32a2] | 167 | logger.debug ('losetup --find') |
---|
[95a24ac] | 168 | diskloop, _ = utils.run (['losetup', '--find']) |
---|
[c6cf378] | 169 | if not diskloop: |
---|
[acc32a2] | 170 | raise Exception ('no diskloop') |
---|
[c6cf378] | 171 | |
---|
[acc32a2] | 172 | logger.debug ('losetup attach') |
---|
[95a24ac] | 173 | try: utils.run (['losetup', '--partscan', diskloop, btrootfsimg]) |
---|
[acc32a2] | 174 | except: raise Exception ('losetup failed') |
---|
[c6cf378] | 175 | |
---|
[acc32a2] | 176 | logger.info ('Partitioning disk image') |
---|
[95a24ac] | 177 | stdout, _ = utils.run (['bash', '-c', f"echo $'n\np\n1\n\n\nt\n83\nw' |fdisk {diskloop}"]) |
---|
[c6cf378] | 178 | |
---|
[ec2ab82] | 179 | time.sleep (3) |
---|
[acc32a2] | 180 | logger.debug ('losetup --detach') |
---|
[95a24ac] | 181 | try: utils.run (['losetup', '--detach', diskloop]) |
---|
[c6cf378] | 182 | except: |
---|
[acc32a2] | 183 | raise Exception ('Liberando disco virtual despues del particionado: ERROR') |
---|
| 184 | |
---|
| 185 | logger.debug ('losetup --find') |
---|
[95a24ac] | 186 | partloop, _ = utils.run (['losetup', '--find']) |
---|
[acc32a2] | 187 | if not partloop: |
---|
| 188 | raise Exception ('no partloop') |
---|
[c6cf378] | 189 | |
---|
[acc32a2] | 190 | logger.debug ('losetup --offset attach') |
---|
[95a24ac] | 191 | utils.run (['losetup', '--offset', '32256', partloop, btrootfsimg]) |
---|
[acc32a2] | 192 | |
---|
| 193 | logger.info ('Creating ext4 filesystem') |
---|
[95a24ac] | 194 | try: utils.run (['mkfs.ext4', '-b', '4096', '-L', btrootfsimglabel, partloop]) |
---|
[c6cf378] | 195 | except: |
---|
[acc32a2] | 196 | raise Exception ('Formateando la particion principal del disco virtual: ERROR') |
---|
[c6cf378] | 197 | |
---|
[ec2ab82] | 198 | time.sleep (3) |
---|
[acc32a2] | 199 | logger.debug ('losetup --detach') |
---|
[95a24ac] | 200 | try: utils.run (['losetup', '--detach', partloop]) |
---|
[c6cf378] | 201 | except: |
---|
[acc32a2] | 202 | raise Exception ('Liberando la particion virtual despues del formateo: ERROR') |
---|
[c6cf378] | 203 | |
---|
[c5e9f68] | 204 | logger.info (f'"{btrootfsimg}" "{btvirtualdisksize}" MB : OK') |
---|
[acc32a2] | 205 | |
---|
[53310a8] | 206 | # debootstrap: Genera el sistema root base con debootstrap |
---|
[31e3607] | 207 | # trabaja sobre un rootfs ya montado |
---|
[53310a8] | 208 | def debootstrap (btrootfsimg, btrootfsmnt, osarch, oscodename, oshttp): |
---|
[acc32a2] | 209 | logger.info ('Iniciando la generación del sistema de archivos') |
---|
| 210 | |
---|
| 211 | logger.info ('debootstrapping system') |
---|
[c5e9f68] | 212 | logger.debug (f'debootstrap --arch="{osarch}" --components=main,universe "{oscodename}" "{btrootfsmnt}" "{oshttp}"') |
---|
[95a24ac] | 213 | try: utils.run (['debootstrap', f'--arch={osarch}', '--components=main,universe', oscodename, btrootfsmnt, oshttp]) |
---|
[c67b7f6] | 214 | except: raise Exception (f'debootstrap --arch="{osarch}" --components=main,universe "{oscodename}" "{btrootfsmnt}" "{oshttp}" : ha fallado!') |
---|
[acc32a2] | 215 | |
---|
[c5e9f68] | 216 | logger.info (f'debootstrap --arch="{osarch}" --components=main,universe "{oscodename}" "{btrootfsmnt}" "{oshttp}" : ok') |
---|
[acc32a2] | 217 | return 0 |
---|
[960b66e] | 218 | |
---|
[d1822ec] | 219 | def copy_og_files (builder, og_shared, og_engine, ogclientmount, osdistrib, oscodename): |
---|
| 220 | logger.info ('Iniciando la personalización con datos del repositorio') |
---|
[bd56977] | 221 | |
---|
| 222 | sources_list_in = f'{builder}/includes/etc/apt/sources.list.{osdistrib.lower()}' |
---|
| 223 | sources_list_out = f'{builder}/includes/etc/apt/sources.list' |
---|
| 224 | fdin = open (sources_list_in, 'r') |
---|
| 225 | fdout = open (sources_list_out, 'w') |
---|
| 226 | while True: |
---|
| 227 | l = fdin.readline() |
---|
| 228 | if not l: break |
---|
| 229 | fdout.write (l.replace ('OSCODENAME', oscodename)) |
---|
| 230 | fdin.close() |
---|
| 231 | fdout.close() |
---|
| 232 | |
---|
| 233 | subprocess.run (f'chmod -R 775 {builder}/includes/usr/bin/*', shell=True) |
---|
| 234 | |
---|
| 235 | os.makedirs (f'{ogclientmount}/opt/opengnsys/lib/engine/bin/', exist_ok=True) |
---|
| 236 | os.makedirs (f'{ogclientmount}/usr/local/etc', exist_ok=True) |
---|
| 237 | os.makedirs (f'{ogclientmount}/usr/local/lib', exist_ok=True) |
---|
| 238 | os.makedirs (f'{ogclientmount}/usr/local/plugins', exist_ok=True) |
---|
| 239 | |
---|
| 240 | subprocess.run (f'rsync -aH {builder}/includes/* {ogclientmount}/' , shell=True) |
---|
[c2fe8f7] | 241 | subprocess.run (f'rsync -aH {og_shared}/* {ogclientmount}/opt/opengnsys/' , shell=True) |
---|
| 242 | subprocess.run (f'rsync -aH {og_engine}/* {ogclientmount}/opt/opengnsys/lib/engine/bin/', shell=True) |
---|
[bd56977] | 243 | |
---|
| 244 | if not os.path.exists (f'{ogclientmount}/etc/pci.ids'): |
---|
| 245 | shutil.copy (f'{og_shared}/lib/pci.ids', f'{ogclientmount}/etc/') |
---|
| 246 | |
---|
| 247 | # Dependencias Qt para el Browser. |
---|
| 248 | subprocess.run (f'rsync -aH {og_shared}/etc/*.qmap {ogclientmount}/usr/local/etc', shell=True) |
---|
| 249 | subprocess.run (f'rsync -aH {og_shared}/lib/qtlib/* {ogclientmount}/usr/local/lib', shell=True) |
---|
| 250 | subprocess.run (f'rsync -aH {og_shared}/lib/fonts {ogclientmount}/usr/local/lib', shell=True) |
---|
| 251 | subprocess.run (f'rsync -aH {og_shared}/lib/qtplugins/* {ogclientmount}/usr/local/plugins', shell=True) |
---|
| 252 | |
---|
| 253 | # Browser y ogAdmClient. |
---|
| 254 | if os.path.exists (f'{og_shared}/bin/browser'): shutil.copy (f'{og_shared}/bin/browser', f'{ogclientmount}/bin/') |
---|
| 255 | if os.path.exists (f'{og_shared}/bin/ogAdmClient'): shutil.copy (f'{og_shared}/bin/ogAdmClient', f'{ogclientmount}/bin/') |
---|
| 256 | |
---|
[d1822ec] | 257 | def ssh_server (btrootfsmnt): |
---|
[960b66e] | 258 | if not os.path.exists ('/root/.ssh/id_rsa'): ## crea un par de claves en la VM, no en el chroot |
---|
[95a24ac] | 259 | utils.run (['ssh-keygen', '-q', '-f', '/root/.ssh/id_rsa', '-N', '']) |
---|
[960b66e] | 260 | |
---|
| 261 | logger.debug ('comprobando directorio .ssh del root') |
---|
[c5e9f68] | 262 | if not os.path.exists (f'{btrootfsmnt}/root/.ssh'): ## crea directorio dentro del chroot |
---|
[960b66e] | 263 | logger.debug ('creando directorio .ssh 600') |
---|
[c5e9f68] | 264 | os.mkdir (f'{btrootfsmnt}/root/.ssh') |
---|
| 265 | os.chmod (f'{btrootfsmnt}/root/.ssh', 0o700) |
---|
[960b66e] | 266 | |
---|
| 267 | logger.debug ('creando el fichero authorized_keys') ## crea archivo en el chroot |
---|
[c5e9f68] | 268 | if not os.path.exists (f'{btrootfsmnt}/root/.ssh/authorized_keys'): |
---|
| 269 | open (f'{btrootfsmnt}/root/.ssh/authorized_keys', 'w').close() |
---|
| 270 | os.chmod (f'{btrootfsmnt}/root/.ssh/authorized_keys', 0o600) |
---|
[960b66e] | 271 | |
---|
| 272 | logger.debug ('importando la clave publica del servidor OG') |
---|
| 273 | #cat /tmp/id_rsa.pub |
---|
| 274 | if os.path.exists ('/root/.ssh/id_rsa.pub'): ## coge la publica de la VM y la pone en el authorized_keys del chroot |
---|
| 275 | fdin = open ('/root/.ssh/id_rsa.pub', 'r') |
---|
[c5e9f68] | 276 | fdout = open (f'{btrootfsmnt}/root/.ssh/authorized_keys', 'a') |
---|
[960b66e] | 277 | while True: |
---|
| 278 | l = fdin.readline() |
---|
| 279 | if not l: break |
---|
| 280 | fdout.write (l) |
---|
| 281 | fdin.close() |
---|
| 282 | fdout.close() |
---|
| 283 | |
---|
| 284 | else: |
---|
| 285 | logger.error ('no key publica og') |
---|
| 286 | |
---|
[d1822ec] | 287 | def ssh_client (btrootfsmnt): |
---|
[c5e9f68] | 288 | if not os.path.exists (f'{btrootfsmnt}/root/.ssh/id_rsa'): |
---|
[95a24ac] | 289 | utils.run (['ssh-keygen', '-q', '-f', f'{btrootfsmnt}/root/.ssh/id_rsa', '-N', '']) ## crea un par de claves en el chroot |
---|
[960b66e] | 290 | |
---|
| 291 | #cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys ## coge la publica y se la autoriza a sí mismo |
---|
[c5e9f68] | 292 | fdin = open (f'{btrootfsmnt}//root/.ssh/id_rsa.pub', 'r') |
---|
| 293 | fdout = open (f'{btrootfsmnt}/root/.ssh/authorized_keys', 'a') |
---|
[960b66e] | 294 | while True: |
---|
| 295 | l = fdin.readline() |
---|
| 296 | if not l: break |
---|
| 297 | fdout.write (l) |
---|
| 298 | fdin.close() |
---|
| 299 | fdout.close() |
---|
| 300 | |
---|
| 301 | ## TODO: exportamos la publica a los repos |
---|
| 302 | #cp /root/.ssh/id_rsa.pub /tmp/rsa.ogclient.pub |
---|
| 303 | |
---|
[d1822ec] | 304 | #move_initrd genera un initrd. |
---|
| 305 | def move_initrd (bttargetdir, osrelease): |
---|
[be59919] | 306 | #echo "cp /tmp/*-${OSRELEASE} ${BTTARGETDIR}" |
---|
| 307 | #cp /tmp/*-${OSRELEASE} ${BTTARGETDIR} ## esto copia algo?? |
---|
[960b66e] | 308 | |
---|
| 309 | ## backup de oginitrd.img, oginitrd.img.sum, ogvmlinuz y ogvmlinuz.sum |
---|
[be59919] | 310 | now = datetime.datetime.now(datetime.timezone.utc).strftime ('%Y%m%d-%H%M%S%z') |
---|
[c5e9f68] | 311 | if os.path.exists (f'{bttargetdir}/oginitrd.img'): |
---|
| 312 | os.rename (f'{bttargetdir}/oginitrd.img' , f'{bttargetdir}/oginitrd.img.{now}') |
---|
| 313 | os.rename (f'{bttargetdir}/oginitrd.img.sum', f'{bttargetdir}/oginitrd.img.sum.{now}') |
---|
| 314 | if os.path.exists (f'{bttargetdir}/ogvmlinuz'): |
---|
| 315 | os.rename (f'{bttargetdir}/ogvmlinuz' , f'{bttargetdir}/ogvmlinuz.{now}') |
---|
| 316 | os.rename (f'{bttargetdir}/ogvmlinuz.sum', f'{bttargetdir}/ogvmlinuz.sum.{now}') |
---|
[960b66e] | 317 | |
---|
[c5e9f68] | 318 | shutil.copy (f'/tmp/initrd.img-{osrelease}', f'{bttargetdir}/oginitrd.img') |
---|
| 319 | shutil.copy (f'/tmp/vmlinuz-{osrelease}', f'{bttargetdir}/ogvmlinuz') |
---|
[960b66e] | 320 | |
---|
[95a24ac] | 321 | utils.write_md5 (f'{bttargetdir}/oginitrd.img') |
---|
| 322 | utils.write_md5 (f'{bttargetdir}/ogvmlinuz') |
---|
[960b66e] | 323 | |
---|
| 324 | #chmod -R 755 $BTTARGETDIR |
---|
[c5e9f68] | 325 | for f in glob.glob (f'{bttargetdir}/oginitrd*') + glob.glob (f'{bttargetdir}/vmlinuz*'): |
---|
[be59919] | 326 | os.chmod (f, 0o755) |
---|
[bf16103] | 327 | |
---|
[d1822ec] | 328 | #mksquashfs convierte el sistema root en sqfs |
---|
| 329 | def mksquashfs (bttargetdir, btrootfsmnt): |
---|
[bf16103] | 330 | logger.info ('Iniciando la creación del sistema de archivos en sqfs') |
---|
| 331 | |
---|
| 332 | # si ya existe un sqfs lo renombramos |
---|
[c5e9f68] | 333 | if os.path.exists (f'{bttargetdir}/ogclient.sqfs'): |
---|
[bf16103] | 334 | now = datetime.datetime.now(datetime.timezone.utc).strftime ('%Y%m%d-%H%M%S%z') |
---|
[c5e9f68] | 335 | os.rename (f'{bttargetdir}/ogclient.sqfs', f'{bttargetdir}/ogclient.sqfs.{now}') |
---|
[bf16103] | 336 | |
---|
[95a24ac] | 337 | utils.run (['mksquashfs', btrootfsmnt, f'{bttargetdir}/ogclient.sqfs']) |
---|
[c5e9f68] | 338 | os.chmod (f'{bttargetdir}/ogclient.sqfs', 0o744) |
---|
[bf16103] | 339 | |
---|
[95a24ac] | 340 | utils.write_md5 (f'{bttargetdir}/ogclient.sqfs') |
---|
[bf16103] | 341 | |
---|
[d1822ec] | 342 | # mkisofs genera la iso del cliente |
---|
| 343 | def mkisofs (pxepkg, isolinux_tpl, bttargetdir, nameisoclient): |
---|
[bf16103] | 344 | #Preparamos los gestores de arranque |
---|
| 345 | try: os.makedirs ('/tmp/iso/isolinux', exist_ok=True) |
---|
| 346 | except: raise |
---|
| 347 | |
---|
| 348 | subprocess.run (['cp -a /usr/lib/syslinux/* /tmp/iso/isolinux/'], shell=True) |
---|
| 349 | |
---|
| 350 | if 'gpxe' == pxepkg: |
---|
| 351 | subprocess.run (['cp -a /usr/share/gpxe/* /tmp/iso/isolinux'], shell=True) |
---|
| 352 | elif 'ipxe' == pxepkg: |
---|
| 353 | subprocess.run (['cp -a /usr/lib/ipxe/* /tmp/iso/isolinux'], shell=True) |
---|
| 354 | else: |
---|
[c5e9f68] | 355 | raise Exception (f'unknown pxepkg value "{pxepkg}"') |
---|
[bf16103] | 356 | |
---|
| 357 | # Si existe el fichero ISO, montarlo para extraer isolinux.bin. |
---|
[c5e9f68] | 358 | if os.path.exists (f'/tmp/iso/isolinux/{pxepkg}.iso'): |
---|
[bf16103] | 359 | os.mkdir ('/tmp/iso/isolinux/mount') |
---|
[95a24ac] | 360 | utils.run (['mount', '-o', 'loop', f'/tmp/iso/isolinux/{pxepkg}.iso', '/tmp/iso/isolinux/mount']) |
---|
[bf16103] | 361 | subprocess.run (['cp -a /tmp/iso/isolinux/mount/* /tmp/iso/isolinux'], shell=True) |
---|
[95a24ac] | 362 | utils.umount ('/tmp/iso/isolinux/mount') |
---|
[bf16103] | 363 | os.rmdir ('/tmp/iso/isolinux/mount') |
---|
[c5e9f68] | 364 | os.unlink (f'/tmp/iso/isolinux/{pxepkg}.iso') |
---|
[bf16103] | 365 | |
---|
| 366 | with open ('/tmp/iso/isolinux/isolinux.cfg', 'w') as fd: |
---|
| 367 | fd.write (isolinux_tpl.strip().replace('__NAMEISOCLIENT__', nameisoclient).replace('__PXEPKG__', pxepkg)) |
---|
| 368 | |
---|
| 369 | # preparamos el directorio boot-tools. |
---|
| 370 | if not os.path.exists ('/tmp/iso/ogclient'): |
---|
| 371 | os.mkdir ('/tmp/iso/ogclient') |
---|
[c5e9f68] | 372 | shutil.copy (f'{bttargetdir}/ogclient.sqfs', '/tmp/iso/ogclient/') |
---|
| 373 | shutil.copy (f'{bttargetdir}/ogclient.sqfs.sum', '/tmp/iso/ogclient/') |
---|
| 374 | shutil.copy (f'{bttargetdir}/ogvmlinuz', '/tmp/iso/ogclient/') |
---|
| 375 | shutil.copy (f'{bttargetdir}/ogvmlinuz.sum', '/tmp/iso/ogclient/') |
---|
| 376 | shutil.copy (f'{bttargetdir}/ogvmlinuz', '/tmp/iso/ogclient/linuxISO') |
---|
| 377 | shutil.copy (f'{bttargetdir}/ogvmlinuz.sum', '/tmp/iso/ogclient/linuxISO.sum') |
---|
| 378 | shutil.copy (f'{bttargetdir}/oginitrd.img', '/tmp/iso/ogclient/') |
---|
| 379 | shutil.copy (f'{bttargetdir}/oginitrd.img.sum', '/tmp/iso/ogclient/') |
---|
[bf16103] | 380 | #el ogclienteToISO debe tener una copia del ogvmlinuz como linuxISO |
---|
| 381 | #cp -prv /var/lib/tftpboot/ogclientToIso/* /tmp/iso/ogclient |
---|
| 382 | |
---|
| 383 | oldpwd = os.getcwd() |
---|
| 384 | os.chdir ('/tmp') |
---|
[bb2d57b] | 385 | logger.debug (f'mkisofs -quiet -V ogClient -o {nameisoclient}.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -J -no-emul-boot -boot-load-size 4 -boot-info-table /tmp/iso') |
---|
| 386 | utils.run (['mkisofs', '-quiet', '-V', 'ogClient', '-o', f'{nameisoclient}.iso', '-b', 'isolinux/isolinux.bin', '-c', 'isolinux/boot.cat', '-J', '-no-emul-boot', '-boot-load-size', '4', '-boot-info-table', '/tmp/iso']) |
---|
[bf16103] | 387 | |
---|
[95a24ac] | 388 | utils.write_md5 (f'{nameisoclient}.iso') |
---|
[bf16103] | 389 | os.chdir (oldpwd) |
---|
| 390 | |
---|
[f002c56] | 391 | shutil.move (f'/tmp/{nameisoclient}.iso' , f'/var/lib/tftpboot/ogclient/{nameisoclient}.iso') |
---|
| 392 | shutil.move (f'/tmp/{nameisoclient}.iso.sum', f'/var/lib/tftpboot/ogclient/{nameisoclient}.iso.sum') |
---|
[305e620] | 393 | |
---|
| 394 | #def __unused_boottoolsBootGraphics(): |
---|
| 395 | # find /tmp/opengnsys_installer/ -name .svn -type d -exec rm -fr {} \; 2>/dev/null; |
---|
| 396 | # apt-get -y install plymouth plymouth-theme-script |
---|
| 397 | # update-alternatives --install /lib/plymouth/themes/default.plymouth default.plymouth /lib/plymouth/themes/opengnsys/opengnsys.plymouth 100 |
---|
| 398 | # update-alternatives --set default.plymouth /lib/plymouth/themes/opengnsys/opengnsys.plymouth |
---|
| 399 | # mkdir -p /etc/initramfs-tools/conf.d |
---|
| 400 | # echo "FRAMEBUFFER=y" > /etc/initramfs-tools/conf.d/splash |
---|
[c2fe8f7] | 401 | |
---|
| 402 | |
---|
| 403 | ############### No usados en el instalador, solo en actualizaciones. |
---|
| 404 | |
---|
| 405 | # ogClientMount [str_program] |
---|
| 406 | #@brief Acceso al 2nd FS del cliente desde el Servidor Opengnsys |
---|
| 407 | #@param 1 Opciona: scripts o programa a ejecutar para automatizaciones |
---|
| 408 | #@return Si no hay parametros: login de acceso. |
---|
| 409 | #@return con un parametro: La salida del programa ejecutado |
---|
| 410 | #def ogClientMount(): |
---|
| 411 | # TODO comprobar que OGFILE y OGFILEMOUNT existe. |
---|
| 412 | # mount | grep $OGCLIENTFILE > /dev/null || mount $OGCLIENTFILE $OGCLIENTMOUNT -o loop,offset=32256 |
---|
| 413 | # mount | grep $OGCLIENTMOUNT/proc > /dev/null || mount --bind /proc $OGCLIENTMOUNT/proc |
---|
| 414 | # mount | grep $OGCLIENTMOUNT/sys > /dev/null || mount --bind /sys $OGCLIENTMOUNT/sys |
---|
| 415 | # mount | grep $OGCLIENTMOUNT/tmp > /dev/null || mount --bind /tmp $OGCLIENTMOUNT/tmp |
---|
| 416 | # mount | grep $OGCLIENTMOUNT/dev > /dev/null || mount --bind /dev $OGCLIENTMOUNT/dev |
---|
| 417 | # mount | grep $OGCLIENTMOUNT/dev/pts > /dev/null || mount --bind /dev/pts $OGCLIENTMOUNT/dev/pts |
---|
| 418 | # [ $# = 0 ] && $(chroot $OGCLIENTMOUNT /sbin/getty 38400 `tty`) |
---|
| 419 | # [ $# = 1 ] && chroot $OGCLIENTMOUNT $1 |
---|
| 420 | |
---|
| 421 | #@brief Desmonta el 2nd FS del cliente desde el Servidor Opengnsys |
---|
| 422 | #def ogClientUnmount(): |
---|
| 423 | # cd /tmp |
---|
| 424 | # echo "desmontando cliente espere" |
---|
| 425 | # sleep 5 |
---|
| 426 | # mount | grep $OGCLIENTMOUNT/dev > /dev/null && umount $OGCLIENTMOUNT/dev || ogClientUnmount |
---|
| 427 | # mount | grep $OGCLIENTMOUNT/dev/pts > /dev/null && umount $OGCLIENTMOUNT/dev/pts || ogClientUnmount |
---|
| 428 | # mount | grep $OGCLIENTMOUNT/proc > /dev/null && umount $OGCLIENTMOUNT/proc || ogClientUnmount |
---|
| 429 | # mount | grep $OGCLIENTMOUNT/sys > /dev/null && umount $OGCLIENTMOUNT/sys || ogClientUnmount |
---|
| 430 | # mount | grep $OGCLIENTMOUNT/tmp > /dev/null && umount $OGCLIENTMOUNT/tmp || ogClientUnmount |
---|
| 431 | # mount | grep $OGCLIENTMOUNT > /dev/null && umount $OGCLIENTMOUNT || ogClientUnmount |
---|