Skip to content

Commit

Permalink
feat(xvoidstrap): allow bootstrapping cross archs
Browse files Browse the repository at this point in the history
This adds the necessary logic to support bootstrapping architectures
that are different from the host architecture.

/proc needs to be mounted because a base-files invocation of install
uses it to chmod tmp and var/tmp...
  • Loading branch information
CameronNemo committed Sep 15, 2020
1 parent 2e88453 commit 376342c
Showing 1 changed file with 61 additions and 4 deletions.
65 changes: 61 additions & 4 deletions xvoidstrap
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#!/bin/sh -e
# xvoidstrap DIR [PACKAGES...] - bootstrap a new Void installation
#
# (Setting XBPS_ARCH=x86_64-musl works.)
# Environment variables honored:
# - XBPS_ARCH: equivalent to XVOIDSTRAP_ARCH, for compatibility
# - XVOIDSTRAP_ARCH: set this when target architecture is different from host
# - XVOIDSTRAP_MIRROR: defaults to https://alpha.de.repo.voidlinux.org/current

: "${XVOIDSTRAP_MIRROR:=https://alpha.de.repo.voidlinux.org/current}"

fail() {
printf '%s\n' "$1" 1>&2
Expand All @@ -26,22 +31,74 @@ esac
[ -d "$CHROOT/etc" ] && fail 'directory contains /etc, bailing out'
[ -d "$CHROOT/usr" ] && fail 'directory contains /usr, bailing out'

# copy static qemu binary into chroot if needed
qemu_static=""
xv_machine="${XVOIDSTRAP_ARCH%-musl}"
if [ "$XVOIDSTRAP_ARCH" ] && [ "$(uname -m)" != "$xv_machine" ]; then
if [ "$xv_machine" = "i686" ]; then
qemu_static="/usr/bin/qemu-i386-static"
else
qemu_static="/usr/bin/qemu-${xv_machine}-static"
fi
if ! [ -x "$qemu_static" ]; then
fail 'install qemu-user-static to perform cross bootstrap'
fi
install -d -m 0755 "${CHROOT}/usr/bin"
cp "$qemu_static" "${CHROOT}${qemu_static}"
fi

chown root:root "$CHROOT"
chmod 0755 "$CHROOT"
mkdir -p "$CHROOT/boot" "$CHROOT/dev"
mkdir -p -m 0555 "$CHROOT/proc" "$CHROOT/sys"

mount -t proc none "$CHROOT/proc"

if [ -d /var/db/xbps/keys ]; then
mkdir -p "$CHROOT/var/db/xbps/keys"
cp -a /var/db/xbps/keys/* "$CHROOT/var/db/xbps/keys"
fi

# compatibility, XBPS_ARCH used to be left alone
if [ -z "$XVOIDSTRAP_ARCH" ] && [ "$XBPS_ARCH" ]; then
XVOIDSTRAP_ARCH="$XBPS_ARCH"
unset XBPS_ARCH
fi

if [ "$XVOIDSTRAP_ARCH" ]; then
export XBPS_TARGET_ARCH="${XVOIDSTRAP_ARCH}"
fi

xbps-install \
-R ${XVOIDSTRAP_MIRROR:-https://alpha.de.repo.voidlinux.org/current} \
-R ${XVOIDSTRAP_MIRROR:-https://alpha.de.repo.voidlinux.org/current}/musl \
-S -r "$CHROOT" \
-R ${XVOIDSTRAP_MIRROR} \
-R ${XVOIDSTRAP_MIRROR}/musl \
-R ${XVOIDSTRAP_MIRROR}/aarch64 \
-SUy -r "$CHROOT" \
${@:-base-system lvm2 cryptsetup grub}

if [ "$XVOIDSTRAP_ARCH" ]; then
unset XBPS_TARGET_ARCH
export XBPS_ARCH="${XVOIDSTRAP_ARCH}"
fi

xbps-reconfigure -r "$CHROOT" -f base-files
chroot "$CHROOT" xbps-reconfigure -f base-files
chroot "$CHROOT" xbps-reconfigure -af

# delete the static qemu binary installed above if and only if
# qemu-user-static was not installed during bootstrap
chroot "$CHROOT" xbps-query qemu-user-static || {
if [ "$?" = "2" ] && [ "$qemu_static" ]; then
rm -f -- "${CHROOT}${qemu_static}"
fi
}

if [ "$XVOIDSTRAP_ARCH" ]; then
unset XBPS_ARCH
fi

umount "$CHROOT/proc"

printf '\n'
cat <<EOF | tee ${CHROOT}/checklist
Void post-install checklist
Expand Down

0 comments on commit 376342c

Please sign in to comment.