future-install-scripts/arch-chroot.in

128 lines
3.5 KiB
Plaintext
Raw Normal View History

2012-06-18 04:44:55 +08:00
#!/bin/bash
shopt -s extglob
m4_include(common)
2012-06-18 04:44:55 +08:00
setup=chroot_setup
unshare=0
2012-06-18 05:52:39 +08:00
usage() {
cat <<EOF
usage: ${0##*/} chroot-dir [command] [arguments...]
2016-04-18 19:05:15 +08:00
-h Print this help message
-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
2012-11-13 10:00:09 +08:00
If 'command' is unspecified, ${0##*/} will launch /bin/bash.
2012-06-18 05:52:39 +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
itself to make it a mountpoint, i.e. 'mount --bind /your/chroot /your/chroot'.
2012-06-18 05:52:39 +08:00
EOF
}
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"
}
chroot_add_resolv_conf() {
local chrootdir=$1
local src=$(resolve_link /etc/resolv.conf)
local dest=$(resolve_link "$chrootdir/etc/resolv.conf" "$chrootdir")
# If we don't have a source resolv.conf file, there's nothing useful we can do.
[[ -e $src ]] || return 0
if [[ ! -e $dest ]]; then
# There are two reasons the destination might not exist:
#
# 1. There may be no resolv.conf in the chroot. In this case, $dest won't exist,
# and it will be equal to $1/etc/resolv.conf. In this case, we'll just exit.
# The chroot environment must not be concerned with DNS resolution.
#
# 2. $1/etc/resolv.conf is (or resolves to) a broken link. The environment
# clearly intends to handle DNS resolution, but something's wrong. Maybe it
# normally creates the target at boot time. We'll (try to) take care of it by
# creating a dummy file at the target, so that we have something to bind to.
# Case 1.
[[ $dest = $chrootdir/etc/resolv.conf ]] && return 0
# Case 2.
install -Dm644 /dev/null "$dest" || return 1
fi
chroot_add_mount "$src" "$dest" --bind
}
while getopts ':hNu:' flag; do
2016-04-18 19:05:15 +08:00
case $flag in
h)
usage
exit 0
;;
N)
setup=unshare_setup
unshare=1
;;
2016-04-18 19:05:15 +08:00
u)
userspec=$OPTARG
;;
:)
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
(( $# )) || die 'No chroot directory specified'
chrootdir=$1
shift
2012-06-18 05:52:39 +08:00
arch-chroot() {
(( EUID == 0 )) || die 'This script must be run with root privileges'
[[ -d $chrootdir ]] || die "Can't create chroot on non-directory %s" "$chrootdir"
2012-06-18 04:44:55 +08:00
$setup "$chrootdir" || die "failed to setup chroot %s" "$chrootdir"
chroot_add_resolv_conf "$chrootdir" || die "failed to setup resolv.conf"
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")
2012-06-18 04:44:55 +08:00
SHELL=/bin/bash $pid_unshare chroot "${chroot_args[@]}" -- "$chrootdir" "${args[@]}"
}
2016-04-18 19:05:15 +08:00
args=("$@")
if (( unshare )); then
$mount_unshare bash -c "$(declare_all); arch-chroot"
else
arch-chroot
fi