Skip to content

Commit aeaecf5

Browse files
committedMar 29, 2023
arch-chroot: add option to preserve the chroot resolv.conf
There is a comprehensive inline comment about why we're touching the chroot resolv.conf. Although it does not consider the cases where: - the link may be broken for specific reasons, and/or - working resolver within the chroot is not wanted v2: - flip the condition check Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
1 parent 7e4e1c1 commit aeaecf5

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed
 

‎arch-chroot.in

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
shopt -s extglob
44

55
unshare=0
6+
keepresolvconf=0
67

78
m4_include(common)
89

@@ -13,6 +14,7 @@ usage: ${0##*/} chroot-dir [command] [arguments...]
1314
-h Print this help message
1415
-N Run in unshare mode as a regular user
1516
-u <user>[:group] Specify non-root user and optional group to use
17+
-r Do not change the resolv.conf within the chroot
1618
1719
If 'command' is unspecified, ${0##*/} will launch /bin/bash.
1820
@@ -79,7 +81,9 @@ arch-chroot() {
7981
[[ -d $chrootdir ]] || die "Can't create chroot on non-directory %s" "$chrootdir"
8082

8183
$setup "$chrootdir" || die "failed to setup chroot %s" "$chrootdir"
82-
chroot_add_resolv_conf "$chrootdir" || die "failed to setup resolv.conf"
84+
if (( ! keepresolvconf )); then
85+
chroot_add_resolv_conf "$chrootdir" || die "failed to setup resolv.conf"
86+
fi
8387

8488
if ! mountpoint -q "$chrootdir"; then
8589
warning "$chrootdir is not a mountpoint. This may have undesirable side effects."
@@ -91,7 +95,7 @@ arch-chroot() {
9195
SHELL=/bin/bash $pid_unshare chroot "${chroot_args[@]}" -- "$chrootdir" "${args[@]}"
9296
}
9397

94-
while getopts ':hNu:' flag; do
98+
while getopts ':hNu:r' flag; do
9599
case $flag in
96100
h)
97101
usage
@@ -103,6 +107,9 @@ while getopts ':hNu:' flag; do
103107
u)
104108
userspec=$OPTARG
105109
;;
110+
r)
111+
keepresolvconf=1
112+
;;
106113
:)
107114
die '%s: option requires an argument -- '\''%s'\' "${0##*/}" "$OPTARG"
108115
;;

‎completion/bash/arch-chroot

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ _arch_chroot() {
22
compopt +o dirnames
33
local cur prev opts i
44
_init_completion -n : || return
5-
opts="-N -u -h"
5+
opts="-N -u -r -h"
66

77
for i in "${COMP_WORDS[@]:1:COMP_CWORD-1}"; do
88
if [[ -d ${i} ]]; then

‎completion/zsh/_arch-chroot

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local -a args=(
55
'(-h --help)'{-h,--help}'[display help]'
66
'-N[Run in unshare mode as a regular user]'
77
'-u[The non-root user and optional group to use]: :->userspecs'
8+
'-r[Do not change the resolv.conf within the chroot]'
89
'1:new root directory:_directories'
910
'*:::command:_normal'
1011
)

‎doc/arch-chroot.8.asciidoc

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ Options
4040
*-u <user>[:group]*::
4141
Specify non-root user and optional group to use.
4242

43+
*-r*::
44+
Do not change the resolv.conf within the chroot. This means that the resolver
45+
might not work in the chroot, which could be the required state.
46+
4347
*-h*::
4448
Output syntax and command line options.
4549

0 commit comments

Comments
 (0)
Please sign in to comment.