gist/alt-e2k-chroot-setup.sh

119 lines
3.5 KiB
Bash

#!/bin/bash
IMAGE_ARCH="e2kv6" # possible values are e2k, e2kv4, e2kv5, e2kv6
IMAGE_VERSION="20240705"
IMAGE_PATH="/ALTLinux/images/Sisyphus/docker/docker-$IMAGE_VERSION-$IMAGE_ARCH.tar.xz"
MIRRORS=("http://elbrus.ivk.ru/pub" "http://setwd.ws/altlinux")
QEMU_BUILD_FLAGS="--disable-werror --disable-pie"
QEMU_BRANCH="e2k-v8.1.3"
CHROOT_DIR="chroot-$IMAGE_ARCH"
BINFMT_REGISTER_FLAGS=""
# this is taken from qemu's binfmt set up script because I don't want to pollute
# binfmt_misc with unrelated stuff
e2k_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xaf\x00'
e2k_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
e2k_old_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x31\x00'
e2k_old_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
qemu_target_list="e2k e2k_old"
if ! command -v git > /dev/null; then
echo "To build qemu-e2k, we need to clone git repository. Install git."
exit 1
fi
if ! command -v xz > /dev/null; then
echo "To unpack ALTLinux Docker image, we need xz. Install it."
exit 1
fi
if ! command -v wget > /dev/null; then
echo "To download ALTLinux Docker image, we need wget. Install it."
exit 1
fi
if [ "$(uname -m)" == "e2k" ]; then
echo "This script is for emulating Elbrus and you're already running on this architecture"
exit 1
fi
echo "WARNING: this script just set ups basic chroot to play with."
echo "Do not use this in production!\n"
echo "During installation, you will be asked for root access to properly"
echo "unpack image and install binfmt_misc rule\n"
read -p "Did you read and understood the message above? [Yn] " yn
case $yn in
[yY|""] ) echo "Continuing..."; ;;
[nN] ) echo "Exiting"; exit 0;;
[*] ) echo "Unrecognized answer"; exit 1;;
esac
set -x
git clone https://git.mentality.rip/OpenE2K/qemu-e2k -b $QEMU_BRANCH --depth=1 || exit 2
pushd qemu-e2k || exit 2
./configure --target-list=e2k-linux-user --static $QEMU_BUILD_FLAGS || exit 2
make -j$(nproc) || exit 2
popd || exit 2
for i in "${MIRRORS[@]}"; do
echo -n "Probing mirror $i..."
if wget -q -O/dev/null "$i"; then
SELECTED_MIRROR=$i
echo "OK"
break;
else
echo "BAD"
fi
done
if [ -z "$SELECTED_MIRROR" ]; then
echo "Can't find working mirror, exiting..."
exit 2
fi
qemu_register_interpreter() {
echo "Setting $qemu as binfmt interpreter for $cpu"
qemu_generate_register | sudo tee /proc/sys/fs/binfmt_misc/register || exit 2
}
qemu_generate_register() {
flags="${BINFMT_REGISTER_FLAGS}"
echo ":qemu-$cpu:M::$magic:$mask:$qemu:$flags"
}
wget -Odocker.txz "$SELECTED_MIRROR$IMAGE_PATH"
mkdir -p $CHROOT_DIR || exit 2
pushd $CHROOT_DIR
sudo tar -xJf ../docker.txz || exit 2
sudo cp ../qemu-e2k/build/qemu-e2k usr/bin || exit 2
# Change default mirror in case if ivk is unreachable (it's kinda stupid to block non-Russian IP addresses, isn't it?)
if [ "$SELECTED_MIRROR" != "http://elbrus.ivk.ru/pub" ]; then
sed "s|http://elbrus.ivk.ru/pub|$SELECTED_MIRROR|g" etc/apt/sources.list.d/ivk.list | sudo tee etc/apt/sources.list.d/mirror.list
sudo mv etc/apt/sources.list.d/ivk.list etc/apt/sources.list.d/ivk.list.disabled
fi
sudo cp /etc/resolv.conf etc/resolv.conf
for cpu in ${qemu_target_list} ; do
qemu="/usr/bin/qemu-e2k"
magic=$(eval echo \$${cpu}_magic)
mask=$(eval echo \$${cpu}_mask)
qemu_register_interpreter
done
popd || exit 2
set +x
echo "All done."
echo "Keep in mind, it's up to you to set up binfmt_misc so it doesn't get lost between reboots"
echo "Now you can do 'sudo chroot $CHROOT_DIR /bin/bash' and have some fun!"