Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

builder: Don't use fakeroot #107

Merged
merged 3 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,12 @@ func (p *Package) BuildYpkg(notif PidNotifier, usr *UserInfo, pman *EopkgManager
return err
}

wdir := p.GetWorkDirInternal()
ymlFile := filepath.Join(wdir, filepath.Base(p.Path))
workDir := p.GetWorkDirInternal()
ymlFile := filepath.Join(workDir, filepath.Base(p.Path))
buildDir := filepath.Join(BuildUserHome, "YPKG")

// Now build the package
cmd := fmt.Sprintf("/bin/su %s -- fakeroot ypkg-build -D %s %s", BuildUser, wdir, ymlFile)
cmd := fmt.Sprintf("ypkg-build -D %s -B %s %s", workDir, buildDir, ymlFile)
if DisableColors {
cmd += " -n"
}
Expand Down
10 changes: 2 additions & 8 deletions builder/chroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,8 @@ func (p *Package) Chroot(notif PidNotifier, pman *EopkgManager, overlay *Overlay
// Allow bash to work
commands.SetStdin(os.Stdin)

// Legacy package format requires root, stay as root.
user := BuildUser
if p.Type == PackageTypeXML {
user = "root"
}

loginCommand := fmt.Sprintf("/bin/su - %s -s %s", user, BuildUserShell)
err := ChrootExecStdin(notif, overlay.MountPoint, loginCommand)
loginCommand := fmt.Sprintf("/bin/su - root -s %s", BuildUserShell)
err := ChrootShell(notif, overlay.MountPoint, loginCommand, BuildUserHome)

commands.SetStdin(nil)
notif.SetActivePID(0)
Expand Down
68 changes: 68 additions & 0 deletions builder/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"log/slog"
"os"
"os/exec"
"path"
"path/filepath"
"slices"
"strconv"
Expand Down Expand Up @@ -153,6 +154,8 @@ func SaneEnvironment(username, home string) []string {
fmt.Sprintf("HOME=%s", home),
fmt.Sprintf("USER=%s", username),
fmt.Sprintf("USERNAME=%s", username),
fmt.Sprintf("CCACHE_DIR=%s", path.Join(BuildUserHome, ".ccache")),
fmt.Sprintf("SCCACHE_DIR=%s", path.Join(BuildUserHome, ".cache", "sccache")),
}
// Consider an option to even filter these out
permitted := []string{
Expand Down Expand Up @@ -227,6 +230,71 @@ func ChrootExecStdin(notif PidNotifier, dir, command string) error {
return c.Wait()
}

func ChrootShell(notif PidNotifier, dir, command, workdir string) error {
// Hold an fd for the og root
fd, err := os.Open("/")
if err != nil {
return err
}

// Remember our working directory
wd, err2 := os.Getwd()
if err2 != nil {
return err2
}

// Ensure chroot directory is available
if err = os.Chdir(dir); err != nil {
return err
}

if err = syscall.Chroot(dir); err != nil {
fd.Close()
return err
}

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

// Spawn a shell
args := []string{"--login"}
c := exec.Command("/bin/bash", args...)
c.Stdout = os.Stdout
c.Stderr = os.Stdout
c.Stdin = os.Stdin
c.Env = ChrootEnvironment
c.Dir = workdir

if err = c.Start(); err != nil {
goto CLEANUP
}

notif.SetActivePID(c.Process.Pid)

if err = c.Wait(); err != nil {
goto CLEANUP
}

CLEANUP:
// Return to our original root and working directory
defer fd.Close()

if err = fd.Chdir(); err != nil {
return err
}

if err = syscall.Chroot("."); err != nil {
return err
}

if err = os.Chdir(wd); err != nil {
return err
}

return err
}

func StartSccache(dir string) {
var buf bytes.Buffer

Expand Down