[arch-projects] [devtools] [PATCH] use nspawn to bind mount needed directories
systemd-nspawn is capable of doing this as of systemd-198. Signed-off-by: Dave Reisner <dreisner@archlinux.org> --- mkarchroot.in | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/mkarchroot.in b/mkarchroot.in index 20de5e6..c7c7bcb 100644 --- a/mkarchroot.in +++ b/mkarchroot.in @@ -95,14 +95,21 @@ bind_mount() { mount --make-slave "$target" } -chroot_mount() { - trap 'trap_chroot_umount' EXIT INT QUIT TERM HUP +build_mount_args() { + local p + declare -g mount_args=() - [[ -n $host_mirror_path ]] && bind_mount "$host_mirror_path" ro + 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") - bind_mount "${cache_dirs[0]}" for cache_dir in ${cache_dirs[@]:1}; do - bind_mount "$cache_dir" ro + printf -v p '%q' "$cache_dir" + mount_args+=(--bind-ro="$p") done } @@ -148,7 +155,7 @@ chroot_lock () { chroot_run() { local dir=$1 shift - eval systemd-nspawn -D "${dir}" -- ${@} 2>/dev/null + eval systemd-nspawn -D "${dir}" "${mount_args[@]}" -- ${@} 2>/dev/null } # }}} @@ -164,7 +171,7 @@ if [[ -n $RUN ]]; then fi chroot_lock - chroot_mount + build_mount_args copy_hostconf chroot_run "${working_dir}" ${RUN} -- 1.8.1.4
Am 28.02.2013 23:09, schrieb Dave Reisner:
systemd-nspawn is capable of doing this as of systemd-198.
Signed-off-by: Dave Reisner <dreisner@archlinux.org> --- mkarchroot.in | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-)
diff --git a/mkarchroot.in b/mkarchroot.in index 20de5e6..c7c7bcb 100644 --- a/mkarchroot.in +++ b/mkarchroot.in @@ -95,14 +95,21 @@ bind_mount() { mount --make-slave "$target" }
-chroot_mount() { - trap 'trap_chroot_umount' EXIT INT QUIT TERM HUP +build_mount_args() { + local p + declare -g mount_args=()
- [[ -n $host_mirror_path ]] && bind_mount "$host_mirror_path" ro + 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")
- bind_mount "${cache_dirs[0]}" for cache_dir in ${cache_dirs[@]:1}; do - bind_mount "$cache_dir" ro + printf -v p '%q' "$cache_dir" + mount_args+=(--bind-ro="$p") done }
@@ -148,7 +155,7 @@ chroot_lock () { chroot_run() { local dir=$1 shift - eval systemd-nspawn -D "${dir}" -- ${@} 2>/dev/null + eval systemd-nspawn -D "${dir}" "${mount_args[@]}" -- ${@} 2>/dev/null }
# }}} @@ -164,7 +171,7 @@ if [[ -n $RUN ]]; then fi
chroot_lock - chroot_mount + build_mount_args copy_hostconf
chroot_run "${working_dir}" ${RUN}
We would still need to use chroot_mount when installing the build env as we don't use nspawn here. Also: doesn't nspawn umount the mounts it mounted on exit? -- Pierre Schmitz, https://pierre-schmitz.com
On Sun, Mar 10, 2013 at 10:09:48AM +0100, Pierre Schmitz wrote:
Am 28.02.2013 23:09, schrieb Dave Reisner:
systemd-nspawn is capable of doing this as of systemd-198.
Signed-off-by: Dave Reisner <dreisner@archlinux.org> --- mkarchroot.in | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-)
diff --git a/mkarchroot.in b/mkarchroot.in index 20de5e6..c7c7bcb 100644 --- a/mkarchroot.in +++ b/mkarchroot.in @@ -95,14 +95,21 @@ bind_mount() { mount --make-slave "$target" }
-chroot_mount() { - trap 'trap_chroot_umount' EXIT INT QUIT TERM HUP +build_mount_args() { + local p + declare -g mount_args=()
- [[ -n $host_mirror_path ]] && bind_mount "$host_mirror_path" ro + 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")
- bind_mount "${cache_dirs[0]}" for cache_dir in ${cache_dirs[@]:1}; do - bind_mount "$cache_dir" ro + printf -v p '%q' "$cache_dir" + mount_args+=(--bind-ro="$p") done }
@@ -148,7 +155,7 @@ chroot_lock () { chroot_run() { local dir=$1 shift - eval systemd-nspawn -D "${dir}" -- ${@} 2>/dev/null + eval systemd-nspawn -D "${dir}" "${mount_args[@]}" -- ${@} 2>/dev/null }
# }}} @@ -164,7 +171,7 @@ if [[ -n $RUN ]]; then fi
chroot_lock - chroot_mount + build_mount_args copy_hostconf
chroot_run "${working_dir}" ${RUN}
We would still need to use chroot_mount when installing the build env as we don't use nspawn here.
Could you clarify this? I'm not following this...
Also: doesn't nspawn umount the mounts it mounted on exit?
Yes, it does. Isn't that what we want? I've been using this patch since I wrote it, and haven't seen any strange behavior as of yet.
Am 10.03.2013 18:37, schrieb Dave Reisner:
On Sun, Mar 10, 2013 at 10:09:48AM +0100, Pierre Schmitz wrote:
Am 28.02.2013 23:09, schrieb Dave Reisner:
systemd-nspawn is capable of doing this as of systemd-198.
Signed-off-by: Dave Reisner <dreisner@archlinux.org> --- mkarchroot.in | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-)
diff --git a/mkarchroot.in b/mkarchroot.in index 20de5e6..c7c7bcb 100644 --- a/mkarchroot.in +++ b/mkarchroot.in @@ -95,14 +95,21 @@ bind_mount() { mount --make-slave "$target" }
-chroot_mount() { - trap 'trap_chroot_umount' EXIT INT QUIT TERM HUP +build_mount_args() { + local p + declare -g mount_args=()
- [[ -n $host_mirror_path ]] && bind_mount "$host_mirror_path" ro + 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")
- bind_mount "${cache_dirs[0]}" for cache_dir in ${cache_dirs[@]:1}; do - bind_mount "$cache_dir" ro + printf -v p '%q' "$cache_dir" + mount_args+=(--bind-ro="$p") done }
@@ -148,7 +155,7 @@ chroot_lock () { chroot_run() { local dir=$1 shift - eval systemd-nspawn -D "${dir}" -- ${@} 2>/dev/null + eval systemd-nspawn -D "${dir}" "${mount_args[@]}" -- ${@} 2>/dev/null }
# }}} @@ -164,7 +171,7 @@ if [[ -n $RUN ]]; then fi
chroot_lock - chroot_mount + build_mount_args copy_hostconf
chroot_run "${working_dir}" ${RUN}
We would still need to use chroot_mount when installing the build env as we don't use nspawn here.
Could you clarify this? I'm not following this...
Also: doesn't nspawn umount the mounts it mounted on exit?
Yes, it does. Isn't that what we want?
I've been using this patch since I wrote it, and haven't seen any strange behavior as of yet.
Strange. You removed the chroot_mount() function even though it's still used when creating the chroot with pacstrap. -- Pierre Schmitz, https://pierre-schmitz.com
On Sun, Mar 10, 2013 at 06:48:21PM +0100, Pierre Schmitz wrote:
Am 10.03.2013 18:37, schrieb Dave Reisner:
On Sun, Mar 10, 2013 at 10:09:48AM +0100, Pierre Schmitz wrote:
Am 28.02.2013 23:09, schrieb Dave Reisner:
systemd-nspawn is capable of doing this as of systemd-198.
Signed-off-by: Dave Reisner <dreisner@archlinux.org> --- mkarchroot.in | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-)
diff --git a/mkarchroot.in b/mkarchroot.in index 20de5e6..c7c7bcb 100644 --- a/mkarchroot.in +++ b/mkarchroot.in @@ -95,14 +95,21 @@ bind_mount() { mount --make-slave "$target" }
-chroot_mount() { - trap 'trap_chroot_umount' EXIT INT QUIT TERM HUP +build_mount_args() { + local p + declare -g mount_args=()
- [[ -n $host_mirror_path ]] && bind_mount "$host_mirror_path" ro + 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")
- bind_mount "${cache_dirs[0]}" for cache_dir in ${cache_dirs[@]:1}; do - bind_mount "$cache_dir" ro + printf -v p '%q' "$cache_dir" + mount_args+=(--bind-ro="$p") done }
@@ -148,7 +155,7 @@ chroot_lock () { chroot_run() { local dir=$1 shift - eval systemd-nspawn -D "${dir}" -- ${@} 2>/dev/null + eval systemd-nspawn -D "${dir}" "${mount_args[@]}" -- ${@} 2>/dev/null }
# }}} @@ -164,7 +171,7 @@ if [[ -n $RUN ]]; then fi
chroot_lock - chroot_mount + build_mount_args copy_hostconf
chroot_run "${working_dir}" ${RUN}
We would still need to use chroot_mount when installing the build env as we don't use nspawn here.
Could you clarify this? I'm not following this...
Also: doesn't nspawn umount the mounts it mounted on exit?
Yes, it does. Isn't that what we want?
I've been using this patch since I wrote it, and haven't seen any strange behavior as of yet.
Strange. You removed the chroot_mount() function even though it's still used when creating the chroot with pacstrap.
Ah, I see. I assumed cleaning a chroot to *re*create it was the same codepath as creating a new one from scratch. I'll play with this more.
-- Pierre Schmitz, https://pierre-schmitz.com
systemd-nspawn is capable of doing this as of systemd-198. Doing this means we can remove all of our home grown chroot mount/umount logic, as it's all performed by pacstrap or systemd-nspawn. Signed-off-by: Dave Reisner <dreisner@archlinux.org> --- mkarchroot.in | 55 +++++++++++++++++-------------------------------------- 1 file changed, 17 insertions(+), 38 deletions(-) diff --git a/mkarchroot.in b/mkarchroot.in index 20de5e6..fee2a37 100644 --- a/mkarchroot.in +++ b/mkarchroot.in @@ -78,31 +78,21 @@ if echo "${host_mirror}" | grep -q 'file://'; then fi # {{{ functions -bind_mount() { - local mode="${2:-rw}" - local target="${working_dir}${1}" - - if [[ ! -e "$target" ]]; then - if [[ -d "$1" ]]; then - install -d "$target" - else - install -D /dev/null "$target" - fi - fi +build_mount_args() { + local p + declare -g mount_args=() - mount -o bind "$1" "$target" - mount -o remount,${mode},bind "$target" - mount --make-slave "$target" -} - -chroot_mount() { - trap 'trap_chroot_umount' EXIT INT QUIT TERM HUP + if [[ -n $host_mirror_path ]]; then + printf -v p '%q' "$host_mirror_path" + mount_args+=(--bind-ro="$p") + fi - [[ -n $host_mirror_path ]] && bind_mount "$host_mirror_path" ro + printf -v p '%q' "${cache_dirs[0]}" + mount_args+=(--bind="$p") - bind_mount "${cache_dirs[0]}" for cache_dir in ${cache_dirs[@]:1}; do - bind_mount "$cache_dir" ro + printf -v p '%q' "$cache_dir" + mount_args+=(--bind-ro="$p") done } @@ -121,16 +111,6 @@ copy_hostconf () { sed -r "s|^#?\\s*CacheDir.+|CacheDir = $(echo -n ${cache_dirs[@]})|g" -i ${working_dir}/etc/pacman.conf } -trap_chroot_umount () { - trap 'trap_abort' INT QUIT TERM HUP - trap 'trap_exit' EXIT - - for cache_dir in ${cache_dirs[@]}; do - umount "${working_dir}/${cache_dir}" - done - [[ -n $host_mirror_path ]] && umount "${working_dir}/${host_mirror_path}" -} - chroot_lock () { # Only reopen the FD if it wasn't handed to us if [[ $(readlink -f /dev/fd/9) != "${working_dir}.lock" ]]; then @@ -148,7 +128,7 @@ chroot_lock () { chroot_run() { local dir=$1 shift - eval systemd-nspawn -D "${dir}" -- ${@} 2>/dev/null + eval systemd-nspawn -D "${dir}" "${mount_args[@]}" -- ${@} 2>/dev/null } # }}} @@ -164,7 +144,7 @@ if [[ -n $RUN ]]; then fi chroot_lock - chroot_mount + build_mount_args copy_hostconf chroot_run "${working_dir}" ${RUN} @@ -181,18 +161,17 @@ else fi chroot_lock - chroot_mount - pacargs="${cache_dirs[@]/#/--cachedir=}" + pacargs=("${cache_dirs[@]/#/--cachedir=}") if [[ -n $pac_conf ]]; then - pacargs="$pacargs --config=${pac_conf}" + pacargs+=("--config=${pac_conf}") fi if (( $# != 0 )); then if [[ $FORCE = 'y' ]]; then - pacargs="$pacargs --force" + pacargs+=("--force") fi - if ! pacstrap -GMcd "${working_dir}" ${pacargs} $@; then + if ! pacstrap -GMcd "${working_dir}" "${pacargs[@]}" "$@"; then die 'Failed to install all packages' fi fi -- 1.8.1.5
Am 10.03.2013 20:11, schrieb Dave Reisner:
systemd-nspawn is capable of doing this as of systemd-198. Doing this means we can remove all of our home grown chroot mount/umount logic, as it's all performed by pacstrap or systemd-nspawn.
It seems nspawn is only able to bind mount one directory at a time. So we cannot use it here as we need to bind multiple directories. Greetings, Pierre -- Pierre Schmitz, https://pierre-schmitz.com
Am 17.03.2013 12:33, schrieb Pierre Schmitz:
Am 10.03.2013 20:11, schrieb Dave Reisner:
systemd-nspawn is capable of doing this as of systemd-198. Doing this means we can remove all of our home grown chroot mount/umount logic, as it's all performed by pacstrap or systemd-nspawn.
It seems nspawn is only able to bind mount one directory at a time. So we cannot use it here as we need to bind multiple directories.
It seems it should be able to do more than one mount, but the implementation has a strange bug where the second host directory get's mounted to the first mountpoint within the container etc. -- Pierre Schmitz, https://pierre-schmitz.com
Am 17.03.2013 13:03, schrieb Pierre Schmitz:
Am 17.03.2013 12:33, schrieb Pierre Schmitz:
Am 10.03.2013 20:11, schrieb Dave Reisner:
systemd-nspawn is capable of doing this as of systemd-198. Doing this means we can remove all of our home grown chroot mount/umount logic, as it's all performed by pacstrap or systemd-nspawn.
It seems nspawn is only able to bind mount one directory at a time. So we cannot use it here as we need to bind multiple directories.
It seems it should be able to do more than one mount, but the implementation has a strange bug where the second host directory get's mounted to the first mountpoint within the container etc.
Tracked it down to commit 961e4526925b7b1e1d3582f2fc9fb38035e2b5fb; so this will get fixed in the next systemd release. -- Pierre Schmitz, https://pierre-schmitz.com
participants (3)
-
Dave Reisner
-
Dave Reisner
-
Pierre Schmitz