2012-06-18 04:44:55 +08:00
|
|
|
#!/bin/bash
|
2024-04-07 00:09:04 +08:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-only
|
2012-06-18 04:44:55 +08:00
|
|
|
|
|
|
|
shopt -s extglob
|
|
|
|
|
2022-07-28 22:02:31 +08:00
|
|
|
unshare=0
|
2023-03-25 05:12:02 +08:00
|
|
|
keepresolvconf=0
|
2021-11-18 03:08:10 +08:00
|
|
|
|
2023-03-27 00:57:04 +08:00
|
|
|
m4_include(common)
|
|
|
|
|
2012-06-18 05:52:39 +08:00
|
|
|
usage() {
|
|
|
|
cat <<EOF
|
2022-02-25 04:42:05 +08:00
|
|
|
usage: ${0##*/} chroot-dir [command] [arguments...]
|
2012-09-09 03:29:18 +08:00
|
|
|
|
2016-04-18 19:05:15 +08:00
|
|
|
-h Print this help message
|
2021-11-18 03:08:10 +08:00
|
|
|
-N Run in unshare mode as a regular user
|
2016-04-18 19:05:15 +08:00
|
|
|
-u <user>[:group] Specify non-root user and optional group to use
|
2023-03-25 05:12:02 +08:00
|
|
|
-r Do not change the resolv.conf within the chroot
|
2012-11-13 10:00:09 +08:00
|
|
|
|
2016-06-12 12:57:16 +08:00
|
|
|
If 'command' is unspecified, ${0##*/} will launch /bin/bash.
|
2012-06-18 05:52:39 +08:00
|
|
|
|
2024-10-29 12:28:58 +08:00
|
|
|
Note that when using future-chroot, the target chroot directory *should* be a
|
2018-10-16 00:57:48 +08:00
|
|
|
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
|
|
|
|
itself to make it a mountpoint, i.e. 'mount --bind /your/chroot /your/chroot'.
|
|
|
|
|
2012-06-18 05:52:39 +08:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2018-06-06 05:57:02 +08:00
|
|
|
resolve_link() {
|
|
|
|
local target=$1
|
|
|
|
local root=$2
|
|
|
|
|
|
|
|
# If a root was given, make sure it ends in a slash.
|
|
|
|
[[ -n $root && $root != */ ]] && root=$root/
|
|
|
|
|
|
|
|
while [[ -L $target ]]; do
|
|
|
|
target=$(readlink -m "$target")
|
|
|
|
# If a root was given, make sure the target is under it.
|
|
|
|
# Make sure to strip any leading slash from target first.
|
|
|
|
[[ -n $root && $target != $root* ]] && target=$root${target#/}
|
|
|
|
done
|
|
|
|
|
|
|
|
printf %s "$target"
|
|
|
|
}
|
|
|
|
|
2015-01-27 07:12:33 +08:00
|
|
|
chroot_add_resolv_conf() {
|
2018-06-06 05:57:02 +08:00
|
|
|
local chrootdir=$1
|
2023-03-25 04:09:20 +08:00
|
|
|
local src
|
2023-08-02 22:05:58 +08:00
|
|
|
local dest="$chrootdir/etc/resolv.conf"
|
2023-03-25 04:09:20 +08:00
|
|
|
|
|
|
|
src=$(resolve_link /etc/resolv.conf)
|
2018-06-06 05:57:02 +08:00
|
|
|
|
|
|
|
# If we don't have a source resolv.conf file, there's nothing useful we can do.
|
|
|
|
[[ -e $src ]] || return 0
|
|
|
|
|
2023-08-02 22:05:58 +08:00
|
|
|
if [[ ! -e "$dest" && ! -h "$dest" ]]; then
|
|
|
|
# There may be no resolv.conf in the chroot. In this case, we'll just exit.
|
|
|
|
# The chroot environment must not be concerned with DNS resolution.
|
|
|
|
return 0
|
2015-01-27 07:12:33 +08:00
|
|
|
fi
|
|
|
|
|
2023-08-02 22:05:58 +08:00
|
|
|
chroot_add_mount "$src" "$dest" -c --bind
|
2015-01-27 07:12:33 +08:00
|
|
|
}
|
|
|
|
|
2024-10-29 12:28:58 +08:00
|
|
|
future-chroot() {
|
2023-03-25 04:18:14 +08:00
|
|
|
(( EUID == 0 )) || die 'This script must be run with root privileges'
|
|
|
|
|
|
|
|
[[ -d $chrootdir ]] || die "Can't create chroot on non-directory %s" "$chrootdir"
|
|
|
|
|
|
|
|
$setup "$chrootdir" || die "failed to setup chroot %s" "$chrootdir"
|
2023-03-25 05:12:02 +08:00
|
|
|
if (( ! keepresolvconf )); then
|
|
|
|
chroot_add_resolv_conf "$chrootdir" || die "failed to setup resolv.conf"
|
|
|
|
fi
|
2023-03-25 04:18:14 +08:00
|
|
|
|
|
|
|
if ! mountpoint -q "$chrootdir"; then
|
|
|
|
warning "$chrootdir is not a mountpoint. This may have undesirable side effects."
|
|
|
|
fi
|
|
|
|
|
|
|
|
chroot_args=()
|
|
|
|
[[ $userspec ]] && chroot_args+=(--userspec "$userspec")
|
|
|
|
|
|
|
|
SHELL=/bin/bash $pid_unshare chroot "${chroot_args[@]}" -- "$chrootdir" "${args[@]}"
|
|
|
|
}
|
|
|
|
|
2023-03-25 05:12:02 +08:00
|
|
|
while getopts ':hNu:r' flag; do
|
2016-04-18 19:05:15 +08:00
|
|
|
case $flag in
|
|
|
|
h)
|
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
;;
|
2021-11-18 03:08:10 +08:00
|
|
|
N)
|
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)
|
|
|
|
userspec=$OPTARG
|
|
|
|
;;
|
2023-03-25 05:12:02 +08:00
|
|
|
r)
|
|
|
|
keepresolvconf=1
|
|
|
|
;;
|
2016-04-18 19:05:15 +08:00
|
|
|
:)
|
|
|
|
die '%s: option requires an argument -- '\''%s'\' "${0##*/}" "$OPTARG"
|
|
|
|
;;
|
|
|
|
?)
|
|
|
|
die '%s: invalid option -- '\''%s'\' "${0##*/}" "$OPTARG"
|
|
|
|
;;
|
|
|
|
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'
|
2012-06-20 22:25:18 +08:00
|
|
|
chrootdir=$1
|
2012-08-13 23:12:41 +08:00
|
|
|
shift
|
2012-06-18 05:52:39 +08:00
|
|
|
|
2021-11-18 03:08:10 +08:00
|
|
|
args=("$@")
|
2022-07-28 22:02:31 +08:00
|
|
|
if (( unshare )); then
|
2023-03-25 04:56:06 +08:00
|
|
|
setup=unshare_setup
|
2024-10-29 12:28:58 +08:00
|
|
|
$mount_unshare bash -c "$(declare_all); future-chroot"
|
2022-07-28 22:02:31 +08:00
|
|
|
else
|
2023-03-25 04:56:06 +08:00
|
|
|
setup=chroot_setup
|
2024-10-29 12:28:58 +08:00
|
|
|
future-chroot
|
2022-07-28 22:02:31 +08:00
|
|
|
fi
|