tree-wide: fix shellcheck warnings and style consistency
This commit is contained in:
parent
560c00c9c2
commit
33cf482649
@ -2,27 +2,30 @@
|
|||||||
|
|
||||||
shopt -s extglob
|
shopt -s extglob
|
||||||
|
|
||||||
|
# m4_include() is recognized as a function definition
|
||||||
|
# shellcheck source=common disable=SC1073,SC1065,SC1064,SC1072
|
||||||
m4_include(common)
|
m4_include(common)
|
||||||
|
|
||||||
setup=chroot_setup
|
setup=chroot_setup
|
||||||
unshare=0
|
unshare=0
|
||||||
|
userspec=''
|
||||||
|
chroot_args=()
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
cat <<EOF
|
cat <<EOF
|
||||||
usage: ${0##*/} chroot-dir [command] [arguments...]
|
usage: ${0##*/} [options] chroot_dir [command [arguments...]]
|
||||||
|
|
||||||
-h Print this help message
|
Options:
|
||||||
-N Run in unshare mode as a regular user
|
-N Run in unshare mode as a regular user
|
||||||
-u <user>[:group] Specify non-root user and optional group to use
|
-u <user>[:group] Specify non-root user and group (optional) to use
|
||||||
|
-h Print this help message
|
||||||
If 'command' is unspecified, ${0##*/} will launch /bin/bash.
|
If 'command' is unspecified, arch-chroot will launch /bin/bash.
|
||||||
|
|
||||||
Note that when using arch-chroot, the target chroot directory *should* be a
|
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
|
mountpoint. This ensures that tools such as pacman(8) or findmnt(8) have an
|
||||||
accurate hierarchy of the mounted filesystems within the chroot.
|
accurate hierarchy of the mounted filesystems within the chroot.
|
||||||
|
|
||||||
If your chroot target is not a mountpoint, you can bind mount the directory on
|
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'.
|
itself to make it one, i.e. 'mount --bind chroot_dir chroot_dir'.
|
||||||
|
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
@ -74,32 +77,32 @@ chroot_add_resolv_conf() {
|
|||||||
chroot_add_mount "$src" "$dest" --bind
|
chroot_add_mount "$src" "$dest" --bind
|
||||||
}
|
}
|
||||||
|
|
||||||
while getopts ':hNu:' flag; do
|
if [[ -z $1 || $1 = @(-h|--help) ]]; then
|
||||||
case $flag in
|
|
||||||
h)
|
|
||||||
usage
|
usage
|
||||||
exit 0
|
exit $(( $# ? 0 : 1 ))
|
||||||
;;
|
fi
|
||||||
|
|
||||||
|
while getopts ':Nu:' flag; do
|
||||||
|
case $flag in
|
||||||
N)
|
N)
|
||||||
setup=unshare_setup
|
setup=unshare_setup
|
||||||
unshare=1
|
unshare=1
|
||||||
;;
|
;;
|
||||||
u)
|
u)
|
||||||
userspec=$OPTARG
|
userspec="$OPTARG"
|
||||||
;;
|
;;
|
||||||
:)
|
:)
|
||||||
die '%s: option requires an argument -- '\''%s'\' "${0##*/}" "$OPTARG"
|
die "%s: option requires an argument -- '%s'" "${0##*/}" "$OPTARG"
|
||||||
;;
|
;;
|
||||||
?)
|
?)
|
||||||
die '%s: invalid option -- '\''%s'\' "${0##*/}" "$OPTARG"
|
die "%s: invalid option -- '%s'" "${0##*/}" "$OPTARG"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
shift $(( OPTIND - 1 ))
|
shift $(( OPTIND - 1 ))
|
||||||
|
|
||||||
(( $# )) || die 'No chroot directory specified'
|
(( $# )) || die 'No chroot directory specified'
|
||||||
chrootdir=$1
|
chrootdir="$1"; shift
|
||||||
shift
|
|
||||||
|
|
||||||
arch-chroot() {
|
arch-chroot() {
|
||||||
(( EUID == 0 )) || die 'This script must be run with root privileges'
|
(( EUID == 0 )) || die 'This script must be run with root privileges'
|
||||||
@ -125,3 +128,5 @@ if (( unshare )); then
|
|||||||
else
|
else
|
||||||
arch-chroot
|
arch-chroot
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# vim: et ts=2 sw=2 ft=sh:
|
||||||
|
122
common
122
common
@ -1,3 +1,6 @@
|
|||||||
|
#!/hint/bash
|
||||||
|
# shellcheck disable=SC2155,SC2064
|
||||||
|
|
||||||
# generated from util-linux source: libmount/src/utils.c
|
# generated from util-linux source: libmount/src/utils.c
|
||||||
declare -A pseudofs_types=([anon_inodefs]=1
|
declare -A pseudofs_types=([anon_inodefs]=1
|
||||||
[apparmorfs]=1
|
[apparmorfs]=1
|
||||||
@ -72,11 +75,12 @@ declare -A fsck_types=([btrfs]=0 # btrfs doesn't need a regular fsck utility
|
|||||||
[vfat]=1
|
[vfat]=1
|
||||||
[xfs]=1)
|
[xfs]=1)
|
||||||
|
|
||||||
|
# shellcheck disable=SC2059
|
||||||
out() { printf "$1 $2\n" "${@:3}"; }
|
out() { printf "$1 $2\n" "${@:3}"; }
|
||||||
error() { out "==> ERROR:" "$@"; } >&2
|
|
||||||
warning() { out "==> WARNING:" "$@"; } >&2
|
|
||||||
msg() { out "==>" "$@"; }
|
msg() { out "==>" "$@"; }
|
||||||
msg2() { out " ->" "$@";}
|
msg2() { out " ->" "$@"; }
|
||||||
|
warning() { out "==> WARNING:" "$@"; } >&2
|
||||||
|
error() { out "==> ERROR:" "$@"; } >&2
|
||||||
die() { error "$@"; exit 1; }
|
die() { error "$@"; exit 1; }
|
||||||
|
|
||||||
ignore_error() {
|
ignore_error() {
|
||||||
@ -86,23 +90,44 @@ ignore_error() {
|
|||||||
|
|
||||||
in_array() {
|
in_array() {
|
||||||
local i
|
local i
|
||||||
|
|
||||||
for i in "${@:2}"; do
|
for i in "${@:2}"; do
|
||||||
[[ $1 = "$i" ]] && return 0
|
[[ "$1" = "$i" ]] && return 0
|
||||||
done
|
done
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rev_array() {
|
||||||
|
local -n _arr="$1"
|
||||||
|
|
||||||
|
mapfile -t _arr < <(printf '%s\n' "${_arr[@]}" | tac)
|
||||||
|
}
|
||||||
|
|
||||||
chroot_add_mount() {
|
chroot_add_mount() {
|
||||||
mount "$@" && CHROOT_ACTIVE_MOUNTS=("$2" "${CHROOT_ACTIVE_MOUNTS[@]}")
|
mount "$@" && CHROOT_ACTIVE_MOUNTS+=("$2")
|
||||||
}
|
}
|
||||||
|
|
||||||
chroot_maybe_add_mount() {
|
chroot_maybe_add_mount() {
|
||||||
local cond=$1; shift
|
local cond="$1"; shift
|
||||||
|
|
||||||
if eval "$cond"; then
|
if eval "$cond"; then
|
||||||
chroot_add_mount "$@"
|
chroot_add_mount "$@"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
chroot_add_mount_lazy() {
|
||||||
|
mount "$@" && CHROOT_ACTIVE_LAZY+=("$2")
|
||||||
|
}
|
||||||
|
|
||||||
|
chroot_bind_device() {
|
||||||
|
touch "$2" && CHROOT_ACTIVE_FILES+=("$2")
|
||||||
|
chroot_add_mount "$1" "$2" --bind
|
||||||
|
}
|
||||||
|
|
||||||
|
chroot_add_link() {
|
||||||
|
ln -sf "$1" "$2" && CHROOT_ACTIVE_FILES+=("$2")
|
||||||
|
}
|
||||||
|
|
||||||
chroot_setup() {
|
chroot_setup() {
|
||||||
CHROOT_ACTIVE_MOUNTS=()
|
CHROOT_ACTIVE_MOUNTS=()
|
||||||
[[ $(trap -p EXIT) ]] && die '(BUG): attempting to overwrite existing EXIT trap'
|
[[ $(trap -p EXIT) ]] && die '(BUG): attempting to overwrite existing EXIT trap'
|
||||||
@ -121,28 +146,14 @@ chroot_setup() {
|
|||||||
|
|
||||||
chroot_teardown() {
|
chroot_teardown() {
|
||||||
if (( ${#CHROOT_ACTIVE_MOUNTS[@]} )); then
|
if (( ${#CHROOT_ACTIVE_MOUNTS[@]} )); then
|
||||||
|
rev_array CHROOT_ACTIVE_MOUNTS
|
||||||
umount "${CHROOT_ACTIVE_MOUNTS[@]}"
|
umount "${CHROOT_ACTIVE_MOUNTS[@]}"
|
||||||
fi
|
fi
|
||||||
unset CHROOT_ACTIVE_MOUNTS
|
unset CHROOT_ACTIVE_MOUNTS
|
||||||
}
|
}
|
||||||
|
|
||||||
chroot_add_mount_lazy() {
|
|
||||||
mount "$@" && CHROOT_ACTIVE_LAZY=("$2" "${CHROOT_ACTIVE_LAZY[@]}")
|
|
||||||
}
|
|
||||||
|
|
||||||
chroot_bind_device() {
|
|
||||||
touch "$2" && CHROOT_ACTIVE_FILES=("$2" "${CHROOT_ACTIVE_FILES[@]}")
|
|
||||||
chroot_add_mount $1 "$2" --bind
|
|
||||||
}
|
|
||||||
|
|
||||||
chroot_add_link() {
|
|
||||||
ln -sf "$1" "$2" && CHROOT_ACTIVE_FILES=("$2" "${CHROOT_ACTIVE_FILES[@]}")
|
|
||||||
}
|
|
||||||
|
|
||||||
unshare_setup() {
|
unshare_setup() {
|
||||||
CHROOT_ACTIVE_MOUNTS=()
|
CHROOT_ACTIVE_MOUNTS=() CHROOT_ACTIVE_LAZY=() CHROOT_ACTIVE_FILES=()
|
||||||
CHROOT_ACTIVE_LAZY=()
|
|
||||||
CHROOT_ACTIVE_FILES=()
|
|
||||||
[[ $(trap -p EXIT) ]] && die '(BUG): attempting to overwrite existing EXIT trap'
|
[[ $(trap -p EXIT) ]] && die '(BUG): attempting to overwrite existing EXIT trap'
|
||||||
trap 'unshare_teardown' EXIT
|
trap 'unshare_teardown' EXIT
|
||||||
|
|
||||||
@ -167,17 +178,20 @@ unshare_teardown() {
|
|||||||
chroot_teardown
|
chroot_teardown
|
||||||
|
|
||||||
if (( ${#CHROOT_ACTIVE_LAZY[@]} )); then
|
if (( ${#CHROOT_ACTIVE_LAZY[@]} )); then
|
||||||
|
rev_array CHROOT_ACTIVE_LAZY
|
||||||
umount --lazy "${CHROOT_ACTIVE_LAZY[@]}"
|
umount --lazy "${CHROOT_ACTIVE_LAZY[@]}"
|
||||||
fi
|
fi
|
||||||
unset CHROOT_ACTIVE_LAZY
|
unset CHROOT_ACTIVE_LAZY
|
||||||
|
|
||||||
if (( ${#CHROOT_ACTIVE_FILES[@]} )); then
|
if (( ${#CHROOT_ACTIVE_FILES[@]} )); then
|
||||||
|
rev_array CHROOT_ACTIVE_FILES
|
||||||
rm "${CHROOT_ACTIVE_FILES[@]}"
|
rm "${CHROOT_ACTIVE_FILES[@]}"
|
||||||
fi
|
fi
|
||||||
unset CHROOT_ACTIVE_FILES
|
unset CHROOT_ACTIVE_FILES
|
||||||
}
|
}
|
||||||
|
|
||||||
pid_unshare="unshare --fork --pid"
|
pid_unshare="unshare --fork --pid"
|
||||||
|
# shellcheck disable=SC2034
|
||||||
mount_unshare="$pid_unshare --mount --map-auto --map-root-user --setuid 0 --setgid 0"
|
mount_unshare="$pid_unshare --mount --map-auto --map-root-user --setuid 0 --setgid 0"
|
||||||
|
|
||||||
# This outputs code for declaring all variables to stdout. For example, if
|
# This outputs code for declaring all variables to stdout. For example, if
|
||||||
@ -198,11 +212,11 @@ declare_all() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try_cast() (
|
try_cast() (
|
||||||
_=$(( $1#$2 ))
|
_=$(( ${1#"$2"} ))
|
||||||
) 2>/dev/null
|
) 2>/dev/null
|
||||||
|
|
||||||
valid_number_of_base() {
|
valid_number_of_base() {
|
||||||
local base=$1 len=${#2} i=
|
local base="$1" len="${#2}" i
|
||||||
|
|
||||||
for (( i = 0; i < len; i++ )); do
|
for (( i = 0; i < len; i++ )); do
|
||||||
try_cast "$base" "${2:i:1}" || return 1
|
try_cast "$base" "${2:i:1}" || return 1
|
||||||
@ -212,29 +226,29 @@ valid_number_of_base() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mangle() {
|
mangle() {
|
||||||
local i= chr= out=
|
local i chr out
|
||||||
local {a..f}= {A..F}=
|
local {a..f}='' {A..F}=''
|
||||||
|
|
||||||
for (( i = 0; i < ${#1}; i++ )); do
|
for (( i = 0; i < ${#1}; i++ )); do
|
||||||
chr=${1:i:1}
|
chr="${1:i:1}"
|
||||||
case $chr in
|
case $chr in
|
||||||
[[:space:]\\])
|
[[:space:]\\])
|
||||||
printf -v chr '%03o' "'$chr"
|
printf -v chr '%03o' "'$chr"
|
||||||
out+=\\
|
out+=\\
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
out+=$chr
|
out+="$chr"
|
||||||
done
|
done
|
||||||
|
|
||||||
printf '%s' "$out"
|
printf '%s' "$out"
|
||||||
}
|
}
|
||||||
|
|
||||||
unmangle() {
|
unmangle() {
|
||||||
local i= chr= out= len=$(( ${#1} - 4 ))
|
local i chr out len="$(( ${#1} - 4 ))"
|
||||||
local {a..f}= {A..F}=
|
local {a..f}='' {A..F}=''
|
||||||
|
|
||||||
for (( i = 0; i < len; i++ )); do
|
for (( i = 0; i < len; i++ )); do
|
||||||
chr=${1:i:1}
|
chr="${1:i:1}"
|
||||||
case $chr in
|
case $chr in
|
||||||
\\)
|
\\)
|
||||||
if valid_number_of_base 8 "${1:i+1:3}" ||
|
if valid_number_of_base 8 "${1:i+1:3}" ||
|
||||||
@ -244,7 +258,7 @@ unmangle() {
|
|||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
out+=$chr
|
out+="$chr"
|
||||||
done
|
done
|
||||||
|
|
||||||
printf '%s' "$out${1:i}"
|
printf '%s' "$out${1:i}"
|
||||||
@ -255,39 +269,37 @@ optstring_match_option() {
|
|||||||
|
|
||||||
IFS=, read -ra patterns <<<"$1"
|
IFS=, read -ra patterns <<<"$1"
|
||||||
for pat in "${patterns[@]}"; do
|
for pat in "${patterns[@]}"; do
|
||||||
if [[ $pat = *=* ]]; then
|
if [[ $pat = *'='* ]]; then
|
||||||
# "key=val" will only ever match "key=val"
|
# "key=val" will only ever match "key=val"
|
||||||
candidate=$2
|
candidate="$2"
|
||||||
else
|
else
|
||||||
# "key" will match "key", but also "key=anyval"
|
# "key" will match "key", but also "key=anyval"
|
||||||
candidate=${2%%=*}
|
candidate="${2%%=*}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
[[ $pat = "$candidate" ]] && return 0
|
[[ "$pat" = "$candidate" ]] && return 0
|
||||||
done
|
done
|
||||||
|
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
optstring_remove_option() {
|
optstring_remove_option() {
|
||||||
local o options_ remove=$2 IFS=,
|
local o _options remove="$2"
|
||||||
|
|
||||||
read -ra options_ <<<"${!1}"
|
IFS=, read -ra _options <<<"${!1}"
|
||||||
|
for o in "${!_options[@]}"; do
|
||||||
for o in "${!options_[@]}"; do
|
optstring_match_option "$remove" "${_options[o]}" && unset '_options[o]'
|
||||||
optstring_match_option "$remove" "${options_[o]}" && unset 'options_[o]'
|
|
||||||
done
|
done
|
||||||
|
|
||||||
declare -g "$1=${options_[*]}"
|
declare -g "$1=${_options[*]}"
|
||||||
}
|
}
|
||||||
|
|
||||||
optstring_normalize() {
|
optstring_normalize() {
|
||||||
local o options_ norm IFS=,
|
local o _options norm
|
||||||
|
|
||||||
read -ra options_ <<<"${!1}"
|
|
||||||
|
|
||||||
|
IFS=, read -ra _options <<<"${!1}"
|
||||||
# remove empty fields
|
# remove empty fields
|
||||||
for o in "${options_[@]}"; do
|
for o in "${_options[@]}"; do
|
||||||
[[ $o ]] && norm+=("$o")
|
[[ $o ]] && norm+=("$o")
|
||||||
done
|
done
|
||||||
|
|
||||||
@ -295,6 +307,12 @@ optstring_normalize() {
|
|||||||
declare -g "$1=${norm[*]:-defaults}"
|
declare -g "$1=${norm[*]:-defaults}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
optstring_has_option() {
|
||||||
|
local o="${2%%=*}"
|
||||||
|
|
||||||
|
optstring_get_option "$1" "$o"
|
||||||
|
}
|
||||||
|
|
||||||
optstring_append_option() {
|
optstring_append_option() {
|
||||||
if ! optstring_has_option "$1" "$2"; then
|
if ! optstring_has_option "$1" "$2"; then
|
||||||
declare -g "$1=${!1},$2"
|
declare -g "$1=${!1},$2"
|
||||||
@ -304,8 +322,6 @@ optstring_append_option() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
optstring_prepend_option() {
|
optstring_prepend_option() {
|
||||||
local options_=$1
|
|
||||||
|
|
||||||
if ! optstring_has_option "$1" "$2"; then
|
if ! optstring_has_option "$1" "$2"; then
|
||||||
declare -g "$1=$2,${!1}"
|
declare -g "$1=$2,${!1}"
|
||||||
fi
|
fi
|
||||||
@ -327,14 +343,8 @@ optstring_get_option() {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
optstring_has_option() {
|
|
||||||
local "${2%%=*}"
|
|
||||||
|
|
||||||
optstring_get_option "$1" "$2"
|
|
||||||
}
|
|
||||||
|
|
||||||
dm_name_for_devnode() {
|
dm_name_for_devnode() {
|
||||||
read dm_name <"/sys/class/block/${1#/dev/}/dm/name"
|
read -r dm_name <"/sys/class/block/${1#/dev/}/dm/name"
|
||||||
if [[ $dm_name ]]; then
|
if [[ $dm_name ]]; then
|
||||||
printf '/dev/mapper/%s' "$dm_name"
|
printf '/dev/mapper/%s' "$dm_name"
|
||||||
else
|
else
|
||||||
@ -351,3 +361,5 @@ fstype_is_pseudofs() {
|
|||||||
fstype_has_fsck() {
|
fstype_has_fsck() {
|
||||||
(( fsck_types["$1"] ))
|
(( fsck_types["$1"] ))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# vim: et ts=2 sw=2 ft=sh:
|
||||||
|
@ -3,7 +3,7 @@ _genfstab() {
|
|||||||
local cur prev words cword
|
local cur prev words cword
|
||||||
_init_completion || return
|
_init_completion || return
|
||||||
|
|
||||||
local opts="-f -L -p -P -t -U -h"
|
local opts="-f -L -P -p -t -U -h"
|
||||||
|
|
||||||
case ${prev} in
|
case ${prev} in
|
||||||
-f)
|
-f)
|
||||||
|
71
genfstab.in
71
genfstab.in
@ -1,7 +1,10 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
# shellcheck disable=SC2154 # optstring_*_option may export variables in runtime
|
||||||
|
|
||||||
shopt -s extglob
|
shopt -s extglob
|
||||||
|
|
||||||
|
# m4_include() is recognized as a function definition
|
||||||
|
# shellcheck source=common disable=SC1073,SC1065,SC1064,SC1072
|
||||||
m4_include(common)
|
m4_include(common)
|
||||||
|
|
||||||
write_source() {
|
write_source() {
|
||||||
@ -43,7 +46,7 @@ write_source() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
optstring_apply_quirks() {
|
optstring_apply_quirks() {
|
||||||
local varname=$1 fstype=$2
|
local varname="$1" fstype="$2"
|
||||||
|
|
||||||
# SELinux displays a 'seclabel' option in /proc/self/mountinfo. We can't know
|
# SELinux displays a 'seclabel' option in /proc/self/mountinfo. We can't know
|
||||||
# if the system we're generating the fstab for has any support for SELinux (as
|
# if the system we're generating the fstab for has any support for SELinux (as
|
||||||
@ -97,8 +100,8 @@ usage: ${0##*/} [options] root
|
|||||||
|
|
||||||
-h Print this help message
|
-h Print this help message
|
||||||
|
|
||||||
genfstab generates output suitable for addition to an fstab file based on the
|
genfstab generates output suitable for addition to an fstab file based on
|
||||||
devices mounted under the mountpoint specified by the given root.
|
the devices mounted under the mountpoint specified by the given root.
|
||||||
|
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
@ -110,15 +113,12 @@ fi
|
|||||||
|
|
||||||
while getopts ':f:LPpt:U' flag; do
|
while getopts ':f:LPpt:U' flag; do
|
||||||
case $flag in
|
case $flag in
|
||||||
|
f)
|
||||||
|
prefixfilter="$OPTARG"
|
||||||
|
;;
|
||||||
L)
|
L)
|
||||||
bytag=LABEL
|
bytag=LABEL
|
||||||
;;
|
;;
|
||||||
U)
|
|
||||||
bytag=UUID
|
|
||||||
;;
|
|
||||||
f)
|
|
||||||
prefixfilter=$OPTARG
|
|
||||||
;;
|
|
||||||
P)
|
P)
|
||||||
pseudofs=1
|
pseudofs=1
|
||||||
;;
|
;;
|
||||||
@ -126,42 +126,45 @@ while getopts ':f:LPpt:U' flag; do
|
|||||||
pseudofs=0
|
pseudofs=0
|
||||||
;;
|
;;
|
||||||
t)
|
t)
|
||||||
bytag=${OPTARG^^}
|
bytag="${OPTARG^^}"
|
||||||
|
;;
|
||||||
|
U)
|
||||||
|
bytag=UUID
|
||||||
;;
|
;;
|
||||||
:)
|
:)
|
||||||
die '%s: option requires an argument -- '\''%s'\' "${0##*/}" "$OPTARG"
|
die "%s: option requires an argument -- '%s'" "${0##*/}" "$OPTARG"
|
||||||
;;
|
;;
|
||||||
?)
|
?)
|
||||||
die '%s: invalid option -- '\''%s'\' "${0##*/}" "$OPTARG"
|
die "%s: invalid option -- '%s'" "${0##*/}" "$OPTARG"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
shift $(( OPTIND - 1 ))
|
shift $(( OPTIND - 1 ))
|
||||||
|
|
||||||
(( $# )) || die "No root directory specified"
|
(( $# )) || die 'No root directory specified'
|
||||||
root=$(realpath -mL "$1"); shift
|
root="$(realpath -mL "$1")"; shift
|
||||||
|
|
||||||
if ! mountpoint -q "$root"; then
|
if ! mountpoint -q "$root"; then
|
||||||
die "$root is not a mountpoint"
|
die '%s: not a mountpoint' "$root"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# handle block devices
|
# handle block devices
|
||||||
findmnt -Recvruno SOURCE,TARGET,FSTYPE,OPTIONS,FSROOT "$root" |
|
findmnt -Recvruno SOURCE,TARGET,FSTYPE,OPTIONS,FSROOT "$root" |
|
||||||
while read -r src target fstype opts fsroot; do
|
while read -r src target fstype opts fsroot; do
|
||||||
if (( !pseudofs )) && fstype_is_pseudofs "$fstype"; then
|
if (( ! pseudofs )) && fstype_is_pseudofs "$fstype"; then
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
|
|
||||||
[[ $target = "$prefixfilter"* ]] || continue
|
[[ "$target" = "$prefixfilter"* ]] || continue
|
||||||
|
|
||||||
# default 5th and 6th columns
|
# default 5th and 6th columns
|
||||||
dump=0 pass=2
|
dump=0 pass=2
|
||||||
|
|
||||||
src=$(unmangle "$src")
|
src="$(unmangle "$src")"
|
||||||
target=$(unmangle "$target")
|
target="$(unmangle "$target")"
|
||||||
target=${target#$root}
|
target="/${target#"$root/"}"
|
||||||
|
|
||||||
if (( !foundroot )) && findmnt "$src" "$root" >/dev/null; then
|
if (( ! foundroot )) && findmnt "$src" "$root" >/dev/null; then
|
||||||
# this is root. we can't possibly have more than one...
|
# this is root. we can't possibly have more than one...
|
||||||
pass=1 foundroot=1
|
pass=1 foundroot=1
|
||||||
fi
|
fi
|
||||||
@ -171,7 +174,7 @@ findmnt -Recvruno SOURCE,TARGET,FSTYPE,OPTIONS,FSROOT "$root" |
|
|||||||
pass=0
|
pass=0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ $fsroot != / && $fstype != btrfs ]]; then
|
if [[ "$fsroot" != "/" && "$fstype" != "btrfs" ]]; then
|
||||||
# it's a bind mount
|
# it's a bind mount
|
||||||
src=$(findmnt -funcevo TARGET "$src")$fsroot
|
src=$(findmnt -funcevo TARGET "$src")$fsroot
|
||||||
src="/${src#$root/}"
|
src="/${src#$root/}"
|
||||||
@ -183,7 +186,7 @@ findmnt -Recvruno SOURCE,TARGET,FSTYPE,OPTIONS,FSROOT "$root" |
|
|||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
fstype=none
|
fstype=none
|
||||||
opts+=,bind
|
opts+=',bind'
|
||||||
pass=0
|
pass=0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@ -192,20 +195,20 @@ findmnt -Recvruno SOURCE,TARGET,FSTYPE,OPTIONS,FSROOT "$root" |
|
|||||||
fuseblk)
|
fuseblk)
|
||||||
# well-behaved FUSE filesystems will report themselves as fuse.$fstype.
|
# well-behaved FUSE filesystems will report themselves as fuse.$fstype.
|
||||||
# this is probably NTFS-3g, but let's just make sure.
|
# this is probably NTFS-3g, but let's just make sure.
|
||||||
if ! newtype=$(lsblk -no FSTYPE "$src") || [[ -z $newtype ]]; then
|
if ! newtype="$(lsblk -no FSTYPE "$src")" || [[ -z $newtype ]]; then
|
||||||
# avoid blanking out fstype, leading to an invalid fstab
|
# avoid blanking out fstype, leading to an invalid fstab
|
||||||
error 'Failed to derive real filesystem type for FUSE device on %s' "$target"
|
error 'Failed to derive real filesystem type for FUSE device on %s' "$target"
|
||||||
else
|
else
|
||||||
fstype=$newtype
|
fstype="$newtype"
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
optstring_apply_quirks "opts" "$fstype"
|
optstring_apply_quirks opts "$fstype"
|
||||||
|
|
||||||
# write one line
|
# write one line
|
||||||
write_source "$src"
|
write_source "$src"
|
||||||
printf '\t%-10s' "/$(mangle "${target#/}")" "$fstype" "$opts"
|
printf '\t%-10s' "$(mangle "$target")" "$fstype" "$opts"
|
||||||
printf '\t%s %s' "$dump" "$pass"
|
printf '\t%s %s' "$dump" "$pass"
|
||||||
printf '\n\n'
|
printf '\n\n'
|
||||||
done
|
done
|
||||||
@ -213,20 +216,20 @@ done
|
|||||||
# handle swaps devices
|
# handle swaps devices
|
||||||
{
|
{
|
||||||
# ignore header
|
# ignore header
|
||||||
read
|
read -r
|
||||||
|
|
||||||
while read -r device type _ _ prio; do
|
while read -r device type _ _ prio; do
|
||||||
options=defaults
|
options=defaults
|
||||||
if (( prio >= 0 )); then
|
if (( prio >= 0 )); then
|
||||||
options+=,pri=$prio
|
options+=",pri=$prio"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# skip files marked deleted by the kernel
|
# skip files marked deleted by the kernel
|
||||||
[[ $device = *'\040(deleted)' ]] && continue
|
[[ "$device" = *'\040(deleted)' ]] && continue
|
||||||
|
|
||||||
if [[ $type = file ]]; then
|
if [[ "$type" = "file" ]]; then
|
||||||
printf '%-20s' "${device#${root%/}}"
|
printf '%-20s' "${device#"${root%/}"}"
|
||||||
elif [[ $device = /dev/dm-+([0-9]) ]]; then
|
elif [[ "$device" = "/dev/dm-"+([0-9]) ]]; then
|
||||||
# device mapper doesn't allow characters we need to worry
|
# device mapper doesn't allow characters we need to worry
|
||||||
# about being mangled, and it does the escaping of dashes
|
# about being mangled, and it does the escaping of dashes
|
||||||
# for us in sysfs.
|
# for us in sysfs.
|
||||||
@ -235,7 +238,7 @@ done
|
|||||||
write_source "$(unmangle "$device")"
|
write_source "$(unmangle "$device")"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
printf '\t%-10s\t%-10s\t%-10s\t0 0\n\n' 'none' 'swap' "$options"
|
printf '\t%-10s\t%-10s\t%-10s\t0 0\n\n' none swap "$options"
|
||||||
done
|
done
|
||||||
} </proc/swaps
|
} </proc/swaps
|
||||||
|
|
||||||
|
44
pacstrap.in
44
pacstrap.in
@ -10,18 +10,20 @@
|
|||||||
|
|
||||||
shopt -s extglob
|
shopt -s extglob
|
||||||
|
|
||||||
|
# m4_include() is recognized as a function definition
|
||||||
|
# shellcheck source=common disable=SC1073,SC1065,SC1064,SC1072
|
||||||
m4_include(common)
|
m4_include(common)
|
||||||
|
|
||||||
hostcache=0
|
hostcache=0
|
||||||
copykeyring=1
|
copykeyring=1
|
||||||
initkeyring=0
|
initkeyring=0
|
||||||
copymirrorlist=1
|
copymirrorlist=1
|
||||||
|
copyconfig=0
|
||||||
|
pacman_config=/etc/pacman.conf
|
||||||
pacman_args=()
|
pacman_args=()
|
||||||
pacmode=-Sy
|
pacmode='-Sy'
|
||||||
setup=chroot_setup
|
setup=chroot_setup
|
||||||
unshare=0
|
unshare=0
|
||||||
copyconf=0
|
|
||||||
pacman_config=/etc/pacman.conf
|
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
cat <<EOF
|
cat <<EOF
|
||||||
@ -37,12 +39,12 @@ usage: ${0##*/} [options] root [packages...]
|
|||||||
-M Avoid copying the host's mirrorlist to the target
|
-M Avoid copying the host's mirrorlist to the target
|
||||||
-N Run in unshare mode as a regular user
|
-N Run in unshare mode as a regular user
|
||||||
-P Copy the host's pacman config to the target
|
-P Copy the host's pacman config to the target
|
||||||
-U Use pacman -U to install packages
|
-U Use 'pacman -U' to install packages
|
||||||
|
|
||||||
-h Print this help message
|
-h Print this help message
|
||||||
|
|
||||||
pacstrap installs packages to the specified new root directory. If no packages
|
pacstrap installs packages to the specified new root directory.
|
||||||
are given, pacstrap defaults to the "base" group.
|
If no packages are given, pacstrap defaults to the 'base' metapackage.
|
||||||
|
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
@ -55,20 +57,20 @@ fi
|
|||||||
while getopts ':C:cDGiKMNPU' flag; do
|
while getopts ':C:cDGiKMNPU' flag; do
|
||||||
case $flag in
|
case $flag in
|
||||||
C)
|
C)
|
||||||
pacman_config=$OPTARG
|
pacman_config="$OPTARG"
|
||||||
;;
|
|
||||||
D)
|
|
||||||
pacman_args+=(-dd)
|
|
||||||
;;
|
;;
|
||||||
c)
|
c)
|
||||||
hostcache=1
|
hostcache=1
|
||||||
;;
|
;;
|
||||||
i)
|
D)
|
||||||
interactive=1
|
pacman_args+=(-dd)
|
||||||
;;
|
;;
|
||||||
G)
|
G)
|
||||||
copykeyring=0
|
copykeyring=0
|
||||||
;;
|
;;
|
||||||
|
i)
|
||||||
|
interactive=1
|
||||||
|
;;
|
||||||
K)
|
K)
|
||||||
initkeyring=1
|
initkeyring=1
|
||||||
;;
|
;;
|
||||||
@ -80,23 +82,23 @@ while getopts ':C:cDGiKMNPU' flag; do
|
|||||||
unshare=1
|
unshare=1
|
||||||
;;
|
;;
|
||||||
P)
|
P)
|
||||||
copyconf=1
|
copyconfig=1
|
||||||
;;
|
;;
|
||||||
U)
|
U)
|
||||||
pacmode=-U
|
pacmode='-U'
|
||||||
;;
|
;;
|
||||||
:)
|
:)
|
||||||
die '%s: option requires an argument -- '\''%s'\' "${0##*/}" "$OPTARG"
|
die "%s: option requires an argument -- '%s'" "${0##*/}" "$OPTARG"
|
||||||
;;
|
;;
|
||||||
?)
|
?)
|
||||||
die '%s: invalid option -- '\''%s'\' "${0##*/}" "$OPTARG"
|
die "%s: invalid option -- '%s'" "${0##*/}" "$OPTARG"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
shift $(( OPTIND - 1 ))
|
shift $(( OPTIND - 1 ))
|
||||||
|
|
||||||
(( $# )) || die "No root directory specified"
|
(( $# )) || die 'No root directory specified'
|
||||||
newroot=$1; shift
|
newroot="$1"; shift
|
||||||
pacman_args+=("$pacmode" "${@:-base}" --config="$pacman_config")
|
pacman_args+=("$pacmode" "${@:-base}" --config="$pacman_config")
|
||||||
|
|
||||||
if (( ! hostcache )); then
|
if (( ! hostcache )); then
|
||||||
@ -131,16 +133,14 @@ pacstrap() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
msg 'Installing packages to %s' "$newroot"
|
msg 'Installing packages to %s' "$newroot"
|
||||||
if ! $pid_unshare pacman -r "$newroot" "${pacman_args[@]}"; then
|
$pid_unshare pacman --root "$newroot" "${pacman_args[@]}" || die 'Failed to install packages to new root'
|
||||||
die 'Failed to install packages to new root'
|
|
||||||
fi
|
|
||||||
|
|
||||||
if (( copymirrorlist )); then
|
if (( copymirrorlist )); then
|
||||||
# install the host's mirrorlist onto the new root
|
# install the host's mirrorlist onto the new root
|
||||||
cp -a /etc/pacman.d/mirrorlist "$newroot/etc/pacman.d/"
|
cp -a /etc/pacman.d/mirrorlist "$newroot/etc/pacman.d/"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if (( copyconf )); then
|
if (( copyconfig )); then
|
||||||
cp -a "$pacman_config" "$newroot/etc/pacman.conf"
|
cp -a "$pacman_config" "$newroot/etc/pacman.conf"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user