Skip to content

Commit

Permalink
gen-manifests: derrive seedArg from filename and env
Browse files Browse the repository at this point in the history
To ensure that manifests get random(ish) and stable UUIDs we set
the seed arg based on the filename and the environment
(`OSBUILD_GEN_MANIFEST_RNG_SEED`).

This should fix the issue discovered in
osbuild/manifest-db#124

that duplicated UUIDs for xfs/btrfs can trigger random(ish) and
hard to diagnose errors.
  • Loading branch information
mvo5 committed May 2, 2024
1 parent 9f26ef7 commit 18294f3
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion cmd/gen-manifests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import (
"encoding/json"
"flag"
"fmt"
"hash/fnv"
"io"
"os"
"path/filepath"
"sort"
"strconv"
"strings"

"github.com/osbuild/images/pkg/blueprint"
Expand Down Expand Up @@ -127,6 +129,14 @@ func makeManifestJob(name string, imgType distro.ImageType, cr composeRequest, d
filename := fmt.Sprintf("%s-%s-%s-boot.json", u(distroName), u(archName), u(name))
cacheDir := filepath.Join(cacheRoot, archName+distribution.Name())

// ensure that each file has a unique seed based on filename
hash := func(s string) int64 {
h := fnv.New64()
h.Write([]byte(filename))
return int64(h.Sum64())
}
seedArg += hash(filename)

options := distro.ImageOptions{Size: 0}
if cr.OSTree != nil {
options.OSTree = &ostree.ImageOptions{
Expand Down Expand Up @@ -440,6 +450,18 @@ func mergeOverrides(base, overrides composeRequest) composeRequest {
return merged
}

func seedArgFromEnv() (int64, error) {
seedFromEnv := os.Getenv("OSBUILD_GEN_MANIFEST_RNG_SEED")
if seedFromEnv == "" {
return 0, nil
}
seedArg, err := strconv.ParseInt(seedFromEnv, 10, 64)
if err != nil {
return 0, fmt.Errorf("cannot parse env OSBUILD_GEN_MANIFEST_RNG_SEED: %w", err)
}
return seedArg, nil
}

func main() {
// common args
var outputDir, cacheRoot string
Expand All @@ -456,7 +478,10 @@ func main() {

flag.Parse()

seedArg := int64(0)
seedArg, err := seedArgFromEnv()
if err != nil {
panic(err)
}
darm := readRepos()
distroFac := distrofactory.NewDefault()
jobs := make([]manifestJob, 0)
Expand Down

0 comments on commit 18294f3

Please sign in to comment.