#!/bin/bash export INSTANCE=$$ umask 022 state_checks() { test $(id -u) -ne 0 && echo "please run as root" && exit 1 test ! -e setup-chroot && echo "This utility must be run in the directory in which it exists." && exit 1 test "$PWD" == "/" && echo "You have already started the session." && exit 1 test ! -d var/cache/dnf && ts/bin/install_chroot } help() { echo -e " This script sets up the environment to work well with build. On it's initial run, it will decompress all the packages necessary to develop and build images. It will also populate the ts build env with any files that it needs. If the setup is already completed, it will only take you into the chroot env for building. You will need to 'exit' this env when you are done working in it. It takes the following options. -a : Automaticly download and build during unwind. -h : Show this help message. -i : Install the env only. Don't enter it after-wards. -b : Run build after entering chroot and exit after-wards. -o : Options to pass to build. Must be the last option, as anything after it will be passed to build. -c : Clean the chroot env. -e : Execute a command after entering chroot. Must be the last option, as anything after it will be executed in the chroot, including arguments. -k : Try and import ssh keys. " } get_opts() { until [ -z "$1" ] ; do case $1 in -a|--autodl) export autodl=true;; -i|--install) install_only=true; export autodl=true;; -b|--build) export build=true ;; -h|--help) help ; exit 255 ;; -o|--options) shift ; export build_opts=$@ ; shift $# ;; -c|--clean) unset build exec_cmd; shift $# ; export clean=true ;; -e|--exec) shift ; export exec_cmd=$@ ; shift $# ;; -u|--user) shift ; export chuser=$1 ;; -k|--keys) shift ; export keys=true;; *) echo "Invalid Argument: $1" ; help ; exit 255 ;; esac shift done get_gnome_proxy set_proxy } first_or_last() { sessions="`ps x -o pid,ppid,comm |grep -e 'setup-chroot' |grep -v grep |grep -v $INSTANCE -c`" return $sessions } get_gnome_proxy() { if which gconftool-2 &>/dev/null; then mode=`gconftool-2 -g /system/proxy/mode 2>/dev/null` use_http_proxy=`gconftool-2 -g /system/http_proxy/use_http_proxy 2>/dev/null` use_same_proxy=`gconftool-2 -g /system/http_proxy/use_same_proxy 2>/dev/null` use_authentication=`gconftool-2 -g /system/http_proxy/use_authentication 2>/dev/null` authentication_user=`gconftool-2 -g /system/http_proxy/authentication_user 2>/dev/null` authentication_password=`gconftool-2 -g /system/http_proxy/authentication_password 2>/dev/null` host=`gconftool-2 -g /system/http_proxy/host 2>/dev/null` port=`gconftool-2 -g /system/http_proxy/port 2>/dev/null` ignore_hosts=`gconftool-2 -g /system/http_proxy/ignore_hosts 2>/dev/null` socks_host=`gconftool-2 -g /system/proxy/socks_host 2>/dev/null` socks_port=`gconftool-2 -g /system/proxy/socks_port 2>/dev/null` secure_host=`gconftool-2 -g /system/proxy/secure_host 2>/dev/null` secure_port=`gconftool-2 -g /system/proxy/secure_port 2>/dev/null` ftp_host=`gconftool-2 -g /system/proxy/ftp_host 2>/dev/null` ftp_port=`gconftool-2 -g /system/proxy/ftp_port 2>/dev/null` fi if [ -z "$use_authentication" ]; then use_authentication=false ; fi } set_proxy() { if [ "$mode" == "manual" ]; then if $use_http_proxy && [ -n "$host" ] && [ -n "$port" ] ; then no_proxy=`echo "$ignore_hosts" |sed -e 's/\[//g' |sed -e 's/\]//g'` export no_proxy if $use_authentication; then if [ -n "$authentication_user" ] && [ -n "$authentication_password" ]; then PROXY_AUTH="$authentication_user:$authentication_password" elif [ -n "$authentication_user" ]; then PROXY_AUTH="$authentication_user" fi export http_proxy=http://${PROXY_AUTH}@$host:$port if [ -n "$ftp_host" ] && [ -n "$ftp_port" ]; then export ftp_proxy=http://${PROXY_AUTH}@$ftp_host:$ftp_port fi if [ -n "$secure_host" ] && [ -n "$secure_port" ]; then export https_proxy=https://${PROXY_AUTH}@$secure_host:$secure_port fi else if [ -n "$ftp_host" ] && [ -n "$ftp_port" ]; then export ftp_proxy=http://$ftp_host:$ftp_port fi export http_proxy=http://$host:$port if [ -n "$secure_host" ] && [ -n "$secure_port" ]; then export https_proxy=https://$secure_host:$secure_port fi fi fi fi } mounted() { if mountpoint $1 >/dev/null 2>&1; then return 0 else return 1 fi } do_mounts() { if ! mounted dev ; then mount --bind /dev dev ; fi if ! mounted tmp ; then mount -t tmpfs /tmp tmp ; fi if ! mounted proc ; then mount -t proc proc proc ; fi if ! mounted sys ; then mount -t sysfs none sys ; fi if ! mounted dev/pts ; then mount --bind /dev/pts dev/pts ; fi if ! mounted run ; then mount --bind /run run ; fi if [ -e /run/pcscd ]; then if ! mounted run/pcscd ; then mkdir -p run/pcscd mount --bind /run/pcscd run/pcscd fi elif [ -e /var/run/pcscd ]; then if ! mounted run/pcscd ; then mkdir -p run/pcscd mount --bind /var/run/pcscd run/pcscd fi fi if [ -e /run/user/0 ]; then if ! mounted run/user/0; then mkdir -p run/user/0 mount --bind /run/user/0 run/user/0 fi fi if [ -n "$SUDO_UID" ]; then if ! mounted run/user/$SUDO_UID; then mkdir -p run/user/$SUDO_UID mount --bind /run/user/$SUDO_UID run/user/$SUDO_UID fi fi if [ -e /mnt/wsl ]; then for mount in `ls -A /mnt`; do if mounted /mnt/$mount; then if ! mounted mnt/$mount; then mkdir -p mnt/$mount mount --bind /mnt/$mount mnt/$mount fi fi done fi } copy_host_essential() { # if which xauth >/dev/null 2>&1 && [ -n "`xauth list`" ]; then # auth_file=$(xauth info | grep "Authority file" | awk '{print $3}') # auth_dir="`dirname $auth_file`" # mkdir -p $PWD$auth_dir # cp -a $auth_file $PWD$auth_dir/. # fi # cp -f /etc/resolv.conf etc/resolv.conf rm -f etc/resolv.conf cp -a /etc/resolv.conf etc/resolv.conf if ${keys:=false}; then if [ -e ~/.ssh ]; then mkdir -p root cp -Prfd ~/.ssh root/. fi fi } launch_chroot() { do_mounts if first_or_last; then copy_host_essential if which dbus-uuidgen > /dev/null; then mkdir -p var/lib/dbus dbus-uuidgen --get > var/lib/dbus/machine-id fi fi if [ -n "$chuser" ]; then chroot_cmd="chroot --userspec=$chuser" else chroot_cmd="chroot" fi $chroot_cmd "$PWD" /ts/TS_ENV howdiditgo=$? if ${keys:=false}; then kill_ssh_agent secure_keys fi if first_or_last ; then do_unmounts if [ -n "$auth_file" ]; then rm $PWD$auth_file fi if [ -e cleanstage2 ] ; then ts/bin/clean_chroot fi fi } kill_ssh_agent() { if [ -f root/.agent.env ] ; then . root/.agent.env > /dev/null kill -9 $SSH_AGENT_PID > /dev/null 2>&1 fi } secure_keys() { if [ -e root/.ssh ]; then rm -rf root/.ssh fi } do_unmounts() { for mount in dev/pts dev tmp proc sys run/pcscd run/user/0 run/user/$SUDO_UID run; do if [ -e $mount ]; then while mounted $mount ; do umount -l $mount if mounted $mount; then fuser -kv "$PWD/$mount" > /dev/null 2>&1 fi done fi done if [ -e mnt/wsl ]; then for mount in `ls -A mnt`; do while mounted mnt/$mount; do umount -l mnt/$mount done done fi } main() { get_opts $@ state_checks launch_chroot if [ -e dostage4 ] ; then copy_host_essential launch_chroot fi if [ -e installed ] ; then rm installed if [ "$install_only" == "true" ] ; then exit 0 else copy_host_essential launch_chroot fi fi } trap "do_unmounts; exit 1" SIGINT SIGHUP SIGTERM main $@ if [ "$howdiditgo" == "0" ] ; then echo -e "\nGoodbye. \n" ; fi exit $howdiditgo