golang基于exec.Command调用chroot命令
chroot命令无法直接使用exec.Command调用,提示错误代码125或127
以查看宿主机磁盘的命令为例
package main
import (
"fmt"
"os/exec"
"syscall"
)
func main() {
// Replace with your chroot path
chrootPath := "/host"
cmd := exec.Command("df", "-Th")
cmd.SysProcAttr = &syscall.SysProcAttr{
Chroot: chrootPath,
}
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("Error executing command in chroot: %v\n", err)
return
}
fmt.Printf("Output from chroot: \n%s\n", string(output))
}
浙公网安备 33010602011771号