From 4a2be59d5dab6158006380c96834942598477956 Mon Sep 17 00:00:00 2001 From: Markus Hilger Date: Sun, 1 Nov 2026 23:22:38 +0100 Subject: [PATCH 1/2] Add container image sources to imgutil build For sites that already build images elsewhere, for example in CI from a Dockerfile, imgutil build -s also takes a container image: a SIF file, an OCI or Docker archive, an OCI layout directory, or a podman reference with its transport, such as docker:// or containers-storage:. Files and directories are recognized by their content, and any other source is a distribution as before. The image is extracted into the scratch directory, with podman or, for a SIF file, from its squashfs partition. It is then completed the way build does: the confluent initramfs configuration, the default package list with the package manager and repositories of the image, and an initramfs for every kernel. The result works with exec and pack like any built tree, and the build info names the image as its source. When the image already has every package of the list, the package manager is not run, so an image prepared in CI builds without network access. Missing packages are named before installing them. The architecture comes from the image, and a requested --arch is checked before extracting it. -r is refused because the image brings its own repositories. With --arch, the matching variant of an image for several architectures is used. Other than a local image, podman pulls or loads the image into a temporary storage that is removed afterwards, so the images and tags of the system stay as they are. Container base images are not bootable systems, so the build also unmasks the units they mask (systemd-remount-fs, getty.target, systemd-logind and others) and removes policy-rc.d, reporting each change, drops the boot entries kernel-install writes (they carry the command line of the build system, installing to disk writes its own), and with --selinux labels the tree with full contexts. Ubuntu images get ubuntu-minimal, which debootstrap installs but container bases lack. EL9, Ubuntu 24.04 and SUSE 16 or later images are supported, as long as imgutil has a package list for the release. Older releases and architectures other than x86_64 and aarch64 are refused. --- confluent_client/doc/man/imgutil.ronn | 52 ++- imgutil/imgutil | 453 ++++++++++++++++++++++---- imgutil/ubuntu/pkglist | 1 + 3 files changed, 442 insertions(+), 64 deletions(-) diff --git a/confluent_client/doc/man/imgutil.ronn b/confluent_client/doc/man/imgutil.ronn index 2705cefc..536b6b63 100644 --- a/confluent_client/doc/man/imgutil.ronn +++ b/confluent_client/doc/man/imgutil.ronn @@ -16,6 +16,10 @@ * `build`: Build a new diskless image from scratch in the specified scratch directory. + With a container image as `-s` source, the image is extracted to the + scratch directory instead, and completed with the confluent initramfs + configuration, the package list and a regenerated initramfs. This + builds images made elsewhere, for example in CI. * `exec`: Start the specified scratch directory as a container and optionally run a command inside it. @@ -43,8 +47,38 @@ * `-a`, `--addpackagelist` : A list of additional packages to include. May be specified multiple times. -* `-s`, `--source` : - Directory to pull installation from, typically a subdirectory of `/var/lib/confluent/distributions`. By default, the repositories for the build system are used. For Ubuntu, this is not supported; the build system repositories are always used. +* `-s`, `--source` |: + Directory to pull installation from, typically a subdirectory of `/var/lib/confluent/distributions`. By default, the repositories for the build system are used. For Ubuntu, a distribution directory is not supported, the build system repositories are always used. + + A container image made elsewhere, for example in CI, can be the source + instead: a SIF file as built by Apptainer or SingularityCE, an OCI or + Docker archive file as written by `podman save`, an OCI layout + directory with its `oci-layout` file, or a podman reference with its + transport, for example `docker://registry.example.com/hpc/compute:latest` + or `containers-storage:localhost/compute` for a local image. Local + images are looked up in the podman storage of the user running + imgutil, normally root. Any other image is pulled or loaded into a + temporary storage next to the scratch directory, which is removed + afterwards, so the images and tags of the system stay as they are. A + path containing a colon cannot be read by podman. A SIF file is + extracted from its squashfs partition, which needs + squashfs-tools 4.4 or newer but not podman. + + Any other directory or name is a distribution as above. The scratch + directory must be empty or not exist. Missing packages are installed + from the repositories of the image, so an image that already has every + package builds without network access. A package list entry with a + version, such as `curl=8.5.0-2ubuntu10`, always runs the package + manager and needs the repositories. Units that container images mask + are unmasked and `/usr/sbin/policy-rc.d` is removed, so the image boots + like an installed system. Package manager settings of the image are + kept, for example container base images leaving out documentation. The + architecture is detected from the image. With `--arch`, the matching + variant of an image for several architectures is used. `-r` is not supported, as the + image brings its own repositories. EL9, Ubuntu 24.04 and SUSE 16 or + later images are supported. With `--selinux`, the full SELinux context + of every file, including the SELinux user, is set from the policy of + the image. * `--arch` : Target architecture to build for (`x86_64` or `aarch64`). For Ubuntu, the @@ -130,6 +164,20 @@ Build an aarch64 Ubuntu diskless image on an amd64 Ubuntu host: imgutil build --arch aarch64 /tmp/myimage +Build from an image made from a Dockerfile and pack it: + + podman build -t localhost/myimage . + imgutil build -s containers-storage:localhost/myimage /tmp/myimage + imgutil pack /tmp/myimage myprofile + +Build from an image that CI pushed to a registry: + + imgutil build -s docker://registry.example.com/hpc/compute:latest /tmp/myimage + +Build from an Apptainer image: + + imgutil build -s /var/tmp/compute.sif /tmp/myimage + Execute a shell in an unpacked image: imgutil exec /tmp/myimage diff --git a/imgutil/imgutil b/imgutil/imgutil index 2422f30b..ed07112f 100644 --- a/imgutil/imgutil +++ b/imgutil/imgutil @@ -22,6 +22,7 @@ import stat import struct import subprocess import sys +import tarfile import tempfile import time try: @@ -654,6 +655,13 @@ class OsHandler(object): selinuxsupport = False # downloaded packages and repository metadata left by the package manager pkgcaches = () + # set by import, the target is an existing root that installs packages + # with its own package manager and repositories + imported = False + # shell snippets for has_packages: list every installed name once, then + # look up each package in it + pkgsetup = 'inst=$(rpm -qa --qf \'[%{PROVIDES}\\n]%{NAME}.%{ARCH}\\n\'); ' + pkgquery = 'printf "%s\\n" "$inst" | grep -xF -- "$p"' def __init__(self, name, version, arch, args): self.name = name @@ -765,6 +773,54 @@ class OsHandler(object): retpkgs.extend(self.list_packages(addpkglist)) return retpkgs + def copy_to_target(self, src, dst): + if self.imported: + # the image may already have it + copy_tree(src, dst) + return + try: + shutil.copytree(src, dst) + except Exception: + sys.stderr.write('Target directory already exists, cannot build\n') + raise + + def has_packages(self, args): + # an imported image that already has every package skips its package + # manager, and with it the need to reach its repositories + args.cmd = ['sh', '-c', self.pkgsetup + 'm=; for p; do ' + self.pkgquery + ' >/dev/null 2>&1 || m="$m $p"; done; ' + '[ -z "$m" ] || { echo "Not in the image, installing with the package manager:$m"; exit 1; }', + 'sh'] + self.packages() + if run_constrainedx(fancy_chroot, (args, self.targpath)): + return False + sys.stdout.write('All packages are already in the image, skipping package installation\n') + sys.stdout.flush() + return True + + def snapshot_initrds(self, initrdname): + # the initramfs of each kernel before the package manager runs + initrds = {} + moddir = os.path.join(self.targpath, 'lib/modules') + for kver in os.listdir(moddir) if os.path.isdir(moddir) else []: + initrd = os.path.join(self.targpath, initrdname.format(kver)) + initrds[kver] = os.path.getmtime(initrd) if os.path.exists(initrd) else None + return initrds + + def stale_kernels(self, initrds, initrdname): + # kernels of an imported image that need their initramfs built: it is + # missing, or unchanged since before the package manager ran and so + # without confluent. One the package manager built or updated has it. + # A modules directory left behind by a removed kernel is skipped. + moddir = os.path.join(self.targpath, 'lib/modules') + for kver in sorted(os.listdir(moddir)) if os.path.isdir(moddir) else []: + bootkernel = os.path.join(self.targpath, 'boot/vmlinuz-' + kver) + if not os.path.exists(bootkernel) and not os.path.exists(os.path.join(moddir, kver, 'vmlinuz')): + continue + initrd = os.path.join(self.targpath, initrdname.format(kver)) + if (os.path.exists(bootkernel) and os.path.exists(initrd) + and os.path.getmtime(initrd) != initrds.get(kver)): + continue + yield kver + class SuseHandler(OsHandler): pkgcaches = ('var/cache/zypp/*',) @@ -785,6 +841,9 @@ class SuseHandler(OsHandler): def add_pkglists(self): self.zyppargs.extend(self.list_packages()) + def packages(self): + return self.zyppargs + def set_source(self, sourcepath): self.sources.append('file://' + sourcepath) enterprise = False @@ -806,7 +865,7 @@ class SuseHandler(OsHandler): def prep_root(self, args): gpgkeys = [] mkdirp(self.targpath) - if not self.sources: + if not self.sources and not self.imported: gpgkeys = glob.glob('/usr/lib/rpm/gnupg/keys/*.asc') targzypp = os.path.join(self.targpath, 'etc/zypp') mkdirp(targzypp) @@ -861,13 +920,20 @@ class SuseHandler(OsHandler): mkdirp(os.path.join(self.targpath, 'etc/dracut.conf.d')) dracutdir = os.path.join(mydir, 'dracut') targdir = os.path.join(self.targpath, 'usr/lib/dracut/modules.d/97diskless') - shutil.copytree(dracutdir, targdir) + self.copy_to_target(dracutdir, targdir) with open(os.path.join(self.targpath, 'etc/dracut.conf.d/diskless.conf'), 'w') as dracutconf: dracutconf.write('compress=xz\nhostonly=no\ndracutmodules+=" diskless base terminfo "\n') cmd = ['chmod', 'a+x'] cmd.extend(glob.glob(os.path.join(targdir, '*'))) subprocess.check_call(cmd) - if self._interactive: + if self.imported: + initrds = self.snapshot_initrds('boot/initrd-{0}') + if not self.has_packages(args): + args.cmd = ['zypper'] if self._interactive else ['zypper', '-n'] + args.cmd += ['install'] + self.zyppargs + if run_constrainedx(fancy_chroot, (args, self.targpath)): + raise Exception('Package installation failed') + elif self._interactive: subprocess.check_call(self.zypper_cmd() + ['install'] + self.zyppargs) else: subprocess.check_call(self.zypper_cmd() + ['-n', 'install'] + self.zyppargs) @@ -881,10 +947,16 @@ class SuseHandler(OsHandler): keysign = 'usr/libexec/ssh/ssh-keysign' if not os.path.exists(os.path.join(self.targpath, keysign)): keysign = 'usr/lib/ssh/ssh-keysign' - with open(os.path.join(self.targpath, 'etc/permissions.local'), 'a') as permout: - permout.write( - '/{0} root:root 4711\n'.format(keysign) - ) + permline = '/{0} root:root 4711'.format(keysign) + # an imported image may have it already + permfile = os.path.join(self.targpath, 'etc/permissions.local') + havelines = [] + if os.path.exists(permfile): + with open(permfile) as permin: + havelines = permin.read().splitlines() + if permline not in havelines: + with open(permfile, 'a') as permout: + permout.write(permline + '\n') if os.path.exists(os.path.join(self.targpath, 'usr/bin/permctl')): args.cmd = ['permctl', '--system', '--set'] else: @@ -893,6 +965,12 @@ class SuseHandler(OsHandler): self.targpath)) + if self.imported: + for kver in self.stale_kernels(initrds, 'boot/initrd-{0}'): + args.cmd = ['dracut', '-f', '/boot/initrd-{0}'.format(kver), kver] + if run_constrainedx(fancy_chroot, (args, self.targpath)): + raise Exception('Building the initramfs of {0} failed'.format(kver)) + return if os.path.exists(os.path.join(self.targpath, 'sbin/mkinitrd')): args.cmd = ['mkinitrd'] else: @@ -941,9 +1019,16 @@ class DebHandler(OsHandler): needapt = 'Missing packages needed in target for capture, to add required packages: apt install ' + ' '.join(needpkgs) self.captureprereqs.append(needapt) + # a name can be provided by a renamed package, libuv1 by libuv1t64 + pkgsetup = ('inst=$(dpkg-query -W -f=\'${db:Status-Status} ${Package} ${Provides}\\n\' | ' + 'awk \'$1 == "installed" { for (i = 2; i <= NF; i++) { sub(/,$/, "", $i); print $i } }\'); ') + def add_pkglists(self): self.includepkgs.extend(self.list_packages()) + def packages(self): + return self.includepkgs + def set_target(self, targpath): self.targpath = targpath @@ -952,12 +1037,10 @@ class DebHandler(OsHandler): mydir = get_mydir(self.oscategory) srcdir = os.path.join(mydir, 'initramfs-tools') targdir = os.path.join(self.targpath, 'etc/initramfs-tools') - try: - shutil.copytree(srcdir, targdir) - except Exception: - sys.stderr.write('Target directory already exists, cannot build\n') - raise + self.copy_to_target(srcdir, targdir) os.chmod(os.path.join(targdir, 'hooks/confluent'), 0o755) + if self.imported: + return #cmd = ['debootstrap', '--include={0}'.format(','.join(self.includepkgs)), self.codename, self.targpath] if self.arch != os.uname().machine: cmd = ['debootstrap', '--arch={0}'.format(deb_arch_map.get(self.arch, self.arch)), self.codename, self.targpath] @@ -975,41 +1058,55 @@ class DebHandler(OsHandler): raise Exception('apt failed in image') def prep_root(self, args): - mkdirp(os.path.join(self.targpath, 'etc/apt')) - for aptdir in ('sources.list.d', 'trusted.gpg.d', 'keyrings'): - mkdirp(os.path.join(self.targpath, 'etc/apt', aptdir)) - shutil.copy('/etc/apt/sources.list', os.path.join(self.targpath, 'etc/apt/sources.list')) - for listfile in glob.glob('/etc/apt/sources.list.d/*'): - shutil.copy(listfile, os.path.join(self.targpath, listfile[1:])) - for keydir in ('/etc/apt/trusted.gpg.d', '/etc/apt/keyrings'): - for keyfile in glob.glob(os.path.join(keydir, '*')): - if not os.path.isfile(keyfile): - continue - tkeyfile = os.path.join( - self.targpath, os.path.relpath(keyfile, '/')) - mkdirp(os.path.dirname(tkeyfile)) - shutil.copy(keyfile, tkeyfile) - if os.path.exists('/etc/apt/trusted.gpg'): - trustedgpg = os.path.join(self.targpath, 'etc/apt/trusted.gpg') - mkdirp(os.path.dirname(trustedgpg)) - shutil.copy( - '/etc/apt/trusted.gpg', trustedgpg) - self._apt(args, ['apt-get', 'update']) - if self.includepkgs: - self._apt(args, ['apt-get', '-y', 'install'] + self.includepkgs) + # an imported image brings its own sources + if not self.imported: + mkdirp(os.path.join(self.targpath, 'etc/apt')) + for aptdir in ('sources.list.d', 'trusted.gpg.d', 'keyrings'): + mkdirp(os.path.join(self.targpath, 'etc/apt', aptdir)) + shutil.copy('/etc/apt/sources.list', os.path.join(self.targpath, 'etc/apt/sources.list')) + for listfile in glob.glob('/etc/apt/sources.list.d/*'): + shutil.copy(listfile, os.path.join(self.targpath, listfile[1:])) + for keydir in ('/etc/apt/trusted.gpg.d', '/etc/apt/keyrings'): + for keyfile in glob.glob(os.path.join(keydir, '*')): + if not os.path.isfile(keyfile): + continue + tkeyfile = os.path.join( + self.targpath, os.path.relpath(keyfile, '/')) + mkdirp(os.path.dirname(tkeyfile)) + shutil.copy(keyfile, tkeyfile) + if os.path.exists('/etc/apt/trusted.gpg'): + trustedgpg = os.path.join(self.targpath, 'etc/apt/trusted.gpg') + mkdirp(os.path.dirname(trustedgpg)) + shutil.copy( + '/etc/apt/trusted.gpg', trustedgpg) + if self.imported: + initrds = self.snapshot_initrds('boot/initrd.img-{0}') + if not (self.imported and self.has_packages(args)): + self._apt(args, ['apt-get', 'update']) + if self.includepkgs: + self._apt(args, ['apt-get', '-y', 'install'] + self.includepkgs) + if self.imported: + for kver in self.stale_kernels(initrds, 'boot/initrd.img-{0}'): + # update mode skips a kernel whose initramfs the image build removed + mode = '-u' if os.path.exists(os.path.join(self.targpath, 'boot/initrd.img-' + kver)) else '-c' + args.cmd = ['update-initramfs', mode, '-k', kver] + if run_constrainedx(fancy_chroot, (args, self.targpath)): + raise Exception('Regenerating the initramfs failed') servicefile = os.path.join( self.targpath, 'usr/lib/systemd/system/ssh.service') if os.path.exists(servicefile): targfile = os.path.join( self.targpath, 'etc/systemd/system/multi-user.target.wants/ssh.service') - if not os.path.exists(targfile): + if not os.path.lexists(targfile): + mkdirp(os.path.dirname(targfile)) os.symlink('/usr/lib/systemd/system/ssh.service', targfile) else: targfile = os.path.join( self.targpath, 'etc/systemd/system/multi-user.target.wants/sshd.service') - if not os.path.exists(targfile): + if not os.path.lexists(targfile): + mkdirp(os.path.dirname(targfile)) os.symlink('/usr/lib/systemd/system/sshd.service', targfile) @@ -1062,6 +1159,12 @@ class ElHandler(OsHandler): # python3 label files written at boot self.yumargs.extend(['selinux-policy-targeted', 'policycoreutils', 'libselinux-utils', 'python3']) + def packages(self): + # set_target puts the options before 'install', the packages follow + if 'install' in self.yumargs: + return self.yumargs[self.yumargs.index('install') + 1:] + return self.yumargs + def set_source(self, sourcepath): yumconfig = create_yumconf(sourcepath, self.addrepos) self.yumargs.extend( @@ -1073,30 +1176,49 @@ class ElHandler(OsHandler): def set_target(self, targpath): self.targpath = targpath - self.yumargs.extend( - ['--installroot={0}'.format(targpath), - '--releasever={0}'.format(self.version)]) - if self.arch != os.uname().machine: - self.yumargs.append('--forcearch={0}'.format(self.arch)) + if not self.imported: + self.yumargs.extend( + ['--installroot={0}'.format(targpath), + '--releasever={0}'.format(self.version)]) + if self.arch != os.uname().machine: + self.yumargs.append('--forcearch={0}'.format(self.arch)) self.yumargs.append('install') def prep_root(self, args): mkdirp(os.path.join(self.targpath, 'usr/lib/dracut/modules.d')) mkdirp(os.path.join(self.targpath, 'etc/dracut.conf.d')) - open(os.path.join(self.targpath, 'etc/resolv.conf'),'w').close() + # an imported image may link it to a path that only resolves in the image + resolv = os.path.join(self.targpath, 'etc/resolv.conf') + if not os.path.lexists(resolv): + open(resolv, 'w').close() mydir = get_mydir(self.oscategory) dracutdir = os.path.join(mydir, 'dracut') targdir = os.path.join(self.targpath, 'usr/lib/dracut/modules.d/97diskless') - shutil.copytree(dracutdir, targdir) + self.copy_to_target(dracutdir, targdir) with open(os.path.join(self.targpath, 'etc/dracut.conf.d/diskless.conf'), 'w') as dracutconf: dracutconf.write('compress=xz\nhostonly=no\ndracutmodules+=" diskless base terminfo "\n') cmd = ['chmod', 'a+x'] cmd.extend(glob.glob(os.path.join(targdir, '*'))) subprocess.check_call(cmd) - if self._interactive: - subprocess.check_call(['yum'] + self.yumargs) + yumcmd = ['yum'] if self._interactive else ['yum', '-y'] + if self.imported: + initrds = self.snapshot_initrds('boot/initramfs-{0}.img') + if not self.has_packages(args): + args.cmd = yumcmd + self.yumargs + if run_constrainedx(fancy_chroot, (args, self.targpath)): + raise Exception('Package installation failed') + # a container build may also not have put the kernel in /boot + for kver in self.stale_kernels(initrds, 'boot/initramfs-{0}.img'): + modkernel = 'lib/modules/{0}/vmlinuz'.format(kver) + if os.path.exists(os.path.join(self.targpath, modkernel)): + args.cmd = ['kernel-install', 'add', kver, '/' + modkernel] + else: + # only /boot has the kernel, so only the initramfs is missing + args.cmd = ['dracut', '-f', '/boot/initramfs-{0}.img'.format(kver), kver] + if run_constrainedx(fancy_chroot, (args, self.targpath)): + raise Exception('Installing kernel {0} failed'.format(kver)) else: - subprocess.check_call(['yum', '-y'] + self.yumargs) + subprocess.check_call(yumcmd + self.yumargs) # boot entries written in the chroot carry the kernel command line of # the build system, and installing to disk writes its own entries for loadent in glob.glob(os.path.join(self.targpath, 'boot/loader/entries/*.conf')): @@ -1365,7 +1487,8 @@ def main(): buildp.add_argument('-p', '--packagelist', help='Filename of package list to replace default pkglist', default='') buildp.add_argument('-a', '--addpackagelist', action='append', default=[], help='A list of additional packages to include, may be specified multiple times') - buildp.add_argument('-s', '--source', help='Directory to pull installation from, typically a subdirectory of /var/lib/confluent/distributions. By default, the repositories for the build system are used. For Ubuntu, this is not supported, the build system repositories are always used.') + buildp.add_argument('-s', '--source', help='Directory to pull installation from, typically a subdirectory of /var/lib/confluent/distributions. By default, the repositories for the build system are used. For Ubuntu, a distribution directory is not supported, the build system repositories are always used. ' + 'A container image made elsewhere, for example in CI, can be the source instead: a SIF file, an OCI or Docker archive, an OCI layout directory, or a podman reference with its transport such as docker://registry.example.com/compute:latest or containers-storage:localhost/compute') buildp.add_argument('--arch', default=None, help='Target architecture to build for (e.g. aarch64). When it differs from the build host architecture, statically linked qemu-user emulator registered in binfmt_misc with the F flag is required. aarch64 is emulated as a Neoverse N1 cpu (qemu 7.1 or later), set QEMU_CPU to choose another') buildp.add_argument('-y', '--non-interactive', help='Avoid prompting for confirmation', action='store_true') buildp.add_argument('--selinux', action='store_true', @@ -1401,7 +1524,10 @@ def main(): parser.print_usage() sys.exit(1) if args.subcommand == 'build': - build_root(args) + if args.source and container_source(args.source): + import_root(args) + else: + build_root(args) elif args.subcommand == 'capture': capture_remote(args) elif args.subcommand == 'unpack': @@ -1557,6 +1683,10 @@ qemu_arch_map = { 'aarch64': 'qemu-aarch64', 'x86_64': 'qemu-x86_64', } +elf_arch_map = { + 62: 'x86_64', + 183: 'aarch64', +} def set_qemu_cpu(targetarch): @@ -1806,19 +1936,18 @@ def fingerprint_host(args, hostpath='/'): return oshandler return oshandler -def build_root(args): - if args.scratchdir: - args.scratchdir = os.path.abspath(args.scratchdir) +def build_root(args, oshandler=None): check_root(args.scratchdir) - yumargs = ['yum', '--installroot={0}'.format(args.scratchdir)] - if args.source: - if '/' not in args.source and not os.path.exists(args.source): - args.source = os.path.join('/var/lib/confluent/distributions/', args.source) - oshandler = fingerprint_source(args.source, args) - if oshandler is not None: - oshandler.set_source(args.source) - else: - oshandler = fingerprint_host(args) + # an imported image comes with its handler + if oshandler is None: + if args.source: + if '/' not in args.source and not os.path.exists(args.source): + args.source = os.path.join('/var/lib/confluent/distributions/', args.source) + oshandler = fingerprint_source(args.source, args) + if oshandler is not None: + oshandler.set_source(args.source) + else: + oshandler = fingerprint_host(args) if oshandler is None: if args.source: sys.stderr.write( @@ -1829,10 +1958,10 @@ def build_root(args): 'Unable to recognize build system os\n') sys.exit(1) hostarch = os.uname().machine - if args.arch and args.arch != oshandler.arch: + if args.arch and uname_arch_map.get(args.arch, args.arch) != oshandler.arch: if args.source: sys.stderr.write( - 'Requested architecture {0} conflicts with source directory ' + 'Requested architecture {0} conflicts with source ' 'architecture {1}\n'.format(args.arch, oshandler.arch)) sys.exit(1) oshandler.set_arch(args.arch) @@ -1867,6 +1996,206 @@ def build_root(args): #if len(args) > 1: # pack_image(opts, args) +container_minversions = {'el': 9, 'ubuntu': 24.04, 'suse': 16} +# podman transports for a container image that is not a local file +container_transports = ('containers-storage:', 'dir:', 'docker://', 'docker-archive:', + 'oci:', 'oci-archive:') + +def container_supported(oscategory): + """Whether building from container images supports oscategory, for + example el10 or ubuntu24.04""" + match = re.match(r'^(el|ubuntu|suse)([0-9]+(\.[0-9]+)?)$', oscategory) + return bool(match) and float(match.group(2)) >= container_minversions[match.group(1)] + +def container_source(source): + """Whether the build source is a container image rather than a + distribution directory""" + if os.path.isdir(source): + return os.path.exists(os.path.join(source, 'oci-layout')) + return os.path.isfile(source) or source.startswith(container_transports) + +def get_sif_offset(sifname): + """Return the offset of the squashfs root partition of the SIF file + sifname, or None if it is not a SIF file""" + with open(sifname, 'rb') as sifin: + header = sifin.read(128) + if header[32:41] != b'SIF_MAGIC': + return None + # descriptor count, offset and total size, little endian like the + # rest of the format + dtotal, doffset, dsize = struct.unpack(' os.fstat(sifin.fileno()).st_size: + raise Exception('{0} is not a valid SIF file'.format(sifname)) + for idx in range(dtotal): + sifin.seek(doffset + idx * (dsize // dtotal)) + # data type, in use flag, three ids, then the data offset + dtype, used, offset = struct.unpack(' Date: Sun, 1 Nov 2026 23:22:38 +0100 Subject: [PATCH 2/2] Add imgutil build --containerfile to start an image for offline builds An image meant to build without network access must contain every package build checks for, and that list differs by distribution, version and architecture and changes between releases. build --containerfile writes a Containerfile, or with --apptainer an Apptainer definition file, generated from the same package lists, including -p, -a and --selinux, so it cannot drift from what build expects. The OS is that of the -s distribution or of the build system, as for a build. --arch names the matching build platform and --from replaces the default base image. -r is refused, the image installs from the repositories of its base image. The generated EL file installs full coreutils instead of the coreutils-single of container bases, and the Ubuntu file cleans the apt cache. --- confluent_client/doc/man/imgutil.ronn | 43 +++++++++- imgutil/imgutil | 116 ++++++++++++++++++++++++-- 2 files changed, 151 insertions(+), 8 deletions(-) diff --git a/confluent_client/doc/man/imgutil.ronn b/confluent_client/doc/man/imgutil.ronn index 536b6b63..ff014ec1 100644 --- a/confluent_client/doc/man/imgutil.ronn +++ b/confluent_client/doc/man/imgutil.ronn @@ -3,6 +3,7 @@ ## SYNOPSIS `imgutil` `build` [] +`imgutil` `build` `--containerfile` [] `imgutil` `exec` [] [...] `imgutil` `unpack` `imgutil` `pack` [] @@ -19,7 +20,9 @@ With a container image as `-s` source, the image is extracted to the scratch directory instead, and completed with the confluent initramfs configuration, the package list and a regenerated initramfs. This - builds images made elsewhere, for example in CI. + builds images made elsewhere, for example in CI. With + `--containerfile`, a Containerfile to start such an image from is + written instead. * `exec`: Start the specified scratch directory as a container and optionally run a command inside it. @@ -105,8 +108,30 @@ * `-v`, `--volume` : Directory to make available in the build environment. `-v /` will cause it to be mounted in image as `/run/external/`. `-v /:/run/root` will override the target to be `/run/root`. Something like `/var/lib/repository:-` will cause it to mount to the identical path inside the image. May be specified multiple times. +* `--containerfile`: + Write a Containerfile instead of building. It installs every package + `build` checks for, generated from the same package lists, so building + from the image needs no network access. Add site specific content and + build it in CI. The OS is that of the `-s` distribution or of the build + system, as for a build, and `--arch` names the platform, so the image + can be built on another architecture with emulation. `-p`, `-a` and + `--selinux` change the packages. Pass the same options to `build` + later, as the command shown in the file does. `-r` is not supported, + as the image installs from the repositories of its base image. EL9, + Ubuntu 24.04 and SUSE 16 or later are supported. + +* `--apptainer`: + With `--containerfile`, write an Apptainer definition file for building + a SIF instead. + +* `--from` : + With `--containerfile`, the base image to use instead of the default for + the OS (AlmaLinux for EL, the official Ubuntu image, openSUSE Leap for + SUSE). + * : - Directory to build new diskless root in. + Directory to build new diskless root in. With `--containerfile`, the file + to write, or `-` for standard output. ## EXEC OPTIONS @@ -178,6 +203,20 @@ Build from an Apptainer image: imgutil build -s /var/tmp/compute.sif /tmp/myimage +Prepare an image of the build system OS in CI that builds without network +access: + + imgutil build --containerfile Containerfile + podman build -t localhost/compute . + podman save -o compute.tar localhost/compute + imgutil build -s compute.tar /tmp/compute + +The same for EL10 as a SIF: + + imgutil build --containerfile --apptainer -s alma-10.0-x86_64 compute.def + apptainer build compute.sif compute.def + imgutil build -s compute.sif /tmp/compute + Execute a shell in an unpacked image: imgutil exec /tmp/myimage diff --git a/imgutil/imgutil b/imgutil/imgutil index ed07112f..869f4bcb 100644 --- a/imgutil/imgutil +++ b/imgutil/imgutil @@ -1498,7 +1498,14 @@ def main(): help='Directory to make available in the build environment. -v / will ' 'cause it to be mounted in image as /run/external/, -v /:/run/root ' 'will override the target to be /run/root, and something like /var/lib/repository:- will cause it to mount to the identical path inside the image', action='append') - buildp.add_argument('scratchdir', help='Directory to build new diskless root in') + buildp.add_argument('--containerfile', action='store_true', + help='Write a Containerfile instead of building, for an image with every package build checks for, ' + 'so that building from the image needs no repositories') + buildp.add_argument('--apptainer', action='store_true', + help='With --containerfile, write an Apptainer definition file for building a SIF instead') + buildp.add_argument('--from', dest='base', default=None, + help='With --containerfile, the base image to use instead of the default for the OS') + buildp.add_argument('scratchdir', help='Directory to build new diskless root in, or with --containerfile the file to write, - for standard output') execp = sps.add_parser('exec', help='Start specified scratch directory as container') execp.add_argument('-v', '--volume', help='Directory to make available in the build environment. -v / will ' @@ -1518,12 +1525,20 @@ def main(): capturep.add_argument('node', help='Node to capture image from') capturep.add_argument('profilename', help='Profile name for captured image') args = parser.parse_args() - if hasattr(args, 'scratchdir') and args.scratchdir: + if hasattr(args, 'scratchdir') and args.scratchdir and not ( + getattr(args, 'containerfile', False) and args.scratchdir == '-'): args.scratchdir = os.path.abspath(args.scratchdir) if not args or not args.subcommand: parser.print_usage() sys.exit(1) if args.subcommand == 'build': + if (args.apptainer or args.base) and not args.containerfile: + sys.stderr.write('--apptainer and --from require --containerfile\n') + sys.exit(1) + if args.containerfile and args.addrepos: + sys.stderr.write('-r is not supported with --containerfile, the image installs ' + 'from the repositories of its base image\n') + sys.exit(1) if args.source and container_source(args.source): import_root(args) else: @@ -1937,14 +1952,16 @@ def fingerprint_host(args, hostpath='/'): return oshandler def build_root(args, oshandler=None): - check_root(args.scratchdir) + if not args.containerfile: + check_root(args.scratchdir) # an imported image comes with its handler if oshandler is None: if args.source: if '/' not in args.source and not os.path.exists(args.source): args.source = os.path.join('/var/lib/confluent/distributions/', args.source) oshandler = fingerprint_source(args.source, args) - if oshandler is not None: + # a Containerfile installs from the repositories of its base image + if oshandler is not None and not args.containerfile: oshandler.set_source(args.source) else: oshandler = fingerprint_host(args) @@ -1965,6 +1982,9 @@ def build_root(args, oshandler=None): 'architecture {1}\n'.format(args.arch, oshandler.arch)) sys.exit(1) oshandler.set_arch(args.arch) + if args.containerfile: + write_containerfile(args, oshandler) + return if oshandler.arch != hostarch: if not oshandler.crossbuild: sys.stderr.write( @@ -2002,8 +2022,8 @@ container_transports = ('containers-storage:', 'dir:', 'docker://', 'docker-arch 'oci:', 'oci-archive:') def container_supported(oscategory): - """Whether building from container images supports oscategory, for - example el10 or ubuntu24.04""" + """Whether building from container images and generating a Containerfile + support oscategory, for example el10 or ubuntu24.04""" match = re.match(r'^(el|ubuntu|suse)([0-9]+(\.[0-9]+)?)$', oscategory) return bool(match) and float(match.group(2)) >= container_minversions[match.group(1)] @@ -2038,6 +2058,10 @@ def get_sif_offset(sifname): raise Exception('No squashfs partition found in {0}'.format(sifname)) def import_root(args): + if args.containerfile: + sys.stderr.write('--containerfile takes the OS from a distribution or the build system, ' + 'not from a container image\n') + sys.exit(1) if args.addrepos: sys.stderr.write('A container image brings its own repositories, -r is not supported\n') sys.exit(1) @@ -2196,6 +2220,86 @@ def _import_root(args): build_root(args, oshandler) +def write_containerfile(args, oshandler): + # A starting point for a CI built image that has every package build + # checks for, generated from the same package list + if os.path.isdir(args.scratchdir): + sys.stderr.write('{0} is a directory, give the file to write or - for ' + 'standard output\n'.format(args.scratchdir)) + sys.exit(1) + if oshandler.arch not in elf_arch_map.values(): + sys.stderr.write('Unsupported architecture {0}, use one of {1}\n'.format( + oshandler.arch, ', '.join(elf_arch_map.values()))) + sys.exit(1) + if not container_supported(oshandler.oscategory): + sys.stderr.write('Generating a Containerfile for {0} is not supported\n'.format(oshandler.oscategory)) + sys.exit(1) + if args.selinux and not oshandler.selinuxsupport: + sys.stderr.write('--selinux is not supported for {0} images\n'.format(oshandler.oscategory)) + sys.exit(1) + if not os.path.exists(oshandler.pkglist): + sys.stderr.write('No package list found for {0}\n'.format(oshandler.oscategory)) + sys.exit(1) + if oshandler.oscategory.startswith('el'): + baseimage = 'docker.io/library/almalinux:{0}'.format(oshandler.oscategory[2:]) + # container bases ship coreutils-single, a build gets full coreutils + install = ['dnf -y upgrade', 'dnf -y install --allowerasing coreutils {0}', 'dnf clean all'] + example = 'dnf -y install munge slurm-slurmd' + elif oshandler.oscategory.startswith('ubuntu'): + baseimage = 'docker.io/library/ubuntu:{0}'.format(oshandler.version) + install = ['apt-get update', 'DEBIAN_FRONTEND=noninteractive apt-get -y dist-upgrade', + 'DEBIAN_FRONTEND=noninteractive apt-get -y install {0}', 'apt-get clean', + 'rm -rf /var/lib/apt/lists/*'] + example = 'apt-get update && apt-get -y install munge slurmd' + else: + # Leap tags name the minor version + version = oshandler.version if '.' in oshandler.version else oshandler.version + '.0' + baseimage = 'registry.opensuse.org/opensuse/leap:{0}'.format(version) + install = ['zypper -n update', 'zypper -n install {0}', 'zypper clean -a'] + example = 'zypper -n install munge slurm-node' + oshandler.add_pkglists() + pkgs = list(dict.fromkeys(oshandler.packages())) + pkglines = [' '.join(pkgs[idx:idx + 8]) for idx in range(0, len(pkgs), 8)] + pkgtext = '\\\n ' + ' \\\n '.join(pkglines) + buildopts = '' + if args.selinux: + buildopts += ' --selinux' + if args.packagelist: + buildopts += ' -p {0}'.format(args.packagelist) + for plist in args.addpackagelist: + buildopts += ' -a {0}'.format(plist) + platform = 'linux/' + deb_arch_map[oshandler.arch] + lines = ['# Generated by imgutil build --containerfile for {0} {1}'.format( + oshandler.oscategory, oshandler.arch), + '# It installs every package imgutil build checks for, so building from the', + '# image does not run the package manager and needs no repositories.'] + if args.apptainer: + lines += ['Bootstrap: docker', 'From: {0}'.format(args.base or baseimage), '', '%post'] + lines += [' ' + cmd.format(pkgtext) for cmd in install] + lines += [' # Site specific content goes here, for example:', + ' # {0}'.format(example), + '', + '# Build the SIF, then the diskless root from it:', + '# apptainer build --arch {0} myimage.sif myimage.def'.format(platform.split('/')[-1]), + '# imgutil build{0} -s myimage.sif /var/lib/confluent/scratch/myimage'.format(buildopts)] + else: + lines += ['FROM --platform={0} {1}'.format(platform, args.base or baseimage), + 'RUN ' + ' && \\\n '.join(install).format(pkgtext), + '', + '# Site specific content goes here, for example:', + '# RUN {0}'.format(example), + '', + '# Build and save the image, then the diskless root from it:', + '# podman build --platform {0} -t localhost/myimage .'.format(platform), + '# podman save -o myimage.tar localhost/myimage', + '# imgutil build{0} -s myimage.tar /var/lib/confluent/scratch/myimage'.format(buildopts)] + text = ''.join(line + '\n' for line in lines) + if args.scratchdir == '-': + sys.stdout.write(text) + else: + with open(args.scratchdir, 'w') as outfile: + outfile.write(text) + def prep_decrypt(indir): indir = os.path.abspath(indir) pubdir = os.path.dirname(indir)