chroot
packageAPI reference for the chroot
package.
Imports
(3)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
Returns
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()
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
Returns
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
}
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
Returns
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()
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
Returns
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()
})
ExitPivot
ExitPivot exits the pivot and changes the current working directory
to the old root. It returns an error if any.
Returns
func ExitPivot() error
{
return syscall.PivotRoot(".", ".")
}
Example
err := chroot.ExitPivot()