Scripts for bootstrapping various programming languages
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

533 lines
23 KiB

# vim:set ft=sh:
# Distfiles mirror:
# TODO
# Changelog:
# - 2026.11:
# - Build glibc with a properly configured gcc rather than the musl version,
# resolving an issue and further future-proofing.
# - Switch from a fully custom profile for the temporary prefix to one
# inheriting fragments of supported upstream profiles. This avoids having
# to specify a lot of things ourselves, and provides better
# future-proofing.
# - 2025.8:
# - Set up a proper gentoo install using prefix, by building a bunch of tools
# in a temporary prefix, and then cross-compiling a final prefix.
# - Use crossdev to build the final system.
# - Both of these changes make the instructions significantly more
# future-proof, although there's still a lot of bugs to fix upstream, and
# ways to lessen the amount of compilation needed.
# - 2025:
# - Upstreamed portage patch
# - Cross-compile all packages in packages.build for the profile
# (this simplifies the steps afterwards significantly)
# - Pin to GCC 13 (gcc 14 isn't working...)
# - Future proofing by ignoring HTTPs certificates
# (portage verifies the checksums anyway)
# - Avoid using wget (need to set FETCHCOMMAND anyway...)
# - Future proofing by not hardcoding a python version
# - 2024.8:
# - Rewrite bootstrap to avoid using LFS
# - Pin the repository version
# - 2024:
# - Initial version
# Overview of bootstrap:
# step 0: Build live-bootstrap as a starting point
# step 1: Install a temporary copy of portage to /tmp/portage
# step 2: Setup a temporary prefix-guest using the live-bootstrap toolchain, build a few additional tools
# step 3: Set up a target prefix, build a cross toolchain to target it
# step 4: Build portage and necessary utilities for the target prefix
# step 5: Switch to it and emerge the rest of @world
# step 6: Install crossdev and build the final system
# Build live-bootstrap. This is a bootstrap chain that starts from a tiny
# binary and builds, in a verifiable manner, up to a modern version of GCC and
# associated tools, which we can use as a springboard for gentoo.
git clone https://github.com/fosslinux/live-bootstrap
git -C live-bootstrap checkout a49273cbb5c381b851198bc1e53c294702f4f7c9
git -C live-bootstrap submodule update --init --recursive --depth=1 --progress
cd live-bootstrap
./download-distfiles.sh # https://github.com/fosslinux/live-bootstrap/wiki/Mirrors
python3 -m venv env
env/bin/pip install -U --no-binary :all: pip
env/bin/pip install -v --no-binary :all: requests
#sudo -s # if not root already
umask 022
env/bin/python ./rootfs.py -c --external-sources --cores $(nproc) -m ''
umount target/dev/shm # double mounted?
umount target/dev/shm target/sys target/proc target/tmp
rm -r target/dev/* # avoid keeping node files around
# Optional: Keep repository for debugging and clean up copy of distfiles
mv target/external/repo .
rm -rf target/external
# Optional: Back up the system
env -i chroot target tar --exclude='/external' --sort=name -cf /target.tar /
env -i chroot target bzip2 -9v /target.tar
mv target/target.tar.bz2 .
# Optional: Merge distfiles to keep them in one place
mkdir -p target/var/cache/distfiles
mount --bind distfiles target/var/cache/distfiles
# Chroot into the live-bootstrap system
cd target
mount -t proc proc proc
mount -t sysfs sysfs sys
mount -t devtmpfs devtmpfs dev
mount -t devpts devpts dev/pts
env -i TERM="$TERM" chroot . /bin/bash -l
umask 022
source /steps/env
cd /tmp
# Optional: Configure additional mirrors for missing/removed distfiles
# This is a space-separated list of URLs, but full paths to local directories
# can be added as well.
export GENTOO_MIRRORS="/mnt/mirror http://distfiles.gentoo.org"
# Download initial distfiles
mkdir -p /var/cache/distfiles; cd /var/cache/distfiles
test -f portage-3.0.82.2.tar.bz2 || curl -LO \
http://gitweb.gentoo.org/proj/portage.git/snapshot/portage-3.0.82.2.tar.bz2
test -f gentoo-20261101.xz.sqfs || curl -LO \
http://distfiles.gentoo.org/snapshots/squashfs/gentoo-20261101.xz.sqfs
test -f squashfs-tools-4.7.5.tar.gz || curl -LO \
https://github.com/plougher/squashfs-tools/archive/refs/tags/4.7.5/squashfs-tools-4.7.5.tar.gz
test -f findutils-4.9.0.tar.xz || curl -LO \
https://ftp.gnu.org/gnu/findutils/findutils-4.9.0.tar.xz
cd /tmp
# Build squashfs-tools to extract the ::gentoo tree. We use the squashfs
# archives as they are archived on the gentoo mirrors for significantly longer.
# TODO: Include in live-bootstrap proper
tar xf /var/cache/distfiles/squashfs-tools-4.7.5.tar.gz
cd squashfs-tools-4.7.5
make -C squashfs-tools install \
INSTALL_PREFIX=/usr \
LZO_SUPPORT=0 LZ4_SUPPORT=0 ZSTD_SUPPORT=0
cd ..
rm -r squashfs-tools-4.7.5
# Build a version of findutils that portage will be OK with (minimum: 4.9.0)
# TODO: Include in live-bootstrap proper
tar xf /var/cache/distfiles/findutils-4.9.0.tar.xz
cd findutils-4.9.0
./configure --prefix=/usr
make install
cd ..
rm -r findutils-4.9.0
# Make some additional root modifications
ln -sfT tar /usr/bin/gtar # needed by portage
cp /usr/include/asm-generic/mman.h /usr/include/asm # needed by dev-libs/openssl
sed '/^#define SYS_openat2\>/d' -i /usr/include/bits/syscall.h # linux-headers too old for openat2, trips up sys-apps/util-linux
# Unpack the ::gentoo tree
unsquashfs /var/cache/distfiles/gentoo-20261101.xz.sqfs
mkdir -p /var/db/repos
rm -rf /var/db/repos/gentoo
mv squashfs-root /var/db/repos/gentoo
# Install temporary copy of portage
tar xf /var/cache/distfiles/portage-3.0.82.2.tar.bz2
ln -sfT portage-3.0.82.2 portage
# Temporarily work around limitation of live-bootstrap python by patching portage
# https://github.com/fosslinux/live-bootstrap/issues/590
sed 's|/proc/self/cgroup|/proc/self/nocgroup|' \
-i portage-3.0.82.2/lib/portage/util/cgroup.py
# Add portage user/group
echo 'portage:x:250:250:portage:/var/tmp/portage:/bin/false' >> /etc/passwd
echo 'portage::250:portage' >> /etc/group
# Create cross repository. Since we will be building a cross toolchain
# targetting a new EPREFIX, the regular crossdev tools will not function.
mkdir -p /var/db/repos/cross/profiles
echo cross > /var/db/repos/cross/profiles/repo_name
mkdir -p /var/db/repos/cross/metadata
printf '%s\n' 'masters = gentoo' 'thin-manifests = true' \
> /var/db/repos/cross/metadata/layout.conf
mkdir -p /var/db/repos/cross/cross-base
ln -sfT /var/db/repos/gentoo/sys-devel/binutils /var/db/repos/cross/cross-base/binutils
ln -sfT /var/db/repos/gentoo/sys-devel/gcc /var/db/repos/cross/cross-base/gcc
mkdir -p /var/db/repos/cross/cross-base/glibc
printf '%s\n' EAPI=8 SLOT=0 KEYWORDS='"*"' \
> /var/db/repos/cross/cross-base/glibc/glibc-0.ebuild
# Register i686-cross-linux-gnu toolchain. Using this CHOST in order to be
# distinguishable from the canonical i686-pc-linux-gnu. This will make it
# possible to build a toolchain targetting a non-EPREFIXed install.
echo cross-i686-cross-linux-gnu >> /var/db/repos/cross/profiles/categories
ln -sfT cross-base /var/db/repos/cross/cross-i686-cross-linux-gnu
# Configure a temporary prefix. This is a stacked/rpath prefix, meaning it will
# use the live-bootstrap toolchain and libc instead of its own. It will be used
# to build a cross-compiler targetting glibc to build a fully-fledged prefix.
# Because no profile exists for the live-bootstrap configuration, a custom
# profile is initialized, trying to make it such that as much as possible can
# be built. The intention is to let portage figure out what it needs to build,
# instead of cherry-picking packages, in order to improve forward-compatibility.
mkdir -p /cross/etc/portage/make.profile
cat > /cross/etc/portage/make.profile/parent << 'EOF'
/var/db/repos/gentoo/profiles/default/linux/x86/23.0/i686
/var/db/repos/gentoo/profiles/features/musl
/var/db/repos/gentoo/profiles/features/prefix/rpath
EOF
cat > /cross/etc/portage/make.profile/make.defaults << 'EOF'
FETCHCOMMAND="curl -k --retry 3 -y 60 --ftp-pasv -o \"\${DISTDIR}/\${FILE}\" -L \"\${URI}\""
RESUMECOMMAND="curl -C - -k --retry 3 -y 60 --ftp-pasv -o \"\${DISTDIR}/\${FILE}\" -L \"\${URI}\""
FEATURES="-news -sandbox -usersandbox -pid-sandbox -ipc-sandbox -network-sandbox -parallel-fetch nodoc noinfo noman"
CLEAN_DELAY=0
BINPKG_COMPRESS=bzip2 # Silence portage about missing zstd
CHOST=i686-unknown-linux-musl # This is the live-bootstrap toolchain CHOST
USE="-nls build minimal"
USE="$USE -seccomp" # Not supported on kernel 4.14
# These variables will be picked up by GCC and reconfigure the current compiler
# search paths, without affecting the system search path for the libc.
C_INCLUDE_PATH="$EROOT/usr/include"
CPLUS_INCLUDE_PATH="$EROOT/usr/include"
LIBRARY_PATH="$EROOT/usr/lib"
# Teach live-bootstrap pkg-config to find libraries in our prefix
PKG_CONFIG_PATH="$EROOT/usr/lib/pkgconfig:$EROOT/usr/share/pkgconfig"
# This variable is used by build systems, and we trust them to actually use it.
# It will make sure that programs will use libraries in our prefix before
# searching in live-bootstrap locations.
LDFLAGS="-Wl,--disable-new-dtags,-rpath,$EPREFIX/usr/lib"
EOF
cat > /cross/etc/portage/repos.conf << 'EOF'
[cross]
location = /var/db/repos/cross
EOF
cat > /cross/etc/portage/package.use << 'EOF'
cross-i686-cross-linux-gnu/binutils -debuginfod # Avoid unnecessary dependencies
cross-i686-cross-linux-gnu/gcc -sanitize -fortran # Avoid unnecessary features
dev-libs/elfutils -debuginfod # Avoid unnecessary dependencies
EOF
cat > /cross/etc/portage/package.mask << 'EOF'
sys-devel/binutils
sys-devel/gcc
EOF
# Avoid reconfiguring the core toolchain. Combined with package.mask, and the
# variables set in make.globals, this allows the existing toolchain to be used
# to build tools in the temporary prefix.
cat > /cross/etc/portage/make.profile/package.provided << 'EOF'
sys-devel/binutils-2.27
sys-devel/gcc-6.2
EOF
# Currently, the crossdev functionality in toolchain.eclass isn't sufficient to
# build a cross-toolchain targetting an EPREFIX,
mkdir -p /cross/etc/portage/env/cross-i686-cross-linux-gnu
cat > /cross/etc/portage/env/cross-i686-cross-linux-gnu/gcc << '_EOF'
# Configure gcc to get its startfiles, headers and libraries from a new root/prefix
TARGET_ESYSROOT="$TARGET_SYSROOT$TARGET_EPREFIX"
EXTRA_ECONF="--with-sysroot=$TARGET_ESYSROOT"
# --with-newlib allows us to change the sysroot without implying the presence
# of libc headers. If sysroot is left empty, GCC will default to
# /usr/$CTARGET/sys-include, which is how crossdev usually solves this.
[[ ! -e $TARGET_ESYSROOT/usr/include/stdlib.h ]] && EXTRA_ECONF+=" --with-newlib"
post_src_install() {
# Add prefix to dynamic linker path. This will only work for x86 glibc, but
# support for more libcs can be gleaned from gcc -dumpspecs output. Ideally,
# toolchain.eclass would make it possible to prefixify the dynamic linker when
# building a cross-compiler, even in prefix-guest.
cat > "$ED/usr/lib/gcc/$CTARGET/specs" << EOF
*link:
+ %{!shared:%{!static:%{!static-pie:-dynamic-linker \
%{m64|mx32:;:$TARGET_EPREFIX/lib/ld-linux.so.2} \
%{m64:$TARGET_EPREFIX/lib64/ld-linux-x86-64.so.2} \
%{mx32:$TARGET_EPREFIX/libx32/ld-linux-x32.so.2} \
}}}
EOF
# Teach pkg-config to search for libraries targetting $CTARGET in our new
# root/prefix. This must be a program, as this is what most build systems
# expect when cross-compiling. Crossdev typically makes this script.
cat > "$ED/usr/bin/$CTARGET-pkg-config" << EOF
#!$EPREFIX/bin/sh
export PKG_CONFIG_LIBDIR="$TARGET_ESYSROOT/usr/lib/pkgconfig:$TARGET_ESYSROOT/usr/share/pkgconfig"
export PKG_CONFIG_SYSTEM_INCLUDE_PATH="$TARGET_ESYSROOT/usr/include"
export PKG_CONFIG_SYSTEM_LIBRARY_PATH="$TARGET_ESYSROOT/lib:$TARGET_ESYSROOT/usr/lib"
exec "$EPREFIX/usr/bin/pkg-config" "\$@"
EOF
chmod +x "$ED/usr/bin/$CTARGET-pkg-config"
}
_EOF
# An issue in live-bootstrap gcc causes --print-multiarch to show a bogus
# value, which trips up python. Work around it.
# https://github.com/fosslinux/live-bootstrap/blob/bbccded/steps/python-3.11.1/patches/multiarch.patch
# https://github.com/fosslinux/live-bootstrap/issues/591
mkdir -p /cross/etc/portage/env/dev-lang
cat > /cross/etc/portage/env/dev-lang/python << 'EOF'
pre_src_prepare() {
sed -i 's/\[MULTIARCH=\$(.*\]/[MULTIARCH=]/' configure.ac || die
}
EOF
# Initialize environment. This makes sure all relevant directories and symlinks
# are available, and the environment is sourced correctly starting with the
# first build. We override env.d/99host to point to where the live-bootstrap
# build tools are located, but nothing else.
PORTAGE_OVERRIDE_EPREFIX=/cross ./portage/bin/emerge -D1u sys-apps/baselayout
echo 'PATH=/usr/bin' > /cross/etc/env.d/99host
PORTAGE_OVERRIDE_EPREFIX=/cross ./portage/bin/env-update
# Create some required symlinks
mkdir -p /cross/bin
ln -sf /bin/sh /cross/bin # Required by sys-devel/gcc
ln -sf /bin/bash /cross/bin # Required by eautoreconf
# Registering some live-bootstrap tools into the package database, in order to
# break some dependency cycles. Doing it this way rather than package.provided
# ensures that "has_version" works in the relevant eclasses, and they get
# rebuilt through --update (as long as the version number doesn't match),
# allowing portage to reconfigure the relevant packages accordingly.
provide() { mkdir -p "/cross/var/db/pkg/$1" && \
echo 0 > "/cross/var/db/pkg/$1/SLOT" && \
echo 0 > "/cross/var/db/pkg/$1/COUNTER"; }
provide app-arch/xz-utils-5.4.0_p_pre
provide app-alternatives/awk-0_pre
# Build tools missing from/broken in live-bootstrap
# TODO: Include in live-bootstrap proper
PORTAGE_OVERRIDE_EPREFIX=/cross MAKEOPTS=-j1 ./portage/bin/emerge -O1u dev-build/make # make in live-bootstrap struggles with parallel builds under portage
PORTAGE_OVERRIDE_EPREFIX=/cross ./portage/bin/emerge -D1u '<sys-apps/gentoo-functions-1' # Bootstrap gentoo-functions
PORTAGE_OVERRIDE_EPREFIX=/cross ./portage/bin/emerge -D1u app-misc/pax-utils # Make sure programs will be stripped
# Configure final prefix. This will be a "real" prefix, with none of the hybrid
# hackery involved in the temporary prefix, and will no longer use anything
# from live-bootstrap. While this intermediate step is costly in build time,
# and it's technically possible to skip either the temporary or the final
# prefix, experience has taught me that this is brittle and hard to support.
mkdir -p /gentoo/etc/portage
ln -sfT /var/db/repos/gentoo/profiles/default/linux/x86/23.0/i686/prefix/kernel-3.2+ /gentoo/etc/portage/make.profile
cat > /gentoo/etc/portage/make.conf << 'EOF'
FETCHCOMMAND="curl -k --retry 3 -y 60 --ftp-pasv -o \"\${DISTDIR}/\${FILE}\" -L \"\${URI}\""
RESUMECOMMAND="curl -C - -k --retry 3 -y 60 --ftp-pasv -o \"\${DISTDIR}/\${FILE}\" -L \"\${URI}\""
FEATURES="-news -sandbox -usersandbox -pid-sandbox -ipc-sandbox -network-sandbox -parallel-fetch nodoc noinfo noman"
CLEAN_DELAY=0
BINPKG_COMPRESS=bzip2 # Silence portage about missing zstd
PORTDIR=/var/db/repos/gentoo
DISTDIR=/var/cache/distfiles
PORTAGE_TMPDIR=/var/tmp
CHOST=i686-cross-linux-gnu
ACCEPT_KEYWORDS="-~$ARCH"
USE="-nls"
USE="$USE -seccomp" # Not supported on kernel 4.14
INSTALL_MASK=/etc/env.d/99host # Avoid using any live-bootstrap tools in this prefix
EOF
# Use profile directory in order to give crossdev free reign over the rest of
# the portage configuration directory.
mkdir -p /gentoo/etc/portage/profile
cat > /gentoo/etc/portage/profile/package.use << 'EOF'
dev-vcs/git -perl -rust # Avoid unnecessary dependencies
sys-devel/gcc -sanitize -fortran # Avoid unnecessary features
app-arch/gzip pic # Don't understand why build breaks without it
EOF
# Configure the config.site for certain autotools programs. More programs might
# need further configuration, and this is only valid for i686-cross-linux-gnu.
cat > /gentoo/etc/portage/config.site << 'EOF'
ac_cv_file__dev_ptmx=yes # dev-lang/python
ac_cv_file__dev_ptc=no # dev-lang/python
gl_cv_func_strcasecmp_works=yes # sys-apps/diffutils
EOF
cat > /gentoo/etc/portage/bashrc << 'EOF'
# When cross-compiling from the temporary prefix into the final prefix,
# programs built for $CBUILD will still need the relevant flags to be set.
if [[ $CBUILD = i686-unknown-linux-musl ]]; then
export BUILD_CFLAGS="-I$BROOT/usr/include"
export BUILD_LDFLAGS="-L$BROOT/usr/lib -Wl,--disable-new-dtags,-rpath,$BROOT/usr/lib"
fi
# While cross-compiling, it's necessary to tell autotools where to find answers
if [[ $CBUILD != $CHOST ]]; then
export CONFIG_SITE="$EROOT/etc/portage/config.site"
fi
# Sidestep broken sanity checks in glibc
# https://bugs.gentoo.org/986777
if [[ $CATEGORY/$PN = cross-*/glibc ]]; then
sanity_prechecks() { :; }
fi
EOF
# An issue in live-bootstrap gcc causes --print-multiarch to show a bogus
# value, which trips up python. Work around it.
# https://github.com/fosslinux/live-bootstrap/blob/bbccded/steps/python-3.11.1/patches/multiarch.patch
# https://github.com/fosslinux/live-bootstrap/issues/591
mkdir -p /gentoo/etc/portage/env/dev-lang
cat > /gentoo/etc/portage/env/dev-lang/python << '_EOF'
if [[ $CBUILD = i686-unknown-linux-musl ]]; then
pre_src_prepare() {
sed -i 's/\[MULTIARCH=\$(.*\]/[MULTIARCH=]/' configure.ac || die
}
fi
_EOF
# Patch shadow to allow it to modify /etc/shadow in a different $ROOT, without
# adding $EPREFIX to only shadow and gshadow...
# https://bugs.gentoo.org/986829
mkdir -p /gentoo/etc/portage/env/sys-apps
cat > /gentoo/etc/portage/env/sys-apps/shadow << 'EOF'
pre_src_prepare() {
sed -e 's|_PATH_GSHADOW|"/etc/gshadow"|' \
-e 's|_PATH_SHADOW|"/etc/shadow"|' \
-i lib/prefix_flag.c || die
}
EOF
# Initialize prefix
PORTAGE_OVERRIDE_EPREFIX=/gentoo USE=build ./portage/bin/emerge -D1u sys-apps/baselayout
echo 'man:x:13:15:System user; man:/dev/null:/sbin/nologin' >> /gentoo/etc/passwd
echo 'man:x:15:' >> /gentoo/etc/group
echo 'shadow:x:42:' >> /gentoo/etc/group
# TODO: The acct-user and acct-group should create the groups honestly
# Bootstrap cross-compiler and libc for final prefix.
# Using --nodeps for glibc because the live-bootstrap environment is guaranteed to provide enough to build glibc
PORTAGE_OVERRIDE_EPREFIX=/cross ./portage/bin/emerge -D1u cross-i686-cross-linux-gnu/binutils
PORTAGE_OVERRIDE_EPREFIX=/cross TARGET_EPREFIX=/gentoo USE='-cxx -openmp' ./portage/bin/emerge -D1u cross-i686-cross-linux-gnu/gcc
PORTAGE_OVERRIDE_EPREFIX=/cross PORTAGE_CONFIGROOT=/gentoo EPREFIX=/gentoo CBUILD=i686-unknown-linux-musl CXX=false ./portage/bin/emerge -D1u sys-kernel/linux-headers
PORTAGE_OVERRIDE_EPREFIX=/cross PORTAGE_CONFIGROOT=/gentoo EPREFIX=/gentoo CBUILD=i686-unknown-linux-musl CXX=false ./portage/bin/emerge -O1u sys-libs/glibc
PORTAGE_OVERRIDE_EPREFIX=/cross ./portage/bin/emerge -O1u cross-i686-cross-linux-gnu/glibc # Dummy package, allows configuring gcc correctly (see toolchain.eclass)
PORTAGE_OVERRIDE_EPREFIX=/cross TARGET_EPREFIX=/gentoo ./portage/bin/emerge -D1U cross-i686-cross-linux-gnu/gcc
# Build compiler and minimal set of tools for final prefix. Hand-picking these
# minimizes the amount of BDEPENDs pulled into the temporary prefix, saving
# some time, and mirrors the way the catalyst stage1 is built.
PORTAGE_OVERRIDE_EPREFIX=/cross PORTAGE_CONFIGROOT=/gentoo EPREFIX=/gentoo CBUILD=i686-unknown-linux-musl USE=build ./portage/bin/emerge -D1u \
app-alternatives/awk \
app-alternatives/gzip \
app-shells/bash \
dev-build/make \
sys-apps/coreutils \
sys-apps/file \
sys-apps/grep \
sys-apps/portage \
sys-apps/sed \
sys-devel/binutils \
sys-devel/gcc
# Finalize prefix
# TODO: Use localedef instead of locale-gen to avoid needing that program at
# this stage. (e.g. localedef -i C -f UTF-8 C.UTF-8)
echo C > /gentoo/etc/locale.gen
( . /gentoo/etc/profile; /cross/usr/sbin/locale-gen )
/gentoo/usr/bin/emerge -DUu @world
/gentoo/usr/bin/emerge -c
# Optional: Back up the created prefix
tar --sort=name -cf /gentoo.tar /gentoo
bzip2 -9v /gentoo.tar
# Initialize crossdev
/gentoo/usr/bin/emerge -Du sys-devel/crossdev app-eselect/eselect-repository
( . /gentoo/etc/profile; eselect repository create crossdev )
# Build an amd64 multilib cross compiler using crossdev. This is currently not
# officially supported...
mkdir -p /gentoo/etc/portage/profile/package.use.mask
printf 'cross-x86_64-pc-linux-gnu/%s -multilib cet\n' \
linux-headers glibc binutils gcc gdb \
> /gentoo/etc/portage/profile/package.use.mask/cross-x86_64-pc-linux-gnu.multilib
( . /gentoo/etc/profile; crossdev -A 'amd64 x86' -S -P -v x86_64-pc-linux-gnu )
# Configure new system
rm -r /gentoo/usr/x86_64-pc-linux-gnu/etc/portage
mkdir -p /gentoo/usr/x86_64-pc-linux-gnu/etc/portage
( . /gentoo/etc/profile; PORTAGE_CONFIGROOT=/gentoo/usr/x86_64-pc-linux-gnu \
eselect profile set --force default/linux/amd64/23.0 )
cat > /gentoo/usr/x86_64-pc-linux-gnu/etc/portage/make.conf << 'EOF'
FEATURES="-news -sandbox -usersandbox -pid-sandbox -ipc-sandbox -network-sandbox -parallel-fetch nodoc noinfo noman"
ROOT="/gentoo/usr/x86_64-pc-linux-gnu"
PKGDIR="$ROOT/var/cache/binpkgs"
PORTAGE_TMPDIR="$ROOT/tmp"
PORTDIR=/var/db/repos/gentoo
DISTDIR=/var/cache/distfiles
CONFIG_SITE=/gentoo/usr/share/config.site # Needed because --prefix for target != /gentoo/usr
SOURCE_DATE_EPOCH=315532800 # 1980-01-01, because 0 is too old for zip files (e.g. python wheels)
EOF
# Cross compile the new system
( . /gentoo/etc/profile; USE=build x86_64-pc-linux-gnu-emerge -bDU1u sys-apps/baselayout )
( . /gentoo/etc/profile; x86_64-pc-linux-gnu-emerge -bDUu @world )
# Create final rootfs
( . /gentoo/etc/profile; ROOT=/gentoo-amd64 USE=build x86_64-pc-linux-gnu-emerge -KDU1u sys-apps/baselayout )
( . /gentoo/etc/profile; ROOT=/gentoo-amd64 x86_64-pc-linux-gnu-emerge -KDUu @world )
###########################################################
# TODO
###########################################################
# Configure final system
mkdir -p /gentoo-amd64/etc/portage
ln -sfT ../../var/db/repos/gentoo/profiles/default/linux/amd64/23.0 /gentoo-amd64/etc/portage/make.profile
echo C > /gentoo-amd64/etc/locale.gen
# Initialize glibc databases
# This is only possible due to how the final prefix is a glibc system, which
# has the appropriate ldconfig and localedef commands. If this weren't the
# case, ldconfig and localedef (locale-gen isn't installed in this setup)
# would have to be invoked manually.
( . /gentoo/etc/profile; ldconfig -r /gentoo-amd64 -f /etc/ld.so.conf -C /etc/ld.so.cache )
( . /gentoo/etc/profile; locale-gen -p /gentoo-amd64 )
# This is the point where you have to move from the x86 system to an amd64 system.
# Make sure that you are running an amd64 kernel before chrooting, or booting it.
exit
umount dev/pts dev sys proc
umount var/cache/distfiles
# Optional: Back up the system
env -i chroot gentoo-amd64 tar --sort=name -cf /gentoo-amd64.tar /
env -i chroot gentoo-amd64 bzip2 -9v /gentoo-amd64.tar
mv gentoo-amd64/gentoo-amd64.tar.bz2 ..
# Copy ::gentoo repo and distfiles
install -dm755 gentoo-amd64/var/db/repos
rsync -a var/db/repos/gentoo/ gentoo-amd64/var/db/repos/gentoo
rsync -a var/cache/distfiles/ gentoo-amd64/var/cache/distfiles
# Optional: Mount the distfiles again
mount --bind ../distfiles gentoo-amd64/var/cache/distfiles
# Enter the final, amd64 system
cd gentoo-amd64
mount -t proc proc proc
mount -t sysfs sysfs sys
mount -t devtmpfs devtmpfs dev
mount -t devpts devpts dev/pts
env -i TERM="$TERM" chroot . /bin/bash -l
umask 022
# Rebuild everything, for good measure
emerge -e @world
emerge -c
# If dev-build/steve is installed on the host, to speed it up:
# MAKEFLAGS=--jobserver-auth=fifo:/dev/steve SANDBOX_WRITE=/dev/steve FEATURES='jobserver-token parallel-install' emerge --jobs --implicit-system-deps=n -e @world