Skip to content

Commit

Permalink
Merge pull request #107 from getsolus/drop-fakeroot
Browse files Browse the repository at this point in the history
builder: Don't use fakeroot
  • Loading branch information
ermo authored Sep 16, 2024
2 parents 8a53437 + 2a9786f commit e4c0a3d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 11 deletions.
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

0 comments on commit e4c0a3d

Please sign in to comment.