Skip to content

Commit 9934711

Browse files
committed
Update: Add systemd-boot preparation
1 parent 41fff77 commit 9934711

4 files changed

Lines changed: 115 additions & 4 deletions

File tree

alteriso5/utils/os.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,29 @@ func ReadFileLine(path string) ([]string, error) {
1414
lines := strings.Split(string(bytes), "\n")
1515
return lines, nil
1616
}
17+
18+
func GetFileSizesInDir(dir string) (map[string]int64, error) {
19+
items, err := os.ReadDir(dir)
20+
if err != nil {
21+
return nil, err
22+
}
23+
24+
ret := map[string]int64{}
25+
26+
for _, i := range items {
27+
info, err := i.Info()
28+
if err != nil {
29+
continue
30+
}
31+
ret[info.Name()] = info.Size()
32+
}
33+
return ret, nil
34+
}
35+
36+
func GetFileSize(file string) (int64, error) {
37+
info, err := os.Stat(file)
38+
if err != nil {
39+
return 0, err
40+
}
41+
return info.Size(), nil
42+
}

alteriso5/work/airootfs/chroot.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ type Chroot struct {
1515

1616
func GetChrootDir(dir, arch, config string) (*Chroot, error) {
1717
env := Chroot{
18-
Arch: arch,
19-
Dir: dir,
18+
Arch: arch,
19+
Dir: dir,
2020
Config: config,
2121
}
2222

@@ -65,6 +65,10 @@ type kernel struct {
6565
Initrd string
6666
}
6767

68+
func (k *kernel) Files() []string {
69+
return []string{k.Linux, k.Initrd}
70+
}
71+
6872
// func (e *Chroot) FindKernels() ([]kernel, error) {
6973
// kernels := []kernel{}
7074

alteriso5/work/task-bootmode-uefi.go

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,86 @@
11
package work
22

3-
import "log/slog"
3+
import (
4+
"log/slog"
5+
"os"
6+
"path"
7+
8+
"github.com/FascodeNet/alterlinux/alteriso5/utils"
9+
"github.com/FascodeNet/alterlinux/alteriso5/work/boot"
10+
"github.com/Hayao0819/nahi/osutils"
11+
)
412

513
// Prepare configuration files for systemd-boot
614
var makeCommonSystemdBootConfig *BuildTask = NewBuildTask("makeCommonSystemdBootConfig", func(w Work) error {
15+
var total int64 = 0
16+
// TODO: Get ucode list
17+
18+
// Calculate ESP size
19+
20+
{
21+
efiboot_files := []string{}
22+
23+
// For UEFI x64
24+
if w.profile.HasBootMode(boot.UefiX64SystemdBootEsp) || w.profile.HasBootMode(boot.UefiX64SystemdBootElTorito) {
25+
efiboot_files = append(efiboot_files,
26+
path.Join(w.Dirs.Pacstrap, "usr", "lib", "systemd", "boot", "efi", "systemd-bootx64.efi"),
27+
path.Join(w.Dirs.Pacstrap, "usr", "share", "edk2-shell", "x64", "Shell_Full.efi"),
28+
path.Join(w.Dirs.Pacstrap, "boot", "memtest86+", "memtest.efi"),
29+
path.Join(w.Dirs.Pacstrap, "usr", "share", "licenses", "spdx", "GPL-2.0-only.txt"),
30+
)
31+
}
32+
33+
// For UEFI ia32
34+
if w.profile.HasBootMode(boot.UefiIa32SystemdBootEsp) || w.profile.HasBootMode(boot.UefiIa32SystemdBootElTorito) {
35+
efiboot_files = append(efiboot_files,
36+
path.Join(w.Dirs.Pacstrap, "usr", "lib", "systemd", "boot", "efi", "systemd-bootia32.efi"),
37+
path.Join(w.Dirs.Pacstrap, "usr", "share", "edk2-shell", "ia32", "Shell_Full.efi"),
38+
)
39+
}
40+
41+
// For kernel
42+
{
43+
chroot, err := w.GetChroot()
44+
if err != nil {
45+
return err
46+
}
47+
kernels, err := chroot.FindKernels()
48+
if err != nil {
49+
return err
50+
}
51+
for _, k := range kernels {
52+
for _, f := range k.Files() {
53+
if osutils.Exists(f) {
54+
efiboot_files = append(efiboot_files, f)
55+
}
56+
}
57+
}
58+
}
59+
60+
for _, f := range efiboot_files {
61+
if s, err := utils.GetFileSize(f); err == nil {
62+
total += s
63+
} else {
64+
slog.Warn("Failed to get file size", "file", f, "error", err)
65+
}
66+
}
67+
68+
slog.Debug("Found files for efiboot", "files", efiboot_files)
69+
}
70+
71+
// For efiboot files
72+
if err := os.MkdirAll(path.Join(w.Base, w.target.Arch, "efiboot"), 0755); err != nil {
73+
return err
74+
}
75+
sizes, err := utils.GetFileSizesInDir(path.Join(w.Base, w.target.Arch, "efiboot"))
76+
if err != nil {
77+
return err
78+
}
79+
for _, s := range sizes {
80+
total += s
81+
}
82+
83+
slog.Debug("efiboot img size", "size", total)
784
return nil
885
})
986

profile/profiledef.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{
22
"install_dir": "alter",
3-
"bootmodes": ["bios.syslinux.mbr", "bios.syslinux.eltorito"],
3+
"bootmodes": [
4+
"bios.syslinux.mbr",
5+
"bios.syslinux.eltorito",
6+
"uefi-x64.systemd-boot.esp"
7+
],
48
"use_alter_syslinux": true
59
}

0 commit comments

Comments
 (0)