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))
}
posted on 2025-07-23 11:29  umichan  阅读(28)  评论(0)    收藏  举报