-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use mount namespace instead of chroot #62
Open
valentindavid
wants to merge
1
commit into
canonical:main
Choose a base branch
from
valentindavid:valentindavid/namespace
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
#!/bin/bash | ||
|
||
set -eu | ||
|
||
# FIXME: This should be replace by a mount of devtmpfs when | ||
# it is supported in user namespaces | ||
bind_dev() { | ||
dev="${1}/dev" | ||
|
||
for device in null zero full random urandom tty; do | ||
touch "${dev}/${device}" | ||
mount --bind "/dev/${device}" "${dev}/${device}" | ||
done | ||
} | ||
|
||
# Because systemd debian package post-install script is not very | ||
# robust, we have to create a symlink and bind mount resolv.conf | ||
# there. | ||
bind_resolv() { | ||
sysroot="${1}" | ||
|
||
resolv_real_path="${sysroot}/run/fake-resolv.conf" | ||
create_symlink=yes | ||
if [ -L "${sysroot}/etc/resolv.conf" ]; then | ||
resolv="$(readlink "${sysroot}/etc/resolv.conf")" | ||
if [ "${resolv}" = "../run/systemd/resolve/stub-resolv.conf" ]; then | ||
resolv_real_path="${sysroot}/run/systemd/resolve/stub-resolv.conf" | ||
create_symlink=no | ||
fi | ||
fi | ||
mkdir -p "$(dirname "${resolv_real_path}")" | ||
touch "${resolv_real_path}" | ||
mount --bind -o ro /etc/resolv.conf "${resolv_real_path}" | ||
if [ "${create_symlink}" = yes ]; then | ||
ln -srf "${resolv_real_path}" "${sysroot}/etc/resolv.conf" | ||
fi | ||
} | ||
|
||
if [ $# -lt 3 ]; then | ||
echo "Expected at least 3 arguments" 1>&2 | ||
exit 1 | ||
fi | ||
|
||
command="${1}" | ||
sysroot="${2}" | ||
shift 2 | ||
|
||
case "${command}" in | ||
spawn) | ||
# This is the first phase. This will re-spawn the script with | ||
# `init` subcommand. There are limitations of what can be | ||
# done within a LXD container. So in the first phase we mount | ||
# a tmpfs filesytem which cannot always be done in a mount | ||
# namespace. Because it is outside of the mount namespace, | ||
# it has to be removed from manually. | ||
# Then we spawn the second phase into a namespace, | ||
# but without changing the root. | ||
|
||
tmpdir="$(mktemp -d --tmpdir mount-ns.XXXXXXXXXX)" | ||
cleanup() { | ||
umount "${tmpdir}" || true | ||
rm -rf "${tmpdir}" | ||
} | ||
mount -t tmpfs tmpfs "${tmpdir}" | ||
mkdir -m 0755 -p "${tmpdir}/dev" | ||
mkdir -m 1777 -p "${tmpdir}/tmp" | ||
mkdir -m 0755 -p "${tmpdir}/run" | ||
options=( | ||
--bind "${tmpdir}/dev" /dev | ||
--bind "${tmpdir}/tmp" /tmp | ||
--bind "${tmpdir}/run" /run | ||
) | ||
trap cleanup EXIT | ||
unshare --pid --fork --mount -- "${0}" init "${sysroot}" "${options[@]}" "${@}" | ||
;; | ||
init) | ||
# This is the second phase. Here we are in a mount namespace, | ||
# spawned from the `spawn` subcommand. But we still have the | ||
# same root directory. So we can bind mount all we need in the | ||
# sysroot. Then we can change the root to that sysroot. | ||
|
||
mount -t proc proc "${sysroot}/proc" | ||
while [ $# -gt 1 ]; do | ||
case "${1}" in | ||
--) | ||
shift | ||
break | ||
;; | ||
--bind|--ro-bind) | ||
if [ -d "$2" ]; then | ||
if ! [ -d "${sysroot}/$3" ]; then | ||
mkdir -p "${sysroot}/$3" | ||
fi | ||
else | ||
if ! [ -e "${sysroot}/$3" ]; then | ||
dir="$(dirname "${sysroot}/$3")" | ||
if ! [ -d "${dir}" ]; then | ||
mkdir -p "${dir}" | ||
fi | ||
touch "${sysroot}/$3" | ||
fi | ||
fi | ||
extra_args=() | ||
case "$1" in | ||
--ro-bind) | ||
extra_args=("-o" "ro") | ||
;; | ||
esac | ||
mount --bind "${extra_args[@]}" "$2" "${sysroot}/$3" | ||
shift 3 | ||
;; | ||
*) | ||
break | ||
;; | ||
esac | ||
done | ||
bind_dev "${sysroot}" | ||
bind_resolv "${sysroot}" | ||
exec unshare --mount --root="${sysroot}" -- "${@}" | ||
;; | ||
*) | ||
echo "Unknown command" 1>&2 | ||
exit 1 | ||
;; | ||
esac |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it would be good to add comments on why we need to call unshare twice (from spawn, then from init) and why we use exec the second time.