#!/usr/bin/python3 import glob import json import os import re import time import shutil import socket import stat import struct import sys import subprocess import traceback try: import yaml except ImportError: yaml = None bootuuid = None vgname = 'localstorage' oldvgname = None def split_mapper_name(devname): # device mapper joins VG and LV name with a hyphen and doubles hyphens # inside either name names = re.split(r'(? 256: grubcfg = gcfg else: with open(rgcfg, 'r') as gin: tgrubcfg = gin.read() tgrubcfg = tgrubcfg.split('\n') if tgrubcfg[0].startswith('search') and '--set=dev' in tgrubcfg[0]: searchparts = tgrubcfg[0].split() searchparts[-1] = bootuuid tgrubcfg[0] = ' '.join(searchparts) with open(rgcfg, 'w') as gout: for gcline in tgrubcfg: gout.write(gcline) gout.write('\n') try: subprocess.check_call(['chroot', rootdir, 'grub2-mkconfig', '-o', grubcfg]) except Exception as e: print(repr(e)) print(rootdir) print(grubcfg) time.sleep(86400) newroot = None with open('/etc/shadow') as shadowin: shents = shadowin.read().split('\n') for shent in shents: shent = shent.split(':') if not shent: continue if shent[0] == 'root' and shent[1] not in ('*', '!!', ''): newroot = shent[1] if newroot: shlines = None with open(os.path.join(rootdir, 'etc/shadow')) as oshadow: shlines = oshadow.read().split('\n') with open(os.path.join(rootdir, 'etc/shadow'), 'w') as oshadow: for line in shlines: if line.startswith('root:'): line = line.split(':') line[1] = newroot line = ':'.join(line) oshadow.write(line + '\n') partnum = None targblock = None for vol in vols: if vol['mount'] == '/boot/efi': targdev = vol['targetdisk'] partnum = re.search(r'(\d+)$', targdev).group(1) targblock = re.search(r'(.*)\d+$', targdev).group(1) if targblock: if targblock.endswith('p') and 'nvme' in targblock: targblock = targblock[:-1] shimpath = subprocess.check_output(['find', os.path.join(rootdir, 'boot/efi'), '-name', 'shimx64.efi']).decode('utf8').strip() shimpath = shimpath.replace(rootdir, '/').replace('/boot/efi', '').replace('//', '/').replace('/', '\\') subprocess.check_call(['efibootmgr', '-c', '-d', targblock, '-l', shimpath, '--part', partnum]) try: os.makedirs(os.path.join(rootdir, 'opt/confluent/bin')) except Exception: pass shutil.copy2('/opt/confluent/bin/apiclient', os.path.join(rootdir, 'opt/confluent/bin/apiclient')) #other network interfaces def had_swap(): if not os.path.exists('/etc/fstab'): # diskless source, assume swap return True with open('/etc/fstab') as tabfile: tabs = tabfile.read().split('\n') for tab in tabs: tab = tab.split() if len(tab) < 3: continue if tab[2] == 'swap': return True return False newrootdev = None newswapdev = None vgmap = None def install_to_disk(imgpath): global vgmap global bootuuid global newrootdev global newswapdev global vgname global oldvgname lvmvols = {} vgmap = {} deftotsize = 0 mintotsize = 0 deflvmsize = 0 minlvmsize = 0 biggestsize = 0 biggestfs = None plainvols = {} allvols = [] swapsize = 0 if had_swap(): with open('/proc/meminfo') as meminfo: swapsize = meminfo.read().split('\n')[0] swapsize = int(swapsize.split()[1]) if swapsize < 2097152: swapsize = swapsize * 2 elif swapsize > 8388608 and swapsize < 67108864: swapsize = swapsize * 0.5 elif swapsize >= 67108864: swapsize = 33554432 swapsize = int(swapsize * 1024) deftotsize = swapsize mintotsize = swapsize for fs in get_image_metadata(imgpath): allvols.append(fs) if fs['device'].startswith('/dev/mapper'): odevname = fs['device'].rsplit('/', 1)[-1] if split_mapper_name(odevname)[0] == vgname: # already in the volume group the install creates, as when # recapturing a node that was installed this way pass # if node has - then /dev/mapper will double up the hypen elif '_' in odevname and '-' in odevname.split('_', 1)[-1]: oldvgname = odevname.rsplit('-', 1)[0].replace('--', '-') osname = oldvgname.split('_')[0] nodename = socket.gethostname().split('.')[0] vgname = '{}_{}'.format(osname, nodename) elif '-' in odevname: # unique one vgmap[odevname] = split_mapper_name(odevname)[0] lvmvols[odevname] = fs continue lvmvols[odevname] = fs deflvmsize += fs['initsize'] minlvmsize += fs['minsize'] else: plainvols[int(re.search(r'(\d+)$', fs['device'])[0])] = fs if fs['initsize'] > biggestsize: biggestfs = fs biggestsize = fs['initsize'] deftotsize += fs['initsize'] mintotsize += fs['minsize'] with open('/tmp/installdisk') as diskin: instdisk = diskin.read() instdisk = '/dev/' + instdisk parted = PartedRunner(instdisk) dinfo = parted.run('unit s print', check=False) dinfo = dinfo.split('\n') sectors = 0 sectorsize = 0 for inf in dinfo: if inf.startswith('Disk {0}:'.format(instdisk)): _, sectors = inf.split(': ') sectors = int(sectors.replace('s', '')) if inf.startswith('Sector size (logical/physical):'): _, sectorsize = inf.split(':') sectorsize = sectorsize.split('/')[0] sectorsize = sectorsize.replace('B', '') sectorsize = int(sectorsize) # for now, only support resizing/growing the largest partition minexcsize = deftotsize - biggestfs['initsize'] mintotsize = deftotsize - biggestfs['initsize'] + biggestfs['minsize'] minsectors = mintotsize // sectorsize if sectors < (minsectors + 65536): raise Exception('Disk too small to fit image') # leave room for the 1MiB gap before the first partition, up to 1MiB of # rounding per partition and the backup GPT at the end of the disk lastsector = sectors - 2048 nparts = len(plainvols) if swapsize and not lvmvols: nparts += 1 biggestsectors = lastsector - 2048 * (nparts + 1) - (minexcsize // sectorsize) biggestsize = sectorsize * biggestsectors parted.run('mklabel gpt') curroffset = 2048 partnum = 0 for volidx in sorted(plainvols): vol = plainvols[volidx] if vol is not biggestfs: size = vol['initsize'] // sectorsize else: size = biggestsize // sectorsize size += 2047 - (size % 2048) end = curroffset + size if end > lastsector: end = lastsector parted.run('mkpart primary {}s {}s'.format(curroffset, end)) # parted numbers the new partitions in order, the source may have gaps partnum += 1 vol['targetdisk'] = get_partname(instdisk, partnum) if vol['mount'] == '/': newrootdev = vol['targetdisk'] curroffset += size + 1 if not lvmvols: if swapsize: swapsize = swapsize // sectorsize swapsize += 2047 - (swapsize % 2048) end = curroffset + swapsize if end > lastsector: end = lastsector parted.run('mkpart swap {}s {}s'.format(curroffset, end)) newswapdev = get_partname(instdisk, partnum + 1) subprocess.check_call(['mkswap', newswapdev]) newswapdev = 'UUID=' + get_uuid(newswapdev) else: parted.run('mkpart lvm {}s 100%'.format(curroffset)) lvmpart = get_partname(instdisk, partnum + 1) subprocess.check_call(['pvcreate', '-ff', '-y', lvmpart]) subprocess.check_call(['vgcreate', vgname, lvmpart]) vgroupmap = {} if yaml and vgmap and os.path.exists('/tmp/volumegroupmap.yml'): with open('/tmp/volumegroupmap.yml') as mapin: vgroupmap = yaml.safe_load(mapin) donedisks = {} for morevolname in vgmap: morevg = vgmap[morevolname] if morevg not in vgroupmap: raise Exception("No mapping defined to create volume group {}".format(morevg)) targdisk = vgroupmap[morevg] if targdisk not in donedisks: moreparted = PartedRunner(targdisk) moreparted.run('mklabel gpt') moreparted.run('mkpart lvm 0% 100%') morelvmpart = get_partname(targdisk, 1) subprocess.check_call(['pvcreate', '-ff', '-y', morelvmpart]) subprocess.check_call(['vgcreate', morevg, morelvmpart]) donedisks[targdisk] = 1 morelvname = split_mapper_name(morevolname)[1] subprocess.check_call(['lvcreate', '-L', '{}b'.format(lvmvols[morevolname]['initsize']), '-y', '-n', morelvname, morevg]) lvmvols[morevolname]['targetdisk'] = '/dev/{}/{}'.format(morevg, morelvname) vginfo = subprocess.check_output(['vgdisplay', vgname, '--units', 'b']).decode('utf8') vginfo = vginfo.split('\n') pesize = 0 pes = 0 for infline in vginfo: infline = infline.split() if len(infline) >= 3 and infline[:2] == ['PE', 'Size']: pesize = int(infline[2]) if len(infline) >= 5 and infline[:2] == ['Free', 'PE']: pes = int(infline[4]) takeaway = swapsize // pesize for volidx in lvmvols: if volidx in vgmap: # was handled previously continue vol = lvmvols[volidx] if vol is biggestfs: continue takeaway += vol['initsize'] // pesize takeaway += 1 biggestextents = pes - takeaway for volidx in lvmvols: vol = lvmvols[volidx] if volidx in vgmap: # was handled previously continue if vol is biggestfs: extents = biggestextents else: extents = vol['initsize'] // pesize extents += 1 if vol['mount'] == '/': lvname = 'root' else: lvname = vol['mount'].replace('/', '_') subprocess.check_call(['lvcreate', '-l', '{}'.format(extents), '-y', '-n', lvname, vgname]) vol['targetdisk'] = '/dev/{}/{}'.format(vgname, lvname) if vol['mount'] == '/': newrootdev = vol['targetdisk'] if swapsize: subprocess.check_call(['lvcreate', '-y', '-l', '{}'.format(swapsize // pesize), '-n', 'swap', vgname]) subprocess.check_call(['mkswap', '/dev/{}/swap'.format(vgname)]) newswapdev = '/dev/{}/swap'.format(vgname) os.makedirs('/run/imginst/targ') for vol in allvols: with open(vol['targetdisk'], 'wb') as partition: partition.write(b'\x00' * 1 * 1024 * 1024) subprocess.check_call(['mkfs.{}'.format(vol['filesystem']), vol['targetdisk']]) subprocess.check_call(['mount', vol['targetdisk'], '/run/imginst/targ']) source = vol['mount'].replace('/', '_') source = '/run/imginst/sources/' + source if not os.path.exists(source): source = '/run/imginst/sources/_' + vol['mount'] blankfsstat = os.statvfs('/run/imginst/targ') blankused = (blankfsstat.f_blocks - blankfsstat.f_bfree) * blankfsstat.f_bsize sys.stdout.write('\nWriting {0}: '.format(vol['mount'])) with subprocess.Popen(['cp', '-ax', source + '/.', '/run/imginst/targ']) as copier: stillrunning = copier.poll() lastprogress = 0.0 while stillrunning is None: currfsstat = os.statvfs('/run/imginst/targ') currused = (currfsstat.f_blocks - currfsstat.f_bfree) * currfsstat.f_bsize currused -= blankused with open('/proc/meminfo') as meminf: for line in meminf.read().split('\n'): if line.startswith('Dirty:'): _, dirty, _ = line.split() dirty = int(dirty) * 1024 progress = (currused - dirty) / vol['minsize'] if progress < lastprogress: progress = lastprogress if progress > 0.99: progress = 0.99 lastprogress = progress progress = progress * 100 sys.stdout.write('\x1b[1K\rWriting {0}: {1:3.2f}%'.format(vol['mount'], progress)) sys.stdout.flush() time.sleep(0.5) stillrunning = copier.poll() if stillrunning != 0: raise Exception("Error copying volume") with subprocess.Popen(['sync']) as syncrun: stillrunning = syncrun.poll() while stillrunning is None: with open('/proc/meminfo') as meminf: for line in meminf.read().split('\n'): if line.startswith('Dirty:'): _, dirty, _ = line.split() dirty = int(dirty) * 1024 progress = (vol['minsize'] - dirty) / vol['minsize'] if progress < lastprogress: progress = lastprogress if progress > 0.99: progress = 0.99 lastprogress = progress progress = progress * 100 sys.stdout.write('\x1b[1K\rWriting {0}: {1:3.2f}%'.format(vol['mount'], progress)) sys.stdout.flush() time.sleep(0.5) stillrunning = syncrun.poll() sys.stdout.write('\x1b[1K\rDone writing {0}'.format(vol['mount'])) sys.stdout.write('\n') sys.stdout.flush() if vol['mount'] == '/boot' or (vol['mount'] == '/' and not bootuuid): # /boot may live on / tbootuuid = subprocess.check_output(['blkid', vol['targetdisk']]) if b'UUID="' in tbootuuid: bootuuid = tbootuuid.split(b'UUID="', 1)[1].split(b'"')[0].decode('utf8') while True: try: subprocess.check_call(['umount', '/run/imginst/targ']) break except subprocess.CalledProcessError: print("Failed to unmount /run/imginst/targ, retrying") time.sleep(1) for vol in allvols: subprocess.check_call(['mount', vol['targetdisk'], '/run/imginst/targ/' + vol['mount']]) if newrootdev and not newrootdev.startswith('/dev/{}/'.format(vgname)): # partition names are not stable on systems with several disks newrootdev = 'UUID=' + get_uuid(newrootdev) fixup('/run/imginst/targ', allvols) if __name__ == '__main__': try: install_to_disk(os.environ['mountsrc']) except Exception: traceback.print_exc() time.sleep(86400) raise