Skip to content

Instantly share code, notes, and snippets.

@ilobmirt
Created January 25, 2022 09:36
Show Gist options
  • Save ilobmirt/31b2c95ac99ca2e740b0e5fcb8f009f3 to your computer and use it in GitHub Desktop.
Save ilobmirt/31b2c95ac99ca2e740b0e5fcb8f009f3 to your computer and use it in GitHub Desktop.
Sets up an x86 environment within an your arm based debian pc from which you could run x86 apps
#!/bin/bash
#=================================================================================================#
#install_envx86.sh
#----------
#by: ilobmirt @ 2022_JAN_25
#
#Sets up an x86 environment within an your arm based pc from which you could run x86 apps
#=================================================================================================#
install_target="/env_x86"
install_environment="i386"
scripts_install_target="/opt/envx86"
script_mount_file="envmount"
script_env_cmd_file="envx86_prompt"
desktop_install_target="$HOME/.local/share/applications/"
mount_host_sys="/sys"
mount_host_proc="/proc"
mount_host_dev="/dev"
mount_host_pts="/dev/pts"
mount_host_shm="/dev/shm"
mount_target_sys="$install_target/sys/"
mount_target_proc="$install_target/proc/"
mount_target_dev="$install_target/dev/"
mount_target_pts="$install_target/dev/pts/"
mount_target_shm="$install_target/dev/shm/"
main_install_packages(){
sudo apt-get update
local installed_packages=$(apt list --installed)
local to_install=""
local does_exist=""
for input_package in $@
do
does_exist=$(echo "$installed_packages" | grep "$input_package/")
if [ -z "$does_exist" ]; then
echo "Package $input_package not found, adding to list of packages"
to_install="$to_install $input_package"
else
echo "Package $input_package is already installed"
fi
done
if [ ! -z "$to_install" ]; then
echo "Installing $to_install"
sudo apt-get install$to_install -y
else
echo "Nothing found to install"
fi
}
env_link(){
sudo mount -t sysfs $mount_host_sys $mount_target_sys
sudo mount -t proc $mount_host_proc $mount_target_proc
sudo mount --bind $mount_host_dev $mount_target_dev
sudo mount --bind $mount_host_pts $mount_target_pts
sudo mount --bind $mount_host_shm $mount_target_shm
}
env_build(){
local debian_version=$(lsb_release -c -s)
local qemu_file="qemu-i386-static"
if [ -d "$install_target" ]; then
sudo umount $mount_target_sys
sudo umount $mount_target_proc
sudo umount $mount_target_pts
sudo umount $mount_target_shm
sudo umount $mount_target_dev
sudo rm -rf $install_target
fi
sudo debootstrap --foreign --arch $install_environment $debian_version $install_target
env_link
sudo cp /usr/bin/$qemu_file $install_target/usr/bin/
sudo chroot $install_target/ /debootstrap/debootstrap --second-stage
}
env_cmd(){
local username_given=$1
local command_given=$2
sudo chroot $install_target/ /bin/su $username_given --command="$command_given"
}
env_install_packages(){
env_cmd root "apt-get update"
local installed_packages=$(env_cmd root "apt list --installed")
local to_install=""
local does_exist=""
for input_package in $@
do
does_exist=$(echo "$installed_packages" | grep "$input_package/")
if [ -z "$does_exist" ]; then
echo "Package $input_package not found, adding to list of packages"
to_install="$to_install $input_package"
else
echo "Package $input_package is already installed"
fi
done
if [ ! -z "$to_install" ]; then
echo "Installing $to_install"
env_cmd root "apt-get install$to_install -y"
else
echo "Nothing found to install"
fi
}
env_setup_bashrc(){
local username_given=$1
local file_append="
export LANGUAGE=\\\"C\\\"
export LC_ALL=\\\"C\\\"
export DISPLAY=:0
"
env_cmd $username_given "echo \"$file_append\" >> ~/.bashrc ; source ~/.bashrc"
}
main_install_service_mount(){
local script_path="$scripts_install_target"
local script_file_name="$script_mount_file"
local script_file_contents="#!/bin/bash
sudo mount -t sysfs $mount_host_sys $mount_target_sys
sudo mount -t proc $mount_host_proc $mount_target_proc
sudo mount --bind $mount_host_dev $mount_target_dev
sudo mount --bind $mount_host_pts $mount_target_pts
sudo mount --bind $mount_host_shm $mount_target_shm"
local service_path="/etc/systemd/system"
local service_file_name="envx86.mount.service"
local service_file_contents="
[Unit]
Description= mounts the x86 environment for use with chroot
[Service]
Type=one-shot
ExecStart=$script_path/$script_file_name
[Install]
WantedBy=multi-user.target
"
if [ ! -d "$script_path" ]; then
sudo mkdir $script_path
fi
sudo sh -c "echo \"$script_file_contents\" > $script_path/$script_file_name"
sudo chmod +x $script_path/$script_file_name
sudo sh -c "echo \"$service_file_contents\" > $service_path/$service_file_name"
sudo systemctl enable $service_file_name
}
main_install_env_prompt(){
local script_path="$scripts_install_target"
local script_file_name="$script_env_cmd_file"
local script_file_contents="#!/bin/bash
/usr/sbin/chroot $install_target/ /bin/su -l \\\$1"
if [ ! -d "$script_path" ]; then
sudo mkdir $script_path
fi
sudo sh -c "echo \"$script_file_contents\" > $script_path/$script_file_name"
sudo chmod +x $script_path/$script_file_name
}
main_install_cmd_desktop(){
local install_path="$desktop_install_target"
local file_name="x86_$1.desktop"
local file_contents="[Desktop Entry]
Encoding=UTF-8
Version=1.0
Comment=Start an x86 user environment
Type=Application
Terminal=false
Exec=xterm -e sudo $scripts_install_target/$script_env_cmd_file $1
Name=CMD $1 x86"
echo "$file_contents" > $install_path/$file_name
}
env_add_user(){
local username_given=$1
local password_given=$2
local local_uid=$(id $username_given -u)
local local_groups=$(id $username_given -nG)
env_cmd root "useradd -m -p \$(mkpasswd -m sha-512 \"$password_given\") -s /bin/bash $username_given"
for group_add in $local_groups
do
env_cmd root "usermod -a -G $group_add $username_given 2> /dev/null"
done
env_setup_bashrc $username_given
}
main(){
local install_username=$USER
local install_password="power_up"
main_install_packages qemu qemu-user qemu-user-static binfmt-support debootstrap binutils
env_build
env_setup_bashrc root
env_install_packages sudo whois x11-apps neofetch cabextract winbind wget xz-utils
env_add_user $install_username $install_password
main_install_service_mount
main_install_env_prompt
main_install_cmd_desktop root
main_install_cmd_desktop $install_username
env_cmd $install_username "mkdir ~/Downloads"
env_cmd $install_username "neofetch"
env_cmd $install_username "xeyes &"
echo ""
echo "==================="
echo "|CONGRATULATIONS! |"
echo "==================="
echo "$install_environment environment has been installed under \"$install_target\"!"
echo "If successful, you should see neofetch reporting that it is running $install_environment software"
echo "Also, if x11 is configured correctly, you should see xeyes present on the host desktop"
echo ""
echo "==================="
echo "|NOTE: |"
echo "==================="
echo "The $install_environment environment user - $install_username has been installed with a default password \"$install_password\""
echo "Consider changing it with the passwd command once you use that account"
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment