Renamed the script and added two actions (chroot and shell)

This commit is contained in:
Suwako Moriya 2019-05-01 02:49:41 +02:00
parent b4c8c68113
commit b659d183a6
2 changed files with 246 additions and 148 deletions

246
gentchroot Executable file
View File

@ -0,0 +1,246 @@
#!/bin/bash
#Setting up color codes
SCRIPT_NAME=$0
REAL_ARGS=$@
Black='\033[0;30m'
Red='\033[0;31m'
Green='\033[0;32m'
Orange='\033[0;33m'
Blue='\033[0;34m'
Purple='\033[0;35m'
Cyan='\033[0;36m'
Light_Gray='\033[0;37m'
Dark_Gray='\033[1;30m'
Light_Red='\033[1;31m'
Light_Green='\033[1;32m'
Yellow='\033[1;33m'
Light_Blue='\033[1;34m'
Light_Purple='\033[1;35m'
Light_Cyan='\033[1;36m'
White='\033[1;37m'
NC='\033[0m'
edebug () {
einfo $@
}
einfo () {
echo -e " ${Green}*${NC} ${@:1}${NC}"
}
ewarn () {
echo -e " ${Yellow}*${NC} ${@:1}${NC}"
}
eerror () {
echo -e " ${Red}*${Light_Red} ${@:1}${NC}"
}
die () {
echo
eerror die called, ending.
exit 1
}
end () {
exit 0
}
show_help () {
echo -e "USAGE : ${Light_Green}${SCRIPT_NAME}${NC} ${Yellow}[options]${NC} ${Green}<action>${NC} ${Light_Red}</path/to/target>${NC} "
echo -e
echo -e "${Green}ACTIONS :${NC}"
echo -e " install Install a gentoo environment in the target directory."
echo -e " If one is already installed, it will simply overwrite the profile and update @system"
echo -e " chroot Chroot in the target directory."
echo -e " shell Open a shell with correct exports in order to run emerge in the target repository."
echo -e " Useful when building an embeded system. (compilation happens on host's root)"
echo -e "${Yellow}OPTIONS :${NC}"
echo -e " -h --help Show this help."
echo -e " -u --userland,--no-root Allow the script to run in userland."
echo -e " -p --profile Use the specified profile."
echo -e " This value will be used in a eselect profile set command."
echo -e " -f --force Run even if something fails."
echo -e "${Light_Red}TARGET :${NC}"
echo -e " </path/to/target> Path to the future chroot installation,"
echo -e " can be . to install in the current directory"
}
getopt --test
if [ $? != 4 ]; then
eerror "Your installation doesn't support enhanced getopt."
die
fi
#Setting up the getopt parser
echo -e ${Light_Blue} -- Welcome to Gentoo Chroot Helper -- ${NC}
echo
SHORT="hup:f"
LONG="help,userland,no-root,profile:,force"
OPTS=$(getopt --options $SHORT --long $LONG --name "$0" -- "$@")
if [ $? != 0 ]
then eerror Failed to parse options
die
fi
eval set -- "$OPTS"
#Setting default values
NO_ROOT=false
SHOW_HELP=false
FORCE=false
TARGET=""
ACTION=""
PROFILE="12"
#Extracting arguments
while true
do case "$1" in
-h | --help )
SHOW_HELP=true
shift
;;
-u | --userland | --no-root )
NO_ROOT=true
shift
;;
-p | --profile )
PROFILE="$2"
shift 2
;;
-f | --force )
FORCE=true
shift
;;
-- )
shift
;;
*)
if ! [ -z ${ACTION} ]
then TARGET=$1
break
fi
if [ -z ${ACTION} ]
then ACTION=$1
fi
shift
;;
esac
if [ -z "$1" ]
then
break
fi
done
if $SHOW_HELP
then
show_help
end
fi
if [ -z ${ACTION} ]
then eerror Please specify an action.
ewarn Run ${SCRIPT_NAME} --help for more info.
die
fi
if [ -z ${TARGET} ]
then eerror Please specify a target.
ewarn Run ${SCRIPT_NAME} --help for more info.
die
fi
if ! ${NO_ROOT}
then
if [ "$EUID" -ne 0 ]
then eerror This script should be ran as root.
ewarn If you want to run the script anyway use the --no-root argument.
ewarn Warning : Userland chroot generation has not been tested yet.
die
fi
fi
if ${NO_ROOT}
then ewarn Warning : NO_ROOT is experimental.
fi
if ${FORCE}
then
die () {
echo
eerror die called, but --force option is set.
}
fi
if [ ${ACTION} == "install" ]
then
PROFILE_CHECK=$(eselect profile list |grep ${PROFILE})
if [ -z "${PROFILE_CHECK}" ]
then eerror Invalid profile specified : ${PROFILE}
die
fi
PORTAGE_RSYNC_OPTS=$(emerge --info|grep PORTAGE_RSYNC_OPTS)
PORTAGE_RSYNC_OPTS=${PORTAGE_RSYNC_OPTS:20}
PORTAGE_RSYNC_OPTS=${PORTAGE_RSYNC_OPTS%\"*}
einfo Installing @system in ${Red}${TARGET}${NC}
einfo Profile selected : ${Light_Green}${PROFILE_CHECK}${NC}
echo
mkdir -p ${TARGET}/etc/portage || { eerror Could not create the directory; die; }
cd ${TARGET} || die
TARGET=$(pwd)
edebug Entered ${Light_Blue}${TARGET}${NC}
mkdir -p ${TARGET}/usr/portage/
export ROOT=${TARGET}
export PORTAGE_CONFIGROOT=${TARGET}
export PORTDIR=${TARGET}/usr/portage
einfo Syncing /usr/portage and ${TARGET} Gentoo Repositories.
rsync ${PORTAGE_RSYNC_OPTS} /usr/portage/ ${PORTDIR} || { eerror Could not rsync .; die; }
einfo Setting up the profile.
rm ${TARGET}/etc/portage/make.profile 2>/dev/null
ln -s ../../usr/portage/profiles/default/linux/amd64/17.0 ${TARGET}/etc/portage/make.profile
eselect profile set ${PROFILE} || { eerror Could not set profile.; die; }
einfo Installing ${Red}@system${NC}
FEATURES="buildpkg" emerge --noreplace -k -j8 @system || { eerror Could not emerge @system ; die; }
einfo Installing chroot scripts in ${Light_Green}${TARGET}${NC}
echo "#!/bin/bash" > mountchroot.sh
echo "mkdir -p ${TARGET}/proc ${TARGET}/sys ${TARGET}/dev || exit 1" >> mountchroot.sh
echo "cp --dereference /etc/resolv.conf ${TARGET}/etc/ || exit 1" >> mountchroot.sh
echo "mount --types proc /proc ${TARGET}/proc || exit 1" >> mountchroot.sh
echo "mount --rbind /sys ${TARGET}/sys || exit 1" >> mountchroot.sh
echo "mount --make-rslave ${TARGET}/sys || exit 1" >> mountchroot.sh
echo "mount --rbind /dev ${TARGET}/dev || exit 1" >> mountchroot.sh
echo "mount --make-rslave ${TARGET}/dev || exit 1" >> mountchroot.sh
echo "#!/bin/bash" > umountchroot.sh
echo "umount -R ${TARGET}/proc || exit 1" >> umountchroot.sh
echo "umount -R ${TARGET}/sys || exit 1" >> umountchroot.sh
echo "umount -R ${TARGET}/dev || exit 1" >> umountchroot.sh
echo "rmdir ${TARGET}/proc ${TARGET}/sys ${TARGET}/dev" >> umountchroot.sh
chmod +x mountchroot.sh umountchroot.sh
fi
if [ "${ACTION}" == "chroot" ]
then
cd ${TARGET} || die
einfo Mounting /proc /sys and /dev.
./mountchroot.sh || { eerror Could not mount the filesystem; eerror If you haven\'t already, run ${Yellow}${SCRIPT_NAME} install ${TARGET}${NC}; ./umountchroot.sh; die; }
einfo Chrooting in ${Green}${TARGET}${NC}.
einfo Note: Run exit to exit the chroot.
export PS1="( $(pwd) ) [chroot] ${PS1}"
chroot . /bin/bash -c "bash ; exit 0" && { einfo Left the chroot.; } || { eerror Could not chroot; ./umountchroot.sh; die; }
einfo Unmounting /proc /sys and /dev.
./umountchroot.sh || { ewarn Could not unmount the filesystem, a process is probably running inside ${Yellow}${TARGET}${NC}.; ewarn Be carefull when dealing with ${Yellow}${TARGET}${NC} as it contains block devices; }
fi
if [ "${ACTION}" == "shell" ]
then
cd ${TARGET} || die
TARGET=$(pwd)
export ROOT=${TARGET}
export PORTAGE_CONFIGROOT=${TARGET}
export PORTDIR=${TARGET}/usr/portage
einfo You are entering a new shell.
einfo When you have finished please run exit to end the program.
cd ${TARGET}
bash
einfo You have left the shell.
fi

View File

@ -1,148 +0,0 @@
#!/bin/bash
#Setting up color codes
SCRIPT_NAME=$0
Black='\033[0;30m'
Red='\033[0;31m'
Green='\033[0;32m'
Orange='\033[0;33m'
Blue='\033[0;34m'
Purple='\033[0;35m'
Cyan='\033[0;36m'
Light_Gray='\033[0;37m'
Dark_Gray='\033[1;30m'
Light_Red='\033[1;31m'
Light_Green='\033[1;32m'
Yellow='\033[1;33m'
Light_Blue='\033[1;34m'
Light_Purple='\033[1;35m'
Light_Cyan='\033[1;36m'
White='\033[1;37m'
NC='\033[0m'
edebug () {
einfo $@
}
einfo () {
echo -e " ${Green}*${NC} ${@:1}${NC}"
}
ewarn () {
echo -e " ${Yellow}*${NC} ${@:1}${NC}"
}
eerror () {
echo -e " ${Red}*${Light_Red} ${@:1}${NC}"
}
die () {
echo
eerror die called, ending.
exit 1
}
end () {
exit 0
}
show_help () {
echo -e "USAGE : ${Light_Green}${SCRIPT_NAME}${NC} ${Yellow}[options]${NC} ${Light_Red}</path/to/target>${NC} "
echo -e
echo -e "${Yellow}OPTIONS :${NC}"
echo -e " -h --help Show this help."
echo -e " -u --userland,--no-root Allow the script to run in userland."
echo -e " -p --profile Use the specified profile."
echo -e " This value will be used in a eselect profile set command."
echo -e "${Light_Red}TARGET :${NC}"
echo -e " </path/to/target> Path to the future chroot installation,"
echo -e " can be . to install in the current directory"
}
getopt --test
if [ $? != 4 ]; then
eerror "Your installation doesn't support enhanced getopt."
die
fi
#Setting up the getopt parser
echo -e ${Light_Blue} -- Welcome to Gentoo Chroot Helper -- ${NC}
echo
SHORT="hup:"
LONG="help,userland,no-root,profile:"
OPTS=$(getopt --options $SHORT --long $LONG --name "$0" -- "$@")
if [ $? != 0 ]
then eerror Failed to parse options
die
fi
eval set -- "$OPTS"
#Setting default values
NO_ROOT=false
TARGET=false
PROFILE="12"
#Extracting arguments
while true
do case "$1" in
-h | --help )
show_help
end
shift
;;
-u | --userland | --no-root )
NO_ROOT=true
shift
;;
-p | --profile )
PROFILE="$2"
shift 2
;;
-- )
shift
;;
*)
TARGET=$1
break
;;
esac
done
if [ -z ${TARGET} ]
then eerror Please specify a target.
ewarn Run ${SCRIPT_NAME} --help for more info.
die
fi
if [ ${NO_ROOT} != true ]
then
if [ "$EUID" -ne 0 ]
then eerror This script should be ran as root.
ewarn If you want to run the script anyway use the --no-root argument.
ewarn Warning : Userland chroot generation has not been tested yet.
die
fi
fi
if [ ${NO_ROOT} == true ]
then ewarn Warning : NO_ROOT is experimental.
fi
PROFILE_CHECK=$(eselect profile list |grep ${PROFILE})
if [ -z "${PROFILE_CHECK}" ]
then eerror Invalid profile specified : ${PROFILE}
die
fi
PORTAGE_RSYNC_OPTS=$(emerge --info|grep PORTAGE_RSYNC_OPTS)
PORTAGE_RSYNC_OPTS=${PORTAGE_RSYNC_OPTS:20}
PORTAGE_RSYNC_OPTS=${PORTAGE_RSYNC_OPTS%\"*}
einfo Installing @system in ${Red}${TARGET}${NC}
einfo Profile selected : ${Light_Green}${PROFILE_CHECK}${NC}
echo
mkdir -p ${TARGET}/etc/portage || (eerror Could not create the directory; die)
cd ${TARGET} || die
TARGET=$(pwd)
edebug Entered ${Light_Blue}${TARGET}${NC}
mkdir -p ${TARGET}/usr/portage/
export ROOT=${TARGET}
export PORTAGE_CONFIGROOT=${TARGET}
export PORTDIR=${TARGET}/usr/portage
einfo Syncing /usr/portage and ${TARGET} Gentoo Repositories.
rsync ${PORTAGE_RSYNC_OPTS} /usr/portage/ ${PORTDIR} || (eerror Could not rsync .; die)
ln -s ../../usr/portage/profiles/default/linux/amd64/17.0 ${TARGET}/etc/portage/make.profile
eselect profile set ${PROFILE} || (eerror Could not set profile.; die)
FEATURES="buildpkg" emerge -k -j8 @system || (eerror Could not emerge @system ; die)