2012-06-18 04:44:55 +08:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
shopt -s extglob
|
|
|
|
|
2022-12-20 16:38:44 +08:00
|
|
|
# m4_include() is recognized as a function definition
|
|
|
|
# shellcheck source=common disable=SC1073,SC1065,SC1064,SC1072
|
2012-06-18 17:07:37 +08:00
|
|
|
m4_include(common)
|
2012-06-18 04:44:55 +08:00
|
|
|
|
2021-11-18 03:08:10 +08:00
|
|
|
setup=chroot_setup
|
2022-07-28 22:02:31 +08:00
|
|
|
unshare=0
|
2022-12-20 16:38:44 +08:00
|
|
|
userspec=''
|
|
|
|
chroot_args=()
|
2021-11-18 03:08:10 +08:00
|
|
|
|
2012-06-18 05:52:39 +08:00
|
|
|
usage() {
|
|
|
|
cat <<EOF
|
2022-12-20 16:38:44 +08:00
|
|
|
usage: ${0##*/} [options] chroot_dir [command [arguments...]]
|
2012-09-09 03:29:18 +08:00
|
|
|
|
2022-12-20 16:38:44 +08:00
|
|
|
Options:
|
2021-11-18 03:08:10 +08:00
|
|
|
-N Run in unshare mode as a regular user
|
2022-12-20 16:38:44 +08:00
|
|
|
-u <user>[:group] Specify non-root user and group (optional) to use
|
2022-12-20 17:47:59 +08:00
|
|
|
|
2022-12-20 16:38:44 +08:00
|
|
|
-h Print this help message
|
2022-12-20 17:47:59 +08:00
|
|
|
|
2022-12-20 16:38:44 +08:00
|
|
|
If 'command' is unspecified, arch-chroot will launch /bin/bash.
|
2012-06-18 05:52:39 +08:00
|
|
|
|
2018-10-16 00:57:48 +08:00
|
|
|
Note that when using arch-chroot, the target chroot directory *should* be a
|
|
|
|
mountpoint. This ensures that tools such as pacman(8) or findmnt(8) have an
|
|
|
|
accurate hierarchy of the mounted filesystems within the chroot.
|
|
|
|
If your chroot target is not a mountpoint, you can bind mount the directory on
|
2022-12-20 16:38:44 +08:00
|
|
|
itself to make it one, i.e. 'mount --bind chroot_dir chroot_dir'.
|
2018-10-16 00:57:48 +08:00
|
|
|
|
2012-06-18 05:52:39 +08:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2022-12-20 16:38:44 +08:00
|
|
|
if [[ -z $1 || $1 = @(-h|--help) ]]; then
|
|
|
|
usage
|
|
|
|
exit $(( $# ? 0 : 1 ))
|
|
|
|
fi
|
|
|
|
|
|
|
|
while getopts ':Nu:' flag; do
|
2016-04-18 19:05:15 +08:00
|
|
|
case $flag in
|
2021-11-18 03:08:10 +08:00
|
|
|
N)
|
|
|
|
setup=unshare_setup
|
2022-07-28 22:02:31 +08:00
|
|
|
unshare=1
|
2021-11-18 03:08:10 +08:00
|
|
|
;;
|
2016-04-18 19:05:15 +08:00
|
|
|
u)
|
2022-12-20 16:38:44 +08:00
|
|
|
userspec="$OPTARG"
|
2016-04-18 19:05:15 +08:00
|
|
|
;;
|
|
|
|
:)
|
2022-12-20 16:38:44 +08:00
|
|
|
die "%s: option requires an argument -- '%s'" "${0##*/}" "$OPTARG"
|
2016-04-18 19:05:15 +08:00
|
|
|
;;
|
|
|
|
?)
|
2022-12-20 16:38:44 +08:00
|
|
|
die "%s: invalid option -- '%s'" "${0##*/}" "$OPTARG"
|
2016-04-18 19:05:15 +08:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $(( OPTIND - 1 ))
|
2012-06-18 04:44:55 +08:00
|
|
|
|
2016-06-12 22:51:04 +08:00
|
|
|
(( $# )) || die 'No chroot directory specified'
|
2022-12-20 16:38:44 +08:00
|
|
|
chrootdir="$1"; shift
|
2012-06-18 05:52:39 +08:00
|
|
|
|
2022-12-20 17:47:59 +08:00
|
|
|
if ! mountpoint -q "$chrootdir"; then
|
|
|
|
warning '%s is not a mountpoint, and thus may cause undesirable side effects. (See help for more info)' "$chrootdir"
|
|
|
|
fi
|
|
|
|
|
|
|
|
command=("$@")
|
|
|
|
|
2021-11-18 03:08:10 +08:00
|
|
|
arch-chroot() {
|
2022-12-20 17:08:10 +08:00
|
|
|
check_root
|
2021-11-18 03:08:10 +08:00
|
|
|
|
2022-12-20 17:47:59 +08:00
|
|
|
[[ -d "$chrootdir" ]] || die '%s: not a directory' "$chrootdir"
|
2018-10-16 00:57:48 +08:00
|
|
|
|
2022-12-20 17:47:59 +08:00
|
|
|
$setup "$chrootdir"
|
|
|
|
chroot_add_resolv_conf "$chrootdir" || die 'Failed to setup resolv.conf in chroot'
|
2021-11-18 03:08:10 +08:00
|
|
|
|
2022-12-20 17:47:59 +08:00
|
|
|
[[ $userspec ]] && chroot_args+=(--userspec="$userspec")
|
2012-06-18 04:44:55 +08:00
|
|
|
|
2022-12-20 17:47:59 +08:00
|
|
|
SHELL=/bin/bash $pid_unshare chroot "${chroot_args[@]}" -- "$chrootdir" "${command[@]}"
|
2021-11-18 03:08:10 +08:00
|
|
|
}
|
2016-04-18 19:05:15 +08:00
|
|
|
|
2022-07-28 22:02:31 +08:00
|
|
|
if (( unshare )); then
|
|
|
|
$mount_unshare bash -c "$(declare_all); arch-chroot"
|
|
|
|
else
|
|
|
|
arch-chroot
|
|
|
|
fi
|
2022-12-20 16:38:44 +08:00
|
|
|
|
|
|
|
# vim: et ts=2 sw=2 ft=sh:
|