Skip to content

Commit d83f0a2

Browse files
authored
refactor kernel_repack with new features to ease usage and maintenance (#9)
fairly large change, but has been tested and in use for a while. * new command: getchangelog - * new command: updatekernel - run an update
2 parents 8509691 + f3f72c3 commit d83f0a2

33 files changed

+8743
-8032
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"os"
7+
"strings"
8+
9+
"github.com/pkg/errors"
10+
repack_v1 "go.resf.org/peridot/tools/kernelmanager/packager/v1"
11+
)
12+
13+
func readChangelogFromSpec(specBytes []byte) []*repack_v1.ChangelogEntry {
14+
// Parse spec down to %changelog
15+
var changelogLines []string
16+
17+
lines := strings.Split(string(specBytes), "\n")
18+
for i, line := range lines {
19+
if strings.Contains(line, "%changelog") {
20+
changelogLines = lines[i+1:]
21+
break
22+
}
23+
}
24+
25+
// Parse changelog
26+
var entries []*repack_v1.ChangelogEntry
27+
currentEntry := &repack_v1.ChangelogEntry{}
28+
for _, line := range changelogLines {
29+
line = strings.TrimSpace(line)
30+
if line == "" {
31+
if currentEntry.Subject != "" {
32+
entries = append(entries, currentEntry)
33+
currentEntry = &repack_v1.ChangelogEntry{}
34+
}
35+
continue
36+
}
37+
38+
if strings.HasPrefix(line, "* ") {
39+
currentEntry.Subject = strings.TrimPrefix(line, "* ")
40+
} else if strings.HasPrefix(line, "- ") {
41+
currentEntry.Messages = append(currentEntry.Messages, strings.TrimPrefix(line, "- "))
42+
}
43+
}
44+
45+
if currentEntry.Subject != "" {
46+
entries = append(entries, currentEntry)
47+
}
48+
49+
return entries
50+
}
51+
52+
func main() {
53+
args := os.Args[1:]
54+
if len(args) == 0 {
55+
panic("usage: getchangelog <path to spec>")
56+
}
57+
58+
specPath := args[0]
59+
specBytes, err := os.ReadFile(specPath)
60+
if err != nil {
61+
panic(errors.Wrap(err, "failed to read spec file"))
62+
}
63+
64+
entries := readChangelogFromSpec(specBytes)
65+
66+
jsonBytes, err := json.Marshal(entries)
67+
if err != nil {
68+
panic(errors.Wrap(err, "failed to marshal entries"))
69+
}
70+
71+
_, err = fmt.Println(string(jsonBytes))
72+
if err != nil {
73+
panic(errors.Wrap(err, "failed to write entries"))
74+
}
75+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func readSpec(t *testing.T, name string) []byte {
12+
specBytes, err := os.ReadFile(fmt.Sprintf("testdata/%s.spec", name))
13+
require.Nil(t, err)
14+
15+
return specBytes
16+
}
17+
18+
func TestReadChangelogFromSpec_1(t *testing.T) {
19+
specBytes := readSpec(t, "1")
20+
entries := readChangelogFromSpec(specBytes)
21+
22+
require.Len(t, entries, 1)
23+
require.Equal(t, "Tue Feb 22 2024 Mustafa Gezen - 1.0-123", entries[0].Subject)
24+
require.Len(t, entries[0].Messages, 1)
25+
require.Equal(t, "Test changelog", entries[0].Messages[0])
26+
}
27+
28+
func TestReadChangelogFromSpec_2(t *testing.T) {
29+
specBytes := readSpec(t, "2")
30+
entries := readChangelogFromSpec(specBytes)
31+
32+
require.Len(t, entries, 2)
33+
34+
require.Equal(t, "Tue Feb 22 2024 Mustafa Gezen - 1.0-123", entries[0].Subject)
35+
require.Len(t, entries[0].Messages, 1)
36+
require.Equal(t, "Test changelog", entries[0].Messages[0])
37+
38+
require.Equal(t, "Tue Feb 22 2024 Mustafa Gezen - 1.0-123", entries[1].Subject)
39+
require.Len(t, entries[1].Messages, 2)
40+
require.Equal(t, "msg1 2", entries[1].Messages[0])
41+
require.Equal(t, "msg2", entries[1].Messages[1])
42+
}
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
# All global changes to build and install should follow this line.
2+
3+
# Disable LTO in userspace packages.
4+
%global _lto_cflags %{nil}
5+
6+
# The libexec directory is not used by the linker, so the shared object there
7+
# should not be exported to RPM provides.
8+
%global __provides_exclude_from ^%{_libexecdir}/kselftests
9+
10+
# Disable the find-provides.ksyms script.
11+
%global __provided_ksyms_provides %{nil}
12+
13+
# All global wide changes should be above this line otherwise
14+
# the %%install section will not see them.
15+
%global __spec_install_pre %{___build_pre}
16+
17+
# Kernel has several large (hundreds of mbytes) rpms, they take ~5 mins
18+
# to compress by single-threaded xz. Switch to threaded compression,
19+
# and from level 2 to 3 to keep compressed sizes close to "w2" results.
20+
#
21+
# NB: if default compression in /usr/lib/rpm/redhat/macros ever changes,
22+
# this one might need tweaking (e.g. if default changes to w3.xzdio,
23+
# change below to w4T.xzdio):
24+
%global _binary_payload w3T.xzdio
25+
26+
# Define the version of the Linux Kernel Archive tarball.
27+
%global LKAver 1.0
28+
29+
# Define the buildid, if required.
30+
%global buildid 123
31+
32+
# Determine the sublevel number and set pkg_version.
33+
%define sublevel %(echo %{LKAver} | %{__awk} -F\. '{ print $3 }')
34+
%if "%{sublevel}" == ""
35+
%global pkg_version %{LKAver}.0
36+
%else
37+
%global pkg_version %{LKAver}
38+
%endif
39+
40+
# Set pkg_release.
41+
%global pkg_release 1%{?buildid}%{?dist}
42+
43+
# Architectures upon which we can sign the kernel
44+
# for secure boot authentication.
45+
%ifarch x86_64 || aarch64
46+
%global signkernel 1
47+
%else
48+
%global signkernel 0
49+
%endif
50+
51+
# Sign modules on all architectures that build modules.
52+
%ifarch x86_64 || aarch64
53+
%global signmodules 1
54+
%else
55+
%global signmodules 0
56+
%endif
57+
58+
# Compress modules on all architectures that build modules.
59+
%ifarch x86_64 || aarch64
60+
%global zipmodules 1
61+
%else
62+
%global zipmodules 0
63+
%endif
64+
65+
%if %{zipmodules}
66+
%global zipsed -e 's/\.ko$/\.ko.xz/'
67+
# For parallel xz processes. Replace with 1 to go back to single process.
68+
%global zcpu `nproc --all`
69+
%endif
70+
71+
# The following build options are enabled by default, but may become disabled
72+
# by later architecture-specific checks. These can also be disabled by using
73+
# --without <opt> in the rpmbuild command, or by forcing these values to 0.
74+
#
75+
# {{.KernelPackage}}
76+
%define with_std %{?_without_std: 0} %{?!_without_std: 1}
77+
#
78+
# {{.KernelPackage}}-headers
79+
%define with_headers %{?_without_headers: 0} %{?!_without_headers: 1}
80+
#
81+
# {{.KernelPackage}}-doc
82+
%define with_doc %{?_without_doc: 0} %{?!_without_doc: 1}
83+
#
84+
# perf
85+
%define with_perf %{?_without_perf: 0} %{?!_without_perf: 1}
86+
#
87+
# tools
88+
%define with_tools %{?_without_tools: 0} %{?!_without_tools: 1}
89+
#
90+
# bpf tool
91+
%define with_bpftool %{?_without_bpftool: 0} %{?!_without_bpftool: 1}
92+
#
93+
# control whether to install the vdso directories
94+
%define with_vdso_install %{?_without_vdso_install: 0} %{?!_without_vdso_install: 1}
95+
#
96+
# Additional option for toracat-friendly, one-off, {{.KernelPackage}} building.
97+
# Only build the base {{.KernelPackage}} (--with baseonly):
98+
%define with_baseonly %{?_with_baseonly: 1} %{?!_with_baseonly: 0}
99+
100+
%global KVERREL %{pkg_version}-%{pkg_release}.%{_target_cpu}
101+
102+
# If requested, only build base {{.KernelPackage}} package.
103+
%if %{with_baseonly}
104+
%define with_doc 0
105+
%define with_perf 0
106+
%define with_tools 0
107+
%define with_bpftool 0
108+
%define with_vdso_install 0
109+
%endif
110+
111+
%ifarch noarch
112+
%define with_std 0
113+
%define with_headers 0
114+
%define with_perf 0
115+
%define with_tools 0
116+
%define with_bpftool 0
117+
%define with_vdso_install 0
118+
%endif
119+
120+
%ifarch x86_64 || aarch64
121+
%define with_doc 0
122+
### as of {{.KernelPackage}}-6.5.4, no more perf and bpftool -ay
123+
%define with_perf 0
124+
%define with_bpftool 0
125+
%endif
126+
127+
%ifarch x86_64
128+
%define asmarch x86
129+
%define bldarch x86_64
130+
%define hdrarch x86_64
131+
%define make_target bzImage
132+
%define kernel_image arch/x86/boot/bzImage
133+
%endif
134+
135+
%ifarch aarch64
136+
%define asmarch arm64
137+
%define bldarch arm64
138+
%define hdrarch arm64
139+
%define make_target Image.gz
140+
%define kernel_image arch/arm64/boot/Image.gz
141+
%endif
142+
143+
%if %{with_vdso_install}
144+
%define use_vdso 1
145+
%define _use_vdso 1
146+
%else
147+
%define _use_vdso 0
148+
%endif
149+
150+
#
151+
# Packages that need to be installed before the kernel is installed,
152+
# as they will be used by the %%post scripts.
153+
#
154+
%define kernel_ml_prereq coreutils, systemd >= 203-2, /usr/bin/kernel-install
155+
%define initrd_prereq dracut >= 027
156+
157+
Name: kernel
158+
Summary: The Linux kernel. (The core of any Linux kernel based operating system.)
159+
License: GPLv2 and Redistributable, no modification permitted.
160+
URL: https://www.kernel.org/
161+
Version: %{pkg_version}
162+
Release: %{pkg_release}
163+
ExclusiveArch: x86_64 aarch64 noarch
164+
ExclusiveOS: Linux
165+
Provides: kernel = %{version}-%{release}
166+
Provides: installonlypkg(kernel)
167+
Requires: %{name}-core-uname-r = %{KVERREL}
168+
Requires: %{name}-modules-uname-r = %{KVERREL}
169+
170+
%changelog
171+
* Tue Feb 22 2024 Mustafa Gezen - 1.0-123
172+
- Test changelog
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
%changelog
2+
* Tue Feb 22 2024 Mustafa Gezen - 1.0-123
3+
- Test changelog
4+
5+
6+
* Tue Feb 22 2024 Mustafa Gezen - 1.0-123
7+
- msg1 2
8+
- msg2
9+
10+

0 commit comments

Comments
 (0)