#!/bin/bash # 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) source @libdir@/functions.sh CHROOT_VERSION='v3' RUN='' NOCOPY='n' working_dir='' branch='stable' APPNAME=$(basename "${0}") # check for systemd running on build system if [[ -d /run/systemd ]];then nosystemd=false else nosystemd=true fi # usage: usage usage() { echo "Usage: ${APPNAME} [options] working-dir [package-list | app]" echo ' options:' echo ' -r Run "app" within the context of the chroot' echo ' -u Update the chroot via pacman' echo ' -C Location of a pacman config file' echo ' -M Location of a makepkg config file' echo ' -S Location of a pacman-mirrors config file' echo ' -n Do not copy config files into the chroot' echo ' -c Set pacman cache' echo ' -b Set repository branch' echo ' -x Use classic chroot instead of systemd-nspawn' echo ' -h This message' exit 1 } while getopts 'r:ufnxhC:M:S:c:b:' arg; do case "${arg}" in r) RUN="$OPTARG" ;; u) RUN='pacman -Syu --noconfirm' ;; C) pac_conf="$OPTARG" ;; M) makepkg_conf="$OPTARG" ;; n) NOCOPY='y' ;; c) cache_dir="$OPTARG" ;; b) branch="$OPTARG" ;; S) mirrors_conf="$OPTARG" ;; x) nosystemd=true ;; h|?) usage ;; *) error "invalid argument '${arg}'"; usage ;; esac done if (( $EUID != 0 )); then die 'This script must be run as root.' fi shift $(($OPTIND - 1)) if [[ -z $RUN ]] && (( $# < 2 )); then die 'You must specify a directory and one or more packages.' elif (( $# < 1 )); then die 'You must specify a directory.' fi working_dir="$(readlink -f ${1})" shift 1 #WORKDIR=${working_dir} [[ -z $working_dir ]] && die 'Please specify a working directory.' if [[ -z $cache_dir ]]; then cache_dirs=($(pacman -v $cache_conf 2>&1 | grep '^Cache Dirs:' | sed 's/Cache Dirs:\s*//g')) else cache_dirs=(${cache_dir}) fi # Get host mirror host_mirror=$(pacman -Sddp extra/devtools 2>/dev/null | sed -E "s#(.*/)(.*/)extra/.*#\1${branch}/\$repo/\$arch#") if echo "${host_mirror}" | grep -q 'file://'; then host_mirror_path=$(echo "${host_mirror}" | sed -E 's#file://(/.*)/\$repo/\$arch#\1#g') fi # {{{ functions build_mount_args() { local p declare -g mount_args=() if [[ -n $host_mirror_path ]]; then printf -v p '%q' "$host_mirror_path" mount_args+=(--bind-ro="$p") fi printf -v p '%q' "${cache_dirs[0]}" mount_args+=(--bind="$p") for cache_dir in ${cache_dirs[@]:1}; do printf -v p '%q' "$cache_dir" mount_args+=(--bind-ro="$p") done } copy_hostconf () { cp -a /etc/pacman.d/gnupg "${working_dir}/etc/pacman.d" echo "Server = ${host_mirror}" > ${working_dir}/etc/pacman.d/mirrorlist sed -i -e "s|^.*Branch=.*|Branch=${branch}|" ${working_dir}/etc/pacman-mirrors.conf if [[ -n $pac_conf && $NOCOPY = 'n' ]]; then cp ${pac_conf} ${working_dir}/etc/pacman.conf fi if [[ -n $makepkg_conf && $NOCOPY = 'n' ]]; then cp ${makepkg_conf} ${working_dir}/etc/makepkg.conf fi if [[ -n $mirrors_conf && $NOCOPY = 'n' ]]; then cp ${mirrors_conf} ${working_dir}/etc/pacman-mirrors.conf sed -i -e "s|^.*Branch=.*|Branch=${branch}|" ${working_dir}/etc/pacman-mirrors.conf fi sed -r "s|^#?\\s*CacheDir.+|CacheDir = $(echo -n ${cache_dirs[@]})|g" -i ${working_dir}/etc/pacman.conf } chroot_lock () { # Only reopen the FD if it wasn't handed to us if [[ $(readlink -f /dev/fd/9) != "${working_dir}.lock" ]]; then exec 9>"${working_dir}.lock" fi # Lock the chroot. Take note of the FD number. if ! flock -n 9; then stat_busy "Locking chroot" flock 9 stat_done fi } # track_mount() { # if [[ -z $CHROOT_ACTIVE_MOUNTS ]]; then # CHROOT_ACTIVE_MOUNTS=() # trap 'chroot_umount' EXIT # fi # # mount "$@" && CHROOT_ACTIVE_MOUNTS=("$2" "${CHROOT_ACTIVE_MOUNTS[@]}") # } # # mount_conditionally() { # local cond=$1; shift # if eval "$cond"; then # track_mount "$@" # fi # } # # api_fs_mount_build() { # mount_conditionally "! mountpoint -q '$1'" "$1" "$1" --bind && # track_mount proc "$1/proc" -t proc -o nosuid,noexec,nodev && # track_mount sys "$1/sys" -t sysfs -o nosuid,noexec,nodev,ro && # track_mount udev "$1/dev" -t devtmpfs -o mode=0755,nosuid && # track_mount devpts "$1/dev/pts" -t devpts -o mode=0620,gid=5,nosuid,noexec && # track_mount shm "$1/dev/shm" -t tmpfs -o mode=1777,nosuid,nodev && # track_mount run "$1/run" -t tmpfs -o nosuid,nodev,mode=0755 && # track_mount tmp "$1/tmp" -t tmpfs -o mode=1777,strictatime,nodev,nosuid # track_mount /etc/resolv.conf "$1/etc/resolv.conf" --bind # track_mount ${cache_dirs[0]} "$1/${cache_dirs[0]}" --bind # } # # chroot_umount() { # umount "${CHROOT_ACTIVE_MOUNTS[@]}" # } chroot_run() { local dir=$1 shift if ${nosystemd};then chroot "${dir}" "${@}" else systemd-nspawn -D "${dir}" "${mount_args[@]}" -- ${@} 2>/dev/null fi } # }}} umask 0022 if [[ -n $RUN ]]; then # run chroot {{{ #Sanity check if [[ ! -f "${working_dir}/.manjaro-chroot" ]]; then die "'${working_dir}' does not appear to be a Manjaro chroot." elif [[ $(cat "${working_dir}/.manjaro-chroot") != ${CHROOT_VERSION} ]]; then die "'${working_dir}' is not compatible with ${APPNAME} version ${CHROOT_VERSION}. Please rebuild." fi chroot_lock if ${nosystemd};then api_fs_mount "${working_dir}" track_mount /etc/resolv.conf "${working_dir}/etc/resolv.conf" --bind track_mount ${cache_dirs[0]} "${working_dir}/${cache_dirs[0]}" --bind else build_mount_args fi copy_hostconf chroot_run "${working_dir}" ${RUN} # }}} else # {{{ build chroot if [[ -e $working_dir ]]; then die "Working directory '${working_dir}' already exists" fi mkdir -p "${working_dir}" if [[ "$(stat -f -c %T "${working_dir}")" == btrfs ]]; then rmdir "${working_dir}" if ! btrfs subvolume create "${working_dir}"; then die "Couldn't create subvolume for '${working_dir}'" fi chmod 0755 "${working_dir}" fi chroot_lock pacargs=("${cache_dirs[@]/#/--cachedir=}") if [[ -n $pac_conf ]]; then pacargs+=("--config=${pac_conf}") fi if ! basestrap -GMcd "${working_dir}" "${pacargs[@]}" "$@"; then die 'Failed to install all packages' fi printf '%s.UTF-8 UTF-8\n' en_US de_DE > "${working_dir}/etc/locale.gen" chroot_run "${working_dir}" locale-gen echo 'LANG=C' > "${working_dir}/etc/locale.conf" copy_hostconf echo "${CHROOT_VERSION}" > "${working_dir}/.manjaro-chroot" # }}} fi