Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion crossdev-stages/src/cli/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ pub async fn ensure_target(
defaults_root: &Utf8Path,
mirror: Option<&str>,
) -> Result<(target::Target, sandbox::Sandbox)> {
let (tgt, resolved_arch) = match ws.resolve_target(target_name) {
// If --arch is given, filter targets by that arch so we don't fall back to
// the most-recently-modified target of a foreign arch. This entry point
// seeds a target from a stage3, so the provider it may adopt is Gentoo's.
let gentoo = crate::provider::RootfsProvider::Gentoo.name();
let resolved = match arch_override {
Some(a) => ws.resolve_target_for_arch(target_name, a, gentoo),
None => ws.resolve_target(target_name),
};
let (tgt, resolved_arch) = match resolved {
Ok(td) => {
let tgt = target::Target::open(td)?;
let arch = arch_override
Expand Down
18 changes: 15 additions & 3 deletions crossdev-stages/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,12 +414,20 @@ pub fn unpack_tarball(
container.uidmaps(&uid_maps());
container.gidmaps(&gid_maps());

// Exclude /dev/* (contents) but keep the /dev/ directory itself:
// catalyst-built stage3 tarballs include /dev/console + /dev/null as
// real device nodes, which mknod() can't create inside a user-
// namespace container (CAP_MKNOD isn't granted). We need /dev/ to
// exist as an empty directory so the kernel's devtmpfs auto-mount
// (CONFIG_DEVTMPFS_MOUNT=y) has a mountpoint to attach to — without
// it, VFS mounts root but / has no /dev/console and PID 1 can't open
// stdio. `./dev/*` matches the contents but not the directory.
let cmd = format!(
"mkdir -p {dest} && \
tar --overwrite -xpf {stage} \
--xattrs-include='*.*' \
--numeric-owner \
--exclude='./dev' \
--exclude='./dev/*' \
-C {dest}",
stage = stage_in_container,
dest = dest_in_container,
Expand Down Expand Up @@ -462,10 +470,14 @@ pub fn pack_tarball(
_ => "-J", // xz
};

// Exclude *contents* of runtime mountpoint dirs but keep the dirs
// themselves — mirrors catalyst stage3 layout (empty /dev /proc /sys
// /run /tmp with the dirs present as mountpoints). See
// unpack_tarball above for why /dev needs to be a real directory.
let cmd = format!(
"tar -cp{compress} --xattrs --xattrs-include='*.*' --numeric-owner \
--exclude='./dev' --exclude='./proc' --exclude='./sys' \
--exclude='./run' --exclude='./tmp' \
--exclude='./dev/*' --exclude='./proc/*' --exclude='./sys/*' \
--exclude='./run/*' --exclude='./tmp/*' \
-f {tarball} -C {src} .",
compress = compress_flag,
tarball = tarball_in_container,
Expand Down
77 changes: 60 additions & 17 deletions crossdev-stages/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ use crate::sandbox::Sandbox;
use crate::target::Target;
use crate::workspace::Workspace;

/// Where `boards/<board>/` is bind-mounted read-only, so config the cross
/// prefix needs can be copied in from inside the container.
const BOARD_DIR_IN_CONTAINER: &str = "/.board-config";

fn project_root(boards_root: &Utf8Path) -> Utf8PathBuf {
boards_root.parent().unwrap_or(boards_root).to_path_buf()
}
Expand Down Expand Up @@ -432,32 +436,71 @@ fn default_deps(
defaults_root: &Utf8Path,
) -> Result<()> {
let board_dir = boards_root.join(&board.name);

// Two portage config roots see a board's target packages: the cross
// prefix's, which `{chost}-emerge` reads (PORTAGE_CONFIGROOT=/usr/<chost>),
// and the target's, which portage reads once the image boots. Board
// portage config goes into both, so it does not depend on which one is in
// play. The cross prefix is an overlay mount, so its half is written
// through the runner: a host-side write into sandbox/usr/<chost> lands
// under the mount, where nothing in the container can read it.
let cross_portage = format!("/usr/{}/etc/portage", board.chost());
let target_portage = target.dir.join("etc/portage");
let target_runner = sandbox
.runner_for_board(ws, &board.arch, board)?
.with_target(&target.dir)
.with_binpkgs(&board_binpkgs_dir(ws, board)?)
.with_extra_ro(&board_dir, BOARD_DIR_IN_CONTAINER);

let board_patches = board_dir.join("portage-patches");
if board_patches.is_dir() {
crate::sandbox::copy_tree(&board_patches, &target_portage.join("patches"))?;
target_runner.run(&format!(
"mkdir -p {cross_portage}/patches && \
cp -a {BOARD_DIR_IN_CONTAINER}/portage-patches/. {cross_portage}/patches/"
))?;
}

// package.provided cuts a dep chain off at an atom that does not
// cross-build for the target: virtual/udev pulling in
// sys-apps/systemd-utils on rv32-musl, say.
let board_provided = board_dir.join("package.provided");
if board_provided.is_file() {
let profile_dir = target_portage.join("profile");
std::fs::create_dir_all(&profile_dir)?;
std::fs::copy(&board_provided, profile_dir.join("package.provided"))?;
target_runner.run(&format!(
"mkdir -p {cross_portage}/profile && \
cp {BOARD_DIR_IN_CONTAINER}/package.provided \
{cross_portage}/profile/package.provided"
))?;
}

// Target packages: defaults UNION board extras MINUS board `-atom` lines.
let target_pkgs = crate::package_list::merge(
crate::package_list::read_required(&defaults_root.join("target-packages.txt"))?,
crate::package_list::read_optional(&board_dir.join("target-packages.txt"))?,
);
if !target_pkgs.is_empty() {
let board_use = board_dir.join("target-packages.use");
crate::package_list::write_accept_keywords(&target_pkgs, &target_portage)?;
crate::package_list::write_package_use(&board_use, &target_portage)?;
// The keyword a line asks for has to be written where the cross emerge
// will look for it: `{chost}-emerge` reads PORTAGE_CONFIGROOT=
// /usr/{chost}, not the sandbox's own /etc/portage. Without this a
// line like `sys-boot/syslinux **` gets its atom emerged and its
// keyword silently dropped, and the emerge fails on a masked package.
let target_runner = sandbox
.runner_for_board(ws, &board.arch, board)?
.with_target(&target.dir)
.with_binpkgs(&board_binpkgs_dir(ws, board)?);
// Written through the runner, not on the host: /usr/<chost> is an
// overlay mount and a host-side write lands under it, where nothing in
// the container can read it. This is where `{chost}-emerge` looks
// (PORTAGE_CONFIGROOT=/usr/<chost>), so a line like
// `sys-boot/syslinux **` gets its keyword here or nowhere.
if let Some(script) = crate::package_list::accept_keywords_script(
&target_pkgs,
&format!("/usr/{}/etc/portage", board.chost()),
) {
// will look for it. Without this a line like `sys-boot/syslinux **`
// gets its atom emerged and its keyword silently dropped, and the
// emerge fails on a masked package.
if let Some(script) =
crate::package_list::accept_keywords_script(&target_pkgs, &cross_portage)
{
target_runner.run(&script)?;
}
if board_use.is_file() {
target_runner.run(&format!(
"mkdir -p {cross_portage}/package.use && \
cp {BOARD_DIR_IN_CONTAINER}/target-packages.use \
{cross_portage}/package.use/board"
))?;
}

let portage = Portage::new(&target_runner);
portage.cross_emerge(&board.chost(), &crate::package_list::atoms(&target_pkgs))?;
Expand Down
19 changes: 15 additions & 4 deletions crossdev-stages/src/portage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ use crate::container::SandboxRunner;
use crate::error::Result;
use crate::stage::{all_llvm_targets, default_cflags, gentoo_arch, llvm_target};

/// Single-quote each atom so portage-style operators (`>=`, `<`, `=`) and
/// SLOT colons survive bash interpretation when the atom list is spliced
/// into a `format!`'d shell command. Atoms never contain single quotes.
fn shell_quote_atoms(atoms: &[&str]) -> String {
atoms
.iter()
.map(|a| format!("'{a}'"))
.collect::<Vec<_>>()
.join(" ")
}

/// Single supported llvm slot; dev-lang/rust-1.95.0 has `LLVM_COMPAT=( 22 )`.
pub const LLVM_SLOT: &str = "22";

Expand Down Expand Up @@ -297,7 +308,7 @@ impl<'a> Portage<'a> {
/// `--changed-use` so a board's `sandbox-packages.use` also applies to
/// packages that are already installed with different flags.
pub fn emerge(&self, packages: &[&str]) -> Result<()> {
let pkgs = packages.join(" ");
let pkgs = shell_quote_atoms(packages);
self.run_emerge(&format!("emerge -b -k --changed-use {pkgs}"))
}

Expand Down Expand Up @@ -332,13 +343,13 @@ impl<'a> Portage<'a> {
/// Cross-emerge packages into the target stage (mounted at `/target`).
/// Uses `{chost}-emerge` which crossdev installs.
pub fn cross_emerge(&self, chost: &str, packages: &[&str]) -> Result<()> {
let pkgs = packages.join(" ");
let pkgs = shell_quote_atoms(packages);
self.run_emerge(&format!("ROOT=/target {chost}-emerge -b -k {pkgs}"))
}

/// Cross-emerge with `USE=build` for bootstrapping (baselayout, portage).
pub fn cross_emerge_build(&self, chost: &str, packages: &[&str]) -> Result<()> {
let pkgs = packages.join(" ");
let pkgs = shell_quote_atoms(packages);
self.run_emerge(&format!(
"USE=build ROOT=/target {chost}-emerge -b -k {pkgs}"
))
Expand All @@ -348,7 +359,7 @@ impl<'a> Portage<'a> {
/// the crossdev prefix (`/usr/{chost}`) rather than `/target`.
/// Used for updating the cross-toolchain itself (gcc, binutils-libs, @system).
pub fn cross_emerge_crossdev(&self, chost: &str, packages: &[&str]) -> Result<()> {
let pkgs = packages.join(" ");
let pkgs = shell_quote_atoms(packages);
self.run_emerge(&format!("{chost}-emerge -b -k {pkgs}"))
}
}
Expand Down
13 changes: 9 additions & 4 deletions crossdev-stages/src/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,11 +395,16 @@ impl Sandbox {
} else {
""
};
// rustc has no upstream target for riscv32-unknown-linux-gnu; skip rust-std on rv32.
let rust_std_ex_pkg = if target_arch == "riscv32" {
""
} else {
" --ex-pkg sys-devel/rust-std"
};
runner.run(&format!(
"crossdev {chost} \
--gcc {gcc_ver} \
--ex-pkg sys-devel/clang-crossdev-wrappers \
--ex-pkg sys-devel/rust-std{grub_ex_pkg}"
--ex-pkg sys-devel/clang-crossdev-wrappers{rust_std_ex_pkg}{grub_ex_pkg}"
))?;

// Switch cross compiler to the installed slot.
Expand Down Expand Up @@ -819,7 +824,7 @@ fn install_overlay(

/// Recursively copy directory `src` into `dst`, overwriting files.
/// Symlinks and other special entries are rejected.
fn copy_tree(src: &Utf8Path, dst: &Utf8Path) -> Result<()> {
pub(crate) fn copy_tree(src: &Utf8Path, dst: &Utf8Path) -> Result<()> {
std::fs::create_dir_all(dst)?;
for entry in std::fs::read_dir(src)? {
let entry = entry?;
Expand All @@ -836,7 +841,7 @@ fn copy_tree(src: &Utf8Path, dst: &Utf8Path) -> Result<()> {
} else {
return Err(Error::CommandFailed {
code: 1,
reason: format!("unsupported entry in portage overlay (symlink?): {from}"),
reason: format!("unsupported entry in copied tree (symlink?): {from}"),
});
}
}
Expand Down
11 changes: 9 additions & 2 deletions crossdev-stages/src/stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ pub fn gentoo_arch(arch: &str) -> Result<&'static str> {

/// Return the full CHOST triple for a given arch.
/// x86 32-bit uses the "pc" vendor (Gentoo convention); others use "unknown".
/// riscv32 uses musl libc (no Gentoo glibc rv32 stage3 exists).
pub fn chost_for_arch(arch: &str) -> Result<String> {
let vendor = if is_ix86(arch) { "pc" } else { "unknown" };
Ok(format!("{arch}-{vendor}-linux-gnu"))
let libc = if arch == "riscv32" { "musl" } else { "gnu" };
Ok(format!("{arch}-{vendor}-linux-{libc}"))
}

fn is_ix86(arch: &str) -> bool {
Expand All @@ -37,6 +39,7 @@ fn is_ix86(arch: &str) -> bool {
/// Return the Gentoo profile path for an OS arch string.
pub fn gentoo_profile(arch: &str) -> Result<&'static str> {
Ok(match arch {
"riscv32" => "default/linux/riscv/23.0/rv32/ilp32/musl",
a if a.starts_with("riscv") => "default/linux/riscv/23.0/rv64/lp64d",
"x86_64" => "default/linux/amd64/23.0",
"aarch64" => "default/linux/arm64/23.0",
Expand Down Expand Up @@ -70,6 +73,7 @@ pub fn default_cflags(arch: &str) -> &'static str {
"x86_64" => "-O3 -march=x86-64 -pipe",
"aarch64" => "-O3 -pipe",
"riscv64" => "-O3 -march=rv64gc -pipe",
"riscv32" => "-Os -march=rv32ima_zicsr_zifencei -mabi=ilp32 -mcmodel=medlow -pipe",
_ => "-O3 -pipe",
}
}
Expand Down Expand Up @@ -114,9 +118,12 @@ pub fn llvm_target(arch: &str) -> Option<&'static str> {
}

/// Return the stage3 variant name for `client.get()`.
/// For riscv64 this is "rv64_lp64d-openrc".
/// For riscv64 this is "rv64_lp64d-openrc"; for riscv32 (soft-float, musl) "rv32_ilp32_musl-openrc".
/// Note: Gentoo autobuilds may not publish an rv32 musl stage3; riscv32 boards bootstrap
/// from scratch and don't rely on `setup`.
pub fn stage_variant(arch: &str) -> &'static str {
match arch {
"riscv32" => "rv32_ilp32_musl-openrc",
a if a.starts_with("riscv") => "rv64_lp64d-openrc",
"aarch64" => "arm64-openrc",
"x86_64" => "amd64-openrc",
Expand Down
Loading
Loading