-
Notifications
You must be signed in to change notification settings - Fork 2.7k
support for storage for rootless containers #936
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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
da98a33
rootless: add management for the userNS
giuseppe 830e1c3
oci: set XDG_RUNTIME_DIR to the runtime from GetRootlessRuntimeDir()
giuseppe 46239c1
rootless: do not configure additional groups
giuseppe c1b2111
test: add env variables to the debug output
giuseppe dcc5d22
rootless: use $HOME/.config/containers/libpod.conf
giuseppe 049e0dd
runtime: change rootless data storage default path
giuseppe 2bc4eb0
docs: add documentation for rootless containers
giuseppe 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,145 @@ | ||
package rootless | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
gosignal "os/signal" | ||
"runtime" | ||
"syscall" | ||
|
||
"github.com/containers/storage/pkg/idtools" | ||
"github.com/docker/docker/pkg/signal" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
/* | ||
extern int reexec_in_user_namespace(int ready); | ||
extern int reexec_in_user_namespace_wait(int pid); | ||
*/ | ||
import "C" | ||
|
||
func runInUser() error { | ||
os.Setenv("_LIBPOD_USERNS_CONFIGURED", "done") | ||
return nil | ||
} | ||
|
||
// IsRootless tells us if we are running in rootless mode | ||
func IsRootless() bool { | ||
return os.Getuid() != 0 || os.Getenv("_LIBPOD_USERNS_CONFIGURED") != "" | ||
} | ||
|
||
func tryMappingTool(tool string, pid int, hostID int, mappings []idtools.IDMap) error { | ||
path, err := exec.LookPath(tool) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
appendTriplet := func(l []string, a, b, c int) []string { | ||
return append(l, fmt.Sprintf("%d", a), fmt.Sprintf("%d", b), fmt.Sprintf("%d", c)) | ||
} | ||
|
||
args := []string{path, fmt.Sprintf("%d", pid)} | ||
args = appendTriplet(args, 0, hostID, 1) | ||
if mappings != nil { | ||
for _, i := range mappings { | ||
args = appendTriplet(args, i.ContainerID+1, i.HostID, i.Size) | ||
} | ||
} | ||
cmd := exec.Cmd{ | ||
Path: path, | ||
Args: args, | ||
} | ||
return cmd.Run() | ||
} | ||
|
||
// BecomeRootInUserNS re-exec podman in a new userNS | ||
func BecomeRootInUserNS() (bool, error) { | ||
|
||
if os.Getuid() == 0 || os.Getenv("_LIBPOD_USERNS_CONFIGURED") != "" { | ||
if os.Getenv("_LIBPOD_USERNS_CONFIGURED") == "init" { | ||
return false, runInUser() | ||
} | ||
return false, nil | ||
} | ||
|
||
runtime.LockOSThread() | ||
defer runtime.UnlockOSThread() | ||
|
||
r, w, err := os.Pipe() | ||
if err != nil { | ||
return false, err | ||
} | ||
defer r.Close() | ||
defer w.Close() | ||
|
||
pidC := C.reexec_in_user_namespace(C.int(r.Fd())) | ||
pid := int(pidC) | ||
if pid < 0 { | ||
return false, errors.Errorf("cannot re-exec process") | ||
} | ||
|
||
setgroups := fmt.Sprintf("/proc/%d/setgroups", pid) | ||
err = ioutil.WriteFile(setgroups, []byte("deny\n"), 0666) | ||
if err != nil { | ||
return false, errors.Wrapf(err, "cannot write setgroups file") | ||
} | ||
|
||
var uids, gids []idtools.IDMap | ||
username := os.Getenv("USER") | ||
mappings, err := idtools.NewIDMappings(username, username) | ||
if err == nil { | ||
uids = mappings.UIDs() | ||
gids = mappings.GIDs() | ||
} | ||
|
||
uidsMapped := false | ||
if mappings != nil && uids != nil { | ||
uidsMapped = tryMappingTool("newuidmap", pid, os.Getuid(), uids) == nil | ||
} | ||
if !uidsMapped { | ||
uidMap := fmt.Sprintf("/proc/%d/uid_map", pid) | ||
err = ioutil.WriteFile(uidMap, []byte(fmt.Sprintf("%d %d 1\n", 0, os.Getuid())), 0666) | ||
if err != nil { | ||
return false, errors.Wrapf(err, "cannot write uid_map") | ||
} | ||
} | ||
|
||
gidsMapped := false | ||
if mappings != nil && gids != nil { | ||
gidsMapped = tryMappingTool("newgidmap", pid, os.Getgid(), gids) == nil | ||
} | ||
if !gidsMapped { | ||
gidMap := fmt.Sprintf("/proc/%d/gid_map", pid) | ||
err = ioutil.WriteFile(gidMap, []byte(fmt.Sprintf("%d %d 1\n", 0, os.Getgid())), 0666) | ||
if err != nil { | ||
return false, errors.Wrapf(err, "cannot write gid_map") | ||
} | ||
} | ||
|
||
_, err = w.Write([]byte("1")) | ||
if err != nil { | ||
return false, errors.Wrapf(err, "write to sync pipe") | ||
} | ||
|
||
c := make(chan os.Signal, 1) | ||
|
||
gosignal.Notify(c) | ||
defer gosignal.Reset() | ||
go func() { | ||
for s := range c { | ||
if s == signal.SIGCHLD || s == signal.SIGPIPE { | ||
continue | ||
} | ||
|
||
56B5 td> | syscall.Kill(int(pidC), s.(syscall.Signal)) | |
} | ||
}() | ||
|
||
if C.reexec_in_user_namespace_wait(pidC) < 0 { | ||
return false, errors.Wrapf(err, "error waiting for the re-exec process") | ||
} | ||
|
||
return true, nil | ||
} |
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.
Does this need to be unlocked somewhere? I'm not sure of a way around it, but a number of google posts to slow performance when this lock is used. Something to watch for.
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.
it should not really matter here as the program is supposed to exit after the function call. I am going to add an
Unlock
though as the fu A377 nction doesn't change the state of the thread, I've added it just to play safe