chroot API

chroot

package

API reference for the chroot package.

F
function

EnterChroot

EnterChroot enters a chroot and changes the current working directory
to the new root. It returns a function that exits the chroot and
changes the current working directory to the old root. Remember to
call this function after you are done with the chroot to avoid
leaving the process in a chroot.

Parameters

rootFs
string

Returns

f
func() error
err
error
pkg/v1/chroot/chroot.go:33-58
func EnterChroot(rootFs string) (f func() error, err error)

{
	fd, err := os.Open("/")
	if err != nil {
		return nil, err
	}

	closeFunc := func() error {
		defer fd.Close()
		if err := fd.Chdir(); err != nil {
			return err
		}
		return syscall.Chroot(".")
	}

	err = syscall.Chroot(rootFs)
	if err != nil {
		return closeFunc, err
	}

	err = os.Chdir("/")
	if err != nil {
		return closeFunc, err
	}

	return closeFunc, nil
}

Example

fExit, err := chroot.EnterChroot("/path/to/new/root")
if err != nil {
	fmt.Printf("Error entering chroot root: %v\n", err)
	if fExit != nil {
		fExit()
	}
	return
}
defer fExit()
F
function

RunChroot

RunChroot runs a function in a chroot and changes the current working
environment to the new root. It exits the chroot after the function
returns. The function returns the error returned by the function and
the error returned by ExitChroot if any.

Parameters

rootFs
string
f
func() error

Returns

fErr
error
err
error
pkg/v1/chroot/chroot.go:74-99
func RunChroot(rootFs string, f func() error) (fErr, err error)

{
	fd, err := os.Open("/")
	if err != nil {
		return nil, err
	}

	err = syscall.Chroot(rootFs)
	if err != nil {
		fd.Close()
		return nil, err
	}

	err = os.Chdir("/")
	if err != nil {
		fd.Close()
		err = syscall.Chroot(".")
		return nil, err
	}

	fErr = f()

	fd.Chdir()
	fd.Close()

	return fErr, syscall.Chroot(".")
}

Example

err := chroot.RunChroot("/path/to/new/root", func() error {
	return exec.Command("ls", "-l").Run()
})
if err != nil {
	fmt.Printf("Error running command in chroot root: %v\n", err)
	return
}
F
function

EnterPivot

EnterPivot enters a pivot and changes the current working directory
to the new root. It returns a function that exits the pivot and
changes the current working directory to the old root. Remember to
call this function after you are done with the pivot to avoid
leaving the process in a pivot.

Parameters

rootFs
string

Returns

f
func() error
err
error
pkg/v1/chroot/pivot.go:34-65
func EnterPivot(rootFs string) (f func() error, err error)

{
	fd, err := os.Open("/")
	if err != nil {
		return nil, err
	}

	closeFunc := func() error {
		defer fd.Close()
		if err := fd.Chdir(); err != nil {
			return err
		}
		return syscall.PivotRoot(".", rootFs)
	}

	pivotDir := filepath.Join(rootFs, ".pivot_root")
	err = os.MkdirAll(pivotDir, 0755)
	if err != nil {
		return closeFunc, err
	}

	err = syscall.PivotRoot(rootFs, pivotDir)
	if err != nil {
		return closeFunc, err
	}

	err = os.Chdir("/")
	if err != nil {
		return closeFunc, err
	}

	return closeFunc, nil
}

Example

fExit, err := chroot.EnterPivot("/path/to/new/root")
if err != nil {
	fmt.Printf("Error entering pivot root: %v\n", err)
	if fExit != nil {
		fExit()
	}
	return
}
defer fExit()
F
function

RunPivot

RunPivot runs a function in a pivot and changes the current working
environment to the new root. It exits the pivot after the function
returns. The function returns the error returned by the function and
the error returned by ExitPivot if any.

Parameters

rootFs
string
f
func() error

Returns

error
pkg/v1/chroot/pivot.go:77-85
func RunPivot(rootFs string, f func() error) error

{
	fExit, err := EnterPivot(rootFs)
	if err != nil {
		return err
	}
	defer fExit()

	return f()
}

Example

err := chroot.RunPivot("/path/to/new/root", func() error {
	return exec.Command("ls", "-l").Run()
})
F
function

ExitPivot

ExitPivot exits the pivot and changes the current working directory
to the old root. It returns an error if any.

Returns

error
pkg/v1/chroot/pivot.go:93-95
func ExitPivot() error

{
	return syscall.PivotRoot(".", ".")
}

Example

err := chroot.ExitPivot()