Skip to content

Commit

Permalink
fix rootless
Browse files Browse the repository at this point in the history
Signed-off-by: Ziwen Ning <[email protected]>
  • Loading branch information
ningziwen committed Feb 19, 2023
1 parent 9e88239 commit 3e50c26
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
4 changes: 2 additions & 2 deletions cmd/nerdctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func initRootCmdFlags(rootCmd *cobra.Command, tomlPath string) (*pflag.FlagSet,
rootCmd.PersistentFlags().StringSlice("hosts-dir", cfg.HostsDir, "A directory that contains <HOST:PORT>/hosts.toml (containerd style) or <HOST:PORT>/{ca.cert, cert.pem, key.pem} (docker style)")
// Experimental enable experimental feature, see in https://github.com/containerd/nerdctl/blob/main/docs/experimental.md
AddPersistentBoolFlag(rootCmd, "experimental", nil, nil, cfg.Experimental, "NERDCTL_EXPERIMENTAL", "Control experimental: https://github.com/containerd/nerdctl/blob/main/docs/experimental.md")
rootCmd.PersistentFlags().String("host-gateway-ip", cfg.HostGatewayIP, "IP address that the special 'host-gateway' string in --add-host resolves to. Defaults to the IP address of the default bridge")
AddPersistentStringFlag(rootCmd, "host-gateway-ip", nil, nil, nil, aliasToBeInherited, cfg.HostGatewayIP, "HOST_GATEWAY_IP", "IP address that the special 'host-gateway' string in --add-host resolves to. Defaults to the IP address of the default bridge")
return aliasToBeInherited, nil
}

Expand Down Expand Up @@ -229,7 +229,7 @@ Config file ($NERDCTL_TOML): %s
}
if appNeedsRootlessParentMain(cmd, args) {
// reexec /proc/self/exe with `nsenter` into RootlessKit namespaces
return rootlessutil.ParentMain()
return rootlessutil.ParentMain(globalOptions.HostGatewayIP)
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/nerdctl/main_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func shellCompleteNamespaceNames(cmd *cobra.Command, args []string, toComplete s
return nil, cobra.ShellCompDirectiveError
}
if rootlessutil.IsRootlessParent() {
_ = rootlessutil.ParentMain()
_ = rootlessutil.ParentMain(globalOptions.HostGatewayIP)
return nil, cobra.ShellCompDirectiveNoFileComp
}
if err != nil {
Expand Down Expand Up @@ -60,7 +60,7 @@ func shellCompleteSnapshotterNames(cmd *cobra.Command, args []string, toComplete
return nil, cobra.ShellCompDirectiveError
}
if rootlessutil.IsRootlessParent() {
_ = rootlessutil.ParentMain()
_ = rootlessutil.ParentMain(globalOptions.HostGatewayIP)
return nil, cobra.ShellCompDirectiveNoFileComp
}
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address)
Expand Down
3 changes: 2 additions & 1 deletion pkg/rootlessutil/parent.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func RootlessKitChildPid(stateDir string) (int, error) {
return strconv.Atoi(pidStr)
}

func ParentMain() error {
func ParentMain(hostGatewayIP string) error {
if !IsRootlessParent() {
return errors.New("should not be called when !IsRootlessParent()")
}
Expand Down Expand Up @@ -104,5 +104,6 @@ func ParentMain() error {
os.Setenv("ROOTLESSKIT_STATE_DIR", stateDir)
os.Setenv("ROOTLESSKIT_PARENT_EUID", strconv.Itoa(os.Geteuid()))
os.Setenv("ROOTLESSKIT_PARENT_EGID", strconv.Itoa(os.Getegid()))
os.Setenv("HOST_GATEWAY_IP", hostGatewayIP)
return syscall.Exec(arg0, args, os.Environ())
}

0 comments on commit 3e50c26

Please sign in to comment.