5 # generated from util-linux source: libmount/src/utils.c
6 declare -A pseudofs_types=([anon_inodefs]=1
21 [fuse.gvfs-fuse-daemon]=1
39 # generated from: pkgfile -vbr '/fsck\..+' | awk -F. '{ print $NF }' | sort
40 declare -A fsck_types=([cramfs]=1
53 out() { printf "$1 $2\n" "${@:3}"; }
54 error() { out "==> ERROR:" "$@"; } >&2
55 warning() { out "==> WARNING:" "$@"; } >&2
56 msg() { out "==>" "$@"; }
57 msg2() { out " ->" "$@";}
58 die() { error "$@"; exit 1; }
68 [[ $1 = "$i" ]] && return 0
74 mount "$@" && CHROOT_ACTIVE_MOUNTS=("$2" "${CHROOT_ACTIVE_MOUNTS[@]}")
77 chroot_maybe_add_mount() {
85 CHROOT_ACTIVE_MOUNTS=()
86 [[ $(trap -p EXIT) ]] && die '(BUG): attempting to overwrite existing EXIT trap'
87 trap 'chroot_teardown' EXIT
89 chroot_add_mount proc "$1/proc" -t proc -o nosuid,noexec,nodev &&
90 chroot_add_mount sys "$1/sys" -t sysfs -o nosuid,noexec,nodev,ro &&
91 ignore_error chroot_maybe_add_mount "[[ -d '$1/sys/firmware/efi/efivars' ]]" \
92 efivarfs "$1/sys/firmware/efi/efivars" -t efivarfs -o nosuid,noexec,nodev &&
93 chroot_add_mount udev "$1/dev" -t devtmpfs -o mode=0755,nosuid &&
94 chroot_add_mount devpts "$1/dev/pts" -t devpts -o mode=0620,gid=5,nosuid,noexec &&
95 chroot_add_mount shm "$1/dev/shm" -t tmpfs -o mode=1777,nosuid,nodev &&
96 chroot_add_mount /run "$1/run" --bind &&
97 chroot_add_mount tmp "$1/tmp" -t tmpfs -o mode=1777,strictatime,nodev,nosuid
101 if (( ${#CHROOT_ACTIVE_MOUNTS[@]} )); then
103 umount "${CHROOT_ACTIVE_MOUNTS[@]}"
105 unset CHROOT_ACTIVE_MOUNTS
112 valid_number_of_base() {
113 local base=$1 len=${#2} i=
115 for (( i = 0; i < len; i++ )); do
116 try_cast "$base" "${2:i:1}" || return 1
124 local {a..f}= {A..F}=
126 for (( i = 0; i < ${#1}; i++ )); do
130 printf -v chr '%03o' "'$chr"
141 local i= chr= out= len=$(( ${#1} - 4 ))
142 local {a..f}= {A..F}=
144 for (( i = 0; i < len; i++ )); do
148 if valid_number_of_base 8 "${1:i+1:3}" ||
149 valid_number_of_base 16 "${1:i+1:3}"; then
150 printf -v chr '%b' "${1:i:4}"
158 printf '%s' "$out${1:i}"
161 optstring_match_option() {
162 local candidate pat patterns
164 IFS=, read -ra patterns <<<"$1"
165 for pat in "${patterns[@]}"; do
166 if [[ $pat = *=* ]]; then
167 # "key=val" will only ever match "key=val"
170 # "key" will match "key", but also "key=anyval"
174 [[ $pat = "$candidate" ]] && return 0
180 optstring_remove_option() {
181 local o options_ remove=$2 IFS=,
183 read -ra options_ <<<"${!1}"
185 for o in "${!options_[@]}"; do
186 optstring_match_option "$remove" "${options_[o]}" && unset 'options_[o]'
189 declare -g "$1=${options_[*]}"
192 optstring_normalize() {
193 local o options_ norm IFS=,
195 read -ra options_ <<<"${!1}"
197 # remove empty fields
198 for o in "${options_[@]}"; do
199 [[ $o ]] && norm+=("$o")
202 # avoid empty strings, reset to "defaults"
203 declare -g "$1=${norm[*]:-defaults}"
206 optstring_append_option() {
207 if ! optstring_has_option "$1" "$2"; then
208 declare -g "$1=${!1},$2"
211 optstring_normalize "$1"
214 optstring_prepend_option() {
217 if ! optstring_has_option "$1" "$2"; then
218 declare -g "$1=$2,${!1}"
221 optstring_normalize "$1"
224 optstring_get_option() {
227 IFS=, read -ra opts <<<"${!1}"
228 for o in "${opts[@]}"; do
229 if optstring_match_option "$2" "$o"; then
238 optstring_has_option() {
241 optstring_get_option "$1" "$2"
244 dm_name_for_devnode() {
245 read dm_name <"/sys/class/block/${1#/dev/}/dm/name"
246 if [[ $dm_name ]]; then
247 printf '/dev/mapper/%s' "$dm_name"
249 # don't leave the caller hanging, just print the original name
250 # along with the failure.
252 error 'Failed to resolve device mapper name for: %s' "$1"
256 fstype_is_pseudofs() {
257 (( pseudofs_types["$1"] ))
261 (( fsck_types["$1"] ))
267 usage: ${0##*/} chroot-dir [command]
269 -h Print this help message
270 -u <user>[:group] Specify non-root user and optional group to use
272 If 'command' is unspecified, ${0##*/} will launch /bin/bash.
274 Note that when using arch-chroot, the target chroot directory *should* be a
275 mountpoint. This ensures that tools such as pacman(8) or findmnt(8) have an
276 accurate hierarchy of the mounted filesystems within the chroot.
278 If your chroot target is not a mountpoint, you can bind mount the directory on
279 itself to make it a mountpoint, i.e. 'mount --bind /your/chroot /your/chroot'.
284 chroot_add_resolv_conf() {
285 local chrootdir=$1 resolv_conf=$1/etc/resolv.conf
287 [[ -e /etc/resolv.conf ]] || return 0
289 # Handle resolv.conf as a symlink to somewhere else.
290 if [[ -L $chrootdir/etc/resolv.conf ]]; then
291 # readlink(1) should always give us *something* since we know at this point
292 # it's a symlink. For simplicity, ignore the case of nested symlinks.
293 resolv_conf=$(readlink "$chrootdir/etc/resolv.conf")
294 if [[ $resolv_conf = /* ]]; then
295 resolv_conf=$chrootdir$resolv_conf
297 resolv_conf=$chrootdir/etc/$resolv_conf
300 # ensure file exists to bind mount over
301 if [[ ! -f $resolv_conf ]]; then
302 install -Dm644 /dev/null "$resolv_conf" || return 1
304 elif [[ ! -e $chrootdir/etc/resolv.conf ]]; then
305 # The chroot might not have a resolv.conf.
309 chroot_add_mount /etc/resolv.conf "$resolv_conf" --bind
312 while getopts ':hu:' flag; do
322 die '%s: option requires an argument -- '\''%s'\' "${0##*/}" "$OPTARG"
325 die '%s: invalid option -- '\''%s'\' "${0##*/}" "$OPTARG"
329 shift $(( OPTIND - 1 ))
331 (( EUID == 0 )) || die 'This script must be run with root privileges'
332 (( $# )) || die 'No chroot directory specified'
336 [[ -d $chrootdir ]] || die "Can't create chroot on non-directory %s" "$chrootdir"
338 #if ! mountpoint -q "$chrootdir"; then
339 # warning "$chrootdir is not a mountpoint. This may have undesirable side effects."
342 chroot_setup "$chrootdir" || die "failed to setup chroot %s" "$chrootdir"
343 chroot_add_resolv_conf "$chrootdir" || die "failed to setup resolv.conf"
346 [[ $userspec ]] && chroot_args+=(--userspec "$userspec")
348 SHELL=/bin/bash unshare --fork --pid chroot "${chroot_args[@]}" -- "$chrootdir" "$@"