Commit cc9b79ca authored by songlinfeng's avatar songlinfeng
Browse files

support rootless for podman

parent 3b3a28f3
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"dtk-container-toolkit/cmd/dtk-ctk/config" "dtk-container-toolkit/cmd/dtk-ctk/config"
"dtk-container-toolkit/cmd/dtk-ctk/hook" "dtk-container-toolkit/cmd/dtk-ctk/hook"
"dtk-container-toolkit/cmd/dtk-ctk/runtime" "dtk-container-toolkit/cmd/dtk-ctk/runtime"
"dtk-container-toolkit/cmd/dtk-ctk/rootless"
"dtk-container-toolkit/internal/info" "dtk-container-toolkit/internal/info"
"os" "os"
...@@ -71,6 +72,7 @@ func main() { ...@@ -71,6 +72,7 @@ func main() {
// Define the subcommands // Define the subcommands
c.Commands = []*cli.Command{ c.Commands = []*cli.Command{
rootless.NewCommand(logger),
runtime.NewCommand(logger), runtime.NewCommand(logger),
config.NewCommand(logger), config.NewCommand(logger),
hook.NewCommand(logger), hook.NewCommand(logger),
......
package rootless
import (
"dtk-container-toolkit/internal/logger"
"fmt"
"os"
"os/exec"
"github.com/urfave/cli/v2"
)
type rootlessCommand struct {
logger logger.Interface
}
type options struct {
runtime string
}
// NewCommand constructs a rootless command with the specified logger
func NewCommand(logger logger.Interface) *cli.Command {
c := rootlessCommand{
logger: logger,
}
return c.build()
}
// build
func (m rootlessCommand) build() *cli.Command {
// Create the 'rootless' command
opts := options{}
c := cli.Command{
Name: "rootless",
Usage: "Provide tools for configuring rootless container runtimes",
Action: func(ctx *cli.Context) error { return run(ctx, &opts) },
}
c.Flags = []cli.Flag{
&cli.StringFlag{
Name: "runtime",
Aliases: []string{"r"},
Usage: "docker or podman",
Value: "docker",
Destination: &opts.runtime,
},
}
return &c
}
func run(c *cli.Context, opts *options) error {
runtime := opts.runtime
if runtime != "docker" && runtime != "podman" {
return fmt.Errorf("invalid runtime %s", runtime)
}
_, err := os.Stat("/usr/bin/s" + runtime)
if os.IsNotExist(err) {
cmd := exec.Command("cp", "/usr/bin/"+runtime, "/usr/bin/s"+runtime)
cmd.Run()
}
_, err = os.Stat("/usr/bin/" + runtime)
if err == nil {
os.Remove("/usr/bin/" + runtime)
}
cmd := exec.Command("cp", "/usr/bin/dtk-docker", "/usr/bin/"+runtime)
err = cmd.Run()
if err != nil {
return fmt.Errorf("failed to copy dtk-docker to /usr/bin/%s: %w", runtime, err)
}
return nil
}
...@@ -44,6 +44,7 @@ func HasRunSubcommand(args [] string) bool { ...@@ -44,6 +44,7 @@ func HasRunSubcommand(args [] string) bool {
} }
func main() { func main() {
runtime := os.Args[0]
args := os.Args[1:] args := os.Args[1:]
newArgs := []string{} newArgs := []string{}
if !HasRunSubcommand(args) { if !HasRunSubcommand(args) {
...@@ -61,7 +62,7 @@ func main() { ...@@ -61,7 +62,7 @@ func main() {
} }
} }
cmd := exec.Command("sdocker", newArgs...) cmd := exec.Command("s"+runtime, newArgs...)
cmd.Stdin = os.Stdin cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
......
...@@ -32,8 +32,6 @@ install -m 755 -t %{buildroot}%{_bindir} dtk-docker ...@@ -32,8 +32,6 @@ install -m 755 -t %{buildroot}%{_bindir} dtk-docker
if [ ! -e %{_bindir}/nvidia-container-runtime-hook ]; then if [ ! -e %{_bindir}/nvidia-container-runtime-hook ]; then
# repairing lost file nvidia-container-runtime-hook # repairing lost file nvidia-container-runtime-hook
ln -sf %{_bindir}/dtk-container-runtime %{_bindir}/nvidia-container-runtime-hook ln -sf %{_bindir}/dtk-container-runtime %{_bindir}/nvidia-container-runtime-hook
mv %{_bindir}/docker %{_bindir}/sdocker
mv %{_bindir}/dtk-docker %{_bindir}/docker
fi fi
# Generate the default config; If this file already exists no changes are made. # Generate the default config; If this file already exists no changes are made.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment