Skip to content

Commit 38b4eaf

Browse files
committed
Implement Isolator interface in SystemdNSspawn and enhance Enter method
1 parent c385bf3 commit 38b4eaf

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

isolation/isolation.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
11
package isolation
22

3-
// Isolator defines the interface for entering a sandboxed environment.
4-
type Isolator interface {
5-
// Enter runs a command within the isolated environment.
6-
// 'name' is the machine name for the container.
7-
// 'path' is the directory to use as the root filesystem.
8-
// 'command' is the command to execute within the container. If empty,
9-
// an interactive shell should be started.
10-
Enter(name, path string, command ...string) error
3+
import (
4+
"os"
5+
"os/exec"
6+
)
7+
8+
// SystemdNSspawn provides isolation using systemd-nspawn.
9+
type SystemdNSspawn struct{}
10+
11+
// Enter uses systemd-nspawn to enter the sandbox environment. This implementation
12+
// now correctly matches the Isolator interface, accepting the 'path' argument.
13+
func (s *SystemdNSspawn) Enter(name, path string, command ...string) error {
14+
// --directory points to the root filesystem for the container.
15+
// --machine gives the container a unique, manageable name.
16+
args := []string{"--directory", path, "--machine", name}
17+
18+
// If no command is provided, default to an interactive bash shell.
19+
if len(command) == 0 {
20+
command = []string{"/bin/bash"}
21+
}
22+
args = append(args, command...)
23+
24+
cmd := exec.Command("systemd-nspawn", args...)
25+
cmd.Stdin = os.Stdin
26+
cmd.Stdout = os.Stdout
27+
cmd.Stderr = os.Stderr
28+
cmd.Env = os.Environ()
29+
30+
return cmd.Run()
1131
}

0 commit comments

Comments
 (0)