-
Notifications
You must be signed in to change notification settings - Fork 0
/
boot.go
146 lines (130 loc) · 4.01 KB
/
boot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package osman
import (
"bytes"
"context"
_ "embed"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"
"syscall"
"text/template"
"time"
"github.com/pkg/errors"
"github.com/outofforest/osman/config"
"github.com/outofforest/osman/infra/storage"
"github.com/outofforest/osman/infra/types"
)
func copyKernel(buildMountPoint string, storage config.Storage, buildID types.BuildID) error {
buildBootDir := filepath.Join(buildMountPoint, "boot")
return forEachBootMaster(bootPrefix(storage.Root), func(diskMountpoint string) error {
kernelDir := filepath.Join(diskMountpoint, "zfs", string(buildID))
if err := os.MkdirAll(kernelDir, 0o755); err != nil {
return errors.WithStack(err)
}
if err := copyFile(filepath.Join(kernelDir, "vmlinuz"), filepath.Join(buildBootDir, "vmlinuz"),
0o755); err != nil {
return errors.WithStack(err)
}
return errors.WithStack(copyFile(filepath.Join(kernelDir, "initramfs.img"),
filepath.Join(buildBootDir, "initramfs.img"), 0o600))
})
}
func cleanKernel(buildID types.BuildID, bootPrefix string) error {
return forEachBootMaster(bootPrefix, func(diskMountpoint string) error {
kernelDir := filepath.Join(diskMountpoint, "zfs", string(buildID))
if err := os.RemoveAll(kernelDir); err != nil && !errors.Is(err, os.ErrNotExist) {
return errors.WithStack(err)
}
return nil
})
}
//go:embed grub.tmpl.cfg
var grubTemplate string
var grubTemplateCompiled = template.Must(template.New("grub").Parse(grubTemplate))
type grubConfig struct {
StorageRoot string
Builds []types.BuildInfo
}
func generateGRUB(ctx context.Context, storage config.Storage, s storage.Driver) error {
builds, err := List(ctx, config.Filter{Types: []types.BuildType{types.BuildTypeBoot}}, s)
if err != nil {
return err
}
sort.Slice(builds, func(i, j int) bool {
return builds[i].CreatedAt.After(builds[j].CreatedAt)
})
for i, b := range builds {
if len(b.Tags) > 0 {
builds[i].Name += ":" + string(b.Tags[0])
}
}
config := grubConfig{
StorageRoot: storage.Root,
Builds: builds,
}
buf := &bytes.Buffer{}
if err := grubTemplateCompiled.Execute(buf, config); err != nil {
return errors.WithStack(err)
}
grubConfig := buf.Bytes()
return forEachBootMaster(bootPrefix(storage.Root), func(diskMountpoint string) error {
grubDir := filepath.Join(diskMountpoint, "grub2")
if err := os.WriteFile(filepath.Join(grubDir, "grub.cfg"), grubConfig, 0o644); err != nil {
return errors.WithStack(err)
}
return errors.WithStack(os.WriteFile(filepath.Join(grubDir, fmt.Sprintf("grub-%s.cfg",
time.Now().UTC().Format(time.RFC3339))), grubConfig, 0o644))
})
}
func forEachBootMaster(prefix string, fn func(mountpoint string) error) error {
path := "/dev/disk/by-label"
files, err := os.ReadDir(path)
if err != nil {
return errors.WithStack(err)
}
for _, f := range files {
if f.IsDir() || !strings.HasPrefix(f.Name(), prefix) {
continue
}
disk := filepath.Join(path, f.Name())
diskMountpoint, err := os.MkdirTemp("", prefix+"*")
if err != nil {
return errors.WithStack(err)
}
if err := syscall.Mount(disk, diskMountpoint, "ext4", 0, ""); err != nil {
return errors.Wrapf(err, "mounting disk '%s' failed", disk)
}
if err := fn(diskMountpoint); err != nil {
return err
}
if err := syscall.Unmount(diskMountpoint, 0); err != nil {
return errors.Wrapf(err, "unmounting disk '%s' failed", disk)
}
if err := os.Remove(diskMountpoint); err != nil {
return errors.WithStack(err)
}
}
return nil
}
func copyFile(dst, src string, perm os.FileMode) error {
//nolint:nosnakecase // imported constant
srcFile, err := os.OpenFile(src, os.O_RDONLY, 0)
if err != nil {
return errors.WithStack(err)
}
defer srcFile.Close()
//nolint:nosnakecase // imported constant
dstFile, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE, perm)
if err != nil {
return errors.WithStack(err)
}
defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile)
return errors.WithStack(err)
}
func bootPrefix(storageRoot string) string {
return "boot-" + filepath.Base(storageRoot) + "-"
}