Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6010 from zyga/feature/user-mount-ns-2
cmd/snap-discard-ns: add support for per-user mount namespaces
- Loading branch information
Showing
6 changed files
with
291 additions
and
39 deletions.
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
""" | ||
Pure-python limited-use wrapper around the mount library function. | ||
""" | ||
from __future__ import print_function, absolute_import, unicode_literals | ||
|
||
from argparse import ArgumentParser | ||
from ctypes import CDLL, c_char_p, c_long, get_errno | ||
from ctypes.util import find_library | ||
from os import strerror | ||
from sys import stderr, version_info | ||
|
||
try: | ||
from typing import Text | ||
except ImportError: | ||
pass | ||
|
||
__all__ = ('mount', ) | ||
|
||
|
||
PY2 = version_info[0] == 2 | ||
|
||
|
||
class MountOpts(object): | ||
"""MountOpts contain various flags for the mount system call.""" | ||
|
||
Bind = 4096 | ||
|
||
|
||
def mount(source, target, fstype, flags=0, data=""): | ||
# type: (Text, Text, Text, int, Text) -> None | ||
"""mount is a thin wrapper around the mount library function.""" | ||
if PY2: | ||
c = b"c" | ||
else: | ||
c = "c" | ||
libc_name = find_library(c) | ||
if libc_name is None: | ||
raise Exception("cannot find the C library") | ||
libc = CDLL(libc_name, use_errno=True) | ||
retval = libc.mount( | ||
c_char_p(source.encode("UTF-8")), | ||
c_char_p(target.encode("UTF-8")), | ||
c_char_p(fstype.encode("UTF-8")), | ||
c_long(flags), | ||
None if data == "" else c_char_p(data.encode("UTF-8"))) | ||
if retval < 0: | ||
errno = get_errno() | ||
raise OSError(errno, strerror(errno)) | ||
|
||
|
||
def main(): | ||
# type: () -> None | ||
parser = ArgumentParser() | ||
parser.add_argument("source", help="path of the device or bind source") | ||
parser.add_argument("target", help="path of the new mount point") | ||
group = parser.add_mutually_exclusive_group(required=True) | ||
group.add_argument("--bind", action="store_const", const="bind", | ||
help="create a new mount point of existing path") | ||
group.add_argument("-t", "--type", help="filesystem type to mount") | ||
opts = parser.parse_args() | ||
if opts.bind is not None: | ||
mount(opts.source, opts.target, "", MountOpts.Bind) | ||
else: | ||
mount(opts.source, opts.target, opts.fstype) | ||
|
||
|
||
if __name__ == "__main__": | ||
try: | ||
main() | ||
except OSError as err: | ||
print(err, file=stderr) | ||
raise SystemExit(1) |
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,9 @@ | ||
#!/bin/sh | ||
if [ "$(command -v python3)" != "" ]; then | ||
exec python3 ./mount.py "$@"; | ||
elif [ "$(command -v python2)" != "" ]; then | ||
exec python2 ./mount.py "$@"; | ||
else | ||
echo "cannot mount: Python 2 or 3 required" | ||
exit 1 | ||
fi |
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