Skip to content

Commit df6bd80

Browse files
authored
Merge pull request #660 from oasisprotocol/ptrus/feature/rootless-containers-fixes
rofl/build: Fix concat
2 parents 6177878 + 9aac458 commit df6bd80

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

build/env/env.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"path/filepath"
88
"strings"
99
"sync"
10-
11-
"github.com/oasisprotocol/cli/cmd/common"
1210
)
1311

1412
// ExecEnv is an execution environment.
@@ -195,19 +193,19 @@ func (de *ContainerEnv) PathToEnv(path string) (string, error) {
195193
}
196194

197195
// FixPermissions implements ExecEnv.
196+
// For container environments, we use chmod to make files accessible rather than chown,
197+
// because chown doesn't work correctly with rootless containers due to user namespace
198+
// UID mapping. Using chmod 666 works for both rootful and rootless containers.
198199
func (de *ContainerEnv) FixPermissions(path string) error {
199-
path, err := de.PathToEnv(path)
200+
pathEnv, err := de.PathToEnv(path)
200201
if err != nil {
201202
return err
202203
}
203204

204-
cmd := exec.Command("chown", fmt.Sprintf("%d:%d", os.Getuid(), os.Getgid()), path) //nolint: gosec
205+
cmd := exec.Command("chmod", "666", pathEnv)
205206
if err = de.WrapCommand(cmd); err != nil {
206207
return err
207208
}
208-
if common.IsVerbose() {
209-
fmt.Println(cmd)
210-
}
211209
return cmd.Run()
212210
}
213211

cmd/rofl/build/artifacts.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ func createVerityHashTree(buildEnv env.ExecEnv, fsFn, hashFn string) (string, er
459459
return "", fmt.Errorf("%w\n%s", err, out.String())
460460
}
461461

462+
// Fix permissions so files are accessible from the host.
462463
if err = buildEnv.FixPermissions(fsFn); err != nil {
463464
return "", err
464465
}
@@ -476,22 +477,31 @@ func createVerityHashTree(buildEnv env.ExecEnv, fsFn, hashFn string) (string, er
476477
return string(data), nil
477478
}
478479

479-
// concatFiles appends the contents of file b to a.
480-
func concatFiles(a, b string) error {
481-
df, err := os.OpenFile(a, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
480+
// concatFiles appends the contents of file b to a using the given build environment.
481+
func concatFiles(buildEnv env.ExecEnv, a, b string) error {
482+
aEnv, err := buildEnv.PathToEnv(a)
482483
if err != nil {
483-
return err
484+
return fmt.Errorf("failed to translate path: %w", err)
484485
}
485-
defer df.Close()
486-
487-
sf, err := os.Open(b)
486+
bEnv, err := buildEnv.PathToEnv(b)
488487
if err != nil {
489-
return err
488+
return fmt.Errorf("failed to translate path: %w", err)
490489
}
491-
defer sf.Close()
492490

493-
_, err = io.Copy(df, sf)
494-
return err
491+
// Use shell to append file b to file a instead of os/io packages. This ensures
492+
// the operation works correctly with containerized builds where the host may not
493+
// have write permissions to container-created files.
494+
cmd := exec.Command("sh", "-c", fmt.Sprintf("cat %q >> %q", bEnv, aEnv)) //nolint:gosec
495+
var out strings.Builder
496+
cmd.Stderr = &out
497+
cmd.Stdout = &out
498+
if err = buildEnv.WrapCommand(cmd); err != nil {
499+
return err
500+
}
501+
if err = cmd.Run(); err != nil {
502+
return fmt.Errorf("%w\n%s", err, out.String())
503+
}
504+
return nil
495505
}
496506

497507
// padWithEmptySpace pads the given file with empty space to make it the given size. See

cmd/rofl/build/tdx.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func tdxPrepareStage2(
166166
}
167167

168168
// Concatenate filesystem and hash tree into one image.
169-
if err = concatFiles(rootfsImage, hashFile); err != nil {
169+
if err = concatFiles(buildEnv, rootfsImage, hashFile); err != nil {
170170
return nil, fmt.Errorf("failed to concatenate rootfs and hash tree files: %w", err)
171171
}
172172

0 commit comments

Comments
 (0)