From b5a58ef77014997365314beb674da1ebb055b9a3 Mon Sep 17 00:00:00 2001 From: Markus Hilger Date: Tue, 3 Nov 2026 16:53:21 +0100 Subject: [PATCH] Install images without LVM to disk image2disk.py formatted the target filesystems, copied the image and ran fixup only in the branch that sets up LVM. For an image whose filesystems are all plain partitions it created the partitions and returned success without writing anything, and installimage then failed on the missing target. Run that part for both layouts. Plain partitions also need their swap in fstab by UUID instead of the localstorage-swap volume, and on EL9 and later root= and resume= refer to the new root and swap by UUID as well, since partition names are not stable with several disks. --- .../profiles/default/scripts/image2disk.py | 99 +++++++------- .../profiles/default/scripts/image2disk.py | 125 +++++++++--------- .../profiles/default/scripts/image2disk.py | 123 +++++++++-------- .../profiles/default/scripts/image2disk.py | 107 ++++++++------- 4 files changed, 238 insertions(+), 216 deletions(-) diff --git a/confluent_osdeploy/el7-diskless/profiles/default/scripts/image2disk.py b/confluent_osdeploy/el7-diskless/profiles/default/scripts/image2disk.py index eaccfb62..f7afc708 100644 --- a/confluent_osdeploy/el7-diskless/profiles/default/scripts/image2disk.py +++ b/confluent_osdeploy/el7-diskless/profiles/default/scripts/image2disk.py @@ -68,6 +68,8 @@ class PartedRunner(): else: return subprocess.run(command, stdout=subprocess.PIPE).stdout.decode('utf8') +swapdev = '/dev/mapper/localstorage-swap' + def fixup(rootdir, vols): devbymount = {} for vol in vols: @@ -92,7 +94,7 @@ def fixup(rootdir, vols): uuid = uuid.strip() entry[0] = 'UUID={}'.format(uuid) elif entry[2] == 'swap': - entry[0] = '/dev/mapper/localstorage-swap' + entry[0] = swapdev entry[0] = entry[0].ljust(42) entry[1] = entry[1].ljust(16) entry[3] = entry[3].ljust(28) @@ -196,6 +198,7 @@ def had_swap(): return False def install_to_disk(imgpath): + global swapdev lvmvols = {} deftotsize = 0 mintotsize = 0 @@ -287,7 +290,9 @@ def install_to_disk(imgpath): if end > lastsector: end = lastsector parted.run('mkpart swap {}s {}s'.format(curroffset, end)) - subprocess.check_call(['mkswap', get_partname(instdisk, volidx + 1)]) + swappart = get_partname(instdisk, volidx + 1) + subprocess.check_call(['mkswap', swappart]) + swapdev = 'UUID=' + subprocess.check_output(['blkid', '-s', 'UUID', '-o', 'value', swappart]).decode('utf8').strip() else: parted.run('mkpart lvm {}s 100%'.format(curroffset)) lvmpart = get_partname(instdisk, volidx + 1) @@ -327,30 +332,51 @@ def install_to_disk(imgpath): if swapsize: subprocess.check_call(['lvcreate', '-y', '-l', '{}'.format(swapsize // pesize), '-n', 'swap', 'localstorage']) subprocess.check_call(['mkswap', '/dev/localstorage/swap']) - 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 - 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: + 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 + 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() - lastprogress = 0.0 + if stillrunning != 0: + raise Exception("Error copying volume") + with subprocess.Popen(['sync']) as syncrun: + stillrunning = syncrun.poll() 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'] + progress = (vol['minsize'] - dirty) / vol['minsize'] if progress < lastprogress: progress = lastprogress if progress > 0.99: @@ -360,35 +386,14 @@ def install_to_disk(imgpath): 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() - subprocess.check_call(['umount', '/run/imginst/targ']) - for vol in allvols: - subprocess.check_call(['mount', vol['targetdisk'], '/run/imginst/targ/' + vol['mount']]) - fixup('/run/imginst/targ', allvols) + sys.stdout.write('\x1b[1K\rDone writing {0}'.format(vol['mount'])) + sys.stdout.write('\n') + sys.stdout.flush() + subprocess.check_call(['umount', '/run/imginst/targ']) + for vol in allvols: + subprocess.check_call(['mount', vol['targetdisk'], '/run/imginst/targ/' + vol['mount']]) + fixup('/run/imginst/targ', allvols) if __name__ == '__main__': diff --git a/confluent_osdeploy/el8-diskless/profiles/default/scripts/image2disk.py b/confluent_osdeploy/el8-diskless/profiles/default/scripts/image2disk.py index 65ca4fe6..98351309 100644 --- a/confluent_osdeploy/el8-diskless/profiles/default/scripts/image2disk.py +++ b/confluent_osdeploy/el8-diskless/profiles/default/scripts/image2disk.py @@ -74,6 +74,8 @@ class PartedRunner(): else: return subprocess.run(command, stdout=subprocess.PIPE).stdout.decode('utf8') +swapdev = '/dev/mapper/localstorage-swap' + def fixup(rootdir, vols): devbymount = {} for vol in vols: @@ -107,7 +109,7 @@ def fixup(rootdir, vols): uuid = uuid.strip() entry[0] = 'UUID={}'.format(uuid) elif entry[2] == 'swap': - entry[0] = '/dev/mapper/localstorage-swap' + entry[0] = swapdev entry[0] = entry[0].ljust(42) entry[1] = entry[1].ljust(16) entry[3] = entry[3].ljust(28) @@ -285,6 +287,7 @@ def had_swap(): return False def install_to_disk(imgpath): + global swapdev global bootuuid lvmvols = {} deftotsize = 0 @@ -376,7 +379,9 @@ def install_to_disk(imgpath): if end > lastsector: end = lastsector parted.run('mkpart swap {}s {}s'.format(curroffset, end)) - subprocess.check_call(['mkswap', get_partname(instdisk, volidx + 1)]) + swappart = get_partname(instdisk, volidx + 1) + subprocess.check_call(['mkswap', swappart]) + swapdev = 'UUID=' + subprocess.check_output(['blkid', '-s', 'UUID', '-o', 'value', swappart]).decode('utf8').strip() else: parted.run('mkpart lvm {}s 100%'.format(curroffset)) lvmpart = get_partname(instdisk, volidx + 1) @@ -416,32 +421,53 @@ def install_to_disk(imgpath): if swapsize: subprocess.check_call(['lvcreate', '-y', '-l', '{}'.format(swapsize // pesize), '-n', 'swap', 'localstorage']) subprocess.check_call(['mkswap', '/dev/localstorage/swap']) - 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: + 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() - lastprogress = 0.0 + if stillrunning != 0: + raise Exception("Error copying volume") + with subprocess.Popen(['sync']) as syncrun: + stillrunning = syncrun.poll() 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'] + progress = (vol['minsize'] - dirty) / vol['minsize'] if progress < lastprogress: progress = lastprogress if progress > 0.99: @@ -451,49 +477,28 @@ def install_to_disk(imgpath): 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': - tbootuuid = subprocess.check_output(['blkid', vol['targetdisk']]) - if b'UUID="' in tbootuuid: - bootuuid = tbootuuid.split(b'UUID="', 1)[1].split(b'"')[0].decode('utf8') + sys.stdout.write('\x1b[1K\rDone writing {0}'.format(vol['mount'])) + sys.stdout.write('\n') + sys.stdout.flush() + if vol['mount'] == '/boot': + 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']) - except subprocess.CalledProcessError: - print("Failed to unmount /run/imginst/targ, retrying") - time.sleep(1) - else: - break - for vol in allvols: - subprocess.check_call(['mount', vol['targetdisk'], '/run/imginst/targ/' + vol['mount']]) - fixup('/run/imginst/targ', allvols) + while True: + try: + subprocess.check_call(['umount', '/run/imginst/targ']) + except subprocess.CalledProcessError: + print("Failed to unmount /run/imginst/targ, retrying") + time.sleep(1) + else: + break + for vol in allvols: + subprocess.check_call(['mount', vol['targetdisk'], '/run/imginst/targ/' + vol['mount']]) + fixup('/run/imginst/targ', allvols) if __name__ == '__main__': diff --git a/confluent_osdeploy/el9-diskless/profiles/default/scripts/image2disk.py b/confluent_osdeploy/el9-diskless/profiles/default/scripts/image2disk.py index 8cab5e50..ae41d3fd 100644 --- a/confluent_osdeploy/el9-diskless/profiles/default/scripts/image2disk.py +++ b/confluent_osdeploy/el9-diskless/profiles/default/scripts/image2disk.py @@ -120,7 +120,10 @@ def fixup(rootdir, vols): uuid = uuid.strip() entry[0] = 'UUID={}'.format(uuid) elif entry[2] == 'swap': - entry[0] = '/dev/mapper/{}-swap'.format(vgname.replace('-', '--')) + if newswapdev and newswapdev.startswith('UUID='): + entry[0] = newswapdev + else: + entry[0] = '/dev/mapper/{}-swap'.format(vgname.replace('-', '--')) entry[0] = entry[0].ljust(42) entry[1] = entry[1].ljust(16) entry[3] = entry[3].ljust(28) @@ -482,6 +485,7 @@ def install_to_disk(imgpath): parted.run('mkpart swap {}s {}s'.format(curroffset, end)) newswapdev = get_partname(instdisk, volidx + 1) subprocess.check_call(['mkswap', newswapdev]) + newswapdev = 'UUID=' + subprocess.check_output(['blkid', '-s', 'UUID', '-o', 'value', newswapdev]).decode('utf8').strip() else: parted.run('mkpart lvm {}s 100%'.format(curroffset)) lvmpart = get_partname(instdisk, volidx + 1) @@ -554,32 +558,53 @@ def install_to_disk(imgpath): 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: + 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() - lastprogress = 0.0 + if stillrunning != 0: + raise Exception("Error copying volume") + with subprocess.Popen(['sync']) as syncrun: + stillrunning = syncrun.poll() 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'] + progress = (vol['minsize'] - dirty) / vol['minsize'] if progress < lastprogress: progress = lastprogress if progress > 0.99: @@ -589,49 +614,31 @@ def install_to_disk(imgpath): 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': - tbootuuid = subprocess.check_output(['blkid', vol['targetdisk']]) - if b'UUID="' in tbootuuid: - bootuuid = tbootuuid.split(b'UUID="', 1)[1].split(b'"')[0].decode('utf8') + sys.stdout.write('\x1b[1K\rDone writing {0}'.format(vol['mount'])) + sys.stdout.write('\n') + sys.stdout.flush() + if vol['mount'] == '/boot': + 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']]) - fixup('/run/imginst/targ', allvols) + 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=' + subprocess.check_output(['blkid', '-s', 'UUID', '-o', 'value', newrootdev]).decode('utf8').strip() + fixup('/run/imginst/targ', allvols) if __name__ == '__main__': diff --git a/confluent_osdeploy/ubuntu20.04-diskless/profiles/default/scripts/image2disk.py b/confluent_osdeploy/ubuntu20.04-diskless/profiles/default/scripts/image2disk.py index 3bca6302..ced4beb3 100644 --- a/confluent_osdeploy/ubuntu20.04-diskless/profiles/default/scripts/image2disk.py +++ b/confluent_osdeploy/ubuntu20.04-diskless/profiles/default/scripts/image2disk.py @@ -71,6 +71,8 @@ class PartedRunner(): else: return subprocess.run(command, stdout=subprocess.PIPE).stdout.decode('utf8') +swapdev = '/dev/mapper/localstorage-swap' + def fixup(rootdir, vols): devbymount = {} for vol in vols: @@ -95,7 +97,7 @@ def fixup(rootdir, vols): uuid = uuid.strip() entry[0] = 'UUID={}'.format(uuid) elif entry[2] == 'swap': - entry[0] = '/dev/mapper/localstorage-swap' + entry[0] = swapdev entry[0] = entry[0].ljust(42) entry[1] = entry[1].ljust(16) entry[3] = entry[3].ljust(28) @@ -227,6 +229,7 @@ def had_swap(): return False def install_to_disk(imgpath): + global swapdev global bootuuid lvmvols = {} deftotsize = 0 @@ -318,7 +321,9 @@ def install_to_disk(imgpath): if end > lastsector: end = lastsector parted.run('mkpart swap {}s {}s'.format(curroffset, end)) - subprocess.check_call(['mkswap', get_partname(instdisk, volidx + 1)]) + swappart = get_partname(instdisk, volidx + 1) + subprocess.check_call(['mkswap', swappart]) + swapdev = 'UUID=' + subprocess.check_output(['blkid', '-s', 'UUID', '-o', 'value', swappart]).decode('utf8').strip() else: parted.run('mkpart lvm {}s 100%'.format(curroffset)) lvmpart = get_partname(instdisk, volidx + 1) @@ -358,30 +363,51 @@ def install_to_disk(imgpath): if swapsize: subprocess.check_call(['lvcreate', '-y', '-l', '{}'.format(swapsize // pesize), '-n', 'swap', 'localstorage']) subprocess.check_call(['mkswap', '/dev/localstorage/swap']) - 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 - 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: + 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 + 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() - lastprogress = 0.0 + if stillrunning != 0: + raise Exception("Error copying volume") + with subprocess.Popen(['sync']) as syncrun: + stillrunning = syncrun.poll() 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'] + progress = (vol['minsize'] - dirty) / vol['minsize'] if progress < lastprogress: progress = lastprogress if progress > 0.99: @@ -391,43 +417,22 @@ def install_to_disk(imgpath): 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': - tbootuuid = subprocess.check_output(['blkid', vol['targetdisk']]) - if b'UUID="' in tbootuuid: - bootuuid = tbootuuid.split(b'UUID="', 1)[1].split(b'"')[0].decode('utf8') + sys.stdout.write('\x1b[1K\rDone writing {0}'.format(vol['mount'])) + sys.stdout.write('\n') + sys.stdout.flush() + if vol['mount'] == '/boot': + tbootuuid = subprocess.check_output(['blkid', vol['targetdisk']]) + if b'UUID="' in tbootuuid: + bootuuid = tbootuuid.split(b'UUID="', 1)[1].split(b'"')[0].decode('utf8') - subprocess.check_call(['umount', '/run/imginst/targ']) - for vol in allvols: - subprocess.check_call(['mount', vol['targetdisk'], '/run/imginst/targ/' + vol['mount']]) - fixup('/run/imginst/targ', allvols) + subprocess.check_call(['umount', '/run/imginst/targ']) + for vol in allvols: + subprocess.check_call(['mount', vol['targetdisk'], '/run/imginst/targ/' + vol['mount']]) + fixup('/run/imginst/targ', allvols) if __name__ == '__main__':