| 1 |
# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
import logging |
| 5 |
from pathlib import Path |
| 6 |
import subprocess |
| 7 |
import tempfile |
| 8 |
from typing import Literal, Optional, TextIO |
| 9 |
|
| 10 |
from vmtest.chroot import chroot_sh_cmd |
| 11 |
from vmtest.config import ( |
| 12 |
ARCHITECTURES, |
| 13 |
HOST_ARCHITECTURE, |
| 14 |
Architecture, |
| 15 |
_run_autoreconf, |
| 16 |
) |
| 17 |
|
| 18 |
logger = logging.getLogger(__name__) |
| 19 |
|
| 20 |
|
| 21 |
_ROOTFS_PACKAGES = ( |
| 22 |
# drgn build dependencies. |
| 23 |
"autoconf", |
| 24 |
"automake", |
| 25 |
"gcc", |
| 26 |
"git", |
| 27 |
"libdw-dev", |
| 28 |
"libelf-dev", |
| 29 |
"libjson-c-dev", |
| 30 |
"libkdumpfile-dev", |
| 31 |
"liblzma-dev", |
| 32 |
"libpcre2-dev", |
| 33 |
"libtool", |
| 34 |
"make", |
| 35 |
"pkgconf", |
| 36 |
"python3", |
| 37 |
"python3-dev", |
| 38 |
"python3-pip", |
| 39 |
"python3-setuptools", |
| 40 |
# Test dependencies. |
| 41 |
"btrfs-progs", |
| 42 |
"check", |
| 43 |
"e2fsprogs", |
| 44 |
"iproute2", |
| 45 |
"kexec-tools", |
| 46 |
"kmod", |
| 47 |
"python3-pyroute2", |
| 48 |
"python3-pytest", |
| 49 |
"python3-pytest-subtests", |
| 50 |
"zstd", |
| 51 |
) |
| 52 |
|
| 53 |
|
| 54 |
def build_rootfs( |
| 55 |
arch: Architecture, |
| 56 |
path: Path, |
| 57 |
*, |
| 58 |
btrfs: Literal["never", "always", "auto"] = "auto", |
| 59 |
) -> None: |
| 60 |
if path.exists(): |
| 61 |
logger.info("%s already exists", path) |
| 62 |
return |
| 63 |
|
| 64 |
packages = list(_ROOTFS_PACKAGES) |
| 65 |
if arch is HOST_ARCHITECTURE: |
| 66 |
for other_arch in ARCHITECTURES.values(): |
| 67 |
if other_arch is not arch: |
| 68 |
packages.append(f"gcc-{other_arch.debian_gcc_target}") |
| 69 |
|
| 70 |
# As of Debian 13, kexec-tools was not available for RISC-V: |
| 71 |
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1019254. We can install |
| 72 |
# it from testing for now. |
| 73 |
if arch.name == "riscv64": |
| 74 |
packages.remove("kexec-tools") |
| 75 |
riscv_kexec_tools_workaround = r""" |
| 76 |
cat > /etc/apt/sources.list.d/testing.list << "EOF" |
| 77 |
deb https://deb.debian.org/debian testing main |
| 78 |
EOF |
| 79 |
|
| 80 |
cat > /etc/apt/preferences.d/99testing << "EOF" |
| 81 |
Package: * |
| 82 |
Pin: release a=testing |
| 83 |
Pin-Priority: 100 |
| 84 |
EOF |
| 85 |
|
| 86 |
apt update |
| 87 |
apt install -y -t testing kexec-tools |
| 88 |
""" |
| 89 |
else: |
| 90 |
riscv_kexec_tools_workaround = "" |
| 91 |
|
| 92 |
logger.info("creating debootstrap rootfs %s", path) |
| 93 |
path.parent.mkdir(parents=True, exist_ok=True) |
| 94 |
with tempfile.TemporaryDirectory(dir=path.parent) as tmp_name: |
| 95 |
tmp_dir = Path(tmp_name) |
| 96 |
snapshot = False |
| 97 |
|
| 98 |
if btrfs != "never": |
| 99 |
try: |
| 100 |
import btrfsutil # type: ignore # No type hints available. |
| 101 |
|
| 102 |
btrfsutil.create_subvolume(tmp_dir / path.name) |
| 103 |
snapshot = True |
| 104 |
except (ImportError, OSError): |
| 105 |
if btrfs == "always": |
| 106 |
raise |
| 107 |
|
| 108 |
subprocess.check_call( |
| 109 |
[ |
| 110 |
"unshare", |
| 111 |
"--map-root-user", |
| 112 |
"--map-auto", |
| 113 |
"sh", |
| 114 |
"-c", |
| 115 |
rf""" |
| 116 |
set -e |
| 117 |
|
| 118 |
arch="$1" |
| 119 |
target="$2" |
| 120 |
packages="$3" |
| 121 |
|
| 122 |
# We're not really an LXC container, but this convinces debootstrap to skip |
| 123 |
# some operations that it can't do in a user namespace. |
| 124 |
container=lxc debootstrap --variant=minbase --foreign --include="$packages" --arch="$arch" stable "$target" |
| 125 |
# By default, apt drops privileges to the "_apt" user. unshare --map-auto can |
| 126 |
# deal with that, but it creates files that can't be accessed as the normal |
| 127 |
# user without unshare, which is annoying. |
| 128 |
echo 'APT::Sandbox::User "root";' > "$target/etc/apt/apt.conf.d/99nosandbox" |
| 129 |
{chroot_sh_cmd('"$target"')} ' |
| 130 |
set -e |
| 131 |
|
| 132 |
container=lxc /debootstrap/debootstrap --second-stage |
| 133 |
{riscv_kexec_tools_workaround} |
| 134 |
apt clean' |
| 135 |
""", |
| 136 |
"sh", |
| 137 |
arch.debian_arch, |
| 138 |
tmp_dir / path.name, |
| 139 |
",".join(packages), |
| 140 |
] |
| 141 |
) |
| 142 |
(tmp_dir / path.name).rename(path) |
| 143 |
logger.info("created debootstrap rootfs %s", path) |
| 144 |
|
| 145 |
if snapshot: |
| 146 |
snapshot_dir = path.parent / (path.name + ".pristine") |
| 147 |
btrfsutil.create_snapshot(path, snapshot_dir, read_only=True) |
| 148 |
logger.info("created snapshot %s", snapshot_dir) |
| 149 |
|
| 150 |
|
| 151 |
def _cross_compile_drgn( |
| 152 |
target: Architecture, directory: Path, *, outfile: Optional[TextIO] = None |
| 153 |
) -> None: |
| 154 |
assert HOST_ARCHITECTURE is not None |
| 155 |
host_rootfs = directory / HOST_ARCHITECTURE.name / "rootfs" |
| 156 |
target_rootfs = directory / target.name / "rootfs" |
| 157 |
logger.info("cross-compiling drgn in %s for %s", host_rootfs, target_rootfs) |
| 158 |
subprocess.check_call( |
| 159 |
[ |
| 160 |
"unshare", |
| 161 |
"--map-root-user", |
| 162 |
"--map-auto", |
| 163 |
"--mount", |
| 164 |
"sh", |
| 165 |
"-c", |
| 166 |
rf""" |
| 167 |
set -e |
| 168 |
|
| 169 |
mount --bind . "$1/mnt" |
| 170 |
mount --bind "$3" "$1/srv" |
| 171 |
{chroot_sh_cmd('"$1"')} ' |
| 172 |
set -e |
| 173 |
|
| 174 |
extension_suffix="$(/srv/usr/bin/python3-config --extension-suffix)" |
| 175 |
build_temp="/mnt/build/temp${{extension_suffix%.so}}" |
| 176 |
mkdir -p "$build_temp" |
| 177 |
cd "$build_temp" |
| 178 |
export CFLAGS="--sysroot=/srv" |
| 179 |
export LDFLAGS="--sysroot=/srv" |
| 180 |
export PYTHON_CPPFLAGS="$(/srv/usr/bin/python3-config --includes)" |
| 181 |
export PKG_CONFIG_LIBDIR="$(pkg-config --variable pc_path pkg-config | sed -r -e "s!(^|:)/!\1/srv/!g" -e "s/$1/$2/g")" |
| 182 |
export PKG_CONFIG_SYSROOT_DIR=/srv |
| 183 |
if [ ! -e Makefile ]; then |
| 184 |
/mnt/libdrgn/configure --host="$2" --with-sysroot=/srv --disable-static --disable-libdrgn --enable-python-extension |
| 185 |
fi |
| 186 |
make -j"$(nproc)" |
| 187 |
cp -v ".libs/_drgn.so" "/mnt/_drgn$extension_suffix" |
| 188 |
' sh "$2" "$4" |
| 189 |
""", |
| 190 |
"sh", |
| 191 |
host_rootfs, |
| 192 |
HOST_ARCHITECTURE.debian_gcc_target, |
| 193 |
target_rootfs, |
| 194 |
target.debian_gcc_target, |
| 195 |
], |
| 196 |
stdout=outfile, |
| 197 |
stderr=outfile, |
| 198 |
) |
| 199 |
|
| 200 |
|
| 201 |
def _build_drgn_in_rootfs(rootfs: Path, *, outfile: Optional[TextIO] = None) -> None: |
| 202 |
logger.info("building drgn using %s", rootfs) |
| 203 |
subprocess.check_call( |
| 204 |
[ |
| 205 |
"unshare", |
| 206 |
"--map-root-user", |
| 207 |
"--map-auto", |
| 208 |
"--mount", |
| 209 |
"sh", |
| 210 |
"-c", |
| 211 |
rf""" |
| 212 |
set -e |
| 213 |
|
| 214 |
mount --bind . "$1/mnt" |
| 215 |
{chroot_sh_cmd('"$1"')} 'cd /mnt && CONFIGURE_FLAGS=--enable-compiler-warnings=error python3 setup.py build_ext -i' |
| 216 |
""", |
| 217 |
"sh", |
| 218 |
rootfs, |
| 219 |
], |
| 220 |
stdout=outfile, |
| 221 |
stderr=outfile, |
| 222 |
) |
| 223 |
|
| 224 |
|
| 225 |
def build_drgn_for_arch( |
| 226 |
target: Architecture, directory: Path, *, outfile: Optional[TextIO] = None |
| 227 |
) -> None: |
| 228 |
_run_autoreconf() |
| 229 |
if target is not HOST_ARCHITECTURE and HOST_ARCHITECTURE is not None: |
| 230 |
return _cross_compile_drgn(target, directory, outfile=outfile) |
| 231 |
else: |
| 232 |
return _build_drgn_in_rootfs( |
| 233 |
directory / target.name / "rootfs", outfile=outfile |
| 234 |
) |
| 235 |
|
| 236 |
|
| 237 |
if __name__ == "__main__": |
| 238 |
import argparse |
| 239 |
|
| 240 |
logging.basicConfig( |
| 241 |
format="%(asctime)s:%(levelname)s:%(name)s:%(message)s", level=logging.INFO |
| 242 |
) |
| 243 |
|
| 244 |
parser = argparse.ArgumentParser( |
| 245 |
description="build root filesystems for vmtest. " |
| 246 |
"This requires debootstrap(8), qemu-user-static, and unprivileged user namespaces.", |
| 247 |
formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
| 248 |
) |
| 249 |
parser.add_argument( |
| 250 |
"-d", |
| 251 |
"--directory", |
| 252 |
metavar="DIR", |
| 253 |
type=Path, |
| 254 |
default="build/vmtest", |
| 255 |
help="directory for vmtest artifacts", |
| 256 |
) |
| 257 |
parser.add_argument( |
| 258 |
"--build-drgn", |
| 259 |
action="store_true", |
| 260 |
help="also build drgn in the current directory using the built rootfs", |
| 261 |
) |
| 262 |
parser.add_argument( |
| 263 |
"--btrfs", |
| 264 |
choices=["never", "always", "auto"], |
| 265 |
default="auto", |
| 266 |
help="make the rootfs a Btrfs subvolume and create a read-only snapshot", |
| 267 |
) |
| 268 |
parser.add_argument( |
| 269 |
"-a", |
| 270 |
"--architecture", |
| 271 |
dest="architectures", |
| 272 |
action="append", |
| 273 |
choices=["all", "foreign", *sorted(ARCHITECTURES)], |
| 274 |
default=argparse.SUPPRESS, |
| 275 |
help='architecture to build for, or "foreign" for all architectures other than the host architecture; may be given multiple times (default: foreign)', |
| 276 |
) |
| 277 |
args = parser.parse_args() |
| 278 |
|
| 279 |
if not hasattr(args, "architectures"): |
| 280 |
args.architectures = ["foreign"] |
| 281 |
architectures = [] |
| 282 |
for name in args.architectures: |
| 283 |
if name == "foreign": |
| 284 |
architectures.extend( |
| 285 |
[ |
| 286 |
arch |
| 287 |
for arch in ARCHITECTURES.values() |
| 288 |
if arch is not HOST_ARCHITECTURE |
| 289 |
] |
| 290 |
) |
| 291 |
elif name == "all": |
| 292 |
architectures.extend(ARCHITECTURES.values()) |
| 293 |
else: |
| 294 |
architectures.append(ARCHITECTURES[name]) |
| 295 |
|
| 296 |
# Create the host architecture rootfs first in case it's needed for |
| 297 |
# building drgn for other architectures. |
| 298 |
try: |
| 299 |
index = architectures.index(HOST_ARCHITECTURE) # type: ignore[arg-type] |
| 300 |
except ValueError: |
| 301 |
pass |
| 302 |
else: |
| 303 |
architectures.insert(0, architectures.pop(index)) |
| 304 |
|
| 305 |
for arch in architectures: |
| 306 |
dir = args.directory / arch.name / "rootfs" |
| 307 |
build_rootfs(arch, dir, btrfs=args.btrfs) |
| 308 |
if args.build_drgn: |
| 309 |
build_drgn_for_arch(arch, args.directory) |