#!/bin/bash # License: GNU GPLv2 # # 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. # # 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. m4_include(lib/common.sh) m4_include(lib/archroot.sh) m4_include(lib/mount.sh) m4_include(lib/messages.sh) # $1: chroot kill_chroot_process(){ local prefix="$1" flink pid name for root_dir in /proc/*/root; do flink=$(readlink "$root_dir") if [ "x$flink" != "x" ]; then if [ "x${flink:0:${#prefix}}" = "x$prefix" ]; then # this process is in the chroot... pid=$(basename "$(dirname "$root_dir")") name=$(ps -p "$pid" -o comm=) msg2 "Killing chroot process: %s (%s)" "$name" "$pid" kill -9 "$pid" fi fi done sleep 1 } # umask might have been changed in /etc/profile # ensure that sane default is set again umask 0022 working_dir='' files=() mount_args=("-B:/etc/hosts:/etc/hosts") unshare_args=("-Cfimu") usage() { print "Usage: %s [options] working-dir [chroot arguments]" "${0##*/}" echo prose 'A wrapper around systemd-nspawn (for systemd systems), and chroot-nspawn (for non-systemd systems), to provide support for pacman.' prose 'WIP: The preceding description is not entirely accurate. This version removes support for `systemd-nspawn`; and uses `chroot` rather than `chroot-nspawn`. It would be good to complete the transition as described.' echo flag "Options:" \ "-C <$(_ FILE)>" 'Location of a pacman config file' \ "-M <$(_ FILE)>" 'Location of a makepkg config file' \ "-c <$(_ PATH)>" 'Set pacman cache directory' \ "-f <$(_ FILE)>" 'Copy from the host to the chroot This option may be given multiple times' \ '-s' 'Do not run setarch' \ "-b <$(_ ARGS)>" "Bind mountargs. format: '[mntarg:srcdir:destdir' This option may be given multiple times" \ '-N' 'Disable networking' \ '-h' 'Show this message' exit 1 } while getopts 'hC:M:c:f:sb:N' arg; do case "$arg" in C) pac_conf="$OPTARG" ;; M) makepkg_conf="$OPTARG" ;; c) cache_dir="$OPTARG" ;; f) files+=("$OPTARG") ;; s) nosetarch=1 ;; b) mount_args+=("$OPTARG") ;; N) unshare_args+=("-n") ;; h|?) usage ;; *) error "invalid argument '%s'" "$arg"; usage ;; esac done shift $((OPTIND - 1)) (( $# < 1 )) && die 'You must specify a directory.' check_root working_dir=$(readlink -f "$1") shift 1 [[ -z $working_dir ]] && die 'Please specify a working directory.' if [[ -z $cache_dir ]]; then cache_dirs=($(pacman -v 2>&1 | grep '^Cache Dirs:' | sed 's/Cache Dirs:\s*//g')) else cache_dirs=("$cache_dir") fi pacconf_cmd=$(command -v pacman-conf || command -v pacconf) # shellcheck disable=2016 host_mirror=$($pacconf_cmd --repo extra Server 2> /dev/null | head -1 | sed -r 's#(.*/)extra/os/.*#\1$repo/os/$arch#') # shellcheck disable=2016 [[ $host_mirror == *file://* ]] && host_mirror_path=$(echo "$host_mirror" | sed -r 's#file://(/.*)/\$repo/os/\$arch#\1#g') # {{{ functions build_mount_args() { if [[ -n $host_mirror_path ]]; then mount_args+=("-B:$host_mirror_path:$host_mirror_path") fi mount_args+=("-B:${cache_dirs[0]}:${cache_dirs[0]}") for cache_dir in "${cache_dirs[@]:1}"; do mount_args+=("-Br:$cache_dir:$cache_dir") done } copy_hostconf () { cp -a /etc/pacman.d/gnupg "$working_dir/etc/pacman.d" echo "Server = $host_mirror" >"$working_dir/etc/pacman.d/mirrorlist" [[ -n $pac_conf ]] && cp "$pac_conf" "$working_dir/etc/pacman.conf" [[ -n $makepkg_conf ]] && cp "$makepkg_conf" "$working_dir/etc/makepkg.conf" local file for file in "${files[@]}"; do mkdir -p "$(dirname "$working_dir$file")" cp -T "$file" "$working_dir$file" done sed -r "s|^#?\\s*CacheDir.+|CacheDir = ${cache_dirs[*]}|g" -i "$working_dir/etc/pacman.conf" } chroot_extra_mount() { chroot_add_resolv_conf "${working_dir}" for arg in "${mount_args[@]}"; do local flag=${arg%%:*} dest=${arg##*:} src=${arg%:*} src=${src#*:} chroot_mount "${src}" "${working_dir}${dest}" "${flag}" done } # }}} umask 0022 # Sanity check if [[ ! -f "$working_dir/.arch-chroot" ]]; then die "'%s' does not appear to be an Arch chroot." "$working_dir" elif [[ $(cat "$working_dir/.arch-chroot") != "$CHROOT_VERSION" ]]; then die "chroot '%s' is not at version %s. Please rebuild." "$working_dir" "$CHROOT_VERSION" fi build_mount_args chroot_api_mount "${working_dir}" || die "failed to setup API filesystems in chroot %s" "${working_dir}" chroot_extra_mount cache_dirs+=('/repo/') copy_hostconf eval "$(grep -a '^CARCH=' "$working_dir/etc/makepkg.conf")" case "$CARCH" in armv7h) CARCH=armv7l;; esac [[ -z $nosetarch ]] || unset CARCH ${CARCH:+setarch "$CARCH"} unshare \ "${unshare_args[@]}" \ --mount-proc \ --setgroups allow \ -- \ chroot \ "${working_dir}" \ "$@" ret=$? kill_chroot_process "${working_dir}" exit $ret