Добавить alt-e2k-chroot-setup.sh

This commit is contained in:
a1batross 2024-07-11 20:22:11 +02:00
parent b18248b5f2
commit ab0f55c059

118
alt-e2k-chroot-setup.sh Normal file
View File

@ -0,0 +1,118 @@
#!/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..."; break;;
[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
./configure --target-list=e2k-linux-user --static $QEMU_BUILD_FLAGS
make -j$(nproc)
popd
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 1
fi
qemu_register_interpreter() {
echo "Setting $qemu as binfmt interpreter for $cpu"
qemu_generate_register | sudo tee /proc/sys/fs/binfmt_misc/register
}
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
# 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
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!"