diff --git a/README.md b/README.md index 7bbf0d1..22a9d3f 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,11 @@ # xcp-ng-build-env -This docker config and collection of supporting scripts allows for creating -a docker container to work on and build a XCP-ng package from an SRPM or from -a directory containing a `SOURCES/` and a `SPECS/` directory along with appropriate -RPM spec file and software sources. -It will build a Docker container with the right build environment (including some +This container config and collection of supporting scripts allows for +creating a container to work on and build a XCP-ng package from an +SRPM or from a directory containing a `SOURCES/` and a `SPECS/` +directory along with appropriate RPM spec file and software sources. + +It will build a container with the right build environment (including some useful tools). Depending on the parameters, it will either do everything automatically to build a given package, or just install build-dependencies and let you work manually from a shell @@ -13,14 +14,21 @@ want. ## Configuration -You'll need to install docker. Follow the instructions for your platform on -https://www.docker.com/ +You'll need to install docker or podman. Podman should be available +from your distro repositories, for Docker follow the instructions for +your platform on https://www.docker.com/ + +If you have both installed, docker will be used by default. If you +want to use a specific container runtime, set `XCPNG_OCI_RUNNER` to +the docker-compatible command to use (typically `podman` or `docker`). -## Building the docker image(s) +## Building the container image(s) -You need one docker image per target version of XCP-ng. +You need one container image per target version of XCP-ng. -Clone this repository (outside any docker container), then use `build.sh` to generate the docker images for the wanted releases of XCP-ng. +Clone this repository (outside any container), then use `build.sh` to +generate the images for the wanted releases of XCP-ng. +Note that Docker and Podman store container images separately. ``` Usage: ./build.sh {version_of_XCP_ng} @@ -115,7 +123,7 @@ git clone https://github.com/xcp-ng-rpms/xapi.git * `-b` / `--branch` allows to select which version of XCP-ng to work on (defaults to the latest known version if not specified). * `--no-exit` drops you to a shell after the build, instead of closing the container. Useful if the build fails and you need to debug. -* `--rm` destroys the container on exit. Helps preventing docker from using too much space on disk. You can still reclaim space afterwards by running `docker container prune` and `docker image prune` +* `--rm` destroys the container on exit. Helps preventing containers from using too much space on disk. You can still reclaim space afterwards by running `docker container prune` and `docker image prune` * `-v` / `--volume` (see *Mounting repos from outside the container* below) @@ -149,9 +157,7 @@ make If you'd like to develop using the tools on your host and preserve the changes to source and revision control but still use the container for building, you -can do using by using a docker volume. - -Once you have built your image you can run it with an extra argument to mount +can do using by mouning a volume in the container, using the `-v` option to mount a directory from your host to a suitable point inside the container. For example, if I clone some repos into a directory on my host, say `/work/code/`, then I can mount it inside the container as follows: diff --git a/build.sh b/build.sh index 9c6ed1e..1c6b21d 100755 --- a/build.sh +++ b/build.sh @@ -8,6 +8,23 @@ if [ -z "$1" ]; then exit fi +RUNNER="" +if [ -n "$XCPNG_OCI_RUNNER" ]; then + RUNNER="$XCPNG_OCI_RUNNER" +else + SUPPORTED_RUNNERS="docker podman" + for COMMAND in $SUPPORTED_RUNNERS; do + if command -v $COMMAND >/dev/null; then + RUNNER="$COMMAND" + break + fi + done + if [ -z "$RUNNER" ]; then + echo >&2 "cannot find a supported runner: $SUPPORTED_RUNNERS" + exit 1 + fi +fi + cd $(dirname "$0") CUSTOM_ARGS=() @@ -56,7 +73,7 @@ fi CUSTOM_ARGS+=( "--build-arg" "CUSTOM_BUILDER_UID=${CUSTOM_UID}" ) CUSTOM_ARGS+=( "--build-arg" "CUSTOM_BUILDER_GID=${CUSTOM_GID}" ) -docker build \ +"$RUNNER" build \ "${CUSTOM_ARGS[@]}" \ -t xcp-ng/xcp-ng-build-env:${1} \ --ulimit nofile=1024 \ diff --git a/run.py b/run.py index 30082d0..80b6011 100755 --- a/run.py +++ b/run.py @@ -1,7 +1,11 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -""" Thin wrapper around "docker run" which simplifies the creation of a build environment for XCP-ng packages. """ +""" +Thin wrapper around "docker run" or "podman run". + +Simplifies the creation of a build environment for XCP-ng packages. +""" import argparse import os @@ -16,6 +20,16 @@ DEFAULT_BRANCH = '8.3' DEFAULT_ULIMIT_NOFILE = 1024 +RUNNER = os.getenv("XCPNG_OCI_RUNNER") +if RUNNER is None: + SUPPORTED_RUNNERS = "docker podman" + for command in SUPPORTED_RUNNERS.split(): + if shutil.which(command): + RUNNER = command + break + else: + raise Exception(f"cannot find a supported runner: {SUPPORTED_RUNNERS}") + def make_mount_dir(): """ Make a randomly-named directory under SRPMS_MOUNT_ROOT. """ srpm_mount_dir = os.path.join(SRPMS_MOUNT_ROOT, str(uuid.uuid4())) @@ -32,6 +46,12 @@ def copy_srpms(srpm_mount_dir, srpms): srpm_name = os.path.basename(srpm) shutil.copyfile(srpm, os.path.join(srpm_mount_dir, srpm_name)) +def is_podman(runner): + if os.path.basename(runner) == "podman": + return True + if subprocess.getoutput(f"{runner} --version").startswith("podman "): + return True + return False def main(): """ Main entry point. """ @@ -93,7 +113,9 @@ def main(): args = parser.parse_args(sys.argv[1:]) - docker_args = ["docker", "run", "-i", "-t", "-u", "builder"] + docker_args = [RUNNER, "run", "-i", "-t", "-u", "builder"] + if is_podman(RUNNER): + docker_args += ["--userns=keep-id"] if os.uname()[4] != "x86_64": docker_args += ["--platform", "linux/amd64"] if args.rm: