manjaro-tools/bin/manjaro-chroot.in

95 lines
2.3 KiB
Text
Raw Normal View History

2014-10-04 01:43:42 +02:00
#!/bin/bash
2014-11-18 19:20:53 +01:00
#
2014-10-04 15:41:22 +02:00
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
2014-10-04 01:43:42 +02:00
#
2014-10-04 15:41:22 +02:00
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
2014-10-04 01:43:42 +02:00
2014-10-08 13:14:28 +02:00
version=@version@
2014-10-04 15:41:22 +02:00
shopt -s extglob
2014-10-04 01:43:42 +02:00
2015-06-09 00:01:31 +02:00
LIBDIR='@libdir@'
2015-12-03 23:08:43 +01:00
SYSCONFDIR='@sysconfdir@'
2015-06-09 00:01:31 +02:00
[[ -r ${LIBDIR}/util-msg.sh ]] && source ${LIBDIR}/util-msg.sh
import ${LIBDIR}/util.sh
import ${LIBDIR}/util-mount.sh
display_settings(){
2016-09-15 23:58:18 +02:00
show_version
show_config
2016-09-15 23:58:18 +02:00
msg "ARGS:"
msg2 "automount: %s" "${automount}"
msg2 "run_args: %s" "${run_args[*]}"
2016-09-15 23:58:18 +02:00
msg "PATHS:"
msg2 "chrootdir: %s" "${chrootdir}"
}
load_user_info
2016-06-14 17:54:44 +02:00
load_config "${USERCONFDIR}/manjaro-tools.conf" || load_config "${SYSCONFDIR}/manjaro-tools.conf"
automount=false
pretend=false
2014-10-04 10:54:55 +02:00
2014-10-04 01:43:42 +02:00
usage() {
2016-09-15 23:58:18 +02:00
echo "usage: ${0##*/} -a [or] ${0##*/} chroot-dir [command]"
echo ' -a Automount detected linux system'
echo ' -q Query settings and pretend'
echo ' -h Print this help message'
echo ''
echo " If 'command' is unspecified, ${0##*/} will launch /bin/sh."
echo ''
echo " If 'automount' is true, ${0##*/} will launch /bin/bash"
echo " and ${chrootdir}."
echo ''
echo ''
exit $1
}
2014-10-04 01:43:42 +02:00
orig_argv=("$@")
2014-10-04 01:43:42 +02:00
opts=':haq'
2014-10-04 01:43:42 +02:00
while getopts ${opts} arg; do
2016-09-15 23:58:18 +02:00
case "${arg}" in
a) automount=true ;;
q) pretend=true ;;
h|?) usage 0 ;;
*) echo "invalid argument ${arg}"; usage 1 ;;
esac
done
shift $(( OPTIND - 1 ))
2014-10-04 01:43:42 +02:00
check_root "$0" "${orig_argv[@]}"
2014-10-04 01:43:42 +02:00
if ${automount};then
2016-09-15 23:58:18 +02:00
chrootdir=/mnt
run_args=/bin/bash
2015-07-03 21:50:08 +02:00
2016-09-15 23:58:18 +02:00
${pretend} && display_settings && exit 1
2015-07-03 21:50:08 +02:00
2016-09-15 23:58:18 +02:00
select_os "${chrootdir}"
else
2016-09-15 23:58:18 +02:00
chrootdir=$1
shift
run_args="$@"
2015-07-03 21:50:08 +02:00
2016-09-15 23:58:18 +02:00
[[ -d ${chrootdir} ]] || die "Can't create chroot on non-directory %s" "${chrootdir}"
2015-07-02 22:54:24 +02:00
2016-09-15 23:58:18 +02:00
${pretend} && display_settings && exit 1
2015-07-03 21:50:08 +02:00
2016-12-05 21:24:16 +01:00
chroot_api_efi_mount "${chrootdir}" || die "failed to setup API filesystems in chroot %s" "${chrootdir}"
2016-09-15 23:58:18 +02:00
chroot_mount /etc/resolv.conf "${chrootdir}/etc/resolv.conf" --bind
fi
2015-07-03 11:08:09 +02:00
SHELL=/bin/sh unshare --fork --pid chroot "${chrootdir}" ${run_args[*]}